mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-18 06:55:03 +00:00
Implemented 1.12 event scripting API.
This commit is contained in:
1 parent
feeef71b5a
commit
bb1da8376d
8 files changed
+174
-87
No files matched your search
@@ -6,7 +6,6 @@
|
||||
package noppes.npcs;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
@@ -32,7 +31,7 @@ import noppes.npcs.scripted.event.PlayerEvent.KeyPressedEvent;
|
||||
import noppes.npcs.scripted.event.PlayerEvent.LoginEvent;
|
||||
import noppes.npcs.scripted.event.PlayerEvent.LogoutEvent;
|
||||
import noppes.npcs.scripted.event.PlayerEvent.PickUpEvent;
|
||||
import noppes.npcs.scripted.event.PlayerEvent.TossEvent;
|
||||
import noppes.npcs.scripted.event.PlayerEvent.DropEvent;
|
||||
import noppes.npcs.scripted.interfaces.IEntity;
|
||||
import noppes.npcs.scripted.interfaces.IItemStack;
|
||||
import noppes.npcs.scripted.interfaces.IWorld;
|
||||
@@ -92,7 +91,7 @@ public class EventHooks {
|
||||
IItemStack[] items = new IItemStack[entityItems.size()];
|
||||
for(int i = 0; i < entityItems.size(); i++){ items[i] = NpcAPI.Instance().getIItemStack(entityItems.get(i).getEntityItem()); }
|
||||
|
||||
TossEvent event = new TossEvent(handler.getPlayer(), items);
|
||||
DropEvent event = new DropEvent(handler.getPlayer(), items);
|
||||
handler.callScript(EnumScriptType.DROP, event,"event", event);
|
||||
return WrapperNpcAPI.EVENT_BUS.post(event);
|
||||
}
|
||||
@@ -103,6 +102,12 @@ public class EventHooks {
|
||||
WrapperNpcAPI.EVENT_BUS.post(event);
|
||||
}
|
||||
|
||||
public static boolean onPlayerToss(PlayerDataScript handler, EntityItem entityItem) {
|
||||
PlayerEvent.TossEvent event = new PlayerEvent.TossEvent(handler.getPlayer(), NpcAPI.Instance().getIItemStack(entityItem.getEntityItem()));
|
||||
handler.callScript(EnumScriptType.TOSS, event,"event", event);
|
||||
return WrapperNpcAPI.EVENT_BUS.post(event);
|
||||
}
|
||||
|
||||
public static boolean onPlayerPickUp(PlayerDataScript handler, EntityItem entityItem) {
|
||||
PickUpEvent event = new PickUpEvent(handler.getPlayer(), new ScriptItemStack(entityItem.getEntityItem()));//NpcAPI.Instance().getIItemStack(entityItem.getEntityItem()));
|
||||
handler.callScript(EnumScriptType.PICKUP, event,"event", event);
|
||||
|
||||
@@ -7,24 +7,26 @@ package noppes.npcs;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.TreeMap;
|
||||
import java.util.*;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.controllers.IScriptHandler;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
|
||||
public class EventScriptContainer {
|
||||
public static EventScriptContainer Current;
|
||||
public String type = "init";
|
||||
//public String type = "init";
|
||||
private static String CurrentType;
|
||||
public String fullscript = "";
|
||||
public String script = "";
|
||||
public TreeMap<Long, String> console = new TreeMap();
|
||||
private HashSet<String> unknownFunctions = new HashSet();
|
||||
public boolean errored = false;
|
||||
public List<String> scripts = new ArrayList();
|
||||
public long lastCreated = 0L;
|
||||
@@ -40,7 +42,7 @@ public class EventScriptContainer {
|
||||
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
this.script = compound.getString("Script");
|
||||
this.type = compound.getString("Type");
|
||||
//this.type = compound.getString("Type");
|
||||
this.console = NBTTags.GetLongStringMap(compound.getTagList("Console", 10));
|
||||
this.scripts = NBTTags.getStringList(compound.getTagList("ScriptList", 10));
|
||||
this.lastCreated = 0L;
|
||||
@@ -48,7 +50,7 @@ public class EventScriptContainer {
|
||||
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
||||
compound.setString("Script", this.script);
|
||||
compound.setString("Type", this.type);
|
||||
//compound.setString("Type", this.type);
|
||||
compound.setTag("Console", NBTTags.NBTLongStringMap(this.console));
|
||||
compound.setTag("ScriptList", NBTTags.nbtStringList(this.scripts));
|
||||
return compound;
|
||||
@@ -74,7 +76,8 @@ public class EventScriptContainer {
|
||||
|
||||
return this.fullscript;
|
||||
}
|
||||
public void run(ScriptEngine engine){
|
||||
|
||||
/*public void run(ScriptEngine engine){
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
engine.getContext().setWriter(pw);
|
||||
@@ -94,10 +97,51 @@ public class EventScriptContainer {
|
||||
appandConsole(e.getMessage());
|
||||
}
|
||||
appandConsole(sw.getBuffer().toString().trim());
|
||||
}*/
|
||||
|
||||
public void run(EnumScriptType type, Event event) {
|
||||
this.run((String)type.function, (Object)event);
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
public void run(String type, Object event) {
|
||||
if (!this.errored && this.hasCode() && !this.unknownFunctions.contains(type)) {
|
||||
this.setEngine(this.handler.getLanguage());
|
||||
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;
|
||||
}
|
||||
|
||||
((Invocable)this.engine).invokeFunction(type, new Object[]{event});
|
||||
} catch (NoSuchMethodException var13) {
|
||||
this.unknownFunctions.add(type);
|
||||
} catch (Throwable var14) {
|
||||
this.errored = true;
|
||||
var14.printStackTrace(pw);
|
||||
} finally {
|
||||
this.appandConsole(sw.getBuffer().toString().trim());
|
||||
pw.close();
|
||||
Current = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void appandConsole(String message) {
|
||||
|
||||
@@ -36,6 +36,7 @@ import net.minecraftforge.event.entity.EntityEvent;
|
||||
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
|
||||
import net.minecraftforge.event.entity.EntityStruckByLightningEvent;
|
||||
import net.minecraftforge.event.entity.PlaySoundAtEntityEvent;
|
||||
import net.minecraftforge.event.entity.item.ItemTossEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingDeathEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingFallEvent;
|
||||
@@ -223,6 +224,14 @@ public class ScriptPlayerEventHandler {
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void invoke(ItemTossEvent event) {
|
||||
if(event.player.worldObj instanceof WorldServer) {
|
||||
PlayerDataScript handler = PlayerData.get(event.player).scriptData;
|
||||
EventHooks.onPlayerToss(handler, event.entityItem);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void invoke(LivingFallEvent event) {
|
||||
if(event.entityLiving.worldObj instanceof WorldServer) {
|
||||
|
||||
@@ -5,28 +5,40 @@
|
||||
|
||||
package noppes.npcs.client.gui.script;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.google.common.reflect.ClassPath;
|
||||
import cpw.mods.fml.common.Loader;
|
||||
import cpw.mods.fml.common.ModContainer;
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent;
|
||||
import cpw.mods.fml.common.network.FMLNetworkEvent;
|
||||
import net.minecraft.client.gui.GuiButton;
|
||||
import net.minecraft.client.gui.GuiConfirmOpenLink;
|
||||
import net.minecraft.client.gui.GuiYesNo;
|
||||
import net.minecraft.client.gui.GuiYesNoCallback;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.EntityEvent;
|
||||
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import noppes.npcs.EventScriptContainer;
|
||||
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.controllers.IScriptHandler;
|
||||
import noppes.npcs.controllers.PixelmonHelper;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
import noppes.npcs.controllers.data.ForgeDataScript;
|
||||
import noppes.npcs.controllers.data.PlayerDataScript;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallback, IGuiData, ITextChangeListener, ICustomScrollListener, IJTextAreaListener, ITextfieldListener {
|
||||
private int activeTab = 0;
|
||||
@@ -80,64 +92,76 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
|
||||
top.active = true;
|
||||
if(this.activeTab > 0) {
|
||||
List<String> hookList = new ArrayList<String>();
|
||||
if(handler instanceof PlayerDataScript) {
|
||||
List<String> hookList = new ArrayList<String>();
|
||||
hookList.add("playerscript.disabled");
|
||||
hookList.add("playerscript.init");
|
||||
hookList.add("playerscript.tick");
|
||||
hookList.add("playerscript.interact");
|
||||
hookList.add("playerscript.damagedEntity");
|
||||
hookList.add("playerscript.damaged");
|
||||
hookList.add("playerscript.kills");
|
||||
hookList.add("playerscript.killed");
|
||||
hookList.add("playerscript.drop");
|
||||
hookList.add("playerscript.respawn");
|
||||
hookList.add("playerscript.break");
|
||||
hookList.add("playerscript.chat");
|
||||
hookList.add("playerscript.login");
|
||||
hookList.add("playerscript.logout");
|
||||
hookList.add("playerscript.keyPressed");
|
||||
hookList.add("playerscript.mouseClicked");
|
||||
hookList.add("playerscript.pickUp");
|
||||
hookList.add("playerscript.pickupXP");
|
||||
hookList.add("playerscript.rangedCharge");
|
||||
hookList.add("playerscript.rangedLaunched");
|
||||
hookList.add("playerscript.timer");
|
||||
hookList.add("playerscript.startItem");
|
||||
hookList.add("playerscript.usingItem");
|
||||
hookList.add("playerscript.stopItem");
|
||||
hookList.add("playerscript.finishItem");
|
||||
hookList.add("playerscript.containerOpen");
|
||||
hookList.add("playerscript.useHoe");
|
||||
hookList.add("playerscript.bonemeal");
|
||||
hookList.add("playerscript.fillBucket");
|
||||
hookList.add("playerscript.jump");
|
||||
hookList.add("playerscript.fall");
|
||||
hookList.add("playerscript.wakeUp");
|
||||
hookList.add("playerscript.sleep");
|
||||
hookList.add("playerscript.playSound");
|
||||
hookList.add("playerscript.lightning");
|
||||
hookList.add("playerscript.changedDim");
|
||||
hookList.add("playerscript.achievement");
|
||||
hookList.add("init");
|
||||
hookList.add("tick");
|
||||
hookList.add("interact");
|
||||
hookList.add("damagedEntity");
|
||||
hookList.add("damaged");
|
||||
hookList.add("kills");
|
||||
hookList.add("killed");
|
||||
hookList.add("drop");
|
||||
hookList.add("respawn");
|
||||
hookList.add("break");
|
||||
hookList.add("chat");
|
||||
hookList.add("login");
|
||||
hookList.add("logout");
|
||||
hookList.add("keyPressed");
|
||||
hookList.add("mouseClicked");
|
||||
hookList.add("toss");
|
||||
hookList.add("pickUp");
|
||||
hookList.add("pickupXP");
|
||||
hookList.add("rangedCharge");
|
||||
hookList.add("rangedLaunched");
|
||||
hookList.add("timer");
|
||||
hookList.add("startItem");
|
||||
hookList.add("usingItem");
|
||||
hookList.add("stopItem");
|
||||
hookList.add("finishItem");
|
||||
hookList.add("containerOpen");
|
||||
hookList.add("useHoe");
|
||||
hookList.add("bonemeal");
|
||||
hookList.add("fillBucket");
|
||||
hookList.add("jump");
|
||||
hookList.add("fall");
|
||||
hookList.add("wakeUp");
|
||||
hookList.add("sleep");
|
||||
hookList.add("playSound");
|
||||
hookList.add("lightning");
|
||||
hookList.add("changedDim");
|
||||
hookList.add("achievement");
|
||||
}
|
||||
else if(handler instanceof ForgeDataScript) {
|
||||
hookList.add("init");
|
||||
|
||||
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(hookList);
|
||||
hooks.selected = getScriptHook(this.handler.getScripts().get(this.activeTab - 1),hookList);
|
||||
addScroll(hooks);
|
||||
}
|
||||
else if(handler instanceof ForgeDataScript){
|
||||
addLabel(new GuiNpcLabel(0, "script.hooks", guiLeft + 4, guiTop + 5));
|
||||
GuiNpcTextField hookField = new GuiNpcTextField(1,this,this.fontRendererObj,guiLeft + 4,guiTop + 14,68,20,"");
|
||||
hookField.setText(this.handler.getScripts().get(this.activeTab - 1).type);
|
||||
addTextField(hookField);
|
||||
ArrayList<ClassPath.ClassInfo> list = new ArrayList();
|
||||
try {
|
||||
list.addAll(ClassPath.from(this.getClass().getClassLoader()).getTopLevelClassesRecursive("net.minecraftforge.event"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
for(ClassPath.ClassInfo classInfo : list){
|
||||
String eventName = classInfo.getName();
|
||||
int i = eventName.lastIndexOf(".");
|
||||
eventName = StringUtils.uncapitalize(eventName.substring(i + 1).replace("$", ""));
|
||||
|
||||
hookList.add(eventName);
|
||||
}
|
||||
}
|
||||
|
||||
addLabel(new GuiNpcLabel(0, "script.hooks", guiLeft - 160, guiTop + 5));
|
||||
GuiCustomScroll hooks = new GuiCustomScroll(this, 1);
|
||||
hooks.allowUserInput = false;
|
||||
hooks.setSize(158, 198);
|
||||
hooks.guiLeft = guiLeft - 160;
|
||||
hooks.guiTop = guiTop + 14;
|
||||
hooks.setUnsortedList(hookList);
|
||||
addScroll(hooks);
|
||||
|
||||
EventScriptContainer var7 = (EventScriptContainer)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);
|
||||
GuiNpcTextArea left = new GuiNpcTextArea(2, this, this.guiLeft + yoffset, this.guiTop + yoffset, this.xSize - 110 - 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"));
|
||||
@@ -182,24 +206,24 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
|
||||
@Override
|
||||
public void customScrollClicked(int i, int j, int k, GuiCustomScroll scroll) {
|
||||
this.handler.getScripts().get(this.activeTab - 1).setType(scroll.getSelected().replace("playerscript.",""));
|
||||
//this.handler.getScripts().get(this.activeTab - 1).setType(scroll.getSelected().replace("playerscript.",""));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unFocused(GuiNpcTextField textfield) {
|
||||
if(textfield.id == 1){
|
||||
/*if(textfield.id == 1){
|
||||
this.handler.getScripts().get(this.activeTab - 1).setType(textfield.getText());
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
private int getScriptHook(EventScriptContainer eventScriptContainer, List<String> list) {
|
||||
/*private int getScriptHook(EventScriptContainer eventScriptContainer, List<String> list) {
|
||||
for(String s : list) {
|
||||
if (s.replace("playerscript.", "").equals(eventScriptContainer.type)) {
|
||||
return list.indexOf(s);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}*/
|
||||
|
||||
private String getConsoleText() {
|
||||
Map<Long, String> map = this.handler.getConsoleText();
|
||||
|
||||
@@ -72,9 +72,6 @@ public class ForgeDataScript implements IScriptHandler {
|
||||
while (var3.hasNext()) {
|
||||
EventScriptContainer script = (EventScriptContainer) var3.next();
|
||||
|
||||
if(!script.type.equals(type))
|
||||
continue;
|
||||
|
||||
script.setEngine(scriptLanguage);
|
||||
if(script.engine == null)
|
||||
return;
|
||||
@@ -86,7 +83,7 @@ public class ForgeDataScript implements IScriptHandler {
|
||||
|
||||
ScriptEngine engine = script.engine;
|
||||
|
||||
script.run(engine);
|
||||
script.run(type, event);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -105,9 +105,6 @@ public class PlayerDataScript implements IScriptHandler {
|
||||
for(int var7 = 0; var7 < this.scripts.size(); ++var7) {
|
||||
script = (EventScriptContainer)this.scripts.get(var7);
|
||||
|
||||
if(!script.type.equals(type.function))
|
||||
continue;
|
||||
|
||||
if(!ScriptController.Instance.playerScripts.errored.contains(Integer.valueOf(var7))) {
|
||||
if(script == null || script.errored || !script.hasCode())
|
||||
return;
|
||||
@@ -128,7 +125,7 @@ public class PlayerDataScript implements IScriptHandler {
|
||||
if(result == null)
|
||||
engine.put("event", result = new PlayerEvent(this.getPlayer()));
|
||||
script.engine.put("API", new WrapperNpcAPI());
|
||||
script.run(engine);
|
||||
script.run(type, event);
|
||||
|
||||
if (script.errored) {
|
||||
ScriptController.Instance.playerScripts.errored.add(var7);
|
||||
|
||||
@@ -249,15 +249,25 @@ public class PlayerEvent extends CustomNPCsEvent {
|
||||
}
|
||||
|
||||
@Cancelable
|
||||
public static class TossEvent extends PlayerEvent {
|
||||
public static class DropEvent extends PlayerEvent {
|
||||
public final IItemStack[] items;
|
||||
|
||||
public TossEvent(IPlayer player, IItemStack[] items) {
|
||||
public DropEvent(IPlayer player, IItemStack[] items) {
|
||||
super(player);
|
||||
this.items = items;
|
||||
}
|
||||
}
|
||||
|
||||
@Cancelable
|
||||
public static class TossEvent extends PlayerEvent {
|
||||
public final IItemStack item;
|
||||
|
||||
public TossEvent(IPlayer player, IItemStack item) {
|
||||
super(player);
|
||||
this.item = item;
|
||||
}
|
||||
}
|
||||
|
||||
@Cancelable
|
||||
public static class AttackEvent extends PlayerEvent {
|
||||
public final int type;
|
||||
|
||||
Reference in new issue
Block a user