diff --git a/src/main/java/noppes/npcs/CustomNpcs.java b/src/main/java/noppes/npcs/CustomNpcs.java index 2d7a19a05..7b19fc433 100644 --- a/src/main/java/noppes/npcs/CustomNpcs.java +++ b/src/main/java/noppes/npcs/CustomNpcs.java @@ -59,6 +59,7 @@ import noppes.npcs.entity.old.EntityNpcMonsterMale; import noppes.npcs.entity.old.EntityNpcNagaFemale; import noppes.npcs.entity.old.EntityNpcNagaMale; import noppes.npcs.entity.old.EntityNpcSkeleton; +import noppes.npcs.scripted.wrapper.WrapperNpcAPI; @Mod(modid = "customnpcs", name = "CustomNpcs", version = "1.2.1") public class CustomNpcs { @@ -173,8 +174,10 @@ public class CustomNpcs { NetworkRegistry.INSTANCE.registerGuiHandler(this, proxy); MinecraftForge.EVENT_BUS.register(new ServerEventsHandler()); + //MinecraftForge.EVENT_BUS.register((new ScriptPlayerEventHandler()).registerForgeEvents()); MinecraftForge.EVENT_BUS.register(new ScriptController()); + FMLCommonHandler.instance().bus().register(new ScriptPlayerEventHandler()); FMLCommonHandler.instance().bus().register(new ServerTickHandler()); registerNpc(EntityNPCHumanMale.class, "npchumanmale"); @@ -244,7 +247,10 @@ public class CustomNpcs { new SpawnController(); new LinkedNpcController(); ScriptController.Instance.loadStoredData(); + ScriptController.Instance.loadForgeScripts(); + ScriptController.Instance.loadPlayerScripts(); ScriptController.HasStart = false; + WrapperNpcAPI.clearCache(); Set names = Block.blockRegistry.getKeys(); for(String name : names){ diff --git a/src/main/java/noppes/npcs/DataScript.java b/src/main/java/noppes/npcs/DataScript.java index d36b0be7c..bea4219bb 100644 --- a/src/main/java/noppes/npcs/DataScript.java +++ b/src/main/java/noppes/npcs/DataScript.java @@ -111,13 +111,29 @@ public class DataScript { script.engine.put(obs[i].toString(), ob); } - return callScript(script); + //return callScript(script); + ScriptEngine engine = script.engine; + engine.put("npc", dummyNpc); + engine.put("world", dummyWorld); + ScriptEvent result = (ScriptEvent) engine.get("event"); + if(result == null) + engine.put("event", result = new ScriptEvent()); + engine.put("EntityType", entities); + engine.put("RoleType", roles); + engine.put("JobType", jobs); + script.run(engine); + + if(clientNeedsUpdate){ + npc.updateClient = true; + clientNeedsUpdate = false; + } + if(aiNeedsUpdate){ + npc.updateAI = true; + aiNeedsUpdate = false; + } + return result.isCancelled(); } - - public boolean isEnabled(){ - return enabled && ScriptController.HasStart && !npc.worldObj.isRemote && !scripts.isEmpty(); - } - + /* private boolean callScript(ScriptContainer script){ ScriptEngine engine = script.engine; engine.put("npc", dummyNpc); @@ -129,7 +145,7 @@ public class DataScript { engine.put("RoleType", roles); engine.put("JobType", jobs); script.run(engine); - + if(clientNeedsUpdate){ npc.updateClient = true; clientNeedsUpdate = false; @@ -140,6 +156,11 @@ public class DataScript { } return result.isCancelled(); } + */ + + public boolean isEnabled(){ + return enabled && ScriptController.HasStart && !npc.worldObj.isRemote && !scripts.isEmpty(); + } public void setWorld(World world) { if(world instanceof WorldServer) diff --git a/src/main/java/noppes/npcs/EventDataScript.java b/src/main/java/noppes/npcs/EventDataScript.java deleted file mode 100644 index 486dc9ec2..000000000 --- a/src/main/java/noppes/npcs/EventDataScript.java +++ /dev/null @@ -1,144 +0,0 @@ -package noppes.npcs; - -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.ListenableFutureTask; -import net.minecraft.client.Minecraft; -import net.minecraft.entity.Entity; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; -import net.minecraft.server.MinecraftServer; -import net.minecraft.world.World; -import net.minecraft.world.WorldServer; -import cpw.mods.fml.common.eventhandler.Event; -import noppes.npcs.constants.EnumScriptType; -import noppes.npcs.controllers.IScriptHandler; -import noppes.npcs.controllers.ScriptContainer; -import noppes.npcs.controllers.ScriptController; -import noppes.npcs.entity.EntityCustomNpc; -import noppes.npcs.entity.EntityNPCInterface; -import noppes.npcs.scripted.ScriptEvent; -import noppes.npcs.scripted.ScriptNpc; -import noppes.npcs.scripted.ScriptWorld; -import noppes.npcs.scripted.constants.EntityType; -import noppes.npcs.scripted.constants.JobType; -import noppes.npcs.scripted.constants.RoleType; -import org.apache.commons.lang3.Validate; - -import javax.script.ScriptEngine; -import java.util.*; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; - -public class EventDataScript implements IScriptHandler { - private List scripts = new ArrayList(); - private String scriptLanguage = "ECMAScript"; - public long lastInited = -1L; - public boolean hadInteract = true; - private boolean enabled = false; - - public EventDataScript() { - } - - public void clear() { - this.scripts = new ArrayList(); - } - - public void readFromNBT(NBTTagCompound compound) { - this.scripts = NBTTags.GetScript(compound.getTagList("Scripts", 10), this); - this.scriptLanguage = compound.getString("ScriptLanguage"); - this.enabled = compound.getBoolean("ScriptEnabled"); - } - - public NBTTagCompound writeToNBT(NBTTagCompound compound) { - compound.setTag("Scripts", NBTTags.NBTScript(this.scripts)); - compound.setString("ScriptLanguage", this.scriptLanguage); - compound.setBoolean("ScriptEnabled", this.enabled); - return compound; - } - - public void runScript(EnumScriptType type, Event event) { - } - - public void runScript(String type, Event event) { - if(this.isEnabled()) { - Minecraft.getMinecraft().func_152344_a(() -> { - if (ScriptController.Instance.lastLoaded > this.lastInited) { - this.lastInited = ScriptController.Instance.lastLoaded; - if (!type.equals("init")) { - EventHooks.onForgeInit(this); - } - } - - Iterator var3 = this.scripts.iterator(); - - while (var3.hasNext()) { - EventScriptContainer script = (EventScriptContainer) var3.next(); - script.run(type, event); - } - - }); - } - } - - public boolean isEnabled() { - return this.enabled && ScriptController.HasStart && this.scripts.size() > 0; - } - - public boolean isClient() { - return false; - } - - public boolean getEnabled() { - return this.enabled; - } - - public void setEnabled(boolean bo) { - this.enabled = bo; - } - - public String getLanguage() { - return this.scriptLanguage; - } - - public void setLanguage(String lang) { - this.scriptLanguage = lang; - } - - public List getScripts() { - return this.scripts; - } - - public String noticeString() { - return "ForgeScript"; - } - - public Map getConsoleText() { - TreeMap map = new TreeMap(); - int tab = 0; - Iterator var3 = this.getScripts().iterator(); - - while(var3.hasNext()) { - EventScriptContainer script = (EventScriptContainer)var3.next(); - ++tab; - Iterator var5 = script.console.entrySet().iterator(); - - while(var5.hasNext()) { - Map.Entry entry = (Map.Entry)var5.next(); - map.put(entry.getKey(), " tab " + tab + ":\n" + (String)entry.getValue()); - } - } - - return map; - } - - public void clearConsole() { - Iterator var1 = this.getScripts().iterator(); - - while(var1.hasNext()) { - EventScriptContainer script = (EventScriptContainer)var1.next(); - script.console.clear(); - } - - } -} diff --git a/src/main/java/noppes/npcs/EventHooks.java b/src/main/java/noppes/npcs/EventHooks.java index 53af997e8..070676d01 100644 --- a/src/main/java/noppes/npcs/EventHooks.java +++ b/src/main/java/noppes/npcs/EventHooks.java @@ -5,14 +5,25 @@ package noppes.npcs; +import cpw.mods.fml.common.eventhandler.Event; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.inventory.Container; import net.minecraft.util.DamageSource; -import noppes.npcs.controllers.data.PlayerScriptData; +import net.minecraft.world.WorldServer; +import net.minecraftforge.event.entity.EntityEvent; +import net.minecraftforge.event.world.WorldEvent; +import noppes.npcs.controllers.IScriptHandler; +import noppes.npcs.controllers.ScriptController; +import noppes.npcs.controllers.data.ForgeDataScript; +import noppes.npcs.controllers.data.PlayerDataScript; +import noppes.npcs.scripted.IEntity; +import noppes.npcs.scripted.IWorld; import noppes.npcs.scripted.NpcAPI; +import noppes.npcs.scripted.ScriptPlayer; +import noppes.npcs.scripted.event.ForgeEvent; import noppes.npcs.scripted.event.PlayerEvent.AttackEvent; import noppes.npcs.scripted.event.PlayerEvent.ChatEvent; import noppes.npcs.scripted.event.PlayerEvent.ContainerClosed; @@ -27,117 +38,156 @@ import noppes.npcs.scripted.event.PlayerEvent.TossEvent; import noppes.npcs.scripted.wrapper.WrapperNpcAPI; import noppes.npcs.constants.EnumScriptType; import noppes.npcs.controllers.PlayerData; +import org.apache.commons.lang3.StringUtils; public class EventHooks { public EventHooks() { } - public static void onForgeInit(EventDataScript handler) { + public static void onForgeInit(IScriptHandler handler) { noppes.npcs.scripted.event.ForgeEvent.InitEvent event = new noppes.npcs.scripted.event.ForgeEvent.InitEvent(); - handler.runScript(EnumScriptType.INIT, event); + handler.callScript(EnumScriptType.INIT, event); WrapperNpcAPI.EVENT_BUS.post(event); } - public static void onPlayerInit(PlayerScriptData handler) { + public static void onPlayerInit(PlayerDataScript handler) { noppes.npcs.scripted.event.PlayerEvent.InitEvent event = new noppes.npcs.scripted.event.PlayerEvent.InitEvent(handler.getPlayer()); - handler.runScript(EnumScriptType.INIT, event); + handler.callScript(EnumScriptType.INIT, event); WrapperNpcAPI.EVENT_BUS.post(event); } - public static void onPlayerTick(PlayerScriptData handler) { + public static void onPlayerTick(PlayerDataScript handler) { noppes.npcs.scripted.event.PlayerEvent.UpdateEvent event = new noppes.npcs.scripted.event.PlayerEvent.UpdateEvent(handler.getPlayer()); - handler.runScript(EnumScriptType.TICK, event); + handler.callScript(EnumScriptType.TICK, event); WrapperNpcAPI.EVENT_BUS.post(event); } - public static boolean onPlayerInteract(PlayerScriptData handler, noppes.npcs.scripted.event.PlayerEvent.InteractEvent event) { - handler.runScript(EnumScriptType.INTERACT, event); + public static boolean onPlayerInteract(PlayerDataScript handler, noppes.npcs.scripted.event.PlayerEvent.InteractEvent event) { + handler.callScript(EnumScriptType.INTERACT, event); return WrapperNpcAPI.EVENT_BUS.post(event); } - public static boolean onPlayerAttack(PlayerScriptData handler, AttackEvent event) { - handler.runScript(EnumScriptType.ATTACK, event); + public static boolean onPlayerAttack(PlayerDataScript handler, AttackEvent event) { + handler.callScript(EnumScriptType.ATTACK, event); return WrapperNpcAPI.EVENT_BUS.post(event); } - public static boolean onPlayerToss(PlayerScriptData handler, EntityItem entityItem) { + public static boolean onPlayerToss(PlayerDataScript handler, EntityItem entityItem) { TossEvent event = new TossEvent(handler.getPlayer(), NpcAPI.Instance().getIItemStack(entityItem.getEntityItem())); - handler.runScript(EnumScriptType.TOSS, event); + handler.callScript(EnumScriptType.TOSS, event); return WrapperNpcAPI.EVENT_BUS.post(event); } - public static void onPlayerLevelUp(PlayerScriptData handler, int change) { + public static void onPlayerLevelUp(PlayerDataScript handler, int change) { LevelUpEvent event = new LevelUpEvent(handler.getPlayer(), change); - handler.runScript(EnumScriptType.LEVEL_UP, event); + handler.callScript(EnumScriptType.LEVEL_UP, event); WrapperNpcAPI.EVENT_BUS.post(event); } - public static boolean onPlayerPickUp(PlayerScriptData handler, EntityItem entityItem) { + public static boolean onPlayerPickUp(PlayerDataScript handler, EntityItem entityItem) { PickUpEvent event = new PickUpEvent(handler.getPlayer(), NpcAPI.Instance().getIItemStack(entityItem.getEntityItem())); - handler.runScript(EnumScriptType.PICKUP, event); + handler.callScript(EnumScriptType.PICKUP, event); return WrapperNpcAPI.EVENT_BUS.post(event); } - public static void onPlayerContainerOpen(PlayerScriptData handler, Container container) { + public static void onPlayerContainerOpen(PlayerDataScript handler, Container container) { ContainerOpen event = new ContainerOpen(handler.getPlayer(), NpcAPI.Instance().getIContainer(container)); - handler.runScript(EnumScriptType.CONTAINER_OPEN, event); + handler.callScript(EnumScriptType.CONTAINER_OPEN, event); WrapperNpcAPI.EVENT_BUS.post(event); } - public static void onPlayerContainerClose(PlayerScriptData handler, Container container) { + public static void onPlayerContainerClose(PlayerDataScript handler, Container container) { ContainerClosed event = new ContainerClosed(handler.getPlayer(), NpcAPI.Instance().getIContainer(container)); - handler.runScript(EnumScriptType.CONTAINER_CLOSED, event); + handler.callScript(EnumScriptType.CONTAINER_CLOSED, event); WrapperNpcAPI.EVENT_BUS.post(event); } - public static void onPlayerDeath(PlayerScriptData handler, DamageSource source, Entity entity) { + public static void onPlayerDeath(PlayerDataScript handler, DamageSource source, Entity entity) { noppes.npcs.scripted.event.PlayerEvent.DiedEvent event = new noppes.npcs.scripted.event.PlayerEvent.DiedEvent(handler.getPlayer(), source, entity); - handler.runScript(EnumScriptType.KILLED, event); + handler.callScript(EnumScriptType.KILLED, event); WrapperNpcAPI.EVENT_BUS.post(event); } - public static void onPlayerKills(PlayerScriptData handler, EntityLivingBase entityLiving) { + public static void onPlayerKills(PlayerDataScript handler, EntityLivingBase entityLiving) { noppes.npcs.scripted.event.PlayerEvent.KilledEntityEvent event = new noppes.npcs.scripted.event.PlayerEvent.KilledEntityEvent(handler.getPlayer(), entityLiving); - handler.runScript(EnumScriptType.KILLS, event); + handler.callScript(EnumScriptType.KILLS, event); WrapperNpcAPI.EVENT_BUS.post(event); } - public static boolean onPlayerDamaged(PlayerScriptData handler, noppes.npcs.scripted.event.PlayerEvent.DamagedEvent event) { - handler.runScript(EnumScriptType.DAMAGED, event); + public static boolean onPlayerDamaged(PlayerDataScript handler, noppes.npcs.scripted.event.PlayerEvent.DamagedEvent event) { + handler.callScript(EnumScriptType.DAMAGED, event); return WrapperNpcAPI.EVENT_BUS.post(event); } - public static void onPlayerLogin(PlayerScriptData handler) { + public static void onPlayerLogin(PlayerDataScript handler) { LoginEvent event = new LoginEvent(handler.getPlayer()); - handler.runScript(EnumScriptType.LOGIN, event); + handler.callScript(EnumScriptType.LOGIN, event); WrapperNpcAPI.EVENT_BUS.post(event); } - public static void onPlayerLogout(PlayerScriptData handler) { + public static void onPlayerLogout(PlayerDataScript handler) { LogoutEvent event = new LogoutEvent(handler.getPlayer()); - handler.runScript(EnumScriptType.LOGOUT, event); + handler.callScript(EnumScriptType.LOGOUT, event); WrapperNpcAPI.EVENT_BUS.post(event); } - public static void onPlayerChat(PlayerScriptData handler, ChatEvent event) { - handler.runScript(EnumScriptType.CHAT, event); + public static void onPlayerChat(PlayerDataScript handler, ChatEvent event) { + handler.callScript(EnumScriptType.CHAT, event, "message", event.message); WrapperNpcAPI.EVENT_BUS.post(event); } - public static boolean onPlayerRanged(PlayerScriptData handler, noppes.npcs.scripted.event.PlayerEvent.RangedLaunchedEvent event) { - handler.runScript(EnumScriptType.RANGED_LAUNCHED, event); + public static boolean onPlayerRanged(PlayerDataScript handler, noppes.npcs.scripted.event.PlayerEvent.RangedLaunchedEvent event) { + handler.callScript(EnumScriptType.RANGED_LAUNCHED, event); return WrapperNpcAPI.EVENT_BUS.post(event); } - public static boolean onPlayerDamagedEntity(PlayerScriptData handler, DamagedEntityEvent event) { - handler.runScript(EnumScriptType.DAMAGED_ENTITY, event); + public static boolean onPlayerDamagedEntity(PlayerDataScript handler, DamagedEntityEvent event) { + handler.callScript(EnumScriptType.DAMAGED_ENTITY, event); return WrapperNpcAPI.EVENT_BUS.post(event); } public static void onPlayerKeyPressed(EntityPlayerMP player, int button, boolean isCtrlPressed, boolean isShiftPressed, boolean isAltPressed, boolean isMetaPressed) { - PlayerScriptData handler = PlayerData.get(player).scriptData; + PlayerDataScript handler = PlayerData.get(player).scriptData; KeyPressedEvent event = new KeyPressedEvent(handler.getPlayer(), button, isCtrlPressed, isAltPressed, isShiftPressed, isMetaPressed); - handler.runScript(EnumScriptType.KEY_PRESSED, event); + handler.callScript(EnumScriptType.KEY_PRESSED, event); WrapperNpcAPI.EVENT_BUS.post(event); } + + public static void onPlayerTimer(PlayerData data, int id) { + PlayerDataScript handler = data.scriptData; + noppes.npcs.scripted.event.PlayerEvent.TimerEvent event = new noppes.npcs.scripted.event.PlayerEvent.TimerEvent(handler.getPlayer(), id); + handler.callScript(EnumScriptType.TIMER, event); + WrapperNpcAPI.EVENT_BUS.post(event); + } + + public static void onForgeEntityEvent(EntityEvent event) { + IEntity e = NpcAPI.Instance().getIEntity(event.entity); + onForgeEvent(new noppes.npcs.scripted.event.ForgeEvent.EntityEvent(event, e), event); + } + + public static void onForgeEvent(ForgeEvent ev, Event event) { + ForgeDataScript data = ScriptController.Instance.forgeScripts; + if(data.isEnabled()) { + String eventName = event.getClass().getName(); + int i = eventName.lastIndexOf("."); + eventName = StringUtils.uncapitalize(eventName.substring(i + 1).replace("$", "")); + if(event.isCancelable()) { + ev.setCanceled(event.isCanceled()); + } + + data.callScript(eventName, event); + WrapperNpcAPI.EVENT_BUS.post(ev); + if(event.isCancelable()) { + event.setCanceled(ev.isCanceled()); + } + + } + } + + public static void onForgeWorldEvent(WorldEvent event) { + if(ScriptController.Instance.forgeScripts.isEnabled()) { + IWorld e = NpcAPI.Instance().getIWorld((WorldServer)event.world); + onForgeEvent(new noppes.npcs.scripted.event.ForgeEvent.WorldEvent(event, e), event); + } + } } diff --git a/src/main/java/noppes/npcs/EventScriptContainer.java b/src/main/java/noppes/npcs/EventScriptContainer.java deleted file mode 100644 index b88844e57..000000000 --- a/src/main/java/noppes/npcs/EventScriptContainer.java +++ /dev/null @@ -1,234 +0,0 @@ -// -// Source code recreated from a .class file by IntelliJ IDEA -// (powered by Fernflower decompiler) -// - -package noppes.npcs; - -import java.io.PrintWriter; -import java.io.StringWriter; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.TreeMap; -import java.util.Map.Entry; -import javax.script.Invocable; -import javax.script.ScriptEngine; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.util.math.BlockPos; -import noppes.npcs.CustomNpcs; -import noppes.npcs.NBTTags; -import noppes.npcs.NoppesUtilServer; -import cpw.mods.fml.common.eventhandler.Event; -import noppes.npcs.constants.EnumScriptType; -import noppes.npcs.controllers.IScriptHandler; -import noppes.npcs.controllers.ScriptController; -import noppes.npcs.scripted.constants.*; - -public class EventScriptContainer { - private static final String lock = "lock"; - public static EventScriptContainer Current; - private static String CurrentType; - private static final HashMap Data = new HashMap(); - public String fullscript = ""; - public String script = ""; - public TreeMap console = new TreeMap(); - public boolean errored = false; - public List scripts = new ArrayList(); - private HashSet unknownFunctions = new HashSet(); - public long lastCreated = 0L; - private String currentScriptLanguage = null; - private ScriptEngine engine = null; - private IScriptHandler handler = null; - private boolean init = false; - private static Method luaCoerce; - private static Method luaCall; - - private static void FillMap(Class c) { - try { - Data.put(c.getSimpleName(), c.newInstance()); - } catch (Exception var8) { - ; - } - - Field[] declaredFields = c.getDeclaredFields(); - Field[] var2 = declaredFields; - int var3 = declaredFields.length; - - for(int var4 = 0; var4 < var3; ++var4) { - Field field = var2[var4]; - - try { - if(Modifier.isStatic(field.getModifiers()) && field.getType() == Integer.TYPE) { - Data.put(c.getSimpleName() + "_" + field.getName(), Integer.valueOf(field.getInt((Object)null))); - } - } catch (Exception var7) { - ; - } - } - - } - - public EventScriptContainer(IScriptHandler handler) { - this.handler = handler; - } - - public void readFromNBT(NBTTagCompound compound) { - this.script = compound.getString("Script"); - this.console = NBTTags.GetLongStringMap(compound.getTagList("Console", 10)); - this.scripts = NBTTags.getStringList(compound.getTagList("ScriptList", 10)); - this.lastCreated = 0L; - } - - public NBTTagCompound writeToNBT(NBTTagCompound compound) { - compound.setString("Script", this.script); - compound.setTag("Console", NBTTags.NBTLongStringMap(this.console)); - compound.setTag("ScriptList", NBTTags.nbtStringList(this.scripts)); - return compound; - } - - private String getFullCode() { - if(!this.init) { - this.fullscript = this.script; - if(!this.fullscript.isEmpty()) { - this.fullscript = this.fullscript + "\n"; - } - - Iterator var1 = this.scripts.iterator(); - - while(var1.hasNext()) { - String loc = (String)var1.next(); - String code = (String)ScriptController.Instance.scripts.get(loc); - if(code != null && !code.isEmpty()) { - this.fullscript = this.fullscript + code + "\n"; - } - } - - this.unknownFunctions = new HashSet(); - } - - return this.fullscript; - } - - public void run(EnumScriptType type, Event event) { - this.run((String)type.function, (Object)event); - } - - public void run(String type, Object event) { - if(!this.errored && this.hasCode() && !this.unknownFunctions.contains(type) /*&& CustomNpcs.EnableScripting*/) { - this.setEngine(currentScriptLanguage.toLowerCase()); - if(this.engine != null) { - if(ScriptController.Instance.lastLoaded > this.lastCreated) { - this.lastCreated = ScriptController.Instance.lastLoaded; - this.init = false; - } - - String var3 = "lock"; - synchronized("lock") { - Current = this; - CurrentType = type; - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - this.engine.getContext().setWriter(pw); - this.engine.getContext().setErrorWriter(pw); - - try { - if(!this.init) { - this.engine.eval(this.getFullCode()); - this.init = true; - } - - if(this.engine.getFactory().getLanguageName().equals("lua")) { - Object e = this.engine.get(type); - if(e != null) { - if(luaCoerce == null) { - luaCoerce = Class.forName("org.luaj.vm2.lib.jse.CoerceJavaToLua").getMethod("coerce", new Class[]{Object.class}); - luaCall = e.getClass().getMethod("call", new Class[]{Class.forName("org.luaj.vm2.LuaValue")}); - } - - luaCall.invoke(e, new Object[]{luaCoerce.invoke((Object)null, new Object[]{event})}); - } else { - this.unknownFunctions.add(type); - } - } else { - ((Invocable)this.engine).invokeFunction(type, new Object[]{event}); - } - } catch (NoSuchMethodException var13) { - this.unknownFunctions.add(type); - } catch (Throwable var14) { - this.errored = true; - var14.printStackTrace(pw); - //NoppesUtilServer.NotifyOPs(this.handler.noticeString() + " script errored", new Object[0]); - } finally { - this.appandConsole(sw.getBuffer().toString().trim()); - pw.close(); - Current = null; - } - - } - } - } - } - - public void appandConsole(String message) { - if(message != null && !message.isEmpty()) { - long time = System.currentTimeMillis(); - if(this.console.containsKey(Long.valueOf(time))) { - message = (String)this.console.get(Long.valueOf(time)) + "\n" + message; - } - - this.console.put(Long.valueOf(time), message); - - while(this.console.size() > 40) { - this.console.remove(this.console.firstKey()); - } - - } - } - - public boolean hasCode() { - return !this.getFullCode().isEmpty(); - } - - public void setEngine(String scriptLanguage) { - if(this.currentScriptLanguage == null || !this.currentScriptLanguage.equals(scriptLanguage)) { - this.engine = ScriptController.Instance.getEngineByName(scriptLanguage); - if(this.engine == null) { - this.errored = true; - } else { - Iterator var2 = Data.entrySet().iterator(); - - while(var2.hasNext()) { - Entry entry = (Entry)var2.next(); - this.engine.put((String)entry.getKey(), entry.getValue()); - } - - //this.engine.put("dump", new Dump(this, (1)null)); - //this.engine.put("log", new Log(this, (1)null)); - this.currentScriptLanguage = scriptLanguage; - this.init = false; - } - } - } - - public boolean isValid() { - return this.init && !this.errored; - } - - static { - FillMap(AnimationType.class); - FillMap(EntityType.class); - FillMap(RoleType.class); - FillMap(JobType.class); - //FillMap(SideType.class); - //FillMap(TacticalType.class); - //FillMap(PotionType.class); - //FillMap(ParticleType.class); - Data.put("PosZero", new BlockPos(BlockPos.ORIGIN)); - } -} diff --git a/src/main/java/noppes/npcs/NBTTags.java b/src/main/java/noppes/npcs/NBTTags.java index 60fa0df7d..fbf2fedf4 100644 --- a/src/main/java/noppes/npcs/NBTTags.java +++ b/src/main/java/noppes/npcs/NBTTags.java @@ -6,7 +6,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagDouble; -import net.minecraft.nbt.NBTTagInt; import net.minecraft.nbt.NBTTagList; import noppes.npcs.controllers.IScriptHandler; @@ -409,12 +408,12 @@ public class NBTTags { } } - public static List GetScript(NBTTagList list, IScriptHandler handler) { + public static List GetScript(NBTTagList list, IScriptHandler handler) { ArrayList scripts = new ArrayList(); for(int i = 0; i < list.tagCount(); ++i) { NBTTagCompound compoundd = list.getCompoundTagAt(i); - EventScriptContainer script = new EventScriptContainer(handler); + PlayerScriptContainer script = new PlayerScriptContainer(handler); script.readFromNBT(compoundd); scripts.add(script); } @@ -422,12 +421,12 @@ public class NBTTags { return scripts; } - public static NBTTagList NBTScript(List scripts) { + public static NBTTagList NBTScript(List scripts) { NBTTagList list = new NBTTagList(); Iterator var2 = scripts.iterator(); while(var2.hasNext()) { - EventScriptContainer script = (EventScriptContainer)var2.next(); + PlayerScriptContainer script = (PlayerScriptContainer)var2.next(); NBTTagCompound compound = new NBTTagCompound(); script.writeToNBT(compound); list.appendTag(compound); diff --git a/src/main/java/noppes/npcs/PacketHandlerServer.java b/src/main/java/noppes/npcs/PacketHandlerServer.java index 48c533168..8a12e60d0 100644 --- a/src/main/java/noppes/npcs/PacketHandlerServer.java +++ b/src/main/java/noppes/npcs/PacketHandlerServer.java @@ -135,7 +135,7 @@ public class PacketHandlerServer{ if(type == EnumPacketServer.ScriptPlayerGet) { compound = ScriptController.Instance.playerScripts.writeToNBT(new NBTTagCompound()); compound.setTag("Languages", ScriptController.Instance.nbtLanguages()); - Server.sendData(player, EnumPacketClient.GUI_DATA, new Object[]{compound}); + Server.sendData(player, EnumPacketClient.GUI_DATA, compound); } else if(type == EnumPacketServer.ScriptPlayerSave) { ScriptController.Instance.setPlayerScripts(Server.readNBT(buffer)); } diff --git a/src/main/java/noppes/npcs/PlayerScriptContainer.java b/src/main/java/noppes/npcs/PlayerScriptContainer.java new file mode 100644 index 000000000..8d3938ca0 --- /dev/null +++ b/src/main/java/noppes/npcs/PlayerScriptContainer.java @@ -0,0 +1,139 @@ +// +// Source code recreated from a .class file by IntelliJ IDEA +// (powered by Fernflower decompiler) +// + +package noppes.npcs; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.TreeMap; +import javax.script.Compilable; +import javax.script.CompiledScript; +import javax.script.Invocable; +import javax.script.ScriptEngine; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.math.BlockPos; +import cpw.mods.fml.common.eventhandler.Event; +import noppes.npcs.constants.EnumScriptType; +import noppes.npcs.controllers.IScriptHandler; +import noppes.npcs.controllers.ScriptController; +import noppes.npcs.scripted.constants.*; + +public class PlayerScriptContainer { + public static PlayerScriptContainer Current; + public String type = "init"; + public String fullscript = ""; + public String script = ""; + public TreeMap console = new TreeMap(); + public boolean errored = false; + public List scripts = new ArrayList(); + public long lastCreated = 0L; + private String currentScriptLanguage = null; + public ScriptEngine engine = null; + private IScriptHandler handler = null; + private CompiledScript compScript = null; + private boolean init = false; + + public PlayerScriptContainer(IScriptHandler handler) { + this.handler = handler; + } + + public void readFromNBT(NBTTagCompound compound) { + this.script = compound.getString("Script"); + this.type = compound.getString("Type"); + this.console = NBTTags.GetLongStringMap(compound.getTagList("Console", 10)); + this.scripts = NBTTags.getStringList(compound.getTagList("ScriptList", 10)); + this.lastCreated = 0L; + } + + public NBTTagCompound writeToNBT(NBTTagCompound compound) { + compound.setString("Script", this.script); + compound.setString("Type", this.type); + compound.setTag("Console", NBTTags.NBTLongStringMap(this.console)); + compound.setTag("ScriptList", NBTTags.nbtStringList(this.scripts)); + return compound; + } + + private String getFullCode() { + if(!this.init) { + this.fullscript = this.script; + if(!this.fullscript.isEmpty()) { + this.fullscript = this.fullscript + "\n"; + } + + Iterator var1 = this.scripts.iterator(); + + while(var1.hasNext()) { + String loc = (String)var1.next(); + String code = (String)ScriptController.Instance.scripts.get(loc); + if(code != null && !code.isEmpty()) { + this.fullscript = this.fullscript + code + "\n"; + } + } + } + + return this.fullscript; + } + public void run(ScriptEngine engine){ + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + engine.getContext().setWriter(pw); + engine.getContext().setErrorWriter(pw); + try { + if(compScript == null && engine instanceof Compilable) + compScript = ((Compilable)engine).compile(getFullCode()); + + if(compScript != null){ + compScript.eval(engine.getContext()); + } + else + engine.eval(getFullCode()); + + } catch (Exception e) { + errored = true; + appandConsole(e.getMessage()); + } + appandConsole(sw.getBuffer().toString().trim()); + } + + public void setType(String type) { + this.type = type; + } + + public void appandConsole(String message) { + if(message != null && !message.isEmpty()) { + long time = System.currentTimeMillis(); + if(this.console.containsKey(Long.valueOf(time))) { + message = (String)this.console.get(Long.valueOf(time)) + "\n" + message; + } + + this.console.put(Long.valueOf(time), message); + + while(this.console.size() > 40) { + this.console.remove(this.console.firstKey()); + } + + } + } + + public boolean hasCode() { + return !this.getFullCode().isEmpty(); + } + + public void setEngine(String scriptLanguage) { + if(currentScriptLanguage != null && currentScriptLanguage.equals(scriptLanguage)) + return; + engine = ScriptController.Instance.getEngineByName(scriptLanguage.toLowerCase()); + currentScriptLanguage = scriptLanguage; + } +} diff --git a/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java b/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java new file mode 100644 index 000000000..6b29d7749 --- /dev/null +++ b/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java @@ -0,0 +1,384 @@ +// +// Source code recreated from a .class file by IntelliJ IDEA +// (powered by Fernflower decompiler) +// + +package noppes.npcs; + +import com.google.common.reflect.ClassPath; +import com.google.common.reflect.ClassPath.ClassInfo; +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; + +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.EventPriority; +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.entity.player.EntityPlayer; +import net.minecraft.util.ChatComponentTranslation; +import net.minecraft.world.WorldServer; +import net.minecraftforge.common.ForgeHooks; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.ServerChatEvent; +import net.minecraftforge.event.entity.EntityEvent; +import net.minecraftforge.event.entity.EntityEvent.EntityConstructing; +import net.minecraftforge.event.entity.player.ItemTooltipEvent; +import net.minecraftforge.event.world.WorldEvent; +import net.minecraftforge.event.world.WorldEvent.PotentialSpawns; +import noppes.npcs.controllers.PlayerData; +import noppes.npcs.controllers.data.PlayerDataScript; +import noppes.npcs.entity.EntityNPCInterface; +import noppes.npcs.scripted.event.ForgeEvent; +import noppes.npcs.controllers.PixelmonHelper; +import noppes.npcs.controllers.ScriptController; + +public class ScriptPlayerEventHandler { + public ScriptPlayerEventHandler() { + } + @SubscribeEvent + public void onServerTick(TickEvent.PlayerTickEvent event) { + if(event.side == Side.SERVER && event.phase == TickEvent.Phase.START) { + EntityPlayer player = event.player; + PlayerData data = PlayerData.get(player); + if(player.ticksExisted % 10 == 0) { + EventHooks.onPlayerTick(data.scriptData); + } + + if(data.playerLevel != player.experienceLevel) { + EventHooks.onPlayerLevelUp(data.scriptData, data.playerLevel - player.experienceLevel); + data.playerLevel = player.experienceLevel; + } + + data.timers.update(); + } + } + /* + @SubscribeEvent + public void invoke(LeftClickBlock event) { + if(!event.entityPlayer.field_70170_p.field_72995_K && event.getWorld() instanceof WorldServer) { + PlayerDataScript handler = PlayerData.get(event.entityPlayer).scriptData; + AttackEvent ev = new AttackEvent(handler.getPlayer(), 2, NpcAPI.Instance().getIBlock(event.getWorld(), event.getPos())); + event.setCanceled(EventHooks.onPlayerAttack(handler, ev)); + if(event.getItemStack().func_77973_b() == CustomItems.scripted_item && !event.isCanceled()) { + ItemScriptedWrapper isw = ItemScripted.GetWrapper(event.getItemStack()); + noppes.npcs.api.event.ItemEvent.AttackEvent eve = new noppes.npcs.api.event.ItemEvent.AttackEvent(isw, handler.getPlayer(), 2, NpcAPI.Instance().getIBlock(event.getWorld(), event.getPos())); + eve.setCanceled(event.isCanceled()); + event.setCanceled(EventHooks.onScriptItemAttack(isw, eve)); + } + + } + } + + @SubscribeEvent + public void invoke(RightClickBlock event) { + if(!event.entityPlayer.field_70170_p.field_72995_K && event.getWorld() instanceof WorldServer) { + if(event.getItemStack().func_77973_b() == CustomItems.nbt_book) { + ((ItemNbtBook)event.getItemStack().func_77973_b()).blockEvent(event); + event.setCanceled(true); + } else { + PlayerDataScript handler = PlayerData.get(event.entityPlayer).scriptData; + handler.hadInteract = true; + InteractEvent ev = new InteractEvent(handler.getPlayer(), 2, NpcAPI.Instance().getIBlock(event.getWorld(), event.getPos())); + event.setCanceled(EventHooks.onPlayerInteract(handler, ev)); + if(event.getItemStack().func_77973_b() == CustomItems.scripted_item && !event.isCanceled()) { + ItemScriptedWrapper isw = ItemScripted.GetWrapper(event.getItemStack()); + noppes.npcs.api.event.ItemEvent.InteractEvent eve = new noppes.npcs.api.event.ItemEvent.InteractEvent(isw, handler.getPlayer(), 2, NpcAPI.Instance().getIBlock(event.getWorld(), event.getPos())); + event.setCanceled(EventHooks.onScriptItemInteract(isw, eve)); + } + + } + } + } + + @SubscribeEvent + public void invoke(EntityInteractEvent event) { + if(!event.entityPlayer.field_70170_p.field_72995_K && event.getWorld() instanceof WorldServer) { + if(event.getItemStack().func_77973_b() == CustomItems.nbt_book) { + ((ItemNbtBook)event.getItemStack().func_77973_b()).entityEvent(event); + event.setCanceled(true); + } else { + PlayerDataScript handler = PlayerData.get(event.entityPlayer).scriptData; + InteractEvent ev = new InteractEvent(handler.getPlayer(), 1, NpcAPI.Instance().getIEntity(event.getTarget())); + event.setCanceled(EventHooks.onPlayerInteract(handler, ev)); + if(event.getItemStack().func_77973_b() == CustomItems.scripted_item && !event.isCanceled()) { + ItemScriptedWrapper isw = ItemScripted.GetWrapper(event.getItemStack()); + noppes.npcs.api.event.ItemEvent.InteractEvent eve = new noppes.npcs.api.event.ItemEvent.InteractEvent(isw, handler.getPlayer(), 1, NpcAPI.Instance().getIEntity(event.getTarget())); + event.setCanceled(EventHooks.onScriptItemInteract(isw, eve)); + } + + } + } + } + + @SubscribeEvent + public void invoke(RightClickItem event) { + if(!event.entityPlayer.field_70170_p.field_72995_K && event.getWorld() instanceof WorldServer) { + if(event.entityPlayer.func_184812_l_() && event.entityPlayer.func_70093_af() && event.getItemStack().func_77973_b() == CustomItems.scripted_item) { + NoppesUtilServer.sendOpenGui(event.entityPlayer, EnumGuiType.ScriptItem, (EntityNPCInterface)null); + } else { + PlayerDataScript handler = PlayerData.get(event.entityPlayer).scriptData; + if(handler.hadInteract) { + handler.hadInteract = false; + } else { + InteractEvent ev = new InteractEvent(handler.getPlayer(), 0, (Object)null); + event.setCanceled(EventHooks.onPlayerInteract(handler, ev)); + if(event.getItemStack().func_77973_b() == CustomItems.scripted_item && !event.isCanceled()) { + ItemScriptedWrapper isw = ItemScripted.GetWrapper(event.getItemStack()); + noppes.npcs.api.event.ItemEvent.InteractEvent eve = new noppes.npcs.api.event.ItemEvent.InteractEvent(isw, handler.getPlayer(), 0, (Object)null); + event.setCanceled(EventHooks.onScriptItemInteract(isw, eve)); + } + + } + } + } + } + + @SubscribeEvent + public void invoke(ArrowLooseEvent event) { + if(!event.entityPlayer.field_70170_p.field_72995_K && event.getWorld() instanceof WorldServer) { + PlayerDataScript handler = PlayerData.get(event.entityPlayer).scriptData; + RangedLaunchedEvent ev = new RangedLaunchedEvent(handler.getPlayer()); + event.setCanceled(EventHooks.onPlayerRanged(handler, ev)); + } + } + + @SubscribeEvent + public void invoke(BreakEvent event) { + if(!event.getPlayer().field_70170_p.field_72995_K && event.getWorld() instanceof WorldServer) { + PlayerDataScript handler = PlayerData.get(event.getPlayer()).scriptData; + noppes.npcs.api.event.PlayerEvent.BreakEvent ev = new noppes.npcs.api.event.PlayerEvent.BreakEvent(handler.getPlayer(), NpcAPI.Instance().getIBlock(event.getWorld(), event.getPos()), event.getExpToDrop()); + event.setCanceled(EventHooks.onPlayerBreak(handler, ev)); + event.setExpToDrop(ev.exp); + } + } + + @SubscribeEvent + public void invoke(ItemTossEvent event) { + if(event.getPlayer().field_70170_p instanceof WorldServer) { + PlayerDataScript handler = PlayerData.get(event.getPlayer()).scriptData; + event.setCanceled(EventHooks.onPlayerToss(handler, event.getEntityItem())); + } + } + + @SubscribeEvent + public void invoke(EntityItemPickupEvent event) { + if(event.entityPlayer.field_70170_p instanceof WorldServer) { + PlayerDataScript handler = PlayerData.get(event.entityPlayer).scriptData; + event.setCanceled(EventHooks.onPlayerPickUp(handler, event.getItem())); + } + } + + @SubscribeEvent + public void invoke(Open event) { + if(event.entityPlayer.field_70170_p instanceof WorldServer) { + PlayerDataScript handler = PlayerData.get(event.entityPlayer).scriptData; + EventHooks.onPlayerContainerOpen(handler, event.getContainer()); + } + } + + @SubscribeEvent + public void invoke(Close event) { + if(event.entityPlayer.field_70170_p instanceof WorldServer) { + PlayerDataScript handler = PlayerData.get(event.entityPlayer).scriptData; + EventHooks.onPlayerContainerClose(handler, event.getContainer()); + } + } + + @SubscribeEvent + public void invoke(LivingDeathEvent event) { + if(event.getEntityLiving().field_70170_p instanceof WorldServer) { + Entity source = NoppesUtilServer.GetDamageSourcee(event.getSource()); + PlayerDataScript handler; + if(event.getEntityLiving() instanceof EntityPlayer) { + handler = PlayerData.get((EntityPlayer)event.getEntityLiving()).scriptData; + EventHooks.onPlayerDeath(handler, event.getSource(), source); + } + + if(source instanceof EntityPlayer) { + handler = PlayerData.get((EntityPlayer)source).scriptData; + EventHooks.onPlayerKills(handler, event.getEntityLiving()); + } + + } + } + + @SubscribeEvent + public void invoke(LivingHurtEvent event) { + if(event.getEntityLiving().field_70170_p instanceof WorldServer) { + Entity source = NoppesUtilServer.GetDamageSourcee(event.getSource()); + PlayerDataScript handler; + if(event.getEntityLiving() instanceof EntityPlayer) { + handler = PlayerData.get((EntityPlayer)event.getEntityLiving()).scriptData; + DamagedEvent pevent = new DamagedEvent(handler.getPlayer(), source, event.getAmount(), event.getSource()); + event.setCanceled(EventHooks.onPlayerDamaged(handler, pevent)); + event.setAmount(pevent.damage); + } + + if(source instanceof EntityPlayer) { + handler = PlayerData.get((EntityPlayer)source).scriptData; + DamagedEntityEvent pevent1 = new DamagedEntityEvent(handler.getPlayer(), event.getEntityLiving(), event.getAmount(), event.getSource()); + event.setCanceled(EventHooks.onPlayerDamagedEntity(handler, pevent1)); + event.setAmount(pevent1.damage); + } + + } + } + + @SubscribeEvent + public void invoke(LivingAttackEvent event) { + if(event.getEntityLiving().field_70170_p instanceof WorldServer) { + Entity source = NoppesUtilServer.GetDamageSourcee(event.getSource()); + if(source instanceof EntityPlayer) { + PlayerDataScript handler = PlayerData.get((EntityPlayer)source).scriptData; + ItemStack item = ((EntityPlayer)source).func_184614_ca(); + IEntity target = NpcAPI.Instance().getIEntity(event.getEntityLiving()); + AttackEvent ev = new AttackEvent(handler.getPlayer(), 1, target); + event.setCanceled(EventHooks.onPlayerAttack(handler, ev)); + if(item.func_77973_b() == CustomItems.scripted_item && !event.isCanceled()) { + ItemScriptedWrapper isw = ItemScripted.GetWrapper(item); + noppes.npcs.api.event.ItemEvent.AttackEvent eve = new noppes.npcs.api.event.ItemEvent.AttackEvent(isw, handler.getPlayer(), 1, target); + eve.setCanceled(event.isCanceled()); + event.setCanceled(EventHooks.onScriptItemAttack(isw, eve)); + } + } + + } + } + + @SubscribeEvent + public void invoke(PlayerLoggedInEvent event) { + if(event.player.field_70170_p instanceof WorldServer) { + PlayerDataScript handler = PlayerData.get(event.player).scriptData; + EventHooks.onPlayerLogin(handler); + } + } + + @SubscribeEvent + public void invoke(PlayerLoggedOutEvent event) { + if(event.player.field_70170_p instanceof WorldServer) { + PlayerDataScript handler = PlayerData.get(event.player).scriptData; + EventHooks.onPlayerLogout(handler); + } + } + */ + + @SubscribeEvent( + priority = EventPriority.HIGHEST + ) + public void invoke(ServerChatEvent event) { + if(event.player.worldObj instanceof WorldServer && event.player != EntityNPCInterface.chateventPlayer) { + PlayerDataScript handler = PlayerData.get(event.player).scriptData; + String message = event.message; + noppes.npcs.scripted.event.PlayerEvent.ChatEvent ev = new noppes.npcs.scripted.event.PlayerEvent.ChatEvent(handler.getPlayer(), event.message); + EventHooks.onPlayerChat(handler, ev); + event.setCanceled(ev.isCanceled()); + if(!message.equals(ev.message)) { + ChatComponentTranslation chat = new ChatComponentTranslation("", new Object[0]); + chat.appendSibling(ForgeHooks.newChatWithLinks(ev.message)); + event.component = chat; + } + } + } + + public ScriptPlayerEventHandler registerForgeEvents() { + ScriptPlayerEventHandler.ForgeEventHandler handler = new ScriptPlayerEventHandler.ForgeEventHandler(); + + try { + Method e = handler.getClass().getMethod("forgeEntity", new Class[]{Event.class}); + Method register = MinecraftForge.EVENT_BUS.getClass().getDeclaredMethod("register", new Class[]{Class.class, Object.class, Method.class, ModContainer.class}); + register.setAccessible(true); + ArrayList list = new ArrayList(ClassPath.from(this.getClass().getClassLoader()).getTopLevelClassesRecursive("net.minecraftforge.event")); + list.addAll(ClassPath.from(this.getClass().getClassLoader()).getTopLevelClassesRecursive("net.minecraftforge.fml.common")); + Iterator e1 = list.iterator(); + + while(true) { + ClassInfo classLoader; + String classes; + do { + if(!e1.hasNext()) { + if(PixelmonHelper.Enabled) { + try { + Field e2 = ClassLoader.class.getDeclaredField("classes"); + e2.setAccessible(true); + ClassLoader classLoader1 = Thread.currentThread().getContextClassLoader(); + ArrayList classes1 = new ArrayList((Vector)e2.get(classLoader1)); + Iterator infoClass1 = classes1.iterator(); + + while(infoClass1.hasNext()) { + Class c2 = (Class)infoClass1.next(); + } + } catch (Exception var12) { + var12.printStackTrace(); + } + + return this; + } + + return this; + } + + classLoader = (ClassInfo)e1.next(); + classes = classLoader.getName(); + } while(classes.startsWith("net.minecraftforge.event.terraingen")); + + Class infoClass = classLoader.load(); + ArrayList c = new ArrayList(Arrays.asList(infoClass.getDeclaredClasses())); + if(c.isEmpty()) { + c.add(infoClass); + } + + Iterator var10 = c.iterator(); + while(var10.hasNext()) { + Class c1 = (Class)var10.next(); + if(!EntityConstructing.class.isAssignableFrom(c1) && !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())) { + register.invoke(MinecraftForge.EVENT_BUS, new Object[]{c1, handler, e, Loader.instance().activeModContainer()}); + } + } + } + } catch (Exception var13) { + var13.printStackTrace(); + return this; + } + } + + public class ForgeEventHandler { + public ForgeEventHandler() { + } + + @SubscribeEvent + public void forgeEntity(Event event) { + if(CustomNpcs.getServer() != null && ScriptController.Instance.forgeScripts.isEnabled()) { + if(event instanceof EntityEvent) { + EntityEvent ev2 = (EntityEvent)event; + if(ev2.entity != null && ev2.entity.worldObj instanceof WorldServer) { + EventHooks.onForgeEntityEvent(ev2); + } + } else if(event instanceof WorldEvent) { + WorldEvent ev1 = (WorldEvent)event; + if(ev1.world instanceof WorldServer) { + EventHooks.onForgeWorldEvent(ev1); + } + } else if(!(event instanceof TickEvent) || ((TickEvent)event).side != Side.CLIENT) { + if(event instanceof PlayerEvent) { + PlayerEvent ev = (PlayerEvent)event; + if(!(ev.player.worldObj instanceof WorldServer)) { + return; + } + } + + EventHooks.onForgeEvent(new ForgeEvent(event), event); + } + } + } + } +} diff --git a/src/main/java/noppes/npcs/client/gui/EventGuiScriptList.java b/src/main/java/noppes/npcs/client/gui/EventGuiScriptList.java index decb5ab47..0dbc6b2d1 100644 --- a/src/main/java/noppes/npcs/client/gui/EventGuiScriptList.java +++ b/src/main/java/noppes/npcs/client/gui/EventGuiScriptList.java @@ -4,23 +4,19 @@ import java.util.ArrayList; import java.util.List; import net.minecraft.client.gui.GuiButton; -import net.minecraft.world.biome.BiomeGenBase; -import noppes.npcs.EventScriptContainer; +import noppes.npcs.PlayerScriptContainer; import noppes.npcs.client.gui.util.GuiCustomScroll; -import noppes.npcs.client.gui.util.GuiNPCInterface; import noppes.npcs.client.gui.util.GuiNpcButton; import noppes.npcs.client.gui.util.GuiNpcLabel; import noppes.npcs.client.gui.util.SubGuiInterface; -import noppes.npcs.controllers.ScriptContainer; -import noppes.npcs.entity.EntityNPCInterface; public class EventGuiScriptList extends SubGuiInterface{ private GuiCustomScroll scroll1; private GuiCustomScroll scroll2; - private EventScriptContainer container; + private PlayerScriptContainer container; private List scripts; - public EventGuiScriptList(List scripts, EventScriptContainer container){ + public EventGuiScriptList(List scripts, PlayerScriptContainer container){ this.container = container; setBackground("menubg.png"); xSize = 346; diff --git a/src/main/java/noppes/npcs/client/gui/script/EventGuiNPCInterface.java b/src/main/java/noppes/npcs/client/gui/script/EventGuiNPCInterface.java deleted file mode 100644 index bf99073f3..000000000 --- a/src/main/java/noppes/npcs/client/gui/script/EventGuiNPCInterface.java +++ /dev/null @@ -1,535 +0,0 @@ -// -// Source code recreated from a .class file by IntelliJ IDEA -// (powered by FernFlower decompiler) -// - -package noppes.npcs.client.gui.script; - -import com.google.common.collect.Lists; -import java.net.URI; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.EntityPlayerSP; -import net.minecraft.client.gui.FontRenderer; -import net.minecraft.client.gui.GuiButton; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.client.renderer.RenderHelper; -import net.minecraft.client.renderer.entity.RenderItem; -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.util.ResourceLocation; -import net.minecraftforge.client.event.GuiScreenEvent.ActionPerformedEvent.Post; -import net.minecraftforge.client.event.GuiScreenEvent.ActionPerformedEvent.Pre; -import net.minecraftforge.common.MinecraftForge; -import noppes.npcs.client.gui.util.*; -import noppes.npcs.entity.EntityNPCInterface; -import org.lwjgl.input.Mouse; -import org.lwjgl.opengl.GL11; - -public abstract class EventGuiNPCInterface extends GuiScreen { - public EntityPlayerSP player; - public boolean drawDefaultBackground; - public EntityNPCInterface npc; - private Map buttons; - private Map topbuttons; - private Map sidebuttons; - private Map textfields; - private Map labels; - private Map scrolls; - private Map sliders; - private Map extra; - private List components; - public String title; - public ResourceLocation background; - public boolean closeOnEsc; - public int guiLeft; - public int guiTop; - public int xSize; - public int ySize; - private SubGuiInterface subgui; - public int mouseX; - public int mouseY; - public float bgScale; - private GuiButton selectedButton; - - public EventGuiNPCInterface(EntityNPCInterface npc) { - this.drawDefaultBackground = true; - this.buttons = new ConcurrentHashMap(); - this.topbuttons = new ConcurrentHashMap(); - this.sidebuttons = new ConcurrentHashMap(); - this.textfields = new ConcurrentHashMap(); - this.labels = new ConcurrentHashMap(); - this.scrolls = new ConcurrentHashMap(); - this.sliders = new ConcurrentHashMap(); - this.extra = new ConcurrentHashMap(); - this.components = new ArrayList(); - this.background = null; - this.closeOnEsc = false; - this.bgScale = 1.0F; - this.player = Minecraft.getMinecraft().thePlayer; - this.npc = npc; - this.title = ""; - this.xSize = 200; - this.ySize = 222; - this.drawDefaultBackground = false; - this.mc = Minecraft.getMinecraft(); - this.itemRender = RenderItem.getInstance(); - this.fontRendererObj = this.mc.fontRenderer; - } - - public EventGuiNPCInterface() { - this((EntityNPCInterface)null); - } - - public void setBackground(String texture) { - this.background = new ResourceLocation("customnpcs", "textures/gui/" + texture); - } - - public ResourceLocation getResource(String texture) { - return new ResourceLocation("customnpcs", "textures/gui/" + texture); - } - - public void setWorldAndResolution(Minecraft mc, int width, int height) { - super.setWorldAndResolution(mc, width, height); - this.initPacket(); - } - - public void initPacket() { - } - - public void initGui() { - super.initGui(); - GuiNpcTextField.unfocus(); - if (this.subgui != null) { - this.subgui.setWorldAndResolution(this.mc, this.width, this.height); - this.subgui.initGui(); - } - - this.guiLeft = (this.width - this.xSize) / 2; - this.guiTop = (this.height - this.ySize) / 2; - this.buttonList = Lists.newArrayList(); - this.buttons = new ConcurrentHashMap(); - this.topbuttons = new ConcurrentHashMap(); - this.sidebuttons = new ConcurrentHashMap(); - this.textfields = new ConcurrentHashMap(); - this.labels = new ConcurrentHashMap(); - this.scrolls = new ConcurrentHashMap(); - this.sliders = new ConcurrentHashMap(); - this.extra = new ConcurrentHashMap(); - this.components = new ArrayList(); - } - - public void updateScreen() { - if (this.subgui != null) { - this.subgui.updateScreen(); - } else { - Iterator var1 = (new ArrayList(this.textfields.values())).iterator(); - - while(var1.hasNext()) { - GuiNpcTextField tf = (GuiNpcTextField)var1.next(); - if (tf.enabled) { - tf.updateCursorCounter(); - } - } - - var1 = (new ArrayList(this.components)).iterator(); - - while(var1.hasNext()) { - IGui comp = (IGui)var1.next(); - comp.updateScreen(); - } - - super.updateScreen(); - } - - } - - public void addExtra(GuiHoverText gui) { - gui.setWorldAndResolution(this.mc, 350, 250); - this.extra.put(gui.id, gui); - } - - public void mouseClicked(int i, int j, int k) { - if (this.subgui != null) { - this.subgui.mouseClicked(i, j, k); - } else { - Iterator var4 = (new ArrayList(this.textfields.values())).iterator(); - - while(var4.hasNext()) { - GuiNpcTextField tf = (GuiNpcTextField)var4.next(); - if (tf.enabled) { - tf.mouseClicked(i, j, k); - } - } - - var4 = (new ArrayList(this.components)).iterator(); - - while(var4.hasNext()) { - IGui comp = (IGui)var4.next(); - if (comp instanceof IMouseListener) { - ((IMouseListener)comp).mouseClicked(i, j, k); - } - } - - this.mouseEvent(i, j, k); - if (k == 0) { - var4 = (new ArrayList(this.scrolls.values())).iterator(); - - while(var4.hasNext()) { - GuiCustomScroll scroll = (GuiCustomScroll)var4.next(); - scroll.mouseClicked(i, j, k); - } - - var4 = this.buttonList.iterator(); - - while(var4.hasNext()) { - GuiButton guibutton = (GuiButton)var4.next(); - if (guibutton.mousePressed(this.mc, this.mouseX, this.mouseY)) { - Pre event = new Pre(this, guibutton, this.buttonList); - if (!MinecraftForge.EVENT_BUS.post(event)) { - this.selectedButton = guibutton; - guibutton.func_146113_a(this.mc.getSoundHandler()); - this.actionPerformed(guibutton); - if (this.equals(this.mc.currentScreen)) { - MinecraftForge.EVENT_BUS.post(new Post(this, guibutton, this.buttonList)); - } - } - break; - } - } - } - } - - } - - public void mouseReleased(int mouseX, int mouseY, int state) { - if (this.selectedButton != null && state == 0) { - this.selectedButton.mouseReleased(mouseX, mouseY); - this.selectedButton = null; - } - - } - - public void mouseEvent(int i, int j, int k) { - } - - protected void actionPerformed(GuiButton guibutton) { - if (this.subgui != null) { - this.subgui.buttonEvent(guibutton); - } else { - this.buttonEvent(guibutton); - } - - } - public void buttonEvent(GuiButton guibutton){}; - - public void keyTyped(char c, int i) { - if (this.subgui != null) { - this.subgui.keyTyped(c, i); - } else { - boolean active = false; - Iterator var4 = this.components.iterator(); - - IGui comp; - while(var4.hasNext()) { - comp = (IGui)var4.next(); - if (comp.isActive()) { - active = true; - break; - } - } - - active = active || GuiNpcTextField.isFieldActive(); - if (!this.closeOnEsc || i != 1 && (active || !this.isInventoryKey(i))) { - var4 = (new ArrayList(this.textfields.values())).iterator(); - - while(var4.hasNext()) { - GuiNpcTextField tf = (GuiNpcTextField)var4.next(); - tf.textboxKeyTyped(c, i); - } - - var4 = (new ArrayList(this.components)).iterator(); - - while(var4.hasNext()) { - comp = (IGui)var4.next(); - if (comp instanceof IKeyListener) { - ((IKeyListener)comp).keyTyped(c, i); - } - } - - } else { - this.close(); - } - } - } - - public void onGuiClosed() { - GuiNpcTextField.unfocus(); - } - - public void close() { - this.displayGuiScreen((GuiScreen)null); - this.mc.setIngameFocus(); - this.save(); - } - - public void addButton(GuiNpcButton button) { - this.buttons.put(button.id, button); - this.buttonList.add(button); - } - - public void addTopButton(GuiMenuTopButton button) { - this.topbuttons.put(button.id, button); - this.buttonList.add(button); - } - - public void addSideButton(GuiMenuSideButton button) { - this.sidebuttons.put(button.id, button); - this.buttonList.add(button); - } - - public GuiNpcButton getButton(int i) { - return (GuiNpcButton)this.buttons.get(i); - } - - public GuiMenuSideButton getSideButton(int i) { - return (GuiMenuSideButton)this.sidebuttons.get(i); - } - - public GuiMenuTopButton getTopButton(int i) { - return (GuiMenuTopButton)this.topbuttons.get(i); - } - - public void addTextField(GuiNpcTextField tf) { - this.textfields.put(tf.id, tf); - } - - public GuiNpcTextField getTextField(int i) { - return (GuiNpcTextField)this.textfields.get(i); - } - - public void add(IGui gui) { - this.components.add(gui); - } - - public IGui get(int id) { - Iterator var2 = this.components.iterator(); - - IGui comp; - do { - if (!var2.hasNext()) { - return null; - } - - comp = (IGui)var2.next(); - } while(comp.getID() != id); - - return comp; - } - - public void addLabel(GuiNpcLabel label) { - this.labels.put(label.id, label); - } - - public GuiNpcLabel getLabel(int i) { - return (GuiNpcLabel)this.labels.get(i); - } - - public void addSlider(GuiNpcSlider slider) { - this.sliders.put(slider.id, slider); - this.buttonList.add(slider); - } - - public GuiNpcSlider getSlider(int i) { - return (GuiNpcSlider)this.sliders.get(i); - } - - public void addScroll(GuiCustomScroll scroll) { - scroll.setWorldAndResolution(this.mc, 350, 250); - this.scrolls.put(scroll.id, scroll); - } - - public GuiCustomScroll getScroll(int id) { - return (GuiCustomScroll)this.scrolls.get(id); - } - - public abstract void save(); - - public void drawScreen(int mouseX, int mouseY, float partialTicks) { - this.mouseX = mouseX; - this.mouseY = mouseY; - int x = mouseX; - int y = mouseY; - if (this.subgui != null) { - y = 0; - x = 0; - } - - if (this.drawDefaultBackground && this.subgui == null) { - this.drawDefaultBackground(); - } - - if (this.background != null && this.mc.renderEngine != null) { - GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); - GL11.glPushMatrix(); - GL11.glTranslatef((float) this.guiLeft, (float) this.guiTop, 0.0F); - GL11.glScalef(this.bgScale, this.bgScale, this.bgScale); - this.mc.renderEngine.bindTexture(this.background); - if (this.xSize > 256) { - this.drawTexturedModalRect(0, 0, 0, 0, 250, this.ySize); - this.drawTexturedModalRect(250, 0, 256 - (this.xSize - 250), 0, this.xSize - 250, this.ySize); - } else { - this.drawTexturedModalRect(0, 0, 0, 0, this.xSize, this.ySize); - } - - GL11.glPopMatrix(); - } - - this.drawCenteredString(this.fontRendererObj, this.title, this.width / 2, 8, 16777215); - Iterator var6 = (new ArrayList(this.labels.values())).iterator(); - - while(var6.hasNext()) { - GuiNpcLabel label = (GuiNpcLabel)var6.next(); - label.drawLabel(this, this.fontRendererObj); - } - - var6 = (new ArrayList(this.textfields.values())).iterator(); - - while(var6.hasNext()) { - GuiNpcTextField tf = (GuiNpcTextField)var6.next(); - tf.drawTextBox(x, y); - } - - var6 = (new ArrayList(this.components)).iterator(); - - while(var6.hasNext()) { - IGui comp = (IGui)var6.next(); - comp.drawScreen(x, y); - } - - var6 = (new ArrayList(this.scrolls.values())).iterator(); - - while(var6.hasNext()) { - GuiCustomScroll scroll = (GuiCustomScroll)var6.next(); - scroll.drawScreen(x, y, partialTicks, !this.hasSubGui() && scroll.isMouseOver(x, y) ? Mouse.getDWheel() : 0); - } - - var6 = (new ArrayList(this.extra.values())).iterator(); - - while(var6.hasNext()) { - GuiScreen gui = (GuiScreen)var6.next(); - gui.drawScreen(x, y, partialTicks); - } - - super.drawScreen(x, y, partialTicks); - if (this.subgui != null) { - this.subgui.drawScreen(mouseX, mouseY, partialTicks); - } - - } - - public FontRenderer getFontRenderer() { - return this.fontRendererObj; - } - - public void elementClicked() { - if (this.subgui != null) { - this.subgui.elementClicked(); - } - - } - - public boolean doesGuiPauseGame() { - return false; - } - - public void doubleClicked() { - } - - public boolean isInventoryKey(int i) { - return i == this.mc.gameSettings.keyBindInventory.getKeyCode(); - } - - public void drawDefaultBackground() { - super.drawDefaultBackground(); - } - - public void displayGuiScreen(GuiScreen gui) { - this.mc.displayGuiScreen(gui); - } - - public void setSubGui(SubGuiInterface gui) { - this.subgui = gui; - this.subgui.npc = this.npc; - this.subgui.setWorldAndResolution(this.mc, this.width, this.height); - this.subgui.parent = this; - this.initGui(); - } - - public void closeSubGui(SubGuiInterface gui) { - this.subgui = null; - } - - public boolean hasSubGui() { - return this.subgui != null; - } - - public SubGuiInterface getSubGui() { - return this.hasSubGui() && this.subgui.hasSubGui() ? this.subgui.getSubGui() : this.subgui; - } - - public void drawNpc(int x, int y) { - this.drawNpc(this.npc, x, y, 1.0F, 0); - } - - public void drawNpc(EntityLivingBase entity, int x, int y, float zoomed, int rotation) { - GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); - GL11.glColorMaterial(0,1); - GL11.glPushMatrix(); - GL11.glTranslatef((float)(this.guiLeft + x), (float)(this.guiTop + y), 50.0F); - float scale = 1.0F; - if ((double)entity.height > 2.4D) { - scale = 2.0F / entity.height; - } - - GL11.glScalef(-30.0F * scale * zoomed, 30.0F * scale * zoomed, 30.0F * scale * zoomed); - GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F); - RenderHelper.enableStandardItemLighting(); - float f2 = entity.renderYawOffset; - float f3 = entity.rotationYaw; - float f4 = entity.rotationPitch; - float f7 = entity.rotationYawHead; - float f5 = (float)(this.guiLeft + x) - (float)this.mouseX; - float f6 = (float)(this.guiTop + y) - 50.0F * scale * zoomed - (float)this.mouseY; - - GL11.glRotatef(135.0F, 0.0F, 1.0F, 0.0F); - GL11.glRotatef(-135.0F, 0.0F, 1.0F, 0.0F); - GL11.glRotatef(-((float) Math.atan((double) (f6 / 40.0F))) * 20.0F, 1.0F, 0.0F, 0.0F); - entity.renderYawOffset = (float)rotation; - entity.rotationYaw = (float)Math.atan((double)(f5 / 80.0F)) * 40.0F + (float)rotation; - entity.rotationPitch = -((float)Math.atan((double)(f6 / 40.0F))) * 20.0F; - entity.rotationYawHead = entity.rotationYaw; - RenderItem.getInstance().zLevel = 180.0F; - RenderItem.getInstance().doRender(entity, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F); - entity.prevRenderYawOffset = entity.renderYawOffset = f2; - entity.prevRotationYaw = entity.rotationYaw = f3; - entity.prevRotationPitch = entity.rotationPitch = f4; - entity.prevRotationYawHead = entity.rotationYawHead = f7; - - GL11.glPopMatrix(); - RenderHelper.disableStandardItemLighting(); - } - - public void openLink(String link) { - try { - Class oclass = Class.forName("java.awt.Desktop"); - Object object = oclass.getMethod("getDesktop").invoke((Object)null); - oclass.getMethod("browse", URI.class).invoke(object, new URI(link)); - } catch (Throwable var4) { - } - - } -} diff --git a/src/main/java/noppes/npcs/client/gui/script/EventGuiScriptList.java b/src/main/java/noppes/npcs/client/gui/script/EventGuiScriptList.java index 533a7654f..af60efd49 100644 --- a/src/main/java/noppes/npcs/client/gui/script/EventGuiScriptList.java +++ b/src/main/java/noppes/npcs/client/gui/script/EventGuiScriptList.java @@ -9,7 +9,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import net.minecraft.client.gui.GuiButton; -import noppes.npcs.EventScriptContainer; +import noppes.npcs.PlayerScriptContainer; import noppes.npcs.client.gui.util.GuiCustomScroll; import noppes.npcs.client.gui.util.GuiNpcButton; import noppes.npcs.client.gui.util.GuiNpcLabel; @@ -18,10 +18,10 @@ import noppes.npcs.client.gui.util.SubGuiInterface; public class EventGuiScriptList extends SubGuiInterface { private GuiCustomScroll scroll1; private GuiCustomScroll scroll2; - private EventScriptContainer container; + private PlayerScriptContainer container; private List scripts; - public EventGuiScriptList(List scripts, EventScriptContainer container) { + public EventGuiScriptList(List scripts, PlayerScriptContainer container) { this.container = container; this.setBackground("menubg.png"); this.xSize = 346; diff --git a/src/main/java/noppes/npcs/client/gui/script/GuiScriptGlobal.java b/src/main/java/noppes/npcs/client/gui/script/GuiScriptGlobal.java index e554014e4..84ade08f8 100644 --- a/src/main/java/noppes/npcs/client/gui/script/GuiScriptGlobal.java +++ b/src/main/java/noppes/npcs/client/gui/script/GuiScriptGlobal.java @@ -11,7 +11,7 @@ import noppes.npcs.client.gui.util.GuiNPCInterface; import noppes.npcs.client.gui.util.GuiNpcButton; import org.lwjgl.opengl.GL11; -public class GuiScriptGlobal extends EventGuiNPCInterface { +public class GuiScriptGlobal extends GuiNPCInterface { private final ResourceLocation resource = new ResourceLocation("customnpcs", "textures/gui/smallbg.png"); public GuiScriptGlobal() { diff --git a/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java b/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java index c03ffd2a6..f55d0b884 100644 --- a/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java +++ b/src/main/java/noppes/npcs/client/gui/script/GuiScriptInterface.java @@ -18,14 +18,16 @@ import net.minecraft.client.gui.GuiYesNo; import net.minecraft.client.gui.GuiYesNoCallback; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; -import noppes.npcs.EventScriptContainer; +import noppes.npcs.PlayerScriptContainer; import noppes.npcs.NoppesStringUtils; import noppes.npcs.client.NoppesUtil; +import noppes.npcs.client.gui.swing.GuiJTextArea; import noppes.npcs.client.gui.util.*; +import noppes.npcs.constants.EnumScriptType; import noppes.npcs.controllers.IScriptHandler; import noppes.npcs.controllers.ScriptController; -public class GuiScriptInterface extends EventGuiNPCInterface implements GuiYesNoCallback, IGuiData, ITextChangeListener { +public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallback, IGuiData, ITextChangeListener, ICustomScrollListener, IJTextAreaListener { private int activeTab = 0; public IScriptHandler handler; public Map> languages = new HashMap(); @@ -38,7 +40,7 @@ public class GuiScriptInterface extends EventGuiNPCInterface implements GuiYesNo } public void initGui() { - this.xSize = (int)((double)this.width * 0.88D); + //this.xSize = (int)((double)this.width * 0.88D); this.ySize = (int)((double)this.xSize * 0.56D); if((double)this.ySize > (double)this.height * 0.95D) { this.ySize = (int)((double)this.height * 0.95D); @@ -53,7 +55,7 @@ public class GuiScriptInterface extends EventGuiNPCInterface implements GuiYesNo this.addTopButton(top = new GuiMenuTopButton(0, this.guiLeft + 4, this.guiTop - 17, "gui.settings")); for(int ta = 0; ta < this.handler.getScripts().size(); ++ta) { - EventScriptContainer var10000 = (EventScriptContainer)this.handler.getScripts().get(ta); + PlayerScriptContainer var10000 = (PlayerScriptContainer)this.handler.getScripts().get(ta); this.addTopButton(top = new GuiMenuTopButton(ta + 1, top, ta + 1 + "")); } @@ -67,17 +69,43 @@ public class GuiScriptInterface extends EventGuiNPCInterface implements GuiYesNo top = this.getTopButton(0); } + List list = new ArrayList(); + list.add("playerscript.init"); + list.add("playerscript.tick"); + list.add("playerscript.interact"); + list.add("playerscript.damaged"); + list.add("playerscript.killed"); + list.add("playerscript.attack"); + list.add("playerscript.kills"); + list.add("playerscript.chat"); + list.add("playerscript.toss"); + list.add("playerscript.containeropen"); + list.add("playerscript.containerclosed"); + list.add("playerscript.login"); + list.add("playerscript.logout"); + list.add("playerscript.chat"); + list.add("playerscript.damagedentity"); + list.add("playerscript.factionupdate"); + list.add("playerscript.keypressed"); + list.add("playerscript.levelup"); + list.add("playerscript.pickup"); + list.add("playerscript.rangedlaunched"); + //list.add("playerscript.break"); + top.active = true; if(this.activeTab > 0) { - EventScriptContainer var7 = (EventScriptContainer)this.handler.getScripts().get(this.activeTab - 1); - GuiNpcTextArea left = new GuiNpcTextArea(2, this, this.guiLeft + 1 + yoffset, this.guiTop + yoffset, this.xSize - 108 - yoffset, (int)((double)this.ySize * 0.96D) - yoffset * 2, var7 == null?"":var7.script); - this.add(left); - /* - GuiTextArea left = new GuiTextArea(2, this.guiLeft + 1 + yoffset, this.guiTop + yoffset, this.xSize - 108 - yoffset, (int)((double)this.ySize * 0.96D) - yoffset * 2, var7 == null?"":var7.script); - left.enableCodeHighlighting(); - left.setListener(this); - this.add(left); - */ + addLabel(new GuiNpcLabel(0, "script.hooks", guiLeft + 4, guiTop + 5)); + GuiCustomScroll hooks = new GuiCustomScroll(this, 1); + hooks.setSize(68, 198); + hooks.guiLeft = guiLeft + 4; + hooks.guiTop = guiTop + 14; + hooks.setUnsortedList(list); + hooks.selected = getScriptHook(this.handler.getScripts().get(this.activeTab - 1),list); + addScroll(hooks); + + PlayerScriptContainer var7 = (PlayerScriptContainer)this.handler.getScripts().get(this.activeTab - 1); + GuiNpcTextArea left = new GuiNpcTextArea(2, this, this.guiLeft + 72 + yoffset, this.guiTop + yoffset, this.xSize - 180 - yoffset, (int)((double)this.ySize * 0.96D) - yoffset * 2, var7 == null?"":var7.script); + this.addTextField(left); int left1 = this.guiLeft + this.xSize - 104; this.addButton(new GuiNpcButton(102, left1, this.guiTop + yoffset, 60, 20, "gui.clear")); this.addButton(new GuiNpcButton(101, left1 + 61, this.guiTop + yoffset, 60, 20, "gui.paste")); @@ -96,12 +124,7 @@ public class GuiScriptInterface extends EventGuiNPCInterface implements GuiYesNo } else { GuiNpcTextArea var8 = new GuiNpcTextArea(2, this, this.guiLeft + 4 + yoffset, this.guiTop + 6 + yoffset, this.xSize - 160 - yoffset, (int)((float)this.ySize * 0.92F) - yoffset * 2, this.getConsoleText()); var8.enabled = false; - this.add(var8); - /* - GuiTextArea var8 = new GuiTextArea(2, this.guiLeft + 4 + yoffset, this.guiTop + 6 + yoffset, this.xSize - 160 - yoffset, (int)((float)this.ySize * 0.92F) - yoffset * 2, this.getConsoleText()); - var8.enabled = false; - this.add(var8); - */ + this.addTextField(var8); int var9 = this.guiLeft + this.xSize - 150; this.addButton(new GuiNpcButton(100, var9, this.guiTop + 125, 60, 20, "gui.copy")); this.addButton(new GuiNpcButton(102, var9, this.guiTop + 146, 60, 20, "gui.clear")); @@ -124,6 +147,20 @@ public class GuiScriptInterface extends EventGuiNPCInterface implements GuiYesNo this.ySize = 256; } + @Override + public void customScrollClicked(int i, int j, int k, GuiCustomScroll scroll) { + this.handler.getScripts().get(this.activeTab - 1).setType(scroll.getSelected().replace("playerscript.","")); + } + + private int getScriptHook(PlayerScriptContainer playerScriptContainer, List list) { + for(String s : list) { + if (s.replace("playerscript.", "").equals(playerScriptContainer.type)) { + return list.indexOf(s); + } + } + return 0; + } + private String getConsoleText() { Map map = this.handler.getConsoleText(); StringBuilder builder = new StringBuilder(); @@ -185,7 +222,7 @@ public class GuiScriptInterface extends EventGuiNPCInterface implements GuiYesNo } if(guibutton.id == 12) { - this.handler.getScripts().add(new EventScriptContainer(this.handler)); + this.handler.getScripts().add(new PlayerScriptContainer(this.handler)); this.activeTab = this.handler.getScripts().size(); this.initGui(); } @@ -207,17 +244,17 @@ public class GuiScriptInterface extends EventGuiNPCInterface implements GuiYesNo } if(guibutton.id == 100) { - NoppesStringUtils.setClipboardContents(((GuiTextArea)this.get(2)).getText()); + NoppesStringUtils.setClipboardContents((this.getTextField(2)).getText()); } if(guibutton.id == 101) { - ((GuiTextArea)this.get(2)).setText(NoppesStringUtils.getClipboardContents()); + (this.getTextField(2)).setText(NoppesStringUtils.getClipboardContents()); } - EventScriptContainer container; + PlayerScriptContainer container; if(guibutton.id == 102) { if(this.activeTab > 0) { - container = (EventScriptContainer)this.handler.getScripts().get(this.activeTab - 1); + container = (PlayerScriptContainer)this.handler.getScripts().get(this.activeTab - 1); container.script = ""; } else { this.handler.clearConsole(); @@ -244,18 +281,19 @@ public class GuiScriptInterface extends EventGuiNPCInterface implements GuiYesNo } if(guibutton.id == 107) { - container = (EventScriptContainer)this.handler.getScripts().get(this.activeTab - 1); + container = (PlayerScriptContainer)this.handler.getScripts().get(this.activeTab - 1); if(container == null) { - this.handler.getScripts().add(container = new EventScriptContainer(this.handler)); + this.handler.getScripts().add(container = new PlayerScriptContainer(this.handler)); } this.setSubGui(new EventGuiScriptList((List)this.languages.get(this.handler.getLanguage()), container)); } if(guibutton.id == 108) { - container = (EventScriptContainer)this.handler.getScripts().get(this.activeTab - 1); + container = (PlayerScriptContainer)this.handler.getScripts().get(this.activeTab - 1); if(container != null) { this.setScript(); + this.AWTWindow = new GuiJTextArea(container.script).setListener(this); } } @@ -263,12 +301,12 @@ public class GuiScriptInterface extends EventGuiNPCInterface implements GuiYesNo private void setScript() { if(this.activeTab > 0) { - EventScriptContainer container = (EventScriptContainer)this.handler.getScripts().get(this.activeTab - 1); + PlayerScriptContainer container = (PlayerScriptContainer)this.handler.getScripts().get(this.activeTab - 1); if(container == null) { - this.handler.getScripts().add(container = new EventScriptContainer(this.handler)); + this.handler.getScripts().add(container = new PlayerScriptContainer(this.handler)); } - String text = ((GuiTextArea)this.get(2)).getText(); + String text = (this.getTextField(2)).getText(); text = text.replace("\r\n", "\n"); text = text.replace("\r", "\n"); container.script = text; @@ -301,10 +339,18 @@ public class GuiScriptInterface extends EventGuiNPCInterface implements GuiYesNo } public void textUpdate(String text) { - EventScriptContainer container = (EventScriptContainer)this.handler.getScripts().get(this.activeTab - 1); + PlayerScriptContainer container = (PlayerScriptContainer)this.handler.getScripts().get(this.activeTab - 1); if(container != null) { container.script = text; } } + + @Override + public void saveText(String text) { + PlayerScriptContainer container = handler.getScripts().get(activeTab); + if(container != null) + container.script = text; + initGui(); + } } diff --git a/src/main/java/noppes/npcs/client/gui/script/GuiScriptPlayers.java b/src/main/java/noppes/npcs/client/gui/script/GuiScriptPlayers.java index 026d6661b..9e3256f27 100644 --- a/src/main/java/noppes/npcs/client/gui/script/GuiScriptPlayers.java +++ b/src/main/java/noppes/npcs/client/gui/script/GuiScriptPlayers.java @@ -9,10 +9,10 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import noppes.npcs.client.Client; import noppes.npcs.constants.EnumPacketServer; -import noppes.npcs.controllers.data.PlayerScriptData; +import noppes.npcs.controllers.data.PlayerDataScript; public class GuiScriptPlayers extends GuiScriptInterface { - private PlayerScriptData script = new PlayerScriptData((EntityPlayer)null); + private PlayerDataScript script = new PlayerDataScript((EntityPlayer)null); public GuiScriptPlayers() { this.handler = this.script; diff --git a/src/main/java/noppes/npcs/controllers/IScriptHandler.java b/src/main/java/noppes/npcs/controllers/IScriptHandler.java index 0932afb3d..72d9cb003 100644 --- a/src/main/java/noppes/npcs/controllers/IScriptHandler.java +++ b/src/main/java/noppes/npcs/controllers/IScriptHandler.java @@ -8,11 +8,11 @@ package noppes.npcs.controllers; import java.util.List; import java.util.Map; import cpw.mods.fml.common.eventhandler.Event; -import noppes.npcs.EventScriptContainer; +import noppes.npcs.PlayerScriptContainer; import noppes.npcs.constants.EnumScriptType; public interface IScriptHandler { - void runScript(EnumScriptType var1, Event var2); + void callScript(EnumScriptType var1, Event var2, Object... obs); boolean isClient(); @@ -24,7 +24,7 @@ public interface IScriptHandler { void setLanguage(String var1); - List getScripts(); + List getScripts(); String noticeString(); diff --git a/src/main/java/noppes/npcs/controllers/PlayerData.java b/src/main/java/noppes/npcs/controllers/PlayerData.java index 29d4420fd..605a5723e 100644 --- a/src/main/java/noppes/npcs/controllers/PlayerData.java +++ b/src/main/java/noppes/npcs/controllers/PlayerData.java @@ -3,14 +3,14 @@ package noppes.npcs.controllers; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.util.EnumFacing; import net.minecraft.world.World; import net.minecraftforge.common.IExtendedEntityProperties; import noppes.npcs.CustomNpcs; import noppes.npcs.constants.EnumRoleType; -import noppes.npcs.controllers.data.PlayerScriptData; +import noppes.npcs.controllers.data.PlayerDataScript; import noppes.npcs.entity.EntityCustomNpc; import noppes.npcs.entity.EntityNPCInterface; +import noppes.npcs.entity.data.DataTimers; import noppes.npcs.roles.RoleCompanion; public class PlayerData implements IExtendedEntityProperties{ @@ -21,7 +21,9 @@ public class PlayerData implements IExtendedEntityProperties{ public PlayerFactionData factionData = new PlayerFactionData(); public PlayerItemGiverData itemgiverData = new PlayerItemGiverData(); public PlayerMailData mailData = new PlayerMailData(); - public PlayerScriptData scriptData; + public PlayerDataScript scriptData; + public int playerLevel = 0; + public DataTimers timers = new DataTimers(this); public EntityNPCInterface editingNpc; public NBTTagCompound cloned; @@ -55,8 +57,8 @@ public class PlayerData implements IExtendedEntityProperties{ transportData.loadNBTData(data); factionData.loadNBTData(data); itemgiverData.loadNBTData(data); - mailData.loadNBTData(data); - + mailData.loadNBTData(data); + timers.readFromNBT(data); if(player != null){ playername = player.getCommandSenderName(); @@ -91,7 +93,8 @@ public class PlayerData implements IExtendedEntityProperties{ factionData.saveNBTData(compound); itemgiverData.saveNBTData(compound); mailData.saveNBTData(compound); - + timers.writeToNBT(compound); + compound.setString("PlayerName", playername); compound.setString("UUID", uuid); compound.setInteger("PlayerCompanionId", companionID); @@ -149,7 +152,7 @@ public class PlayerData implements IExtendedEntityProperties{ PlayerData data = new PlayerData(); if(data.player == null) { data.player = player; - data.scriptData = new PlayerScriptData(player); + data.scriptData = new PlayerDataScript(player); NBTTagCompound compound = PlayerDataController.instance.loadPlayerDataOld(player.getCommandSenderName()); data.setNBT(compound); diff --git a/src/main/java/noppes/npcs/controllers/ScriptController.java b/src/main/java/noppes/npcs/controllers/ScriptController.java index ae61d1043..b426ac053 100644 --- a/src/main/java/noppes/npcs/controllers/ScriptController.java +++ b/src/main/java/noppes/npcs/controllers/ScriptController.java @@ -28,7 +28,8 @@ import net.minecraft.server.MinecraftServer; import net.minecraftforge.event.world.WorldEvent; import noppes.npcs.CustomNpcs; import noppes.npcs.LogWriter; -import noppes.npcs.controllers.data.PlayerScriptData; +import noppes.npcs.controllers.data.ForgeDataScript; +import noppes.npcs.controllers.data.PlayerDataScript; import noppes.npcs.entity.EntityNPCInterface; import noppes.npcs.scripted.ScriptAnimal; import noppes.npcs.scripted.ScriptEntity; @@ -56,9 +57,11 @@ public class ScriptController { private boolean loaded = false; public boolean shouldSave = false; - public PlayerScriptData playerScripts = new PlayerScriptData((EntityPlayer)null); + public PlayerDataScript playerScripts = new PlayerDataScript((EntityPlayer)null); public long lastPlayerUpdate = 0L; + public ForgeDataScript forgeScripts = new ForgeDataScript(); + public ScriptController(){ loaded = false; Instance = this; @@ -74,8 +77,44 @@ public class ScriptController { } } + private File forgeScriptsFile() { + return new File(this.dir, "forge_scripts.json"); + } + + public boolean loadForgeScripts() { + this.forgeScripts.clear(); + File file = this.forgeScriptsFile(); + + try { + if(!file.exists()) { + return false; + } else { + this.forgeScripts.readFromNBT(NBTJsonUtil.LoadFile(file)); + return true; + } + } catch (Exception var3) { + LogWriter.error("Error loading: " + file.getAbsolutePath(), var3); + return false; + } + } + + public void setForgeScripts(NBTTagCompound compound) { + this.forgeScripts.readFromNBT(compound); + File file = this.forgeScriptsFile(); + + try { + NBTJsonUtil.SaveFile(file, compound); + this.forgeScripts.lastInited = -1L; + } catch (IOException var4) { + var4.printStackTrace(); + } catch (JsonException var5) { + var5.printStackTrace(); + } + + } + private File playerScriptsFile() { - return new File(this.dir, "player_scripts.json"); + return new File(dir, "player_scripts.json"); } public boolean loadPlayerScripts() { @@ -87,6 +126,7 @@ public class ScriptController { return false; } else { this.playerScripts.readFromNBT(NBTJsonUtil.LoadFile(file)); + shouldSave = false; return true; } } catch (Exception var3) { @@ -107,7 +147,6 @@ public class ScriptController { } catch (JsonException var5) { var5.printStackTrace(); } - } private void loadCategories(){ diff --git a/src/main/java/noppes/npcs/controllers/data/ForgeDataScript.java b/src/main/java/noppes/npcs/controllers/data/ForgeDataScript.java new file mode 100644 index 000000000..d9bec4c3f --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/data/ForgeDataScript.java @@ -0,0 +1,179 @@ +// +// Source code recreated from a .class file by IntelliJ IDEA +// (powered by Fernflower decompiler) +// + +package noppes.npcs.controllers.data; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.Map.Entry; + +import cpw.mods.fml.common.eventhandler.Event; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.Entity; +import net.minecraft.nbt.NBTTagCompound; +import noppes.npcs.EventHooks; +import noppes.npcs.PlayerScriptContainer; +import noppes.npcs.NBTTags; +import noppes.npcs.constants.EnumScriptType; +import noppes.npcs.controllers.IScriptHandler; +import noppes.npcs.controllers.ScriptController; + +import javax.script.ScriptEngine; + +public class ForgeDataScript implements IScriptHandler { + private List scripts = new ArrayList(); + private String scriptLanguage = "ECMAScript"; + public long lastInited = -1L; + public boolean hadInteract = true; + private boolean enabled = false; + + public ForgeDataScript() { + } + + public void clear() { + this.scripts = new ArrayList(); + } + + public void readFromNBT(NBTTagCompound compound) { + this.scripts = NBTTags.GetScript(compound.getTagList("Scripts", 10), this); + this.scriptLanguage = compound.getString("ScriptLanguage"); + this.enabled = compound.getBoolean("ScriptEnabled"); + } + + public NBTTagCompound writeToNBT(NBTTagCompound compound) { + compound.setTag("Scripts", NBTTags.NBTScript(this.scripts)); + compound.setString("ScriptLanguage", this.scriptLanguage); + compound.setBoolean("ScriptEnabled", this.enabled); + return compound; + } + + public void callScript(EnumScriptType type, Event event, Object... obs) { + if(this.isEnabled()) { +// CustomNpcs.getServer().func_152344_a(() -> { + Minecraft.getMinecraft().func_152344_a(() -> { + if (ScriptController.Instance.lastLoaded > this.lastInited) { + this.lastInited = ScriptController.Instance.lastLoaded; + if (!type.equals("init")) { + EventHooks.onForgeInit(this); + } + } + + Iterator var3 = this.scripts.iterator(); + + while (var3.hasNext()) { + PlayerScriptContainer script = (PlayerScriptContainer) var3.next(); + if(script.type != type.function) + continue; + + script.setEngine(scriptLanguage); + if(script.engine == null) + return; + for(int i = 0; i + 1 < obs.length; i += 2){ + Object ob = obs[i + 1]; + if(ob instanceof Entity) + ob = ScriptController.Instance.getScriptForEntity((Entity)ob); + script.engine.put(obs[i].toString(), ob); + } + ScriptEngine engine = script.engine; + + //script.run(EnumScriptType.valueOf(type), event); + script.run(engine); + } + }); + } + } + + public void callScript(String type, Event event) { + if(this.isEnabled()) { +// CustomNpcs.getServer().func_152344_a(() -> { + Minecraft.getMinecraft().func_152344_a(() -> { + if (ScriptController.Instance.lastLoaded > this.lastInited) { + this.lastInited = ScriptController.Instance.lastLoaded; + if (!type.equals("init")) { + EventHooks.onForgeInit(this); + } + } + + Iterator var3 = this.scripts.iterator(); + + while (var3.hasNext()) { + PlayerScriptContainer script = (PlayerScriptContainer) var3.next(); + //script.run(EnumScriptType.valueOf(type), event); + + script.setEngine(scriptLanguage); + if(script.engine == null) + return; + ScriptEngine engine = script.engine; + + script.run(engine); + } + }); + } + } + + public boolean isEnabled() { + return this.enabled && ScriptController.HasStart && this.scripts.size() > 0; + } + + public boolean isClient() { + return false; + } + + public boolean getEnabled() { + return this.enabled; + } + + public void setEnabled(boolean bo) { + this.enabled = bo; + } + + public String getLanguage() { + return this.scriptLanguage; + } + + public void setLanguage(String lang) { + this.scriptLanguage = lang; + } + + public List getScripts() { + return this.scripts; + } + + public String noticeString() { + return "ForgeScript"; + } + + public Map getConsoleText() { + TreeMap map = new TreeMap(); + int tab = 0; + Iterator var3 = this.getScripts().iterator(); + + while(var3.hasNext()) { + PlayerScriptContainer script = (PlayerScriptContainer)var3.next(); + ++tab; + Iterator var5 = script.console.entrySet().iterator(); + + while(var5.hasNext()) { + Entry entry = (Entry)var5.next(); + map.put(entry.getKey(), " tab " + tab + ":\n" + (String)entry.getValue()); + } + } + + return map; + } + + public void clearConsole() { + Iterator var1 = this.getScripts().iterator(); + + while(var1.hasNext()) { + PlayerScriptContainer script = (PlayerScriptContainer)var1.next(); + script.console.clear(); + } + + } +} diff --git a/src/main/java/noppes/npcs/controllers/data/PlayerScriptData.java b/src/main/java/noppes/npcs/controllers/data/PlayerDataScript.java similarity index 66% rename from src/main/java/noppes/npcs/controllers/data/PlayerScriptData.java rename to src/main/java/noppes/npcs/controllers/data/PlayerDataScript.java index dbc26740e..e648f8b15 100644 --- a/src/main/java/noppes/npcs/controllers/data/PlayerScriptData.java +++ b/src/main/java/noppes/npcs/controllers/data/PlayerDataScript.java @@ -10,23 +10,32 @@ import java.util.*; import java.util.Map.Entry; import com.google.common.base.Preconditions; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.math.BlockPos; import cpw.mods.fml.common.eventhandler.Event; +import net.minecraft.world.WorldServer; import noppes.npcs.EventHooks; -import noppes.npcs.EventScriptContainer; +import noppes.npcs.PlayerScriptContainer; import noppes.npcs.NBTTags; import noppes.npcs.scripted.ScriptPlayer; import noppes.npcs.constants.EnumScriptType; import noppes.npcs.controllers.IScriptHandler; import noppes.npcs.controllers.ScriptController; +import noppes.npcs.scripted.ScriptWorld; +import noppes.npcs.scripted.constants.EntityType; +import noppes.npcs.scripted.constants.JobType; +import noppes.npcs.scripted.constants.RoleType; +import noppes.npcs.scripted.event.PlayerEvent; import javax.annotation.CheckForNull; +import javax.script.ScriptEngine; -public class PlayerScriptData implements IScriptHandler { - public List scripts = new ArrayList(); - private String scriptLanguage = "ECMAScript"; +public class PlayerDataScript implements IScriptHandler { + public List scripts = new ArrayList(); + public String scriptLanguage = "ECMAScript"; private EntityPlayer player; private ScriptPlayer playerAPI; private long lastPlayerUpdate = 0L; @@ -36,8 +45,20 @@ public class PlayerScriptData implements IScriptHandler { private static Map console = new TreeMap(); private static List errored = new ArrayList(); - public PlayerScriptData(EntityPlayer player) { - this.player = player; + public ScriptPlayer dummyPlayer; + public ScriptWorld dummyWorld; + private final static EntityType entities = new EntityType(); + private final static JobType jobs = new JobType(); + private final static RoleType roles = new RoleType(); + + public PlayerDataScript(EntityPlayer player) { + if(player != null) { + this.player = player; + if (player instanceof EntityPlayer) + dummyPlayer = new ScriptPlayer((EntityPlayerMP) player); + if (player.worldObj instanceof WorldServer) + dummyWorld = new ScriptWorld((WorldServer) player.worldObj); + } } public void clear() { @@ -61,9 +82,9 @@ public class PlayerScriptData implements IScriptHandler { return compound; } - public void runScript(EnumScriptType type, Event event) { + public void callScript(EnumScriptType type, Event event, Object... obs) { if(this.isEnabled()) { - EventScriptContainer script; + PlayerScriptContainer script; if(ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastPlayerUpdate > this.lastPlayerUpdate) { this.lastInited = ScriptController.Instance.lastLoaded; errored.clear(); @@ -72,9 +93,10 @@ public class PlayerScriptData implements IScriptHandler { Iterator i = ScriptController.Instance.playerScripts.scripts.iterator(); while(i.hasNext()) { - script = ( EventScriptContainer)i.next(); - EventScriptContainer s = new EventScriptContainer(this); + script = (PlayerScriptContainer)i.next(); + PlayerScriptContainer s = new PlayerScriptContainer(this); s.readFromNBT(script.writeToNBT(new NBTTagCompound())); + this.scripts.add(s); } } @@ -86,23 +108,34 @@ public class PlayerScriptData implements IScriptHandler { } for(int var7 = 0; var7 < this.scripts.size(); ++var7) { - script = (EventScriptContainer)this.scripts.get(var7); + script = (PlayerScriptContainer)this.scripts.get(var7); + + if(!script.type.equals(type.function)) + continue; + if(!errored.contains(Integer.valueOf(var7))) { - script.run(type, event); - if(script.errored) { - errored.add(Integer.valueOf(var7)); + if(script == null || script.errored || !script.hasCode()) + return; + script.setEngine(scriptLanguage); + if(script.engine == null) + return; + for(int i = 0; i + 1 < obs.length; i += 2){ + Object ob = obs[i + 1]; + if(ob instanceof Entity) + ob = ScriptController.Instance.getScriptForEntity((Entity)ob); + script.engine.put(obs[i].toString(), ob); } - Iterator var8 = script.console.entrySet().iterator(); - - while(var8.hasNext()) { - Entry entry = (Entry)var8.next(); - if(!console.containsKey(entry.getKey())) { - console.put((Long)entry.getKey(), " tab " + (var7 + 1) + ":\n" + (String)entry.getValue()); - } - } - - script.console.clear(); + ScriptEngine engine = script.engine; + engine.put("player", dummyPlayer); + engine.put("world", dummyWorld); + PlayerEvent result = (PlayerEvent) engine.get("event"); + if(result == null) + engine.put("event", result = new PlayerEvent(this.getPlayer())); + engine.put("EntityType", entities); + engine.put("RoleType", roles); + engine.put("JobType", jobs); + script.run(engine); } } @@ -133,83 +166,83 @@ public class PlayerScriptData implements IScriptHandler { this.scriptLanguage = lang; } - public List getScripts() { + public List getScripts() { return this.scripts; } public static final class ToStringHelper { private final String className; - private final PlayerScriptData.ToStringHelper.ValueHolder holderHead; - private PlayerScriptData.ToStringHelper.ValueHolder holderTail; + private final PlayerDataScript.ToStringHelper.ValueHolder holderHead; + private PlayerDataScript.ToStringHelper.ValueHolder holderTail; private boolean omitNullValues; private boolean omitEmptyValues; private ToStringHelper(String className) { - this.holderHead = new PlayerScriptData.ToStringHelper.ValueHolder(); + this.holderHead = new PlayerDataScript.ToStringHelper.ValueHolder(); this.holderTail = this.holderHead; this.omitNullValues = false; this.omitEmptyValues = false; this.className = (String) Preconditions.checkNotNull(className); } - public PlayerScriptData.ToStringHelper omitNullValues() { + public PlayerDataScript.ToStringHelper omitNullValues() { this.omitNullValues = true; return this; } - public PlayerScriptData.ToStringHelper add(String name, @CheckForNull Object value) { + public PlayerDataScript.ToStringHelper add(String name, @CheckForNull Object value) { return this.addHolder(name, value); } - public PlayerScriptData.ToStringHelper add(String name, boolean value) { + public PlayerDataScript.ToStringHelper add(String name, boolean value) { return this.addUnconditionalHolder(name, String.valueOf(value)); } - public PlayerScriptData.ToStringHelper add(String name, char value) { + public PlayerDataScript.ToStringHelper add(String name, char value) { return this.addUnconditionalHolder(name, String.valueOf(value)); } - public PlayerScriptData.ToStringHelper add(String name, double value) { + public PlayerDataScript.ToStringHelper add(String name, double value) { return this.addUnconditionalHolder(name, String.valueOf(value)); } - public PlayerScriptData.ToStringHelper add(String name, float value) { + public PlayerDataScript.ToStringHelper add(String name, float value) { return this.addUnconditionalHolder(name, String.valueOf(value)); } - public PlayerScriptData.ToStringHelper add(String name, int value) { + public PlayerDataScript.ToStringHelper add(String name, int value) { return this.addUnconditionalHolder(name, String.valueOf(value)); } - public PlayerScriptData.ToStringHelper add(String name, long value) { + public PlayerDataScript.ToStringHelper add(String name, long value) { return this.addUnconditionalHolder(name, String.valueOf(value)); } - public PlayerScriptData.ToStringHelper addValue(@CheckForNull Object value) { + public PlayerDataScript.ToStringHelper addValue(@CheckForNull Object value) { return this.addHolder(value); } - public PlayerScriptData.ToStringHelper addValue(boolean value) { + public PlayerDataScript.ToStringHelper addValue(boolean value) { return this.addUnconditionalHolder(String.valueOf(value)); } - public PlayerScriptData.ToStringHelper addValue(char value) { + public PlayerDataScript.ToStringHelper addValue(char value) { return this.addUnconditionalHolder(String.valueOf(value)); } - public PlayerScriptData.ToStringHelper addValue(double value) { + public PlayerDataScript.ToStringHelper addValue(double value) { return this.addUnconditionalHolder(String.valueOf(value)); } - public PlayerScriptData.ToStringHelper addValue(float value) { + public PlayerDataScript.ToStringHelper addValue(float value) { return this.addUnconditionalHolder(String.valueOf(value)); } - public PlayerScriptData.ToStringHelper addValue(int value) { + public PlayerDataScript.ToStringHelper addValue(int value) { return this.addUnconditionalHolder(String.valueOf(value)); } - public PlayerScriptData.ToStringHelper addValue(long value) { + public PlayerDataScript.ToStringHelper addValue(long value) { return this.addUnconditionalHolder(String.valueOf(value)); } @@ -243,9 +276,9 @@ public class PlayerScriptData implements IScriptHandler { String nextSeparator = ""; StringBuilder builder = (new StringBuilder(32)).append(this.className).append('{'); - for(PlayerScriptData.ToStringHelper.ValueHolder valueHolder = this.holderHead.next; valueHolder != null; valueHolder = valueHolder.next) { + for(PlayerDataScript.ToStringHelper.ValueHolder valueHolder = this.holderHead.next; valueHolder != null; valueHolder = valueHolder.next) { Object value = valueHolder.value; - if (!(valueHolder instanceof PlayerScriptData.ToStringHelper.UnconditionalValueHolder)) { + if (!(valueHolder instanceof PlayerDataScript.ToStringHelper.UnconditionalValueHolder)) { if (value == null) { if (omitNullValuesSnapshot) { continue; @@ -273,45 +306,45 @@ public class PlayerScriptData implements IScriptHandler { return builder.append('}').toString(); } - private PlayerScriptData.ToStringHelper.ValueHolder addHolder() { - PlayerScriptData.ToStringHelper.ValueHolder valueHolder = new PlayerScriptData.ToStringHelper.ValueHolder(); + private PlayerDataScript.ToStringHelper.ValueHolder addHolder() { + PlayerDataScript.ToStringHelper.ValueHolder valueHolder = new PlayerDataScript.ToStringHelper.ValueHolder(); this.holderTail = this.holderTail.next = valueHolder; return valueHolder; } - private PlayerScriptData.ToStringHelper addHolder(@CheckForNull Object value) { - PlayerScriptData.ToStringHelper.ValueHolder valueHolder = this.addHolder(); + private PlayerDataScript.ToStringHelper addHolder(@CheckForNull Object value) { + PlayerDataScript.ToStringHelper.ValueHolder valueHolder = this.addHolder(); valueHolder.value = value; return this; } - private PlayerScriptData.ToStringHelper addHolder(String name, @CheckForNull Object value) { - PlayerScriptData.ToStringHelper.ValueHolder valueHolder = this.addHolder(); + private PlayerDataScript.ToStringHelper addHolder(String name, @CheckForNull Object value) { + PlayerDataScript.ToStringHelper.ValueHolder valueHolder = this.addHolder(); valueHolder.value = value; valueHolder.name = (String)Preconditions.checkNotNull(name); return this; } - private PlayerScriptData.ToStringHelper.UnconditionalValueHolder addUnconditionalHolder() { - PlayerScriptData.ToStringHelper.UnconditionalValueHolder valueHolder = new PlayerScriptData.ToStringHelper.UnconditionalValueHolder(); + private PlayerDataScript.ToStringHelper.UnconditionalValueHolder addUnconditionalHolder() { + PlayerDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = new PlayerDataScript.ToStringHelper.UnconditionalValueHolder(); this.holderTail = this.holderTail.next = valueHolder; return valueHolder; } - private PlayerScriptData.ToStringHelper addUnconditionalHolder(Object value) { - PlayerScriptData.ToStringHelper.UnconditionalValueHolder valueHolder = this.addUnconditionalHolder(); + private PlayerDataScript.ToStringHelper addUnconditionalHolder(Object value) { + PlayerDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = this.addUnconditionalHolder(); valueHolder.value = value; return this; } - private PlayerScriptData.ToStringHelper addUnconditionalHolder(String name, Object value) { - PlayerScriptData.ToStringHelper.UnconditionalValueHolder valueHolder = this.addUnconditionalHolder(); + private PlayerDataScript.ToStringHelper addUnconditionalHolder(String name, Object value) { + PlayerDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = this.addUnconditionalHolder(); valueHolder.value = value; valueHolder.name = (String)Preconditions.checkNotNull(name); return this; } - private static final class UnconditionalValueHolder extends PlayerScriptData.ToStringHelper.ValueHolder { + private static final class UnconditionalValueHolder extends PlayerDataScript.ToStringHelper.ValueHolder { private UnconditionalValueHolder() { super(); } @@ -323,15 +356,15 @@ public class PlayerScriptData implements IScriptHandler { @CheckForNull Object value; @CheckForNull - PlayerScriptData.ToStringHelper.ValueHolder next; + PlayerDataScript.ToStringHelper.ValueHolder next; private ValueHolder() { } } } - public static PlayerScriptData.ToStringHelper toStringHelper(Object self) { - return new PlayerScriptData.ToStringHelper(self.getClass().getSimpleName()); + public static PlayerDataScript.ToStringHelper toStringHelper(Object self) { + return new PlayerDataScript.ToStringHelper(self.getClass().getSimpleName()); } public String noticeString() { @@ -339,7 +372,7 @@ public class PlayerScriptData implements IScriptHandler { return "Global script"; } else { BlockPos pos = new BlockPos(this.player); - return PlayerScriptData.toStringHelper(this.player).add("x", pos.getX()).add("y", pos.getY()).add("z", pos.getZ()).toString(); + return PlayerDataScript.toStringHelper(this.player).add("x", pos.getX()).add("y", pos.getY()).add("z", pos.getZ()).toString(); } } diff --git a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java index 2108e317e..2eb18c6f5 100644 --- a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java +++ b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java @@ -106,7 +106,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti public ICustomNpc wrappedNPC; private static final GameProfileAlt chateventProfile = new GameProfileAlt(); - private static FakePlayer chateventPlayer; + public static FakePlayer chateventPlayer; public DataDisplay display; public DataStats stats; public DataAI ai; diff --git a/src/main/java/noppes/npcs/entity/data/DataTimers.java b/src/main/java/noppes/npcs/entity/data/DataTimers.java new file mode 100644 index 000000000..bdad6f419 --- /dev/null +++ b/src/main/java/noppes/npcs/entity/data/DataTimers.java @@ -0,0 +1,130 @@ +// +// Source code recreated from a .class file by IntelliJ IDEA +// (powered by Fernflower decompiler) +// + +package noppes.npcs.entity.data; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import noppes.npcs.EventHooks; +import noppes.npcs.scripted.ITimers; +import noppes.npcs.controllers.PlayerData; +import noppes.npcs.entity.EntityNPCInterface; +import noppes.npcs.scripted.CustomNPCsException; + +public class DataTimers implements ITimers { + private Object parent; + private Map timers = new HashMap(); + + public DataTimers(Object parent) { + this.parent = parent; + } + + public void start(int id, int ticks, boolean repeat) { + if(this.timers.containsKey(Integer.valueOf(id))) { + throw new CustomNPCsException("There is already a timer with id: " + id, new Object[0]); + } else { + this.timers.put(Integer.valueOf(id), new DataTimers.Timer(id, ticks, repeat)); + } + } + + public void forceStart(int id, int ticks, boolean repeat) { + this.timers.put(Integer.valueOf(id), new DataTimers.Timer(id, ticks, repeat)); + } + + public boolean has(int id) { + return this.timers.containsKey(Integer.valueOf(id)); + } + + public boolean stop(int id) { + return this.timers.remove(Integer.valueOf(id)) != null; + } + + public void reset(int id) { + DataTimers.Timer timer = (DataTimers.Timer)this.timers.get(Integer.valueOf(id)); + if(timer == null) { + throw new CustomNPCsException("There is no timer with id: " + id, new Object[0]); + } else { + timer.ticks = 0; + } + } + + public void writeToNBT(NBTTagCompound compound) { + NBTTagList list = new NBTTagList(); + Iterator var3 = this.timers.values().iterator(); + + while(var3.hasNext()) { + DataTimers.Timer timer = (DataTimers.Timer)var3.next(); + NBTTagCompound c = new NBTTagCompound(); + c.setInteger("ID", timer.id); + c.setInteger("TimerTicks", timer.id); + c.setBoolean("Repeat", timer.repeat); + c.setInteger("Ticks", timer.ticks); + list.appendTag(c); + } + + compound.setTag("NpcsTimers", list); + } + + public void readFromNBT(NBTTagCompound compound) { + HashMap timers = new HashMap(); + NBTTagList list = compound.getTagList("NpcsTimers", 10); + + for(int i = 0; i < list.tagCount(); ++i) { + NBTTagCompound c = list.getCompoundTagAt(i); + DataTimers.Timer t = new DataTimers.Timer(c.getInteger("ID"), c.getInteger("TimerTicks"), c.getBoolean("Repeat")); + t.ticks = c.getInteger("Ticks"); + timers.put(Integer.valueOf(t.id), t); + } + + this.timers = timers; + } + + public void update() { + Iterator var1 = (new ArrayList(this.timers.values())).iterator(); + + while(var1.hasNext()) { + DataTimers.Timer timer = (DataTimers.Timer)var1.next(); + timer.update(); + } + + } + + public void clear() { + this.timers = new HashMap(); + } + + class Timer { + public int id; + private boolean repeat; + private int timerTicks; + private int ticks = 0; + + public Timer(int id, int ticks, boolean repeat) { + this.id = id; + this.repeat = repeat; + this.timerTicks = ticks; + this.ticks = ticks; + } + + public void update() { + if(this.ticks-- <= 0) { + if(this.repeat) { + this.ticks = this.timerTicks; + } else { + DataTimers.this.stop(this.id); + } + + Object ob = DataTimers.this.parent; + if(ob instanceof PlayerData) { + EventHooks.onPlayerTimer((PlayerData)ob, this.id); + } + } + } + } +} diff --git a/src/main/java/noppes/npcs/scripted/ITimers.java b/src/main/java/noppes/npcs/scripted/ITimers.java new file mode 100644 index 000000000..24d1de2fe --- /dev/null +++ b/src/main/java/noppes/npcs/scripted/ITimers.java @@ -0,0 +1,20 @@ +// +// Source code recreated from a .class file by IntelliJ IDEA +// (powered by Fernflower decompiler) +// + +package noppes.npcs.scripted; + +public interface ITimers { + void start(int var1, int var2, boolean var3); + + void forceStart(int var1, int var2, boolean var3); + + boolean has(int var1); + + boolean stop(int var1); + + void reset(int var1); + + void clear(); +} diff --git a/src/main/java/noppes/npcs/scripted/NpcAPI.java b/src/main/java/noppes/npcs/scripted/NpcAPI.java index 3bb0e16bf..ef2eaf454 100644 --- a/src/main/java/noppes/npcs/scripted/NpcAPI.java +++ b/src/main/java/noppes/npcs/scripted/NpcAPI.java @@ -62,7 +62,7 @@ public abstract class NpcAPI { return null; } else { try { - Class c = Class.forName("noppes.npcs.api.wrapper.WrapperNpcAPI"); + Class c = Class.forName("noppes.npcs.scripted.wrapper.WrapperNpcAPI"); instance = (NpcAPI) c.getMethod("Instance").invoke((Object) null); } catch (Exception var1) { var1.printStackTrace(); diff --git a/src/main/java/noppes/npcs/scripted/event/CustomNPCsEvent.java b/src/main/java/noppes/npcs/scripted/event/CustomNPCsEvent.java index 52ddb6a94..9aa5c5aa4 100644 --- a/src/main/java/noppes/npcs/scripted/event/CustomNPCsEvent.java +++ b/src/main/java/noppes/npcs/scripted/event/CustomNPCsEvent.java @@ -6,10 +6,10 @@ package noppes.npcs.scripted.event; import cpw.mods.fml.common.eventhandler.Event; -import noppes.npcs.controllers.ScriptController; +import noppes.npcs.scripted.NpcAPI; -public class CustomNPCsEvent extends Event { - public final ScriptController API = ScriptController.Instance; +public class CustomNPCsEvent extends Event{ + public final NpcAPI API = NpcAPI.Instance(); public CustomNPCsEvent() { } diff --git a/src/main/resources/assets/customnpcs/lang/en_US.lang b/src/main/resources/assets/customnpcs/lang/en_US.lang index 7c458469d..0fec0fea7 100644 --- a/src/main/resources/assets/customnpcs/lang/en_US.lang +++ b/src/main/resources/assets/customnpcs/lang/en_US.lang @@ -482,7 +482,7 @@ playerscript.logout=Logout playerscript.pickup=Pickup playerscript.rangedlaunched=Range Launched playerscript.toss=Toss -playerscript.update=Update +playerscript.tick=Update item.npcWand.name=Npc Wand item.npcMobCloner.name=Mob Cloner