keep backpack content by tags in compound
This commit is contained in:
@ -1,5 +1,8 @@
|
|||||||
package de.jottyfan.minecraft.quickiefabric.container;
|
package de.jottyfan.minecraft.quickiefabric.container;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import de.jottyfan.minecraft.quickiefabric.items.ItemBackpack;
|
import de.jottyfan.minecraft.quickiefabric.items.ItemBackpack;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.inventory.Inventories;
|
import net.minecraft.inventory.Inventories;
|
||||||
@ -9,16 +12,23 @@ 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author jotty
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class BackpackInventory implements Inventory {
|
public class BackpackInventory implements Inventory {
|
||||||
public static final int SECTION_SIZE = 9;
|
private final static Logger LOGGER = LogManager.getLogger(BackpackInventory.class);
|
||||||
private final DefaultedList<ItemStack> stacks;
|
private final DefaultedList<ItemStack> stacks;
|
||||||
private final BackpackScreenHandler handler;
|
private final BackpackScreenHandler handler;
|
||||||
|
private Hand hand;
|
||||||
|
|
||||||
private 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(ItemBackpack.SLOTSIZE, ItemStack.EMPTY);
|
||||||
readItemsFromTag(this.stacks, tag);
|
readItemsFromTag(this.stacks, tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,35 +45,6 @@ public class BackpackInventory implements Inventory {
|
|||||||
return new BackpackInventory(stack.getTag().getCompound("backpack"), handler);
|
return new BackpackInventory(stack.getTag().getCompound("backpack"), handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readItemsFromTag(DefaultedList<ItemStack> 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<ItemStack> 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<ItemStack> getList() {
|
public DefaultedList<ItemStack> getList() {
|
||||||
return stacks;
|
return stacks;
|
||||||
}
|
}
|
||||||
@ -110,6 +91,7 @@ public class BackpackInventory implements Inventory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setStack(int slot, ItemStack stack) {
|
public void setStack(int slot, ItemStack stack) {
|
||||||
|
// LOGGER.info("set slot {} item stack {}x {}", slot, stack.getCount(), stack.getItem().getName());
|
||||||
this.stacks.set(slot, stack);
|
this.stacks.set(slot, stack);
|
||||||
markDirty();
|
markDirty();
|
||||||
}
|
}
|
||||||
@ -121,7 +103,8 @@ public class BackpackInventory implements Inventory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void markDirty() {
|
public void markDirty() {
|
||||||
handler.onContentChanged(this);
|
// LOGGER.info("dirty marked");
|
||||||
|
// handler.onContentChanged(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -133,13 +116,47 @@ public class BackpackInventory implements Inventory {
|
|||||||
@Override
|
@Override
|
||||||
public void onClose(PlayerEntity player) {
|
public void onClose(PlayerEntity player) {
|
||||||
Inventory.super.onClose(player);
|
Inventory.super.onClose(player);
|
||||||
ItemStack stack = handler.getStack();
|
ItemStack stack = player.getStackInHand(hand);
|
||||||
if (stack != null) {
|
if (stack != null) {
|
||||||
if (!stack.hasTag()) {
|
if (!stack.hasTag()) {
|
||||||
stack.setTag(new CompoundTag());
|
stack.setTag(new CompoundTag());
|
||||||
}
|
}
|
||||||
stack.getTag().put("backpack", writeItemsToTag());
|
stack.getTag().put("backpack", writeItemsToTag());
|
||||||
}
|
}
|
||||||
|
player.getStackInHand(hand).setTag(stack.getTag());
|
||||||
player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f);
|
player.playSound(SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.PLAYERS, 1f, 1f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void readItemsFromTag(DefaultedList<ItemStack> 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<ItemStack> 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);
|
||||||
|
compoundTag = itemStack.toTag(compoundTag);
|
||||||
|
listTag.add(compoundTag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CompoundTag tag = new CompoundTag();
|
||||||
|
tag.put("items", listTag);
|
||||||
|
return tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHand(Hand hand) {
|
||||||
|
this.hand = hand;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,9 +15,12 @@ import net.minecraft.text.TranslatableText;
|
|||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
public class BackpackScreen extends HandledScreen<BackpackScreenHandler> implements ScreenHandlerProvider<BackpackScreenHandler> {
|
public class BackpackScreen extends HandledScreen<BackpackScreenHandler>
|
||||||
private final static Identifier TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, "textures/gui/backpack.png");
|
implements ScreenHandlerProvider<BackpackScreenHandler> {
|
||||||
private final static Identifier SLOT_TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, "textures/gui/slot.png");
|
private final static Identifier TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC,
|
||||||
|
"textures/gui/backpack.png");
|
||||||
|
private final static Identifier SLOT_TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC,
|
||||||
|
"textures/gui/slot.png");
|
||||||
private int slots;
|
private int slots;
|
||||||
private final Integer containerHeight = 222;
|
private final Integer containerHeight = 222;
|
||||||
private final Integer containerWidth = 176;
|
private final Integer containerWidth = 176;
|
||||||
@ -65,8 +68,7 @@ public class BackpackScreen extends HandledScreen<BackpackScreenHandler> impleme
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void drawForeground(MatrixStack matrixStack, int i, int j)
|
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.title, 8.0F, -20.0F, 4210752);
|
||||||
this.textRenderer.draw(matrixStack, this.playerInventory.getDisplayName(), 8.0F, 101f, 4210752);
|
this.textRenderer.draw(matrixStack, this.playerInventory.getDisplayName(), 8.0F, 101f, 4210752);
|
||||||
}
|
}
|
||||||
|
@ -4,17 +4,17 @@ import de.jottyfan.minecraft.quickiefabric.init.RegistryManager;
|
|||||||
import de.jottyfan.minecraft.quickiefabric.items.ItemBackpack;
|
import de.jottyfan.minecraft.quickiefabric.items.ItemBackpack;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.entity.player.PlayerInventory;
|
import net.minecraft.entity.player.PlayerInventory;
|
||||||
import net.minecraft.inventory.Inventory;
|
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.network.PacketByteBuf;
|
import net.minecraft.network.PacketByteBuf;
|
||||||
import net.minecraft.screen.ScreenHandler;
|
import net.minecraft.screen.ScreenHandler;
|
||||||
import net.minecraft.screen.slot.Slot;
|
import net.minecraft.screen.slot.Slot;
|
||||||
import net.minecraft.util.collection.DefaultedList;
|
import net.minecraft.util.Hand;
|
||||||
|
|
||||||
public class BackpackScreenHandler extends ScreenHandler {
|
public class BackpackScreenHandler extends ScreenHandler {
|
||||||
|
|
||||||
private final BackpackInventory backpackInventory;
|
private final BackpackInventory backpackInventory;
|
||||||
private final PlayerEntity player;
|
private final PlayerEntity player;
|
||||||
|
private final Hand hand;
|
||||||
private final ItemStack stack; // the stack of the backpack item
|
private final ItemStack stack; // the stack of the backpack item
|
||||||
private int backpackSlotSize;
|
private int backpackSlotSize;
|
||||||
|
|
||||||
@ -22,6 +22,7 @@ public class BackpackScreenHandler extends ScreenHandler {
|
|||||||
super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId);
|
super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId);
|
||||||
this.player = playerInventory.player;
|
this.player = playerInventory.player;
|
||||||
this.stack = buf.readItemStack();
|
this.stack = buf.readItemStack();
|
||||||
|
this.hand = buf.readByte() > 0 ? Hand.MAIN_HAND : Hand.OFF_HAND;
|
||||||
this.backpackInventory = BackpackInventory.getInventory(this, player, stack);
|
this.backpackInventory = BackpackInventory.getInventory(this, player, stack);
|
||||||
backpackSlotSize = ItemBackpack.SLOTSIZE;
|
backpackSlotSize = ItemBackpack.SLOTSIZE;
|
||||||
|
|
||||||
@ -41,20 +42,6 @@ public class BackpackScreenHandler extends ScreenHandler {
|
|||||||
for (int x = 0; x < 9; ++x) {
|
for (int x = 0; x < 9; ++x) {
|
||||||
this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 58 + spacing));
|
this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 58 + spacing));
|
||||||
}
|
}
|
||||||
playerInventory.onOpen(player);
|
|
||||||
|
|
||||||
DefaultedList<ItemStack> backpackSlots = backpackInventory.getList();
|
|
||||||
if (backpackSlots.size() == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int x = 0; x < 9; x++) {
|
|
||||||
for (int y = 0; y < backpackSlotSize / 9; y++) {
|
|
||||||
this.getSlot(x + y * 9).setStack(backpackSlots.get(x + y * 9));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int x = 0; x < (backpackSlotSize % 9); x++) {
|
|
||||||
this.getSlot(x + (backpackSlotSize / 9) * 9).setStack(backpackSlots.get(x + (backpackSlotSize / 9) * 9));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public PlayerEntity getPlayer() {
|
public PlayerEntity getPlayer() {
|
||||||
@ -66,24 +53,13 @@ public class BackpackScreenHandler extends ScreenHandler {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onContentChanged(Inventory inv) {
|
|
||||||
super.onContentChanged(inv);
|
|
||||||
if (inv == this.backpackInventory) {
|
|
||||||
this.updateInv();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close(PlayerEntity player) {
|
public void close(PlayerEntity player) {
|
||||||
super.close(player);
|
super.close(player);
|
||||||
|
this.backpackInventory.setHand(hand);
|
||||||
this.backpackInventory.onClose(player);
|
this.backpackInventory.onClose(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateInv() {
|
|
||||||
this.sendContentUpdates();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack transferSlot(PlayerEntity player, int slotNum) {
|
public ItemStack transferSlot(PlayerEntity player, int slotNum) {
|
||||||
ItemStack copy = ItemStack.EMPTY;
|
ItemStack copy = ItemStack.EMPTY;
|
||||||
|
@ -39,6 +39,7 @@ 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.writeByte(hand == Hand.MAIN_HAND ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -50,6 +51,7 @@ 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.writeByte(hand == Hand.MAIN_HAND ? 1 : 0);
|
||||||
return new BackpackScreenHandler(syncId, inv, buf);
|
return new BackpackScreenHandler(syncId, inv, buf);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user