Added bone meal support to farming station

* 2 new slots for bone meal
* Will try to use it on everything thats on the field and cannot be
harvested
* Different RF usage for successful and unsuccessfull tries
* Fix: Output slots no longer accept seeds
* Fix: "Only one tool per slot" policy works again
This commit is contained in:
Henry Loenwind authored and HenryLoenwind committed 2015-03-20 03:16:23 +01:00
1 parent 39b871dd17
commit 139544af3e
6 files changed
+140 -45

No files matched your search

@@ -263,6 +263,8 @@ public final class Config {
public static int farmContinuousEnergyUseRF = 40;
public static int farmActionEnergyUseRF = 500;
public static int farmAxeActionEnergyUseRF = 1000;
public static int farmBonemealActionEnergyUseRF = 160;
public static int farmBonemealTryEnergyUseRF = 80;
public static int farmDefaultSize = 3;
public static boolean farmAxeDamageOnLeafBreak = false;
@@ -950,6 +952,11 @@ public final class Config {
farmAxeActionEnergyUseRF = config.get(sectionFarm.name, "farmAxeActionEnergyUseRF", farmAxeActionEnergyUseRF,
"The amount of power used by a farm per wood block 'chopped'").getInt(farmAxeActionEnergyUseRF);
farmBonemealActionEnergyUseRF = config.get(sectionFarm.name, "farmBonemealActionEnergyUseRF", farmBonemealActionEnergyUseRF,
"The amount of power used by a farm per bone meal used").getInt(farmBonemealActionEnergyUseRF);
farmBonemealTryEnergyUseRF = config.get(sectionFarm.name, "farmBonemealTryEnergyUseRF", farmBonemealTryEnergyUseRF,
"The amount of power used by a farm per bone meal try").getInt(farmBonemealTryEnergyUseRF);
farmDefaultSize = config.get(sectionFarm.name, "farmDefaultSize", farmDefaultSize,
"The number of blocks a farm will extend from its center").getInt(farmDefaultSize);
@@ -14,7 +14,8 @@ public class FarmStationContainer extends AbstractMachineContainer {
private static final int COL_TOOLS = 44;
private static final int COL_INPUT = 53;
private static final int COL_OUTPUT = 116;
private static final int COL_FERTILIZER = 116;
private static final int COL_OUTPUT = 107;
private static final int SLOT_SIZE = 18;
private static final int ONE = 0 * SLOT_SIZE;
@@ -33,6 +34,9 @@ public class FarmStationContainer extends AbstractMachineContainer {
new Point(COL_TOOLS + TWO, ROW_TOOLS),
new Point(COL_TOOLS + THREE, ROW_TOOLS),
new Point(COL_FERTILIZER + ONE, ROW_TOOLS),
new Point(COL_FERTILIZER + TWO, ROW_TOOLS),
new Point(COL_INPUT + ONE, ROW_IO + ONE),
new Point(COL_INPUT + TWO, ROW_IO + ONE),
new Point(COL_INPUT + ONE, ROW_IO + TWO),
@@ -25,6 +25,11 @@ public class PacketFarmAction implements IMessage, IMessageHandler<PacketFarmAct
this.coords = coords;
}
public PacketFarmAction(BlockCoord bc) {
this.coords = new ArrayList<BlockCoord>(1);
this.coords.add(bc);
}
@Override
public void toBytes(ByteBuf buffer) {
int size = coords.size();
@@ -9,6 +9,7 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemShears;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@@ -31,11 +32,14 @@ import crazypants.enderio.network.PacketHandler;
import crazypants.enderio.power.BasicCapacitor;
import crazypants.enderio.power.Capacitors;
import crazypants.enderio.power.ICapacitor;
import crazypants.enderio.tool.ArrayMappingTool;
import crazypants.util.BlockCoord;
import crazypants.util.Lang;
public class TileFarmStation extends AbstractPoweredTaskEntity {
private static final int TICKS_PER_WORK = 20;
public enum ToolType {
HOE {
@Override
@@ -66,6 +70,12 @@ public class TileFarmStation extends AbstractPoweredTaskEntity {
boolean match(ItemStack item) {
return item.getItem() instanceof ItemShears;
}
},
NONE {
@Override
boolean match(ItemStack item) {
return false;
}
};
public final boolean itemMatches(ItemStack item) {
@@ -90,6 +100,15 @@ public class TileFarmStation extends AbstractPoweredTaskEntity {
}
return false;
}
public static ToolType getToolType(ItemStack stack) {
for (ToolType type : values()) {
if (type.itemMatches(stack)) {
return type;
}
}
return NONE;
}
}
public static final String NOTIFICATION_NO_HOE = "noHoe";
@@ -99,13 +118,20 @@ public class TileFarmStation extends AbstractPoweredTaskEntity {
private BlockCoord lastScanned;
private EntityPlayerMP farmerJoe;
public static final int NUM_TOOL_SLOTS = 3;
private static final int minToolSlot = 0;
private static final int maxToolSlot = 2;
private static final int maxToolSlot = -1 + NUM_TOOL_SLOTS;
public static final int NUM_FERTILIZER_SLOTS = 2;
public static final int minFirtSlot = maxToolSlot + 1;
public static final int maxFirtSlot = maxToolSlot + NUM_FERTILIZER_SLOTS;
public static final int NUM_SUPPLY_SLOTS = 4;
public static final int minSupSlot = maxToolSlot + 1;
public static final int maxSupSlot = maxToolSlot + NUM_SUPPLY_SLOTS;
public static final int minSupSlot = maxFirtSlot + 1;
public static final int maxSupSlot = maxFirtSlot + NUM_SUPPLY_SLOTS;
private final BitSet lockedSlots = new BitSet();
@@ -117,7 +143,7 @@ public class TileFarmStation extends AbstractPoweredTaskEntity {
private boolean wasActive;
public TileFarmStation() {
super(new SlotDefinition(7, 6, 1));
super(new SlotDefinition(9, 6, 1));
}
public int getFarmSize() {
@@ -321,21 +347,21 @@ public class TileFarmStation extends AbstractPoweredTaskEntity {
}
if(i <= maxToolSlot) {
if (ToolType.isTool(stack)) {
int otherSlot = i == minToolSlot ? maxToolSlot : minToolSlot;
if (inventory[otherSlot] == null) {
return true;
} else { // let's make sure there's not one of this type in here already
for (ToolType type : ToolType.values()) {
if (type.itemMatches(inventory[otherSlot]) && type.itemMatches(stack)) {
return false;
}
for (int j = minToolSlot; j <= maxToolSlot; j++) {
if (ToolType.getToolType(stack).itemMatches(inventory[j])) {
return false;
}
return true;
}
return true;
}
return false;
} else if (i <= maxFirtSlot) {
return stack.getItem() == Items.dye && stack.getItemDamage() == 15;
} else if (i <= maxSupSlot) {
return (inventory[i] != null || !isSlotLocked(i)) && FarmersCommune.instance.canPlant(stack);
} else {
return false;
}
return (inventory[i] != null || !isSlotLocked(i)) && FarmersCommune.instance.canPlant(stack);
}
@Override
@@ -376,7 +402,7 @@ public class TileFarmStation extends AbstractPoweredTaskEntity {
protected void doTick() {
if (sendNotification && shouldDoWorkThisTick(20)) {
if (sendNotification && shouldDoWorkThisTick(TICKS_PER_WORK)) {
sendNotification = false;
sendNotification();
}
@@ -424,23 +450,50 @@ public class TileFarmStation extends AbstractPoweredTaskEntity {
if(!isOpen(bc)) {
IHarvestResult harvest = FarmersCommune.instance.harvestBlock(this, bc, block, meta);
if(harvest != null) {
if(harvest.getDrops() != null) {
PacketFarmAction pkt = new PacketFarmAction(harvest.getHarvestedBlocks());
PacketHandler.INSTANCE.sendToAllAround(pkt, new TargetPoint(worldObj.provider.dimensionId, bc.x, bc.y, bc.z, 64));
for (EntityItem ei : harvest.getDrops()) {
if(ei != null) {
insertHarvestDrop(ei, bc);
if(!ei.isDead) {
worldObj.spawnEntityInWorld(ei);
}
if(harvest != null && harvest.getDrops() != null) {
PacketFarmAction pkt = new PacketFarmAction(harvest.getHarvestedBlocks());
PacketHandler.INSTANCE.sendToAllAround(pkt, new TargetPoint(worldObj.provider.dimensionId, bc.x, bc.y, bc.z, 64));
for (EntityItem ei : harvest.getDrops()) {
if(ei != null) {
insertHarvestDrop(ei, bc);
if(!ei.isDead) {
worldObj.spawnEntityInWorld(ei);
}
}
}
} else if (!isOpen(bc) && hasBonemeal() && bonemealCooldown-- <= 0) {
// there's a block and it did not produce harvest. Try bonemealing it
if (inventory[minFirtSlot].getItem().onItemUse(inventory[minFirtSlot], farmerJoe, worldObj, bc.x, bc.y, bc.z, 1, 0.5f, 0.5f, 0.5f)) {
worldObj.playAuxSFX(2005, bc.x, bc.y, bc.z, 0);
PacketHandler.INSTANCE.sendToAllAround(new PacketFarmAction(bc), new TargetPoint(worldObj.provider.dimensionId, bc.x, bc.y, bc.z, 64));
if (inventory[minFirtSlot].stackSize == 0) {
inventory[minFirtSlot] = null;
}
bonemealCooldown = 5;
usePower(Config.farmBonemealActionEnergyUseRF);
} else {
usePower(Config.farmBonemealTryEnergyUseRF);
}
}
}
}
private int bonemealCooldown = 5; // no need to persist this
private boolean hasBonemeal() {
if (inventory[minFirtSlot] != null) {
return true;
}
for (int i = minFirtSlot + 1; i <= maxFirtSlot; i++) {
if (inventory[i] != null) {
inventory[minFirtSlot] = inventory[i];
inventory[i] = null;
return true;
}
}
return false;
}
private boolean isOutputFull() {
for (int i = slotDefinition.minOutputSlot; i <= slotDefinition.maxOutputSlot; i++) {
ItemStack curStack = inventory[i];
@@ -673,24 +726,12 @@ public class TileFarmStation extends AbstractPoweredTaskEntity {
lockedSlots.set(i);
}
int slotLayoutVersion = nbtRoot.getInteger("slotLayoutVersion");
if (slotLayoutVersion < 1) {
// update from v0 (2+4+4+1) to v1 (3+4+4+1) by moving in+out+cap one back
for (int i = (3+4+4+1) - 2; i >= 2; i--) {
if (inventory[i+1] == null) {
// Should always be true, but better safe than deleting items
inventory[i+1] = inventory[i];
inventory[i] = null;
}
}
slotLayoutVersion = 1;
}
if (slotLayoutVersion < 2) {
// update from v1 (3+4+4+1) to v2 (3+4+6+1) by moving cap to new position
if (inventory[(3+4+6+1)-1] == null) {
inventory[(3+4+6+1)-1] = inventory[(3+4+4+1)-1];
inventory[(3+4+4+1)-3] = null;
}
slotLayoutVersion = 2;
if (slotLayoutVersion == 0) {
inventory = (new ArrayMappingTool<ItemStack>("TTSSSSOOOOC", "TTTBBSSSSOOOOOOC")).map(inventory);
} else if (slotLayoutVersion == 1) {
inventory = (new ArrayMappingTool<ItemStack>("TTTSSSSOOOOC", "TTTBBSSSSOOOOOOC")).map(inventory);
} else if (slotLayoutVersion == 2) {
inventory = (new ArrayMappingTool<ItemStack>("TTTSSSSOOOOOOC", "TTTBBSSSSOOOOOOC")).map(inventory);
}
}
@@ -714,7 +755,7 @@ public class TileFarmStation extends AbstractPoweredTaskEntity {
}
nbtRoot.setIntArray("lockedSlots", locked);
}
nbtRoot.setInteger("slotLayoutVersion", 2);
nbtRoot.setInteger("slotLayoutVersion", 3);
}
}
@@ -0,0 +1,38 @@
package crazypants.enderio.tool;
import java.lang.reflect.Array;
import java.util.Arrays;
import crazypants.enderio.machine.AbstractMachineEntity;
public class ArrayMappingTool<T extends Object> {
private final byte[] from_mapping;
private final byte[] to_mapping;
public ArrayMappingTool(String from_mapping, String to_mapping) {
this.from_mapping = from_mapping.getBytes();
this.to_mapping = to_mapping.getBytes();
}
private T getNextT(T[] in, byte what) {
for (int i = 0; i < from_mapping.length; i++) {
if (from_mapping[i] == what && in[i] != null) {
T result = in[i];
in[i] = null;
return result;
}
}
return null;
}
public T[] map(T[] in) {
T[] in_tmp = in.clone();
T[] out = (T[]) Array.newInstance(in.getClass().getComponentType(), to_mapping.length);
for (int i = 0; i < to_mapping.length; i++) {
out[i] = getNextT(in_tmp, to_mapping[i]);
}
return out;
}
}