allow for special plating behaviour in the crops api

This commit is contained in:
Hea3veN 2015-05-31 15:40:50 -03:00
parent 29a3340aa2
commit 4eaf4bb2b2
13 changed files with 229 additions and 73 deletions

View file

@ -4,12 +4,14 @@ import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
public final class CropManager {
private static List<ICropHandler> handlers = new ArrayList<ICropHandler>();
private static ICropHandler defaultHandler;
private CropManager() {
@ -19,13 +21,21 @@ public final class CropManager {
handlers.add(cropHandler);
}
public static void setDefaultHandler(ICropHandler cropHandler) {
defaultHandler = cropHandler;
}
public static ICropHandler getDefaultHandler() {
return defaultHandler;
}
public static boolean isSeed(ItemStack stack) {
for (ICropHandler cropHandler : handlers) {
if (cropHandler.isSeed(stack)) {
return true;
}
}
return false;
return defaultHandler.isSeed(stack);
}
public static boolean canSustainPlant(World world, ItemStack seed, int x, int y, int z) {
@ -34,7 +44,18 @@ public final class CropManager {
return true;
}
}
return false;
return defaultHandler.canSustainPlant(world, seed, x, y, z);
}
public static boolean plantCrop(World world, EntityPlayer player, ItemStack seed, int x, int y,
int z) {
for (ICropHandler cropHandler : handlers) {
if (cropHandler.isSeed(seed) && cropHandler.canSustainPlant(world, seed, x, y, z)
&& cropHandler.plantCrop(world, player, seed, x, y, z)) {
return true;
}
}
return defaultHandler.plantCrop(world, player, seed, x, y, z);
}
public static boolean isMature(IBlockAccess blockAccess, Block block, int meta, int x, int y,
@ -44,7 +65,7 @@ public final class CropManager {
return true;
}
}
return false;
return defaultHandler.isMature(blockAccess, block, meta, x, y, z);
}
public static boolean harvestCrop(World world, int x, int y, int z, List<ItemStack> drops) {
@ -55,7 +76,7 @@ public final class CropManager {
return cropHandler.harvestCrop(world, x, y, z, drops);
}
}
return false;
return defaultHandler.harvestCrop(world, x, y, z, drops);
}
}

View file

