some steps forward

This commit is contained in:
Jörg Henke 2020-08-14 18:02:01 +02:00
parent 38458dd71d
commit 7995208175
5 changed files with 138 additions and 143 deletions

View File

@ -10,11 +10,9 @@ import net.minecraft.util.collection.DefaultedList;
public class BackpackInventory implements Inventory { public class BackpackInventory implements Inventory {
public static final int SECTION_SIZE = 9; public static final int SECTION_SIZE = 9;
private final DefaultedList<ItemStack> stacks; private final DefaultedList<ItemStack> stacks;
public final PlayerEntity accessor;
private BackpackScreenHandler container; private BackpackScreenHandler container;
public BackpackInventory(BackpackScreenHandler container, PlayerEntity player) { public BackpackInventory(BackpackScreenHandler container) {
this.accessor = player;
this.stacks = DefaultedList.ofSize(SECTION_SIZE * ItemBackpack.SLOTSIZE, ItemStack.EMPTY); this.stacks = DefaultedList.ofSize(SECTION_SIZE * ItemBackpack.SLOTSIZE, ItemStack.EMPTY);
this.container = container; this.container = container;
} }

View File

@ -19,17 +19,12 @@ public class BackpackScreen extends HandledScreen<BackpackScreenHandler> impleme
private final static Identifier TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, "textures/gui/backpack.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 final static Identifier SLOT_TEXTURE = new Identifier(RegistryManager.QUICKIEFABRIC, "textures/gui/slot.png");
private int slots; private int slots;
private Integer containerHeight; private final Integer containerHeight = 222;
private Integer containerWidth; private final Integer containerWidth = 176;
public BackpackScreen(BackpackScreenHandler handler, PlayerInventory inventory, Text text) { public BackpackScreen(BackpackScreenHandler handler, PlayerInventory inventory, Text text) {
super(handler, inventory, new TranslatableText("container.quickiefabric.backpack")); super(handler, inventory, new TranslatableText("container.quickiefabric.backpack"));
slots = ItemBackpack.SLOTSIZE; slots = ItemBackpack.SLOTSIZE;
if ((slots / 9) == 4) {
this.containerHeight = 184;
} else if ((slots / 9) > 4) {
this.containerHeight = 222;
}
} }
@Override @Override

View File

@ -13,114 +13,116 @@ 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 inv;
private final PlayerEntity player;
private int backpackSlots;
private Hand hand;
public BackpackScreenHandler(int syncId, PlayerInventory playerInv) {
super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId);
this.inv = new BackpackInventory(this, playerInv.player);
this.player = playerInv.player;
this.hand = player.getActiveHand();
backpackSlots = ItemBackpack.SLOTSIZE;
int spacing; private final BackpackInventory backpackInventory;
if(backpackSlots % 9 == 0) private final PlayerEntity player;
spacing = 30 + (backpackSlots /9) * 18 + ((backpackSlots /9) < 5 ? 0 : 2); private int backpackSlots;
else private Hand hand;
spacing = 30 + (backpackSlots /9 + 1) * 18 + ((backpackSlots /9) < 5 ? 0 : 2);
for(int y = 0; y < (backpackSlots /9); y++) { public BackpackScreenHandler(int syncId, PlayerInventory playerInventory) {
for(int x = 0; x < 9; ++x) { super(RegistryManager.BACKPACK_SCREEN_HANDLER, syncId);
this.addSlot(new Slot(inv, x + y * 9, 8 + x * 18, 18 + y * 18)); this.player = playerInventory.player;
} this.backpackInventory = new BackpackInventory(this);
} backpackSlots = ItemBackpack.SLOTSIZE;
if((backpackSlots % 9) != 0)
for(int x = 0; x < (backpackSlots % 9); x++) {
this.addSlot(new Slot(inv, x + (backpackSlots /9) * 9, 8 + x * 18, 18 + (backpackSlots /9) * 18));
}
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++) {
for (int x = 0; x < 9; ++x) {
this.addSlot(new Slot(backpackInventory, x + y * 9, 8 + x * 18, 18 + 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 x = 0; x < 9; ++x) {
this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 58 + spacing));
}
}
}
for(int y = 0; y < 3; ++y) { @Override
for(int x = 0; x < 9; ++x) { public boolean canUse(PlayerEntity player) {
this.addSlot(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, spacing + y * 18)); return true;
} }
}
for(int x = 0; x < 9; ++x) { public void onContentChanged(Inventory inv) {
this.addSlot(new Slot(playerInv, x, 8 + x * 18, 58 + spacing)); super.onContentChanged(inv);
} if (inv == this.backpackInventory) {
this.updateInv();
}
}
DefaultedList<ItemStack> ad = DefaultedList.ofSize(backpackSlots, ItemStack.EMPTY); public void close(PlayerEntity player) {
ItemBackpack.getInventory(player.getStackInHand(this.hand), ad); super.close(player);
if(ad.size() == 0) this.backpackInventory.onClose(player);
return; }
for(int x = 0; x < 9; x++) public void updateInv() {
for(int y = 0; y < backpackSlots /9; y++) { this.sendContentUpdates();
this.getSlot(x + y * 9).setStack(ad.get(x+y*9)); }
}
for(int x = 0; x < (backpackSlots % 9); x++) { @Override
this.getSlot(x + (backpackSlots /9)*9).setStack(ad.get(x+(backpackSlots /9)*9)); public ItemStack transferSlot(PlayerEntity player, int slotNum) {
} ItemStack copy = ItemStack.EMPTY;
Slot clickedSlot = this.slots.get(slotNum);
if (clickedSlot != null && clickedSlot.hasStack()) {
ItemStack clickedStack = clickedSlot.getStack();
copy = clickedStack.copy();
if (slotNum < backpackSlots) {
if (!this.insertItem(clickedStack, backpackSlots, this.slots.size(), true)) {
return ItemStack.EMPTY;
}
} else if (!this.insertItem(clickedStack, 0, backpackSlots, false)) {
return ItemStack.EMPTY;
}
} if (clickedStack.isEmpty()) {
clickedSlot.setStack(ItemStack.EMPTY);
@Override } else {
public boolean canUse(PlayerEntity player) { clickedSlot.markDirty();
return true; }
} }
public void onContentChanged(Inventory inv) { return copy;
super.onContentChanged(inv); }
if (inv == this.inv) {
this.updateInv();
}
}
public void close(PlayerEntity player) { @Override
super.close(player); public ItemStack onSlotClick(int i, int j, SlotActionType slotActionType, PlayerEntity playerEntity) {
this.inv.onClose(player); if (i > 0) {
} if (this.getSlot(i).getStack().equals(playerEntity.getStackInHand(hand))) {
return ItemStack.EMPTY;
}
}
return super.onSlotClick(i, j, slotActionType, playerEntity);
}
public void updateInv() { public BackpackScreenHandler withHand(Hand hand) {
this.sendContentUpdates(); this.hand = hand;
} DefaultedList<ItemStack> ad = DefaultedList.ofSize(backpackSlots, ItemStack.EMPTY);
ItemBackpack.getInventory(player.getStackInHand(this.hand), ad);
@Override if (ad.size() == 0) {
public ItemStack transferSlot(PlayerEntity player, int slotNum) { return this;
ItemStack copy = ItemStack.EMPTY; }
Slot clickedSlot = this.slots.get(slotNum); for (int x = 0; x < 9; x++) {
if (clickedSlot != null && clickedSlot.hasStack()) { for (int y = 0; y < backpackSlots / 9; y++) {
ItemStack clickedStack = clickedSlot.getStack(); this.getSlot(x + y * 9).setStack(ad.get(x + y * 9));
copy = clickedStack.copy(); }
if (slotNum < backpackSlots) { }
if (!this.insertItem(clickedStack, backpackSlots, this.slots.size(), true)) { for (int x = 0; x < (backpackSlots % 9); x++) {
return ItemStack.EMPTY; this.getSlot(x + (backpackSlots / 9) * 9).setStack(ad.get(x + (backpackSlots / 9) * 9));
} }
} else if (!this.insertItem(clickedStack, 0, backpackSlots, false)) { return this;
return ItemStack.EMPTY; }
}
if (clickedStack.isEmpty()) {
clickedSlot.setStack(ItemStack.EMPTY);
} else {
clickedSlot.markDirty();
}
}
return copy;
}
@Override
public ItemStack onSlotClick(int int_1, int int_2, SlotActionType slotActionType_1, PlayerEntity playerEntity_1) {
if(int_1 > 0) {
if (this.getSlot(int_1).getStack().equals(playerEntity_1.getStackInHand(hand)))
return ItemStack.EMPTY;
}
return super.onSlotClick(int_1, int_2, slotActionType_1, playerEntity_1);
}
} }

