This commit is contained in:
Shadows_of_Fire 2018-07-07 06:07:22 -04:00
parent 3df0738429
commit 71d5d52462
5 changed files with 103 additions and 119 deletions

View file

@ -51,6 +51,9 @@ public final class ActuallyAdditionsAPI{
public static final List<LensConversionRecipe> RECONSTRUCTOR_LENS_CONVERSION_RECIPES = new ArrayList<>(); public static final List<LensConversionRecipe> RECONSTRUCTOR_LENS_CONVERSION_RECIPES = new ArrayList<>();
public static final List<EmpowererRecipe> EMPOWERER_RECIPES = new ArrayList<>(); public static final List<EmpowererRecipe> EMPOWERER_RECIPES = new ArrayList<>();
public static final Map<Item, IColorLensChanger> RECONSTRUCTOR_LENS_COLOR_CHANGERS = new HashMap<>(); public static final Map<Item, IColorLensChanger> RECONSTRUCTOR_LENS_COLOR_CHANGERS = new HashMap<>();
/**
* Farmer behaviors are sorted when first accessed, this will not be done until after loading, but do not add behaviors at runtime.
*/
public static final List<IFarmerBehavior> FARMER_BEHAVIORS = new ArrayList<>(); public static final List<IFarmerBehavior> FARMER_BEHAVIORS = new ArrayList<>();
public static final List<CoffeeIngredient> COFFEE_MACHINE_INGREDIENTS = new ArrayList<>(); public static final List<CoffeeIngredient> COFFEE_MACHINE_INGREDIENTS = new ArrayList<>();
public static final List<CompostRecipe> COMPOST_RECIPES = new ArrayList<>(); public static final List<CompostRecipe> COMPOST_RECIPES = new ArrayList<>();

View file

@ -19,7 +19,8 @@ public interface IFarmerBehavior{
/** /**
* Try to plant a seed with this behavior * Try to plant a seed with this behavior
* If this method return true, the seed ItemStack will have one item deducted from it. * If this method returns true, the seed ItemStack will be shrunk by one.
* This method will not be called if the block at the given position is not replaceable.
* *
* @param seed The seed stack to plant * @param seed The seed stack to plant
* @param world The world * @param world The world
@ -40,4 +41,6 @@ public interface IFarmerBehavior{
FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer); FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer);
int getPriority(); int getPriority();
default Integer getPrioInt() { return getPriority(); }
} }

View file

@ -41,65 +41,55 @@ import net.minecraft.world.WorldServer;
import net.minecraftforge.common.IPlantable; import net.minecraftforge.common.IPlantable;
import net.minecraftforge.common.util.FakePlayerFactory; import net.minecraftforge.common.util.FakePlayerFactory;
public class DefaultFarmerBehavior implements IFarmerBehavior{ public class DefaultFarmerBehavior implements IFarmerBehavior {
public static boolean defaultPlant(World world, BlockPos pos, IBlockState toPlant, IFarmer farmer, int use){ public static boolean defaultPlant(World world, BlockPos pos, IBlockState toPlant, IFarmer farmer, int use) {
if(toPlant != null){ if (toPlant != null) {
IBlockState state = world.getBlockState(pos); BlockPos farmland = pos.down();
Block block = state.getBlock(); Block farmlandBlock = world.getBlockState(farmland).getBlock();
if (farmlandBlock instanceof BlockDirt || farmlandBlock instanceof BlockGrass) {
world.setBlockToAir(pos);
useHoeAt(world, farmland);
world.playSound(null, farmland, SoundEvents.ITEM_HOE_TILL, SoundCategory.BLOCKS, 1.0F, 1.0F);
farmer.extractEnergy(use);
}
if(block.isReplaceable(world, pos)){ if (tryPlant(toPlant, world, pos)) {
BlockPos farmland = pos.down(); farmer.extractEnergy(use);
Block farmlandBlock = world.getBlockState(farmland).getBlock(); return true;
if(farmlandBlock instanceof BlockDirt || farmlandBlock instanceof BlockGrass){
world.setBlockToAir(pos);
useHoeAt(world, farmland);
world.playSound(null, farmland, SoundEvents.ITEM_HOE_TILL, SoundCategory.BLOCKS, 1.0F, 1.0F);
farmer.extractEnergy(use);
}
if(tryPlant(toPlant, world, pos)){
farmer.extractEnergy(use);
return true;
}
} }
} }
return false; return false;
} }
private static boolean tryPlant(IBlockState toPlant, World world, BlockPos pos){ private static boolean tryPlant(IBlockState toPlant, World world, BlockPos pos) {
if(toPlant.getBlock().canPlaceBlockAt(world, pos)){ if (toPlant.getBlock().canPlaceBlockAt(world, pos)) {
world.setBlockState(pos, toPlant); world.setBlockState(pos, toPlant);
return true; return true;
} }
return false; return false;
} }
@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(defaultPlant(world, pos, this.getPlantablePlantFromStack(seed, world, pos), farmer, use)){ if (defaultPlant(world, pos, this.getPlantablePlantFromStack(seed, world, pos), farmer, use)) { return FarmerResult.SUCCESS; }
return FarmerResult.SUCCESS;
}
} }
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 = 250; int use = 250;
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 instanceof BlockCrops){ if (block instanceof BlockCrops) {
if(((BlockCrops)block).isMaxAge(state)){ if (((BlockCrops) block).isMaxAge(state)) { return doFarmerStuff(state, world, pos, farmer); }
return doFarmerStuff(state, world, pos, farmer); } else if ((BlockCrops.AGE).equals(block.getBlockState().getProperty("age"))) {
} if (state.getValue(BlockCrops.AGE) >= 7 && !(block instanceof BlockStem)) return doFarmerStuff(state, world, pos, farmer);
}
else if((BlockCrops.AGE).equals(block.getBlockState().getProperty("age"))) {
if(state.getValue(BlockCrops.AGE) >= 7 && !(block instanceof BlockStem)) return doFarmerStuff(state, world, pos, farmer);
} }
} }
return FarmerResult.FAIL; return FarmerResult.FAIL;
@ -110,25 +100,24 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
List<ItemStack> other = new ArrayList<>(); List<ItemStack> other = new ArrayList<>();
NonNullList<ItemStack> drops = NonNullList.create(); NonNullList<ItemStack> drops = NonNullList.create();
state.getBlock().getDrops(drops, world, pos, state, 0); state.getBlock().getDrops(drops, world, pos, state, 0);
for(ItemStack stack : drops){ for (ItemStack stack : drops) {
if(this.getPlantableFromStack(stack) != null){ if (this.getPlantableFromStack(stack) != null) {
seeds.add(stack); seeds.add(stack);
} } else {
else{
other.add(stack); other.add(stack);
} }
} }
boolean putSeeds = true; boolean putSeeds = true;
if(!farmer.canAddToSeeds(seeds)){ if (!farmer.canAddToSeeds(seeds)) {
other.addAll(seeds); other.addAll(seeds);
putSeeds = false; putSeeds = false;
} }
if(farmer.canAddToOutput(other)){ if (farmer.canAddToOutput(other)) {
farmer.addToOutput(other); farmer.addToOutput(other);
if(putSeeds){ if (putSeeds) {
farmer.addToSeeds(seeds); farmer.addToSeeds(seeds);
} }
@ -142,82 +131,69 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
} }
@Override @Override
public int getPriority(){ public int getPriority() {
return 0; return 0;
} }
private IBlockState getPlantablePlantFromStack(ItemStack stack, World world, BlockPos pos){ private IBlockState getPlantablePlantFromStack(ItemStack stack, World world, BlockPos pos) {
if(StackUtil.isValid(stack)){ if (StackUtil.isValid(stack)) {
IPlantable plantable = this.getPlantableFromStack(stack); IPlantable plantable = this.getPlantableFromStack(stack);
if(plantable != null){ if (plantable != null) {
IBlockState state = plantable.getPlant(world, pos); IBlockState state = plantable.getPlant(world, pos);
if(state != null && state.getBlock() instanceof IGrowable){ if (state != null && state.getBlock() instanceof IGrowable) return state;
return state;
}
} }
} }
return null; return null;
} }
private IPlantable getPlantableFromStack(ItemStack stack){ private IPlantable getPlantableFromStack(ItemStack stack) {
Item item = stack.getItem(); Item item = stack.getItem();
if(item instanceof IPlantable){ if (item instanceof IPlantable) {
return (IPlantable)item; return (IPlantable) item;
} } else if (item instanceof ItemBlock) {
else if(item instanceof ItemBlock){
Block block = Block.getBlockFromItem(item); Block block = Block.getBlockFromItem(item);
if(block instanceof IPlantable){ if (block instanceof IPlantable) return (IPlantable) block;
return (IPlantable)block;
}
} }
return null; return null;
} }
private static ItemStack hoe = ItemStack.EMPTY; private static ItemStack hoe = ItemStack.EMPTY;
private static ItemStack getHoeStack(){ private static ItemStack getHoeStack() {
if(hoe.isEmpty()) hoe = new ItemStack(Items.DIAMOND_HOE); if (hoe.isEmpty()) hoe = new ItemStack(Items.DIAMOND_HOE);
return hoe; return hoe;
} }
public static EnumActionResult useHoeAt(World world, BlockPos pos) public static EnumActionResult useHoeAt(World world, BlockPos pos) {
{
EntityPlayer player = FakePlayerFactory.getMinecraft((WorldServer) world); EntityPlayer player = FakePlayerFactory.getMinecraft((WorldServer) world);
ItemStack itemstack = getHoeStack(); ItemStack itemstack = getHoeStack();
if (!player.canPlayerEdit(pos.offset(EnumFacing.UP), EnumFacing.UP, itemstack)) if (!player.canPlayerEdit(pos.offset(EnumFacing.UP), EnumFacing.UP, itemstack)) {
{
return EnumActionResult.FAIL; return EnumActionResult.FAIL;
} } else {
else
{
int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(itemstack, player, world, pos); int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(itemstack, player, world, pos);
if (hook != 0) return hook > 0 ? EnumActionResult.SUCCESS : EnumActionResult.FAIL; if (hook != 0) return hook > 0 ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;
IBlockState iblockstate = world.getBlockState(pos); IBlockState iblockstate = world.getBlockState(pos);
Block block = iblockstate.getBlock(); Block block = iblockstate.getBlock();
if (world.isAirBlock(pos.up())) if (world.isAirBlock(pos.up())) {
{ if (block == Blocks.GRASS || block == Blocks.GRASS_PATH) {
if (block == Blocks.GRASS || block == Blocks.GRASS_PATH)
{
world.setBlockState(pos, Blocks.FARMLAND.getDefaultState()); world.setBlockState(pos, Blocks.FARMLAND.getDefaultState());
return EnumActionResult.SUCCESS; return EnumActionResult.SUCCESS;
} }
if (block == Blocks.DIRT) if (block == Blocks.DIRT) {
{ switch (iblockstate.getValue(BlockDirt.VARIANT)) {
switch (iblockstate.getValue(BlockDirt.VARIANT)) case DIRT:
{ world.setBlockState(pos, Blocks.FARMLAND.getDefaultState());
case DIRT: return EnumActionResult.SUCCESS;
world.setBlockState(pos, Blocks.FARMLAND.getDefaultState()); case COARSE_DIRT:
return EnumActionResult.SUCCESS; world.setBlockState(pos, Blocks.DIRT.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT));
case COARSE_DIRT: return EnumActionResult.SUCCESS;
world.setBlockState(pos, Blocks.DIRT.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT)); default:
return EnumActionResult.SUCCESS;
default: break;
} }
} }
} }

View file

@ -369,9 +369,18 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
return this.isRedstoneToggle() && this.isPulseMode; return this.isRedstoneToggle() && this.isPulseMode;
} }
public enum NBTType{ public enum NBTType {
/**
* Use when normal writeToNBT/readToNBT is expected.
*/
SAVE_TILE, SAVE_TILE,
/**
* Use when data needs to be sent to the client.
*/
SYNC, SYNC,
/**
* Wat
*/
SAVE_BLOCK SAVE_BLOCK
} }
} }

