ActuallyAdditions/src/main/java/ellpeck/actuallyadditions/tile/TileEntityGrinder.java

273 lines
10 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("TileEntityGrinder.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://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* <EFBFBD> 2015 Ellpeck
*/
2015-03-07 12:51:28 +01:00
package ellpeck.actuallyadditions.tile;
2015-10-03 22:09:53 +02:00
2015-05-20 22:39:43 +02:00
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyReceiver;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ellpeck.actuallyadditions.config.values.ConfigIntValues;
import ellpeck.actuallyadditions.network.sync.IPacketSyncerToClient;
import ellpeck.actuallyadditions.network.sync.PacketSyncerToClient;
import ellpeck.actuallyadditions.recipe.CrusherRecipeRegistry;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-05-20 22:39:43 +02:00
import net.minecraftforge.common.util.ForgeDirection;
import java.util.ArrayList;
import java.util.Random;
public class TileEntityGrinder extends TileEntityInventoryBase implements IEnergyReceiver, IPacketSyncerToClient{
2015-05-20 22:39:43 +02:00
2015-10-03 10:16:18 +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;
public EnergyStorage storage = new EnergyStorage(60000);
2015-10-03 10:16:18 +02:00
public int firstCrushTime;
public int secondCrushTime;
public boolean isDouble;
private int lastEnergy;
2015-10-03 10:16:18 +02:00
private int lastFirstCrush;
private int lastSecondCrush;
2015-10-03 10:19:40 +02:00
2015-10-03 10:16:18 +02:00
public TileEntityGrinder(int slots, String name){
super(slots, name);
}
2015-10-03 10:19:40 +02:00
2015-10-03 10:16:18 +02:00
public TileEntityGrinder(){
super(3, "grinder");
this.isDouble = false;
}
2015-05-20 22:39:43 +02:00
@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;
}
@Override
public int[] getValues(){
return new int[]{this.storage.getEnergyStored(), this.firstCrushTime, this.secondCrushTime};
}
@Override
public void setValues(int[] values){
this.storage.setEnergyStored(values[0]);
this.firstCrushTime = values[1];
this.secondCrushTime = values[2];
}
@Override
public void sendUpdate(){
PacketSyncerToClient.sendPacket(this);
}
@Override
@SuppressWarnings("unchecked")
public void updateEntity(){
if(!worldObj.isRemote){
2015-06-21 02:28:49 +02:00
boolean flag = this.firstCrushTime > 0 || this.secondCrushTime > 0;
boolean canCrushOnFirst = this.canCrushOn(SLOT_INPUT_1, SLOT_OUTPUT_1_1, SLOT_OUTPUT_1_2);
boolean canCrushOnSecond = false;
2015-10-03 10:16:18 +02:00
if(this.isDouble){
canCrushOnSecond = this.canCrushOn(SLOT_INPUT_2, SLOT_OUTPUT_2_1, SLOT_OUTPUT_2_2);
}
if(canCrushOnFirst){
2015-08-15 20:41:45 +02:00
if(this.storage.getEnergyStored() >= getEnergyUse()){
this.firstCrushTime++;
2015-08-15 20:41:45 +02:00
if(this.firstCrushTime >= getMaxCrushTime()){
this.finishCrushing(SLOT_INPUT_1, SLOT_OUTPUT_1_1, SLOT_OUTPUT_1_2);
this.firstCrushTime = 0;
}
}
}
2015-10-02 16:48:01 +02:00
else{
this.firstCrushTime = 0;
}
if(this.isDouble){
if(canCrushOnSecond){
2015-08-15 20:41:45 +02:00
if(this.storage.getEnergyStored() >= getEnergyUse()){
this.secondCrushTime++;
2015-08-15 20:41:45 +02:00
if(this.secondCrushTime >= getMaxCrushTime()){
this.finishCrushing(SLOT_INPUT_2, SLOT_OUTPUT_2_1, SLOT_OUTPUT_2_2);
this.secondCrushTime = 0;
}
}
}
2015-10-02 16:48:01 +02:00
else{
this.secondCrushTime = 0;
}
}
2015-10-02 16:48:01 +02:00
if(this.storage.getEnergyStored() >= getEnergyUse() && this.firstCrushTime > 0 || this.secondCrushTime > 0){
this.storage.extractEnergy(getEnergyUse(), false);
}
2015-06-21 02:28:49 +02:00
if(flag != (this.firstCrushTime > 0 || this.secondCrushTime > 0)){
this.markDirty();
2015-06-21 02:28:49 +02:00
int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
if(meta == 1){
2015-10-02 16:48:01 +02:00
if(!this.canCrushOn(SLOT_INPUT_1, SLOT_OUTPUT_1_1, SLOT_OUTPUT_1_2) && (!this.isDouble || !this.canCrushOn(SLOT_INPUT_2, SLOT_OUTPUT_2_1, SLOT_OUTPUT_2_2))){
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 2);
}
}
else{
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 1, 2);
2015-06-21 02:28:49 +02:00
}
}
if(lastEnergy != this.storage.getEnergyStored() || this.lastFirstCrush != this.firstCrushTime || this.lastSecondCrush != this.secondCrushTime){
this.lastEnergy = this.storage.getEnergyStored();
this.lastFirstCrush = this.firstCrushTime;
this.lastSecondCrush = this.secondCrushTime;
this.sendUpdate();
}
}
}
public boolean canCrushOn(int theInput, int theFirstOutput, int theSecondOutput){
if(this.slots[theInput] != null){
ArrayList<ItemStack> outputOnes = CrusherRecipeRegistry.getOutputOnes(this.slots[theInput]);
if(outputOnes != null && !outputOnes.isEmpty()){
ItemStack outputOne = outputOnes.get(0);
ArrayList<ItemStack> outputTwos = CrusherRecipeRegistry.getOutputTwos(this.slots[theInput]);
ItemStack outputTwo = outputTwos == null ? null : outputTwos.get(0);
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;
}
2015-10-03 10:19:40 +02:00
private int getEnergyUse(){
return this.isDouble ? ConfigIntValues.GRINDER_DOUBLE_ENERGY_USED.getValue() : ConfigIntValues.GRINDER_ENERGY_USED.getValue();
}
private int getMaxCrushTime(){
return this.isDouble ? ConfigIntValues.GRINDER_DOUBLE_CRUSH_TIME.getValue() : ConfigIntValues.GRINDER_CRUSH_TIME.getValue();
}
public void finishCrushing(int theInput, int theFirstOutput, int theSecondOutput){
ArrayList<ItemStack> outputOnes = CrusherRecipeRegistry.getOutputOnes(this.slots[theInput]);
if(outputOnes != null){
ItemStack outputOne = outputOnes.get(0);
if(outputOne != null){
if(this.slots[theFirstOutput] == null){
this.slots[theFirstOutput] = outputOne.copy();
}
else if(this.slots[theFirstOutput].getItem() == outputOne.getItem()){
this.slots[theFirstOutput].stackSize += outputOne.stackSize;
}
2015-10-02 16:48:01 +02:00
}
2015-06-21 02:28:49 +02:00
}
ArrayList<ItemStack> outputTwos = CrusherRecipeRegistry.getOutputTwos(this.slots[theInput]);
if(outputTwos != null){
ItemStack outputTwo = outputTwos.get(0);
if(outputTwo != null){
int rand = new Random().nextInt(100)+1;
if(rand <= CrusherRecipeRegistry.getOutputTwoChance(this.slots[theInput])){
if(this.slots[theSecondOutput] == null){
this.slots[theSecondOutput] = outputTwo.copy();
}
else if(this.slots[theSecondOutput].getItem() == outputTwo.getItem()){
this.slots[theSecondOutput].stackSize += outputTwo.stackSize;
}
2015-10-02 16:48:01 +02:00
}
}
}
this.slots[theInput].stackSize--;
2015-10-03 10:16:18 +02:00
if(this.slots[theInput].stackSize <= 0){
this.slots[theInput] = null;
}
}
@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);
super.readFromNBT(compound);
}
2015-10-03 22:13:57 +02:00
@Override
public void writeToNBT(NBTTagCompound compound){
compound.setInteger("FirstCrushTime", this.firstCrushTime);
compound.setInteger("SecondCrushTime", this.secondCrushTime);
this.storage.writeToNBT(compound);
super.writeToNBT(compound);
}
2015-10-03 10:19:40 +02:00
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
return (i == SLOT_INPUT_1 || i == SLOT_INPUT_2) && CrusherRecipeRegistry.getRecipeFromInput(stack) != null;
2015-10-03 10:19:40 +02:00
}
@SideOnly(Side.CLIENT)
2015-05-20 22:39:43 +02:00
public int getEnergyScaled(int i){
2015-10-02 16:48:01 +02:00
return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored();
}
@SideOnly(Side.CLIENT)
public int getFirstTimeToScale(int i){
2015-10-02 16:48:01 +02:00
return this.firstCrushTime*i/this.getMaxCrushTime();
}
@SideOnly(Side.CLIENT)
public int getSecondTimeToScale(int i){
2015-10-02 16:48:01 +02:00
return this.secondCrushTime*i/this.getMaxCrushTime();
}
@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-10-03 10:16:18 +02:00
public static class TileEntityGrinderDouble extends TileEntityGrinder{
public TileEntityGrinderDouble(){
super(6, "grinderDouble");
this.isDouble = true;
}
}
}