2026-01-13 17:34:14 +02:00
|
|
|
package noppes.npcs.controllers.data;
|
|
|
|
|
|
2026-01-14 00:16:58 +02:00
|
|
|
import cpw.mods.fml.common.eventhandler.Event;
|
2026-01-13 17:34:14 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2026-01-14 00:16:58 +02:00
|
|
|
import noppes.npcs.constants.EnumScriptType;
|
2026-01-13 22:16:51 +02:00
|
|
|
import noppes.npcs.controllers.ScriptContainer;
|
2026-01-13 17:34:14 +02:00
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Common interface for script units that can be edited in a script GUI.
|
|
|
|
|
* Implemented by both ScriptContainer (ECMAScript) and JaninoScript (Java/Janino).
|
|
|
|
|
* This allows GuiScriptInterface to work with both script types uniformly.
|
|
|
|
|
*/
|
|
|
|
|
public interface IScriptUnit {
|
|
|
|
|
|
2026-01-13 22:16:51 +02:00
|
|
|
// NBT type identifiers
|
|
|
|
|
String NBT_TYPE_KEY = "ScriptUnitType";
|
|
|
|
|
String TYPE_ECMASCRIPT = "ECMAScript";
|
|
|
|
|
String TYPE_JANINO = "Janino";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an IScriptUnit from NBT data.
|
|
|
|
|
* Uses the "ScriptUnitType" tag to determine which implementation to create.
|
|
|
|
|
*
|
|
|
|
|
* @param compound The NBT data
|
|
|
|
|
* @param handler The script handler (used for ScriptContainer)
|
|
|
|
|
* @return A ScriptContainer or JaninoScript based on the NBT type
|
|
|
|
|
*/
|
|
|
|
|
static IScriptUnit createFromNBT(NBTTagCompound compound, IScriptHandler handler) {
|
|
|
|
|
String type = compound.getString(NBT_TYPE_KEY);
|
|
|
|
|
IScriptUnit unit;
|
|
|
|
|
|
|
|
|
|
if (TYPE_JANINO.equals(type)) {
|
|
|
|
|
// Create appropriate JaninoScript based on handler
|
|
|
|
|
unit = handler.createJaninoScriptUnit();
|
|
|
|
|
if (unit == null) {
|
|
|
|
|
// Fallback to ScriptContainer if handler doesn't support Janino
|
|
|
|
|
unit = new ScriptContainer(handler);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Default to ScriptContainer for ECMAScript or missing type
|
|
|
|
|
unit = new ScriptContainer(handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unit.readFromNBT(compound);
|
|
|
|
|
return unit;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
// ==================== SCRIPT CONTENT ====================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the main script text content.
|
|
|
|
|
*/
|
|
|
|
|
String getScript();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the main script text content.
|
|
|
|
|
*/
|
|
|
|
|
void setScript(String script);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the list of external script file names loaded for this unit.
|
|
|
|
|
*/
|
|
|
|
|
List<String> getExternalScripts();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the list of external script file names to load.
|
|
|
|
|
*/
|
|
|
|
|
void setExternalScripts(List<String> scripts);
|
|
|
|
|
|
|
|
|
|
// ==================== CONSOLE ====================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the console output map (timestamp -> message).
|
|
|
|
|
*/
|
|
|
|
|
TreeMap<Long, String> getConsole();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear all console output.
|
|
|
|
|
*/
|
|
|
|
|
void clearConsole();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Append a message to the console.
|
|
|
|
|
*/
|
|
|
|
|
void appendConsole(String message);
|
|
|
|
|
|
|
|
|
|
// ==================== LANGUAGE ====================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the scripting language for this unit.
|
|
|
|
|
* Returns "ECMAScript", "Java", etc.
|
|
|
|
|
*/
|
|
|
|
|
String getLanguage();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the scripting language for this unit.
|
|
|
|
|
* @param language The language name (e.g., "ECMAScript", "Java")
|
|
|
|
|
*/
|
|
|
|
|
void setLanguage(String language);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if this script unit uses Janino (Java) compilation.
|
|
|
|
|
*/
|
|
|
|
|
default boolean isJanino() {
|
|
|
|
|
return "Java".equals(getLanguage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== HOOK GENERATION ====================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a stub/template for the given hook name.
|
|
|
|
|
* ECMAScript: function hookName(event) { }
|
|
|
|
|
* Janino: full Java method signature with return type
|
|
|
|
|
*
|
|
|
|
|
* @param hookName The hook name to generate a stub for
|
|
|
|
|
* @param hookData Optional data (e.g., Method object for Janino)
|
|
|
|
|
* @return The generated stub string
|
|
|
|
|
*/
|
|
|
|
|
String generateHookStub(String hookName, Object hookData);
|
|
|
|
|
|
|
|
|
|
// ==================== NBT ====================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write this script unit to NBT.
|
|
|
|
|
*/
|
|
|
|
|
NBTTagCompound writeToNBT(NBTTagCompound compound);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read this script unit from NBT.
|
|
|
|
|
*/
|
|
|
|
|
void readFromNBT(NBTTagCompound compound);
|
|
|
|
|
|
|
|
|
|
// ==================== STATE ====================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if this script unit has any code (script or external scripts).
|
|
|
|
|
*/
|
|
|
|
|
boolean hasCode();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if this script unit has encountered an error during execution.
|
|
|
|
|
*/
|
|
|
|
|
boolean hasErrored();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the error state of this script unit.
|
|
|
|
|
*/
|
|
|
|
|
void setErrored(boolean errored);
|
2026-01-14 00:16:58 +02:00
|
|
|
|
|
|
|
|
// ==================== 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);
|
2026-01-13 17:34:14 +02:00
|
|
|
}
|