SPIGOT-7313: More accurately edit data on Jukeboxes

By: Parker Hawke <hawkeboyz2@hotmail.com>
This commit is contained in:
CraftBukkit/Spigot
2023-03-29 19:23:41 +11:00
parent 8a53e8b4eb
commit 26ae05b48f
2 changed files with 65 additions and 20 deletions

View File

@@ -2,7 +2,6 @@ package org.bukkit.craftbukkit.block;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.BlockJukeBox;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.TileEntity;
import net.minecraft.world.level.block.entity.TileEntityJukeBox;
import org.bukkit.Effect;
@@ -40,14 +39,19 @@ public class CraftJukebox extends CraftBlockEntityState<TileEntityJukeBox> imple
boolean result = super.update(force, applyPhysics);
if (result && this.isPlaced() && this.getType() == Material.JUKEBOX) {
CraftWorld world = (CraftWorld) this.getWorld();
Material record = this.getPlaying();
if (record == Material.AIR) {
getWorldHandle().setBlock(this.getPosition(), Blocks.JUKEBOX.defaultBlockState().setValue(BlockJukeBox.HAS_RECORD, false), 3);
} else {
getWorldHandle().setBlock(this.getPosition(), Blocks.JUKEBOX.defaultBlockState().setValue(BlockJukeBox.HAS_RECORD, true), 3);
getWorldHandle().setBlock(this.getPosition(), data, 3);
TileEntity tileEntity = this.getTileEntityFromWorld();
if (tileEntity instanceof TileEntityJukeBox jukebox) {
CraftWorld world = (CraftWorld) this.getWorld();
if (record.isAir()) {
jukebox.setRecordWithoutPlaying(ItemStack.EMPTY);
world.playEffect(this.getLocation(), Effect.IRON_DOOR_CLOSE, 0); // TODO: Fix this enum constant. This stops jukeboxes
} else {
world.playEffect(this.getLocation(), Effect.RECORD_PLAY, record);
}
}
world.playEffect(this.getLocation(), Effect.RECORD_PLAY, record);
}
return result;
@@ -67,6 +71,11 @@ public class CraftJukebox extends CraftBlockEntityState<TileEntityJukeBox> imple
setRecord(new org.bukkit.inventory.ItemStack(record));
}
@Override
public boolean hasRecord() {
return getHandle().getValue(BlockJukeBox.HAS_RECORD) && !getPlaying().isAir();
}
@Override
public org.bukkit.inventory.ItemStack getRecord() {
ItemStack record = this.getSnapshot().getFirstItem();
@@ -76,22 +85,54 @@ public class CraftJukebox extends CraftBlockEntityState<TileEntityJukeBox> imple
@Override
public void setRecord(org.bukkit.inventory.ItemStack record) {
ItemStack nms = CraftItemStack.asNMSCopy(record);
this.getSnapshot().setRecordWithoutPlaying(nms);
if (nms.isEmpty()) {
this.data = this.data.setValue(BlockJukeBox.HAS_RECORD, false);
} else {
this.data = this.data.setValue(BlockJukeBox.HAS_RECORD, true);
}
TileEntityJukeBox snapshot = this.getSnapshot();
snapshot.setRecordWithoutPlaying(nms);
snapshot.recordStartedTick = snapshot.tickCount;
snapshot.isPlaying = !nms.isEmpty();
this.data = this.data.setValue(BlockJukeBox.HAS_RECORD, !nms.isEmpty());
}
@Override
public boolean isPlaying() {
return getHandle().getValue(BlockJukeBox.HAS_RECORD);
requirePlaced();
TileEntity tileEntity = this.getTileEntityFromWorld();
return tileEntity instanceof TileEntityJukeBox jukebox && jukebox.isRecordPlaying();
}
@Override
public boolean startPlaying() {
requirePlaced();
TileEntity tileEntity = this.getTileEntityFromWorld();
if (!(tileEntity instanceof TileEntityJukeBox jukebox)) {
return false;
}
ItemStack record = jukebox.getFirstItem();
if (record.isEmpty() || isPlaying()) {
return false;
}
jukebox.isPlaying = true;
jukebox.recordStartedTick = jukebox.tickCount;
getWorld().playEffect(getLocation(), Effect.RECORD_PLAY, CraftMagicNumbers.getMaterial(record.getItem()));
return true;
}
@Override
public void stopPlaying() {
getWorld().playEffect(getLocation(), Effect.RECORD_PLAY, Material.AIR);
requirePlaced();
TileEntity tileEntity = this.getTileEntityFromWorld();
if (!(tileEntity instanceof TileEntityJukeBox jukebox)) {
return;
}
jukebox.isPlaying = false;
getWorld().playEffect(getLocation(), Effect.IRON_DOOR_CLOSE, 0); // TODO: Fix this enum constant. This stops jukeboxes
}
@Override