Skip to content

Commit f5270a6

Browse files
committed
Add a way for mods to be able to integrate their plant growth with AppleCore
* API: Add IAppleCoreDispatcher interface and AppleCoreAPI.dispatcher to access it + Used for dispatching AppleCore events (for now, just the plant growth ones) * Add BlockCropsExample to example mod
1 parent be66e07 commit f5270a6

File tree

8 files changed

+149
-10
lines changed

8 files changed

+149
-10
lines changed

TODO

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
General:
22

33
API:
4-
[ ] Add a way for mods to be able to integrate their plant growth with AppleCore
54

65
Core/ASM:
76
Plant growth modification:
@@ -68,3 +67,4 @@ Archive:
6867
[x] Implement event-driven starvation @done (2014-07-24 01:38) @project(API)
6968
[x] Implement event-driven configurable health regen @done (2014-07-24 01:38) @project(API)
7069
[x] Implement event-driven configurable exhaustion @done (2014-07-24 01:38) @project(API)
70+
[x] Add a way for mods to be able to integrate their plant growth with AppleCore @done (15-01-27 14:50) @project(API)

java/squeek/applecore/AppleCore.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.apache.logging.log4j.LogManager;
66
import org.apache.logging.log4j.Logger;
77
import squeek.applecore.api_impl.AppleCoreAccessorMutatorImpl;
8+
import squeek.applecore.api_impl.AppleCoreDispatcherImpl;
89
import squeek.applecore.asm.TransformerModuleHandler;
910
import squeek.applecore.client.DebugInfoHandler;
1011
import squeek.applecore.client.HUDOverlayHandler;
@@ -40,8 +41,9 @@ public void preInit(FMLPreInitializationEvent event)
4041
MetadataCollection metadataCollection = MetadataCollection.from(is, ModInfo.MODID);
4142
Loader.instance().activeModContainer().bindMetadata(metadataCollection);
4243

43-
// force initialization of the singleton
44+
// force initialization of the singletons
4445
AppleCoreAccessorMutatorImpl.values();
46+
AppleCoreDispatcherImpl.values();
4547

4648
ModConfig.init(event.getSuggestedConfigurationFile());
4749

