Compare commits

..
1 Commits
Author SHA1 Message Date
luisc d752db8a49 implemented graal js engine 2025-05-30 15:33:48 -05:00
17 changed files with 57 additions and 126 deletions

No files matched your search

+1 -1
View File
@@ -5,7 +5,7 @@ plugins {
id 'idea'
}
version = "1.10.1"
version = "1.10"
group= "kamkeel.customnpc-plus"
archivesBaseName = "CustomNPC-Plus"
+1 -1
View File
@@ -36,7 +36,7 @@
def embedMixin = !project.hasProperty("nomixin");
dependencies {
runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.5.4-GTNH:dev")
runtimeOnlyNonPublishable("curse.maven:Nashorn-374413:2924946")
shadowImplementation(project.files("lib/GraalJS.jar"))
if(embedMixin){
// Normal Jar will include built in Mixins
BIN
View File
Binary file not shown.
+1 -1
Submodule src/api updated: bbbea878fe...5c6b16e75f.
@@ -49,6 +49,9 @@ public final class CustomGuiClosePacket extends AbstractPacket {
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
if (!(player.openContainer instanceof ContainerCustomGui))
return;
NBTTagCompound comp = ByteBufUtils.readNBT(in);
EventHooks.onCustomGuiClose((IPlayer) NpcAPI.Instance().getIEntity(player), (new ScriptGui()).fromNBT(comp));
}
+1 -1
View File
@@ -41,7 +41,7 @@ import noppes.npcs.scripted.NpcAPI;
import java.io.File;
import java.util.Set;
@Mod(modid = "customnpcs", name = "CustomNPC+", version = "1.10.1")
@Mod(modid = "customnpcs", name = "CustomNPC+", version = "1.10")
public class CustomNpcs {
@SidedProxy(clientSide = "noppes.npcs.client.ClientProxy", serverSide = "noppes.npcs.CommonProxy")
@@ -390,11 +390,6 @@ public class EventHooks {
return NpcAPI.EVENT_BUS.post(event);
}
public static boolean onPlayerRightClick(PlayerDataScript handler, PlayerEvent.RightClickEvent event) {
handler.callScript(EnumScriptType.RIGHT_CLICK, event);
return NpcAPI.EVENT_BUS.post(event);
}
public static boolean onProfileChange(PlayerDataScript handler, IPlayer player, IProfile profile, int newSlot, int prevSlot, boolean post) {
PlayerEvent.ProfileEvent.Changed event = new PlayerEvent.ProfileEvent.Changed(player, profile, newSlot, prevSlot, post);
handler.callScript(EnumScriptType.PROFILE_CHANGE, event);
@@ -154,7 +154,6 @@ public class ScriptItemEventHandler {
ItemNpcTool.setColor(held.getTagCompound(), color);
e.setCanceled(true);
return;
}
}
}
@@ -23,7 +23,6 @@ import net.minecraftforge.event.entity.item.ItemTossEvent;
import net.minecraftforge.event.entity.living.*;
import net.minecraftforge.event.entity.player.*;
import net.minecraftforge.event.world.BlockEvent;
import noppes.npcs.api.IWorld;
import noppes.npcs.api.entity.IPlayer;
import noppes.npcs.api.item.IItemCustomizable;
import noppes.npcs.config.ConfigMain;
@@ -35,10 +34,8 @@ import noppes.npcs.controllers.ScriptController;
import noppes.npcs.controllers.data.*;
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.items.ItemNpcTool;
import noppes.npcs.items.ItemScripted;
import noppes.npcs.quests.QuestItem;
import noppes.npcs.scripted.NpcAPI;
import noppes.npcs.scripted.event.ItemEvent;
import static noppes.npcs.config.ConfigMain.TrackedQuestUpdateFrequency;
@@ -126,46 +123,9 @@ public class ScriptPlayerEventHandler {
if (!event.entityPlayer.worldObj.isRemote && event.entityPlayer.worldObj instanceof WorldServer && event.entityPlayer instanceof EntityPlayerMP) {
PlayerDataScript handler = ScriptController.Instance.getPlayerScripts(event.entityPlayer);
IPlayer ip = NoppesUtilServer.getIPlayer(event.entityPlayer);
noppes.npcs.scripted.event.player.PlayerEvent.RightClickEvent rightClickEvent = new noppes.npcs.scripted.event.player.PlayerEvent.RightClickEvent(ip, 1, NpcAPI.Instance().getIEntity(event.target));
boolean rightClick = EventHooks.onPlayerRightClick(handler, rightClickEvent);
noppes.npcs.scripted.event.player.PlayerEvent.InteractEvent ev = new noppes.npcs.scripted.event.player.PlayerEvent.InteractEvent(ip, 1, NpcAPI.Instance().getIEntity(event.target));
boolean interact = EventHooks.onPlayerInteract(handler, ev);
event.setCanceled(rightClick || interact);
}
}
@SubscribeEvent
public void invoke(PlayerInteractEvent event) {
if (event.entityPlayer == null || event.entityPlayer.worldObj == null || event.entityPlayer.worldObj.isRemote || !(event.entityPlayer instanceof EntityPlayerMP))
return;
if(event.action == PlayerInteractEvent.Action.LEFT_CLICK_BLOCK)
return;
IPlayer ip = NoppesUtilServer.getIPlayer(event.entityPlayer);
PlayerDataScript handler = ScriptController.Instance.getPlayerScripts(event.entityPlayer);
PlayerData pd = PlayerData.get(event.entityPlayer);
if (pd == null) {
return;
}
if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR){
if (pd.hadInteract) {
pd.hadInteract = false;
return;
}
noppes.npcs.scripted.event.player.PlayerEvent.RightClickEvent rightClickEvent = new noppes.npcs.scripted.event.player.PlayerEvent.RightClickEvent(ip, 0, null);
event.setCanceled(EventHooks.onPlayerRightClick(handler, rightClickEvent));
} else if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK){
pd.hadInteract = true;
IWorld iw = (IWorld) NpcAPI.Instance().getIWorld(event.world);
Object blockCtx = NpcAPI.Instance().getIBlock(iw, event.x, event.y, event.z);
noppes.npcs.scripted.event.player.PlayerEvent.RightClickEvent rightClickEvent = new noppes.npcs.scripted.event.player.PlayerEvent.RightClickEvent(ip, 2, blockCtx);
event.setCanceled(EventHooks.onPlayerRightClick(handler, rightClickEvent));
IPlayer scriptPlayer = (IPlayer) NpcAPI.Instance().getIEntity(event.entityPlayer);
noppes.npcs.scripted.event.player.PlayerEvent.InteractEvent ev = new noppes.npcs.scripted.event.player.PlayerEvent.InteractEvent(scriptPlayer, 1, NpcAPI.Instance().getIEntity(event.target));
event.setCanceled(EventHooks.onPlayerInteract(handler, ev));
}
}
@@ -7,7 +7,6 @@ import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Facing;
import static noppes.npcs.items.ItemNpcTool.BRUSH_COLOR_TAG;
@@ -19,11 +18,6 @@ public class TileVariant extends TileEntity {
public int variant = 14;
public int rotation;
// Rotation = 0 Couch is Facing North
// Rotation = 1 Couch is Facing East
// Rotation = 2 Couch is Facing South
// Rotation = 3 Couch is Facing West
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
version = compound.getInteger("CNPCVersion");
@@ -93,8 +93,6 @@ public class SubGuiMagic extends SubGuiInterface implements ITextfieldListener,
addLabel(new GuiNpcLabel(50, "magic.interaction", guiLeft + 170, guiTop + 30 + 160 + 5));
interactionField = new GuiNpcTextField(90, this, fontRendererObj, guiLeft + 245, guiTop + 30 + 160, 45, 20, "");
interactionField.setFloatsOnly();
interactionField.setMinMaxDefaultFloat(-Float.MAX_VALUE, Float.MAX_VALUE, 0.0f);
addTextField(interactionField);
if (selectedInteraction != null) {
interactionField.setText(interactionValues.get(selectedInteraction) + "");
@@ -119,7 +119,6 @@ public class GuiNpcAdvanced extends GuiNPCInterface2 implements IGuiData {
@Override
public void setGuiData(NBTTagCompound compound) {
if (compound.hasKey("RoleData")) {
npc.advanced.setRole(compound.getByte("RoleData"));
if (npc.roleInterface != null)
npc.roleInterface.readFromNBT(compound);
@@ -18,7 +18,6 @@ public class GuiScriptPlayers extends GuiScriptInterface {
hookList.add(EnumScriptType.INIT.function);
hookList.add(EnumScriptType.TICK.function);
hookList.add(EnumScriptType.INTERACT.function);
hookList.add(EnumScriptType.RIGHT_CLICK.function);
hookList.add(EnumScriptType.ATTACK.function);
hookList.add(EnumScriptType.ATTACKED.function);
hookList.add(EnumScriptType.DAMAGED_ENTITY.function);
@@ -100,7 +100,7 @@ public class BlockCouchWoolRenderer extends BlockRendererInterface {
setCouchWood(var1.getBlockMetadata());
if (tile.hasCornerLeft) {
GL11.glPushMatrix();
GL11.glRotatef(270, 0, 1, 0);
GL11.glRotatef(90, 0, 1, 0);
modelCouchCorner.CouchBack.render(0.0625F);
GL11.glPopMatrix();
} else if (tile.hasCornerRight)
@@ -119,12 +119,8 @@ public class BlockCouchWoolRenderer extends BlockRendererInterface {
float[] color = ColorUtil.hexToRGB(tile.color);
GL11.glColor3f(color[0], color[1], color[2]);
if (tile.hasCornerLeft){
GL11.glPushMatrix();
GL11.glRotatef(270, 0, 1, 0);
if (tile.hasCornerLeft)
modelCouchCorner.Cussion.render(0.0625F);
GL11.glPopMatrix();
}
else if (tile.hasCornerRight)
modelCouchCorner.Cussion.render(0.0625F);
else if (tile.hasLeft && tile.hasRight)
@@ -1,8 +1,10 @@
package noppes.npcs.controllers;
import com.oracle.truffle.js.scriptengine.GraalJSEngineFactory;
import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import jdk.nashorn.api.scripting.ClassFilter;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
//import jdk.nashorn.api.scripting.ClassFilter;
//import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
@@ -21,6 +23,8 @@ import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.scripted.ScriptWorld;
import noppes.npcs.util.JsonException;
import noppes.npcs.util.NBTJsonUtil;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.HostAccess;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
@@ -31,6 +35,7 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.function.Predicate;
import static noppes.npcs.util.CustomNPCsThreader.customNPCThread;
@@ -39,7 +44,7 @@ public class ScriptController {
public static ScriptController Instance;
public static boolean HasStart = false;
private final ScriptEngineManager manager;
private ScriptEngineFactory nashornFactory;
private ScriptEngineFactory graalFactory;
public Map<String, String> languages = new HashMap<String, String>();
public Map<String, String> scripts = new HashMap<String, String>();
public long lastLoaded = 0;
@@ -64,14 +69,14 @@ public class ScriptController {
LogWriter.info("Script Engines Available:");
try {
this.nashornFactory = new NashornScriptEngineFactory();
LogWriter.info("→ standalone Nashorn loaded");
} catch (NoClassDefFoundError e) {
this.graalFactory = new GraalJSEngineFactory();
LogWriter.info("→ standalone Nashorn loaded: " + this.graalFactory.getEngineName());
} catch (Exception e) {
// fallback to built-in (Java 8–11) — also guarded
try {
ScriptEngine eng = manager.getEngineByName("nashorn");
if (eng != null) {
this.nashornFactory = eng.getFactory();
this.graalFactory = eng.getFactory();
LogWriter.info("→ built-in Nashorn loaded");
}
} catch (Throwable t) {
@@ -298,25 +303,27 @@ public class ScriptController {
private static final List<String> nashornNames = immutableList("nashorn", "Nashorn", "js", "JS", "JavaScript", "javascript", "ECMAScript", "ecmascript");
public ScriptEngine getEngineByName(String language) {
if (nashornNames.contains(language) && this.nashornFactory != null) {
ScriptEngine scriptEngine;
if (nashornNames.contains(language) && this.graalFactory != null) {
ScriptEngine scriptEngine = getGraalJSEngine();
if (scriptEngine != null) return scriptEngine;
if (ConfigScript.EnableBannedClasses) {
try {
ClassFilter filter = s -> {
for (String className : ConfigScript.BannedClasses) {
if (s.compareTo(className) == 0) {
return false;
}
}
return true;
};
NashornScriptEngineFactory nashornScriptEngineFactory = (NashornScriptEngineFactory) this.nashornFactory;
scriptEngine = nashornScriptEngineFactory.getScriptEngine(filter);
// ClassFilter filter = s -> {
// for (String className : ConfigScript.BannedClasses) {
// if (s.compareTo(className) == 0) {
// return false;
// }
// }
// return true;
// };
// NashornScriptEngineFactory nashornScriptEngineFactory = (NashornScriptEngineFactory) this.graalFactory;
// scriptEngine = nashornScriptEngineFactory.getScriptEngine(filter);
} catch (Exception e) {
scriptEngine = this.nashornFactory.getScriptEngine();
scriptEngine = this.graalFactory.getScriptEngine();
}
} else {
scriptEngine = this.nashornFactory.getScriptEngine();
scriptEngine = this.graalFactory.getScriptEngine();
}
scriptEngine.setBindings(this.manager.getBindings(), ScriptContext.GLOBAL_SCOPE);
return scriptEngine;
@@ -324,6 +331,22 @@ public class ScriptController {
return manager.getEngineByName(language);
}
public ScriptEngine getGraalJSEngine() {
Context.Builder polyglotContext = Context.newBuilder("js")
.option("js.nashorn-compat", "true")
.option("js.scripting", "true")
.option("js.syntax-extensions", "true")
.allowAllAccess(true)
.allowNativeAccess(true)
.allowIO(true)
.allowHostAccess(HostAccess.ALL)
.allowHostClassLookup(s -> true);
return GraalJSScriptEngine.create(
((GraalJSEngineFactory)this.graalFactory).getPolyglotEngine(),
polyglotContext);
}
private static List<String> immutableList(String... elements) {
return Collections.unmodifiableList(Arrays.asList(elements));
}
@@ -242,7 +242,7 @@ public class DataScript implements INpcScriptHandler {
@Override
public void callScript(String hookName, Event event) {
try {
EnumScriptType enumScriptType = EnumScriptType.valueOfIgnoreCase(hookName);
EnumScriptType enumScriptType = EnumScriptType.valueOf(hookName);
this.callScript(enumScriptType, event);
} catch (IllegalArgumentException ignored) {
}
@@ -13,16 +13,13 @@ import noppes.npcs.api.IDamageSource;
import noppes.npcs.api.entity.IEntity;
import noppes.npcs.api.entity.IEntityLivingBase;
import noppes.npcs.api.entity.IPlayer;
import noppes.npcs.api.event.IItemEvent;
import noppes.npcs.api.event.IPlayerEvent;
import noppes.npcs.api.handler.data.IPlayerEffect;
import noppes.npcs.api.handler.data.IProfile;
import noppes.npcs.api.item.IItemCustomizable;
import noppes.npcs.api.item.IItemStack;
import noppes.npcs.constants.EnumScriptType;
import noppes.npcs.scripted.NpcAPI;
import noppes.npcs.scripted.event.CustomNPCsEvent;
import noppes.npcs.scripted.event.ItemEvent;
public class PlayerEvent extends CustomNPCsEvent implements IPlayerEvent {
public final IPlayer player;
@@ -653,38 +650,6 @@ public class PlayerEvent extends CustomNPCsEvent implements IPlayerEvent {
}
}
@Cancelable
public static class RightClickEvent extends PlayerEvent implements IPlayerEvent.RightClickEvent {
/**
* 0:air, 1:entity, 2:block
*/
public final int type;
public final Object target;
public RightClickEvent(IPlayer player, int type, Object target) {
super(player);
this.type = type;
this.target = target;
}
public String getHookName() {
return EnumScriptType.RIGHT_CLICK.function;
}
public int getType() {
return type;
}
public Object getTarget() {
return target;
}
public IPlayer getPlayer() {
return player;
}
}
public static class UpdateEvent extends PlayerEvent implements IPlayerEvent.UpdateEvent {
public UpdateEvent(IPlayer player) {
super(player);