diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandFilter.java b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandFilter.java index 0afe058b..cb8adccc 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandFilter.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandFilter.java @@ -220,16 +220,44 @@ public class CommandFilter extends CommandBase { } private void initalizeScript() { + try { + // First attempt + initializeEngine(); + } catch (ScriptException e1) { + // It's not a huge deal + printPackageWarning(e1); + + if (!config.getScriptEngineName().equals("rhino")) { + reporter.reportWarning(this, "Falling back to the Rhino engine."); + config.setScriptEngineName("rhino"); + config.saveAll(); + + try { + initializeEngine(); + } catch (ScriptException e2) { + // And again .. + printPackageWarning(e2); + } + } + } + } + + private void printPackageWarning(ScriptException e) { + reporter.reportWarning(this, "Unable to initialize packages for JavaScript engine.", e); + } + + /** + * Initialize the current configured engine. + * @throws ScriptException If we are unable to import packages. + */ + private void initializeEngine() throws ScriptException { ScriptEngineManager manager = new ScriptEngineManager(); - engine = manager.getEngineByName("JavaScript"); + engine = manager.getEngineByName(config.getScriptEngineName()); // Import useful packages - try { - engine.eval("importPackage(org.bukkit);"); - engine.eval("importPackage(com.comphenix.protocol.reflect);"); - } catch (ScriptException e) { - throw new IllegalStateException("Unable to initialize packages for JavaScript engine.", e); - } + engine.eval("importPackage(org.bukkit);"); + engine.eval("importPackage(com.comphenix.protocol.reflect);"); + } /** diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java index 4ce0bf06..d6571e85 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java @@ -41,9 +41,10 @@ class ProtocolConfig { private static final String BACKGROUND_COMPILER_ENABLED = "background compiler"; private static final String DEBUG_MODE_ENABLED = "debug"; - private static final String INJECTION_METHOD = "injection method"; + private static final String SCRIPT_ENGINE_NAME = "script engine"; + private static final String UPDATER_NOTIFY = "notify"; private static final String UPDATER_DOWNLAD = "download"; private static final String UPDATER_DELAY = "delay"; @@ -257,6 +258,24 @@ class ProtocolConfig { updater.set(UPDATER_LAST_TIME, lastTimeSeconds); } + /** + * Retrieve the unique name of the script engine to use for filtering. + * @return Script engine to use. + */ + public String getScriptEngineName() { + return global.getString(SCRIPT_ENGINE_NAME, "JavaScript"); + } + + /** + * Set the unique name of the script engine to use for filtering. + *
+ * This setting will take effect next time ProtocolLib is started. + * @param name - name of the script engine to use. + */ + public void setScriptEngineName(String name) { + global.set(SCRIPT_ENGINE_NAME, name); + } + /** * Retrieve the default injection method. * @return Default method. diff --git a/ProtocolLib/src/main/resources/config.yml b/ProtocolLib/src/main/resources/config.yml index e9185886..1de87503 100644 --- a/ProtocolLib/src/main/resources/config.yml +++ b/ProtocolLib/src/main/resources/config.yml @@ -21,4 +21,7 @@ global: injection method: # Whether or not to enable the filter command - debug: false \ No newline at end of file + debug: false + + # The engine used by the filter command + script engine: JavaScript \ No newline at end of file