Skip to content

Commit

Permalink
Move away from EntityJoinWorldEvent as there are possible issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
tterrag1098 committed Jul 2, 2014
1 parent 2a9acab commit dc9bd97
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/main/java/tterrag/rtc/RecipeAddition.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface RecipeAddition
{
public enum EventTime { INIT, POST_INIT, PLAYER_JOIN }
public enum EventTime { INIT, POST_INIT, WORLD_LOAD }

/**
* The {@link EventTime} at which this method is to be executed
Expand Down
100 changes: 60 additions & 40 deletions src/main/java/tterrag/rtc/RecipeTweakingCore.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package tterrag.rtc;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

import net.minecraftforge.common.MinecraftForge;
import tterrag.rtc.RecipeAddition.EventTime;
Expand All @@ -30,21 +30,19 @@ public class RecipeTweakingCore

static boolean donePlayerJoinTweaks = false;

public static Logger logger = Logger.getLogger("RecipeTweakingCore");

@EventHandler
public void init(FMLInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(new TooltipHandler());

doTweaks(EventTime.INIT);
}

@EventHandler
public void postInit(FMLPostInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(new TweakingRegistry());

doTweaks(EventTime.POST_INIT);
}

Expand All @@ -60,69 +58,81 @@ public static void registerPackageName(String packageName)

static void doTweaks(EventTime event)
{
logger.info("Doing tweaks at time: " + event.toString());
log("Doing tweaks at time: " + event.toString());

for (String packageName : packageNames)
{
removeRecipes(event, packageName);
addRecipes(event, packageName);
try
{
log("Processing package: " + packageName);
removeRecipes(event, packageName);
addRecipes(event, packageName);
}
catch (IOException e)
{
logErr("Error processing package: " + packageName);
throw new RuntimeException(e);
}
}
}

private static void removeRecipes(EventTime event, String packageName)
private static void removeRecipes(EventTime event, String packageName) throws IOException
{
try
{
ClassPath classpath = ClassPath.from(RecipeTweakingCore.class.getClassLoader());
Set<ClassInfo> classes = classpath.getTopLevelClasses(packageName);
ClassPath classpath = ClassPath.from(RecipeTweakingCore.class.getClassLoader());
Set<ClassInfo> classes = classpath.getTopLevelClasses(packageName);

for (ClassInfo c : classes)
for (ClassInfo c : classes)
{
for (Method m : loadClassSafe(c))
{
for (Method m : loadClassSafe(c))
RecipeRemoval r = m.getAnnotation(RecipeRemoval.class);
if (r != null && allModsLoaded(r.requiredModids()) && r.time() == event)
{
RecipeRemoval r = m.getAnnotation(RecipeRemoval.class);
if (r != null && allModsLoaded(r.requiredModids()) && r.time() == event)
try
{
log("Processing remove method \"" + m.getName() + "\" in class \"" + c.getName() + "\"");
m.invoke(null, new Object[] {});
}
catch (Throwable t)
{
logErr("[Removals] An exception was thrown processing \"" + m.getName() + "\" in class \"" + c.getName() + "\"" + "these removals will likely not occur.");
t.printStackTrace();
}
}
}
}
catch (Throwable t)
{
t.printStackTrace();
logger.severe("[Removals] An exception was thrown processing a class, these removals will likely not occur.");
}

TweakingRegistry.removeRecipes();
}

private static void addRecipes(EventTime event, String packageName)
private static void addRecipes(EventTime event, String packageName) throws IOException
{
try
{
ClassPath classpath = ClassPath.from(RecipeTweakingCore.class.getClassLoader());
Set<ClassInfo> classes = classpath.getTopLevelClasses(packageName);

for (ClassInfo c : classes)
ClassPath classpath = ClassPath.from(RecipeTweakingCore.class.getClassLoader());
Set<ClassInfo> classes = classpath.getTopLevelClasses(packageName);

for (ClassInfo c : classes)
{
for (Method m : loadClassSafe(c))
{
for (Method m : loadClassSafe(c))
RecipeAddition r = m.getAnnotation(RecipeAddition.class);
if (r != null && allModsLoaded(r.requiredModids()) && r.time() == event)
{
RecipeAddition r = m.getAnnotation(RecipeAddition.class);
if (r != null && allModsLoaded(r.requiredModids()) && r.time() == event)
try
{
log("Processing add method \"" + m.getName() + "\" in class \"" + c.getName() + "\"");
m.invoke(null, new Object[] {});
}
catch (Throwable t)
{
logErr("[Additions] An exception was thrown processing \"" + m.getName() + "\" in class \"" + c.getName() + "\"" + "these additions will likely not occur.");
t.printStackTrace();
}
}
}
}
catch (Throwable t)
{
t.printStackTrace();
logger.severe("[Additions] An exception was thrown processing a class, these additons will likely not occur.");
}
}

private static Method[] loadClassSafe(ClassInfo c)
{
try
Expand All @@ -132,8 +142,8 @@ private static Method[] loadClassSafe(ClassInfo c)
}
catch (Throwable t)
{
logger.info(String.format("Class %s threw an error, skipping...", c.getName()));
return new Method[]{};
logErr(String.format("Class %s threw an error on load, skipping...", c.getName()));
return new Method[] {};
}
}

Expand All @@ -146,4 +156,14 @@ private static boolean allModsLoaded(String[] modids)
}
return true;
}

public static void log(String msg)
{
System.out.println("[RTC] " + msg);
}

public static void logErr(String msg)
{
System.err.println("[RTC] " + msg);
}
}
6 changes: 3 additions & 3 deletions src/main/java/tterrag/rtc/TweakingRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.world.WorldEvent;
import tterrag.rtc.RecipeAddition.EventTime;

public class TweakingRegistry
Expand Down Expand Up @@ -146,11 +146,11 @@ else if (contains(id, -1))
}

@ForgeSubscribe
public void onPlayerJoin(EntityJoinWorldEvent event)
public void onWorldLoad(WorldEvent.Load event)
{
if (!RecipeTweakingCore.donePlayerJoinTweaks)
{
RecipeTweakingCore.doTweaks(EventTime.PLAYER_JOIN);
RecipeTweakingCore.doTweaks(EventTime.WORLD_LOAD);
RecipeTweakingCore.donePlayerJoinTweaks = true;
}
}
Expand Down

0 comments on commit dc9bd97

Please sign in to comment.