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

@ -41,34 +41,29 @@ import net.minecraft.world.WorldServer;
import net.minecraftforge.common.IPlantable;
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){
if(toPlant != null){
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if(block.isReplaceable(world, pos)){
public static boolean defaultPlant(World world, BlockPos pos, IBlockState toPlant, IFarmer farmer, int use) {
if (toPlant != null) {
BlockPos farmland = pos.down();
Block farmlandBlock = world.getBlockState(farmland).getBlock();
if(farmlandBlock instanceof BlockDirt || farmlandBlock instanceof BlockGrass){
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)){
if (tryPlant(toPlant, world, pos)) {
farmer.extractEnergy(use);
return true;
}
}
}
return false;
}
private static boolean tryPlant(IBlockState toPlant, World world, BlockPos pos){
if(toPlant.getBlock().canPlaceBlockAt(world, pos)){
private static boolean tryPlant(IBlockState toPlant, World world, BlockPos pos) {
if (toPlant.getBlock().canPlaceBlockAt(world, pos)) {
world.setBlockState(pos, toPlant);
return true;
}
@ -76,30 +71,25 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
}
@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;
if(farmer.getEnergy() >= use*2){
if(defaultPlant(world, pos, this.getPlantablePlantFromStack(seed, world, pos), farmer, use)){
return FarmerResult.SUCCESS;
}
if (farmer.getEnergy() >= use * 2) {
if (defaultPlant(world, pos, this.getPlantablePlantFromStack(seed, world, pos), farmer, use)) { return FarmerResult.SUCCESS; }
}
return FarmerResult.FAIL;
}
@Override
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer){
public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) {
int use = 250;
if(farmer.getEnergy() >= use){
if (farmer.getEnergy() >= use) {
IBlockState state = world.getBlockState(pos);
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(state.getValue(BlockCrops.AGE) >= 7 && !(block instanceof BlockStem)) return doFarmerStuff(state, world, pos, farmer);
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 (state.getValue(BlockCrops.AGE) >= 7 && !(block instanceof BlockStem)) return doFarmerStuff(state, world, pos, farmer);
}
}
return FarmerResult.FAIL;
@ -110,25 +100,24 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
List<ItemStack> other = new ArrayList<>();
NonNullList<ItemStack> drops = NonNullList.create();
state.getBlock().getDrops(drops, world, pos, state, 0);
for(ItemStack stack : drops){
if(this.getPlantableFromStack(stack) != null){
for (ItemStack stack : drops) {
if (this.getPlantableFromStack(stack) != null) {
seeds.add(stack);
}
else{
} else {
other.add(stack);
}
}
boolean putSeeds = true;
if(!farmer.canAddToSeeds(seeds)){
if (!farmer.canAddToSeeds(seeds)) {
other.addAll(seeds);
putSeeds = false;
}
if(farmer.canAddToOutput(other)){
if (farmer.canAddToOutput(other)) {
farmer.addToOutput(other);
if(putSeeds){
if (putSeeds) {
farmer.addToSeeds(seeds);
}
@ -142,82 +131,69 @@ public class DefaultFarmerBehavior implements IFarmerBehavior{
}
@Override
public int getPriority(){
public int getPriority() {
return 0;
}
private IBlockState getPlantablePlantFromStack(ItemStack stack, World world, BlockPos pos){
if(StackUtil.isValid(stack)){
private IBlockState getPlantablePlantFromStack(ItemStack stack, World world, BlockPos pos) {
if (StackUtil.isValid(stack)) {
IPlantable plantable = this.getPlantableFromStack(stack);
if(plantable != null){
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;
}
private IPlantable getPlantableFromStack(ItemStack stack){
private IPlantable getPlantableFromStack(ItemStack stack) {
Item item = stack.getItem();
if(item instanceof IPlantable){
return (IPlantable)item;
}
else if(item instanceof ItemBlock){
if (item instanceof IPlantable) {
return (IPlantable) item;
} 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;
}
private static ItemStack hoe = ItemStack.EMPTY;
private static ItemStack getHoeStack(){
if(hoe.isEmpty()) hoe = new ItemStack(Items.DIAMOND_HOE);
private static ItemStack getHoeStack() {
if (hoe.isEmpty()) hoe = new ItemStack(Items.DIAMOND_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);
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

@ -369,9 +369,18 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
return this.isRedstoneToggle() && this.isPulseMode;
}
public enum NBTType{
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);
}
}
}
}