2015-03-07 12:51:28 +01:00
|
|
|
package ellpeck.actuallyadditions.tile;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2015-05-20 22:39:43 +02:00
|
|
|
import cofh.api.energy.EnergyStorage;
|
|
|
|
import cofh.api.energy.IEnergyReceiver;
|
2015-03-07 02:23:31 +01:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2015-04-24 19:22:03 +02:00
|
|
|
import ellpeck.actuallyadditions.config.values.ConfigIntValues;
|
2015-03-07 12:51:28 +01:00
|
|
|
import ellpeck.actuallyadditions.recipe.GrinderRecipes;
|
2015-03-07 02:23:31 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-05-20 22:39:43 +02:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
2015-05-20 22:39:43 +02:00
|
|
|
public class TileEntityGrinder extends TileEntityInventoryBase implements IEnergyReceiver{
|
|
|
|
|
|
|
|
public EnergyStorage storage = new EnergyStorage(60000, energyUsePerTick+20);
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate){
|
|
|
|
return this.storage.receiveEnergy(maxReceive, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergyStored(ForgeDirection from){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxEnergyStored(ForgeDirection from){
|
|
|
|
return this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canConnectEnergy(ForgeDirection from){
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2015-05-04 17:26:50 +02:00
|
|
|
public static class TileEntityGrinderDouble extends TileEntityGrinder{
|
|
|
|
|
|
|
|
public TileEntityGrinderDouble(){
|
2015-05-20 22:39:43 +02:00
|
|
|
super(6, "grinderDouble");
|
2015-05-04 17:26:50 +02:00
|
|
|
this.isDouble = true;
|
|
|
|
this.maxCrushTime = this.getStandardSpeed();
|
2015-05-20 22:39:43 +02:00
|
|
|
energyUsePerTick = ConfigIntValues.GRINDER_DOUBLE_ENERGY_USED.getValue();
|
2015-05-04 17:26:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-20 22:39:43 +02:00
|
|
|
public static final int SLOT_INPUT_1 = 0;
|
|
|
|
public static final int SLOT_OUTPUT_1_1 = 1;
|
|
|
|
public static final int SLOT_OUTPUT_1_2 = 2;
|
|
|
|
public static final int SLOT_INPUT_2 = 3;
|
|
|
|
public static final int SLOT_OUTPUT_2_1 = 4;
|
|
|
|
public static final int SLOT_OUTPUT_2_2 = 5;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2015-05-20 22:39:43 +02:00
|
|
|
public static int energyUsePerTick;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
|
|
|
public int maxCrushTime;
|
|
|
|
|
|
|
|
public int firstCrushTime;
|
|
|
|
public int secondCrushTime;
|
|
|
|
|
|
|
|
public boolean isDouble;
|
|
|
|
|
2015-05-04 17:26:50 +02:00
|
|
|
public TileEntityGrinder(int slots, String name){
|
|
|
|
super(slots, name);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
2015-05-04 17:26:50 +02:00
|
|
|
public TileEntityGrinder(){
|
2015-05-20 22:39:43 +02:00
|
|
|
super(3, "grinder");
|
2015-05-04 17:26:50 +02:00
|
|
|
this.isDouble = false;
|
2015-04-24 19:22:03 +02:00
|
|
|
this.maxCrushTime = this.getStandardSpeed();
|
2015-05-20 22:39:43 +02:00
|
|
|
energyUsePerTick = ConfigIntValues.GRINDER_ENERGY_USED.getValue();
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void updateEntity(){
|
|
|
|
if(!worldObj.isRemote){
|
2015-05-07 16:36:29 +02:00
|
|
|
|
|
|
|
//TODO Remove after some Updating
|
|
|
|
if(this.isDouble && this.getClass() != TileEntityGrinderDouble.class){
|
|
|
|
ItemStack[] theSlots = this.slots.clone();
|
|
|
|
worldObj.removeTileEntity(xCoord, yCoord, zCoord);
|
|
|
|
worldObj.setTileEntity(xCoord, yCoord, zCoord, new TileEntityGrinderDouble());
|
|
|
|
((TileEntityGrinderDouble)worldObj.getTileEntity(xCoord, yCoord, zCoord)).slots = theSlots.clone();
|
|
|
|
}
|
|
|
|
|
2015-03-07 02:23:31 +01:00
|
|
|
boolean canCrushOnFirst = this.canCrushOn(SLOT_INPUT_1, SLOT_OUTPUT_1_1, SLOT_OUTPUT_1_2);
|
|
|
|
boolean canCrushOnSecond = false;
|
|
|
|
if(this.isDouble) canCrushOnSecond = this.canCrushOn(SLOT_INPUT_2, SLOT_OUTPUT_2_1, SLOT_OUTPUT_2_2);
|
|
|
|
|
2015-05-20 22:39:43 +02:00
|
|
|
if(this.storage.getEnergyStored() >= energyUsePerTick){
|
2015-03-07 02:23:31 +01:00
|
|
|
if(canCrushOnFirst){
|
|
|
|
this.firstCrushTime++;
|
|
|
|
if(this.firstCrushTime >= maxCrushTime){
|
|
|
|
this.finishCrushing(SLOT_INPUT_1, SLOT_OUTPUT_1_1, SLOT_OUTPUT_1_2);
|
|
|
|
this.firstCrushTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else this.firstCrushTime = 0;
|
|
|
|
|
|
|
|
if(this.isDouble){
|
|
|
|
if(canCrushOnSecond){
|
|
|
|
this.secondCrushTime++;
|
|
|
|
if(this.secondCrushTime >= maxCrushTime){
|
|
|
|
this.finishCrushing(SLOT_INPUT_2, SLOT_OUTPUT_2_1, SLOT_OUTPUT_2_2);
|
|
|
|
this.secondCrushTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else this.secondCrushTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.firstCrushTime = 0;
|
|
|
|
this.secondCrushTime = 0;
|
|
|
|
}
|
|
|
|
|
2015-05-20 22:39:43 +02:00
|
|
|
if(this.firstCrushTime > 0 || this.secondCrushTime > 0) this.storage.extractEnergy(energyUsePerTick, false);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canCrushOn(int theInput, int theFirstOutput, int theSecondOutput){
|
|
|
|
if(this.slots[theInput] != null){
|
|
|
|
ItemStack outputOne = GrinderRecipes.instance().getOutput(this.slots[theInput], false);
|
|
|
|
ItemStack outputTwo = GrinderRecipes.instance().getOutput(this.slots[theInput], true);
|
|
|
|
if(this.slots[theInput] != null){
|
|
|
|
if(outputOne != null){
|
|
|
|
if((this.slots[theFirstOutput] == null || (this.slots[theFirstOutput].isItemEqual(outputOne) && this.slots[theFirstOutput].stackSize <= this.slots[theFirstOutput].getMaxStackSize()-outputOne.stackSize)) && (outputTwo == null || (this.slots[theSecondOutput] == null || (this.slots[theSecondOutput].isItemEqual(outputTwo) && this.slots[theSecondOutput].stackSize <= this.slots[theSecondOutput].getMaxStackSize()-outputTwo.stackSize)))){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void finishCrushing(int theInput, int theFirstOutput, int theSecondOutput){
|
|
|
|
ItemStack outputOnFirst = GrinderRecipes.instance().getOutput(this.slots[theInput], false);
|
|
|
|
if (this.slots[theFirstOutput] == null) this.slots[theFirstOutput] = outputOnFirst.copy();
|
|
|
|
else if(this.slots[theFirstOutput].getItem() == outputOnFirst.getItem()) this.slots[theFirstOutput].stackSize += outputOnFirst.stackSize;
|
|
|
|
|
|
|
|
int chance = GrinderRecipes.instance().getSecondChance(this.slots[theInput]);
|
|
|
|
ItemStack outputOnSecond = GrinderRecipes.instance().getOutput(this.slots[theInput], true);
|
|
|
|
if(outputOnSecond != null){
|
|
|
|
int rand = new Random().nextInt(100) + 1;
|
|
|
|
if(rand <= chance){
|
|
|
|
if(this.slots[theSecondOutput] == null) this.slots[theSecondOutput] = outputOnSecond.copy();
|
|
|
|
else if(this.slots[theSecondOutput].getItem() == outputOnSecond.getItem()) this.slots[theSecondOutput].stackSize += outputOnSecond.stackSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.slots[theInput].stackSize--;
|
|
|
|
if (this.slots[theInput].stackSize <= 0) this.slots[theInput] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound compound){
|
|
|
|
compound.setInteger("FirstCrushTime", this.firstCrushTime);
|
|
|
|
compound.setInteger("SecondCrushTime", this.secondCrushTime);
|
2015-05-20 22:39:43 +02:00
|
|
|
this.storage.writeToNBT(compound);
|
2015-03-07 02:23:31 +01:00
|
|
|
super.writeToNBT(compound);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound compound){
|
|
|
|
this.firstCrushTime = compound.getInteger("FirstCrushTime");
|
|
|
|
this.secondCrushTime = compound.getInteger("SecondCrushTime");
|
2015-05-20 22:39:43 +02:00
|
|
|
this.storage.readFromNBT(compound);
|
2015-03-07 02:23:31 +01:00
|
|
|
super.readFromNBT(compound);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
2015-05-20 22:39:43 +02:00
|
|
|
public int getEnergyScaled(int i){
|
|
|
|
return this.getEnergyStored(ForgeDirection.UNKNOWN) * i / this.getMaxEnergyStored(ForgeDirection.UNKNOWN);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getFirstTimeToScale(int i){
|
|
|
|
return this.firstCrushTime * i / this.maxCrushTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getSecondTimeToScale(int i){
|
|
|
|
return this.secondCrushTime * i / this.maxCrushTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
2015-05-20 22:39:43 +02:00
|
|
|
return (i == SLOT_INPUT_1 || i == SLOT_INPUT_2) && GrinderRecipes.instance().getOutput(stack, false) != null;
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, int side){
|
|
|
|
return this.isItemValidForSlot(slot, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, int side){
|
2015-05-20 22:39:43 +02:00
|
|
|
return slot == SLOT_OUTPUT_1_1 || slot == SLOT_OUTPUT_1_2 || slot == SLOT_OUTPUT_2_1 || slot == SLOT_OUTPUT_2_2;
|
2015-04-02 12:06:42 +02:00
|
|
|
}
|
2015-04-24 19:22:03 +02:00
|
|
|
|
|
|
|
public int getStandardSpeed(){
|
|
|
|
return this.isDouble ? ConfigIntValues.GRINDER_DOUBLE_CRUSH_TIME.getValue() : ConfigIntValues.GRINDER_CRUSH_TIME.getValue();
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|