Skip to content

Commit da69e6c

Browse files
committed
Added slot textures to /open, added villager inventory to /open, made lectern has_book blockstate get updated by /open, fixed occasional disconnect from packet encode/decode
1 parent 9069694 commit da69e6c

File tree

17 files changed

+70
-45
lines changed

17 files changed

+70
-45
lines changed

src/main/java/com/luneruniverse/minecraft/mod/nbteditor/containers/ArmorHandsContainerIO.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ public boolean isSupported(NbtCompound container) {
1818
NbtElement armorItemsNbtElement = container.get("ArmorItems");
1919
if (armorItemsNbtElement != null) {
2020
if (!(armorItemsNbtElement instanceof NbtList armorItemsNbt) ||
21-
armorItemsNbt.nbte$getHeldType().filter(heldType -> heldType == NbtElement.COMPOUND_TYPE).isEmpty() ||
22-
armorItemsNbt.size() > 4) {
21+
armorItemsNbt.size() > 4 ||
22+
armorItemsNbt.nbte$getHeldType().filter(
23+
heldType -> heldType == 0 || heldType == NbtElement.COMPOUND_TYPE).isEmpty()) {
2324
return false;
2425
}
2526
}
2627

2728
NbtElement handItemsNbtElement = container.get("HandItems");
2829
if (handItemsNbtElement != null) {
2930
if (!(handItemsNbtElement instanceof NbtList handItemsNbt) ||
30-
handItemsNbt.nbte$getHeldType().filter(heldType -> heldType == NbtElement.COMPOUND_TYPE).isEmpty() ||
31-
handItemsNbt.size() > 2) {
31+
handItemsNbt.size() > 2 ||
32+
handItemsNbt.nbte$getHeldType().filter(
33+
heldType -> heldType == 0 || heldType == NbtElement.COMPOUND_TYPE).isEmpty()) {
3234
return false;
3335
}
3436
}

src/main/java/com/luneruniverse/minecraft/mod/nbteditor/containers/ContainerIOs.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public static void registerItemEntityIO(Item item, EntityType<?> entity, Functio
7575
private static final ItemBlockContainerIO DISPENSER_IO = ItemBlockContainerIO.forSlotKeyItems(9);
7676
private static final ItemBlockContainerIO HOPPER_IO = ItemBlockContainerIO.forSlotKeyItems(5);
7777
private static final ItemBlockContainerIO JUKEBOX_IO = ItemBlockContainerIO.forKeys(BlockEntityType.JUKEBOX, "RecordItem");
78-
private static final ItemBlockContainerIO LECTERN_IO = ItemBlockContainerIO.forKeys(BlockEntityType.LECTERN, "Book");
78+
private static final ItemBlockContainerIO LECTERN_IO = BlockStateUpdatingContainerIO.forItemBlock(
79+
ItemBlockContainerIO.forKeys(BlockEntityType.LECTERN, "Book"), "has_book");
7980
private static final Function<EntityType<?>, ItemEntityContainerIO> ITEM_FRAME_IO =
8081
entityId -> ItemEntityContainerIO.forKeys(entityId, "Item");
8182
private static final ContainerIO<ItemStack> BUNDLE_IO = Version.<ContainerIO<ItemStack>>newSwitch()
@@ -142,6 +143,9 @@ public static void registerItemEntityIO(Item item, EntityType<?> entity, Functio
142143
.range(null, "1.20.4", () -> new ConcatContainerIO<>(
143144
new ArmorHandsContainerIO(), new KeysContainerIO(false, "DecorItem"), new DonkeyChestContainerIO(true)))
144145
.get());
146+
private static final ContainerIO<LocalEntity> VILLAGER_IO = new ConcatContainerIO<>(
147+
EQUIPMENT_IO.apply(EntityType.VILLAGER).entity(),
148+
ContainerIO.forLocalNBT(new OrderNbtListContainerIO(8).forNbtCompound("Inventory")));
145149
private static final ContainerIO<LocalEntity> CHEST_BOAT_IO =
146150
ContainerIO.forLocalNBT(new SlotKeyNbtListContainerIO(27).forNbtCompoundItems());
147151

@@ -208,6 +212,7 @@ public static void loadClass() {}
208212
registerEntityIO(EntityType.MULE, DONKEY_IO);
209213
registerEntityIO(EntityType.LLAMA, LLAMA_IO);
210214
registerEntityIO(EntityType.TRADER_LLAMA, LLAMA_IO);
215+
registerEntityIO(EntityType.VILLAGER, VILLAGER_IO);
211216
MVClientNetworking.PlayNetworkStateEvents.Join.EVENT.register(() -> {
212217
for (EntityType<?> entityType : MVRegistry.ENTITY_TYPE) {
213218
if (ENTITY_IO.containsKey(entityType))

src/main/java/com/luneruniverse/minecraft/mod/nbteditor/containers/OrderNbtListContainerIO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public ContainerIO<NbtCompound> forNbtCompoundItems() {
2929

3030
@Override
3131
public boolean isSupported(NbtList container) {
32-
return container.size() <= maxSlots &&
33-
container.nbte$getHeldType().filter(heldType -> heldType == NbtElement.COMPOUND_TYPE).isPresent();
32+
return container.size() <= maxSlots && container.nbte$getHeldType().filter(
33+
heldType -> heldType == 0 || heldType == NbtElement.COMPOUND_TYPE).isPresent();
3434
}
3535

3636
@Override

src/main/java/com/luneruniverse/minecraft/mod/nbteditor/localnbt/LocalEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public Text toHoverableText() {
203203

204204
public CompletableFuture<Optional<EntityReference>> summon(RegistryKey<World> world, Vec3d pos) {
205205
return NBTEditorClient.SERVER_CONN
206-
.sendRequest(requestId -> new SummonEntityC2SPacket(requestId, world, pos, getId(), nbt), ViewEntityS2CPacket.class)
206+
.sendRequest(requestId -> new SummonEntityC2SPacket(requestId, world, pos, getId(), nbt.copy()), ViewEntityS2CPacket.class)
207207
.thenApply(optional -> optional.filter(ViewEntityS2CPacket::foundEntity)
208208
.map(packet -> {
209209
EntityReference ref = new EntityReference(packet.getWorld(), packet.getUUID(),

src/main/java/com/luneruniverse/minecraft/mod/nbteditor/nbtreferences/BlockReference.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void saveLocalNBT(LocalBlock block, Runnable onFinished) {
8282
this.block = block.getBlock();
8383
this.state = block.getState();
8484
this.nbt = block.getNBT();
85-
MVClientNetworking.send(new SetBlockC2SPacket(world, pos, block.getId(), state, nbt,
85+
MVClientNetworking.send(new SetBlockC2SPacket(world, pos, block.getId(), state.copy(), nbt.copy(),
8686
ConfigScreen.isRecreateBlocksAndEntities(), ConfigScreen.isTriggerBlockUpdates()));
8787
onFinished.run();
8888
}
@@ -99,7 +99,7 @@ public NbtCompound getNBT() {
9999
public void saveNBT(Identifier id, NbtCompound toSave, Runnable onFinished) {
100100
this.block = MVRegistry.BLOCK.get(id);
101101
this.nbt = toSave;
102-
MVClientNetworking.send(new SetBlockC2SPacket(world, pos, id, state, toSave,
102+
MVClientNetworking.send(new SetBlockC2SPacket(world, pos, id, state.copy(), toSave.copy(),
103103
ConfigScreen.isRecreateBlocksAndEntities(), ConfigScreen.isTriggerBlockUpdates()));
104104
onFinished.run();
105105
}
@@ -113,7 +113,7 @@ public BlockStateProperties getState() {
113113
}
114114
public void saveState(BlockStateProperties state, Runnable onFinished) {
115115
this.state = state;
116-
MVClientNetworking.send(new SetBlockC2SPacket(world, pos, MVRegistry.BLOCK.getId(block), state, nbt,
116+
MVClientNetworking.send(new SetBlockC2SPacket(world, pos, MVRegistry.BLOCK.getId(block), state.copy(), nbt.copy(),
117117
ConfigScreen.isRecreateBlocksAndEntities(), ConfigScreen.isTriggerBlockUpdates()));
118118
onFinished.run();
119119
}

src/main/java/com/luneruniverse/minecraft/mod/nbteditor/nbtreferences/EntityReference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public NbtCompound getNBT() {
7070
public void saveNBT(Identifier id, NbtCompound toSave, Runnable onFinished) {
7171
this.entityType = MVRegistry.ENTITY_TYPE.get(id);
7272
this.nbt = toSave;
73-
MVClientNetworking.send(new SetEntityC2SPacket(world, uuid, id, toSave,
73+
MVClientNetworking.send(new SetEntityC2SPacket(world, uuid, id, toSave.copy(),
7474
ConfigScreen.isRecreateBlocksAndEntities()));
7575
onFinished.run();
7676
}

src/main/java/com/luneruniverse/minecraft/mod/nbteditor/nbtreferences/itemreferences/ServerItemReference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ItemStack getItem() {
4444
public void saveItem(ItemStack toSave, Runnable onFinished) {
4545
screen.getScreenHandler().getSlot(slot).setStackNoCallbacks(toSave);
4646
if (NBTEditorClient.SERVER_CONN.isContainerScreen())
47-
MVClientNetworking.send(new SetSlotC2SPacket(slot, toSave));
47+
MVClientNetworking.send(new SetSlotC2SPacket(slot, toSave.copy()));
4848
onFinished.run();
4949
}
5050

src/main/java/com/luneruniverse/minecraft/mod/nbteditor/screens/containers/ClientChestScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void show() {
7373
private ButtonWidget nextPageJump;
7474

7575
private ClientChestScreen() {
76-
super(new ClientScreenHandler(6), TextInst.translatable("nbteditor.client_chest"));
76+
super(6, TextInst.translatable("nbteditor.client_chest"));
7777
}
7878
private void setPageData(ClientChestPage pageData) {
7979
ItemStack[] items = pageData.getItemsOrThrow();

src/main/java/com/luneruniverse/minecraft/mod/nbteditor/screens/containers/ClientHandledScreen.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import net.minecraft.client.util.math.MatrixStack;
3030
import net.minecraft.item.ItemStack;
3131
import net.minecraft.item.Items;
32-
import net.minecraft.screen.GenericContainerScreenHandler;
3332
import net.minecraft.screen.slot.Slot;
3433
import net.minecraft.screen.slot.SlotActionType;
3534
import net.minecraft.text.Text;
@@ -79,11 +78,16 @@ public static boolean handleKeybind(int keyCode, ItemStack item, ItemReference r
7978

8079
private ServerInventoryManager serverInv;
8180

82-
protected ClientHandledScreen(GenericContainerScreenHandler handler, Text title) {
83-
super(handler, MainUtil.client.player.getInventory(), title);
81+
protected ClientHandledScreen(int rows, Text title) {
82+
super(new ClientScreenHandler(rows), MainUtil.client.player.getInventory(), title);
83+
((ClientScreenHandler) handler).setScreen(this);
8484
handler.disableSyncing();
8585
}
8686

87+
protected void setSlotTextures(Identifier... textures) {
88+
((ClientScreenHandler) handler).setSlotTextures(textures);
89+
}
90+
8791
public ServerInventoryManager getServerInventoryManager() {
8892
return serverInv;
8993
}
@@ -200,7 +204,7 @@ protected void onMouseClick(Slot slot, int slotId, int button, SlotActionType ac
200204
}
201205
case QUICK_MOVE -> {
202206
ItemStack prevItem = slot.getStack().copy();
203-
LockableSlot.unlockDuring(() -> handler.onSlotClick(slot.id, button, actionType, MainUtil.client.player));
207+
ClientScreenHandlerSlot.unlockDuring(() -> handler.onSlotClick(slot.id, button, actionType, MainUtil.client.player));
204208
slot.setStackNoCallbacks(prevItem);
205209
serverInv.updateServer();
206210
}

src/main/java/com/luneruniverse/minecraft/mod/nbteditor/screens/containers/ClientScreenHandler.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.minecraft.screen.GenericContainerScreenHandler;
1313
import net.minecraft.screen.ScreenHandlerType;
1414
import net.minecraft.screen.slot.Slot;
15+
import net.minecraft.util.Identifier;
1516

1617
public class ClientScreenHandler extends GenericContainerScreenHandler {
1718

@@ -28,7 +29,16 @@ public ClientScreenHandler(int rows) {
2829
default -> throw new IllegalArgumentException("Invalid row count: " + rows);
2930
}, SYNC_ID, MainUtil.client.player.getInventory(), new SimpleInventory(rows * 9), rows);
3031

31-
slots.replaceAll(LockableSlot::new);
32+
slots.replaceAll(ClientScreenHandlerSlot::new);
33+
}
34+
35+
public void setScreen(ClientHandledScreen screen) {
36+
slots.forEach(slot -> ((ClientScreenHandlerSlot) slot).setScreen(screen));
37+
}
38+
39+
public void setSlotTextures(Identifier... textures) {
40+
for (int i = 0; i < textures.length; i++)
41+
((ClientScreenHandlerSlot) slots.get(i)).setTexture(textures[i]);
3242
}
3343

3444
@Override

0 commit comments

Comments
 (0)