added Item auto-split to the double furnace and double crusher

Closes #196
This commit is contained in:
Ellpeck 2016-08-12 18:14:41 +02:00
parent 35aae43d21
commit 199b307ef7
4 changed files with 131 additions and 10 deletions

View file

@ -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

View file

@ -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

View file

@ -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();
}
}
}

View file

@ -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();
}
}
}