2022-03-23 03:54:06 -05:00
|
|
|
package noppes.npcs.controllers.data;
|
|
|
|
|
|
|
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
|
import cpw.mods.fml.common.eventhandler.Event;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
|
import noppes.npcs.NBTTags;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.api.entity.ICustomNpc;
|
2023-01-07 00:21:08 -05:00
|
|
|
import noppes.npcs.config.ConfigScript;
|
2022-03-23 03:54:06 -05:00
|
|
|
import noppes.npcs.constants.EnumScriptType;
|
|
|
|
|
import noppes.npcs.controllers.ScriptContainer;
|
|
|
|
|
import noppes.npcs.controllers.ScriptController;
|
|
|
|
|
import noppes.npcs.entity.EntityNPCInterface;
|
2022-05-11 22:08:31 -05:00
|
|
|
import noppes.npcs.scripted.NpcAPI;
|
2022-03-23 03:54:06 -05:00
|
|
|
|
|
|
|
|
import javax.annotation.CheckForNull;
|
|
|
|
|
import java.lang.reflect.Array;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
2023-03-16 04:27:19 -05:00
|
|
|
public class GlobalNPCDataScript implements IScriptHandler {
|
2022-03-23 03:54:06 -05:00
|
|
|
public List<ScriptContainer> scripts = new ArrayList();
|
|
|
|
|
public String scriptLanguage = "ECMAScript";
|
|
|
|
|
private EntityNPCInterface npc;
|
|
|
|
|
private ICustomNpc npcAPI;
|
|
|
|
|
public long lastInited = -1L;
|
2023-02-21 12:45:48 -06:00
|
|
|
private long lastNpcUpdate = -1L;
|
2022-03-23 03:54:06 -05:00
|
|
|
public boolean enabled = false;
|
|
|
|
|
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript(EntityNPCInterface npc) {
|
2022-03-23 03:54:06 -05:00
|
|
|
if(npc != null) {
|
|
|
|
|
this.npc = npc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void clear() {
|
|
|
|
|
this.scripts = new ArrayList();
|
|
|
|
|
}
|
|
|
|
|
public void readFromNBT(NBTTagCompound compound) {
|
2023-02-19 13:38:43 -06:00
|
|
|
if (compound.hasKey("Scripts")) {
|
|
|
|
|
this.scripts = NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this);
|
|
|
|
|
} else {
|
|
|
|
|
this.scripts = NBTTags.GetScript(compound,this);
|
|
|
|
|
}
|
2022-03-23 03:54:06 -05:00
|
|
|
this.scriptLanguage = compound.getString("ScriptLanguage");
|
2022-05-29 16:01:05 -05:00
|
|
|
if (!ScriptController.Instance.languages.containsKey(scriptLanguage)) {
|
|
|
|
|
if (!ScriptController.Instance.languages.isEmpty()) {
|
|
|
|
|
this.scriptLanguage = (String) ScriptController.Instance.languages.keySet().toArray()[0];
|
|
|
|
|
} else {
|
|
|
|
|
this.scriptLanguage = "ECMAScript";
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-23 03:54:06 -05:00
|
|
|
this.enabled = compound.getBoolean("ScriptEnabled");
|
2023-05-29 23:57:45 -05:00
|
|
|
|
|
|
|
|
for (ScriptContainer container : this.scripts) {
|
|
|
|
|
container.setEngine(this.getLanguage());
|
|
|
|
|
}
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
|
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
2023-02-19 13:38:43 -06:00
|
|
|
compound.setInteger("TotalScripts",this.scripts.size());
|
|
|
|
|
for (int i = 0; i < this.scripts.size(); i++) {
|
|
|
|
|
compound.setTag("Tab"+i,this.scripts.get(i).writeToNBT(new NBTTagCompound()));
|
|
|
|
|
}
|
2022-03-23 03:54:06 -05:00
|
|
|
compound.setString("ScriptLanguage", this.scriptLanguage);
|
|
|
|
|
compound.setBoolean("ScriptEnabled", this.enabled);
|
|
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void callScript(EnumScriptType type, Event event) {
|
|
|
|
|
if (this.isEnabled()) {
|
2023-03-16 04:27:19 -05:00
|
|
|
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastGlobalNpcUpdate > this.lastNpcUpdate) {
|
2022-03-23 03:54:06 -05:00
|
|
|
this.lastInited = ScriptController.Instance.lastLoaded;
|
2023-03-16 04:27:19 -05:00
|
|
|
this.lastNpcUpdate = ScriptController.Instance.lastGlobalNpcUpdate;
|
2023-02-21 12:45:48 -06:00
|
|
|
|
|
|
|
|
for (ScriptContainer script : this.scripts) {
|
|
|
|
|
script.errored = false;
|
|
|
|
|
}
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-23 20:45:04 -06:00
|
|
|
for (ScriptContainer script : this.scripts) {
|
2023-02-21 12:45:48 -06:00
|
|
|
if(script == null || script.errored || !script.hasCode() )
|
|
|
|
|
continue;
|
2023-02-19 15:20:51 -06:00
|
|
|
|
2023-02-21 12:45:48 -06:00
|
|
|
script.run(type, event);
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isEnabled() {
|
2023-01-07 00:21:08 -05:00
|
|
|
return ConfigScript.GlobalNPCScripts && this.enabled && ScriptController.HasStart && this.scripts.size() > 0;
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
|
|
|
|
public boolean isClient() {
|
|
|
|
|
return this.npc.isClientWorld();
|
|
|
|
|
}
|
|
|
|
|
public boolean getEnabled() {
|
2022-12-24 01:46:43 -06:00
|
|
|
return this.enabled;
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
|
|
|
|
public void setEnabled(boolean bo) {
|
|
|
|
|
this.enabled = bo;
|
|
|
|
|
}
|
|
|
|
|
public String getLanguage() {
|
2022-12-24 01:46:43 -06:00
|
|
|
return this.scriptLanguage;
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
|
|
|
|
public void setLanguage(String lang) {
|
|
|
|
|
this.scriptLanguage = lang;
|
|
|
|
|
}
|
2023-02-19 16:22:28 -06:00
|
|
|
public void setScripts(List<ScriptContainer> list) {
|
|
|
|
|
this.scripts = list;
|
|
|
|
|
}
|
2022-03-23 03:54:06 -05:00
|
|
|
public List<ScriptContainer> getScripts() {
|
|
|
|
|
return this.scripts;
|
|
|
|
|
}
|
|
|
|
|
public String noticeString() {
|
|
|
|
|
if(this.npc == null) {
|
|
|
|
|
return "Global script";
|
|
|
|
|
} else {
|
|
|
|
|
BlockPos pos = new BlockPos(this.npc);
|
2023-03-16 04:27:19 -05:00
|
|
|
return GlobalNPCDataScript.toStringHelper(this.npc).add("x", pos.getX()).add("y", pos.getY()).add("z", pos.getZ()).toString();
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public ICustomNpc getNpc() {
|
|
|
|
|
if (this.npcAPI == null) {
|
2022-05-11 22:08:31 -05:00
|
|
|
this.npcAPI = (ICustomNpc) NpcAPI.Instance().getIEntity(this.npc);
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
|
|
|
|
return this.npcAPI;
|
|
|
|
|
}
|
2022-12-23 20:45:04 -06:00
|
|
|
|
2022-03-23 03:54:06 -05:00
|
|
|
public Map<Long, String> getConsoleText() {
|
2023-02-21 12:45:48 -06:00
|
|
|
TreeMap<Long, String> map = new TreeMap<>();
|
|
|
|
|
int tab = 0;
|
|
|
|
|
for (ScriptContainer script : this.getScripts()) {
|
|
|
|
|
++tab;
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<Long, String> longStringEntry : script.console.entrySet()) {
|
|
|
|
|
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + longStringEntry.getValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return map;
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
2022-12-23 20:45:04 -06:00
|
|
|
|
2022-03-23 03:54:06 -05:00
|
|
|
public void clearConsole() {
|
2023-02-21 12:45:48 -06:00
|
|
|
for (ScriptContainer script : this.getScripts()) {
|
|
|
|
|
script.console.clear();
|
|
|
|
|
}
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
2023-02-19 15:20:51 -06:00
|
|
|
|
2022-03-23 03:54:06 -05:00
|
|
|
public static final class ToStringHelper {
|
|
|
|
|
private final String className;
|
2023-03-16 04:27:19 -05:00
|
|
|
private final GlobalNPCDataScript.ToStringHelper.ValueHolder holderHead;
|
|
|
|
|
private GlobalNPCDataScript.ToStringHelper.ValueHolder holderTail;
|
2022-03-23 03:54:06 -05:00
|
|
|
private boolean omitNullValues;
|
|
|
|
|
private boolean omitEmptyValues;
|
|
|
|
|
private ToStringHelper(String className) {
|
2023-03-16 04:27:19 -05:00
|
|
|
this.holderHead = new GlobalNPCDataScript.ToStringHelper.ValueHolder();
|
2022-03-23 03:54:06 -05:00
|
|
|
this.holderTail = this.holderHead;
|
|
|
|
|
this.omitNullValues = false;
|
|
|
|
|
this.omitEmptyValues = false;
|
|
|
|
|
this.className = (String) Preconditions.checkNotNull(className);
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper omitNullValues() {
|
2022-03-23 03:54:06 -05:00
|
|
|
this.omitNullValues = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper add(String name, @CheckForNull Object value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addHolder(name, value);
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper add(String name, boolean value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper add(String name, char value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper add(String name, double value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper add(String name, float value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper add(String name, int value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper add(String name, long value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper addValue(@CheckForNull Object value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addHolder(value);
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper addValue(boolean value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper addValue(char value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper addValue(double value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper addValue(float value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper addValue(int value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public GlobalNPCDataScript.ToStringHelper addValue(long value) {
|
2022-03-23 03:54:06 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
|
|
|
|
private static boolean isEmpty(Object value) {
|
|
|
|
|
if (value instanceof CharSequence) {
|
|
|
|
|
return ((CharSequence)value).length() == 0;
|
|
|
|
|
} else if (value instanceof Collection) {
|
|
|
|
|
return ((Collection)value).isEmpty();
|
|
|
|
|
} else if (value instanceof Map) {
|
|
|
|
|
return ((Map)value).isEmpty();
|
|
|
|
|
} else if (value instanceof Optional) {
|
|
|
|
|
return !((Optional)value).isPresent();
|
|
|
|
|
} else if (value instanceof OptionalInt) {
|
|
|
|
|
return !((OptionalInt)value).isPresent();
|
|
|
|
|
} else if (value instanceof OptionalLong) {
|
|
|
|
|
return !((OptionalLong)value).isPresent();
|
|
|
|
|
} else if (value instanceof OptionalDouble) {
|
|
|
|
|
return !((OptionalDouble)value).isPresent();
|
|
|
|
|
} else if (value instanceof com.google.common.base.Optional) {
|
|
|
|
|
return !((com.google.common.base.Optional)value).isPresent();
|
|
|
|
|
} else if (value.getClass().isArray()) {
|
|
|
|
|
return Array.getLength(value) == 0;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public String toString() {
|
|
|
|
|
boolean omitNullValuesSnapshot = this.omitNullValues;
|
|
|
|
|
boolean omitEmptyValuesSnapshot = this.omitEmptyValues;
|
|
|
|
|
String nextSeparator = "";
|
|
|
|
|
StringBuilder builder = (new StringBuilder(32)).append(this.className).append('{');
|
2023-03-16 04:27:19 -05:00
|
|
|
for(GlobalNPCDataScript.ToStringHelper.ValueHolder valueHolder = this.holderHead.next; valueHolder != null; valueHolder = valueHolder.next) {
|
2022-03-23 03:54:06 -05:00
|
|
|
Object value = valueHolder.value;
|
2023-03-16 04:27:19 -05:00
|
|
|
if (!(valueHolder instanceof GlobalNPCDataScript.ToStringHelper.UnconditionalValueHolder)) {
|
2022-03-23 03:54:06 -05:00
|
|
|
if (value == null) {
|
|
|
|
|
if (omitNullValuesSnapshot) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} else if (omitEmptyValuesSnapshot && isEmpty(value)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
builder.append(nextSeparator);
|
|
|
|
|
nextSeparator = ", ";
|
|
|
|
|
if (valueHolder.name != null) {
|
|
|
|
|
builder.append(valueHolder.name).append('=');
|
|
|
|
|
}
|
|
|
|
|
if (value != null && value.getClass().isArray()) {
|
|
|
|
|
Object[] objectArray = new Object[]{value};
|
|
|
|
|
String arrayString = Arrays.deepToString(objectArray);
|
|
|
|
|
builder.append(arrayString, 1, arrayString.length() - 1);
|
|
|
|
|
} else {
|
|
|
|
|
builder.append(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return builder.append('}').toString();
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
private GlobalNPCDataScript.ToStringHelper.ValueHolder addHolder() {
|
|
|
|
|
GlobalNPCDataScript.ToStringHelper.ValueHolder valueHolder = new GlobalNPCDataScript.ToStringHelper.ValueHolder();
|
2022-03-23 03:54:06 -05:00
|
|
|
this.holderTail = this.holderTail.next = valueHolder;
|
|
|
|
|
return valueHolder;
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
private GlobalNPCDataScript.ToStringHelper addHolder(@CheckForNull Object value) {
|
|
|
|
|
GlobalNPCDataScript.ToStringHelper.ValueHolder valueHolder = this.addHolder();
|
2022-03-23 03:54:06 -05:00
|
|
|
valueHolder.value = value;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
private GlobalNPCDataScript.ToStringHelper addHolder(String name, @CheckForNull Object value) {
|
|
|
|
|
GlobalNPCDataScript.ToStringHelper.ValueHolder valueHolder = this.addHolder();
|
2022-03-23 03:54:06 -05:00
|
|
|
valueHolder.value = value;
|
|
|
|
|
valueHolder.name = (String)Preconditions.checkNotNull(name);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
private GlobalNPCDataScript.ToStringHelper.UnconditionalValueHolder addUnconditionalHolder() {
|
|
|
|
|
GlobalNPCDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = new GlobalNPCDataScript.ToStringHelper.UnconditionalValueHolder();
|
2022-03-23 03:54:06 -05:00
|
|
|
this.holderTail = this.holderTail.next = valueHolder;
|
|
|
|
|
return valueHolder;
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
private GlobalNPCDataScript.ToStringHelper addUnconditionalHolder(Object value) {
|
|
|
|
|
GlobalNPCDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = this.addUnconditionalHolder();
|
2022-03-23 03:54:06 -05:00
|
|
|
valueHolder.value = value;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
private GlobalNPCDataScript.ToStringHelper addUnconditionalHolder(String name, Object value) {
|
|
|
|
|
GlobalNPCDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = this.addUnconditionalHolder();
|
2022-03-23 03:54:06 -05:00
|
|
|
valueHolder.value = value;
|
|
|
|
|
valueHolder.name = (String)Preconditions.checkNotNull(name);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
private static final class UnconditionalValueHolder extends GlobalNPCDataScript.ToStringHelper.ValueHolder {
|
2022-03-23 03:54:06 -05:00
|
|
|
private UnconditionalValueHolder() {
|
|
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private static class ValueHolder {
|
|
|
|
|
@CheckForNull
|
|
|
|
|
String name;
|
|
|
|
|
@CheckForNull
|
|
|
|
|
Object value;
|
|
|
|
|
@CheckForNull
|
2023-03-16 04:27:19 -05:00
|
|
|
GlobalNPCDataScript.ToStringHelper.ValueHolder next;
|
2022-03-23 03:54:06 -05:00
|
|
|
private ValueHolder() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-16 04:27:19 -05:00
|
|
|
public static GlobalNPCDataScript.ToStringHelper toStringHelper(Object self) {
|
|
|
|
|
return new GlobalNPCDataScript.ToStringHelper(self.getClass().getSimpleName());
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
|
|
|
|
}
|