more backpack stuff

This commit is contained in:
Jörg Henke 2020-08-18 21:49:47 +02:00
parent 1303489a90
commit fd41db811b
3 changed files with 3 additions and 12 deletions

View File

@ -9,7 +9,6 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents; import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand;
import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.collection.DefaultedList;
public class BackpackInventory implements Inventory { public class BackpackInventory implements Inventory {
@ -17,13 +16,13 @@ public class BackpackInventory implements Inventory {
private final DefaultedList<ItemStack> stacks; private final DefaultedList<ItemStack> stacks;
private final BackpackScreenHandler handler; private final BackpackScreenHandler handler;
public BackpackInventory(CompoundTag tag, BackpackScreenHandler handler) { private BackpackInventory(CompoundTag tag, BackpackScreenHandler handler) {
this.handler = handler; this.handler = handler;
this.stacks = DefaultedList.ofSize(SECTION_SIZE * ItemBackpack.SLOTSIZE, ItemStack.EMPTY); this.stacks = DefaultedList.ofSize(SECTION_SIZE * ItemBackpack.SLOTSIZE, ItemStack.EMPTY);
readItemsFromTag(this.stacks, tag); readItemsFromTag(this.stacks, tag);
} }
public static BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player) { public static final BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player) {
ItemStack stack = player.getMainHandStack(); ItemStack stack = player.getMainHandStack();
if (stack != null) { if (stack != null) {
if (!stack.hasTag()) { if (!stack.hasTag()) {

View File

@ -17,13 +17,11 @@ public class BackpackScreenHandler extends ScreenHandler {
private final BackpackInventory backpackInventory; private final BackpackInventory backpackInventory;
private final PlayerEntity player; private final PlayerEntity player;
private int backpackSlots; private int backpackSlots;
private Hand hand;
public BackpackScreenHandler(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) { public BackpackScreenHandler(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) {
super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId); super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId);
this.player = playerInventory.player; this.player = playerInventory.player;
ItemStack stack = buf.readItemStack(); ItemStack stack = buf.readItemStack();
this.hand = buf.readInt() == 0 ? Hand.MAIN_HAND : Hand.OFF_HAND;
this.backpackInventory = BackpackInventory.getInventory(this, player); this.backpackInventory = BackpackInventory.getInventory(this, player);
backpackSlots = ItemBackpack.SLOTSIZE; backpackSlots = ItemBackpack.SLOTSIZE;
@ -108,8 +106,4 @@ public class BackpackScreenHandler extends ScreenHandler {
} }
return copy; return copy;
} }
public Hand getHand() {
return hand;
}
} }

View File

@ -39,7 +39,6 @@ public class ItemBackpack extends Item implements DyeableItem {
@Override @Override
public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf packetByteBuf) { public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf packetByteBuf) {
packetByteBuf.writeItemStack(itemStack); packetByteBuf.writeItemStack(itemStack);
packetByteBuf.writeInt(hand.equals(Hand.MAIN_HAND) ? 0 : 1);
} }
@Override @Override
@ -51,7 +50,6 @@ public class ItemBackpack extends Item implements DyeableItem {
public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeItemStack(itemStack); buf.writeItemStack(itemStack);
buf.writeInt(hand.equals(Hand.MAIN_HAND) ? 0 : 1);
return new BackpackScreenHandler(syncId, inv, buf); return new BackpackScreenHandler(syncId, inv, buf);
} }
}); });