Fix up farmer behaviors

This commit is contained in:
Shadows_of_Fire 2018-07-27 20:05:30 -04:00
parent 7a5b1dcd1c
commit f59c431649
8 changed files with 61 additions and 42 deletions

View file

@ -10,8 +10,24 @@
package de.ellpeck.actuallyadditions.api.farmer;
public enum FarmerResult{
/**
* Return values for IFarmerBehavior, each one has a different meaning in harvest and planting contexts.
*
*/
public enum FarmerResult {
/**
* Indicates that your behavior failed to perform the action it attempted.
*/
FAIL,
/**
* Indicates that your behavior succeeded doing the action it attempted. For harvesting, this also shrinks the current stack by one, and stops processing.
*/
SUCCESS,
/**
* Indicates you want the farmer to halt all further actions this tick. This will exit the farmer behavior loop if returned.
*/
STOP_PROCESSING
}

View file

@ -15,7 +15,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public interface IFarmerBehavior{
public interface IFarmerBehavior {
/**
* Try to plant a seed with this behavior
@ -41,6 +41,8 @@ public interface IFarmerBehavior{
FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer);
int getPriority();
default Integer getPrioInt() { return getPriority(); }
default Integer getPrioInt() {
return getPriority();
}
}

View file

@ -41,7 +41,7 @@ public class CactusFarmerBehavior implements IFarmerBehavior{
farmer.extractEnergy(use);
return FarmerResult.SUCCESS;
}
return FarmerResult.STOP_PROCESSING;
return FarmerResult.FAIL;
}
}
}

View file

@ -74,7 +74,7 @@ public class DefaultFarmerBehavior implements IFarmerBehavior {
public FarmerResult tryPlantSeed(ItemStack seed, World world, BlockPos pos, IFarmer farmer) {
int use = 350;
if (farmer.getEnergy() >= use * 2) {
if (defaultPlant(world, pos, this.getPlantablePlantFromStack(seed, world, pos), farmer, use)) { return FarmerResult.SUCCESS; }
if (defaultPlant(world, pos, this.getPlantablePlantFromStack(seed, world, pos), farmer, use)) return FarmerResult.SUCCESS;
}
return FarmerResult.FAIL;
}

View file

@ -24,41 +24,39 @@ import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class MelonPumpkinFarmerBehavior implements IFarmerBehavior{
public class MelonPumpkinFarmerBehavior implements IFarmerBehavior {
@Override
public FarmerResult tryPlantSeed(ItemStack seed, World world, BlockPos pos, IFarmer farmer){
public FarmerResult tryPlantSeed(ItemStack seed, World world, BlockPos pos, IFarmer farmer) {
int use = 350;
if(farmer.getEnergy() >= use*2){
if(StackUtil.isValid(seed)){
if (farmer.getEnergy() >= use * 2) {
if (StackUtil.isValid(seed)) {
Item seedItem = seed.getItem();
boolean isPumpkin = seedItem == Items.PUMPKIN_SEEDS;
if(isPumpkin || seedItem == Items.MELON_SEEDS){
if((pos.getX()%2 == 0) == (pos.getZ()%2 == 0)){
if (isPumpkin || seedItem == Items.MELON_SEEDS) {
if ((pos.getX() % 2 == 0) == (pos.getZ() % 2 == 0)) {
IBlockState toPlant = (isPumpkin ? Blocks.PUMPKIN_STEM : Blocks.MELON_STEM).getDefaultState();
if(DefaultFarmerBehavior.defaultPlant(world, pos, toPlant, farmer, use)){
return FarmerResult.SUCCESS;
}
if (DefaultFarmerBehavior.defaultPlant(world, pos, toPlant, farmer, use)) return FarmerResult.SUCCESS;
}
return FarmerResult.STOP_PROCESSING;
}
return FarmerResult.FAIL;
}
}
return FarmerResult.FAIL;
}
@Override
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer){
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) {
int use = 500;
if(farmer.getEnergy() >= use){
if (farmer.getEnergy() >= use) {
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if(block == Blocks.PUMPKIN || block == Blocks.MELON_BLOCK){
NonNullList<ItemStack> drops = NonNullList.create();
if (block == Blocks.PUMPKIN || block == Blocks.MELON_BLOCK) {
NonNullList<ItemStack> drops = NonNullList.create();
block.getDrops(drops, world, pos, state, 0);
if(!drops.isEmpty()){
if(farmer.canAddToOutput(drops)){
if (!drops.isEmpty()) {
if (farmer.canAddToOutput(drops)) {
world.playEvent(2001, pos, Block.getStateId(state));
world.setBlockToAir(pos);
@ -75,7 +73,7 @@ public class MelonPumpkinFarmerBehavior implements IFarmerBehavior{
}
@Override
public int getPriority(){
public int getPriority() {
return 10;
}
}

View file

@ -37,7 +37,7 @@ public class NetherWartFarmerBehavior implements IFarmerBehavior{
farmer.extractEnergy(use);
return FarmerResult.SUCCESS;
}
return FarmerResult.STOP_PROCESSING;
return FarmerResult.FAIL;
}
}
return FarmerResult.FAIL;
@ -70,7 +70,7 @@ public class NetherWartFarmerBehavior implements IFarmerBehavior{
}
}
}
return FarmerResult.STOP_PROCESSING;
return FarmerResult.FAIL;
}
}
return FarmerResult.FAIL;

View file

@ -23,14 +23,14 @@ import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class ReedFarmerBehavior implements IFarmerBehavior{
public class ReedFarmerBehavior implements IFarmerBehavior {
@Override
public FarmerResult tryPlantSeed(ItemStack seed, World world, BlockPos pos, IFarmer farmer){
public FarmerResult tryPlantSeed(ItemStack seed, World world, BlockPos pos, IFarmer farmer) {
int use = 250;
if(farmer.getEnergy() >= use){
if(seed.getItem() == Items.REEDS){
if(Blocks.REEDS.canPlaceBlockAt(world, pos)){
if (farmer.getEnergy() >= use) {
if (seed.getItem() == Items.REEDS) {
if (Blocks.REEDS.canPlaceBlockAt(world, pos)) {
world.setBlockState(pos, Blocks.REEDS.getDefaultState(), 2);
farmer.extractEnergy(use);
return FarmerResult.SUCCESS;
@ -42,30 +42,30 @@ public class ReedFarmerBehavior implements IFarmerBehavior{
}
@Override
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer){
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) {
int use = 250;
if(farmer.getEnergy() >= use){
if (farmer.getEnergy() >= use) {
IBlockState state = world.getBlockState(pos);
if(state.getBlock() instanceof BlockReed){
if (state.getBlock() instanceof BlockReed) {
FarmerResult result = FarmerResult.STOP_PROCESSING;
for(int i = 2; i >= 1; --i){
if(farmer.getEnergy() >= use){
for (int i = 2; i >= 1; --i) {
if (farmer.getEnergy() >= use) {
BlockPos up = pos.up(i);
IBlockState upState = world.getBlockState(up);
if(upState.getBlock() instanceof BlockReed){
NonNullList<ItemStack> drops = NonNullList.create();
if (upState.getBlock() instanceof BlockReed) {
NonNullList<ItemStack> drops = NonNullList.create();
upState.getBlock().getDrops(drops, world, pos, state, 0);
if(!drops.isEmpty()){
if(farmer.canAddToOutput(drops)){
if (!drops.isEmpty()) {
if (farmer.canAddToOutput(drops)) {
world.playEvent(2001, up, Block.getStateId(upState));
world.setBlockToAir(up);
farmer.extractEnergy(use);
farmer.addToOutput(drops);
result = FarmerResult.STOP_PROCESSING; //Success no longer makes it not replant, and the plant logic seems sketchy right after harvesting. This works tho.
result = FarmerResult.STOP_PROCESSING;
}
}
}
@ -79,7 +79,7 @@ public class ReedFarmerBehavior implements IFarmerBehavior{
}
@Override
public int getPriority(){
public int getPriority() {
return 2;
}
}

View file

@ -127,7 +127,10 @@ public class TileEntityFarmer extends TileEntityInventoryBase implements IFarmer
IBlockState state = world.getBlockState(query);
if (StackUtil.isValid(stack) && state.getBlock().isReplaceable(world, query)) {
FarmerResult plantResult = behavior.tryPlantSeed(stack, this.world, query, this);
if (plantResult == FarmerResult.SUCCESS) this.inv.getStackInSlot(i).shrink(1);
if (plantResult == FarmerResult.SUCCESS) {
this.inv.getStackInSlot(i).shrink(1);
return;
} else if (plantResult == FarmerResult.STOP_PROCESSING) return;
}
}