2021-12-17 15:14:52 -06:00
|
|
|
package noppes.npcs;
|
|
|
|
|
|
|
|
|
|
import com.google.common.reflect.ClassPath;
|
|
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
|
|
|
|
import cpw.mods.fml.common.Loader;
|
|
|
|
|
import cpw.mods.fml.common.ModContainer;
|
|
|
|
|
import cpw.mods.fml.common.eventhandler.Event;
|
|
|
|
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
|
|
|
|
import cpw.mods.fml.common.gameevent.PlayerEvent;
|
|
|
|
|
import cpw.mods.fml.common.gameevent.TickEvent;
|
|
|
|
|
import cpw.mods.fml.common.network.FMLNetworkEvent;
|
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
|
import net.minecraft.world.WorldServer;
|
|
|
|
|
import net.minecraftforge.event.entity.EntityEvent;
|
|
|
|
|
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
|
|
|
|
|
import net.minecraftforge.event.world.ChunkDataEvent;
|
|
|
|
|
import net.minecraftforge.event.world.ChunkEvent;
|
|
|
|
|
import net.minecraftforge.event.world.ChunkWatchEvent;
|
|
|
|
|
import net.minecraftforge.event.world.WorldEvent;
|
2023-07-19 11:17:11 -04:00
|
|
|
import noppes.npcs.compat.PixelmonHelper;
|
2021-12-17 15:14:52 -06:00
|
|
|
import noppes.npcs.controllers.ScriptController;
|
|
|
|
|
import noppes.npcs.scripted.event.ForgeEvent;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.lang.reflect.Modifier;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.Vector;
|
|
|
|
|
|
|
|
|
|
public class ScriptForgeEventHandler {
|
|
|
|
|
public ScriptForgeEventHandler() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
|
public void forgeEntity(Event event) {
|
2025-03-13 17:14:45 -04:00
|
|
|
if (CustomNpcs.getServer() != null && ScriptController.Instance.forgeScripts.isEnabled()) {
|
|
|
|
|
if (event instanceof TickEvent && !(((TickEvent) event).side == Side.SERVER && ((TickEvent) event).phase == TickEvent.Phase.START)) {
|
2021-12-17 15:14:52 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (event instanceof EntityEvent) {
|
|
|
|
|
EntityEvent ev2 = (EntityEvent) event;
|
|
|
|
|
if (ev2.entity != null && ev2.entity.worldObj instanceof WorldServer) {
|
2021-12-17 15:14:52 -06:00
|
|
|
EventHooks.onForgeEntityEvent(ev2);
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
} else if (event instanceof WorldEvent) {
|
|
|
|
|
WorldEvent ev1 = (WorldEvent) event;
|
|
|
|
|
if (ev1.world instanceof WorldServer) {
|
2021-12-17 15:14:52 -06:00
|
|
|
EventHooks.onForgeWorldEvent(ev1);
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
} else if (!(event instanceof TickEvent) || ((TickEvent) event).side != Side.CLIENT) {
|
2021-12-17 15:14:52 -06:00
|
|
|
if (event instanceof PlayerEvent) {
|
|
|
|
|
PlayerEvent ev = (PlayerEvent) event;
|
2022-01-15 22:52:27 -06:00
|
|
|
if (ev.player == null || !(ev.player.worldObj instanceof WorldServer)) {
|
2021-12-17 15:14:52 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EventHooks.onForgeEvent(new ForgeEvent(event), event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ScriptForgeEventHandler registerForgeEvents() {
|
|
|
|
|
try {
|
|
|
|
|
Method e = this.getClass().getMethod("forgeEntity", new Class[]{Event.class});
|
|
|
|
|
Method register = FMLCommonHandler.instance().bus().getClass().getDeclaredMethod("register", new Class[]{Class.class, Object.class, Method.class, ModContainer.class});
|
|
|
|
|
register.setAccessible(true);
|
|
|
|
|
ArrayList list = new ArrayList();
|
|
|
|
|
list.addAll(ClassPath.from(this.getClass().getClassLoader()).getTopLevelClassesRecursive("cpw.mods.fml.common.gameevent"));
|
|
|
|
|
list.addAll(ClassPath.from(this.getClass().getClassLoader()).getTopLevelClassesRecursive("net.minecraftforge.event"));
|
|
|
|
|
list.removeAll(ClassPath.from(this.getClass().getClassLoader()).getTopLevelClassesRecursive("net.minecraftforge.event.terraingen"));
|
|
|
|
|
Iterator e1 = list.iterator();
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
while (true) {
|
2021-12-17 15:14:52 -06:00
|
|
|
ClassPath.ClassInfo classLoader;
|
|
|
|
|
String classes;
|
|
|
|
|
do {
|
2025-03-13 17:14:45 -04:00
|
|
|
if (!e1.hasNext()) {
|
|
|
|
|
if (PixelmonHelper.Enabled) {
|
2021-12-17 15:14:52 -06:00
|
|
|
try {
|
|
|
|
|
Field e2 = ClassLoader.class.getDeclaredField("classes");
|
|
|
|
|
e2.setAccessible(true);
|
|
|
|
|
ClassLoader classLoader1 = Thread.currentThread().getContextClassLoader();
|
2025-03-13 17:14:45 -04:00
|
|
|
ArrayList classes1 = new ArrayList((Vector) e2.get(classLoader1));
|
2021-12-17 15:14:52 -06:00
|
|
|
Iterator infoClass1 = classes1.iterator();
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
while (infoClass1.hasNext()) {
|
|
|
|
|
Class c2 = (Class) infoClass1.next();
|
2021-12-17 15:14:52 -06:00
|
|
|
}
|
|
|
|
|
} catch (Exception var12) {
|
|
|
|
|
var12.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
classLoader = (ClassPath.ClassInfo) e1.next();
|
2021-12-17 15:14:52 -06:00
|
|
|
classes = classLoader.getName();
|
2025-03-13 17:14:45 -04:00
|
|
|
} while (classes.startsWith("net.minecraftforge.event.terraingen"));
|
2021-12-17 15:14:52 -06:00
|
|
|
|
|
|
|
|
Class infoClass = classLoader.load();
|
|
|
|
|
ArrayList c = new ArrayList(Arrays.asList(infoClass.getDeclaredClasses()));
|
2025-03-13 17:14:45 -04:00
|
|
|
if (c.isEmpty()) {
|
2021-12-17 15:14:52 -06:00
|
|
|
c.add(infoClass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Iterator var10 = c.iterator();
|
2025-03-13 17:14:45 -04:00
|
|
|
while (var10.hasNext()) {
|
|
|
|
|
Class c1 = (Class) var10.next();
|
|
|
|
|
if (!EntityEvent.EntityConstructing.class.isAssignableFrom(c1) && !WorldEvent.PotentialSpawns.class.isAssignableFrom(c1) && !TickEvent.RenderTickEvent.class.isAssignableFrom(c1) && !TickEvent.ClientTickEvent.class.isAssignableFrom(c1) && !FMLNetworkEvent.ClientCustomPacketEvent.class.isAssignableFrom(c1) && !ItemTooltipEvent.class.isAssignableFrom(c1) && Event.class.isAssignableFrom(c1) && !Modifier.isAbstract(c1.getModifiers()) && Modifier.isPublic(c1.getModifiers()) && !ChunkEvent.class.isAssignableFrom(c1) && !ChunkWatchEvent.class.isAssignableFrom(c1) && !ChunkDataEvent.class.isAssignableFrom(c1)) {
|
2021-12-17 15:14:52 -06:00
|
|
|
register.invoke(FMLCommonHandler.instance().bus(), new Object[]{c1, this, e, Loader.instance().activeModContainer()});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception var13) {
|
|
|
|
|
var13.printStackTrace();
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
}
|