mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-26 06:53:24 +00:00
Added tree farming support.
This commit is contained in:
1 parent
fd542615f6
commit
89e183619f
5 files changed
+175
-20
No files matched your search
@@ -29,6 +29,8 @@ public class FarmersComune implements IFarmerJoe {
|
||||
joinComune(new NetherWartFarmer());
|
||||
joinComune(new StemFarmer(Blocks.reeds, new ItemStack(Items.reeds)));
|
||||
joinComune(new StemFarmer(Blocks.cactus, new ItemStack(Blocks.cactus)));
|
||||
joinComune(new TreeFarmer(Blocks.sapling, Blocks.log));
|
||||
joinComune(new TreeFarmer(Blocks.sapling, Blocks.log2));
|
||||
}
|
||||
|
||||
private List<IFarmerJoe> farmers = new ArrayList<IFarmerJoe>();
|
||||
|
||||
@@ -83,7 +83,7 @@ public class SeedFarmer implements IFarmerJoe {
|
||||
|
||||
protected boolean plantFromInvenetory(TileFarmStation farm, BlockCoord bc) {
|
||||
World worldObj = farm.getWorldObj();
|
||||
if(canPlant(worldObj, bc) && farm.getSeedFromSupplies(getSeeds(), bc)) {
|
||||
if(canPlant(worldObj, bc) && farm.getSeedFromSupplies(getSeeds(), bc) != null) {
|
||||
return plant(worldObj, bc);
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -65,7 +65,7 @@ public class StemFarmer extends SeedFarmer {
|
||||
@Override
|
||||
protected boolean plantFromInvenetory(TileFarmStation farm, BlockCoord bc) {
|
||||
World worldObj = farm.getWorldObj();
|
||||
if(canPlant(worldObj, bc) && farm.getSeedFromSupplies(seeds, bc)) {
|
||||
if(canPlant(worldObj, bc) && farm.getSeedFromSupplies(seeds, bc) != null) {
|
||||
return plant(worldObj, bc);
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -10,6 +10,7 @@ import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.IProjectile;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemAxe;
|
||||
import net.minecraft.item.ItemHoe;
|
||||
import net.minecraft.item.ItemShears;
|
||||
@@ -34,7 +35,7 @@ import crazypants.util.Util;
|
||||
|
||||
public class TileFarmStation extends AbstractPoweredTaskEntity implements IEntitySelector {
|
||||
|
||||
private static final float ENERGY_PER_TICK = 4;
|
||||
private static final float ENERGY_PER_TICK = 1;
|
||||
|
||||
private crazypants.util.BlockCoord lastScanned;
|
||||
private FakePlayer farmerJoe;
|
||||
@@ -57,11 +58,31 @@ public class TileFarmStation extends AbstractPoweredTaskEntity implements IEntit
|
||||
return hasTool(ItemHoe.class);
|
||||
}
|
||||
|
||||
private boolean hasTool(Class<ItemHoe> class1) {
|
||||
public boolean hasAxe() {
|
||||
return hasTool(ItemAxe.class);
|
||||
}
|
||||
|
||||
public int geAxeLootingValue() {
|
||||
ItemStack tool = getTool(ItemAxe.class);
|
||||
if(tool == null) {
|
||||
return 0;
|
||||
}
|
||||
return EnchantmentHelper.getEnchantmentLevel(Enchantment.looting.effectId, tool);
|
||||
}
|
||||
|
||||
public void damageAxe() {
|
||||
damageTool(ItemAxe.class, 1);
|
||||
}
|
||||
|
||||
public void damageHoe(int i) {
|
||||
damageTool(ItemHoe.class, i);
|
||||
}
|
||||
|
||||
private boolean hasTool(Class<? extends Item> class1) {
|
||||
return getTool(class1) != null;
|
||||
}
|
||||
|
||||
private ItemStack getTool(Class<ItemHoe> class1) {
|
||||
private ItemStack getTool(Class<? extends Item> class1) {
|
||||
for (int i = minToolSlot; i <= maxToolSlot; i++) {
|
||||
if(Util.isType(inventory[i], class1)) {
|
||||
return inventory[i];
|
||||
@@ -70,13 +91,15 @@ public class TileFarmStation extends AbstractPoweredTaskEntity implements IEntit
|
||||
return null;
|
||||
}
|
||||
|
||||
public void damageHoe(int i) {
|
||||
ItemStack tool = getTool(ItemHoe.class);
|
||||
private void damageTool(Class<? extends Item> class1, int damage) {
|
||||
ItemStack tool = getTool(class1);
|
||||
if(tool != null) {
|
||||
tool.damageItem(1, farmerJoe);
|
||||
tool.damageItem(damage, farmerJoe);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public EntityPlayer getFakePlayer() {
|
||||
return farmerJoe;
|
||||
}
|
||||
@@ -111,8 +134,12 @@ public class TileFarmStation extends AbstractPoweredTaskEntity implements IEntit
|
||||
}
|
||||
}
|
||||
|
||||
public Block getBlock(BlockCoord up) {
|
||||
return worldObj.getBlock(up.x, up.y, up.z);
|
||||
public Block getBlock(BlockCoord bc) {
|
||||
return worldObj.getBlock(bc.x, bc.y, bc.z);
|
||||
}
|
||||
|
||||
public int getBlockMeta(BlockCoord bc) {
|
||||
return worldObj.getBlockMetadata(bc.x, bc.y, bc.z);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -173,21 +200,30 @@ public class TileFarmStation extends AbstractPoweredTaskEntity implements IEntit
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean getSeedFromSupplies(ItemStack stack, BlockCoord forBlock) {
|
||||
public ItemStack getSeedFromSupplies(ItemStack stack, BlockCoord forBlock) {
|
||||
return getSeedFromSupplies(stack, forBlock, true);
|
||||
}
|
||||
|
||||
public ItemStack getSeedFromSupplies(ItemStack stack, BlockCoord forBlock, boolean matchMetadata) {
|
||||
if(stack == null || forBlock == null) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
int slot = getSupplySlotForCoord(forBlock);
|
||||
ItemStack inv = inventory[slot];
|
||||
if(inv != null && inv.isItemEqual(stack)) {
|
||||
inv.stackSize--;
|
||||
if(inv.stackSize == 0) {
|
||||
inv = null;
|
||||
if(inv != null) {
|
||||
if(matchMetadata ? inv.isItemEqual(stack) : inv.getItem() == stack.getItem()) {
|
||||
ItemStack result = inv.copy();
|
||||
result.stackSize = 1;
|
||||
|
||||
inv.stackSize--;
|
||||
if(inv.stackSize == 0) {
|
||||
inv = null;
|
||||
}
|
||||
setInventorySlotContents(slot, inv);
|
||||
return result;
|
||||
}
|
||||
setInventorySlotContents(slot, inv);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
protected int getSupplySlotForCoord(BlockCoord forBlock) {
|
||||
@@ -208,7 +244,7 @@ public class TileFarmStation extends AbstractPoweredTaskEntity implements IEntit
|
||||
}
|
||||
BoundingBox bb = new BoundingBox(getLocation());
|
||||
AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(bb.minX, bb.minY, bb.minZ, bb.maxX, bb.maxY, bb.maxZ);
|
||||
aabb = aabb.expand(farmSize + 1, farmSize + 1, farmSize + 1);
|
||||
aabb = aabb.expand(farmSize + 3, farmSize + 3, farmSize + 3);
|
||||
List<Entity> interestingItems = worldObj.selectEntitiesWithinAABB(Entity.class, aabb, this);
|
||||
|
||||
for (Entity entity : interestingItems) {
|
||||
@@ -396,4 +432,6 @@ public class TileFarmStation extends AbstractPoweredTaskEntity implements IEntit
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package crazypants.enderio.farm;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.IPlantable;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import crazypants.util.BlockCoord;
|
||||
|
||||
public class TreeFarmer implements IFarmerJoe {
|
||||
|
||||
protected Block sapling;
|
||||
protected ItemStack saplingItem;
|
||||
protected Block wood;
|
||||
|
||||
public TreeFarmer(Block sapling, Block wood) {
|
||||
this.sapling = sapling;
|
||||
saplingItem = new ItemStack(sapling);
|
||||
this.wood = wood;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canHarvest(TileFarmStation farm, BlockCoord bc, Block block, int meta) {
|
||||
return block == wood;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlant(ItemStack stack) {
|
||||
return stack != null && stack.getItem() == saplingItem.getItem();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean prepareBlock(TileFarmStation farm, BlockCoord bc, Block block, int meta) {
|
||||
if(block == sapling) {
|
||||
return true;
|
||||
}
|
||||
return plantFromInventory(farm, bc, block, meta);
|
||||
}
|
||||
|
||||
protected boolean plantFromInventory(TileFarmStation farm, BlockCoord bc, Block block, int meta) {
|
||||
World worldObj = farm.getWorldObj();
|
||||
if(canPlant(worldObj, bc)) {
|
||||
ItemStack seed = farm.getSeedFromSupplies(saplingItem, bc, false);
|
||||
if(seed != null) {
|
||||
return plant(worldObj, bc, seed);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean canPlant(World worldObj, BlockCoord bc) {
|
||||
Block ground = worldObj.getBlock(bc.x, bc.y - 1, bc.z);
|
||||
IPlantable plantable = (IPlantable) sapling;
|
||||
if(sapling.canPlaceBlockAt(worldObj, bc.x, bc.y, bc.z) &&
|
||||
sapling.canBlockStay(worldObj, bc.x, bc.y, bc.z) &&
|
||||
ground.canSustainPlant(worldObj, bc.x, bc.y - 1, bc.z, ForgeDirection.UP, plantable)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean plant(World worldObj, BlockCoord bc, ItemStack seed) {
|
||||
worldObj.setBlock(bc.x, bc.y, bc.z, Blocks.air, 0, 1 | 2);
|
||||
if(canPlant(worldObj, bc)) {
|
||||
worldObj.setBlock(bc.x, bc.y, bc.z, sapling, seed.getItemDamage(), 1 | 2);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IHarvestResult harvestBlock(TileFarmStation farm, BlockCoord bc, Block block, int meta) {
|
||||
HarvestResult res = new HarvestResult();
|
||||
harvestUp(farm, bc, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
protected void harvestUp(TileFarmStation farm, BlockCoord bc, HarvestResult res) {
|
||||
boolean foundWoodAtLevel = false;
|
||||
if(wood == farm.getBlock(bc) && farm.hasAxe()) {
|
||||
res.harvestedBlocks.add(bc);
|
||||
ArrayList<ItemStack> drops = wood.getDrops(farm.getWorld(), bc.x, bc.y, bc.z, farm.getBlockMeta(bc), farm.geAxeLootingValue());
|
||||
if(drops != null) {
|
||||
for (ItemStack drop : drops) {
|
||||
res.drops.add(new EntityItem(farm.getWorld(), bc.x + 0.5, bc.y + 0.5, bc.z + 0.5, drop.copy()));
|
||||
}
|
||||
|
||||
}
|
||||
farm.damageAxe();
|
||||
farm.getWorld().setBlockToAir(bc.x, bc.y, bc.z);
|
||||
foundWoodAtLevel = true;
|
||||
} else {
|
||||
//for new trees, check the sides
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
if(dir.offsetY == 0) {
|
||||
foundWoodAtLevel |= wood == farm.getBlock(bc.getLocation(dir));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(foundWoodAtLevel) {
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
if(dir != ForgeDirection.DOWN) {
|
||||
harvestUp(farm, bc.getLocation(dir), res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user