From 950b89a58c61da94fa5fa33ec9d2ea072f79c6d6 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 2 Aug 2016 16:32:13 +0200 Subject: [PATCH] Added bags. They're awesome and super useful. --- .../mod/blocks/BlockCompost.java | 3 +- .../mod/creative/CreativeTab.java | 3 + .../mod/inventory/ContainerBag.java | 352 ++++++++++++++++++ .../mod/inventory/GuiHandler.java | 12 +- .../mod/inventory/gui/GuiBag.java | 113 ++++++ .../mod/inventory/slot/SlotDeletion.java | 33 ++ .../mod/items/InitItems.java | 5 + .../actuallyadditions/mod/items/ItemBag.java | 110 ++++++ .../mod/network/PacketHandler.java | 19 +- .../mod/tile/TileEntityInputter.java | 9 +- .../actuallyadditions/mod/util/ItemUtil.java | 4 + .../actuallyadditions/mod/util/WorldUtil.java | 2 +- .../actuallyadditions/textures/gui/guiBag.png | Bin 0 -> 2024 bytes .../textures/gui/guiVoidBag.png | Bin 0 -> 2021 bytes 14 files changed, 657 insertions(+), 8 deletions(-) create mode 100644 src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerBag.java create mode 100644 src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiBag.java create mode 100644 src/main/java/de/ellpeck/actuallyadditions/mod/inventory/slot/SlotDeletion.java create mode 100644 src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemBag.java create mode 100644 src/main/resources/assets/actuallyadditions/textures/gui/guiBag.png create mode 100644 src/main/resources/assets/actuallyadditions/textures/gui/guiVoidBag.png diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockCompost.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockCompost.java index b7c657874..598b5d51f 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockCompost.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockCompost.java @@ -14,6 +14,7 @@ import de.ellpeck.actuallyadditions.api.recipe.CompostRecipe; import de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase; import de.ellpeck.actuallyadditions.mod.tile.TileEntityCompost; import de.ellpeck.actuallyadditions.mod.util.AssetUtil; +import de.ellpeck.actuallyadditions.mod.util.ItemUtil; import de.ellpeck.actuallyadditions.mod.util.StringUtil; import net.minecraft.block.SoundType; import net.minecraft.block.material.Material; @@ -119,7 +120,7 @@ public class BlockCompost extends BlockContainerBase implements IHudDisplay{ compost.setInventorySlotContents(0, null); return true; } - else if(stackPlayer.isItemEqual(slot)){ + else if(ItemUtil.canBeStacked(stackPlayer, slot)){ int addedStackSize = Math.min(slot.stackSize, stackPlayer.getMaxStackSize()-stackPlayer.stackSize); ItemStack stackToAdd = stackPlayer.copy(); stackToAdd.stackSize += addedStackSize; diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/creative/CreativeTab.java b/src/main/java/de/ellpeck/actuallyadditions/mod/creative/CreativeTab.java index 863027f1f..33906b881 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/creative/CreativeTab.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/creative/CreativeTab.java @@ -139,6 +139,9 @@ public class CreativeTab extends CreativeTabs{ this.add(InitBlocks.blockBlackLotus); this.add(InitBlocks.blockBookletStand); + this.add(InitItems.itemBag); + this.add(InitItems.itemVoidBag); + this.add(InitItems.itemWorm); this.add(InitItems.itemPlayerProbe); this.add(InitItems.itemColorLens); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerBag.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerBag.java new file mode 100644 index 000000000..ead68b035 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerBag.java @@ -0,0 +1,352 @@ +/* + * This file ("ContainerBag.java") is part of the Actually Additions mod for Minecraft. + * It is created and owned by Ellpeck and distributed + * under the Actually Additions License to be found at + * http://ellpeck.de/actaddlicense + * View the source code at https://github.com/Ellpeck/ActuallyAdditions + * + * © 2015-2016 Ellpeck + */ + +package de.ellpeck.actuallyadditions.mod.inventory; + +import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotDeletion; +import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotFilter; +import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotImmovable; +import de.ellpeck.actuallyadditions.mod.items.ItemBag; +import de.ellpeck.actuallyadditions.mod.items.ItemDrill; +import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; +import de.ellpeck.actuallyadditions.mod.tile.FilterSettings; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.*; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.TextComponentTranslation; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import javax.annotation.Nullable; + + +public class ContainerBag extends Container implements IButtonReactor{ + + private final InventoryBag bagInventory; + public final FilterSettings filter = new FilterSettings(0, 4, false, true, false, 0, -1000); + private final InventoryPlayer inventory; + private final boolean isVoid; + public boolean autoInsert; + private boolean oldAutoInsert; + + public ContainerBag(InventoryPlayer inventory, boolean isVoid){ + this.inventory = inventory; + this.bagInventory = new InventoryBag(isVoid); + this.isVoid = isVoid; + + for(int i = 0; i < 4; i++){ + this.addSlotToContainer(new SlotFilter(this.bagInventory, i, 155, 10+i*18)); + } + + if(this.isVoid){ + this.addSlotToContainer(new SlotDeletion(this.bagInventory, 4, 64, 65){ + @Override + public boolean isItemValid(@Nullable ItemStack stack){ + return ContainerBag.this.filter.check(stack, ContainerBag.this.bagInventory.slots); + } + }); + } + else{ + for(int i = 0; i < 4; i++){ + for(int j = 0; j < 7; j++){ + this.addSlotToContainer(new Slot(this.bagInventory, j+i*7+4, 10+j*18, 10+i*18){ + @Override + public boolean isItemValid(@Nullable ItemStack stack){ + return ContainerBag.this.filter.check(stack, ContainerBag.this.bagInventory.slots); + } + }); + } + } + } + + for(int i = 0; i < 3; i++){ + for(int j = 0; j < 9; j++){ + this.addSlotToContainer(new Slot(inventory, j+i*9+9, 8+j*18, 94+i*18)); + } + } + for(int i = 0; i < 9; i++){ + if(i == inventory.currentItem){ + this.addSlotToContainer(new SlotImmovable(inventory, i, 8+i*18, 152)); + } + else{ + this.addSlotToContainer(new Slot(inventory, i, 8+i*18, 152)); + } + } + + ItemStack stack = inventory.getCurrentItem(); + if(stack != null && stack.getItem() instanceof ItemBag){ + ItemDrill.loadSlotsFromNBT(this.bagInventory.slots, inventory.getCurrentItem()); + if(stack.hasTagCompound()){ + NBTTagCompound compound = stack.getTagCompound(); + this.filter.readFromNBT(compound, "Filter"); + this.autoInsert = compound.getBoolean("AutoInsert"); + } + } + } + + public static int getSlotAmount(boolean isVoid){ + return isVoid ? 5 : 32; + } + + @Override + public void detectAndSendChanges(){ + super.detectAndSendChanges(); + + if(this.filter.needsUpdateSend() || this.autoInsert != this.oldAutoInsert){ + for(IContainerListener listener : this.listeners){ + listener.sendProgressBarUpdate(this, 0, this.filter.isWhitelist ? 1 : 0); + listener.sendProgressBarUpdate(this, 1, this.filter.respectMeta ? 1 : 0); + listener.sendProgressBarUpdate(this, 2, this.filter.respectNBT ? 1 : 0); + listener.sendProgressBarUpdate(this, 3, this.filter.respectOredict); + listener.sendProgressBarUpdate(this, 4, this.autoInsert ? 1 : 0); + } + this.filter.updateLasts(); + this.oldAutoInsert = this.autoInsert; + } + } + + @Override + @SideOnly(Side.CLIENT) + public void updateProgressBar(int id, int data){ + if(id == 0){ + this.filter.isWhitelist = data == 1; + } + else if(id == 1){ + this.filter.respectMeta = data == 1; + } + else if(id == 2){ + this.filter.respectNBT = data == 1; + } + else if(id == 3){ + this.filter.respectOredict = data; + } + else if(id == 4){ + this.autoInsert = data == 1; + } + } + + @Override + public ItemStack transferStackInSlot(EntityPlayer player, int slot){ + int inventoryStart = this.bagInventory.slots.length; + int inventoryEnd = inventoryStart+26; + int hotbarStart = inventoryEnd+1; + int hotbarEnd = hotbarStart+8; + + Slot theSlot = this.inventorySlots.get(slot); + + if(theSlot != null && theSlot.getHasStack()){ + ItemStack newStack = theSlot.getStack(); + ItemStack currentStack = newStack.copy(); + + //Other Slots in Inventory excluded + if(slot >= inventoryStart){ + //Shift from Inventory + if(this.isVoid || !this.filter.check(newStack, this.bagInventory.slots) || !this.mergeItemStack(newStack, 4, 32, false)){ + if(slot >= inventoryStart && slot <= inventoryEnd){ + if(!this.mergeItemStack(newStack, hotbarStart, hotbarEnd+1, false)){ + return null; + } + } + else if(slot >= inventoryEnd+1 && slot < hotbarEnd+1 && !this.mergeItemStack(newStack, inventoryStart, inventoryEnd+1, false)){ + return null; + } + } + // + + } + else if(!this.mergeItemStack(newStack, inventoryStart, hotbarEnd+1, false)){ + return null; + } + + if(newStack.stackSize <= 0){ + theSlot.putStack(null); + } + else{ + theSlot.onSlotChanged(); + } + + if(newStack.stackSize == currentStack.stackSize){ + return null; + } + theSlot.onPickupFromSlot(player, newStack); + + return currentStack; + } + return null; + } + + @Override + public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, EntityPlayer player){ + if(slotId >= 0 && slotId < this.inventorySlots.size() && this.getSlot(slotId) instanceof SlotFilter){ + //Calls the Filter's SlotClick function + return ((SlotFilter)this.getSlot(slotId)).slotClick(player); + } + else if(clickTypeIn == ClickType.SWAP && dragType == this.inventory.currentItem){ + return null; + } + else{ + return super.slotClick(slotId, dragType, clickTypeIn, player); + } + } + + @Override + public void onContainerClosed(EntityPlayer player){ + ItemStack stack = this.inventory.getCurrentItem(); + if(stack != null && stack.getItem() instanceof ItemBag){ + ItemDrill.writeSlotsToNBT(this.bagInventory.slots, this.inventory.getCurrentItem()); + NBTTagCompound compound = stack.getTagCompound(); + this.filter.writeToNBT(compound, "Filter"); + compound.setBoolean("AutoInsert", this.autoInsert); + } + super.onContainerClosed(player); + } + + @Override + public boolean canInteractWith(EntityPlayer player){ + return this.bagInventory.isUseableByPlayer(player); + } + + @Override + public void onButtonPressed(int buttonID, EntityPlayer player){ + if(buttonID == 0){ + this.autoInsert = !this.autoInsert; + } + else{ + this.filter.onButtonPressed(buttonID); + } + } + + public static class InventoryBag implements IInventory{ + + public ItemStack[] slots; + + public InventoryBag(boolean isVoid){ + this.slots = new ItemStack[getSlotAmount(isVoid)]; + } + + @Override + public String getName(){ + return "bag"; + } + + @Override + public int getInventoryStackLimit(){ + return 64; + } + + @Override + public void markDirty(){ + + } + + @Override + public boolean isUseableByPlayer(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){ + return 0; + } + + @Override + public void setField(int id, int value){ + + } + + @Override + public int getFieldCount(){ + return 0; + } + + @Override + public void clear(){ + int length = this.slots.length; + this.slots = new ItemStack[length]; + } + + @Override + public void setInventorySlotContents(int i, ItemStack stack){ + this.slots[i] = stack; + this.markDirty(); + } + + @Override + public int getSizeInventory(){ + return this.slots.length; + } + + @Override + public ItemStack getStackInSlot(int i){ + if(i < this.getSizeInventory()){ + return this.slots[i]; + } + return null; + } + + @Override + public ItemStack decrStackSize(int i, int j){ + if(this.slots[i] != null){ + ItemStack stackAt; + if(this.slots[i].stackSize <= j){ + stackAt = this.slots[i]; + this.slots[i] = null; + this.markDirty(); + return stackAt; + } + else{ + stackAt = this.slots[i].splitStack(j); + if(this.slots[i].stackSize <= 0){ + this.slots[i] = null; + } + this.markDirty(); + return stackAt; + } + } + return null; + } + + @Override + public ItemStack removeStackFromSlot(int index){ + ItemStack stack = this.slots[index]; + this.slots[index] = null; + return stack; + } + + @Override + public boolean hasCustomName(){ + return false; + } + + + @Override + public ITextComponent getDisplayName(){ + return new TextComponentTranslation(this.getName()); + } + } +} \ No newline at end of file diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/GuiHandler.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/GuiHandler.java index 600f10cfc..83f067c17 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/GuiHandler.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/GuiHandler.java @@ -96,6 +96,10 @@ public class GuiHandler implements IGuiHandler{ return new ContainerMiner(entityPlayer.inventory, tile); case LASER_RELAY_ITEM_WHITELIST: return new ContainerLaserRelayItemWhitelist(entityPlayer.inventory, tile); + case BAG: + return new ContainerBag(entityPlayer.inventory, false); + case VOID_BAG: + return new ContainerBag(entityPlayer.inventory, true); default: return null; } @@ -172,6 +176,10 @@ public class GuiHandler implements IGuiHandler{ return new GuiBookletStand(tile); case LASER_RELAY_ITEM_WHITELIST: return new GuiLaserRelayItemWhitelist(entityPlayer.inventory, tile); + case BAG: + return new GuiBag(entityPlayer.inventory, false); + case VOID_BAG: + return new GuiBag(entityPlayer.inventory, true); default: return null; } @@ -209,7 +217,9 @@ public class GuiHandler implements IGuiHandler{ MINER, BOOK_STAND, LASER_RELAY_ITEM_WHITELIST, - FILTER(false); + FILTER(false), + BAG(false), + VOID_BAG(false); public final boolean checkTileEntity; diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiBag.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiBag.java new file mode 100644 index 000000000..2e6a9fb2a --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiBag.java @@ -0,0 +1,113 @@ +/* + * This file ("GuiBag.java") is part of the Actually Additions mod for Minecraft. + * It is created and owned by Ellpeck and distributed + * under the Actually Additions License to be found at + * http://ellpeck.de/actaddlicense + * View the source code at https://github.com/Ellpeck/ActuallyAdditions + * + * © 2015-2016 Ellpeck + */ + +package de.ellpeck.actuallyadditions.mod.inventory.gui; + +import de.ellpeck.actuallyadditions.mod.inventory.ContainerBag; +import de.ellpeck.actuallyadditions.mod.network.PacketClientToServer; +import de.ellpeck.actuallyadditions.mod.network.PacketHandler; +import de.ellpeck.actuallyadditions.mod.util.AssetUtil; +import de.ellpeck.actuallyadditions.mod.util.ModUtil; +import de.ellpeck.actuallyadditions.mod.util.StringUtil; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.text.TextFormatting; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +@SideOnly(Side.CLIENT) +public class GuiBag extends GuiContainer{ + + private static final ResourceLocation RES_LOC = AssetUtil.getGuiLocation("guiBag"); + private static final ResourceLocation RES_LOC_VOID = AssetUtil.getGuiLocation("guiVoidBag"); + + private final ContainerBag container; + private FilterSettingsGui filter; + private final boolean isVoid; + private GuiButton buttonAutoInsert; + + public GuiBag(InventoryPlayer inventory, boolean isVoid){ + this(isVoid, new ContainerBag(inventory, isVoid)); + } + + private GuiBag(boolean isVoid, ContainerBag container){ + super(container); + this.xSize = 176; + this.ySize = 90+86; + this.isVoid = isVoid; + this.container = container; + } + + @Override + public void initGui(){ + super.initGui(); + + this.filter = new FilterSettingsGui(this.container.filter, this.guiLeft+138, this.guiTop+10, this.buttonList); + + this.buttonAutoInsert = new GuiButton(0, this.guiLeft-21, this.guiTop+8, 20, 20, (this.container.autoInsert ? TextFormatting.DARK_GREEN : TextFormatting.RED)+"I"); + this.buttonList.add(this.buttonAutoInsert); + } + + @Override + protected void actionPerformed(GuiButton button) throws IOException{ + NBTTagCompound data = new NBTTagCompound(); + data.setInteger("ButtonID", button.id); + data.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId()); + data.setInteger("WorldID", Minecraft.getMinecraft().theWorld.provider.getDimension()); + PacketHandler.theNetwork.sendToServer(new PacketClientToServer(data, PacketHandler.GUI_BUTTON_TO_CONTAINER_HANDLER)); + } + + @Override + public void updateScreen(){ + super.updateScreen(); + this.filter.update(); + + this.buttonAutoInsert.displayString = (this.container.autoInsert ? TextFormatting.DARK_GREEN : TextFormatting.RED)+"I"; + } + + @Override + public void drawGuiContainerForegroundLayer(int x, int y){ + AssetUtil.displayNameString(this.fontRendererObj, this.xSize, -10, StringUtil.localize("container."+ModUtil.MOD_ID+"."+(this.isVoid ? "voidBag" : "bag")+".name")); + } + + @Override + public void drawGuiContainerBackgroundLayer(float f, int x, int y){ + GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); + + this.mc.getTextureManager().bindTexture(AssetUtil.GUI_INVENTORY_LOCATION); + this.drawTexturedModalRect(this.guiLeft, this.guiTop+90, 0, 0, 176, 86); + + this.mc.getTextureManager().bindTexture(this.isVoid ? RES_LOC_VOID : RES_LOC); + this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, 176, 90); + } + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks){ + super.drawScreen(mouseX, mouseY, partialTicks); + this.filter.drawHover(mouseX, mouseY); + + if(this.buttonAutoInsert.isMouseOver()){ + List text = new ArrayList(); + text.add(TextFormatting.BOLD+"Auto-Insert "+(this.container.autoInsert ? "On" : "Off")); + text.addAll(this.mc.fontRendererObj.listFormattedStringToWidth("Turn this on to make items that get picked up automatically go into the bag.", 200)); + this.drawHoveringText(text, mouseX, mouseY); + } + } +} \ No newline at end of file diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/slot/SlotDeletion.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/slot/SlotDeletion.java new file mode 100644 index 000000000..535d704b1 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/slot/SlotDeletion.java @@ -0,0 +1,33 @@ +/* + * This file ("SlotDeletion.java") is part of the Actually Additions mod for Minecraft. + * It is created and owned by Ellpeck and distributed + * under the Actually Additions License to be found at + * http://ellpeck.de/actaddlicense + * View the source code at https://github.com/Ellpeck/ActuallyAdditions + * + * © 2015-2016 Ellpeck + */ + +package de.ellpeck.actuallyadditions.mod.inventory.slot; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; + +public class SlotDeletion extends Slot{ + + public SlotDeletion(IInventory inv, int slot, int x, int y){ + super(inv, slot, x, y); + } + + @Override + public void putStack(ItemStack stack){ + this.onSlotChanged(); + } + + @Override + public boolean canTakeStack(EntityPlayer player){ + return false; + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/items/InitItems.java b/src/main/java/de/ellpeck/actuallyadditions/mod/items/InitItems.java index b30eb8262..4ef742c7b 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/items/InitItems.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/items/InitItems.java @@ -211,9 +211,14 @@ public final class InitItems{ public static Item itemPlayerProbe; public static Item itemWorm; + public static Item itemBag; + public static Item itemVoidBag; + public static void init(){ ModUtil.LOGGER.info("Initializing Items..."); + itemBag = new ItemBag("itemBag", false); + itemVoidBag = new ItemBag("itemVoidBag", true); itemWorm = new ItemWorm("itemWorm"); itemPlayerProbe = new ItemPlayerProbe("itemPlayerProbe"); itemFilter = new ItemFilter("itemFilter"); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemBag.java b/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemBag.java new file mode 100644 index 000000000..ee443ce43 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemBag.java @@ -0,0 +1,110 @@ +/* + * This file ("ItemBag.java") is part of the Actually Additions mod for Minecraft. + * It is created and owned by Ellpeck and distributed + * under the Actually Additions License to be found at + * http://ellpeck.de/actaddlicense + * View the source code at https://github.com/Ellpeck/ActuallyAdditions + * + * © 2015-2016 Ellpeck + */ + +package de.ellpeck.actuallyadditions.mod.items; + +import de.ellpeck.actuallyadditions.mod.ActuallyAdditions; +import de.ellpeck.actuallyadditions.mod.inventory.ContainerBag; +import de.ellpeck.actuallyadditions.mod.inventory.GuiHandler.GuiTypes; +import de.ellpeck.actuallyadditions.mod.items.base.ItemBase; +import de.ellpeck.actuallyadditions.mod.tile.FilterSettings; +import de.ellpeck.actuallyadditions.mod.util.ItemUtil; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ActionResult; +import net.minecraft.util.EnumActionResult; +import net.minecraft.util.EnumHand; +import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.player.EntityItemPickupEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +public class ItemBag extends ItemBase{ + + private final boolean isVoid; + + public ItemBag(String name, boolean isVoid){ + super(name); + this.isVoid = isVoid; + this.setMaxStackSize(1); + + MinecraftForge.EVENT_BUS.register(this); + } + + @SubscribeEvent + public void onItemPickup(EntityItemPickupEvent event){ + EntityPlayer player = event.getEntityPlayer(); + EntityItem item = event.getItem(); + if(item != null && !item.isDead){ + ItemStack stack = item.getEntityItem(); + if(stack != null && stack.getItem() != null){ + for(int i = 0; i < player.inventory.getSizeInventory(); i++){ + if(i != player.inventory.currentItem){ + + ItemStack invStack = player.inventory.getStackInSlot(i); + if(invStack != null && invStack.getItem() instanceof ItemBag && invStack.hasTagCompound()){ + if(invStack.getTagCompound().getBoolean("AutoInsert")){ + boolean needsSave = false; + + boolean isVoid = ((ItemBag)invStack.getItem()).isVoid; + ItemStack[] inventory = new ItemStack[ContainerBag.getSlotAmount(isVoid)]; + ItemDrill.loadSlotsFromNBT(inventory, invStack); + + FilterSettings filter = new FilterSettings(0, 4, false, false, false, 0, 0); + filter.readFromNBT(invStack.getTagCompound(), "Filter"); + if(filter.check(stack, inventory)){ + for(int j = 4; j < inventory.length; j++){ + ItemStack bagStack = inventory[j]; + if(bagStack != null){ + if(ItemUtil.canBeStacked(bagStack, stack)){ + int maxTransfer = Math.min(stack.stackSize, stack.getMaxStackSize()-bagStack.stackSize); + if(maxTransfer > 0){ + bagStack.stackSize += maxTransfer; + stack.stackSize -= maxTransfer; + needsSave = true; + } + } + } + else{ + inventory[j] = stack.copy(); + stack.stackSize = 0; + needsSave = true; + } + + if(stack.stackSize <= 0){ + break; + } + } + } + + if(needsSave && !isVoid){ //void doesn't need to save as items are deleted + ItemDrill.writeSlotsToNBT(inventory, invStack); + } + } + } + } + + if(stack.stackSize <= 0){ + break; + } + } + } + } + } + + @Override + public ActionResult onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand){ + if(!world.isRemote){ + player.openGui(ActuallyAdditions.instance, (this.isVoid ? GuiTypes.VOID_BAG : GuiTypes.BAG).ordinal(), world, (int)player.posX, (int)player.posY, (int)player.posZ); + } + return new ActionResult(EnumActionResult.PASS, stack); + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java b/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java index 308895ccb..955211d8d 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/network/PacketHandler.java @@ -20,9 +20,11 @@ import de.ellpeck.actuallyadditions.mod.tile.TileEntityBookletStand; import de.ellpeck.actuallyadditions.mod.util.AssetUtil; import de.ellpeck.actuallyadditions.mod.util.ModUtil; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.inventory.Container; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; @@ -39,7 +41,9 @@ import java.util.UUID; public final class PacketHandler{ + public static SimpleNetworkWrapper theNetwork; public static final List DATA_HANDLERS = new ArrayList(); + public static final IDataHandler PARTICLE_HANDLER = new IDataHandler(){ @Override @SideOnly(Side.CLIENT) @@ -75,6 +79,19 @@ public final class PacketHandler{ } } }; + public static final IDataHandler GUI_BUTTON_TO_CONTAINER_HANDLER = new IDataHandler(){ + @Override + public void handleData(NBTTagCompound compound){ + World world = DimensionManager.getWorld(compound.getInteger("WorldID")); + Entity entity = world.getEntityByID(compound.getInteger("PlayerID")); + if(entity != null && entity instanceof EntityPlayer){ + Container container = ((EntityPlayer)entity).openContainer; + if(container != null && container instanceof IButtonReactor){ + ((IButtonReactor)container).onButtonPressed(compound.getInteger("ButtonID"), (EntityPlayer)entity); + } + } + } + }; public static final IDataHandler GUI_NUMBER_TO_TILE_HANDLER = new IDataHandler(){ @Override public void handleData(NBTTagCompound compound){ @@ -111,7 +128,6 @@ public final class PacketHandler{ } } }; - public static SimpleNetworkWrapper theNetwork; public static final IDataHandler BOOKLET_STAND_BUTTON_HANDLER = new IDataHandler(){ @Override public void handleData(NBTTagCompound compound){ @@ -162,5 +178,6 @@ public final class PacketHandler{ DATA_HANDLERS.add(GUI_NUMBER_TO_TILE_HANDLER); DATA_HANDLERS.add(CHANGE_PLAYER_DATA_HANDLER); DATA_HANDLERS.add(PLAYER_DATA_TO_CLIENT_HANDLER); + DATA_HANDLERS.add(GUI_BUTTON_TO_CONTAINER_HANDLER); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java index 4bf775d72..ee5ec8612 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java @@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; import de.ellpeck.actuallyadditions.mod.network.gui.INumberReactor; +import de.ellpeck.actuallyadditions.mod.util.ItemUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; @@ -154,7 +155,7 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt } } //If ESD has enough Space & Item in question is on whitelist - if(tempStack != null && (this.slots[0] == null || (tempStack.isItemEqual(this.slots[0]) && this.slots[0].stackSize < maxSize)) && this.checkBothFilters(tempStack, false)){ + if(tempStack != null && (this.slots[0] == null || (ItemUtil.canBeStacked(tempStack, this.slots[0]) && this.slots[0].stackSize < maxSize)) && this.checkBothFilters(tempStack, false)){ //Deal with ISided if(theSided != null){ //Check if Item can be inserted from any Side (Because Sidedness gets ignored!) @@ -183,7 +184,7 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt if(can){ //If ESD already has Items if(this.slots[0] != null){ - if(theStack.isItemEqual(this.slots[0])){ + if(ItemUtil.canBeStacked(theStack, this.slots[0])){ //If the StackSize is smaller than the space the ESD has left if(theStack.stackSize <= maxSize-this.slots[0].stackSize){ this.slots[0].stackSize += theStack.stackSize; @@ -247,7 +248,7 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt maxSize = theInventory.getInventoryStackLimit(); } } - if(theInventory.isItemValidForSlot(i, this.slots[0]) && (tempStack == null || (tempStack.isItemEqual(this.slots[0]) && tempStack.stackSize < maxSize)) && this.checkBothFilters(this.slots[0], true)){ + if(theInventory.isItemValidForSlot(i, this.slots[0]) && (tempStack == null || (ItemUtil.canBeStacked(tempStack, this.slots[0]) && tempStack.stackSize < maxSize)) && this.checkBothFilters(this.slots[0], true)){ if(theSided != null){ for(int j = 0; j <= 5; j++){ if(theSided.canInsertItem(i, this.slots[0], EnumFacing.values()[j])){ @@ -271,7 +272,7 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt if(can){ if(theStack != null){ ItemStack copiedStack = theStack.copy(); - if(copiedStack.isItemEqual(this.slots[0])){ + if(ItemUtil.canBeStacked(copiedStack, this.slots[0])){ if(this.slots[0].stackSize <= maxSize-copiedStack.stackSize){ copiedStack.stackSize += this.slots[0].stackSize; this.slots[0] = null; diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/util/ItemUtil.java b/src/main/java/de/ellpeck/actuallyadditions/mod/util/ItemUtil.java index a52ad9a29..5613ec52f 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/util/ItemUtil.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/util/ItemUtil.java @@ -124,4 +124,8 @@ public final class ItemUtil{ } } } + + public static boolean canBeStacked(ItemStack stack1, ItemStack stack2){ + return ItemStack.areItemsEqual(stack1, stack2) && ItemStack.areItemStackTagsEqual(stack1, stack2); + } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java b/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java index 66cf83b49..3d51eafce 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java @@ -312,7 +312,7 @@ public final class WorldUtil{ for(int i = start; i < end; i++){ if(shouldAlwaysWork || ((!(inventory instanceof ISidedInventory) || ((ISidedInventory)inventory).canInsertItem(i, stackToPutIn, side)) && inventory.isItemValidForSlot(i, stackToPutIn))){ ItemStack stackInQuestion = inventory.getStackInSlot(i); - if(stackToPutIn != null && (stackInQuestion == null || (stackInQuestion.isItemEqual(stackToPutIn) && stackInQuestion.getMaxStackSize() >= stackInQuestion.stackSize+stackToPutIn.stackSize))){ + if(stackToPutIn != null && (stackInQuestion == null || (ItemUtil.canBeStacked(stackInQuestion, stackToPutIn) && stackInQuestion.getMaxStackSize() >= stackInQuestion.stackSize+stackToPutIn.stackSize))){ if(stackInQuestion == null){ inventory.setInventorySlotContents(i, stackToPutIn.copy()); } diff --git a/src/main/resources/assets/actuallyadditions/textures/gui/guiBag.png b/src/main/resources/assets/actuallyadditions/textures/gui/guiBag.png new file mode 100644 index 0000000000000000000000000000000000000000..57bc5c0fadddf10fe12babb4e6b258c31d070763 GIT binary patch literal 2024 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5893O0R7}x|G$6&2?&#~tz_78O`%fY(kk47* z5n0T@z_%KN8T;NG`2!S`C~=J_3C>R|DNig)Whh9@%q!8$OD$0_(KFDq%rUO>V_;xU z^K@|xsfc@fC9o^Tl85!e8!hdVt2V9A$!4=WZy>RAL2UVg({q@=FSiSE^S{*p|A+e% zXQvzXA2hS#{`cu}`bR%B{`2$maY2r%Tff3h!=G3`zrH?R*>rck`;2m%)%ic48b4x4 zv3>oX;Y9io#uHW7?lVp}-@!cL>ze({3iAco6<$Zyvpd)+^Eq7G{O>pOhtJQ?Z~y%J z`}=R-*SpQV?s@HH+C!7O^);rSeZeZ$^E;USpDR-O?Pp)o}{{btF5-*)cwGv{9Jx$h+7 z38`M)*Y2}7YW%(vvwU{zWtIC+7*cwVP5Zj|Y{Zn>ymgn)9?G1QU&OGO#?-$V#$u`SXTL7lTrlg&Y?+2N7jJB;+alS$>HOKj>%5sa|CF!% ze5-sl+;{V?y|b@~eP15TeBtlEt1CWRodgBsvfusd(?Ee#b?yIG#ud-@PhND+?*7IR z9d?vNt?}&tdZlr{^AIWH@~yw@>tFxP%VOAdf4@{|oO&cE5tjV_c=Ze@ft~-g-;Uwx zpYot9H=kRBbHb2Lca(%3CAl{3^ZljgvhS~jC)}#2-Txny?!E8J>+t<|_~LV~Rlphe zo&CO9pt9q2$3V?b9}T&A+y9glk;!)mCCofufhvV|Lo23F;D(?;VAw2 z8fx5tMQHk8S%#lI7NF#B&G1RGkYUrjujLF+uvR0$DsJ$DJCBa{*E2e`f1Mt?WS$hr Ny`HXqF6*2UngCD1rGx+g literal 0 HcmV?d00001 diff --git a/src/main/resources/assets/actuallyadditions/textures/gui/guiVoidBag.png b/src/main/resources/assets/actuallyadditions/textures/gui/guiVoidBag.png new file mode 100644 index 0000000000000000000000000000000000000000..9a3f8d4ce710de1c7e397f254a5b062b837eac99 GIT binary patch literal 2021 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5893O0R7}x|G$6&2?&#~tz_78O`%fY(kk47* z5n0T@z_%KN8T;NG`2!S`C~=J_3C>R|DNig)Whh9@%q!8$OD$0_(KFDq%rUO>V_;xU z_H=O!sfc@fEwJy3l>lo%*-@_u(cekY3Cnjy_k;@dRlhlM*z(fUd%riVG`X(2@9n$q z@9%FvA9+(&`}6c)_T@Y+YYvp326F@ceV+F!$5842-dpXvpRak-H?>pn-RtY?6b3vhC0IO8-x3&zrVk4|NQy+`Nw*wa=$0If37+3{)uHj>jCeV zN6cPMof`|ZdJBK0i5Nrh6u+FwaW`)Qec}XEn9OM4I^vXoLT6QZBrqCK!Xo%T>xaBo zLMy>CeR)WsG#Z@v(hyC9GxX)L+~TmZ(yC)YT7<@u^ z72byCxD5GE89EHhwC?W6h2}VDvY(oF#|)a|ZnfJ;aW|~=GS8b0&2ev?<$75Uge;Zb zeioGPfjRD*V*fFw2VUnltvR#d(r0hDh?aX{*y{ScKOdi8eH;$Vs}P-{eV1Yuif#S( z)9{$((;rJa1rOct|1md$A?BgR+55MW&YiMW)lH6@1IkMajl64r?6--%{#cgRVcHp>;{D8~L_`0Gn?#Y^z^v^n}(m=iQ_FuKGJeSOP;kAbERk2OajJH<#f;!lqBTNst+%s2gn_w#DFb~z$ zL*1Y!Kcn}qzLyFNr@+~JPRx%yWoA8XOU1O@8HUrNyopOZlH$=nOvf6mll z*f+(!SGPg#Y5FFHA3<`a+Zd{*%s