@@ -22,15 +22,24 @@ public class CropMod implements WurmMod, Configurable {
2222 private Logger logger = Logger .getLogger (this .getClass ().getName ());
2323
2424
25+ //
26+ // The method configure is called when the mod is being loaded
27+ //
2528 @ Override
2629 public void configure (Properties properties ) {
2730
2831 disableWeeds = Boolean .valueOf (properties .getProperty ("disableWeeds" , Boolean .toString (disableWeeds )));
29-
3032 logger .log (Level .INFO , "disableWeeds: " + disableWeeds );
3133
34+ //
35+ // We initialize a method hook that gets called right before CropTilePoller.checkForFarmGrowth is called
36+ //
3237 if (disableWeeds ) {
3338 try {
39+
40+ //
41+ // To make sure we hook the correct method the list of method parameter types is compiled
42+ //
3443 CtClass [] paramTypes = {
3544 CtPrimitiveType .intType ,
3645 CtPrimitiveType .intType ,
@@ -41,16 +50,35 @@ public void configure(Properties properties) {
4150 CtPrimitiveType .booleanType
4251 };
4352
53+ //
54+ // next we register the hook for
55+ // com.wurmonline.server.zones.CropTilePoller.checkForFarmGrowth(int, int, int, byte, byte, MeshIO, boolean)
56+ //
4457 HookManager .getInstance ().registerHook ("com.wurmonline.server.zones.CropTilePoller" , "checkForFarmGrowth" , Descriptor .ofMethod (CtPrimitiveType .voidType , paramTypes ), new InvocationHandler () {
4558
59+ //
60+ // The actual hook is an InvocationHandler. It's invoke method is called instead of the hooked method.
61+ // The object, method and arguments are passed as parameters to invoke()
62+ //
4663 @ Override
4764 public Object invoke (Object object , Method method , Object [] args ) throws Throwable {
65+ //
66+ // When the hook is called we can do stuff depending on the input parameters
67+ // Here we check if the tileAge is 6 (the second ripe stage)
68+ //
4869 byte aData = ((Number )args [4 ]).byteValue ();
4970 final int tileState = aData >> 4 ;
5071 int tileAge = tileState & 0x7 ;
51- if (tileAge == 6 )
72+ if (tileAge == 6 ) {
73+ // tileAge is 6. Advancing it further would create weeds.
74+ // Therefor we just exit here.
75+ // return null is required if the hooked method has a void return type
5276 return null ;
77+ }
5378
79+ //
80+ // tileAge is not 6. We just continue by calling the hooked method
81+ //
5482 return method .invoke (object , args );
5583 }
5684 });
0 commit comments