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
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2016-10-31 18:03:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2016-12-04 15:03:01 +01:00
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
2017-02-22 13:28:53 +01:00
|
|
|
import de.ellpeck.actuallyadditions.api.farmer.FarmerResult;
|
2016-12-04 15:03:01 +01:00
|
|
|
import de.ellpeck.actuallyadditions.api.farmer.IFarmerBehavior;
|
|
|
|
import de.ellpeck.actuallyadditions.api.internal.IFarmer;
|
2016-11-16 20:31:16 +01:00
|
|
|
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.state.IBlockState;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-12-04 15:03:01 +01:00
|
|
|
import net.minecraft.world.World;
|
2016-11-26 08:58:42 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2016-10-31 18:03:18 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public class TileEntityFarmer extends TileEntityInventoryBase implements IFarmer {
|
2016-10-31 19:03:04 +01:00
|
|
|
|
2017-02-22 13:28:53 +01:00
|
|
|
private static final List<IFarmerBehavior> SORTED_FARMER_BEHAVIORS = new ArrayList<IFarmerBehavior>();
|
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;
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public TileEntityFarmer() {
|
2016-10-31 18:03:18 +01:00
|
|
|
super(12, "farmer");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
|
2016-10-31 18:03:18 +01:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2018-06-23 01:39:30 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2016-10-31 18:03:18 +01:00
|
|
|
compound.setInteger("WaitTime", this.waitTime);
|
2016-12-06 20:06:40 +01:00
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
if (type == NBTType.SAVE_TILE) {
|
2016-10-31 18:03:18 +01:00
|
|
|
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
|
2018-06-23 01:39:30 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
|
2016-10-31 18:03:18 +01:00
|
|
|
super.readSyncableNBT(compound, type);
|
2018-06-23 01:39:30 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2016-10-31 18:03:18 +01:00
|
|
|
this.waitTime = compound.getInteger("WaitTime");
|
2016-12-06 20:06:40 +01:00
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
if (type == NBTType.SAVE_TILE) {
|
2016-10-31 18:03:18 +01:00
|
|
|
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
|
2018-06-23 01:39:30 +02:00
|
|
|
public void updateEntity() {
|
2016-10-31 18:03:18 +01:00
|
|
|
super.updateEntity();
|
2018-06-23 01:39:30 +02:00
|
|
|
if (!this.world.isRemote) {
|
|
|
|
if (!this.isRedstonePowered && this.storage.getEnergyStored() > 0) {
|
|
|
|
if (this.waitTime > 0) {
|
2016-10-31 19:03:04 +01:00
|
|
|
this.waitTime--;
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
if (this.waitTime <= 0) {
|
2016-12-04 15:03:01 +01:00
|
|
|
int radiusAroundCenter = 4;
|
2016-10-31 19:03:04 +01:00
|
|
|
|
2016-12-04 15:03:01 +01:00
|
|
|
IBlockState state = this.world.getBlockState(this.pos);
|
|
|
|
int meta = state.getBlock().getMetaFromState(state);
|
2018-06-23 01:39:30 +02:00
|
|
|
BlockPos center = this.pos.offset(EnumFacing.getHorizontal(meta), radiusAroundCenter + 1);
|
2016-10-31 18:03:18 +01:00
|
|
|
|
2016-12-04 15:03:01 +01:00
|
|
|
BlockPos query = center.add(this.checkX, 0, this.checkY);
|
|
|
|
this.checkBehaviors(query);
|
|
|
|
|
|
|
|
this.checkX++;
|
2018-06-23 01:39:30 +02:00
|
|
|
if (this.checkX > radiusAroundCenter) {
|
2016-12-04 15:03:01 +01:00
|
|
|
this.checkX = -radiusAroundCenter;
|
|
|
|
this.checkY++;
|
2018-06-23 01:39:30 +02:00
|
|
|
if (this.checkY > radiusAroundCenter) {
|
2016-12-04 15:03:01 +01:00
|
|
|
this.checkY = -radiusAroundCenter;
|
2016-10-31 19:03:04 +01:00
|
|
|
}
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
} else {
|
2016-10-31 19:03:04 +01:00
|
|
|
this.waitTime = 5;
|
|
|
|
}
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
2016-10-31 19:03:04 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
if (this.lastEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()) {
|
2016-10-31 19:03:04 +01:00
|
|
|
this.lastEnergy = this.storage.getEnergyStored();
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
private void checkBehaviors(BlockPos query) {
|
|
|
|
if (SORTED_FARMER_BEHAVIORS.size() != ActuallyAdditionsAPI.FARMER_BEHAVIORS.size()) {
|
2017-02-22 13:28:53 +01:00
|
|
|
SORTED_FARMER_BEHAVIORS.clear();
|
|
|
|
SORTED_FARMER_BEHAVIORS.addAll(ActuallyAdditionsAPI.FARMER_BEHAVIORS);
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
Collections.sort(SORTED_FARMER_BEHAVIORS, new Comparator<IFarmerBehavior>() {
|
2017-02-22 13:28:53 +01:00
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public int compare(IFarmerBehavior behavior1, IFarmerBehavior behavior2) {
|
2017-02-22 13:28:53 +01:00
|
|
|
Integer prio1 = behavior1.getPriority();
|
|
|
|
Integer prio2 = behavior2.getPriority();
|
|
|
|
return prio2.compareTo(prio1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
for (IFarmerBehavior behavior : SORTED_FARMER_BEHAVIORS) {
|
2017-02-22 13:28:53 +01:00
|
|
|
FarmerResult harvestResult = behavior.tryHarvestPlant(this.world, query, this);
|
2018-06-23 01:39:30 +02:00
|
|
|
if (harvestResult == FarmerResult.STOP_PROCESSING) return;
|
|
|
|
else {
|
|
|
|
for (int i = 0; i < this.inv.getSlots(); i++) {
|
|
|
|
ItemStack stack = this.inv.getStackInSlot(i);
|
|
|
|
if (StackUtil.isValid(stack)) {
|
2017-02-22 13:28:53 +01:00
|
|
|
FarmerResult plantResult = behavior.tryPlantSeed(stack, this.world, query, this);
|
2018-06-23 01:39:30 +02:00
|
|
|
if (plantResult == FarmerResult.SUCCESS) {
|
|
|
|
this.inv.getStackInSlot(i).shrink(1);
|
2017-02-22 13:28:53 +01:00
|
|
|
return;
|
2018-06-23 01:39:30 +02:00
|
|
|
} else if (plantResult == FarmerResult.STOP_PROCESSING) { return; }
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public boolean canInsert(int i, ItemStack stack, boolean automation) {
|
|
|
|
return !automation || i < 6;
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public boolean canExtract(int slot, ItemStack stack, boolean automation) {
|
|
|
|
return !automation || slot >= 6;
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
2016-10-31 19:03:04 +01:00
|
|
|
|
2016-11-26 08:58:42 +01:00
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public IEnergyStorage getEnergyStorage(EnumFacing facing) {
|
2016-11-26 08:58:42 +01:00
|
|
|
return this.storage;
|
|
|
|
}
|
2016-12-04 15:03:01 +01:00
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public EnumFacing getOrientation() {
|
2016-12-04 15:03:01 +01:00
|
|
|
IBlockState state = this.world.getBlockState(this.pos);
|
2018-03-19 16:46:43 +01:00
|
|
|
return WorldUtil.getDirectionByPistonRotation(state);
|
2016-12-04 15:03:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public BlockPos getPosition() {
|
2017-02-20 13:19:19 +01:00
|
|
|
return this.pos;
|
|
|
|
}
|
|
|
|
|
2016-12-04 15:03:01 +01:00
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public int getX() {
|
2016-12-04 15:03:01 +01:00
|
|
|
return this.pos.getX();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public int getY() {
|
2016-12-04 15:03:01 +01:00
|
|
|
return this.pos.getY();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public int getZ() {
|
2016-12-04 15:03:01 +01:00
|
|
|
return this.pos.getZ();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public World getWorldObject() {
|
2016-12-04 15:03:01 +01:00
|
|
|
return this.world;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public void extractEnergy(int amount) {
|
2016-12-04 15:03:01 +01:00
|
|
|
this.storage.extractEnergyInternal(amount, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public int getEnergy() {
|
2016-12-04 15:03:01 +01:00
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canAddToSeeds(List<ItemStack> stacks) {
|
|
|
|
return StackUtil.canAddAll(inv, stacks, 0, 6, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canAddToOutput(List<ItemStack> stacks) {
|
|
|
|
return StackUtil.canAddAll(inv, stacks, 6, 12, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addToSeeds(List<ItemStack> stacks) {
|
|
|
|
StackUtil.addAll(inv, stacks, 0, 6, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addToOutput(List<ItemStack> stacks) {
|
|
|
|
StackUtil.addAll(inv, stacks, 6, 12, false);
|
|
|
|
}
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|