public void addLoader(Class<?> loaderClass) { try { for (Method method : loaderClass.getMethods()) for (Annotation annotation : method.getDeclaredAnnotations()) if (annotation.annotationType().equals(Load.class)) if (method.getParameterCount() == 0 || (FMLStateEvent.class.isAssignableFrom(method.getParameterTypes()[0]) && method.getParameterTypes()[0].equals(((Load) annotation).value().getEvent().getClass()))) { Collection<Method> methods = stateLoaderMap.getOrDefault(((Load) annotation).value(), new ArrayList<>()); if (!methods.contains(method)) methods.add(method); stateLoaderMap.put(((Load) annotation).value(), methods); } loaderInstanceMap.put(loaderClass, loaderClass.newInstance()); } catch (Exception e) { FoodCraftReloaded.getLogger().warn("Un-able to register loader " + loaderClass.getName(), e); } }
/** * Fires an {@link FMLStateEvent} and applies all patches associated with it * * @param event the {@link FMLStateEvent} */ public <T extends FMLStateEvent> void fireStateEvent(T event) { synchronized (this.stateEventBasedPatches) { Iterator<StateEventBasedPatch> itr = this.stateEventBasedPatches.get(event.getClass()).iterator(); while (itr.hasNext()) { StateEventBasedPatch patch = itr.next(); if (!patch.needsToBeApplied(event)) continue; if (patch.printLogFor(event)) { if (!patch.applyStateEventPatch(event)) handleApplyFailure(patch); else handlyApplySuccess(patch); } else patch.applyStateEventPatch(event); } } }
@Override public <T extends FMLStateEvent> boolean applyStateEventPatch(T e) { FMLServerAboutToStartEvent event = (FMLServerAboutToStartEvent) e; Field commandManager = ReflectionHelper.getField(ObfuscatedField.MinecraftServer_commandManager); if (commandManager != null) { try { commandManager.set(event.getServer(), new ServerCommandManager(event.getServer(), event.getServer().getCommandManager())); PatchManager.instance().getGlobalAppliedPatches().setPatchSuccessfullyApplied(this.displayName, true); return true; } catch (Exception ex) { PatchManager.instance().getGlobalAppliedPatches().setPatchSuccessfullyApplied(this.displayName, false); return false; } } else { PatchManager.instance().getGlobalAppliedPatches().setPatchSuccessfullyApplied(this.displayName, false); return false; } }
@Override public <T extends FMLStateEvent> boolean applyStateEventPatch(T event) { try { SimpleReloadableResourceManager resourceManager = (SimpleReloadableResourceManager) Minecraft.getMinecraft().getResourceManager(); Field reloadListenerList = ReflectionHelper.getField(ObfuscatedField.SimpleReloadableResourceManager_reloadListeners); List<?> reloadListeners = (List<?>) reloadListenerList.get(resourceManager); reloadListeners.remove(Minecraft.getMinecraft().renderGlobal); Minecraft.getMinecraft().renderGlobal = new RenderGlobal(Minecraft.getMinecraft()); resourceManager.registerReloadListener(Minecraft.getMinecraft().renderGlobal); PatchManager.instance().getGlobalAppliedPatches().setPatchSuccessfullyApplied(this.displayName, true); return true; } catch (Exception ex) { PatchManager.instance().getGlobalAppliedPatches().setPatchSuccessfullyApplied(this.displayName, false); return false; } }
@Override public <T extends FMLStateEvent> boolean applyStateEventPatch(T event) { Field instance = ReflectionHelper.getField(ObfuscatedField.ClientCommandHandler_instance); Field modifiers = ReflectionHelper.getField(ObfuscatedField.Field_modifiers); try { modifiers.setInt(instance, instance.getModifiers() & ~Modifier.FINAL); instance.set(null, new ClientCommandManager((ClientCommandHandler) instance.get(null))); PatchManager.instance().getGlobalAppliedPatches().setPatchSuccessfullyApplied(this.displayName, true); return true; } catch (Exception ex) { PatchManager.instance().getGlobalAppliedPatches().setPatchSuccessfullyApplied(this.displayName, false); return false; } }
private void sendEventToModContainer(FMLEvent stateEvent, ModContainer mc) { String modId = mc.getModId(); Collection<String> requirements = Collections2.transform(mc.getRequirements(),new ArtifactVersionNameFunction()); for (ArtifactVersion av : mc.getDependencies()) { if (av.getLabel()!= null && requirements.contains(av.getLabel()) && modStates.containsEntry(av.getLabel(),ModState.ERRORED)) { FMLLog.log(modId, Level.ERROR, "Skipping event %s and marking errored mod %s since required dependency %s has errored", stateEvent.getEventType(), modId, av.getLabel()); modStates.put(modId, ModState.ERRORED); return; } } activeContainer = mc; stateEvent.applyModContainer(activeContainer()); ThreadContext.put("mod", modId); FMLLog.log(modId, Level.TRACE, "Sending event %s to mod %s", stateEvent.getEventType(), modId); eventChannels.get(modId).post(stateEvent); FMLLog.log(modId, Level.TRACE, "Sent event %s to mod %s", stateEvent.getEventType(), modId); ThreadContext.remove("mod"); activeContainer = null; if (stateEvent instanceof FMLStateEvent) { if (!errors.containsKey(modId)) { modStates.put(modId, ((FMLStateEvent)stateEvent).getModState()); } else { modStates.put(modId, ModState.ERRORED); } } }
public void preInit(FMLPreInitializationEvent event) { CommonProxy.event = new WeakReference<FMLStateEvent>(event); IWTechConfig.preInit(); IWTechCapablilties.preInit(); IWTechItems.preInit(); IWTechBlocks.preInit(); IWTechCreativeTabs.preInit(); IWTechEntities.preInit(); IWTechTileEntities.preInit(); IWTechWorldGenerators.preInit(); }
@Override public <T extends FMLStateEvent> boolean applyStateEventPatch(T e) { FMLServerAboutToStartEvent event = (FMLServerAboutToStartEvent) e; try { Constructor<PlayerList> ctor; if (event.getServer().isDedicatedServer()) ctor = (Constructor<PlayerList>) (Constructor<?>) DedicatedPlayerList.class.getDeclaredConstructor(DedicatedServer.class); else ctor = (Constructor<PlayerList>) (Constructor<?>) IntegratedPlayerList.class.getDeclaredConstructor(IntegratedServer.class); ctor.setAccessible(true); if (event.getServer().isDedicatedServer()) event.getServer().setPlayerList(ctor.newInstance((DedicatedServer) event.getServer())); else event.getServer().setPlayerList(ctor.newInstance((IntegratedServer) event.getServer())); PatchManager.instance().getGlobalAppliedPatches().setPatchSuccessfullyApplied(this.displayName, true); return true; } catch (Exception ex) { PatchManager.instance().getGlobalAppliedPatches().setPatchSuccessfullyApplied(this.displayName, false); return false; } }
@Override public boolean applyStateEventPatch(FMLStateEvent event) { if (event instanceof FMLPostInitializationEvent) return applyPostInitPatches((FMLPostInitializationEvent) event); else if (event instanceof FMLServerAboutToStartEvent) return applyServerStartPatches((FMLServerAboutToStartEvent) event); else throw new IllegalArgumentException("Invalid Event"); }
public static <T extends FMLStateEvent> T event() { return CommonProxy.event(); }
public void init(FMLInitializationEvent event) { CommonProxy.event = new WeakReference<FMLStateEvent>(event); IWTechCrafting.init(); IWTechGuiElements.init(); }
public void postInit(FMLPostInitializationEvent event) { CommonProxy.event = new WeakReference<FMLStateEvent>(event); }
public static <T extends FMLStateEvent> T event() { @SuppressWarnings("unchecked") T result = (T) CommonProxy.event.get(); return result; }
@Override public Collection<Class<? extends FMLStateEvent>> stateEventClasses() { return Sets.<Class<? extends FMLStateEvent>>newHashSet(FMLServerAboutToStartEvent.class); }
@Override public <T extends FMLStateEvent> boolean needsToBeApplied(T event) { return true; }
@Override public <T extends FMLStateEvent> boolean printLogFor(T event) { return true; }
@Override public Collection<Class<? extends FMLStateEvent>> stateEventClasses() { return Sets.<Class<? extends FMLStateEvent>>newHashSet(FMLInitializationEvent.class); }
@Override public Collection<Class<? extends FMLStateEvent>> stateEventClasses() { return Sets.<Class<? extends FMLStateEvent>>newHashSet(FMLPostInitializationEvent.class); }
@Override public <T extends FMLStateEvent> boolean needsToBeApplied(T event) { return event instanceof FMLServerAboutToStartEvent ? !((FMLServerAboutToStartEvent) event).getServer().isDedicatedServer() : true; }
@Override public <T extends FMLStateEvent> boolean printLogFor(T event) { return !(event instanceof FMLPostInitializationEvent); }
@Override public Collection<Class<? extends FMLStateEvent>> stateEventClasses() { return Sets.<Class<? extends FMLStateEvent>>newHashSet(FMLPostInitializationEvent.class, FMLServerAboutToStartEvent.class); }
public void onStateEvent(FMLStateEvent event) { PatchManager.instance().fireStateEvent(event); }
/** * @return the {@link FMLStateEvent} subclasses for which the apply method if this patch should be invoked */ Collection<Class<? extends FMLStateEvent>> stateEventClasses();
/** * Invoked to apply the patch * * @param event an event of one of the types of {@link #stateEventClasses()} * @return whether the patch could successfully be applied */ <T extends FMLStateEvent> boolean applyStateEventPatch(T event);
/** * Returns whether this patch needs to be applied. * * @param event the {@link FMLStateEvent} which would be used to invoke the apply method * @return whether this patch needs to be applied. */ <T extends FMLStateEvent> boolean needsToBeApplied(T event);
/** * Returns whether the PatchManager should automatically print a log message * * @param event the {@link FMLStateEvent} which would be used to invoke the apply method * @return whether to print a log message */ <T extends FMLStateEvent> boolean printLogFor(T event);