2015-08-29 14:33:25 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("TileEntityGrinder.java") is part of the Actually Additions mod for Minecraft.
|
2015-08-29 14:33:25 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
2016-05-16 22:52:27 +02:00
|
|
|
* http://ellpeck.de/actaddlicense
|
2015-08-29 14:33:25 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-2016 Ellpeck
|
2015-08-29 14:33:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2015-10-03 22:09:53 +02:00
|
|
|
|
2015-05-20 22:39:43 +02:00
|
|
|
import cofh.api.energy.EnergyStorage;
|
2016-05-01 22:26:26 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.SoundHandler;
|
2016-08-12 18:14:41 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.recipe.CrusherRecipeRegistry;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.Util;
|
2016-07-04 20:15:41 +02:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
2016-08-12 18:14:41 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2015-03-07 02:23:31 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-01-07 19:47:53 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-05-01 22:26:26 +02:00
|
|
|
import net.minecraft.util.SoundCategory;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2016-08-31 20:16:36 +02:00
|
|
|
public class TileEntityGrinder extends TileEntityInventoryBase implements ICustomEnergyReceiver, IButtonReactor{
|
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;
|
2016-05-04 22:40:58 +02:00
|
|
|
public static final int ENERGY_USE = 40;
|
2016-11-23 18:10:55 +01:00
|
|
|
public final CustomEnergyStorage storage = new CustomEnergyStorage(60000, 100);
|
2015-10-03 10:16:18 +02:00
|
|
|
public int firstCrushTime;
|
|
|
|
public int secondCrushTime;
|
|
|
|
public boolean isDouble;
|
2016-09-12 20:45:29 +02:00
|
|
|
public boolean isAutoSplit;
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastEnergy;
|
2015-10-03 10:16:18 +02:00
|
|
|
private int lastFirstCrush;
|
|
|
|
private int lastSecondCrush;
|
2016-08-12 18:14:41 +02:00
|
|
|
private boolean lastAutoSplit;
|
|
|
|
|
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
|
2016-01-07 19:47:53 +01:00
|
|
|
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){
|
2015-05-20 22:39:43 +02:00
|
|
|
return this.storage.receiveEnergy(maxReceive, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public int getEnergyStored(EnumFacing from){
|
2015-05-20 22:39:43 +02:00
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public int getMaxEnergyStored(EnumFacing from){
|
2015-05-20 22:39:43 +02:00
|
|
|
return this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public boolean canConnectEnergy(EnumFacing from){
|
2015-05-20 22:39:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
|
|
compound.setInteger("FirstCrushTime", this.firstCrushTime);
|
|
|
|
compound.setInteger("SecondCrushTime", this.secondCrushTime);
|
2016-08-12 18:14:41 +02:00
|
|
|
compound.setBoolean("IsAutoSplit", this.isAutoSplit);
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
|
|
this.firstCrushTime = compound.getInteger("FirstCrushTime");
|
|
|
|
this.secondCrushTime = compound.getInteger("SecondCrushTime");
|
2016-08-12 18:14:41 +02:00
|
|
|
this.isAutoSplit = compound.getBoolean("IsAutoSplit");
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
this.storage.readFromNBT(compound);
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
2015-03-07 02:23:31 +01:00
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2016-05-02 17:46:53 +02:00
|
|
|
if(!this.worldObj.isRemote){
|
2016-08-12 18:14:41 +02:00
|
|
|
if(this.isDouble && this.isAutoSplit){
|
|
|
|
TileEntityFurnaceDouble.autoSplit(this.slots, SLOT_INPUT_1, SLOT_INPUT_2);
|
|
|
|
}
|
|
|
|
|
2015-06-21 02:28:49 +02:00
|
|
|
boolean flag = this.firstCrushTime > 0 || this.secondCrushTime > 0;
|
|
|
|
|
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;
|
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);
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2015-12-13 14:36:34 +01:00
|
|
|
boolean shouldPlaySound = false;
|
|
|
|
|
2015-06-13 12:52:22 +02:00
|
|
|
if(canCrushOnFirst){
|
2016-05-04 22:40:58 +02:00
|
|
|
if(this.storage.getEnergyStored() >= ENERGY_USE){
|
2016-11-21 16:23:48 +01:00
|
|
|
if(this.firstCrushTime%20 == 0){
|
2015-12-13 14:36:34 +01:00
|
|
|
shouldPlaySound = true;
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
this.firstCrushTime++;
|
2016-05-02 17:46:53 +02:00
|
|
|
if(this.firstCrushTime >= this.getMaxCrushTime()){
|
2015-03-07 02:23:31 +01:00
|
|
|
this.finishCrushing(SLOT_INPUT_1, SLOT_OUTPUT_1_1, SLOT_OUTPUT_1_2);
|
|
|
|
this.firstCrushTime = 0;
|
|
|
|
}
|
2016-11-23 18:10:55 +01:00
|
|
|
this.storage.extractEnergyInternal(ENERGY_USE, false);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
2015-06-13 12:52:22 +02:00
|
|
|
}
|
2015-10-02 16:48:01 +02:00
|
|
|
else{
|
|
|
|
this.firstCrushTime = 0;
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2015-06-13 12:52:22 +02:00
|
|
|
if(this.isDouble){
|
|
|
|
if(canCrushOnSecond){
|
2016-05-04 22:40:58 +02:00
|
|
|
if(this.storage.getEnergyStored() >= ENERGY_USE){
|
2016-11-21 16:23:48 +01:00
|
|
|
if(this.secondCrushTime%20 == 0){
|
2015-12-13 14:36:34 +01:00
|
|
|
shouldPlaySound = true;
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
this.secondCrushTime++;
|
2016-05-02 17:46:53 +02:00
|
|
|
if(this.secondCrushTime >= this.getMaxCrushTime()){
|
2015-03-07 02:23:31 +01:00
|
|
|
this.finishCrushing(SLOT_INPUT_2, SLOT_OUTPUT_2_1, SLOT_OUTPUT_2_2);
|
|
|
|
this.secondCrushTime = 0;
|
|
|
|
}
|
2016-11-23 18:10:55 +01:00
|
|
|
this.storage.extractEnergyInternal(ENERGY_USE, false);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
}
|
2015-10-02 16:48:01 +02:00
|
|
|
else{
|
|
|
|
this.secondCrushTime = 0;
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
2016-08-12 18:14:41 +02:00
|
|
|
if((this.lastEnergy != this.storage.getEnergyStored() || this.lastFirstCrush != this.firstCrushTime || this.lastSecondCrush != this.secondCrushTime || this.isAutoSplit != this.lastAutoSplit) && this.sendUpdateWithInterval()){
|
2015-07-02 04:21:21 +02:00
|
|
|
this.lastEnergy = this.storage.getEnergyStored();
|
|
|
|
this.lastFirstCrush = this.firstCrushTime;
|
|
|
|
this.lastSecondCrush = this.secondCrushTime;
|
2016-08-12 18:14:41 +02:00
|
|
|
this.lastAutoSplit = this.isAutoSplit;
|
2015-07-02 04:21:21 +02:00
|
|
|
}
|
2015-12-13 14:36:34 +01:00
|
|
|
|
2016-11-07 19:58:34 +01:00
|
|
|
if(shouldPlaySound){
|
2016-08-05 03:25:12 +02:00
|
|
|
this.worldObj.playSound(null, this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), SoundHandler.crusher, SoundCategory.BLOCKS, 0.15F, 1.0F);
|
2015-12-13 14:36:34 +01:00
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
2016-02-01 17:49:55 +01:00
|
|
|
return (i == SLOT_INPUT_1 || i == SLOT_INPUT_2) && CrusherRecipeRegistry.getRecipeFromInput(stack) != null;
|
|
|
|
}
|
|
|
|
|
2015-03-07 02:23:31 +01:00
|
|
|
public boolean canCrushOn(int theInput, int theFirstOutput, int theSecondOutput){
|
2016-11-20 20:13:26 +01:00
|
|
|
if(StackUtil.isValid(this.slots.get(theInput))){
|
|
|
|
ItemStack outputOne = CrusherRecipeRegistry.getOutputOnes(this.slots.get(theInput));
|
|
|
|
ItemStack outputTwo = CrusherRecipeRegistry.getOutputTwos(this.slots.get(theInput));
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(outputOne)){
|
2016-10-30 17:13:46 +01:00
|
|
|
if(outputOne.getItemDamage() == Util.WILDCARD){
|
|
|
|
outputOne.setItemDamage(0);
|
|
|
|
}
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(outputTwo) && outputTwo.getItemDamage() == Util.WILDCARD){
|
2016-10-30 17:13:46 +01:00
|
|
|
outputTwo.setItemDamage(0);
|
|
|
|
}
|
2016-11-20 20:13:26 +01:00
|
|
|
if((!StackUtil.isValid(this.slots.get(theFirstOutput)) || (this.slots.get(theFirstOutput).isItemEqual(outputOne) && StackUtil.getStackSize(this.slots.get(theFirstOutput)) <= this.slots.get(theFirstOutput).getMaxStackSize()-StackUtil.getStackSize(outputOne))) && (!StackUtil.isValid(outputTwo) || (!StackUtil.isValid(this.slots.get(theSecondOutput)) || (this.slots.get(theSecondOutput).isItemEqual(outputTwo) && StackUtil.getStackSize(this.slots.get(theSecondOutput)) <= this.slots.get(theSecondOutput).getMaxStackSize()-StackUtil.getStackSize(outputTwo))))){
|
2016-10-30 17:13:46 +01:00
|
|
|
return true;
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-03 10:19:40 +02:00
|
|
|
private int getMaxCrushTime(){
|
2015-11-28 19:02:01 +01:00
|
|
|
return this.isDouble ? 150 : 100;
|
2015-10-03 10:19:40 +02:00
|
|
|
}
|
|
|
|
|
2015-03-07 02:23:31 +01:00
|
|
|
public void finishCrushing(int theInput, int theFirstOutput, int theSecondOutput){
|
2016-11-20 20:13:26 +01:00
|
|
|
ItemStack outputOne = CrusherRecipeRegistry.getOutputOnes(this.slots.get(theInput));
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(outputOne)){
|
2016-10-30 17:13:46 +01:00
|
|
|
if(outputOne.getItemDamage() == Util.WILDCARD){
|
|
|
|
outputOne.setItemDamage(0);
|
|
|
|
}
|
2016-11-20 20:13:26 +01:00
|
|
|
if(!StackUtil.isValid(this.slots.get(theFirstOutput))){
|
|
|
|
this.slots.set(theFirstOutput, outputOne.copy());
|
2016-10-30 17:13:46 +01:00
|
|
|
}
|
2016-11-20 20:13:26 +01:00
|
|
|
else if(this.slots.get(theFirstOutput).getItem() == outputOne.getItem()){
|
|
|
|
this.slots.set(theFirstOutput, StackUtil.addStackSize(this.slots.get(theFirstOutput), StackUtil.getStackSize(outputOne)));
|
2015-10-02 16:48:01 +02:00
|
|
|
}
|
2015-06-21 02:28:49 +02:00
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2016-11-20 20:13:26 +01:00
|
|
|
ItemStack outputTwo = CrusherRecipeRegistry.getOutputTwos(this.slots.get(theInput));
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(outputTwo)){
|
2016-10-30 17:13:46 +01:00
|
|
|
if(outputTwo.getItemDamage() == Util.WILDCARD){
|
|
|
|
outputTwo.setItemDamage(0);
|
|
|
|
}
|
2016-11-02 19:36:32 +01:00
|
|
|
int rand = this.worldObj.rand.nextInt(100)+1;
|
2016-11-20 20:13:26 +01:00
|
|
|
if(rand <= CrusherRecipeRegistry.getOutputTwoChance(this.slots.get(theInput))){
|
|
|
|
if(!StackUtil.isValid(this.slots.get(theSecondOutput))){
|
|
|
|
this.slots.set(theSecondOutput, outputTwo.copy());
|
2015-12-16 20:06:12 +01:00
|
|
|
}
|
2016-11-20 20:13:26 +01:00
|
|
|
else if(this.slots.get(theSecondOutput).getItem() == outputTwo.getItem()){
|
|
|
|
this.slots.set(theSecondOutput, StackUtil.addStackSize(this.slots.get(theSecondOutput), StackUtil.getStackSize(outputTwo)));
|
2015-10-02 16:48:01 +02:00
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 20:13:26 +01:00
|
|
|
this.slots.set(theInput, StackUtil.addStackSize(this.slots.get(theInput), -1));
|
2015-03-07 02:23:31 +01: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();
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getFirstTimeToScale(int i){
|
2015-10-02 16:48:01 +02:00
|
|
|
return this.firstCrushTime*i/this.getMaxCrushTime();
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getSecondTimeToScale(int i){
|
2015-10-02 16:48:01 +02:00
|
|
|
return this.secondCrushTime*i/this.getMaxCrushTime();
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
|
2015-03-07 02:23:31 +01:00
|
|
|
return this.isItemValidForSlot(slot, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing 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
|
|
|
|
2016-08-12 18:14:41 +02:00
|
|
|
@Override
|
|
|
|
public void onButtonPressed(int buttonID, EntityPlayer player){
|
|
|
|
if(buttonID == 0){
|
|
|
|
this.isAutoSplit = !this.isAutoSplit;
|
|
|
|
this.markDirty();
|
|
|
|
}
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|