From 1303489a90c3fbb12461aedf4062c118b06d32fb Mon Sep 17 00:00:00 2001 From: jotty Date: Tue, 18 Aug 2020 17:15:51 +0200 Subject: [PATCH] some steps forward in menu container server client stuff --- gradle.properties | 2 +- .../container/BackpackInventory.java | 14 ++++ .../container/BackpackScreenHandler.java | 42 +++++------ .../quickiefabric/init/RegistryManager.java | 2 +- .../quickiefabric/items/ItemBackpack.java | 75 ++++++++----------- 5 files changed, 70 insertions(+), 65 deletions(-) diff --git a/gradle.properties b/gradle.properties index 42296a5..d608f8f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.8.8+build.202 # Mod Properties - mod_version = 1.16.1.7 + mod_version = 1.16.1.8 maven_group = de.jottyfan.minecraft archives_base_name = quickiefabric diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java index ae590ac..18dc9d9 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java @@ -9,6 +9,7 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; +import net.minecraft.util.Hand; import net.minecraft.util.collection.DefaultedList; public class BackpackInventory implements Inventory { @@ -22,6 +23,19 @@ public class BackpackInventory implements Inventory { readItemsFromTag(this.stacks, tag); } + public static BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player) { + ItemStack stack = player.getMainHandStack(); + if (stack != null) { + if (!stack.hasTag()) { + stack.setTag(new CompoundTag()); + } + if (!stack.getTag().contains("backpack")) { + stack.getTag().put("backpack", new CompoundTag()); + } + } + return new BackpackInventory(stack.getTag().getCompound("backpack"), handler); + } + private void readItemsFromTag(DefaultedList inventory, CompoundTag tag) { ListTag listTag = tag.getList("items", 10); for (int i = 0; i < listTag.size(); ++i) { diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java index 51170f3..c62cbda 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java @@ -6,6 +6,7 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; +import net.minecraft.network.PacketByteBuf; import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.slot.Slot; import net.minecraft.util.Hand; @@ -18,14 +19,17 @@ public class BackpackScreenHandler extends ScreenHandler { private int backpackSlots; private Hand hand; - public BackpackScreenHandler(int syncId, PlayerInventory playerInventory) { + public BackpackScreenHandler(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) { super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId); this.player = playerInventory.player; - this.backpackInventory = ItemBackpack.getInventory(this, hand, player); + ItemStack stack = buf.readItemStack(); + this.hand = buf.readInt() == 0 ? Hand.MAIN_HAND : Hand.OFF_HAND; + this.backpackInventory = BackpackInventory.getInventory(this, player); backpackSlots = ItemBackpack.SLOTSIZE; int spacing = 112; + backpackInventory.onOpen(player); for (int y = 0; y < 6; y++) { for (int x = 0; x < 9; ++x) { this.addSlot(new Slot(backpackInventory, x + y * 9, 8 + x * 18, -10 + y * 18)); @@ -39,8 +43,22 @@ public class BackpackScreenHandler extends ScreenHandler { for (int x = 0; x < 9; ++x) { this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 58 + spacing)); } + playerInventory.onOpen(player); + + DefaultedList ad = DefaultedList.ofSize(backpackSlots, ItemStack.EMPTY); + if (ad.size() == 0) { + return; + } + for (int x = 0; x < 9; x++) { + for (int y = 0; y < backpackSlots / 9; y++) { + this.getSlot(x + y * 9).setStack(ad.get(x + y * 9)); + } + } + for (int x = 0; x < (backpackSlots % 9); x++) { + this.getSlot(x + (backpackSlots / 9) * 9).setStack(ad.get(x + (backpackSlots / 9) * 9)); + } } - + public PlayerEntity getPlayer() { return this.player; } @@ -91,24 +109,6 @@ public class BackpackScreenHandler extends ScreenHandler { return copy; } - public BackpackScreenHandler withHand(Hand hand) { - this.hand = hand; - DefaultedList ad = DefaultedList.ofSize(backpackSlots, ItemStack.EMPTY); -// ItemBackpack.getInventory(player.getStackInHand(this.hand), ad); - if (ad.size() == 0) { - return this; - } - for (int x = 0; x < 9; x++) { - for (int y = 0; y < backpackSlots / 9; y++) { - this.getSlot(x + y * 9).setStack(ad.get(x + y * 9)); - } - } - for (int x = 0; x < (backpackSlots % 9); x++) { - this.getSlot(x + (backpackSlots / 9) * 9).setStack(ad.get(x + (backpackSlots / 9) * 9)); - } - return this; - } - public Hand getHand() { return hand; } diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java index 55aea8d..6b3db1a 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java @@ -49,7 +49,7 @@ public class RegistryManager { public static final String QUICKIEFABRIC = "quickiefabric"; public static final Identifier BACKPACK_IDENTIFIER = new Identifier(QUICKIEFABRIC, "backpack"); - public static final ScreenHandlerType BACKPACK_SCREEN_HANDLER = ScreenHandlerRegistry.registerSimple(RegistryManager.BACKPACK_IDENTIFIER, BackpackScreenHandler::new); + public static final ScreenHandlerType BACKPACK_SCREEN_HANDLER = ScreenHandlerRegistry.registerExtended(RegistryManager.BACKPACK_IDENTIFIER, BackpackScreenHandler::new); public static final ItemGroup QUICKIEFABRIC_GROUP = FabricItemGroupBuilder.create(new Identifier(QUICKIEFABRIC, "all")).icon(() -> new ItemStack(QuickieItems.SPEEDPOWDER)) .appendItems(stacks -> { diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBackpack.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBackpack.java index 6c7d1ad..7231b35 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBackpack.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBackpack.java @@ -1,17 +1,18 @@ package de.jottyfan.minecraft.quickiefabric.items; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import de.jottyfan.minecraft.quickiefabric.container.BackpackInventory; import de.jottyfan.minecraft.quickiefabric.container.BackpackScreenHandler; import de.jottyfan.minecraft.quickiefabric.init.RegistryManager; +import io.netty.buffer.Unpooled; +import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.DyeableItem; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.screen.SimpleNamedScreenHandlerFactory; +import net.minecraft.item.ItemUsageContext; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; @@ -24,7 +25,6 @@ import net.minecraft.world.World; * */ public class ItemBackpack extends Item implements DyeableItem { - private final static Logger LOGGER = LogManager.getLogger(ItemBackpack.class); public final static Integer SLOTSIZE = 54; public ItemBackpack() { @@ -34,43 +34,34 @@ public class ItemBackpack extends Item implements DyeableItem { @Override public TypedActionResult use(World world, PlayerEntity player, Hand hand) { if (!world.isClient) { - SimpleNamedScreenHandlerFactory factory = new SimpleNamedScreenHandlerFactory( - (id, inventory, buf) -> new BackpackScreenHandler(id, player.inventory).withHand(hand), - Text.method_30163(RegistryManager.BACKPACK_IDENTIFIER.toString())); - player.openHandledScreen(factory); + ItemStack itemStack = player.getActiveItem(); + player.openHandledScreen(new ExtendedScreenHandlerFactory() { + @Override + public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf packetByteBuf) { + packetByteBuf.writeItemStack(itemStack); + packetByteBuf.writeInt(hand.equals(Hand.MAIN_HAND) ? 0 : 1); + } + + @Override + public Text getDisplayName() { + return Text.method_30163(RegistryManager.BACKPACK_IDENTIFIER.toString()); + } + + @Override + public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { + PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); + buf.writeItemStack(itemStack); + buf.writeInt(hand.equals(Hand.MAIN_HAND) ? 0 : 1); + return new BackpackScreenHandler(syncId, inv, buf); + } + }); } return new TypedActionResult(ActionResult.PASS, player.getStackInHand(hand)); } - - public static BackpackInventory getInventory(BackpackScreenHandler handler, Hand hand, PlayerEntity player) { - ItemStack stack = player.getMainHandStack(); - if (stack != null) { - if (!stack.hasTag()) { - stack.setTag(new CompoundTag()); - } - if (!stack.getTag().contains("backpack")) { - stack.getTag().put("backpack", new CompoundTag()); - } - } else { - LOGGER.warn("player.getMainHandStack() is null"); - } - return new BackpackInventory(stack.getTag().getCompound("backpack"), handler); + + @Override + public ActionResult useOnBlock(ItemUsageContext context) { + // TODO implement unloading by right clicking on a chest; if left click, load as much as possible + return super.useOnBlock(context); } - -// public static void setInventory(ItemStack backpack, DefaultedList list) { -// CompoundTag nbt = backpack.getOrCreateTag(); -// Inventories.toTag(nbt, list); -// backpack.setTag(nbt); -// } -// -// public static void getInventory(ItemStack backpack, DefaultedList defaultedList) { -// CompoundTag nbt; -// if (backpack.hasTag()) { -// nbt = backpack.getTag(); -// } else { -// nbt = new CompoundTag(); -// } -// Inventories.fromTag(nbt, defaultedList); -// backpack.setTag(nbt); -// } }