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

218 lines
8.8 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;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
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;
2016-11-26 08:58:42 +01:00
import net.minecraftforge.energy.IEnergyStorage;
2016-10-31 18:03:18 +01:00
import java.util.ArrayList;
2016-10-31 18:03:18 +01:00
import java.util.List;
2016-11-26 20:43:50 +01:00
public class TileEntityFarmer extends TileEntityInventoryBase{
2016-10-31 19:03:04 +01:00
public static final int USE_PER_OPERATION = 1500;
2016-11-26 20:43:50 +01:00
public final CustomEnergyStorage storage = new CustomEnergyStorage(100000, 1000, 0);
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");
}
2016-11-26 21:32:27 +01:00
public static IPlantable getPlantableFromStack(ItemStack stack){
Item item = stack.getItem();
if(item instanceof IPlantable){
return (IPlantable)item;
}
else if(item instanceof ItemBlock){
Block block = Block.getBlockFromItem(item);
if(block instanceof IPlantable){
return (IPlantable)block;
}
}
return null;
}
2016-10-31 18:03:18 +01:00
@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-11-26 21:32:27 +01:00
if(!this.world.isRemote){
2016-10-31 19:03:04 +01:00
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;
2016-11-26 21:32:27 +01:00
IBlockState state = this.world.getBlockState(this.pos);
2016-10-31 19:03:04 +01:00
int meta = state.getBlock().getMetaFromState(state);
2016-11-27 20:25:09 +01:00
BlockPos center = this.pos.offset(EnumFacing.getHorizontal(meta), radiusAroundCenter+1);
2016-10-31 19:03:04 +01:00
BlockPos plant = center.add(this.checkX, 0, this.checkY);
2016-11-26 21:32:27 +01:00
IBlockState plantState = this.world.getBlockState(plant);
2016-10-31 19:03:04 +01:00
Block plantBlock = plantState.getBlock();
if(plantBlock instanceof BlockCrops){
if(((BlockCrops)plantBlock).isMaxAge(plantState)){
List<ItemStack> seeds = new ArrayList<ItemStack>();
List<ItemStack> other = new ArrayList<ItemStack>();
2016-11-26 21:32:27 +01:00
List<ItemStack> drops = plantBlock.getDrops(this.world, plant, plantState, 0);
for(ItemStack stack : drops){
if(getPlantableFromStack(stack) != null){
seeds.add(stack);
}
else{
other.add(stack);
}
}
boolean putSeeds = true;
if(!WorldUtil.addToInventory(this.slots, 0, 6, seeds, false)){
other.addAll(seeds);
putSeeds = false;
}
2016-10-31 19:03:04 +01:00
if(WorldUtil.addToInventory(this.slots, 6, 12, other, false)){
WorldUtil.addToInventory(this.slots, 6, 12, other, true);
if(putSeeds){
WorldUtil.addToInventory(this.slots, 0, 6, seeds, true);
}
2016-10-31 19:03:04 +01:00
2016-11-26 21:32:27 +01:00
this.world.playEvent(2001, plant, Block.getStateId(plantState));
this.world.setBlockToAir(plant);
2016-10-31 19:03:04 +01:00
didSomething = true;
}
2016-10-31 18:03:18 +01:00
}
}
2016-11-26 21:32:27 +01:00
else if(plantBlock.isReplaceable(this.world, plant)){
2016-10-31 19:03:04 +01:00
BlockPos farmland = plant.down();
2016-11-26 21:32:27 +01:00
IBlockState farmlandState = this.world.getBlockState(farmland);
2016-10-31 19:03:04 +01:00
Block farmlandBlock = farmlandState.getBlock();
IBlockState toPlant = this.getFirstPlantablePlantFromSlots(plant);
if(toPlant != null){
2016-11-26 21:32:27 +01:00
this.world.setBlockState(plant, toPlant, 3);
didSomething = true;
2016-10-31 19:03:04 +01:00
}
else if(farmlandBlock instanceof BlockDirt || farmlandBlock instanceof BlockGrass){
2016-11-26 21:32:27 +01:00
this.world.setBlockState(farmland, Blocks.FARMLAND.getDefaultState(), 2);
this.world.setBlockToAir(plant);
this.world.playSound(null, farmland, SoundEvents.ITEM_HOE_TILL, SoundCategory.BLOCKS, 1.0F, 1.0F);
2016-10-31 19:03:04 +01:00
didSomething = true;
}
}
if(didSomething){
this.storage.extractEnergyInternal(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.getStackInSlot(i);
if(StackUtil.isValid(stack)){
IPlantable plantable = getPlantableFromStack(stack);
2016-11-01 15:46:02 +01:00
if(plantable != null){
2016-11-26 21:32:27 +01:00
IBlockState state = plantable.getPlant(this.world, pos);
if(state != null && state.getBlock() instanceof BlockCrops && state.getBlock().canPlaceBlockAt(this.world, pos)){
this.slots.decrStackSize(i, 1);
2016-10-31 18:03:18 +01:00
return state;
}
}
}
}
return null;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
return i < 6 && StackUtil.isValid(stack) && getPlantableFromStack(stack) != null;
2016-10-31 18:03:18 +01:00
}
@Override
public boolean canExtractItem(int slot, ItemStack stack){
2016-10-31 18:03:18 +01:00
return slot >= 6;
}
2016-10-31 19:03:04 +01:00
2016-11-26 08:58:42 +01:00
@Override
public IEnergyStorage getEnergyStorage(EnumFacing facing){
return this.storage;
}
2016-10-31 18:03:18 +01:00
}