Farmer goes brrrr

This commit is contained in:
Flanks255 2022-01-09 11:01:49 -06:00
parent 1a3f223a4b
commit 2707b69d7a
7 changed files with 65 additions and 38 deletions

View file

@ -16,6 +16,7 @@ import de.ellpeck.actuallyadditions.mod.misc.apiimpl.farmer.*;
import net.minecraft.item.ItemStack; 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;
import net.minecraft.world.server.ServerWorld;
public interface IFarmerBehavior { public interface IFarmerBehavior {
@ -40,7 +41,7 @@ public interface IFarmerBehavior {
* @param farmer The Farmer doing this action. Can be used to query and extract energy and add items to the slots * @param farmer The Farmer doing this action. Can be used to query and extract energy and add items to the slots
* @return If harvesting was successful * @return If harvesting was successful
*/ */
FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer); FarmerResult tryHarvestPlant(ServerWorld world, BlockPos pos, IFarmer farmer);
int getPriority(); int getPriority();

View file

@ -19,9 +19,15 @@ import net.minecraft.block.Blocks;
import net.minecraft.item.BlockItem; import net.minecraft.item.BlockItem;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootParameters;
import net.minecraft.util.NonNullList; import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import java.util.List;
public class CactusFarmerBehavior implements IFarmerBehavior { public class CactusFarmerBehavior implements IFarmerBehavior {
@ -49,7 +55,7 @@ public class CactusFarmerBehavior implements IFarmerBehavior {
} }
@Override @Override
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) { public FarmerResult tryHarvestPlant(ServerWorld world, BlockPos pos, IFarmer farmer) {
int use = 250; int use = 250;
if (farmer.getEnergy() >= use) { if (farmer.getEnergy() >= use) {
BlockState state = world.getBlockState(pos); BlockState state = world.getBlockState(pos);
@ -61,8 +67,9 @@ public class CactusFarmerBehavior implements IFarmerBehavior {
BlockPos up = pos.above(i); BlockPos up = pos.above(i);
BlockState upState = world.getBlockState(up); BlockState upState = world.getBlockState(up);
if (upState.getBlock() == Blocks.CACTUS) { if (upState.getBlock() == Blocks.CACTUS) {
NonNullList<ItemStack> drops = NonNullList.create(); List<ItemStack> drops = state.getDrops(new LootContext.Builder(world)
//upState.getBlock().getDrops(drops, world, up, upState, 0); .withParameter(LootParameters.ORIGIN, new Vector3d(pos.getX(), pos.getY(), pos.getZ()))
.withParameter(LootParameters.TOOL, ItemStack.EMPTY));
if (!drops.isEmpty()) { if (!drops.isEmpty()) {
if (farmer.canAddToOutput(drops)) { if (farmer.canAddToOutput(drops)) {

View file

@ -16,14 +16,14 @@ import de.ellpeck.actuallyadditions.api.internal.IFarmer;
import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import net.minecraft.block.*; import net.minecraft.block.*;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem; import net.minecraft.item.*;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.loot.LootContext; import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootParameters;
import net.minecraft.state.properties.BlockStateProperties; import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.*; import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld; import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.IPlantable; import net.minecraftforge.common.IPlantable;
@ -75,7 +75,7 @@ public class DefaultFarmerBehavior implements IFarmerBehavior {
} }
@Override @Override
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) { public FarmerResult tryHarvestPlant(ServerWorld world, BlockPos pos, IFarmer farmer) {
int use = 250; int use = 250;
if (farmer.getEnergy() >= use) { if (farmer.getEnergy() >= use) {
BlockState state = world.getBlockState(pos); BlockState state = world.getBlockState(pos);
@ -86,8 +86,8 @@ public class DefaultFarmerBehavior implements IFarmerBehavior {
return this.doFarmerStuff(state, world, pos, farmer); return this.doFarmerStuff(state, world, pos, farmer);
} }
} }
else if (state.hasProperty(BlockStateProperties.AGE_25)) { else if (state.hasProperty(BlockStateProperties.AGE_7)) {
if (state.getValue(BlockStateProperties.AGE_25) >= 7 && !(block instanceof StemBlock)) { if (state.getValue(BlockStateProperties.AGE_7) >= 7 && !(block instanceof StemBlock)) {
return this.doFarmerStuff(state, world, pos, farmer); return this.doFarmerStuff(state, world, pos, farmer);
} }
} }
@ -95,11 +95,14 @@ public class DefaultFarmerBehavior implements IFarmerBehavior {
return FarmerResult.FAIL; return FarmerResult.FAIL;
} }
private FarmerResult doFarmerStuff(BlockState state, World world, BlockPos pos, IFarmer farmer) { private FarmerResult doFarmerStuff(BlockState state, ServerWorld world, BlockPos pos, IFarmer farmer) {
List<ItemStack> seeds = new ArrayList<>(); List<ItemStack> seeds = new ArrayList<>();
List<ItemStack> other = new ArrayList<>(); List<ItemStack> other = new ArrayList<>();
NonNullList<ItemStack> drops = NonNullList.create(); List<ItemStack> drops = state.getDrops(new LootContext.Builder(world)
//state.getBlock().getDrops(drops, world, pos, state, 0); .withParameter(LootParameters.ORIGIN, new Vector3d(pos.getX(), pos.getY(), pos.getZ()))
.withParameter(LootParameters.TOOL, ItemStack.EMPTY));
if (drops.isEmpty())
return FarmerResult.FAIL;
for (ItemStack stack : drops) { for (ItemStack stack : drops) {
if (this.getPlantableFromStack(stack) != null) { if (this.getPlantableFromStack(stack) != null) {
seeds.add(stack); seeds.add(stack);
@ -172,39 +175,33 @@ public class DefaultFarmerBehavior implements IFarmerBehavior {
public static ActionResultType useHoeAt(World world, BlockPos pos) { public static ActionResultType useHoeAt(World world, BlockPos pos) {
PlayerEntity player = FakePlayerFactory.getMinecraft((ServerWorld) world); PlayerEntity player = FakePlayerFactory.getMinecraft((ServerWorld) world); //TODO we need our own fakeplayer for the mod.
ItemStack itemstack = getHoeStack(); ItemStack itemstack = getHoeStack();
if (!player.mayUseItemAt(pos.relative(Direction.UP), Direction.UP, itemstack)) { if (!player.mayUseItemAt(pos.relative(Direction.UP), Direction.UP, itemstack)) {
return ActionResultType.FAIL; return ActionResultType.FAIL;
} else { } else {
/* int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(itemstack, player, world, pos); ItemUseContext dummyContext = new ItemUseContext(world, player, Hand.MAIN_HAND, itemstack, new BlockRayTraceResult(new Vector3d(0.5, 0.5, 0.5), Direction.UP, pos, false));
int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(dummyContext);
if (hook != 0) { if (hook != 0) {
return hook > 0 return hook > 0
? ActionResultType.SUCCESS ? ActionResultType.SUCCESS
: ActionResultType.FAIL; : ActionResultType.FAIL;
} }
if (world.isEmptyBlock(pos.above())) { if (world.isEmptyBlock(pos.above())) {
Block block = world.getBlockState(pos).getBlock();
if (block == Blocks.GRASS || block == Blocks.GRASS_PATH) { if (block == Blocks.GRASS || block == Blocks.GRASS_PATH) {
world.setBlockAndUpdate(pos, Blocks.FARMLAND.defaultBlockState()); world.setBlockAndUpdate(pos, Blocks.FARMLAND.defaultBlockState());
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
} }
if (block == Blocks.DIRT) { if (Tags.Blocks.DIRT.contains(block)) {
switch (iblockstate.getValue(BlockDirt.VARIANT)) { world.setBlockAndUpdate(pos, Blocks.FARMLAND.defaultBlockState());
case DIRT: return ActionResultType.SUCCESS;
world.setBlockAndUpdate(pos, Blocks.FARMLAND.defaultBlockState());
return ActionResultType.SUCCESS;
case COARSE_DIRT:
world.setBlockAndUpdate(pos, Blocks.DIRT.defaultBlockState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT));
return ActionResultType.SUCCESS;
default:
}
} }
}*/ }
return ActionResultType.PASS; return ActionResultType.PASS;
} }
} }

View file

@ -20,9 +20,15 @@ import net.minecraft.block.Blocks;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootParameters;
import net.minecraft.util.NonNullList; import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import java.util.List;
public class MelonPumpkinFarmerBehavior implements IFarmerBehavior { public class MelonPumpkinFarmerBehavior implements IFarmerBehavior {
@ -51,14 +57,15 @@ public class MelonPumpkinFarmerBehavior implements IFarmerBehavior {
} }
@Override @Override
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) { public FarmerResult tryHarvestPlant(ServerWorld world, BlockPos pos, IFarmer farmer) {
int use = 500; int use = 500;
if (farmer.getEnergy() >= use) { if (farmer.getEnergy() >= use) {
BlockState state = world.getBlockState(pos); BlockState state = world.getBlockState(pos);
Block block = state.getBlock(); Block block = state.getBlock();
if (block == Blocks.PUMPKIN || block == Blocks.MELON) { if (block == Blocks.PUMPKIN || block == Blocks.MELON) {
NonNullList<ItemStack> drops = NonNullList.create(); List<ItemStack> drops = state.getDrops(new LootContext.Builder(world)
//block.getDrops(drops, world, pos, state, 0); .withParameter(LootParameters.ORIGIN, new Vector3d(pos.getX(), pos.getY(), pos.getZ()))
.withParameter(LootParameters.TOOL, ItemStack.EMPTY));
if (!drops.isEmpty()) { if (!drops.isEmpty()) {
if (farmer.canAddToOutput(drops)) { if (farmer.canAddToOutput(drops)) {
world.levelEvent(2001, pos, Block.getId(state)); world.levelEvent(2001, pos, Block.getId(state));

View file

@ -19,13 +19,19 @@ import net.minecraft.block.Blocks;
import net.minecraft.block.NetherWartBlock; import net.minecraft.block.NetherWartBlock;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootParameters;
import net.minecraft.state.properties.BlockStateProperties; import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.NonNullList; import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.IPlantable; import net.minecraftforge.common.IPlantable;
import java.util.List;
public class NetherWartFarmerBehavior implements IFarmerBehavior { public class NetherWartFarmerBehavior implements IFarmerBehavior {
@Override @Override
@ -45,14 +51,15 @@ public class NetherWartFarmerBehavior implements IFarmerBehavior {
} }
@Override @Override
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) { public FarmerResult tryHarvestPlant(ServerWorld world, BlockPos pos, IFarmer farmer) {
int use = 500; int use = 500;
if (farmer.getEnergy() >= use) { if (farmer.getEnergy() >= use) {
BlockState state = world.getBlockState(pos); BlockState state = world.getBlockState(pos);
if (state.getBlock() instanceof NetherWartBlock) { if (state.getBlock() instanceof NetherWartBlock) {
if (state.getValue(BlockStateProperties.AGE_3) >= 3) { if (state.getValue(BlockStateProperties.AGE_3) >= 3) {
NonNullList<ItemStack> drops = NonNullList.create(); List<ItemStack> drops = state.getDrops(new LootContext.Builder(world)
//state.getBlock().getDrops(drops, world, pos, state, 0); .withParameter(LootParameters.ORIGIN, new Vector3d(pos.getX(), pos.getY(), pos.getZ()))
.withParameter(LootParameters.TOOL, ItemStack.EMPTY));
if (!drops.isEmpty()) { if (!drops.isEmpty()) {
boolean toInput = farmer.canAddToSeeds(drops); boolean toInput = farmer.canAddToSeeds(drops);
if (toInput || farmer.canAddToOutput(drops)) { if (toInput || farmer.canAddToOutput(drops)) {

View file

@ -19,9 +19,15 @@ import net.minecraft.block.Blocks;
import net.minecraft.block.SugarCaneBlock; import net.minecraft.block.SugarCaneBlock;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootParameters;
import net.minecraft.util.NonNullList; import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import java.util.List;
public class ReedFarmerBehavior implements IFarmerBehavior { public class ReedFarmerBehavior implements IFarmerBehavior {
@ -42,7 +48,7 @@ public class ReedFarmerBehavior implements IFarmerBehavior {
} }
@Override @Override
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) { public FarmerResult tryHarvestPlant(ServerWorld world, BlockPos pos, IFarmer farmer) {
int use = 250; int use = 250;
if (farmer.getEnergy() >= use) { if (farmer.getEnergy() >= use) {
BlockState state = world.getBlockState(pos); BlockState state = world.getBlockState(pos);
@ -54,8 +60,9 @@ public class ReedFarmerBehavior implements IFarmerBehavior {
BlockPos up = pos.above(i); BlockPos up = pos.above(i);
BlockState upState = world.getBlockState(up); BlockState upState = world.getBlockState(up);
if (upState.getBlock() instanceof SugarCaneBlock) { if (upState.getBlock() instanceof SugarCaneBlock) {
NonNullList<ItemStack> drops = NonNullList.create(); List<ItemStack> drops = state.getDrops(new LootContext.Builder(world)
//upState.getBlock().getDrops(drops, world, pos, state, 0); .withParameter(LootParameters.ORIGIN, new Vector3d(pos.getX(), pos.getY(), pos.getZ()))
.withParameter(LootParameters.TOOL, ItemStack.EMPTY));
if (!drops.isEmpty()) { if (!drops.isEmpty()) {
if (farmer.canAddToOutput(drops)) { if (farmer.canAddToOutput(drops)) {

View file

@ -36,6 +36,7 @@ import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent; import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.IEnergyStorage; import net.minecraftforge.energy.IEnergyStorage;
@ -141,7 +142,7 @@ public class TileEntityFarmer extends TileEntityInventoryBase implements IFarmer
} }
for (IFarmerBehavior behavior : SORTED_FARMER_BEHAVIORS) { for (IFarmerBehavior behavior : SORTED_FARMER_BEHAVIORS) {
FarmerResult harvestResult = behavior.tryHarvestPlant(this.level, query, this); FarmerResult harvestResult = behavior.tryHarvestPlant((ServerWorld) level, query, this);
if (harvestResult == FarmerResult.STOP_PROCESSING) { if (harvestResult == FarmerResult.STOP_PROCESSING) {
return; return;
} }