ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/misc/apiimpl/farmer/CactusFarmerBehavior.java

82 lines
3.1 KiB
Java
Raw Normal View History

package de.ellpeck.actuallyadditions.common.misc.apiimpl.farmer;
2016-12-04 15:36:14 +01:00
import de.ellpeck.actuallyadditions.api.farmer.FarmerResult;
2016-12-04 15:36:14 +01:00
import de.ellpeck.actuallyadditions.api.farmer.IFarmerBehavior;
import de.ellpeck.actuallyadditions.api.internal.IFarmer;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCactus;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
2016-12-04 15:36:14 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
2019-05-02 09:10:29 +02:00
public class CactusFarmerBehavior implements IFarmerBehavior {
2016-12-04 15:36:14 +01:00
@Override
2019-05-02 09:10:29 +02:00
public FarmerResult tryPlantSeed(ItemStack seed, World world, BlockPos pos, IFarmer farmer) {
2016-12-04 15:36:14 +01:00
int use = 250;
2019-05-02 09:10:29 +02:00
if (farmer.getEnergy() >= use) {
2016-12-04 15:36:14 +01:00
Item item = seed.getItem();
2019-05-02 09:10:29 +02:00
if (item instanceof ItemBlock) {
2016-12-04 15:36:14 +01:00
Block block = Block.getBlockFromItem(item);
2019-05-02 09:10:29 +02:00
if (block instanceof BlockCactus) {
if (block.canPlaceBlockAt(world, pos)) {
IBlockState state = block.getDefaultState();
world.setBlockState(pos, state, 2);
world.playEvent(2001, pos, Block.getStateId(state));
2016-12-04 15:36:14 +01:00
farmer.extractEnergy(use);
return FarmerResult.SUCCESS;
}
2018-07-28 02:05:30 +02:00
return FarmerResult.FAIL;
2016-12-04 15:36:14 +01:00
}
}
}
return FarmerResult.FAIL;
2016-12-04 15:36:14 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) {
2016-12-04 15:36:14 +01:00
int use = 250;
2019-05-02 09:10:29 +02:00
if (farmer.getEnergy() >= use) {
2016-12-04 15:36:14 +01:00
IBlockState state = world.getBlockState(pos);
2019-05-02 09:10:29 +02:00
if (state.getBlock() instanceof BlockCactus) {
FarmerResult result = FarmerResult.STOP_PROCESSING;
2019-05-02 09:10:29 +02:00
for (int i = 2; i >= 1; i--) {
if (farmer.getEnergy() >= use) {
2016-12-04 15:36:14 +01:00
BlockPos up = pos.up(i);
IBlockState upState = world.getBlockState(up);
2019-05-02 09:10:29 +02:00
if (upState.getBlock() instanceof BlockCactus) {
NonNullList<ItemStack> drops = NonNullList.create();
upState.getBlock().getDrops(drops, world, up, upState, 0);
2016-12-04 15:36:14 +01:00
2019-05-02 09:10:29 +02:00
if (!drops.isEmpty()) {
if (farmer.canAddToOutput(drops)) {
2016-12-04 15:36:14 +01:00
world.playEvent(2001, up, Block.getStateId(upState));
world.setBlockToAir(up);
farmer.extractEnergy(use);
farmer.addToOutput(drops);
result = FarmerResult.SUCCESS;
2016-12-04 15:36:14 +01:00
}
}
}
}
}
return result;
2016-12-04 15:36:14 +01:00
}
}
return FarmerResult.FAIL;
}
@Override
2019-05-02 09:10:29 +02:00
public int getPriority() {
2018-01-31 07:46:37 +01:00
return 4;
2020-09-09 16:49:01 +02:00
}
}