mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-22 04:52:26 +00:00
Drastically reduce getBlock calls in orchard mode (#19)
* Drastically reduce getBlock calls in orchard mode * improve caching
This commit is contained in:
1 parent
bfa39a7738
commit
b58e4c57c7
12 files changed
+97
-67
No files matched your search
@@ -0,0 +1,32 @@
|
||||
/*******************************************************************************
|
||||
* Copyright 2022, glee8e
|
||||
*
|
||||
* This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details.
|
||||
******************************************************************************/
|
||||
package forestry.api.farming;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
/**
|
||||
* A basic form of {@link IFarmable}. It adds one more variant of isSapling, allowing much better runtime performance than otherwise.
|
||||
*/
|
||||
public interface IFarmableBasic extends IFarmable {
|
||||
|
||||
/**
|
||||
* @return true if the block at the given location is a "sapling" for this type, i.e. a non-harvestable immature version of the crop.
|
||||
*/
|
||||
default boolean isSaplingAt(World world, int x, int y, int z) {
|
||||
return world.blockExists(x, y, z) && isSapling(world.getBlock(x, y, z), isMetadataAware() ? world.getBlockMetadata(x, y, z) : 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return true if given block at the given location is a "sapling" for this type, i.e. a non-harvestable immature version of the crop.
|
||||
*/
|
||||
boolean isSapling(Block block, int meta);
|
||||
|
||||
default boolean isMetadataAware() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,6 @@ import forestry.api.farming.IFarmHousing;
|
||||
public class FarmLogicIC2Crops extends FarmLogicOrchard {
|
||||
public FarmLogicIC2Crops(IFarmHousing housing) {
|
||||
super(housing);
|
||||
this.farmables = Farmables.farmables.get(FarmableReference.IC2Crops.get());
|
||||
this.setFarmables(Farmables.farmables.get(FarmableReference.IC2Crops.get()));
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
import forestry.api.farming.*;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -31,11 +32,6 @@ import cpw.mods.fml.common.registry.GameRegistry;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
import forestry.api.farming.FarmDirection;
|
||||
import forestry.api.farming.Farmables;
|
||||
import forestry.api.farming.ICrop;
|
||||
import forestry.api.farming.IFarmHousing;
|
||||
import forestry.api.farming.IFarmable;
|
||||
import forestry.api.genetics.IFruitBearer;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
import forestry.core.utils.vect.VectUtil;
|
||||
@@ -48,9 +44,12 @@ public class FarmLogicOrchard extends FarmLogic {
|
||||
protected final HashMap<Vect, Integer> lastExtents = new HashMap<>();
|
||||
protected final ImmutableList<Block> traversalBlocks;
|
||||
|
||||
protected boolean precalcBlockType;
|
||||
protected boolean precalcBlockMeta;
|
||||
|
||||
public FarmLogicOrchard(IFarmHousing housing) {
|
||||
super(housing);
|
||||
this.farmables = Farmables.farmables.get(FarmableReference.Orchard.get());
|
||||
this.setFarmables(Farmables.farmables.get(FarmableReference.Orchard.get()));
|
||||
|
||||
ImmutableList.Builder<Block> traversalBlocksBuilder = ImmutableList.builder();
|
||||
if (PluginManager.Module.AGRICRAFT.isEnabled() || PluginManager.Module.INDUSTRIALCRAFT.isEnabled()) {
|
||||
@@ -217,8 +216,14 @@ public class FarmLogicOrchard extends FarmLogic {
|
||||
return true;
|
||||
}
|
||||
|
||||
Block block = precalcBlockType ? world.getBlock(position.x, position.y, position.z) : null;
|
||||
int meta = precalcBlockMeta ? world.getBlockMetadata(position.x, position.y, position.z) : -1;
|
||||
|
||||
for (IFarmable farmable : farmables) {
|
||||
if (farmable.isSaplingAt(world, position.x, position.y, position.z)) {
|
||||
if (farmable instanceof IFarmableBasic) {
|
||||
if (((IFarmableBasic) farmable).isSapling(block, meta))
|
||||
return true;
|
||||
} else if (farmable.isSaplingAt(world, position.x, position.y, position.z)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -257,4 +262,16 @@ public class FarmLogicOrchard extends FarmLogic {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void setFarmables(Collection<IFarmable> farmables) {
|
||||
this.farmables = farmables;
|
||||
for (IFarmable farmable : farmables) {
|
||||
if (farmable instanceof IFarmableBasic) {
|
||||
precalcBlockType = true;
|
||||
if (((IFarmableBasic) farmable).isMetadataAware()) {
|
||||
precalcBlockMeta = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,12 +16,12 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import forestry.api.farming.ICrop;
|
||||
import forestry.api.farming.IFarmable;
|
||||
import forestry.api.farming.IFarmableBasic;
|
||||
import forestry.core.config.Constants;
|
||||
import forestry.core.utils.ItemStackUtil;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
|
||||
public class FarmableBasicAgricraft implements IFarmable {
|
||||
public class FarmableBasicAgricraft implements IFarmableBasic {
|
||||
|
||||
private final Block block;
|
||||
private final int matureMeta;
|
||||
@@ -32,8 +32,8 @@ public class FarmableBasicAgricraft implements IFarmable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSaplingAt(World world, int x, int y, int z) {
|
||||
return world.getBlock(x, y, z) == block;
|
||||
public boolean isSapling(Block block, int meta) {
|
||||
return this.block == block;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,12 +16,12 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import forestry.api.farming.ICrop;
|
||||
import forestry.api.farming.IFarmable;
|
||||
import forestry.api.farming.IFarmableBasic;
|
||||
import forestry.core.config.Constants;
|
||||
import forestry.core.utils.ItemStackUtil;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
|
||||
public class FarmableBasicFruit implements IFarmable {
|
||||
public class FarmableBasicFruit implements IFarmableBasic {
|
||||
|
||||
private final Block block;
|
||||
private final int matureMeta;
|
||||
@@ -32,8 +32,8 @@ public class FarmableBasicFruit implements IFarmable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSaplingAt(World world, int x, int y, int z) {
|
||||
return world.getBlock(x, y, z) == block;
|
||||
public boolean isSapling(Block block, int meta) {
|
||||
return this.block == block;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,12 +16,12 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import forestry.api.farming.ICrop;
|
||||
import forestry.api.farming.IFarmable;
|
||||
import forestry.api.farming.IFarmableBasic;
|
||||
import forestry.core.config.Constants;
|
||||
import forestry.core.utils.ItemStackUtil;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
|
||||
public class FarmableBasicGrowthCraft implements IFarmable {
|
||||
public class FarmableBasicGrowthCraft implements IFarmableBasic {
|
||||
|
||||
private final Block block;
|
||||
private final int matureMeta;
|
||||
@@ -36,8 +36,8 @@ public class FarmableBasicGrowthCraft implements IFarmable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSaplingAt(World world, int x, int y, int z) {
|
||||
return world.getBlock(x, y, z) == block;
|
||||
public boolean isSapling(Block block, int meta) {
|
||||
return this.block == block;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,19 +19,19 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import forestry.api.farming.ICrop;
|
||||
import forestry.api.farming.IFarmable;
|
||||
import forestry.api.farming.IFarmableBasic;
|
||||
import forestry.core.utils.BlockUtil;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
|
||||
public class FarmableCocoa implements IFarmable {
|
||||
public class FarmableCocoa implements IFarmableBasic {
|
||||
|
||||
private static final Block COCOA_PLANT = Blocks.cocoa;
|
||||
public static final Item COCOA_SEED = Items.dye;
|
||||
public static final int COCOA_META = 3;
|
||||
|
||||
@Override
|
||||
public boolean isSaplingAt(World world, int x, int y, int z) {
|
||||
return world.getBlock(x, y, z) == COCOA_PLANT;
|
||||
public boolean isSapling(Block block, int meta) {
|
||||
return block == COCOA_PLANT;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,21 +17,14 @@ import net.minecraft.world.World;
|
||||
|
||||
import forestry.api.arboriculture.IAlleleTreeSpecies;
|
||||
import forestry.api.farming.ICrop;
|
||||
import forestry.api.farming.IFarmable;
|
||||
import forestry.api.farming.IFarmableBasic;
|
||||
import forestry.arboriculture.genetics.TreeGenome;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
import forestry.plugins.PluginArboriculture;
|
||||
|
||||
public class FarmableGE implements IFarmable {
|
||||
|
||||
public class FarmableGE implements IFarmableBasic {
|
||||
@Override
|
||||
public boolean isSaplingAt(World world, int x, int y, int z) {
|
||||
|
||||
if (world.isAirBlock(x, y, z)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Block block = world.getBlock(x, y, z);
|
||||
public boolean isSapling(Block block, int meta) {
|
||||
return PluginArboriculture.blocks.saplingGE == block;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,10 +16,10 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import forestry.api.farming.ICrop;
|
||||
import forestry.api.farming.IFarmable;
|
||||
import forestry.api.farming.IFarmableBasic;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
|
||||
public class FarmableGenericCrop implements IFarmable {
|
||||
public class FarmableGenericCrop implements IFarmableBasic {
|
||||
|
||||
private final ItemStack seed;
|
||||
private final Block block;
|
||||
@@ -34,8 +34,8 @@ public class FarmableGenericCrop implements IFarmable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSaplingAt(World world, int x, int y, int z) {
|
||||
return world.getBlock(x, y, z) == block;
|
||||
public boolean isSapling(Block block, int meta) {
|
||||
return this.block == block;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,11 +16,11 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import forestry.api.farming.ICrop;
|
||||
import forestry.api.farming.IFarmable;
|
||||
import forestry.api.farming.IFarmableBasic;
|
||||
import forestry.core.utils.ItemStackUtil;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
|
||||
public class FarmableGenericSapling implements IFarmable {
|
||||
public class FarmableGenericSapling implements IFarmableBasic {
|
||||
|
||||
protected final Block sapling;
|
||||
private final int saplingMeta;
|
||||
@@ -33,22 +33,13 @@ public class FarmableGenericSapling implements IFarmable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSaplingAt(World world, int x, int y, int z) {
|
||||
|
||||
if (world.isAirBlock(x, y, z)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (world.getBlock(x, y, z) == sapling) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (saplingMeta >= 0) {
|
||||
return world.getBlockMetadata(x, y, z) == saplingMeta;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
public boolean isSapling(Block block, int meta) {
|
||||
return block == sapling && (saplingMeta < 0 || meta == saplingMeta);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMetadataAware() {
|
||||
return saplingMeta >= 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,17 +10,18 @@
|
||||
******************************************************************************/
|
||||
package forestry.farming.logic;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import forestry.api.farming.ICrop;
|
||||
import forestry.api.farming.IFarmable;
|
||||
import forestry.api.farming.IFarmableBasic;
|
||||
import forestry.core.config.Constants;
|
||||
import forestry.core.utils.ItemStackUtil;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
|
||||
public class FarmableGourd implements IFarmable {
|
||||
public class FarmableGourd implements IFarmableBasic {
|
||||
|
||||
private final ItemStack seed;
|
||||
private final ItemStack stem;
|
||||
@@ -33,12 +34,8 @@ public class FarmableGourd implements IFarmable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSaplingAt(World world, int x, int y, int z) {
|
||||
if (world.isAirBlock(x, y, z)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ItemStackUtil.equals(world.getBlock(x, y, z), stem);
|
||||
public boolean isSapling(Block block, int meta) {
|
||||
return ItemStackUtil.equals(block, stem);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,12 +16,12 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import forestry.api.farming.ICrop;
|
||||
import forestry.api.farming.IFarmable;
|
||||
import forestry.api.farming.IFarmableBasic;
|
||||
import forestry.core.config.Constants;
|
||||
import forestry.core.utils.ItemStackUtil;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
|
||||
public class FarmableStacked implements IFarmable {
|
||||
public class FarmableStacked implements IFarmableBasic {
|
||||
|
||||
private final Block block;
|
||||
private final int matureHeight;
|
||||
@@ -34,8 +34,8 @@ public class FarmableStacked implements IFarmable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSaplingAt(World world, int x, int y, int z) {
|
||||
return world.getBlock(x, y, z) == block;
|
||||
public boolean isSapling(Block block, int meta) {
|
||||
return this.block == block;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user