Fix Lectern Controllers storing ItemStacks from nbt (#7150)

This commit is contained in:
IThundxr 2025-01-10 12:11:57 -05:00 committed by GitHub
parent b3662c45f4
commit 76668d9f9b
Failed to generate hash of commit

View file

@ -3,6 +3,7 @@ package com.simibubi.create.content.redstone.link.controller;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import com.simibubi.create.AllItems;
import com.simibubi.create.AllSoundEvents; import com.simibubi.create.AllSoundEvents;
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity; import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour; import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour;
@ -28,7 +29,7 @@ import net.minecraftforge.fml.DistExecutor;
public class LecternControllerBlockEntity extends SmartBlockEntity { public class LecternControllerBlockEntity extends SmartBlockEntity {
private ItemStack controller = ItemStack.EMPTY; private CompoundTag controllerNbt = new CompoundTag();
private UUID user; private UUID user;
private UUID prevUser; // used only on client private UUID prevUser; // used only on client
private boolean deactivatedThisTick; // used only on server private boolean deactivatedThisTick; // used only on server
@ -43,7 +44,7 @@ public class LecternControllerBlockEntity extends SmartBlockEntity {
@Override @Override
protected void write(CompoundTag compound, boolean clientPacket) { protected void write(CompoundTag compound, boolean clientPacket) {
super.write(compound, clientPacket); super.write(compound, clientPacket);
compound.put("Controller", controller.save(new CompoundTag())); compound.put("ControllerData", controllerNbt);
if (user != null) if (user != null)
compound.putUUID("User", user); compound.putUUID("User", user);
} }
@ -51,18 +52,25 @@ public class LecternControllerBlockEntity extends SmartBlockEntity {
@Override @Override
public void writeSafe(CompoundTag compound) { public void writeSafe(CompoundTag compound) {
super.writeSafe(compound); super.writeSafe(compound);
compound.put("Controller", controller.save(new CompoundTag())); compound.put("ControllerData", controllerNbt);
} }
@Override @Override
protected void read(CompoundTag compound, boolean clientPacket) { protected void read(CompoundTag compound, boolean clientPacket) {
super.read(compound, clientPacket); super.read(compound, clientPacket);
controller = ItemStack.of(compound.getCompound("Controller"));
// Migrate old data if that is found
if (compound.contains("Controller")) {
controllerNbt = ItemStack.of(compound.getCompound("Controller")).getTag();
} else {
controllerNbt = compound.getCompound("ControllerData");
}
user = compound.hasUUID("User") ? compound.getUUID("User") : null; user = compound.hasUUID("User") ? compound.getUUID("User") : null;
} }
public ItemStack getController() { public ItemStack getController() {
return controller; return getController();
} }
public boolean hasUser() { return user != null; } public boolean hasUser() { return user != null; }
@ -138,8 +146,8 @@ public class LecternControllerBlockEntity extends SmartBlockEntity {
} }
public void setController(ItemStack newController) { public void setController(ItemStack newController) {
controller = newController;
if (newController != null) { if (newController != null) {
controllerNbt = newController.getTag();
AllSoundEvents.CONTROLLER_PUT.playOnServer(level, worldPosition); AllSoundEvents.CONTROLLER_PUT.playOnServer(level, worldPosition);
} }
} }
@ -148,7 +156,7 @@ public class LecternControllerBlockEntity extends SmartBlockEntity {
ItemStack newController = stack.copy(); ItemStack newController = stack.copy();
stack.setCount(0); stack.setCount(0);
if (player.getItemInHand(hand).isEmpty()) { if (player.getItemInHand(hand).isEmpty()) {
player.setItemInHand(hand, controller); player.setItemInHand(hand, createLinkedController());
} else { } else {
dropController(state); dropController(state);
} }
@ -164,10 +172,10 @@ public class LecternControllerBlockEntity extends SmartBlockEntity {
double x = worldPosition.getX() + 0.5 + 0.25 * dir.getStepX(); double x = worldPosition.getX() + 0.5 + 0.25 * dir.getStepX();
double y = worldPosition.getY() + 1; double y = worldPosition.getY() + 1;
double z = worldPosition.getZ() + 0.5 + 0.25 * dir.getStepZ(); double z = worldPosition.getZ() + 0.5 + 0.25 * dir.getStepZ();
ItemEntity itementity = new ItemEntity(level, x, y, z, controller.copy()); ItemEntity itementity = new ItemEntity(level, x, y, z, createLinkedController());
itementity.setDefaultPickUpDelay(); itementity.setDefaultPickUpDelay();
level.addFreshEntity(itementity); level.addFreshEntity(itementity);
controller = null; controllerNbt = new CompoundTag();
} }
public static boolean playerInRange(Player player, Level world, BlockPos pos) { public static boolean playerInRange(Player player, Level world, BlockPos pos) {
@ -176,4 +184,10 @@ public class LecternControllerBlockEntity extends SmartBlockEntity {
return player.distanceToSqr(Vec3.atCenterOf(pos)) < reach * reach; return player.distanceToSqr(Vec3.atCenterOf(pos)) < reach * reach;
} }
private ItemStack createLinkedController() {
ItemStack stack = AllItems.LINKED_CONTROLLER.asStack();
stack.setTag(controllerNbt);
return stack;
}
} }