ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFarmer.java

214 lines
8.3 KiB
Java
Raw Normal View History

2016-10-31 18:03:18 +01:00
/*
* This file ("TileEntityFarmer.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://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2016 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.tile;
2016-10-31 19:03:04 +01:00
import cofh.api.energy.EnergyStorage;
2016-10-31 18:03:18 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.BlockDirt;
import net.minecraft.block.BlockGrass;
2016-10-31 18:03:18 +01:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
2016-11-01 15:46:02 +01:00
import net.minecraft.item.ItemBlock;
2016-10-31 18:03:18 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.IPlantable;
import java.util.List;
2016-10-31 19:03:04 +01:00
public class TileEntityFarmer extends TileEntityInventoryBase implements ICustomEnergyReceiver{
public static final int USE_PER_OPERATION = 1500;
public final EnergyStorage storage = new EnergyStorage(100000);
2016-10-31 18:03:18 +01:00
private int waitTime;
private int checkX;
private int checkY;
2016-10-31 19:03:04 +01:00
private int lastEnergy;
2016-10-31 18:03:18 +01:00
public TileEntityFarmer(){
super(12, "farmer");
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
super.writeSyncableNBT(compound, type);
if(type != NBTType.SAVE_BLOCK){
compound.setInteger("WaitTime", this.waitTime);
compound.setInteger("CheckX", this.checkX);
compound.setInteger("CheckY", this.checkY);
}
2016-10-31 19:03:04 +01:00
this.storage.writeToNBT(compound);
2016-10-31 18:03:18 +01:00
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
super.readSyncableNBT(compound, type);
if(type != NBTType.SAVE_BLOCK){
this.waitTime = compound.getInteger("WaitTime");
this.checkX = compound.getInteger("CheckX");
this.checkY = compound.getInteger("CheckY");
}
2016-10-31 19:03:04 +01:00
this.storage.readFromNBT(compound);
2016-10-31 18:03:18 +01:00
}
@Override
public void updateEntity(){
super.updateEntity();
2016-10-31 19:03:04 +01:00
if(!this.worldObj.isRemote){
if(!this.isRedstonePowered){
if(this.waitTime > 0){
this.waitTime--;
if(this.waitTime <= 0){
if(this.storage.getEnergyStored() >= USE_PER_OPERATION){
boolean didSomething = false;
int radiusAroundCenter = 4;
IBlockState state = this.worldObj.getBlockState(this.pos);
int meta = state.getBlock().getMetaFromState(state);
EnumFacing side = meta == 0 ? EnumFacing.NORTH : (meta == 1 ? EnumFacing.SOUTH : (meta == 2 ? EnumFacing.WEST : EnumFacing.EAST));
BlockPos center = this.pos.offset(side, radiusAroundCenter+1);
BlockPos plant = center.add(this.checkX, 0, this.checkY);
IBlockState plantState = this.worldObj.getBlockState(plant);
Block plantBlock = plantState.getBlock();
if(plantBlock instanceof BlockCrops){
if(((BlockCrops)plantBlock).isMaxAge(plantState)){
List<ItemStack> drops = plantBlock.getDrops(this.worldObj, plant, plantState, 0);
if(WorldUtil.addToInventory(this, 6, 12, drops, EnumFacing.UP, false, true)){
WorldUtil.addToInventory(this, 6, 12, drops, EnumFacing.UP, true, true);
this.worldObj.playEvent(2001, plant, Block.getStateId(plantState));
2016-10-31 19:03:04 +01:00
this.worldObj.setBlockToAir(plant);
didSomething = true;
}
2016-10-31 18:03:18 +01:00
}
}
2016-10-31 19:03:04 +01:00
else if(plantBlock.isReplaceable(this.worldObj, plant)){
BlockPos farmland = plant.down();
IBlockState farmlandState = this.worldObj.getBlockState(farmland);
Block farmlandBlock = farmlandState.getBlock();
IBlockState toPlant = this.getFirstPlantablePlantFromSlots(plant);
if(toPlant != null){
this.worldObj.setBlockState(plant, toPlant, 3);
didSomething = true;
2016-10-31 19:03:04 +01:00
}
else if(farmlandBlock instanceof BlockDirt || farmlandBlock instanceof BlockGrass){
this.worldObj.setBlockState(farmland, Blocks.FARMLAND.getDefaultState(), 2);
this.worldObj.setBlockToAir(plant);
this.worldObj.playSound(null, farmland, SoundEvents.ITEM_HOE_TILL, SoundCategory.BLOCKS, 1.0F, 1.0F);
didSomething = true;
}
}
if(didSomething){
this.storage.extractEnergy(USE_PER_OPERATION, false);
2016-10-31 18:03:18 +01:00
}
2016-10-31 19:03:04 +01:00
this.checkX++;
if(this.checkX > radiusAroundCenter){
this.checkX = -radiusAroundCenter;
this.checkY++;
if(this.checkY > radiusAroundCenter){
this.checkY = -radiusAroundCenter;
}
}
2016-10-31 18:03:18 +01:00
}
}
}
2016-10-31 19:03:04 +01:00
else{
this.waitTime = 5;
}
2016-10-31 18:03:18 +01:00
}
2016-10-31 19:03:04 +01:00
if(this.lastEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
this.lastEnergy = this.storage.getEnergyStored();
2016-10-31 18:03:18 +01:00
}
}
}
2016-11-01 15:46:02 +01:00
private IBlockState getFirstPlantablePlantFromSlots(BlockPos pos){
2016-10-31 18:03:18 +01:00
for(int i = 0; i < 6; i++){
ItemStack stack = this.slots[i];
if(stack != null){
2016-11-01 15:46:02 +01:00
IPlantable plantable = null;
2016-10-31 18:03:18 +01:00
Item item = stack.getItem();
if(item instanceof IPlantable){
2016-11-01 15:46:02 +01:00
plantable = (IPlantable)item;
}
else if(item instanceof ItemBlock){
Block block = Block.getBlockFromItem(item);
if(block instanceof IPlantable){
plantable = (IPlantable)block;
}
}
if(plantable != null){
IBlockState state = plantable.getPlant(this.worldObj, pos);
if(state != null && state.getBlock() instanceof BlockCrops && state.getBlock().canPlaceBlockAt(this.worldObj, pos)){
2016-10-31 18:03:18 +01:00
this.decrStackSize(i, 1);
return state;
}
}
}
}
return null;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
return i < 6 && stack != null && stack.getItem() instanceof IPlantable;
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
return this.isItemValidForSlot(slot, stack);
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
return slot >= 6;
}
2016-10-31 19:03:04 +01:00
@Override
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){
return this.storage.receiveEnergy(maxReceive, simulate);
}
@Override
public int getEnergyStored(EnumFacing from){
return this.storage.getEnergyStored();
}
@Override
public int getMaxEnergyStored(EnumFacing from){
return this.storage.getMaxEnergyStored();
}
@Override
public boolean canConnectEnergy(EnumFacing from){
return true;
}
2016-10-31 18:03:18 +01:00
}