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<EmpowererRecipe> EMPOWERER_RECIPES = new ArrayList<>();
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<CoffeeIngredient> COFFEE_MACHINE_INGREDIENTS = 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
* 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 world The world
@ -40,4 +41,6 @@ public interface IFarmerBehavior{
FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer);
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) {
if (toPlant != null) {
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if(block.isReplaceable(world, pos)){
BlockPos farmland = pos.down();
Block farmlandBlock = world.getBlockState(farmland).getBlock();
if (farmlandBlock instanceof BlockDirt || farmlandBlock instanceof BlockGrass) {
@ -63,7 +59,6 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
return true;
}
}
}
return false;
}
@ -79,9 +74,7 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
public FarmerResult tryPlantSeed(ItemStack seed, World world, BlockPos pos, IFarmer farmer) {
int use = 350;
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;
}
@ -94,11 +87,8 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
Block block = state.getBlock();
if (block instanceof BlockCrops) {
if(((BlockCrops)block).isMaxAge(state)){
return doFarmerStuff(state, world, pos, farmer);
}
}
else if((BlockCrops.AGE).equals(block.getBlockState().getProperty("age"))) {
if (((BlockCrops) block).isMaxAge(state)) { 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);
}
}
@ -113,8 +103,7 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
for (ItemStack stack : drops) {
if (this.getPlantableFromStack(stack) != null) {
seeds.add(stack);
}
else{
} else {
other.add(stack);
}
}
@ -151,9 +140,7 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
IPlantable plantable = this.getPlantableFromStack(stack);
if (plantable != null) {
IBlockState state = plantable.getPlant(world, pos);
if(state != null && state.getBlock() instanceof IGrowable){
return state;
}
if (state != null && state.getBlock() instanceof IGrowable) return state;
}
}
return null;
@ -163,12 +150,9 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
Item item = stack.getItem();
if (item instanceof IPlantable) {
return (IPlantable) item;
}
else if(item instanceof ItemBlock){
} else if (item instanceof ItemBlock) {
Block block = Block.getBlockFromItem(item);
if(block instanceof IPlantable){
return (IPlantable)block;
}
if (block instanceof IPlantable) return (IPlantable) block;
}
return null;
}
@ -180,44 +164,36 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
return hoe;
}
public static EnumActionResult useHoeAt(World world, BlockPos pos)
{
public static EnumActionResult useHoeAt(World world, BlockPos pos) {
EntityPlayer player = FakePlayerFactory.getMinecraft((WorldServer) world);
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;
}
else
{
} else {
int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(itemstack, player, world, pos);
if (hook != 0) return hook > 0 ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;
IBlockState iblockstate = world.getBlockState(pos);
Block block = iblockstate.getBlock();
if (world.isAirBlock(pos.up()))
{
if (block == Blocks.GRASS || block == Blocks.GRASS_PATH)
{
if (world.isAirBlock(pos.up())) {
if (block == Blocks.GRASS || block == Blocks.GRASS_PATH) {
world.setBlockState(pos, Blocks.FARMLAND.getDefaultState());
return EnumActionResult.SUCCESS;
}
if (block == Blocks.DIRT)
{
switch (iblockstate.getValue(BlockDirt.VARIANT))
{
if (block == Blocks.DIRT) {
switch (iblockstate.getValue(BlockDirt.VARIANT)) {
case DIRT:
world.setBlockState(pos, Blocks.FARMLAND.getDefaultState());
return EnumActionResult.SUCCESS;
case COARSE_DIRT:
world.setBlockState(pos, Blocks.DIRT.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT));
return EnumActionResult.SUCCESS;
default: break;
default:
}
}
}

View file

@ -370,8 +370,17 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
}
public enum NBTType {
/**
* Use when normal writeToNBT/readToNBT is expected.
*/
SAVE_TILE,
/**
* Use when data needs to be sent to the client.
*/
SYNC,
/**
* Wat
*/
SAVE_BLOCK
}
}

View file

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