View File

@ -41,7 +41,7 @@ public class ItemBackpack extends Item implements DyeableItem {
LOGGER.info("found registered screen {}", id.toString()); LOGGER.info("found registered screen {}", id.toString());
} }
SimpleNamedScreenHandlerFactory factory = new SimpleNamedScreenHandlerFactory( SimpleNamedScreenHandlerFactory factory = new SimpleNamedScreenHandlerFactory(
(id, inventory, buf) -> new BackpackScreenHandler(id, player.inventory), (id, inventory, buf) -> new BackpackScreenHandler(id, player.inventory).withHand(hand),
Text.method_30163(RegistryManager.BACKPACK_CONTAINTER.toString())); Text.method_30163(RegistryManager.BACKPACK_CONTAINTER.toString()));
player.openHandledScreen(factory); player.openHandledScreen(factory);
} }

View File

@ -1,36 +1,36 @@
{ {
"schemaVersion": 1, "schemaVersion": 1,
"id": "quickiefabric", "id": "quickiefabric",
"version": "${version}", "version": "${version}",
"name": "quickiefabric",
"name": "quickiefabric", "description": "Quickiefabric is the fabric implementation of Quickie, a mot to speed up daily work in Minecraft.",
"description": "Quickiefabric is the fabric implementation of Quickie, a mot to speed up daily work in Minecraft.", "authors": [
"authors": [ "Jottyfan"
"Jottyfan" ],
], "contact": {
"contact": { "homepage": "https://gitlab.com/jottyfan/quickiefabric",
"homepage": "https://gitlab.com/jottyfan/quickiefabric", "sources": "https://gitlab.com/jottyfan/quickiefabric"
"sources": "https://gitlab.com/jottyfan/quickiefabric" },
}, "license": "MIT",
"icon": "assets/quickiefabric/icon.png",
"license": "MIT", "environment": "*",
"icon": "assets/quickiefabric/icon.png", "entrypoints": {
"main": [
"environment": "*", "de.jottyfan.minecraft.quickiefabric.QuickieFabric"
"entrypoints": { ],
"main": [ "client": [
"de.jottyfan.minecraft.quickiefabric.QuickieFabric" "de.jottyfan.minecraft.quickiefabric.QuickieFabricClient"
] ]
}, },
"mixins": [ "mixins": [
"quickiefabric.mixins.json" "quickiefabric.mixins.json"
], ],
"depends": { "depends": {
"fabricloader": ">=0.7.4", "fabricloader": ">=0.7.4",
"fabric": "*", "fabric": "*",
"minecraft": "1.16.x" "minecraft": "1.16.x"
}, },
"suggests": { "suggests": {
"flamingo": "*" "flamingo": "*"
} }
} }