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;
|
|
|
|
|
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;
|
2019-05-02 09:10:29 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
|
2021-03-01 18:46:12 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFarmer;
|
2018-08-10 05:04:07 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
|
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;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
2021-03-01 18:46:12 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerInventory;
|
|
|
|
import net.minecraft.inventory.container.Container;
|
|
|
|
import net.minecraft.inventory.container.INamedContainerProvider;
|
2016-10-31 18:03:18 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.state.properties.BlockStateProperties;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2016-10-31 18:03:18 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2021-03-01 18:46:12 +01:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
import net.minecraft.util.text.StringTextComponent;
|
2016-12-04 15:03:01 +01:00
|
|
|
import net.minecraft.world.World;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-11-26 08:58:42 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2016-10-31 18:03:18 +01:00
|
|
|
|
2021-03-01 18:46:12 +01:00
|
|
|
import javax.annotation.Nullable;
|
2021-02-26 22:15:48 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase.NBTType;
|
|
|
|
|
2021-03-01 18:46:12 +01:00
|
|
|
public class TileEntityFarmer extends TileEntityInventoryBase implements IFarmer, INamedContainerProvider {
|
2016-10-31 19:03:04 +01:00
|
|
|
|
2018-07-07 12:07:22 +02:00
|
|
|
private static final List<IFarmerBehavior> SORTED_FARMER_BEHAVIORS = new ArrayList<>();
|
2016-11-26 20:43:50 +01:00
|
|
|
public final CustomEnergyStorage storage = new CustomEnergyStorage(100000, 1000, 0);
|
2021-02-27 16:33:00 +01:00
|
|
|
public final LazyOptional<IEnergyStorage> lazyEnergy = LazyOptional.of(() -> this.storage);
|
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() {
|
2021-02-27 16:33:00 +01:00
|
|
|
super(ActuallyTiles.FARMER_TILE.get(), 12);
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT 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) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("WaitTime", this.waitTime);
|
2016-12-06 20:06:40 +01:00
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
if (type == NBTType.SAVE_TILE) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("CheckX", this.checkX);
|
|
|
|
compound.putInt("CheckY", this.checkY);
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
2016-10-31 19:03:04 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT 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) {
|
2021-02-27 16:33:00 +01:00
|
|
|
this.waitTime = compound.getInt("WaitTime");
|
2016-12-06 20:06:40 +01:00
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
if (type == NBTType.SAVE_TILE) {
|
2021-02-27 16:33:00 +01:00
|
|
|
this.checkX = compound.getInt("CheckX");
|
|
|
|
this.checkY = compound.getInt("CheckY");
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
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();
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!this.level.isClientSide) {
|
2018-06-23 01:39:30 +02:00
|
|
|
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) {
|
2019-02-06 21:32:03 +01:00
|
|
|
int area = ConfigIntValues.FARMER_AREA.getValue();
|
2021-02-26 22:15:48 +01:00
|
|
|
if (area % 2 == 0) {
|
|
|
|
area++;
|
|
|
|
}
|
2019-02-06 21:32:03 +01:00
|
|
|
int radius = area / 2;
|
2016-10-31 19:03:04 +01:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockState state = this.level.getBlockState(this.worldPosition);
|
|
|
|
BlockPos center = this.worldPosition.relative(state.getValue(BlockStateProperties.HORIZONTAL_FACING), radius + 1);
|
2016-10-31 18:03:18 +01:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockPos query = center.offset(this.checkX, 0, this.checkY);
|
2016-12-04 15:03:01 +01:00
|
|
|
this.checkBehaviors(query);
|
|
|
|
|
|
|
|
this.checkX++;
|
2019-02-06 21:32:03 +01:00
|
|
|
if (this.checkX > radius) {
|
|
|
|
this.checkX = -radius;
|
2016-12-04 15:03:01 +01:00
|
|
|
this.checkY++;
|
2019-02-06 21:32:03 +01:00
|
|
|
if (this.checkY > radius) {
|
|
|
|
this.checkY = -radius;
|
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-07-07 12:07:22 +02:00
|
|
|
private static boolean sorted = false;
|
|
|
|
|
|
|
|
private static void sort() {
|
|
|
|
SORTED_FARMER_BEHAVIORS.clear();
|
|
|
|
SORTED_FARMER_BEHAVIORS.addAll(ActuallyAdditionsAPI.FARMER_BEHAVIORS);
|
|
|
|
Collections.sort(SORTED_FARMER_BEHAVIORS, (b1, b2) -> b2.getPrioInt().compareTo(b1.getPrioInt()));
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
private void checkBehaviors(BlockPos query) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (!sorted) {
|
|
|
|
sort();
|
|
|
|
}
|
2017-02-22 13:28:53 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
for (IFarmerBehavior behavior : SORTED_FARMER_BEHAVIORS) {
|
2021-08-22 17:09:06 +02:00
|
|
|
FarmerResult harvestResult = behavior.tryHarvestPlant(this.level, query, this);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (harvestResult == FarmerResult.STOP_PROCESSING) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-07 12:07:22 +02:00
|
|
|
for (int i = 0; i < 6; i++) { //Process seed slots only
|
|
|
|
ItemStack stack = this.inv.getStackInSlot(i);
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockState state = this.level.getBlockState(query);
|
2021-02-27 16:33:00 +01:00
|
|
|
if (StackUtil.isValid(stack) && state.getMaterial().isReplaceable()) {
|
2021-08-22 17:09:06 +02:00
|
|
|
FarmerResult plantResult = behavior.tryPlantSeed(stack, this.level, query, this);
|
2018-07-28 02:05:30 +02:00
|
|
|
if (plantResult == FarmerResult.SUCCESS) {
|
|
|
|
this.inv.getStackInSlot(i).shrink(1);
|
|
|
|
return;
|
2021-02-26 22:15:48 +01:00
|
|
|
} else if (plantResult == FarmerResult.STOP_PROCESSING) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-07 12:07:22 +02:00
|
|
|
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public IAcceptor getAcceptor() {
|
|
|
|
return (slot, stack, automation) -> !automation || slot < 6;
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public IRemover getRemover() {
|
|
|
|
return (slot, automation) -> !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
|
2021-02-27 16:33:00 +01:00
|
|
|
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
|
|
|
|
return this.lazyEnergy;
|
2016-11-26 08:58:42 +01:00
|
|
|
}
|
2016-12-04 15:03:01 +01:00
|
|
|
|
|
|
|
@Override
|
2021-02-27 13:24:45 +01:00
|
|
|
public Direction getOrientation() {
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockState state = this.level.getBlockState(this.worldPosition);
|
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() {
|
2021-08-22 17:09:06 +02:00
|
|
|
return this.worldPosition;
|
2017-02-20 13:19:19 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 15:03:01 +01:00
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public int getX() {
|
2021-08-22 17:09:06 +02:00
|
|
|
return this.worldPosition.getX();
|
2016-12-04 15:03:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public int getY() {
|
2021-08-22 17:09:06 +02:00
|
|
|
return this.worldPosition.getY();
|
2016-12-04 15:03:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public int getZ() {
|
2021-08-22 17:09:06 +02:00
|
|
|
return this.worldPosition.getZ();
|
2016-12-04 15:03:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public World getWorldObject() {
|
2021-08-22 17:09:06 +02:00
|
|
|
return this.level;
|
2016-12-04 15:03:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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) {
|
2019-02-27 19:53:05 +01:00
|
|
|
return StackUtil.canAddAll(this.inv, stacks, 0, 6, false);
|
2018-06-23 01:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canAddToOutput(List<ItemStack> stacks) {
|
2019-02-27 19:53:05 +01:00
|
|
|
return StackUtil.canAddAll(this.inv, stacks, 6, 12, false);
|
2018-06-23 01:39:30 +02:00
|
|
|
}
|
2018-07-07 12:07:22 +02:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
@Override
|
|
|
|
public void addToSeeds(List<ItemStack> stacks) {
|
2019-02-27 19:53:05 +01:00
|
|
|
StackUtil.addAll(this.inv, stacks, 0, 6, false);
|
2018-06-23 01:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addToOutput(List<ItemStack> stacks) {
|
2019-02-27 19:53:05 +01:00
|
|
|
StackUtil.addAll(this.inv, stacks, 6, 12, false);
|
2018-06-23 01:39:30 +02:00
|
|
|
}
|
2021-03-01 18:46:12 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ITextComponent getDisplayName() {
|
|
|
|
return StringTextComponent.EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity p_createMenu_3_) {
|
|
|
|
return new ContainerFarmer(windowId, playerInventory, this);
|
|
|
|
}
|
2016-10-31 18:03:18 +01:00
|
|
|
}
|