diff --git a/src/main/java/WayofTime/bloodmagic/core/registry/HarvestRegistry.java b/src/main/java/WayofTime/bloodmagic/core/registry/HarvestRegistry.java deleted file mode 100644 index 9be7251b..00000000 --- a/src/main/java/WayofTime/bloodmagic/core/registry/HarvestRegistry.java +++ /dev/null @@ -1,102 +0,0 @@ -package WayofTime.bloodmagic.core.registry; - -import WayofTime.bloodmagic.util.BlockStack; -import WayofTime.bloodmagic.iface.IHarvestHandler; -import net.minecraft.block.Block; -import net.minecraft.block.BlockStem; - -import java.util.*; - -public class HarvestRegistry { - private static List handlerList = new ArrayList<>(); - private static Map standardCrops = new HashMap<>(); - private static Set tallCrops = new HashSet<>(); - private static Map stemCrops = new HashMap<>(); - private static Map amplifierMap = new HashMap<>(); - - /** - * Registers a handler for the Harvest Ritual to call. - * - * @param handler - The custom handler to register - */ - public static void registerHandler(IHarvestHandler handler) { - if (!handlerList.contains(handler)) - handlerList.add(handler); - } - - /** - * Registers a standard crop (IE: Wheat, Carrots, Potatoes, Netherwart, etc) - * for the - * {@link WayofTime.bloodmagic.ritual.harvest.HarvestHandlerPlantable} - * handler to handle. - * - * @param crop - The crop block to handle. - * @param matureMeta - The meta value at which the crop is considered mature and ready - * to be harvested. - */ - public static void registerStandardCrop(Block crop, int matureMeta) { - if (!standardCrops.containsKey(crop)) - standardCrops.put(crop, matureMeta); - } - - /** - * Registers a tall crop (Sugar Cane and Cactus) for the - * {@link WayofTime.bloodmagic.ritual.harvest.HarvestHandlerTall} handler to - * handle. - * - * @param crop - The crop block to handle. - */ - public static void registerTallCrop(BlockStack crop) { - if (!tallCrops.contains(crop)) - tallCrops.add(crop); - } - - /** - * Registers a stem crop (Melon and Pumpkin) for the - * {@link WayofTime.bloodmagic.ritual.harvest.HarvestHandlerStem} handler to - * handle. - *

- * Use {@link net.minecraftforge.oredict.OreDictionary#WILDCARD_VALUE} to - * accept any meta for the crop block. - *

- * The Stem must be instanceof {@link BlockStem} - * - * @param crop - The crop block to handle. - * @param stem - The stem of the crop - */ - public static void registerStemCrop(BlockStack crop, BlockStack stem) { - if (!stemCrops.containsKey(crop) && stem.getBlock() instanceof BlockStem) - stemCrops.put(stem, crop); - } - - /** - * Registers a range amplifier for the Harvest Ritual. - * - * @param blockStack - The block for the amplifier. - * @param range - The range the amplifier provides. - */ - public static void registerRangeAmplifier(BlockStack blockStack, int range) { - if (!amplifierMap.containsKey(blockStack)) - amplifierMap.put(blockStack, range); - } - - public static List getHandlerList() { - return new ArrayList<>(handlerList); - } - - public static Map getStandardCrops() { - return new HashMap<>(standardCrops); - } - - public static Set getTallCrops() { - return new HashSet<>(tallCrops); - } - - public static Map getStemCrops() { - return new HashMap<>(stemCrops); - } - - public static Map getAmplifierMap() { - return new HashMap<>(amplifierMap); - } -} diff --git a/src/main/java/WayofTime/bloodmagic/iface/IHarvestHandler.java b/src/main/java/WayofTime/bloodmagic/iface/IHarvestHandler.java deleted file mode 100644 index 91240f62..00000000 --- a/src/main/java/WayofTime/bloodmagic/iface/IHarvestHandler.java +++ /dev/null @@ -1,26 +0,0 @@ -package WayofTime.bloodmagic.iface; - -import WayofTime.bloodmagic.util.BlockStack; -import WayofTime.bloodmagic.ritual.data.IMasterRitualStone; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; - -/** - * Used to define a HarvestHandler for the Harvest Ritual. - */ -public interface IHarvestHandler { - /** - * Called whenever the Harvest Ritual attempts to harvest a block.
- * Use this to break the block, plant a new one, and drop the produced - * items.
- * Make sure to do checks so you are certain the blocks being handled are - * the block types you want. - * - * @param world - The world the - * {@link IMasterRitualStone} is in. - * @param pos - The position of the Block being checked - * @param blockStack - The Block being checked - * @return If the block was successfully harvested. - */ - boolean harvestAndPlant(World world, BlockPos pos, BlockStack blockStack); -} diff --git a/src/main/java/WayofTime/bloodmagic/registry/ModRituals.java b/src/main/java/WayofTime/bloodmagic/registry/ModRituals.java index 5f5a6564..8582d583 100644 --- a/src/main/java/WayofTime/bloodmagic/registry/ModRituals.java +++ b/src/main/java/WayofTime/bloodmagic/registry/ModRituals.java @@ -2,7 +2,7 @@ package WayofTime.bloodmagic.registry; import WayofTime.bloodmagic.ConfigHandler; import WayofTime.bloodmagic.util.BlockStack; -import WayofTime.bloodmagic.core.registry.HarvestRegistry; +import WayofTime.bloodmagic.ritual.harvest.HarvestRegistry; import WayofTime.bloodmagic.core.registry.ImperfectRitualRegistry; import WayofTime.bloodmagic.core.registry.RitualRegistry; import WayofTime.bloodmagic.ritual.data.Ritual; @@ -144,9 +144,9 @@ public class ModRituals public static void initHarvestHandlers() { - HarvestRegistry.registerRangeAmplifier(new BlockStack(Blocks.DIAMOND_BLOCK), 15); - HarvestRegistry.registerRangeAmplifier(new BlockStack(Blocks.GOLD_BLOCK), 10); - HarvestRegistry.registerRangeAmplifier(new BlockStack(Blocks.IRON_BLOCK), 6); + HarvestRegistry.registerRangeAmplifier(Blocks.DIAMOND_BLOCK.getDefaultState(), 15); + HarvestRegistry.registerRangeAmplifier(Blocks.GOLD_BLOCK.getDefaultState(), 10); + HarvestRegistry.registerRangeAmplifier(Blocks.IRON_BLOCK.getDefaultState(), 6); HarvestRegistry.registerHandler(new HarvestHandlerPlantable()); HarvestRegistry.registerHandler(new HarvestHandlerTall()); diff --git a/src/main/java/WayofTime/bloodmagic/ritual/RitualHarvest.java b/src/main/java/WayofTime/bloodmagic/ritual/RitualHarvest.java index ec6650f1..63036470 100644 --- a/src/main/java/WayofTime/bloodmagic/ritual/RitualHarvest.java +++ b/src/main/java/WayofTime/bloodmagic/ritual/RitualHarvest.java @@ -1,14 +1,23 @@ package WayofTime.bloodmagic.ritual; import WayofTime.bloodmagic.BloodMagic; -import WayofTime.bloodmagic.util.BlockStack; -import WayofTime.bloodmagic.iface.IHarvestHandler; -import WayofTime.bloodmagic.core.registry.HarvestRegistry; +import WayofTime.bloodmagic.ritual.harvest.IHarvestHandler; +import WayofTime.bloodmagic.ritual.harvest.HarvestRegistry; import WayofTime.bloodmagic.ritual.data.*; +import com.google.common.collect.Lists; +import net.minecraft.block.state.IBlockState; +import net.minecraft.inventory.InventoryHelper; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.items.ItemHandlerHelper; import java.util.ArrayList; +import java.util.List; /** * This ritual uses registered {@link IHarvestHandler}'s to harvest blocks. @@ -18,7 +27,7 @@ import java.util.ArrayList; *

* This ritual includes a way to change the range based on what block is above * the MasterRitualStone. You can use - * {@link HarvestRegistry#registerRangeAmplifier(BlockStack, int)} to register a + * {@link HarvestRegistry#registerRangeAmplifier(net.minecraft.block.state.IBlockState, int)} to register a * new amplifier. */ public class RitualHarvest extends Ritual { @@ -47,7 +56,7 @@ public class RitualHarvest extends Ritual { harvestArea.resetIterator(); while (harvestArea.hasNext()) { BlockPos nextPos = harvestArea.next().add(pos); - if (harvestBlock(world, nextPos)) { + if (harvestBlock(world, nextPos, masterRitualStone.getBlockPos())) { harvested++; } } @@ -102,12 +111,34 @@ public class RitualHarvest extends Ritual { return new RitualHarvest(); } - public static boolean harvestBlock(World world, BlockPos pos) { - BlockStack harvestStack = BlockStack.getStackFromPos(world, pos); + public static boolean harvestBlock(World world, BlockPos cropPos, BlockPos controllerPos) { + IBlockState harvestState = world.getBlockState(cropPos); + TileEntity potentialInventory = world.getTileEntity(controllerPos.up()); + IItemHandler itemHandler = null; + if (potentialInventory != null && potentialInventory.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN)) + itemHandler = potentialInventory.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN); - for (IHarvestHandler handler : HarvestRegistry.getHandlerList()) - if (handler.harvestAndPlant(world, pos, harvestStack)) - return true; + for (IHarvestHandler handler : HarvestRegistry.getHarvestHandlers()) { + if (handler.test(world, cropPos, harvestState)) { + List drops = Lists.newArrayList(); + if (handler.harvest(world, cropPos, harvestState, drops)) { + for (ItemStack stack : drops) { + if (stack.isEmpty()) + continue; + + // TODO I wrote this, but didn't actually think about whether it should be a thing. Remove the true if we want to keep it + if (itemHandler == null || true) + InventoryHelper.spawnItemStack(world, cropPos.getX(), cropPos.getY(), cropPos.getZ(), stack); + else { + ItemStack remainder = ItemHandlerHelper.insertItemStacked(itemHandler, stack, false); + if (!remainder.isEmpty()) + InventoryHelper.spawnItemStack(world, cropPos.getX(), cropPos.getY(), cropPos.getZ(), remainder); + } + } + return true; + } + } + } return false; } diff --git a/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerPlantable.java b/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerPlantable.java index e6f600ed..38a405f9 100644 --- a/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerPlantable.java +++ b/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerPlantable.java @@ -1,21 +1,18 @@ package WayofTime.bloodmagic.ritual.harvest; -import WayofTime.bloodmagic.BloodMagic; -import WayofTime.bloodmagic.util.BlockStack; -import WayofTime.bloodmagic.iface.IHarvestHandler; -import WayofTime.bloodmagic.core.registry.HarvestRegistry; +import WayofTime.bloodmagic.util.BMLog; import net.minecraft.block.Block; import net.minecraft.block.BlockCrops; -import net.minecraft.entity.item.EntityItem; +import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; +import net.minecraft.util.NonNullList; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.common.IPlantable; import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.registry.ForgeRegistries; -import net.minecraftforge.fml.relauncher.ReflectionHelper; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -25,10 +22,10 @@ import java.util.List; /** * Harvest handler for standard plantable crops such as Wheat, Potatoes, and * Netherwart.
- * Register a new crop for this handler with - * {@link HarvestRegistry#registerStandardCrop(Block, int)} + * Register a new crop for this handler with {@link HarvestRegistry#registerStandardCrop(Block, int)} */ public class HarvestHandlerPlantable implements IHarvestHandler { + public HarvestHandlerPlantable() { HarvestRegistry.registerStandardCrop(Blocks.CARROTS, 7); HarvestRegistry.registerStandardCrop(Blocks.WHEAT, 7); @@ -54,19 +51,12 @@ public class HarvestHandlerPlantable implements IHarvestHandler { } @Override - public boolean harvestAndPlant(World world, BlockPos pos, BlockStack blockStack) { - if (!HarvestRegistry.getStandardCrops().containsKey(blockStack.getBlock())) - return false; - - int matureMeta = HarvestRegistry.getStandardCrops().get(blockStack.getBlock()); - - if (blockStack.getMeta() < matureMeta) - return false; - - List drops = blockStack.getBlock().getDrops(world, pos, blockStack.getState(), 0); + public boolean harvest(World world, BlockPos pos, IBlockState state, List drops) { + NonNullList blockDrops = NonNullList.create(); + state.getBlock().getDrops(blockDrops, world, pos, state, 0); boolean foundSeed = false; - for (ItemStack stack : drops) { + for (ItemStack stack : blockDrops) { if (stack.isEmpty()) continue; @@ -78,16 +68,13 @@ public class HarvestHandlerPlantable implements IHarvestHandler { } if (foundSeed) { - world.setBlockState(pos, blockStack.getBlock().getDefaultState()); - world.playEvent(2001, pos, Block.getStateId(blockStack.getState())); - for (ItemStack stack : drops) { + world.setBlockState(pos, state.getBlock().getDefaultState()); + world.playEvent(2001, pos, Block.getStateId(state)); + for (ItemStack stack : blockDrops) { if (stack.isEmpty()) continue; - if (!world.isRemote) { - EntityItem toDrop = new EntityItem(world, pos.getX(), pos.getY() + 0.5, pos.getZ(), stack); - world.spawnEntity(toDrop); - } + drops.add(stack); } return true; @@ -96,6 +83,11 @@ public class HarvestHandlerPlantable implements IHarvestHandler { return false; } + @Override + public boolean test(World world, BlockPos pos, IBlockState state) { + return HarvestRegistry.getStandardCrops().containsKey(state.getBlock()) && state.getBlock().getMetaFromState(state) == HarvestRegistry.getStandardCrops().get(state.getBlock()); + } + private static void addThirdPartyCrop(String modid, String regName, int matureMeta) { if (!Loader.isModLoaded(modid)) return; @@ -110,19 +102,19 @@ public class HarvestHandlerPlantable implements IHarvestHandler { return; try { - ClassLoader loader = HarvestHandlerPlantable.class.getClassLoader(); - String className = "com.pam.harvestcraft.blocks.CropRegistry"; - Class registry = ReflectionHelper.getClass(loader, className); - Field names = ReflectionHelper.findField(registry, "cropNames"); - Method getCrop = registry.getMethod("getCrop", String.class); + Class pamRegistry = Class.forName("com.pam.harvestcraft.blocks.CropRegistry"); + Field names = pamRegistry.getDeclaredField("cropNames"); + Method getCrop = pamRegistry.getMethod("getCrop", String.class); for (String name : (String[]) names.get(null)) { BlockCrops crop = (BlockCrops) getCrop.invoke(null, name); HarvestRegistry.registerStandardCrop(crop, crop.getMaxAge()); } - } catch (NoSuchMethodException e) { - BloodMagic.instance.logger.error("HarvestCraft integration cancelled; unable to find crop name mapper"); + } catch (ClassNotFoundException e) { + BMLog.DEFAULT.error("HarvestCraft integration cancelled; unable to find registry class"); + } catch (NoSuchMethodException | NoSuchFieldException e) { + BMLog.DEFAULT.error("HarvestCraft integration cancelled; unable to find crop name mapper"); } catch (IllegalAccessException | InvocationTargetException e) { - BloodMagic.instance.logger.error("HarvestCraft integration cancelled; crop name lookup broke"); + BMLog.DEFAULT.error("HarvestCraft integration cancelled; crop name lookup broke"); } } } diff --git a/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerStem.java b/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerStem.java index 79aa7dbd..2e7bb2fd 100644 --- a/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerStem.java +++ b/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerStem.java @@ -1,62 +1,55 @@ package WayofTime.bloodmagic.ritual.harvest; -import WayofTime.bloodmagic.util.BlockStack; -import WayofTime.bloodmagic.iface.IHarvestHandler; -import WayofTime.bloodmagic.core.registry.HarvestRegistry; +import net.minecraft.block.BlockPumpkin; import net.minecraft.block.BlockStem; -import net.minecraft.entity.item.EntityItem; +import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; +import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import net.minecraftforge.oredict.OreDictionary; -import java.util.ArrayList; import java.util.List; /** - * Harvest handler for crops with stems such as Pumpkins and Melons. - * {@link OreDictionary#WILDCARD_VALUE} is used as a wildcard to allow the crop - * to be harvested at any metadata. Rotation based crop blocks are a good reason + * Harvest handler for crops with stems such as Pumpkins and Melons. Rotation based crop blocks are a good reason * to use this (see pumpkins).
* Register a new crop for this handler with - * {@link HarvestRegistry#registerStemCrop(BlockStack, BlockStack)} + * {@link HarvestRegistry#registerStemCrop(IBlockState, IBlockState)} */ public class HarvestHandlerStem implements IHarvestHandler { + public HarvestHandlerStem() { - HarvestRegistry.registerStemCrop(new BlockStack(Blocks.PUMPKIN, OreDictionary.WILDCARD_VALUE), new BlockStack(Blocks.PUMPKIN_STEM, 7)); - HarvestRegistry.registerStemCrop(new BlockStack(Blocks.MELON_BLOCK), new BlockStack(Blocks.MELON_STEM, 7)); + for (EnumFacing facing : EnumFacing.HORIZONTALS) + HarvestRegistry.registerStemCrop(Blocks.PUMPKIN.getDefaultState().withProperty(BlockPumpkin.FACING, facing), Blocks.PUMPKIN_STEM.getDefaultState().withProperty(BlockStem.AGE, 7)); + + HarvestRegistry.registerStemCrop(Blocks.MELON_BLOCK.getDefaultState(), Blocks.MELON_STEM.getDefaultState().withProperty(BlockStem.AGE, 7)); } @Override - public boolean harvestAndPlant(World world, BlockPos pos, BlockStack blockStack) { - boolean retFlag = false; - List drops = new ArrayList<>(); - BlockPos cropPos = pos; - if (HarvestRegistry.getStemCrops().containsKey(blockStack)) { - EnumFacing cropDir = blockStack.getBlock().getActualState(blockStack.getState(), world, pos).getValue(BlockStem.FACING); + public boolean harvest(World world, BlockPos pos, IBlockState state, List drops) { + EnumFacing cropDir = state.getBlock().getActualState(state, world, pos).getValue(BlockStem.FACING); - if (cropDir != EnumFacing.UP) { - cropPos = pos.offset(cropDir); - BlockStack probableCrop = BlockStack.getStackFromPos(world, cropPos); - BlockStack regCrop = HarvestRegistry.getStemCrops().get(blockStack); + if (cropDir != EnumFacing.UP) { + BlockPos cropPos = pos.offset(cropDir); + IBlockState probableCrop = world.getBlockState(cropPos); + IBlockState registeredCrop = HarvestRegistry.getStemCrops().get(state); - if ((regCrop.getMeta() == OreDictionary.WILDCARD_VALUE && regCrop.getBlock() == probableCrop.getBlock()) || regCrop.equals(probableCrop)) { - drops = probableCrop.getBlock().getDrops(world, cropPos, probableCrop.getState(), 0); - world.destroyBlock(cropPos, false); - retFlag = true; - } + if (registeredCrop == probableCrop) { + NonNullList blockDrops = NonNullList.create(); + probableCrop.getBlock().getDrops(blockDrops, world, cropPos, probableCrop, 0); + drops.addAll(blockDrops); + world.destroyBlock(cropPos, false); + return true; } } - if (!world.isRemote) { - for (ItemStack drop : drops) { - EntityItem item = new EntityItem(world, cropPos.getX(), cropPos.getY() + 0.5, cropPos.getZ(), drop); - world.spawnEntity(item); - } - } + return false; + } - return retFlag; + @Override + public boolean test(World world, BlockPos pos, IBlockState state) { + return HarvestRegistry.getStemCrops().containsKey(state); } } diff --git a/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerTall.java b/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerTall.java index a9f11698..544a3d83 100644 --- a/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerTall.java +++ b/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestHandlerTall.java @@ -1,49 +1,46 @@ package WayofTime.bloodmagic.ritual.harvest; -import WayofTime.bloodmagic.util.BlockStack; -import WayofTime.bloodmagic.iface.IHarvestHandler; -import WayofTime.bloodmagic.core.registry.HarvestRegistry; -import net.minecraft.entity.item.EntityItem; +import net.minecraft.block.BlockCactus; +import net.minecraft.block.BlockReed; +import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; +import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import java.util.ArrayList; import java.util.List; /** * Harvest handler for crops that grow vertically such as Sugar Cane and Cactus.
* Register a new crop for this handler with - * {@link HarvestRegistry#registerTallCrop(BlockStack)} + * {@link HarvestRegistry#registerTallCrop(IBlockState)} */ public class HarvestHandlerTall implements IHarvestHandler { + public HarvestHandlerTall() { - HarvestRegistry.registerTallCrop(new BlockStack(Blocks.REEDS)); - HarvestRegistry.registerTallCrop(new BlockStack(Blocks.CACTUS)); + for (int i = 0; i < 15; i++) { + HarvestRegistry.registerTallCrop(Blocks.REEDS.getDefaultState().withProperty(BlockReed.AGE, i)); + HarvestRegistry.registerTallCrop(Blocks.CACTUS.getDefaultState().withProperty(BlockCactus.AGE, i)); + } } @Override - public boolean harvestAndPlant(World world, BlockPos pos, BlockStack blockStack) { - boolean retFlag = false; - - List drops = new ArrayList<>(); - if (HarvestRegistry.getTallCrops().contains(blockStack)) { - BlockStack up = BlockStack.getStackFromPos(world, pos.up()); - if (up.equals(blockStack)) { - drops = up.getBlock().getDrops(world, pos.up(), up.getState(), 0); - world.destroyBlock(pos.up(), false); - retFlag = true; - } + public boolean harvest(World world, BlockPos pos, IBlockState state, List drops) { + IBlockState up = world.getBlockState(pos.up()); + if (up.getBlock() == state.getBlock()) { + NonNullList blockDrops = NonNullList.create(); + up.getBlock().getDrops(blockDrops, world, pos.up(), up, 0); + drops.addAll(blockDrops); + world.destroyBlock(pos.up(), false); + return true; } - if (!world.isRemote) { - for (ItemStack drop : drops) { - EntityItem item = new EntityItem(world, pos.getX(), pos.getY() + 0.5, pos.getZ(), drop); - world.spawnEntity(item); - } - } + return false; + } - return retFlag; + @Override + public boolean test(World world, BlockPos pos, IBlockState state) { + return HarvestRegistry.getTallCrops().contains(state); } } diff --git a/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestRegistry.java b/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestRegistry.java new file mode 100644 index 00000000..c5deba16 --- /dev/null +++ b/src/main/java/WayofTime/bloodmagic/ritual/harvest/HarvestRegistry.java @@ -0,0 +1,105 @@ +package WayofTime.bloodmagic.ritual.harvest; + +import com.google.common.collect.*; +import net.minecraft.block.Block; +import net.minecraft.block.BlockStem; +import net.minecraft.block.state.IBlockState; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class HarvestRegistry { + + private static final List HARVEST_HANDLERS = Lists.newArrayList(); + private static final Map STANDARD_CROPS = Maps.newHashMap(); + private static final Set TALL_CROPS = Sets.newHashSet(); + private static final Map STEM_CROPS = Maps.newHashMap(); + private static final Map AMPLIFIERS = Maps.newHashMap(); + + /** + * Registers a handler for the Harvest Ritual to call. + * + * @param handler - The custom handler to register + */ + public static void registerHandler(IHarvestHandler handler) { + if (!HARVEST_HANDLERS.contains(handler)) + HARVEST_HANDLERS.add(handler); + } + + /** + * Registers a standard crop (IE: Wheat, Carrots, Potatoes, Netherwart, etc) + * for the + * {@link WayofTime.bloodmagic.ritual.harvest.HarvestHandlerPlantable} + * handler to handle. + * + * @param crop - The crop block to handle. + * @param matureMeta - The meta value at which the crop is considered mature and ready + * to be harvested. + */ + public static void registerStandardCrop(Block crop, int matureMeta) { + if (!STANDARD_CROPS.containsKey(crop)) + STANDARD_CROPS.put(crop, matureMeta); + } + + /** + * Registers a tall crop (Sugar Cane and Cactus) for the + * {@link WayofTime.bloodmagic.ritual.harvest.HarvestHandlerTall} handler to + * handle. + * + * @param crop - The crop block to handle. + */ + public static void registerTallCrop(IBlockState crop) { + if (!TALL_CROPS.contains(crop)) + TALL_CROPS.add(crop); + } + + /** + * Registers a stem crop (Melon and Pumpkin) for the + * {@link WayofTime.bloodmagic.ritual.harvest.HarvestHandlerStem} handler to + * handle. + *

+ * Use {@link net.minecraftforge.oredict.OreDictionary#WILDCARD_VALUE} to + * accept any meta for the crop block. + *

+ * The Stem must be instanceof {@link BlockStem} + * + * @param crop - The crop block to handle. + * @param stem - The stem of the crop + */ + public static void registerStemCrop(IBlockState crop, IBlockState stem) { + if (!STEM_CROPS.containsKey(crop) && stem.getBlock() instanceof BlockStem) + STEM_CROPS.put(stem, crop); + } + + /** + * Registers a range amplifier for the Harvest Ritual. + * + * @param block - The block for the amplifier. + * @param range - The range the amplifier provides. + */ + public static void registerRangeAmplifier(IBlockState block, int range) { + if (!AMPLIFIERS.containsKey(block)) + AMPLIFIERS.put(block, range); + } + + public static List getHarvestHandlers() { + return ImmutableList.copyOf(HARVEST_HANDLERS); + } + + public static Map getStandardCrops() { + return ImmutableMap.copyOf(STANDARD_CROPS); + } + + public static Set getTallCrops() { + return ImmutableSet.copyOf(TALL_CROPS); + } + + public static Map getStemCrops() { + return ImmutableMap.copyOf(STEM_CROPS); + } + + public static Map getAmplifiers() { + return ImmutableMap.copyOf(AMPLIFIERS); + } +} diff --git a/src/main/java/WayofTime/bloodmagic/ritual/harvest/IHarvestHandler.java b/src/main/java/WayofTime/bloodmagic/ritual/harvest/IHarvestHandler.java new file mode 100644 index 00000000..b8b49316 --- /dev/null +++ b/src/main/java/WayofTime/bloodmagic/ritual/harvest/IHarvestHandler.java @@ -0,0 +1,37 @@ +package WayofTime.bloodmagic.ritual.harvest; + +import net.minecraft.block.state.IBlockState; +import net.minecraft.item.ItemStack; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +import java.util.List; + +/** + * Used to define a HarvestHandler for the Harvest Ritual. + */ +public interface IHarvestHandler { + + /** + * Called whenever the Harvest Ritual attempts to harvest a block.
+ * Use this to break the block and plant a new one.
+ * Add the items to be dropped to the drops list.
+ * + * @param world - The world + * @param pos - The position of the {@link IBlockState} being checked + * @param state - The {@link IBlockState} being checked + * @param drops - The items to be dropped + * @return If the block was successfully harvested. + */ + boolean harvest(World world, BlockPos pos, IBlockState state, List drops); + + /** + * Tests to see if the block is valid for harvest. + * + * @param world The world + * @param pos The position in the world of the {@link IBlockState} being checked + * @param state The {@link IBlockState} being checked + * @return if this block is valid for harvest. + */ + boolean test(World world, BlockPos pos, IBlockState state); +}