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

254 lines
8.5 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
*
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;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.farmer.FarmerResult;
import de.ellpeck.actuallyadditions.api.farmer.IFarmerBehavior;
import de.ellpeck.actuallyadditions.api.internal.IFarmer;
2021-11-13 18:02:42 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
2022-01-08 19:54:43 +01:00
import de.ellpeck.actuallyadditions.mod.config.CommonConfig;
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;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-10-31 18:03:18 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.energy.IEnergyStorage;
2016-10-31 18:03:18 +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;
2024-03-02 21:23:08 +01:00
public class TileEntityFarmer extends TileEntityInventoryBase implements IFarmer, MenuProvider {
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);
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;
2024-03-02 21:23:08 +01:00
public TileEntityFarmer(BlockPos pos, BlockState state) {
super(ActuallyBlocks.FARMER.getTileEntityType(), pos, state, 12);
2016-10-31 18:03:18 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
compound.putInt("WaitTime", this.waitTime);
}
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);
2022-02-16 02:41:49 +01:00
super.writeSyncableNBT(compound, type);
2016-10-31 18:03:18 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
this.waitTime = compound.getInt("WaitTime");
}
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);
2022-02-16 02:41:49 +01:00
super.readSyncableNBT(compound, type);
2016-10-31 18:03:18 +01:00
}
2024-03-02 21:23:08 +01:00
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityFarmer tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityFarmer tile) {
tile.serverTick();
if (!tile.isRedstonePowered && tile.storage.getEnergyStored() > 0) {
if (tile.waitTime > 0) {
tile.waitTime--;
2016-10-31 19:03:04 +01:00
2024-03-02 21:23:08 +01:00
if (tile.waitTime <= 0) {
int area = CommonConfig.Machines.FARMER_AREA.get();
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
2024-03-02 21:23:08 +01:00
BlockPos center = pos.relative(state.getValue(BlockStateProperties.HORIZONTAL_FACING), radius + 1);
2016-10-31 18:03:18 +01:00
2024-03-02 21:23:08 +01:00
BlockPos query = center.offset(tile.checkX, 0, tile.checkY);
tile.checkBehaviors(query);
2024-03-02 21:23:08 +01:00
tile.checkX++;
if (tile.checkX > radius) {
tile.checkX = -radius;
tile.checkY++;
if (tile.checkY > radius) {
tile.checkY = -radius;
2016-10-31 19:03:04 +01:00
}
2016-10-31 18:03:18 +01:00
}
}
} else {
2024-03-02 21:23:08 +01:00
tile.waitTime = 5;
2016-10-31 19:03:04 +01:00
}
2016-10-31 18:03:18 +01:00
}
2016-10-31 19:03:04 +01:00
2024-03-02 21:23:08 +01:00
if (tile.lastEnergy != tile.storage.getEnergyStored() && tile.sendUpdateWithInterval()) {
tile.lastEnergy = tile.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()));
2022-01-08 19:54:43 +01:00
sorted = true;
2018-07-07 12:07:22 +02:00
}
private void checkBehaviors(BlockPos query) {
2021-02-26 22:15:48 +01:00
if (!sorted) {
sort();
}
for (IFarmerBehavior behavior : SORTED_FARMER_BEHAVIORS) {
2024-03-02 21:23:08 +01:00
FarmerResult harvestResult = behavior.tryHarvestPlant((ServerLevel) 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);
BlockState state = this.level.getBlockState(query);
2024-03-03 01:20:53 +01:00
if (StackUtil.isValid(stack) && state.canBeReplaced()) {
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
2024-03-04 20:21:48 +01:00
public IEnergyStorage getEnergyStorage(Direction facing) {
return this.storage;
2016-11-26 08:58:42 +01:00
}
@Override
public Direction getOrientation() {
BlockState state = this.level.getBlockState(this.worldPosition);
2018-03-19 16:46:43 +01:00
return WorldUtil.getDirectionByPistonRotation(state);
}
@Override
public BlockPos getPosition() {
return this.worldPosition;
}
@Override
public int getX() {
return this.worldPosition.getX();
}
@Override
public int getY() {
return this.worldPosition.getY();
}
@Override
public int getZ() {
return this.worldPosition.getZ();
}
@Override
2024-03-02 21:23:08 +01:00
public Level getWorldObject() {
return this.level;
}
@Override
public void extractEnergy(int amount) {
this.storage.extractEnergyInternal(amount, false);
}
@Override
public int getEnergy() {
return this.storage.getEnergyStored();
}
@Override
public boolean canAddToSeeds(List<ItemStack> stacks) {
2019-02-27 19:53:05 +01:00
return StackUtil.canAddAll(this.inv, stacks, 0, 6, false);
}
@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-07-07 12:07:22 +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);
}
@Override
public void addToOutput(List<ItemStack> stacks) {
2019-02-27 19:53:05 +01:00
StackUtil.addAll(this.inv, stacks, 6, 12, false);
}
@Override
2024-03-02 21:23:08 +01:00
public Component getDisplayName() {
2024-03-03 01:20:53 +01:00
return Component.translatable("container.actuallyadditions.farmer");
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player p_createMenu_3_) {
return new ContainerFarmer(windowId, playerInventory, this);
}
2016-10-31 18:03:18 +01:00
}