|
| 1 | +package squeek.applecore.example; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | +import net.minecraft.block.BlockCrops; |
| 5 | +import net.minecraft.world.World; |
| 6 | +import squeek.applecore.api.AppleCoreAPI; |
| 7 | +import cpw.mods.fml.common.Loader; |
| 8 | +import cpw.mods.fml.common.Optional; |
| 9 | +import cpw.mods.fml.common.eventhandler.Event; |
| 10 | +import cpw.mods.fml.common.eventhandler.Event.Result; |
| 11 | + |
| 12 | +public class BlockCropsExample extends BlockCrops |
| 13 | +{ |
| 14 | + // abstract the AppleCoreAPI reference into an Optional.Method so that AppleCore is not a hard dependency |
| 15 | + @Optional.Method(modid = "AppleCore") |
| 16 | + public Event.Result validateAppleCoreGrowthTick(World world, int x, int y, int z, Random random) |
| 17 | + { |
| 18 | + return AppleCoreAPI.dispatcher.validatePlantGrowth(this, world, x, y, z, random); |
| 19 | + } |
| 20 | + |
| 21 | + // abstract the AppleCoreAPI reference into an Optional.Method so that AppleCore is not a hard dependency |
| 22 | + @Optional.Method(modid = "AppleCore") |
| 23 | + public void announceAppleCoreGrowthTick(World world, int x, int y, int z) |
| 24 | + { |
| 25 | + AppleCoreAPI.dispatcher.announcePlantGrowth(this, world, x, y, z); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * A custom updateTick implementation (that doesn't call super.updateTick) |
| 30 | + * will not fire the AppleCore events that get inserted into BlockCrops.updateTick, |
| 31 | + * which means that the custom implementation will need to fire and handle them |
| 32 | + */ |
| 33 | + @Override |
| 34 | + public void updateTick(World world, int x, int y, int z, Random random) |
| 35 | + { |
| 36 | + // get the result of the event |
| 37 | + Event.Result allowGrowthResult = Event.Result.DEFAULT; |
| 38 | + if (Loader.isModLoaded("AppleCore")) |
| 39 | + { |
| 40 | + allowGrowthResult = validateAppleCoreGrowthTick(world, x, y, z, random); |
| 41 | + } |
| 42 | + |
| 43 | + // note: you could return early here if plant growth is the only thing |
| 44 | + // that this method does. This would allow for skipping the allowGrowthResult == Event.Result.DEFAULT |
| 45 | + // check in the template below, changing it to: allowGrowthResult == Result.ALLOW || (DEFAULT CONDITIONS) |
| 46 | + if (allowGrowthResult == Event.Result.DENY) |
| 47 | + return; |
| 48 | + |
| 49 | + // for each growth requirement/conditional in updateTick, the following template should be applied: |
| 50 | + // allowGrowthResult == Result.ALLOW || (allowGrowthResult == Result.DEFAULT && (DEFAULT CONDITIONS)) |
| 51 | + // this follows the PlantGrowthEvent.AllowGrowthTick specifications: |
| 52 | + // - Event.Result.ALLOW means to always allow the growth tick without condition |
| 53 | + // - Event.Result.DEFAULT means to continue with the conditionals as normal |
| 54 | + // - Event.Result.DENY means to always disallow the growth tick |
| 55 | + if (allowGrowthResult == Event.Result.ALLOW || (allowGrowthResult == Event.Result.DEFAULT && world.getBlockLightValue(x, y + 1, z) >= 9)) |
| 56 | + { |
| 57 | + int meta = world.getBlockMetadata(x, y, z); |
| 58 | + |
| 59 | + if (meta < 7) |
| 60 | + { |
| 61 | + // same template as above is applied here, but the default conditional is |
| 62 | + // moved into a variable for clarity |
| 63 | + boolean defaultGrowthCondition = random.nextInt(50) == 0; |
| 64 | + if (allowGrowthResult == Result.ALLOW || (allowGrowthResult == Result.DEFAULT && defaultGrowthCondition)) |
| 65 | + { |
| 66 | + ++meta; |
| 67 | + world.setBlockMetadataWithNotify(x, y, z, meta, 2); |
| 68 | + |
| 69 | + // *After* the actual growth occurs, we simply let everyone know that it happened |
| 70 | + if (Loader.isModLoaded("AppleCore")) |
| 71 | + { |
| 72 | + announceAppleCoreGrowthTick(world, x, y, z); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments