Skip to content

Commit

Permalink
Fix Lectern Controllers storing ItemStacks from nbt (Creators-of-Crea…
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Nov 14, 2024
1 parent d39f899 commit 275b30a
Showing 1 changed file with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.UUID;

import com.simibubi.create.AllItems;
import com.simibubi.create.AllSoundEvents;
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour;
Expand All @@ -28,7 +29,7 @@

public class LecternControllerBlockEntity extends SmartBlockEntity {

private ItemStack controller = ItemStack.EMPTY;
private CompoundTag controllerNbt = new CompoundTag();
private UUID user;
private UUID prevUser; // used only on client
private boolean deactivatedThisTick; // used only on server
Expand All @@ -43,26 +44,33 @@ public void addBehaviours(List<BlockEntityBehaviour> behaviours) { }
@Override
protected void write(CompoundTag compound, boolean clientPacket) {
super.write(compound, clientPacket);
compound.put("Controller", controller.save(new CompoundTag()));
compound.put("ControllerData", controllerNbt);
if (user != null)
compound.putUUID("User", user);
}

@Override
public void writeSafe(CompoundTag compound) {
super.writeSafe(compound);
compound.put("Controller", controller.save(new CompoundTag()));
compound.put("ControllerData", controllerNbt);
}

@Override
protected void read(CompoundTag compound, boolean 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;
}

public ItemStack getController() {
return controller;
return getController();
}

public boolean hasUser() { return user != null; }
Expand Down Expand Up @@ -138,7 +146,7 @@ private void tryToggleActive() {
}

public void setController(ItemStack newController) {
controller = newController;
controllerNbt = newController.getTag();
if (newController != null) {
AllSoundEvents.CONTROLLER_PUT.playOnServer(level, worldPosition);
}
Expand All @@ -148,7 +156,7 @@ public void swapControllers(ItemStack stack, Player player, InteractionHand hand
ItemStack newController = stack.copy();
stack.setCount(0);
if (player.getItemInHand(hand).isEmpty()) {
player.setItemInHand(hand, controller);
player.setItemInHand(hand, createLinkedController());
} else {
dropController(state);
}
Expand All @@ -164,10 +172,10 @@ public void dropController(BlockState state) {
double x = worldPosition.getX() + 0.5 + 0.25 * dir.getStepX();
double y = worldPosition.getY() + 1;
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();
level.addFreshEntity(itementity);
controller = null;
controllerNbt = new CompoundTag();
}

public static boolean playerInRange(Player player, Level world, BlockPos pos) {
Expand All @@ -176,4 +184,10 @@ public static boolean playerInRange(Player player, Level world, BlockPos pos) {
return player.distanceToSqr(Vec3.atCenterOf(pos)) < reach * reach;
}

private ItemStack createLinkedController() {
ItemStack stack = AllItems.LINKED_CONTROLLER.asStack();
stack.setTag(controllerNbt);
return stack;
}

}

0 comments on commit 275b30a

Please sign in to comment.