mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-22 04:52:26 +00:00
Clean up farm itemEntity collection code
This commit is contained in:
1 parent
5f18a51827
commit
f2b95e1777
13 files changed
+110
-81
No files matched your search
@@ -10,9 +10,15 @@
|
||||
******************************************************************************/
|
||||
package forestry.farming.logic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.AxisAlignedBB;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
@@ -20,13 +26,14 @@ import forestry.api.farming.FarmDirection;
|
||||
import forestry.api.farming.IFarmHousing;
|
||||
import forestry.api.farming.IFarmLogic;
|
||||
import forestry.core.config.Constants;
|
||||
import forestry.core.entities.EntitySelector;
|
||||
import forestry.core.render.SpriteSheet;
|
||||
import forestry.core.utils.EntityUtil;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
|
||||
public abstract class FarmLogic implements IFarmLogic {
|
||||
|
||||
private final EntitySelectorFarm entitySelectorFarm = new EntitySelectorFarm(this);
|
||||
protected final IFarmHousing housing;
|
||||
|
||||
protected boolean isManual;
|
||||
|
||||
protected FarmLogic(IFarmHousing housing) {
|
||||
@@ -47,6 +54,8 @@ public abstract class FarmLogic implements IFarmLogic {
|
||||
return SpriteSheet.ITEMS.getLocation();
|
||||
}
|
||||
|
||||
public abstract boolean isAcceptedWindfall(ItemStack stack);
|
||||
|
||||
protected final boolean isAirBlock(Block block) {
|
||||
return block.getMaterial() == Material.air;
|
||||
}
|
||||
@@ -64,4 +73,51 @@ public abstract class FarmLogic implements IFarmLogic {
|
||||
getWorld().setBlock(position.x, position.y, position.z, block, meta, Constants.FLAG_BLOCK_SYNCH_AND_UPDATE);
|
||||
}
|
||||
|
||||
private AxisAlignedBB getHarvestBox(IFarmHousing farmHousing, boolean toWorldHeight) {
|
||||
Vect coords = new Vect(farmHousing.getCoords());
|
||||
Vect area = new Vect(farmHousing.getArea());
|
||||
Vect offset = new Vect(farmHousing.getOffset());
|
||||
|
||||
Vect min = coords.add(offset);
|
||||
Vect max = min.add(area);
|
||||
|
||||
int maxY = max.y;
|
||||
if (toWorldHeight) {
|
||||
maxY = getWorld().getHeight();
|
||||
}
|
||||
|
||||
return AxisAlignedBB.getBoundingBox(min.x, min.y, min.z, max.x, maxY, max.z);
|
||||
}
|
||||
|
||||
protected List<ItemStack> collectEntityItems(boolean toWorldHeight) {
|
||||
AxisAlignedBB harvestBox = getHarvestBox(housing, toWorldHeight);
|
||||
|
||||
List<EntityItem> entityItems = EntityUtil.selectEntitiesWithinAABB(housing.getWorld(), entitySelectorFarm, harvestBox);
|
||||
List<ItemStack> stacks = new ArrayList<>();
|
||||
for (EntityItem entity : entityItems) {
|
||||
ItemStack contained = entity.getEntityItem();
|
||||
stacks.add(contained.copy());
|
||||
entity.setDead();
|
||||
}
|
||||
return stacks;
|
||||
}
|
||||
|
||||
private static class EntitySelectorFarm extends EntitySelector<EntityItem> {
|
||||
private final FarmLogic farmLogic;
|
||||
|
||||
public EntitySelectorFarm(FarmLogic farmLogic) {
|
||||
super(EntityItem.class);
|
||||
this.farmLogic = farmLogic;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isEntityApplicableTyped(EntityItem entity) {
|
||||
if (entity.isDead) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ItemStack contained = entity.getEntityItem();
|
||||
return farmLogic.isAcceptedGermling(contained) || farmLogic.isAcceptedWindfall(contained);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,16 +15,12 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.AxisAlignedBB;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.World;
|
||||
@@ -93,36 +89,10 @@ public class FarmLogicArboreal extends FarmLogicHomogeneous {
|
||||
return (int) (10 * hydrationModifier);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<ItemStack> collect() {
|
||||
|
||||
Collection<ItemStack> products = produce;
|
||||
produce = new ArrayList<>();
|
||||
|
||||
Vect coords = new Vect(housing.getCoords());
|
||||
Vect area = new Vect(housing.getArea());
|
||||
Vect offset = new Vect(housing.getOffset());
|
||||
|
||||
Vect min = coords.add(offset);
|
||||
Vect max = min.add(area);
|
||||
|
||||
AxisAlignedBB harvestBox = AxisAlignedBB.getBoundingBox(min.x, min.y, min.z, max.x, getWorld().getHeight(), max.z);
|
||||
List<Entity> list = getWorld().getEntitiesWithinAABB(Entity.class, harvestBox);
|
||||
|
||||
for (Entity entity : list) {
|
||||
if (entity instanceof EntityItem) {
|
||||
EntityItem item = (EntityItem) entity;
|
||||
if (!item.isDead) {
|
||||
ItemStack contained = item.getEntityItem();
|
||||
if (isAcceptedGermling(contained) || isWindfall(contained)) {
|
||||
produce.add(contained.copy());
|
||||
item.setDead();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
produce = collectEntityItems(true);
|
||||
return products;
|
||||
}
|
||||
|
||||
|
||||
@@ -76,6 +76,11 @@ public class FarmLogicCocoa extends FarmLogic {
|
||||
return cocoa.isGermling(itemstack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAcceptedWindfall(ItemStack stack) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ItemStack> collect() {
|
||||
return null;
|
||||
|
||||
@@ -10,16 +10,11 @@
|
||||
******************************************************************************/
|
||||
package forestry.farming.logic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.AxisAlignedBB;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
@@ -35,9 +30,8 @@ import forestry.core.utils.vect.Vect;
|
||||
import forestry.core.utils.vect.VectUtil;
|
||||
|
||||
public abstract class FarmLogicCrops extends FarmLogicWatered {
|
||||
|
||||
private final Iterable<IFarmable> seeds;
|
||||
private static final ItemStack farmland = new ItemStack(Blocks.farmland, 1, OreDictionary.WILDCARD_VALUE);
|
||||
private final Iterable<IFarmable> seeds;
|
||||
|
||||
protected FarmLogicCrops(IFarmHousing housing, Iterable<IFarmable> seeds) {
|
||||
super(housing, new ItemStack(Blocks.dirt), new ItemStack(Blocks.farmland));
|
||||
@@ -60,7 +54,8 @@ public abstract class FarmLogicCrops extends FarmLogicWatered {
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isWindfall(ItemStack itemstack) {
|
||||
@Override
|
||||
public boolean isAcceptedWindfall(ItemStack itemstack) {
|
||||
for (IFarmable germling : seeds) {
|
||||
if (germling.isWindfall(itemstack)) {
|
||||
return true;
|
||||
@@ -69,39 +64,10 @@ public abstract class FarmLogicCrops extends FarmLogicWatered {
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<ItemStack> collect() {
|
||||
|
||||
Collection<ItemStack> products = produce;
|
||||
produce = new ArrayList<>();
|
||||
|
||||
Vect coords = new Vect(housing.getCoords());
|
||||
Vect area = new Vect(housing.getArea());
|
||||
Vect offset = new Vect(housing.getOffset());
|
||||
|
||||
Vect min = coords.add(offset);
|
||||
Vect max = min.add(area);
|
||||
|
||||
AxisAlignedBB harvestBox = AxisAlignedBB.getBoundingBox(min.x, min.y, min.z, max.x, max.y, max.z);
|
||||
List<Entity> list = housing.getWorld().getEntitiesWithinAABB(Entity.class, harvestBox);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < list.size(); i++) {
|
||||
Entity entity = list.get(i);
|
||||
|
||||
if (entity instanceof EntityItem) {
|
||||
EntityItem item = (EntityItem) entity;
|
||||
if (!item.isDead) {
|
||||
ItemStack contained = item.getEntityItem();
|
||||
if (isAcceptedGermling(contained) || isWindfall(contained)) {
|
||||
produce.add(contained.copy());
|
||||
item.setDead();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
produce = collectEntityItems(false);
|
||||
return products;
|
||||
}
|
||||
|
||||
|
||||
@@ -69,6 +69,11 @@ public class FarmLogicGourd extends FarmLogic {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAcceptedWindfall(ItemStack stack) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ItemStack> collect() {
|
||||
return null;
|
||||
|
||||
@@ -32,7 +32,7 @@ public abstract class FarmLogicHomogeneous extends FarmLogic {
|
||||
private final ItemStack soilBlock;
|
||||
protected final List<IFarmable> germlings;
|
||||
|
||||
ArrayList<ItemStack> produce = new ArrayList<>();
|
||||
List<ItemStack> produce = new ArrayList<>();
|
||||
|
||||
protected FarmLogicHomogeneous(IFarmHousing housing, ItemStack resource, ItemStack soilBlock, Iterable<IFarmable> germlings) {
|
||||
super(housing);
|
||||
@@ -63,7 +63,8 @@ public abstract class FarmLogicHomogeneous extends FarmLogic {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean isWindfall(ItemStack itemstack) {
|
||||
@Override
|
||||
public boolean isAcceptedWindfall(ItemStack itemstack) {
|
||||
for (IFarmable germling : germlings) {
|
||||
if (germling.isWindfall(itemstack)) {
|
||||
return true;
|
||||
|
||||
@@ -88,6 +88,11 @@ public class FarmLogicOrchard extends FarmLogic {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAcceptedWindfall(ItemStack stack) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ItemStack> collect() {
|
||||
return null;
|
||||
|
||||
@@ -43,7 +43,7 @@ public class FarmLogicPeat extends FarmLogicWatered {
|
||||
}
|
||||
|
||||
Block block = BlockSoil.getBlockFromItem(itemStack.getItem());
|
||||
if (block == null || !(block instanceof BlockSoil)) {
|
||||
if (!(block instanceof BlockSoil)) {
|
||||
return false;
|
||||
}
|
||||
BlockSoil blockSoil = (BlockSoil) block;
|
||||
@@ -70,6 +70,11 @@ public class FarmLogicPeat extends FarmLogicWatered {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAcceptedWindfall(ItemStack stack) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ICrop> harvest(int x, int y, int z, FarmDirection direction, int extent) {
|
||||
World world = getWorld();
|
||||
@@ -84,7 +89,7 @@ public class FarmLogicPeat extends FarmLogicWatered {
|
||||
}
|
||||
|
||||
Block block = Block.getBlockFromItem(occupant.getItem());
|
||||
if (block == null || !(block instanceof BlockSoil)) {
|
||||
if (!(block instanceof BlockSoil)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Executable → Regular
+7
-2
@@ -30,11 +30,11 @@ import forestry.api.farming.IFarmable;
|
||||
import forestry.core.utils.ItemStackUtil;
|
||||
import forestry.core.utils.vect.Vect;
|
||||
|
||||
public class FarmLogicPoale extends FarmLogic {
|
||||
public class FarmLogicReeds extends FarmLogic {
|
||||
|
||||
private final IFarmable[] germlings;
|
||||
|
||||
public FarmLogicPoale(IFarmHousing housing) {
|
||||
public FarmLogicReeds(IFarmHousing housing) {
|
||||
super(housing);
|
||||
Collection<IFarmable> farmables = Farmables.farmables.get("farmPoales");
|
||||
germlings = farmables.toArray(new IFarmable[farmables.size()]);
|
||||
@@ -83,6 +83,11 @@ public class FarmLogicPoale extends FarmLogic {
|
||||
return itemstack.getItem() == Items.reeds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAcceptedWindfall(ItemStack stack) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ItemStack> collect() {
|
||||
return null;
|
||||
@@ -82,6 +82,11 @@ public class FarmLogicRubber extends FarmLogic {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAcceptedWindfall(ItemStack stack) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ItemStack> collect() {
|
||||
return null;
|
||||
|
||||
@@ -83,6 +83,11 @@ public class FarmLogicSucculent extends FarmLogic {
|
||||
return ItemStackUtil.equals(Blocks.cactus, itemstack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAcceptedWindfall(ItemStack stack) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ItemStack> collect() {
|
||||
return null;
|
||||
|
||||
@@ -12,6 +12,7 @@ package forestry.farming.logic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.init.Blocks;
|
||||
@@ -36,7 +37,7 @@ public abstract class FarmLogicWatered extends FarmLogic {
|
||||
|
||||
private static final FluidStack STACK_WATER = Fluids.WATER.getFluid(1000);
|
||||
|
||||
ArrayList<ItemStack> produce = new ArrayList<>();
|
||||
List<ItemStack> produce = new ArrayList<>();
|
||||
|
||||
protected FarmLogicWatered(IFarmHousing housing, ItemStack resource, ItemStack ground) {
|
||||
super(housing);
|
||||
|
||||
@@ -57,7 +57,7 @@ import forestry.farming.logic.FarmLogicGourd;
|
||||
import forestry.farming.logic.FarmLogicInfernal;
|
||||
import forestry.farming.logic.FarmLogicOrchard;
|
||||
import forestry.farming.logic.FarmLogicPeat;
|
||||
import forestry.farming.logic.FarmLogicPoale;
|
||||
import forestry.farming.logic.FarmLogicReeds;
|
||||
import forestry.farming.logic.FarmLogicShroom;
|
||||
import forestry.farming.logic.FarmLogicSucculent;
|
||||
import forestry.farming.logic.FarmLogicVegetable;
|
||||
@@ -67,8 +67,8 @@ import forestry.farming.logic.FarmableGenericCrop;
|
||||
import forestry.farming.logic.FarmableGenericSapling;
|
||||
import forestry.farming.logic.FarmableGourd;
|
||||
import forestry.farming.logic.FarmableStacked;
|
||||
import forestry.farming.logic.FarmableVanillaSapling;
|
||||
import forestry.farming.logic.FarmableVanillaMushroom;
|
||||
import forestry.farming.logic.FarmableVanillaSapling;
|
||||
import forestry.farming.proxy.ProxyFarming;
|
||||
import forestry.farming.render.EnumFarmBlockTexture;
|
||||
import forestry.farming.tiles.TileControl;
|
||||
@@ -174,7 +174,7 @@ public class PluginFarming extends ForestryPlugin {
|
||||
Circuit.farmCerealManual = new CircuitFarmLogic("manualCereal", FarmLogicCereal.class).setManual();
|
||||
Circuit.farmVegetableManual = new CircuitFarmLogic("manualVegetable", FarmLogicVegetable.class).setManual();
|
||||
Circuit.farmSucculentManual = new CircuitFarmLogic("manualSucculent", FarmLogicSucculent.class).setManual();
|
||||
Circuit.farmPoalesManual = new CircuitFarmLogic("manualPoales", FarmLogicPoale.class).setManual();
|
||||
Circuit.farmPoalesManual = new CircuitFarmLogic("manualPoales", FarmLogicReeds.class).setManual();
|
||||
Circuit.farmGourdManual = new CircuitFarmLogic("manualGourd", FarmLogicGourd.class).setManual();
|
||||
Circuit.farmCocoaManual = new CircuitFarmLogic("manualCocoa", FarmLogicCocoa.class).setManual();
|
||||
|
||||
|
||||
Reference in new issue
Block a user