java/squeek/applecore/api/AppleCoreAPI.java

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public abstract class AppleCoreAPI
1010
{
1111
public static IAppleCoreAccessor accessor;
1212
public static IAppleCoreMutator mutator;
13+
public static IAppleCoreDispatcher dispatcher;
1314
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package squeek.applecore.api;
2+
3+
import java.util.Random;
4+
import net.minecraft.block.Block;
5+
import net.minecraft.world.World;
6+
import squeek.applecore.api.plants.PlantGrowthEvent;
7+
import cpw.mods.fml.common.eventhandler.Event;
8+
9+
public interface IAppleCoreDispatcher
10+
{
11+
/**
12+
* Fires a {@link PlantGrowthEvent.AllowGrowthTick} event and returns the result.
13+
*
14+
* See {@link PlantGrowthEvent.AllowGrowthTick} for how to use the result.
15+
*/
16+
public Event.Result validatePlantGrowth(Block block, World world, int x, int y, int z, Random random);
17+
18+
/**
19+
* Fires a {@link PlantGrowthEvent.GrowthTick} event.
20+
*/
21+
public void announcePlantGrowth(Block block, World world, int x, int y, int z);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package squeek.applecore.api_impl;
2+
3+
import java.util.Random;
4+
import net.minecraft.block.Block;
5+
import net.minecraft.world.World;
6+
import net.minecraftforge.common.MinecraftForge;
7+
import squeek.applecore.api.AppleCoreAPI;
8+
import squeek.applecore.api.IAppleCoreDispatcher;
9+
import squeek.applecore.api.plants.PlantGrowthEvent;
10+
import cpw.mods.fml.common.eventhandler.Event;
11+
12+
public enum AppleCoreDispatcherImpl implements IAppleCoreDispatcher
13+
{
14+
INSTANCE;
15+
16+
private AppleCoreDispatcherImpl()
17+
{
18+
AppleCoreAPI.dispatcher = this;
19+
}
20+
21+
@Override
22+
public Event.Result validatePlantGrowth(Block block, World world, int x, int y, int z, Random random)
23+
{
24+
PlantGrowthEvent.AllowGrowthTick event = new PlantGrowthEvent.AllowGrowthTick(block, world, x, y, z, random);
25+
MinecraftForge.EVENT_BUS.post(event);
26+
return event.getResult();
27+
}
28+
29+
@Override
30+
public void announcePlantGrowth(Block block, World world, int x, int y, int z)
31+
{
32+
PlantGrowthEvent.GrowthTick event = new PlantGrowthEvent.GrowthTick(block, world, x, y, z);
33+
MinecraftForge.EVENT_BUS.post(event);
34+
}
35+
}

java/squeek/applecore/asm/Hooks.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import squeek.applecore.api.hunger.ExhaustionEvent;
1818
import squeek.applecore.api.hunger.HealthRegenEvent;
1919
import squeek.applecore.api.hunger.StarvationEvent;
20-
import squeek.applecore.api.plants.PlantGrowthEvent;
2120
import cpw.mods.fml.common.eventhandler.Event.Result;
2221

2322
public class Hooks
@@ -31,7 +30,7 @@ public static void onPostFoodStatsAdded(FoodStats foodStats, ItemFood itemFood,
3130
{
3231
MinecraftForge.EVENT_BUS.post(new FoodEvent.FoodEaten(player, itemStack, foodValues, hungerAdded, saturationAdded));
3332
}
34-
33+
3534
public static int getItemInUseMaxDuration(EntityPlayer player, int savedMaxDuration)
3635
{
3736
EnumAction useAction = player.getItemInUse().getItemUseAction();
@@ -143,15 +142,12 @@ public static boolean fireFoodStatsAdditionEvent(EntityPlayer player, FoodValues
143142

144143
public static Result fireAllowPlantGrowthEvent(Block block, World world, int x, int y, int z, Random random)
145144
{
146-
PlantGrowthEvent.AllowGrowthTick event = new PlantGrowthEvent.AllowGrowthTick(block, world, x, y, z, random);
147-
MinecraftForge.EVENT_BUS.post(event);
148-
return event.getResult();
145+
return AppleCoreAPI.dispatcher.validatePlantGrowth(block, world, x, y, z, random);
149146
}
150147

151148
public static void fireOnGrowthEvent(Block block, World world, int x, int y, int z)
152149
{
153-
PlantGrowthEvent.GrowthTick event = new PlantGrowthEvent.GrowthTick(block, world, x, y, z);
154-
MinecraftForge.EVENT_BUS.post(event);
150+
AppleCoreAPI.dispatcher.announcePlantGrowth(block, world, x, y, z);
155151
}
156152

157153
public static int toolTipX, toolTipY, toolTipW, toolTipH;

java/squeek/applecore/example/AppleCoreExample.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package squeek.applecore.example;
22

3+
import net.minecraft.block.Block;
34
import net.minecraft.item.Item;
45
import net.minecraftforge.common.MinecraftForge;
56
import org.apache.logging.log4j.LogManager;
@@ -26,15 +27,19 @@ public class AppleCoreExample
2627

2728
public static Item testFood;
2829
public static Item testMetadataFood;
30+
public static Block testBlockCrops;
2931

3032
@EventHandler
3133
public void preInit(FMLPreInitializationEvent event)
3234
{
3335
testFood = new ItemNonStandardFood().setUnlocalizedName("testNonStandardFood");
3436
GameRegistry.registerItem(testFood, "testNonStandardFood");
3537

36-
testMetadataFood = new ItemMetadataFood(new int[] {1, 10}, new float[] {2f, 0.1f}).setUnlocalizedName("testMetadataFood");
38+
testMetadataFood = new ItemMetadataFood(new int[]{1, 10}, new float[]{2f, 0.1f}).setUnlocalizedName("testMetadataFood");
3739
GameRegistry.registerItem(testMetadataFood, "testMetadataFood");
40+
41+
testBlockCrops = new BlockCropsExample();
42+
GameRegistry.registerBlock(testBlockCrops, "testBlockCrops");
3843
}
3944

4045
@EventHandler
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)