mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-18 04:35:03 +00:00
Added run(EnumScriptType, Event) and
run(String, Event) methods to IScriptUnit interface, with JaninoScript delegating to callByHookType(). This provides a cleaner abstraction that supports both ScriptContainer and JaninoScript without type checking. Affected files: - IScriptUnit: Added run() methods to interface - JaninoScript: Implemented run() delegation methods - DataScript, PlayerDataScript, GlobalNPCDataScript, ForgeDataScript - EffectScript, LinkedItemScript, RecipeScript - ScriptCustomItem, TileScripted: Refactored to use polymorphic calls
This commit is contained in:
1 parent
cd60a03544
commit
cc0959dcfe
12 files changed
+156
-52
No files matched your search
@@ -153,9 +153,7 @@ public class DataScript implements IScriptHandler {
|
||||
}
|
||||
|
||||
for (IScriptUnit script : this.eventScripts) {
|
||||
if (script instanceof ScriptContainer) {
|
||||
((ScriptContainer) script).run(type, event);
|
||||
}
|
||||
script.run(type, event);
|
||||
}
|
||||
|
||||
ScriptContainer script = scripts.get(type);
|
||||
|
||||
@@ -8,7 +8,6 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
import noppes.npcs.config.ConfigScript;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
import noppes.npcs.scripted.event.player.PlayerEvent;
|
||||
|
||||
@@ -62,8 +61,9 @@ public class EffectScript implements IScriptHandler {
|
||||
if (!this.isEnabled()) {
|
||||
return;
|
||||
}
|
||||
if (container instanceof ScriptContainer)
|
||||
((ScriptContainer) container).run(s, event);
|
||||
if (container != null) {
|
||||
container.run(s, event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -78,14 +78,9 @@ public class ForgeDataScript implements IScriptHandler {
|
||||
}
|
||||
|
||||
for (IScriptUnit script : this.scripts) {
|
||||
if (script == null || !script.hasCode())
|
||||
if (script == null || script.hasErrored() || !script.hasCode())
|
||||
continue;
|
||||
if (script instanceof ScriptContainer) {
|
||||
ScriptContainer container = (ScriptContainer) script;
|
||||
if (container.errored)
|
||||
continue;
|
||||
container.run(type, event);
|
||||
}
|
||||
script.run(type, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,14 +94,9 @@ public class GlobalNPCDataScript implements IScriptHandler {
|
||||
}
|
||||
|
||||
for (IScriptUnit script : this.scripts) {
|
||||
if (script == null || !script.hasCode())
|
||||
if (script == null || script.hasErrored() || !script.hasCode())
|
||||
continue;
|
||||
if (script instanceof ScriptContainer) {
|
||||
ScriptContainer container = (ScriptContainer) script;
|
||||
if (container.errored)
|
||||
continue;
|
||||
container.run(hookName, event);
|
||||
}
|
||||
script.run(hookName, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package noppes.npcs.controllers.data;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.janino.JaninoScript;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -149,4 +150,26 @@ public interface IScriptUnit {
|
||||
* Set the error state of this script unit.
|
||||
*/
|
||||
void setErrored(boolean errored);
|
||||
|
||||
// ==================== EXECUTION ====================
|
||||
|
||||
/**
|
||||
* Execute this script unit for the given hook type and event.
|
||||
* For ScriptContainer: calls run(EnumScriptType, Event)
|
||||
* For JaninoScript: calls callByHookType(EnumScriptType, Event)
|
||||
*
|
||||
* @param type The script hook type
|
||||
* @param event The event object
|
||||
*/
|
||||
void run(EnumScriptType type, Event event);
|
||||
|
||||
/**
|
||||
* Execute this script unit for the given hook name and event.
|
||||
* For ScriptContainer: calls run(String, Event)
|
||||
* For JaninoScript: converts hookName to EnumScriptType and calls callByHookType
|
||||
*
|
||||
* @param hookName The hook name (e.g., "init", "interact")
|
||||
* @param event The event object
|
||||
*/
|
||||
void run(String hookName, Object event);
|
||||
}
|
||||
@@ -8,7 +8,6 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
import noppes.npcs.config.ConfigScript;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -56,8 +55,9 @@ public class LinkedItemScript implements IScriptHandler {
|
||||
if (!this.isEnabled()) {
|
||||
return;
|
||||
}
|
||||
if (container instanceof ScriptContainer)
|
||||
((ScriptContainer) container).run(s, event);
|
||||
if (container != null) {
|
||||
container.run(s, event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -121,18 +121,15 @@ public class PlayerDataScript implements IScriptHandler {
|
||||
IScriptUnit script = scripts.get(i);
|
||||
if (errored.contains(i))
|
||||
continue;
|
||||
if (script instanceof ScriptContainer) {
|
||||
ScriptContainer container = (ScriptContainer) script;
|
||||
container.run(hookName, event);
|
||||
if (container.errored) {
|
||||
errored.add(i);
|
||||
}
|
||||
for (Entry<Long, String> entry : container.console.entrySet()) {
|
||||
if (!console.containsKey(entry.getKey()))
|
||||
console.put(entry.getKey(), " tab " + (i + 1) + ":\n" + entry.getValue());
|
||||
}
|
||||
container.console.clear();
|
||||
script.run(hookName, event);
|
||||
if (script.hasErrored()) {
|
||||
errored.add(i);
|
||||
}
|
||||
for (Entry<Long, String> entry : script.getConsole().entrySet()) {
|
||||
if (!console.containsKey(entry.getKey()))
|
||||
console.put(entry.getKey(), " tab " + (i + 1) + ":\n" + entry.getValue());
|
||||
}
|
||||
script.clearConsole();
|
||||
}
|
||||
} else {
|
||||
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastPlayerUpdate > this.lastPlayerUpdate) {
|
||||
@@ -151,12 +148,9 @@ public class PlayerDataScript implements IScriptHandler {
|
||||
}
|
||||
|
||||
for (IScriptUnit script : this.scripts) {
|
||||
if (script == null || script.hasErrored()|| !script.hasCode())
|
||||
if (script == null || script.hasErrored() || !script.hasCode())
|
||||
continue;
|
||||
if (script instanceof ScriptContainer) {
|
||||
ScriptContainer container = (ScriptContainer) script;
|
||||
container.run(hookName, event);
|
||||
}
|
||||
script.run(hookName, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
import noppes.npcs.config.ConfigScript;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -53,8 +52,9 @@ public class RecipeScript implements IScriptHandler {
|
||||
public void callScript(String s, Event event) {
|
||||
if (!this.isEnabled())
|
||||
return;
|
||||
if (container instanceof ScriptContainer)
|
||||
((ScriptContainer) container).run(s, event);
|
||||
if (container != null) {
|
||||
container.run(s, event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user