View file

@ -12,7 +12,6 @@ package de.ellpeck.actuallyadditions.mod.tile;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI; import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
@ -31,7 +30,7 @@ import net.minecraftforge.energy.IEnergyStorage;
public class TileEntityFarmer extends TileEntityInventoryBase implements IFarmer { public class TileEntityFarmer extends TileEntityInventoryBase implements IFarmer {
private static final List<IFarmerBehavior> SORTED_FARMER_BEHAVIORS = new ArrayList<IFarmerBehavior>(); private static final List<IFarmerBehavior> SORTED_FARMER_BEHAVIORS = new ArrayList<>();
public final CustomEnergyStorage storage = new CustomEnergyStorage(100000, 1000, 0); public final CustomEnergyStorage storage = new CustomEnergyStorage(100000, 1000, 0);
private int waitTime; private int waitTime;
@ -108,36 +107,30 @@ public class TileEntityFarmer extends TileEntityInventoryBase implements IFarmer
} }
} }
private void checkBehaviors(BlockPos query) { private static boolean sorted = false;
if (SORTED_FARMER_BEHAVIORS.size() != ActuallyAdditionsAPI.FARMER_BEHAVIORS.size()) {
SORTED_FARMER_BEHAVIORS.clear();
SORTED_FARMER_BEHAVIORS.addAll(ActuallyAdditionsAPI.FARMER_BEHAVIORS);
Collections.sort(SORTED_FARMER_BEHAVIORS, new Comparator<IFarmerBehavior>() { private static void sort() {
@Override SORTED_FARMER_BEHAVIORS.clear();
public int compare(IFarmerBehavior behavior1, IFarmerBehavior behavior2) { SORTED_FARMER_BEHAVIORS.addAll(ActuallyAdditionsAPI.FARMER_BEHAVIORS);
Integer prio1 = behavior1.getPriority(); Collections.sort(SORTED_FARMER_BEHAVIORS, (b1, b2) -> b2.getPrioInt().compareTo(b1.getPrioInt()));
Integer prio2 = behavior2.getPriority(); }
return prio2.compareTo(prio1);
} private void checkBehaviors(BlockPos query) {
});
} if (!sorted) sort();
for (IFarmerBehavior behavior : SORTED_FARMER_BEHAVIORS) { for (IFarmerBehavior behavior : SORTED_FARMER_BEHAVIORS) {
FarmerResult harvestResult = behavior.tryHarvestPlant(this.world, query, this); FarmerResult harvestResult = behavior.tryHarvestPlant(this.world, query, this);
if (harvestResult == FarmerResult.STOP_PROCESSING) return; if (harvestResult == FarmerResult.STOP_PROCESSING) return;
else { for (int i = 0; i < 6; i++) { //Process seed slots only
for (int i = 0; i < this.inv.getSlots(); i++) { ItemStack stack = this.inv.getStackInSlot(i);
ItemStack stack = this.inv.getStackInSlot(i); IBlockState state = world.getBlockState(query);
if (StackUtil.isValid(stack)) { 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) { if (plantResult == FarmerResult.SUCCESS) this.inv.getStackInSlot(i).shrink(1);
this.inv.getStackInSlot(i).shrink(1);
return;
} else if (plantResult == FarmerResult.STOP_PROCESSING) { return; }
}
} }
} }
} }
} }