@ -3,6 +3,7 @@ package buildcraft.api.crops;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
@ -30,6 +31,20 @@ public interface ICropHandler {
*/
boolean canSustainPlant(World world, ItemStack seed, int x, int y, int z);
/**
* Plant the item in the block. You can assume plantCrop() will only be
* called if canSustainPlant() returned true.
*
* @param world
* @param player
* @param seed
* @param x
* @param y
* @param z
* @return true if the item was planted at (x, y, z).
*/
boolean plantCrop(World world, EntityPlayer player, ItemStack seed, int x, int y, int z);
/**
* Check if a crop is mature and can be harvested.
*
@ -51,7 +66,8 @@ public interface ICropHandler {
* @param x
* @param y
* @param z
* @param drops a list to return the harvest's drops.
* @param drops
* a list to return the harvest's drops.
* @return true if the block was successfully harvested.
*/
boolean harvestCrop(World world, int x, int y, int z, List<ItemStack> drops);

View file

@ -6,7 +6,7 @@
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
*/
@API(apiVersion = "1.0", owner = "BuildCraftAPI|Core", provides = "BuildCraftAPI|crops")
@API(apiVersion = "1.1", owner = "BuildCraftAPI|Core", provides = "BuildCraftAPI|crops")
package buildcraft.api.crops;
import cpw.mods.fml.common.API;

View file

@ -433,7 +433,7 @@ public class BuildCraftCore extends BuildCraftMod {
FMLCommonHandler.instance().bus().register(new TickHandlerCore());
CropManager.registerHandler(new CropHandlerPlantable());
CropManager.setDefaultHandler(new CropHandlerPlantable());
CropManager.registerHandler(new CropHandlerReeds());
BuildCraftAPI.registerWorldProperty("soft", new WorldPropertyIsSoft());

View file

@ -90,6 +90,7 @@ import buildcraft.robotics.ai.AIRobotHarvest;
import buildcraft.robotics.ai.AIRobotLoad;
import buildcraft.robotics.ai.AIRobotLoadFluids;
import buildcraft.robotics.ai.AIRobotMain;
import buildcraft.robotics.ai.AIRobotPlant;
import buildcraft.robotics.ai.AIRobotPumpBlock;
import buildcraft.robotics.ai.AIRobotRecharge;
import buildcraft.robotics.ai.AIRobotSearchAndGotoBlock;
@ -314,6 +315,7 @@ public class BuildCraftRobotics extends BuildCraftMod {
RobotManager.registerAIRobot(AIRobotHarvest.class, "aiRobotHarvest");
RobotManager.registerAIRobot(AIRobotLoad.class, "aiRobotLoad", "buildcraft.core.robots.AIRobotLoad");
RobotManager.registerAIRobot(AIRobotLoadFluids.class, "aiRobotLoadFluids", "buildcraft.core.robots.AIRobotLoadFluids");
RobotManager.registerAIRobot(AIRobotPlant.class, "aiRobotPlant");
RobotManager.registerAIRobot(AIRobotPumpBlock.class, "aiRobotPumpBlock", "buildcraft.core.robots.AIRobotPumpBlock");
RobotManager.registerAIRobot(AIRobotRecharge.class, "aiRobotRecharge", "buildcraft.core.robots.AIRobotRecharge");
RobotManager.registerAIRobot(AIRobotSearchAndGotoBlock.class, "aiRobotSearchAndGoToBlock");

View file

@ -9,6 +9,7 @@ import net.minecraft.block.BlockFlower;
import net.minecraft.block.BlockMelon;
import net.minecraft.block.BlockMushroom;
import net.minecraft.block.BlockTallGrass;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
@ -55,6 +56,11 @@ public class CropHandlerPlantable implements ICropHandler {
}
}
@Override
public boolean plantCrop(World world, EntityPlayer player, ItemStack seed, int x, int y, int z) {
return BlockUtils.useItemOnBlock(world, player, seed, x, y, z, ForgeDirection.UP);
}
@Override
public boolean isMature(IBlockAccess blockAccess, Block block, int meta, int x, int y, int z) {
if (block == null) {
@ -89,5 +95,4 @@ public class CropHandlerPlantable implements ICropHandler {
}
return false;
}
}

View file

@ -3,18 +3,14 @@ package buildcraft.core.crops;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.IPlantable;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.crops.CropManager;
import buildcraft.api.crops.ICropHandler;
import buildcraft.core.lib.utils.BlockUtils;
public class CropHandlerReeds implements ICropHandler {
@ -25,35 +21,21 @@ public class CropHandlerReeds implements ICropHandler {
@Override
public boolean canSustainPlant(World world, ItemStack seed, int x, int y, int z) {
Block block = world.getBlock(x, y, z);
return block.canSustainPlant(world, x, y, z, ForgeDirection.UP, (IPlantable) Blocks.reeds)
&& block != Blocks.reeds && world.isAirBlock(x, y + 1, z);
return CropManager.getDefaultHandler().canSustainPlant(world, seed, x, y, z);
}
@Override
public boolean plantCrop(World world, EntityPlayer player, ItemStack seed, int x, int y, int z) {
return CropManager.getDefaultHandler().plantCrop(world, player, seed, x, y, z);
}
@Override
public boolean isMature(IBlockAccess blockAccess, Block block, int meta, int x, int y, int z) {
if (block == null) {
return false;
} else if (block == Blocks.reeds) {
if (y > 0 && blockAccess.getBlock(x, y - 1, z) == block) {
return true;
}
}
return false;
}
@Override
public boolean harvestCrop(World world, int x, int y, int z, List<ItemStack> drops) {
if (!world.isRemote) {
Block block = world.getBlock(x, y, z);
int meta = world.getBlockMetadata(x, y, z);
if (BlockUtils.breakBlock((WorldServer) world, x, y, z, drops)) {
world.playAuxSFXAtEntity(null, 2001, x, y, z, Block.getIdFromBlock(block)
+ (meta << 12));
return true;
}
}
return false;
}
}

View file

@ -25,8 +25,11 @@ import net.minecraft.world.Explosion;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import cpw.mods.fml.common.FMLCommonHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;
import net.minecraftforge.fluids.Fluid;
@ -306,4 +309,16 @@ public final class BlockUtils {
return world.getBlockMetadata(x, y, z);
}
}
public static boolean useItemOnBlock(World world, EntityPlayer player, ItemStack stack, int x,
int y, int z, ForgeDirection direction) {
boolean done = stack.getItem().onItemUseFirst(stack, player, world, x, y, z,
direction.ordinal(), 0.5F, 0.5F, 0.5F);
if (!done) {
done = stack.getItem().onItemUse(stack, player, world, x, y, z, direction.ordinal(),
0.5F, 0.5F, 0.5F);
}
return done;
}
}

View file

@ -0,0 +1,89 @@
package buildcraft.robotics.ai;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MathHelper;
import net.minecraft.world.WorldServer;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.crops.CropManager;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.core.lib.utils.BlockUtils;
import buildcraft.core.proxy.CoreProxy;
public class AIRobotPlant extends AIRobot {
private BlockIndex blockFound;
private int delay = 0;
public AIRobotPlant(EntityRobotBase iRobot) {
super(iRobot);
}
public AIRobotPlant(EntityRobotBase iRobot, BlockIndex iBlockFound) {
this(iRobot);
blockFound = iBlockFound;
}
@Override
public void start() {
robot.aimItemAt(blockFound.x, blockFound.y, blockFound.z);
robot.setItemActive(true);
}
@Override
public void update() {
if (blockFound == null) {
setSuccess(false);
terminate();
}
if (delay++ > 40) {
EntityPlayer player = CoreProxy.proxy.getBuildCraftPlayer((WorldServer) robot.worldObj)
.get();
if (CropManager.plantCrop(robot.worldObj, player, robot.getHeldItem(), blockFound.x,
blockFound.y, blockFound.z)) {
} else {
setSuccess(false);
}
if (robot.getHeldItem().stackSize > 0) {
BlockUtils.dropItem((WorldServer) robot.worldObj,
MathHelper.floor_double(robot.posX), MathHelper.floor_double(robot.posY),
MathHelper.floor_double(robot.posZ), 6000, robot.getHeldItem());
}
robot.setItemInUse(null);
terminate();
}
}
@Override
public void end() {
robot.setItemActive(false);
}
@Override
public boolean canLoadFromNBT() {
return true;
}
@Override
public void writeSelfToNBT(NBTTagCompound nbt) {
super.writeSelfToNBT(nbt);
if (blockFound != null) {
NBTTagCompound sub = new NBTTagCompound();
blockFound.writeTo(sub);
nbt.setTag("blockFound", sub);
}
}
@Override
public void loadSelfFromNBT(NBTTagCompound nbt) {
super.loadSelfFromNBT(nbt);
if (nbt.hasKey("blockFound")) {
blockFound = new BlockIndex (nbt.getCompoundTag("blockFound"));
}
}
}

View file

@ -10,6 +10,7 @@ package buildcraft.robotics.ai;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MathHelper;
import net.minecraft.world.WorldServer;
@ -51,8 +52,8 @@ public class AIRobotUseToolOnBlock extends AIRobot {
EntityPlayer player = CoreProxy.proxy.getBuildCraftPlayer((WorldServer) robot.worldObj)
.get();
if (stack.getItem().onItemUse(stack, player, robot.worldObj, useToBlock.x,
useToBlock.y, useToBlock.z, ForgeDirection.UP.ordinal(), 0, 0, 0)) {
if (BlockUtils.useItemOnBlock(robot.worldObj, player, stack, useToBlock.x,
useToBlock.y, useToBlock.z, ForgeDirection.UP)) {
if (robot.getHeldItem().isItemStackDamageable()) {
robot.getHeldItem().damageItem(1, robot);
@ -86,4 +87,29 @@ public class AIRobotUseToolOnBlock extends AIRobot {
public int getEnergyCost() {
return 8;
}
@Override
public boolean canLoadFromNBT() {
return true;
}
@Override
public void writeSelfToNBT(NBTTagCompound nbt) {
super.writeSelfToNBT(nbt);
if (useToBlock != null) {
NBTTagCompound sub = new NBTTagCompound();
useToBlock.writeTo(sub);
nbt.setTag("blockFound", sub);
}
}
@Override
public void loadSelfFromNBT(NBTTagCompound nbt) {
super.loadSelfFromNBT(nbt);
if (nbt.hasKey("blockFound")) {
useToBlock = new BlockIndex (nbt.getCompoundTag("blockFound"));
}
}
}

View file

@ -24,8 +24,8 @@ import buildcraft.core.lib.inventory.filters.IStackFilter;
import buildcraft.core.lib.utils.IBlockFilter;
import buildcraft.robotics.ai.AIRobotFetchAndEquipItemStack;
import buildcraft.robotics.ai.AIRobotGotoSleep;
import buildcraft.robotics.ai.AIRobotPlant;
import buildcraft.robotics.ai.AIRobotSearchAndGotoBlock;
import buildcraft.robotics.ai.AIRobotUseToolOnBlock;
import buildcraft.robotics.statements.ActionRobotFilter;
public class BoardRobotPlanter extends RedstoneBoardRobot {
@ -71,11 +71,11 @@ public class BoardRobotPlanter extends RedstoneBoardRobot {
if (ai instanceof AIRobotSearchAndGotoBlock) {
if (ai.success()) {
blockFound = ((AIRobotSearchAndGotoBlock) ai).getBlockFound();
startDelegateAI(new AIRobotUseToolOnBlock(robot, blockFound));
startDelegateAI(new AIRobotPlant(robot, blockFound));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
}
} else if (ai instanceof AIRobotUseToolOnBlock) {
} else if (ai instanceof AIRobotPlant) {
releaseBlockFound();
} else if (ai instanceof AIRobotFetchAndEquipItemStack) {
if (robot.getHeldItem() == null) {

View file

@ -1,34 +1,37 @@
package buildcraft.transport.stripes;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.IPlantable;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.crops.CropManager;
import buildcraft.api.transport.IStripesActivator;
import buildcraft.api.transport.IStripesHandler;
public class StripesHandlerPlant implements IStripesHandler {
@Override
public StripesHandlerType getType() {
return StripesHandlerType.ITEM_USE;
}
@Override
public StripesHandlerType getType() {
return StripesHandlerType.ITEM_USE;
}
@Override
public boolean shouldHandle(ItemStack stack) {
return stack.getItem() instanceof IPlantable;
}
@Override
public boolean shouldHandle(ItemStack stack) {
return CropManager.isSeed(stack);
}
@Override
public boolean handle(World world, int x, int y, int z, ForgeDirection direction, ItemStack stack, EntityPlayer player, IStripesActivator activator) {
Block b = world.getBlock(x, y - 1, z);
IPlantable plant = (IPlantable) stack.getItem();
if (b.canSustainPlant(world, x, y - 1, z, ForgeDirection.UP, (IPlantable) stack.getItem())) {
world.setBlock(x, y, z, plant.getPlant(world, x, y, z), plant.getPlantMetadata(world, x, y, z), 3);
return true;
}
return false;
}
@Override
public boolean handle(World world, int x, int y, int z, ForgeDirection direction,
ItemStack stack, EntityPlayer player, IStripesActivator activator) {
if (CropManager.canSustainPlant(world, stack, x, y - 1, z)) {
if (CropManager.plantCrop(world, player, stack, x, y - 1, z)) {
if (stack.stackSize > 0) {
activator.sendItem(stack, direction.getOpposite());
}
return true;
}
}
return false;
}
}

View file

@ -6,12 +6,15 @@ import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.Position;
import buildcraft.api.transport.IStripesActivator;
import buildcraft.api.transport.IStripesHandler;
import buildcraft.core.lib.utils.BlockUtils;
public class StripesHandlerUse implements IStripesHandler {
public static final List<Item> items = new ArrayList<Item>();
@ -33,21 +36,15 @@ public class StripesHandlerUse implements IStripesHandler {
Position target = new Position(x, y, z, direction);
target.moveForwards(1.0D);
boolean done = stack.getItem().onItemUseFirst(stack, player, world,
(int) target.x, (int) target.y, (int) target.z,
direction.getOpposite().ordinal(), 0.5F, 0.5F, 0.5F);
if (!done) {
done = stack.getItem().onItemUse(stack, player, world,
(int) target.x, (int) target.y, (int) target.z,
direction.getOpposite().ordinal(), 0.5F, 0.5F, 0.5F);
if (BlockUtils.useItemOnBlock(world, player, stack, MathHelper.floor_double(target.x),
MathHelper.floor_double(target.y), MathHelper.floor_double(target.z),
direction.getOpposite())) {
if (stack.stackSize > 0) {
activator.sendItem(stack, direction.getOpposite());
}
return true;
}
if (stack.stackSize > 0) {
activator.sendItem(stack, direction.getOpposite());
}
return done;
return false;
}
}