From 199b307ef700795d0e6e200fcfb66b010e4e7b2a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 12 Aug 2016 18:14:41 +0200 Subject: [PATCH] added Item auto-split to the double furnace and double crusher Closes #196 --- .../mod/inventory/gui/GuiFurnaceDouble.java | 29 ++++++++- .../mod/inventory/gui/GuiGrinder.java | 28 +++++++++ .../mod/tile/TileEntityFurnaceDouble.java | 61 ++++++++++++++++--- .../mod/tile/TileEntityGrinder.java | 23 ++++++- 4 files changed, 131 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiFurnaceDouble.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiFurnaceDouble.java index 88d0a01c6..a4b8a0d7a 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiFurnaceDouble.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiFurnaceDouble.java @@ -11,17 +11,22 @@ package de.ellpeck.actuallyadditions.mod.inventory.gui; import de.ellpeck.actuallyadditions.mod.inventory.ContainerFurnaceDouble; +import de.ellpeck.actuallyadditions.mod.network.PacketHandler; +import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper; import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase; import de.ellpeck.actuallyadditions.mod.tile.TileEntityFurnaceDouble; import de.ellpeck.actuallyadditions.mod.util.AssetUtil; +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.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.Collections; @SideOnly(Side.CLIENT) public class GuiFurnaceDouble extends GuiContainer{ @@ -30,6 +35,8 @@ public class GuiFurnaceDouble extends GuiContainer{ private final TileEntityFurnaceDouble tileFurnace; private EnergyDisplay energy; + private GuiButton buttonAutoSplit; + public GuiFurnaceDouble(InventoryPlayer inventory, TileEntityBase tile){ super(new ContainerFurnaceDouble(inventory, tile)); this.tileFurnace = (TileEntityFurnaceDouble)tile; @@ -41,6 +48,10 @@ public class GuiFurnaceDouble extends GuiContainer{ public void drawScreen(int x, int y, float f){ super.drawScreen(x, y, f); this.energy.drawOverlay(x, y); + + if(this.buttonAutoSplit.isMouseOver()){ + this.drawHoveringText(Collections.singletonList(TextFormatting.BOLD+"Auto-Split Items "+(this.tileFurnace.isAutoSplit ? "On" : "Off")), x, y); + } } @Override @@ -49,11 +60,27 @@ public class GuiFurnaceDouble extends GuiContainer{ this.energy.onMouseClick(mouseX, mouseY, mouseButton); } - @Override public void initGui(){ super.initGui(); this.energy = new EnergyDisplay(this.guiLeft+27, this.guiTop+5, this.tileFurnace.storage); + + this.buttonAutoSplit = new GuiInputter.SmallerButton(0, this.guiLeft, this.guiTop, "S"); + this.buttonList.add(this.buttonAutoSplit); + } + + @Override + public void updateScreen(){ + super.updateScreen(); + + this.buttonAutoSplit.displayString = (this.tileFurnace.isAutoSplit ? TextFormatting.DARK_GREEN : TextFormatting.RED)+"S"; + } + + @Override + protected void actionPerformed(GuiButton button) throws IOException{ + if(button.id == 0){ + PacketHandlerHelper.sendButtonPacket(this.tileFurnace, button.id); + } } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiGrinder.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiGrinder.java index 12dadefe1..9e03512e2 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiGrinder.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiGrinder.java @@ -11,17 +11,21 @@ package de.ellpeck.actuallyadditions.mod.inventory.gui; import de.ellpeck.actuallyadditions.mod.inventory.ContainerGrinder; +import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper; import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase; import de.ellpeck.actuallyadditions.mod.tile.TileEntityGrinder; import de.ellpeck.actuallyadditions.mod.util.AssetUtil; +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.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.Collections; @SideOnly(Side.CLIENT) public class GuiGrinder extends GuiContainer{ @@ -32,6 +36,8 @@ public class GuiGrinder extends GuiContainer{ private final boolean isDouble; private EnergyDisplay energy; + private GuiButton buttonAutoSplit; + public GuiGrinder(InventoryPlayer inventoryPlayer, TileEntityBase tile){ this(inventoryPlayer, tile, false); } @@ -48,6 +54,11 @@ public class GuiGrinder extends GuiContainer{ public void initGui(){ super.initGui(); this.energy = new EnergyDisplay(this.guiLeft+(this.isDouble ? 13 : 42), this.guiTop+5, this.tileGrinder.storage); + + if(this.isDouble){ + this.buttonAutoSplit = new GuiInputter.SmallerButton(0, this.guiLeft-10, this.guiTop, "S"); + this.buttonList.add(this.buttonAutoSplit); + } } @Override @@ -56,11 +67,28 @@ public class GuiGrinder extends GuiContainer{ this.energy.onMouseClick(mouseX, mouseY, mouseButton); } + @Override + protected void actionPerformed(GuiButton button) throws IOException{ + if(this.isDouble && button.id == 0){ + PacketHandlerHelper.sendButtonPacket(this.tileGrinder, button.id); + } + } + + @Override + public void updateScreen(){ + super.updateScreen(); + + this.buttonAutoSplit.displayString = (this.tileGrinder.isAutoSplit ? TextFormatting.DARK_GREEN : TextFormatting.RED)+"S"; + } @Override public void drawScreen(int x, int y, float f){ super.drawScreen(x, y, f); this.energy.drawOverlay(x, y); + + if(this.isDouble && this.buttonAutoSplit.isMouseOver()){ + this.drawHoveringText(Collections.singletonList(TextFormatting.BOLD+"Auto-Split Items "+(this.tileGrinder.isAutoSplit ? "On" : "Off")), x, y); + } } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceDouble.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceDouble.java index dbac070c3..90c71f9f6 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceDouble.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceDouble.java @@ -12,8 +12,11 @@ package de.ellpeck.actuallyadditions.mod.tile; import cofh.api.energy.EnergyStorage; import cofh.api.energy.IEnergyReceiver; +import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; +import de.ellpeck.actuallyadditions.mod.util.ItemUtil; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.nbt.NBTTagCompound; @@ -21,7 +24,7 @@ import net.minecraft.util.EnumFacing; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements IEnergyReceiver{ +public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements IEnergyReceiver, IButtonReactor{ public static final int SLOT_INPUT_1 = 0; public static final int SLOT_OUTPUT_1 = 1; @@ -36,6 +39,9 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements private int lastFirstSmelt; private int lastSecondSmelt; + public boolean isAutoSplit; + private boolean lastAutoSplit; + public TileEntityFurnaceDouble(){ super(4, "furnaceDouble"); } @@ -46,6 +52,7 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements if(type != NBTType.SAVE_BLOCK){ compound.setInteger("FirstSmeltTime", this.firstSmeltTime); compound.setInteger("SecondSmeltTime", this.secondSmeltTime); + compound.setBoolean("IsAutoSplit", this.isAutoSplit); } this.storage.writeToNBT(compound); } @@ -56,6 +63,7 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements if(type != NBTType.SAVE_BLOCK){ this.firstSmeltTime = compound.getInteger("FirstSmeltTime"); this.secondSmeltTime = compound.getInteger("SecondSmeltTime"); + this.isAutoSplit = compound.getBoolean("IsAutoSplit"); } this.storage.readFromNBT(compound); } @@ -64,6 +72,10 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements public void updateEntity(){ super.updateEntity(); if(!this.worldObj.isRemote){ + if(this.isAutoSplit){ + autoSplit(this.slots, SLOT_INPUT_1, SLOT_INPUT_2); + } + boolean flag = this.firstSmeltTime > 0 || this.secondSmeltTime > 0; boolean canSmeltOnFirst = this.canSmeltOn(SLOT_INPUT_1, SLOT_OUTPUT_1); @@ -112,14 +124,46 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements } } - if((this.lastEnergy != this.storage.getEnergyStored() || this.lastFirstSmelt != this.firstSmeltTime || this.lastSecondSmelt != this.secondSmeltTime) && this.sendUpdateWithInterval()){ + if((this.lastEnergy != this.storage.getEnergyStored() || this.lastFirstSmelt != this.firstSmeltTime || this.lastSecondSmelt != this.secondSmeltTime || this.isAutoSplit != this.lastAutoSplit) && this.sendUpdateWithInterval()){ this.lastEnergy = this.storage.getEnergyStored(); this.lastFirstSmelt = this.firstSmeltTime; + this.lastAutoSplit = this.isAutoSplit; this.lastSecondSmelt = this.secondSmeltTime; } } } + public static void autoSplit(ItemStack[] slots, int slot1, int slot2){ + ItemStack first = slots[slot1]; + ItemStack second = slots[slot2]; + + if(first != null || second != null){ + ItemStack toSplit = null; + if(first == null && second != null){ + toSplit = second; + } + else if(second == null && first != null){ + toSplit = first; + } + else if(ItemUtil.canBeStacked(first, second)){ + if(first.stackSize < first.getMaxStackSize() || second.stackSize < second.getMaxStackSize()){ + if(!((first.stackSize <= second.stackSize+1 && first.stackSize >= second.stackSize-1) || (second.stackSize <= first.stackSize+1 && second.stackSize >= first.stackSize-1))){ + toSplit = first; + toSplit.stackSize += second.stackSize; + } + } + } + + if(toSplit != null && toSplit.stackSize > 1){ + ItemStack splitFirst = toSplit.copy(); + ItemStack secondSplit = splitFirst.splitStack(splitFirst.stackSize/2); + + slots[slot1] = splitFirst; + slots[slot2] = secondSplit; + } + } + } + @Override public boolean isItemValidForSlot(int i, ItemStack stack){ return (i == SLOT_INPUT_1 || i == SLOT_INPUT_2) && FurnaceRecipes.instance().getSmeltingResult(stack) != null; @@ -154,11 +198,6 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements } } - @SideOnly(Side.CLIENT) - public int getEnergyScaled(int i){ - return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored(); - } - @SideOnly(Side.CLIENT) public int getFirstTimeToScale(int i){ return this.firstSmeltTime*i/SMELT_TIME; @@ -198,4 +237,12 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements public boolean canConnectEnergy(EnumFacing from){ return true; } + + @Override + public void onButtonPressed(int buttonID, EntityPlayer player){ + if(buttonID == 0){ + this.isAutoSplit = !this.isAutoSplit; + this.markDirty(); + } + } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGrinder.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGrinder.java index a76e29d96..d6dfed01b 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGrinder.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGrinder.java @@ -15,10 +15,12 @@ import cofh.api.energy.EnergyStorage; import cofh.api.energy.IEnergyReceiver; import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues; import de.ellpeck.actuallyadditions.mod.misc.SoundHandler; +import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; import de.ellpeck.actuallyadditions.mod.recipe.CrusherRecipeRegistry; import de.ellpeck.actuallyadditions.mod.util.Util; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; @@ -28,7 +30,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; import java.util.List; -public class TileEntityGrinder extends TileEntityInventoryBase implements IEnergyReceiver{ +public class TileEntityGrinder extends TileEntityInventoryBase implements IEnergyReceiver, IButtonReactor{ public static final int SLOT_INPUT_1 = 0; public static final int SLOT_OUTPUT_1_1 = 1; @@ -45,6 +47,9 @@ public class TileEntityGrinder extends TileEntityInventoryBase implements IEnerg private int lastFirstCrush; private int lastSecondCrush; + public boolean isAutoSplit; + private boolean lastAutoSplit; + public TileEntityGrinder(int slots, String name){ super(slots, name); } @@ -79,6 +84,7 @@ public class TileEntityGrinder extends TileEntityInventoryBase implements IEnerg if(type != NBTType.SAVE_BLOCK){ compound.setInteger("FirstCrushTime", this.firstCrushTime); compound.setInteger("SecondCrushTime", this.secondCrushTime); + compound.setBoolean("IsAutoSplit", this.isAutoSplit); } this.storage.writeToNBT(compound); super.writeSyncableNBT(compound, type); @@ -89,6 +95,7 @@ public class TileEntityGrinder extends TileEntityInventoryBase implements IEnerg if(type != NBTType.SAVE_BLOCK){ this.firstCrushTime = compound.getInteger("FirstCrushTime"); this.secondCrushTime = compound.getInteger("SecondCrushTime"); + this.isAutoSplit = compound.getBoolean("IsAutoSplit"); } this.storage.readFromNBT(compound); super.readSyncableNBT(compound, type); @@ -98,6 +105,10 @@ public class TileEntityGrinder extends TileEntityInventoryBase implements IEnerg public void updateEntity(){ super.updateEntity(); if(!this.worldObj.isRemote){ + if(this.isDouble && this.isAutoSplit){ + TileEntityFurnaceDouble.autoSplit(this.slots, SLOT_INPUT_1, SLOT_INPUT_2); + } + boolean flag = this.firstCrushTime > 0 || this.secondCrushTime > 0; boolean canCrushOnFirst = this.canCrushOn(SLOT_INPUT_1, SLOT_OUTPUT_1_1, SLOT_OUTPUT_1_2); @@ -159,10 +170,11 @@ public class TileEntityGrinder extends TileEntityInventoryBase implements IEnerg } } - if((this.lastEnergy != this.storage.getEnergyStored() || this.lastFirstCrush != this.firstCrushTime || this.lastSecondCrush != this.secondCrushTime) && this.sendUpdateWithInterval()){ + if((this.lastEnergy != this.storage.getEnergyStored() || this.lastFirstCrush != this.firstCrushTime || this.lastSecondCrush != this.secondCrushTime || this.isAutoSplit != this.lastAutoSplit) && this.sendUpdateWithInterval()){ this.lastEnergy = this.storage.getEnergyStored(); this.lastFirstCrush = this.firstCrushTime; this.lastSecondCrush = this.secondCrushTime; + this.lastAutoSplit = this.isAutoSplit; } if(shouldPlaySound && !ConfigBoolValues.LESS_SOUND.isEnabled()){ @@ -270,4 +282,11 @@ public class TileEntityGrinder extends TileEntityInventoryBase implements IEnerg return slot == SLOT_OUTPUT_1_1 || slot == SLOT_OUTPUT_1_2 || slot == SLOT_OUTPUT_2_1 || slot == SLOT_OUTPUT_2_2; } + @Override + public void onButtonPressed(int buttonID, EntityPlayer player){ + if(buttonID == 0){ + this.isAutoSplit = !this.isAutoSplit; + this.markDirty(); + } + } }