backpack improvements

This commit is contained in:
Jörg Henke 2020-08-19 21:47:59 +02:00
parent fd41db811b
commit e0511d8971
3 changed files with 25 additions and 22 deletions

View File

@ -22,8 +22,8 @@ public class BackpackInventory implements Inventory {
readItemsFromTag(this.stacks, tag); readItemsFromTag(this.stacks, tag);
} }
public static final BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player) { public static final BackpackInventory getInventory(BackpackScreenHandler handler, PlayerEntity player,
ItemStack stack = player.getMainHandStack(); ItemStack stack) {
if (stack != null) { if (stack != null) {
if (!stack.hasTag()) { if (!stack.hasTag()) {
stack.setTag(new CompoundTag()); stack.setTag(new CompoundTag());
@ -64,9 +64,8 @@ public class BackpackInventory implements Inventory {
return tag; return tag;
} }
public DefaultedList<ItemStack> getList(DefaultedList<ItemStack> dl) { public DefaultedList<ItemStack> getList() {
dl = stacks; return stacks;
return dl;
} }
@Override @Override
@ -134,7 +133,7 @@ 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 = player.getMainHandStack(); ItemStack stack = handler.getStack();
if (stack != null) { if (stack != null) {
if (!stack.hasTag()) { if (!stack.hasTag()) {
stack.setTag(new CompoundTag()); stack.setTag(new CompoundTag());

View File

@ -9,21 +9,21 @@ 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.Hand;
import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.collection.DefaultedList;
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 int backpackSlots; private final ItemStack stack; // the stack of the backpack item
private int backpackSlotSize;
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(); this.stack = buf.readItemStack();
this.backpackInventory = BackpackInventory.getInventory(this, player); this.backpackInventory = BackpackInventory.getInventory(this, player, stack);
backpackSlots = ItemBackpack.SLOTSIZE; backpackSlotSize = ItemBackpack.SLOTSIZE;
int spacing = 112; int spacing = 112;
@ -43,17 +43,17 @@ public class BackpackScreenHandler extends ScreenHandler {
} }
playerInventory.onOpen(player); playerInventory.onOpen(player);
DefaultedList<ItemStack> ad = DefaultedList.ofSize(backpackSlots, ItemStack.EMPTY); DefaultedList<ItemStack> backpackSlots = backpackInventory.getList();
if (ad.size() == 0) { if (backpackSlots.size() == 0) {
return; return;
} }
for (int x = 0; x < 9; x++) { for (int x = 0; x < 9; x++) {
for (int y = 0; y < backpackSlots / 9; y++) { for (int y = 0; y < backpackSlotSize / 9; y++) {
this.getSlot(x + y * 9).setStack(ad.get(x + y * 9)); this.getSlot(x + y * 9).setStack(backpackSlots.get(x + y * 9));
} }
} }
for (int x = 0; x < (backpackSlots % 9); x++) { for (int x = 0; x < (backpackSlotSize % 9); x++) {
this.getSlot(x + (backpackSlots / 9) * 9).setStack(ad.get(x + (backpackSlots / 9) * 9)); this.getSlot(x + (backpackSlotSize / 9) * 9).setStack(backpackSlots.get(x + (backpackSlotSize / 9) * 9));
} }
} }
@ -91,11 +91,11 @@ public class BackpackScreenHandler extends ScreenHandler {
if (clickedSlot != null && clickedSlot.hasStack()) { if (clickedSlot != null && clickedSlot.hasStack()) {
ItemStack clickedStack = clickedSlot.getStack(); ItemStack clickedStack = clickedSlot.getStack();
copy = clickedStack.copy(); copy = clickedStack.copy();
if (slotNum < backpackSlots) { if (slotNum < backpackSlotSize) {
if (!this.insertItem(clickedStack, backpackSlots, this.slots.size(), true)) { if (!this.insertItem(clickedStack, backpackSlotSize, this.slots.size(), true)) {
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
} else if (!this.insertItem(clickedStack, 0, backpackSlots, false)) { } else if (!this.insertItem(clickedStack, 0, backpackSlotSize, false)) {
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
if (clickedStack.isEmpty()) { if (clickedStack.isEmpty()) {
@ -106,4 +106,8 @@ public class BackpackScreenHandler extends ScreenHandler {
} }
return copy; return copy;
} }
public ItemStack getStack() {
return stack;
}
} }

View File

@ -33,8 +33,8 @@ public class ItemBackpack extends Item implements DyeableItem {
@Override @Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
ItemStack itemStack = player.getStackInHand(hand);
if (!world.isClient) { if (!world.isClient) {
ItemStack itemStack = player.getActiveItem();
player.openHandledScreen(new ExtendedScreenHandlerFactory() { player.openHandledScreen(new ExtendedScreenHandlerFactory() {
@Override @Override
public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf packetByteBuf) { public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf packetByteBuf) {
@ -54,7 +54,7 @@ public class ItemBackpack extends Item implements DyeableItem {
} }
}); });
} }
return new TypedActionResult<ItemStack>(ActionResult.PASS, player.getStackInHand(hand)); return new TypedActionResult<ItemStack>(ActionResult.SUCCESS, itemStack);
} }
@Override @Override