mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-11-26 08:48:34 +01:00
Fix up farmer behaviors
This commit is contained in:
parent
7a5b1dcd1c
commit
f59c431649
8 changed files with 61 additions and 42 deletions
|
@ -10,8 +10,24 @@
|
||||||
|
|
||||||
package de.ellpeck.actuallyadditions.api.farmer;
|
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,
|
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,
|
SUCCESS,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates you want the farmer to halt all further actions this tick. This will exit the farmer behavior loop if returned.
|
||||||
|
*/
|
||||||
STOP_PROCESSING
|
STOP_PROCESSING
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
public interface IFarmerBehavior{
|
public interface IFarmerBehavior {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Try to plant a seed with this behavior
|
* Try to plant a seed with this behavior
|
||||||
|
@ -42,5 +42,7 @@ public interface IFarmerBehavior{
|
||||||
|
|
||||||
int getPriority();
|
int getPriority();
|
||||||
|
|
||||||
default Integer getPrioInt() { return getPriority(); }
|
default Integer getPrioInt() {
|
||||||
|
return getPriority();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ public class CactusFarmerBehavior implements IFarmerBehavior{
|
||||||
farmer.extractEnergy(use);
|
farmer.extractEnergy(use);
|
||||||
return FarmerResult.SUCCESS;
|
return FarmerResult.SUCCESS;
|
||||||
}
|
}
|
||||||
return FarmerResult.STOP_PROCESSING;
|
return FarmerResult.FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,7 +74,7 @@ public class DefaultFarmerBehavior implements IFarmerBehavior {
|
||||||
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;
|
int use = 350;
|
||||||
if (farmer.getEnergy() >= use * 2) {
|
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;
|
return FarmerResult.FAIL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,41 +24,39 @@ import net.minecraft.util.NonNullList;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
public class MelonPumpkinFarmerBehavior implements IFarmerBehavior{
|
public class MelonPumpkinFarmerBehavior implements IFarmerBehavior {
|
||||||
|
|
||||||
@Override
|
@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;
|
int use = 350;
|
||||||
if(farmer.getEnergy() >= use*2){
|
if (farmer.getEnergy() >= use * 2) {
|
||||||
if(StackUtil.isValid(seed)){
|
if (StackUtil.isValid(seed)) {
|
||||||
Item seedItem = seed.getItem();
|
Item seedItem = seed.getItem();
|
||||||
boolean isPumpkin = seedItem == Items.PUMPKIN_SEEDS;
|
boolean isPumpkin = seedItem == Items.PUMPKIN_SEEDS;
|
||||||
if(isPumpkin || seedItem == Items.MELON_SEEDS){
|
if (isPumpkin || seedItem == Items.MELON_SEEDS) {
|
||||||
if((pos.getX()%2 == 0) == (pos.getZ()%2 == 0)){
|
if ((pos.getX() % 2 == 0) == (pos.getZ() % 2 == 0)) {
|
||||||
IBlockState toPlant = (isPumpkin ? Blocks.PUMPKIN_STEM : Blocks.MELON_STEM).getDefaultState();
|
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.STOP_PROCESSING;
|
||||||
}
|
}
|
||||||
|
return FarmerResult.FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return FarmerResult.FAIL;
|
return FarmerResult.FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer){
|
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) {
|
||||||
int use = 500;
|
int use = 500;
|
||||||
if(farmer.getEnergy() >= use){
|
if (farmer.getEnergy() >= use) {
|
||||||
IBlockState state = world.getBlockState(pos);
|
IBlockState state = world.getBlockState(pos);
|
||||||
Block block = state.getBlock();
|
Block block = state.getBlock();
|
||||||
if(block == Blocks.PUMPKIN || block == Blocks.MELON_BLOCK){
|
if (block == Blocks.PUMPKIN || block == Blocks.MELON_BLOCK) {
|
||||||
NonNullList<ItemStack> drops = NonNullList.create();
|
NonNullList<ItemStack> drops = NonNullList.create();
|
||||||
block.getDrops(drops, world, pos, state, 0);
|
block.getDrops(drops, world, pos, state, 0);
|
||||||
if(!drops.isEmpty()){
|
if (!drops.isEmpty()) {
|
||||||
if(farmer.canAddToOutput(drops)){
|
if (farmer.canAddToOutput(drops)) {
|
||||||
world.playEvent(2001, pos, Block.getStateId(state));
|
world.playEvent(2001, pos, Block.getStateId(state));
|
||||||
world.setBlockToAir(pos);
|
world.setBlockToAir(pos);
|
||||||
|
|
||||||
|
@ -75,7 +73,7 @@ public class MelonPumpkinFarmerBehavior implements IFarmerBehavior{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getPriority(){
|
public int getPriority() {
|
||||||
return 10;
|
return 10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ public class NetherWartFarmerBehavior implements IFarmerBehavior{
|
||||||
farmer.extractEnergy(use);
|
farmer.extractEnergy(use);
|
||||||
return FarmerResult.SUCCESS;
|
return FarmerResult.SUCCESS;
|
||||||
}
|
}
|
||||||
return FarmerResult.STOP_PROCESSING;
|
return FarmerResult.FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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;
|
return FarmerResult.FAIL;
|
||||||
|
|
|
@ -23,14 +23,14 @@ import net.minecraft.util.NonNullList;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
public class ReedFarmerBehavior implements IFarmerBehavior{
|
public class ReedFarmerBehavior implements IFarmerBehavior {
|
||||||
|
|
||||||
@Override
|
@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;
|
int use = 250;
|
||||||
if(farmer.getEnergy() >= use){
|
if (farmer.getEnergy() >= use) {
|
||||||
if(seed.getItem() == Items.REEDS){
|
if (seed.getItem() == Items.REEDS) {
|
||||||
if(Blocks.REEDS.canPlaceBlockAt(world, pos)){
|
if (Blocks.REEDS.canPlaceBlockAt(world, pos)) {
|
||||||
world.setBlockState(pos, Blocks.REEDS.getDefaultState(), 2);
|
world.setBlockState(pos, Blocks.REEDS.getDefaultState(), 2);
|
||||||
farmer.extractEnergy(use);
|
farmer.extractEnergy(use);
|
||||||
return FarmerResult.SUCCESS;
|
return FarmerResult.SUCCESS;
|
||||||
|
@ -42,30 +42,30 @@ public class ReedFarmerBehavior implements IFarmerBehavior{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer){
|
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) {
|
||||||
int use = 250;
|
int use = 250;
|
||||||
if(farmer.getEnergy() >= use){
|
if (farmer.getEnergy() >= use) {
|
||||||
IBlockState state = world.getBlockState(pos);
|
IBlockState state = world.getBlockState(pos);
|
||||||
if(state.getBlock() instanceof BlockReed){
|
if (state.getBlock() instanceof BlockReed) {
|
||||||
FarmerResult result = FarmerResult.STOP_PROCESSING;
|
FarmerResult result = FarmerResult.STOP_PROCESSING;
|
||||||
|
|
||||||
for(int i = 2; i >= 1; --i){
|
for (int i = 2; i >= 1; --i) {
|
||||||
if(farmer.getEnergy() >= use){
|
if (farmer.getEnergy() >= use) {
|
||||||
BlockPos up = pos.up(i);
|
BlockPos up = pos.up(i);
|
||||||
IBlockState upState = world.getBlockState(up);
|
IBlockState upState = world.getBlockState(up);
|
||||||
if(upState.getBlock() instanceof BlockReed){
|
if (upState.getBlock() instanceof BlockReed) {
|
||||||
NonNullList<ItemStack> drops = NonNullList.create();
|
NonNullList<ItemStack> drops = NonNullList.create();
|
||||||
upState.getBlock().getDrops(drops, world, pos, state, 0);
|
upState.getBlock().getDrops(drops, world, pos, state, 0);
|
||||||
|
|
||||||
if(!drops.isEmpty()){
|
if (!drops.isEmpty()) {
|
||||||
if(farmer.canAddToOutput(drops)){
|
if (farmer.canAddToOutput(drops)) {
|
||||||
world.playEvent(2001, up, Block.getStateId(upState));
|
world.playEvent(2001, up, Block.getStateId(upState));
|
||||||
world.setBlockToAir(up);
|
world.setBlockToAir(up);
|
||||||
|
|
||||||
farmer.extractEnergy(use);
|
farmer.extractEnergy(use);
|
||||||
farmer.addToOutput(drops);
|
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
|
@Override
|
||||||
public int getPriority(){
|
public int getPriority() {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,7 +127,10 @@ public class TileEntityFarmer extends TileEntityInventoryBase implements IFarmer
|
||||||
IBlockState state = world.getBlockState(query);
|
IBlockState state = world.getBlockState(query);
|
||||||
if (StackUtil.isValid(stack) && state.getBlock().isReplaceable(world, query)) {
|
if (StackUtil.isValid(stack) && state.getBlock().isReplaceable(world, query)) {
|
||||||
FarmerResult plantResult = behavior.tryPlantSeed(stack, this.world, query, this);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue