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

View File

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

View File

@ -33,8 +33,8 @@ public class ItemBackpack extends Item implements DyeableItem {
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
ItemStack itemStack = player.getStackInHand(hand);
if (!world.isClient) {
ItemStack itemStack = player.getActiveItem();
player.openHandledScreen(new ExtendedScreenHandlerFactory() {
@Override
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