From d01fe52aba42c9acc21ac911adcd9715059a765a Mon Sep 17 00:00:00 2001 From: Christian Knaapen Date: Sun, 16 Sep 2018 14:21:05 +0200 Subject: [PATCH] Some work on randomizerbag --- .../inventory/RandomizerBagContainer.java | 95 ++++++++++- .../inventory/RandomizerBagGuiContainer.java | 51 +++++- .../inventory/RandomizerBagInventory.java | 153 +++++++++++++----- .../inventory/RandomizerGuiHandler.java | 5 +- .../textures/gui/container/randomizerbag.png | Bin 0 -> 1431 bytes 5 files changed, 257 insertions(+), 47 deletions(-) create mode 100644 src/main/resources/assets/effortlessbuilding/textures/gui/container/randomizerbag.png diff --git a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagContainer.java b/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagContainer.java index dcf6235..4195f31 100644 --- a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagContainer.java +++ b/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagContainer.java @@ -1,11 +1,104 @@ package nl.requios.effortlessbuilding.inventory; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.ClickType; import net.minecraft.inventory.Container; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import net.minecraft.util.EnumHand; public class RandomizerBagContainer extends Container { + + private final IInventory bagInventory; + private final int sizeInventory; + + public RandomizerBagContainer(InventoryPlayer parInventoryPlayer, IInventory parIInventory) { + bagInventory = parIInventory; + sizeInventory = bagInventory.getSizeInventory(); + for (int i = 0; i < RandomizerBagInventory.INV_SIZE; ++i) { + this.addSlotToContainer(new Slot(bagInventory, i, 80 + (18 * (i / 4)), 8 + (18 * (i % 4)))); + } + + // add player inventory slots + int i; + for (i = 0; i < 3; ++i) { + for (int j = 0; j < 9; ++j) { + addSlotToContainer(new Slot(parInventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); + } + } + + // add hotbar slots + for (i = 0; i < 9; ++i) { + addSlotToContainer(new Slot(parInventoryPlayer, i, 8 + i * 18, 142)); + } + } + @Override public boolean canInteractWith(EntityPlayer playerIn) { - return false; + return bagInventory.isUsableByPlayer(playerIn); + } + + @Override + public ItemStack transferStackInSlot(EntityPlayer playerIn, + int slotIndex) { + ItemStack itemStack1 = null; + Slot slot = inventorySlots.get(slotIndex); + + if (slot != null && slot.getHasStack()) { + ItemStack itemStack2 = slot.getStack(); + itemStack1 = itemStack2.copy(); + + if (slotIndex == 0) { + if (!mergeItemStack(itemStack2, sizeInventory, + sizeInventory + 36, true)) { + return null; + } + + slot.onSlotChange(itemStack2, itemStack1); + } else if (slotIndex != 1) { + if (slotIndex >= sizeInventory + && slotIndex < sizeInventory + 27) // player inventory slots + { + if (!mergeItemStack(itemStack2, sizeInventory + 27, + sizeInventory + 36, false)) { + return null; + } + } else if (slotIndex >= sizeInventory + 27 + && slotIndex < sizeInventory + 36 + && !mergeItemStack(itemStack2, sizeInventory + 1, + sizeInventory + 27, false)) // hotbar slots + { + return null; + } + } else if (!mergeItemStack(itemStack2, sizeInventory, + sizeInventory + 36, false)) { + return null; + } + + if (itemStack2.getCount() == 0) { + slot.putStack(null); + } else { + slot.onSlotChanged(); + } + + if (itemStack2.getCount() == itemStack1.getCount()) { + return null; + } + + slot.onTake(playerIn, itemStack2); + } + + return itemStack1; + } + + @Override + public ItemStack slotClick(int slot, int dragType, ClickType clickTypeIn, EntityPlayer player) { + // this will prevent the player from interacting with the item that opened the inventory: + if (slot >= 0 && getSlot(slot) != null && getSlot(slot).getStack() == player.getHeldItem(EnumHand.MAIN_HAND)) { + return null; + } + return super.slotClick(slot, dragType, clickTypeIn, player); } } diff --git a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagGuiContainer.java b/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagGuiContainer.java index 0ee87a5..4d2df0c 100644 --- a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagGuiContainer.java +++ b/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagGuiContainer.java @@ -1,15 +1,60 @@ package nl.requios.effortlessbuilding.inventory; import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; +import net.minecraft.inventory.IInventory; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; +import nl.requios.effortlessbuilding.EffortlessBuilding; +@SideOnly(Side.CLIENT) public class RandomizerBagGuiContainer extends GuiContainer { - public RandomizerBagGuiContainer(Container inventorySlotsIn) { - super(inventorySlotsIn); + private static final ResourceLocation grinderGuiTextures = + new ResourceLocation(EffortlessBuilding.MODID + + ":textures/gui/container/bagrandomizer.png"); + private final InventoryPlayer inventoryPlayer; + private final IInventory tileGrinder; + + public RandomizerBagGuiContainer(InventoryPlayer parInventoryPlayer, + IInventory parInventoryGrinder) { + super(new RandomizerBagContainer(parInventoryPlayer, + parInventoryGrinder)); + inventoryPlayer = parInventoryPlayer; + tileGrinder = parInventoryGrinder; } @Override - protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { + protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { + String s = tileGrinder.getDisplayName().getUnformattedText(); + fontRenderer.drawString(s, xSize / 2 - fontRenderer.getStringWidth(s) / 2, 6, 4210752); + fontRenderer.drawString(inventoryPlayer.getDisplayName().getUnformattedText(), 8, ySize - 96 + 2, 4210752); + } + /** + * Args : renderPartialTicks, mouseX, mouseY + */ + @Override + protected void drawGuiContainerBackgroundLayer(float partialTicks, + int mouseX, int mouseY) { + GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); + mc.getTextureManager().bindTexture(grinderGuiTextures); + int marginHorizontal = (width - xSize) / 2; + int marginVertical = (height - ySize) / 2; + drawTexturedModalRect(marginHorizontal, marginVertical, 0, 0, xSize, ySize); + + // Draw progress indicator + int progressLevel = getProgressLevel(24); + drawTexturedModalRect(marginHorizontal + 79, marginVertical + 34, 176, 14, progressLevel + 1, 16); + } + + private int getProgressLevel(int progressIndicatorPixelWidth) { + int ticksGrindingItemSoFar = tileGrinder.getField(2); + int ticksPerItem = tileGrinder.getField(3); + return ticksPerItem != 0 && ticksGrindingItemSoFar != 0 ? + ticksGrindingItemSoFar * progressIndicatorPixelWidth / ticksPerItem + : 0; } } diff --git a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagInventory.java b/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagInventory.java index 052559b..93baa79 100644 --- a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagInventory.java +++ b/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagInventory.java @@ -1,13 +1,17 @@ package nl.requios.effortlessbuilding.inventory; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.TextComponentString; import net.minecraftforge.items.IItemHandler; import javax.annotation.Nonnull; -public class RandomizerBagInventory implements IItemHandler { +public class RandomizerBagInventory implements IInventory { //Reference to NBT data private final ItemStack invItem; @@ -27,10 +31,15 @@ public class RandomizerBagInventory implements IItemHandler { } @Override - public int getSlots() { + public int getSizeInventory() { return INV_SIZE; } + @Override + public boolean isEmpty() { + return false; + } + @Nonnull @Override public ItemStack getStackInSlot(int slot) { @@ -39,48 +48,42 @@ public class RandomizerBagInventory implements IItemHandler { @Nonnull @Override - public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) { - ItemStack slotStack = getStackInSlot(slot); - if (slotStack.getCount() == 0) { - setInventorySlotContents(slot, stack); - return null; + public ItemStack decrStackSize(int slot, int amount) { + if (inventory[slot] != null) + { + ItemStack itemstack; + + if (inventory[slot].getCount() <= amount) + { + itemstack = inventory[slot]; + inventory[slot] = null; + return itemstack; + } + else + { + itemstack = inventory[slot].splitStack(amount); + + if (inventory[slot].getCount() == 0) + { + inventory[slot] = null; + } + + return itemstack; + } } - if (getSlotLimit(slot) - slotStack.getCount() < stack.getCount()) { - //Not enough place remaining, split stack - slotStack.setCount(getSlotLimit(slot)); - onInventoryChanged(); - stack.copy().shrink(getSlotLimit(slot) - slotStack.getCount()); - //TODO make proper - return stack; - } else { - slotStack.grow(stack.getCount()); - onInventoryChanged(); + else + { return null; } } - @Nonnull @Override - public ItemStack extractItem(int slot, int amount, boolean simulate) { - ItemStack stack = getStackInSlot(slot); - if (stack == null) return stack; - - if (stack.getCount() > amount) { - stack = stack.splitStack(amount); - onInventoryChanged(); - } else { - setInventorySlotContents(slot, null); - } - return stack; - } - - @Override - public int getSlotLimit(int slot) { - return 64; + public ItemStack removeStackFromSlot(int index) { + return null; } public void onInventoryChanged() { - for (int i = 0; i < getSlotLimit(0); ++i) { + for (int i = 0; i < getFieldCount(); ++i) { if (getStackInSlot(i) != null && getStackInSlot(i).getCount() == 0) { inventory[i] = null; } @@ -92,12 +95,66 @@ public class RandomizerBagInventory implements IItemHandler { public void setInventorySlotContents(int slot, ItemStack stack) { inventory[slot] = stack; - if (stack != null && stack.getCount() > getSlotLimit(slot)) { - stack.setCount(getSlotLimit(slot)); + if (stack != null && stack.getCount() > getInventoryStackLimit()) + { + stack.setCount(getInventoryStackLimit()); } - // Don't forget this line or your inventory will not be saved! - onInventoryChanged(); + markDirty(); + } + + @Override + public int getInventoryStackLimit() { + return 64; + } + + @Override + public void markDirty() { + + } + + @Override + public boolean isUsableByPlayer(EntityPlayer player) { + return true; + } + + @Override + public void openInventory(EntityPlayer player) { + + } + + @Override + public void closeInventory(EntityPlayer player) { + + } + + @Override + public boolean isItemValidForSlot(int index, ItemStack stack) { + return true; + } + + @Override + public int getField(int id) { + //variables + return 0; + } + + @Override + public void setField(int id, int value) { + + } + + @Override + public int getFieldCount() { + return 0; + } + + @Override + public void clear() { + for (int i = 0; i < inventory.length; ++i) + { + inventory[i] = null; + } } public void readFromNBT(NBTTagCompound compound) { @@ -111,7 +168,7 @@ public class RandomizerBagInventory implements IItemHandler { int slot = item.getInteger("Slot"); // Just double-checking that the saved slot index is within our inventory array bounds - if (slot >= 0 && slot < getSlots()) { + if (slot >= 0 && slot < getFieldCount()) { inventory[slot] = new ItemStack(item); } } @@ -124,7 +181,7 @@ public class RandomizerBagInventory implements IItemHandler { // Create a new NBT Tag List to store itemstacks as NBT Tags NBTTagList items = new NBTTagList(); - for (int i = 0; i < getSlots(); ++i) { + for (int i = 0; i < getSizeInventory(); ++i) { // Only write stacks that contain items if (getStackInSlot(i) != null) { // Make a new NBT Tag Compound to write the itemstack and slot index to @@ -141,4 +198,18 @@ public class RandomizerBagInventory implements IItemHandler { tagcompound.setTag("ItemInventory", items); } + @Override + public String getName() { + return "Testname"; + } + + @Override + public boolean hasCustomName() { + return false; + } + + @Override + public ITextComponent getDisplayName() { + return new TextComponentString("Testname"); + } } diff --git a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerGuiHandler.java b/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerGuiHandler.java index 7a2aec6..e1aa391 100644 --- a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerGuiHandler.java +++ b/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerGuiHandler.java @@ -1,6 +1,7 @@ package nl.requios.effortlessbuilding.inventory; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.EnumHand; import net.minecraft.world.World; import net.minecraftforge.fml.common.network.IGuiHandler; import nl.requios.effortlessbuilding.EffortlessBuilding; @@ -14,7 +15,7 @@ public class RandomizerGuiHandler implements IGuiHandler { public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) { // Use the player's held item to create the inventory - return new RandomizerBagContainer(); + return new RandomizerBagContainer(player.inventory, new RandomizerBagInventory(player.getHeldItem(EnumHand.MAIN_HAND))); } return null; } @@ -25,7 +26,7 @@ public class RandomizerGuiHandler implements IGuiHandler { if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) { // We have to cast the new container as our custom class // and pass in currently held item for the inventory - return new RandomizerBagGuiContainer(new RandomizerBagContainer()); + return new RandomizerBagGuiContainer(player.inventory, new RandomizerBagInventory(player.getHeldItem(EnumHand.MAIN_HAND))); } return null; } diff --git a/src/main/resources/assets/effortlessbuilding/textures/gui/container/randomizerbag.png b/src/main/resources/assets/effortlessbuilding/textures/gui/container/randomizerbag.png new file mode 100644 index 0000000000000000000000000000000000000000..ff34cf823cc448375f8c38af6a7fd302077e4978 GIT binary patch literal 1431 zcmeAS@N?(olHy`uVBq!ia0y~yU}|7sV4T3g1Qgjg(X9wbu_bxCyDx` z7I;J!Gca%qgD@k*tT_@uLG}_)Usv`=oB})y+WFsA9|MJCGeaUuobz*YQ}arITm}Z` zqSVBa)D(sC%#sWRcTeAd6une-1_qYjo-U3d6}R4AH56oE;5cG%VE^;utjyCrUR?4E zVmPpNA_D`D1PB=_2_`xyWL literal 0 HcmV?d00001