From 66854d5466f343ec5b4e11b9a76f93d8b68593b0 Mon Sep 17 00:00:00 2001 From: jottyfan Date: Sat, 15 Aug 2020 18:25:11 +0200 Subject: [PATCH] proceeding the backpack --- .../container/BackpackInventory.java | 63 ++++++++++++++++-- .../container/BackpackScreen.java | 7 ++ .../container/BackpackScreenHandler.java | 55 ++++++--------- .../quickiefabric/init/RegistryManager.java | 4 +- .../quickiefabric/items/ItemBackpack.java | 55 ++++++++------- .../quickiefabric/textures/item/backpack.png | Bin 1898 -> 1907 bytes 6 files changed, 120 insertions(+), 64 deletions(-) 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 6039642..ae590ac 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java @@ -5,16 +5,50 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.Inventories; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvents; import net.minecraft.util.collection.DefaultedList; public class BackpackInventory implements Inventory { public static final int SECTION_SIZE = 9; private final DefaultedList stacks; - private BackpackScreenHandler container; - - public BackpackInventory(BackpackScreenHandler container) { + private final BackpackScreenHandler handler; + + public BackpackInventory(CompoundTag tag, BackpackScreenHandler handler) { + this.handler = handler; this.stacks = DefaultedList.ofSize(SECTION_SIZE * ItemBackpack.SLOTSIZE, ItemStack.EMPTY); - this.container = container; + readItemsFromTag(this.stacks, tag); + } + + private void readItemsFromTag(DefaultedList inventory, CompoundTag tag) { + ListTag listTag = tag.getList("items", 10); + for (int i = 0; i < listTag.size(); ++i) { + CompoundTag compoundTag = listTag.getCompound(i); + int j = compoundTag.getInt("slot"); + + if (j >= 0 && j < inventory.size()) { + inventory.set(j, ItemStack.fromTag(compoundTag)); + } + } + } + + private CompoundTag writeItemsToTag() { + DefaultedList inventory = stacks; + ListTag listTag = new ListTag(); + for (int i = 0; i < inventory.size(); ++i) { + ItemStack itemStack = (ItemStack) inventory.get(i); + if (!itemStack.isEmpty()) { + CompoundTag compoundTag = new CompoundTag(); + compoundTag.putInt("slot", i); + itemStack.toTag(compoundTag); + listTag.add(compoundTag); + } + } + CompoundTag tag = new CompoundTag(); + tag.put("items", listTag); + return tag; } public DefaultedList getList(DefaultedList dl) { @@ -75,6 +109,25 @@ public class BackpackInventory implements Inventory { @Override public void markDirty() { - this.container.onContentChanged(this); + handler.onContentChanged(this); + } + + @Override + public void onOpen(PlayerEntity player) { + Inventory.super.onOpen(player); + player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f); + } + + @Override + public void onClose(PlayerEntity player) { + Inventory.super.onClose(player); + ItemStack stack = player.getMainHandStack(); + if (stack != null) { + if (!stack.hasTag()) { + stack.setTag(new CompoundTag()); + } + stack.getTag().put("backpack", writeItemsToTag()); + } + player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f); } } diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreen.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreen.java index e51b17a..97cdca4 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreen.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreen.java @@ -63,4 +63,11 @@ public class BackpackScreen extends HandledScreen impleme drawSlots(matrices, guiX, guiY); } + + @Override + protected void drawForeground(MatrixStack matrixStack, int i, int j) + { + this.textRenderer.draw(matrixStack, this.title, 8.0F, -20.0F, 4210752); + this.textRenderer.draw(matrixStack, this.playerInventory.getDisplayName(), 8.0F, 101f, 4210752); + } } 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 1d9ff46..51170f3 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackScreenHandler.java @@ -8,7 +8,6 @@ import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.slot.Slot; -import net.minecraft.screen.slot.SlotActionType; import net.minecraft.util.Hand; import net.minecraft.util.collection.DefaultedList; @@ -22,34 +21,28 @@ public class BackpackScreenHandler extends ScreenHandler { public BackpackScreenHandler(int syncId, PlayerInventory playerInventory) { super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId); this.player = playerInventory.player; - this.backpackInventory = new BackpackInventory(this); + this.backpackInventory = ItemBackpack.getInventory(this, hand, player); backpackSlots = ItemBackpack.SLOTSIZE; - int spacing; - if (backpackSlots % 9 == 0) { - spacing = 30 + (backpackSlots / 9) * 18 + ((backpackSlots / 9) < 5 ? 0 : 2); - } else { - spacing = 30 + (backpackSlots / 9 + 1) * 18 + ((backpackSlots / 9) < 5 ? 0 : 2); - } - for (int y = 0; y < (backpackSlots / 9); y++) { + int spacing = 112; + + 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, 18 + y * 18)); + this.addSlot(new Slot(backpackInventory, x + y * 9, 8 + x * 18, -10 + y * 18)); } } - if ((backpackSlots % 9) != 0) { - for (int x = 0; x < (backpackSlots % 9); x++) { - this.addSlot( - new Slot(backpackInventory, x + (backpackSlots / 9) * 9, 8 + x * 18, 18 + (backpackSlots / 9) * 18)); - } - for (int y = 0; y < 3; ++y) { - for (int x = 0; x < 9; ++x) { - this.addSlot(new Slot(playerInventory, x + y * 9 + 9, 8 + x * 18, spacing + y * 18)); - } - } + for (int y = 0; y < 3; ++y) { for (int x = 0; x < 9; ++x) { - this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 58 + spacing)); + this.addSlot(new Slot(playerInventory, x + y * 9 + 9, 8 + x * 18, spacing + y * 18)); } } + for (int x = 0; x < 9; ++x) { + this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 58 + spacing)); + } + } + + public PlayerEntity getPlayer() { + return this.player; } @Override @@ -57,6 +50,7 @@ public class BackpackScreenHandler extends ScreenHandler { return true; } + @Override public void onContentChanged(Inventory inv) { super.onContentChanged(inv); if (inv == this.backpackInventory) { @@ -64,6 +58,7 @@ public class BackpackScreenHandler extends ScreenHandler { } } + @Override public void close(PlayerEntity player) { super.close(player); this.backpackInventory.onClose(player); @@ -87,31 +82,19 @@ public class BackpackScreenHandler extends ScreenHandler { } else if (!this.insertItem(clickedStack, 0, backpackSlots, false)) { return ItemStack.EMPTY; } - if (clickedStack.isEmpty()) { clickedSlot.setStack(ItemStack.EMPTY); } else { clickedSlot.markDirty(); } } - return copy; } - @Override - public ItemStack onSlotClick(int i, int j, SlotActionType slotActionType, PlayerEntity playerEntity) { - if (i > 0) { - if (this.getSlot(i).getStack().equals(playerEntity.getStackInHand(hand))) { - return ItemStack.EMPTY; - } - } - return super.onSlotClick(i, j, slotActionType, playerEntity); - } - public BackpackScreenHandler withHand(Hand hand) { this.hand = hand; DefaultedList ad = DefaultedList.ofSize(backpackSlots, ItemStack.EMPTY); - ItemBackpack.getInventory(player.getStackInHand(this.hand), ad); +// ItemBackpack.getInventory(player.getStackInHand(this.hand), ad); if (ad.size() == 0) { return this; } @@ -125,4 +108,8 @@ public class BackpackScreenHandler extends ScreenHandler { } 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 3e67adb..55aea8d 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java @@ -48,8 +48,8 @@ public class RegistryManager { public static final String QUICKIEFABRIC = "quickiefabric"; - public static final Identifier BACKPACK_CONTAINTER = new Identifier(QUICKIEFABRIC, "backpack"); - public static final ScreenHandlerType BACKPACK_SCREEN_HANDLER = ScreenHandlerRegistry.registerSimple(RegistryManager.BACKPACK_CONTAINTER, BackpackScreenHandler::new); + 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 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 0112239..6c7d1ad 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBackpack.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBackpack.java @@ -3,10 +3,10 @@ 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 net.minecraft.entity.player.PlayerEntity; -import net.minecraft.inventory.Inventories; import net.minecraft.item.DyeableItem; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -15,10 +15,7 @@ import net.minecraft.screen.SimpleNamedScreenHandlerFactory; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; -import net.minecraft.util.Identifier; import net.minecraft.util.TypedActionResult; -import net.minecraft.util.collection.DefaultedList; -import net.minecraft.util.registry.Registry; import net.minecraft.world.World; /** @@ -27,7 +24,7 @@ import net.minecraft.world.World; * */ public class ItemBackpack extends Item implements DyeableItem { - private static final Logger LOGGER = LogManager.getLogger(ItemBackpack.class); + private final static Logger LOGGER = LogManager.getLogger(ItemBackpack.class); public final static Integer SLOTSIZE = 54; public ItemBackpack() { @@ -37,31 +34,43 @@ public class ItemBackpack extends Item implements DyeableItem { @Override public TypedActionResult use(World world, PlayerEntity player, Hand hand) { if (!world.isClient) { - for (Identifier id : Registry.SCREEN_HANDLER.getIds()) { - LOGGER.info("found registered screen {}", id.toString()); - } SimpleNamedScreenHandlerFactory factory = new SimpleNamedScreenHandlerFactory( (id, inventory, buf) -> new BackpackScreenHandler(id, player.inventory).withHand(hand), - Text.method_30163(RegistryManager.BACKPACK_CONTAINTER.toString())); + Text.method_30163(RegistryManager.BACKPACK_IDENTIFIER.toString())); player.openHandledScreen(factory); } return new TypedActionResult(ActionResult.PASS, player.getStackInHand(hand)); } - public static void setInventory(ItemStack backpack, DefaultedList list) { - CompoundTag nbt = backpack.getOrCreateTag(); - Inventories.toTag(nbt, list); - backpack.setTag(nbt); + 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); } - 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); - } +// 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); +// } } diff --git a/src/main/resources/assets/quickiefabric/textures/item/backpack.png b/src/main/resources/assets/quickiefabric/textures/item/backpack.png index e79537d9dbaf68a76d81ed7766c085ac24d79251..9ec749a8ce3077066703d7247be0149767fd864b 100644 GIT binary patch delta 1856 zcmV-G2fz604)YF>B!BXHR9JLUVRs;Ka&Km7Y-J#Hd2nSQWq4_3004N}#aCOh<2VTY z*D7WS0`y#tUasmL%<|{R7E0pWbl;xosz1AUkIK{o^oy;b4+roR2Bw5G)Rt zUFHHI+3P6hm8?6j>&B|^qBnbjp%RR8-KMbg1A2x%FMV5n3V-ze$x$9_qP|X)<%rV? zpb=Lfp(}u0j!jvPgjZyD-PdbbHslnam(Z5|vF6V*;AiH=N#x)IMTe_n8)L|j6I&Ni zw>}d+8t3_(uglTx-l!=fCmup#aZNKrxqbz|ge!@M4V|(`z|~K?nJLg>V4Vhu%CxdgAsJIfNbgML+5ev5_+h-*NPG z9OGJDaREyXYf-wCQ-WQc)jFRFI3s@ErdjNHW}FEv(ZHh)l2x3IGNFy^L5?~VI%u3@ zqlOe64~`g_8w)i9WdazPn@n)sJn7&lSvsI*X`q8Z%YQo;I>Y8OT%&}*ost222+jzP z{HN1=$LKg_LlN1uYlXacb{evn$PpP!FasiVtfKm}zsUjSxV1?2rG$?$Xi6J@nX9&%F#9pwe)|kAE=YNF$GOfNF;v?(j!A;*pMQj`84q~ z#!#2!yZRsf@}KIm!VcM%&MtH2vNWwk^$^rURu6;2waD=OjH0N&cF&07&|>H1*hZam zig?)$cqgPKYko=g8jT8XBlW(dw*7>pfQ@YjZ z(%-n|mlc_f)8^>bG}YAIy!j0a-GmH@HvMq*Uh)#|oPNqmUOK=`ue3Q_?(C~{5B+Gb zN1|o%h;-5kT?-4(ou<_orb?VB@Dzki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