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

@ -45,10 +45,6 @@ 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);
Block block = state.getBlock();
if(block.isReplaceable(world, pos)){
BlockPos farmland = pos.down(); BlockPos farmland = pos.down();
Block farmlandBlock = world.getBlockState(farmland).getBlock(); Block farmlandBlock = world.getBlockState(farmland).getBlock();
if (farmlandBlock instanceof BlockDirt || farmlandBlock instanceof BlockGrass) { if (farmlandBlock instanceof BlockDirt || farmlandBlock instanceof BlockGrass) {
@ -63,7 +59,6 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
return true; return true;
} }
} }
}
return false; return false;
} }
@ -79,9 +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)){ if (defaultPlant(world, pos, this.getPlantablePlantFromStack(seed, world, pos), farmer, use)) { return FarmerResult.SUCCESS; }
return FarmerResult.SUCCESS;
}
} }
return FarmerResult.FAIL; return FarmerResult.FAIL;
} }
@ -94,11 +87,8 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
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"))) {
}
}
else if((BlockCrops.AGE).equals(block.getBlockState().getProperty("age"))) {
if (state.getValue(BlockCrops.AGE) >= 7 && !(block instanceof BlockStem)) return doFarmerStuff(state, world, pos, farmer); if (state.getValue(BlockCrops.AGE) >= 7 && !(block instanceof BlockStem)) return doFarmerStuff(state, world, pos, farmer);
} }
} }
@ -113,8 +103,7 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
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);
} }
} }
@ -151,9 +140,7 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
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;
@ -163,12 +150,9 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
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;
} }
@ -180,44 +164,36 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
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: case DIRT:
world.setBlockState(pos, Blocks.FARMLAND.getDefaultState()); world.setBlockState(pos, Blocks.FARMLAND.getDefaultState());
return EnumActionResult.SUCCESS; return EnumActionResult.SUCCESS;
case COARSE_DIRT: case COARSE_DIRT:
world.setBlockState(pos, Blocks.DIRT.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT)); world.setBlockState(pos, Blocks.DIRT.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT));
return EnumActionResult.SUCCESS; return EnumActionResult.SUCCESS;
default: break; default:
} }
} }
} }

View file

@ -370,8 +370,17 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
} }
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()) {
private static void sort() {
SORTED_FARMER_BEHAVIORS.clear(); SORTED_FARMER_BEHAVIORS.clear();
SORTED_FARMER_BEHAVIORS.addAll(ActuallyAdditionsAPI.FARMER_BEHAVIORS); SORTED_FARMER_BEHAVIORS.addAll(ActuallyAdditionsAPI.FARMER_BEHAVIORS);
Collections.sort(SORTED_FARMER_BEHAVIORS, (b1, b2) -> b2.getPrioInt().compareTo(b1.getPrioInt()));
}
Collections.sort(SORTED_FARMER_BEHAVIORS, new Comparator<IFarmerBehavior>() { private void checkBehaviors(BlockPos query) {
@Override
public int compare(IFarmerBehavior behavior1, IFarmerBehavior behavior2) { if (!sorted) sort();
Integer prio1 = behavior1.getPriority();
Integer prio2 = behavior2.getPriority();
return prio2.compareTo(prio1);
}
});
}
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);
if (StackUtil.isValid(stack)) { IBlockState state = world.getBlockState(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) { if (plantResult == FarmerResult.SUCCESS) this.inv.getStackInSlot(i).shrink(1);
this.inv.getStackInSlot(i).shrink(1);
return;
} else if (plantResult == FarmerResult.STOP_PROCESSING) { return; }
}
} }
} }
} }
} }