mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-20 02:59:42 +00:00
Compress Script Hooks and Context System
This commit is contained in:
1 parent
e2ca76e626
commit
19b01e2099
17 files changed
+595
-770
No files matched your search
@@ -1,6 +1,5 @@
|
||||
package noppes.npcs.controllers.data;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import kamkeel.npcs.network.packets.request.script.PlayerScriptPacket;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
@@ -8,7 +7,6 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import noppes.npcs.EventHooks;
|
||||
import noppes.npcs.NBTTags;
|
||||
import noppes.npcs.api.handler.IScriptHookHandler;
|
||||
import noppes.npcs.api.entity.IPlayer;
|
||||
import noppes.npcs.config.ConfigScript;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
@@ -18,35 +16,23 @@ import noppes.npcs.controllers.ScriptController;
|
||||
import noppes.npcs.janino.impl.JaninoPlayerScript;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
import noppes.npcs.scripted.event.player.PlayerEvent;
|
||||
import noppes.npcs.util.ScriptToStringHelper;
|
||||
|
||||
import javax.annotation.CheckForNull;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.OptionalLong;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class PlayerDataScript implements IScriptHandlerPacket {
|
||||
public List<IScriptUnit> scripts = new ArrayList<>();
|
||||
public String scriptLanguage = "ECMAScript";
|
||||
public class PlayerDataScript extends MultiScriptHandler {
|
||||
private EntityPlayer player;
|
||||
private IPlayer playerAPI;
|
||||
private long lastPlayerUpdate = 0L;
|
||||
|
||||
public long lastInited = -1;
|
||||
public boolean hadInteract = true;
|
||||
private boolean enabled = false;
|
||||
|
||||
private static Map<Long, String> console = new TreeMap<Long, String>();
|
||||
private static List<Integer> errored = new ArrayList<Integer>();
|
||||
// Static console/errored for IndividualPlayerScripts mode
|
||||
private static Map<Long, String> staticConsole = new TreeMap<>();
|
||||
private static List<Integer> errored = new ArrayList<>();
|
||||
|
||||
public PlayerDataScript(EntityPlayer player) {
|
||||
if (player != null) {
|
||||
@@ -54,38 +40,23 @@ public class PlayerDataScript implements IScriptHandlerPacket {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
console = new TreeMap<Long, String>();
|
||||
errored = new ArrayList<Integer>();
|
||||
staticConsole = new TreeMap<>();
|
||||
errored = new ArrayList<>();
|
||||
scripts = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
if (compound.hasKey("Scripts")) {
|
||||
this.scripts = new ArrayList<>(NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this));
|
||||
} else {
|
||||
this.scripts = new ArrayList<>(NBTTags.GetScript(compound, this));
|
||||
}
|
||||
this.scriptLanguage = compound.getString("ScriptLanguage");
|
||||
if (!ScriptController.Instance.languages.containsKey(scriptLanguage)) {
|
||||
if (!ScriptController.Instance.languages.isEmpty()) {
|
||||
this.scriptLanguage = (String) ScriptController.Instance.languages.keySet().toArray()[0];
|
||||
} else {
|
||||
this.scriptLanguage = "ECMAScript";
|
||||
}
|
||||
}
|
||||
this.enabled = compound.getBoolean("ScriptEnabled");
|
||||
console = NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10));
|
||||
super.readFromNBT(compound);
|
||||
staticConsole = NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
||||
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()));
|
||||
}
|
||||
compound.setString("ScriptLanguage", this.scriptLanguage);
|
||||
compound.setBoolean("ScriptEnabled", this.enabled);
|
||||
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(console));
|
||||
super.writeToNBT(compound);
|
||||
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(staticConsole));
|
||||
return compound;
|
||||
}
|
||||
|
||||
@@ -94,13 +65,13 @@ public class PlayerDataScript implements IScriptHandlerPacket {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptContext getContext() {
|
||||
return ScriptContext.PLAYER;
|
||||
protected boolean canRunScripts() {
|
||||
return isEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHookContext() {
|
||||
return IScriptHookHandler.CONTEXT_PLAYER;
|
||||
public ScriptContext getContext() {
|
||||
return ScriptContext.PLAYER;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -113,7 +84,6 @@ public class PlayerDataScript implements IScriptHandlerPacket {
|
||||
PlayerScriptPacket.Save(index, totalCount, nbt);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void callScript(String hookName, Event event) {
|
||||
if (!isEnabled())
|
||||
@@ -147,8 +117,8 @@ public class PlayerDataScript implements IScriptHandlerPacket {
|
||||
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());
|
||||
if (!staticConsole.containsKey(entry.getKey()))
|
||||
staticConsole.put(entry.getKey(), " tab " + (i + 1) + ":\n" + entry.getValue());
|
||||
}
|
||||
script.clearConsole();
|
||||
}
|
||||
@@ -176,41 +146,34 @@ public class PlayerDataScript implements IScriptHandlerPacket {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClient() {
|
||||
return this.player != null && this.player.isClientWorld();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getEnabled() {
|
||||
return ScriptController.Instance.playerScripts.enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean bo) {
|
||||
ScriptController.Instance.playerScripts.enabled = bo;
|
||||
this.enabled = bo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLanguage() {
|
||||
return ScriptController.Instance.playerScripts.scriptLanguage;
|
||||
}
|
||||
|
||||
public void setLanguage(String lang) {
|
||||
this.scriptLanguage = lang;
|
||||
}
|
||||
|
||||
public void setScripts(List<IScriptUnit> list) {
|
||||
this.scripts = list;
|
||||
}
|
||||
|
||||
public List<IScriptUnit> getScripts() {
|
||||
return this.scripts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String noticeString() {
|
||||
if (this.player == null) {
|
||||
return "Global script";
|
||||
}
|
||||
BlockPos pos = new BlockPos(this.player);
|
||||
return PlayerDataScript.toStringHelper(this.player).add("x", pos.getX()).add("y", pos.getY()).add("z", pos.getZ()).toString();
|
||||
return ScriptToStringHelper.toStringHelper(this.player).add("x", pos.getX()).add("y", pos.getY()).add("z", pos.getZ()).toString();
|
||||
}
|
||||
|
||||
public IPlayer getPlayer() {
|
||||
@@ -220,213 +183,21 @@ public class PlayerDataScript implements IScriptHandlerPacket {
|
||||
return this.playerAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, String> getConsoleText() {
|
||||
if (ConfigScript.IndividualPlayerScripts) {
|
||||
return console;
|
||||
return staticConsole;
|
||||
}
|
||||
return IScriptHandlerPacket.super.getConsoleText();
|
||||
return super.getConsoleText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearConsole() {
|
||||
if (ConfigScript.IndividualPlayerScripts) {
|
||||
console.clear();
|
||||
staticConsole.clear();
|
||||
return;
|
||||
}
|
||||
IScriptHandlerPacket.super.clearConsole();
|
||||
}
|
||||
|
||||
public static final class ToStringHelper {
|
||||
private final String className;
|
||||
private final PlayerDataScript.ToStringHelper.ValueHolder holderHead;
|
||||
private PlayerDataScript.ToStringHelper.ValueHolder holderTail;
|
||||
private boolean omitNullValues;
|
||||
private boolean omitEmptyValues;
|
||||
|
||||
private ToStringHelper(String className) {
|
||||
this.holderHead = new PlayerDataScript.ToStringHelper.ValueHolder();
|
||||
this.holderTail = this.holderHead;
|
||||
this.omitNullValues = false;
|
||||
this.omitEmptyValues = false;
|
||||
this.className = (String) Preconditions.checkNotNull(className);
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper omitNullValues() {
|
||||
this.omitNullValues = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper add(String name, @CheckForNull Object value) {
|
||||
return this.addHolder(name, value);
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper add(String name, boolean value) {
|
||||
return this.addUnconditionalHolder(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper add(String name, char value) {
|
||||
return this.addUnconditionalHolder(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper add(String name, double value) {
|
||||
return this.addUnconditionalHolder(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper add(String name, float value) {
|
||||
return this.addUnconditionalHolder(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper add(String name, int value) {
|
||||
return this.addUnconditionalHolder(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper add(String name, long value) {
|
||||
return this.addUnconditionalHolder(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper addValue(@CheckForNull Object value) {
|
||||
return this.addHolder(value);
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper addValue(boolean value) {
|
||||
return this.addUnconditionalHolder(String.valueOf(value));
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper addValue(char value) {
|
||||
return this.addUnconditionalHolder(String.valueOf(value));
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper addValue(double value) {
|
||||
return this.addUnconditionalHolder(String.valueOf(value));
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper addValue(float value) {
|
||||
return this.addUnconditionalHolder(String.valueOf(value));
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper addValue(int value) {
|
||||
return this.addUnconditionalHolder(String.valueOf(value));
|
||||
}
|
||||
|
||||
public PlayerDataScript.ToStringHelper addValue(long value) {
|
||||
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('{');
|
||||
for (PlayerDataScript.ToStringHelper.ValueHolder valueHolder = this.holderHead.next; valueHolder != null; valueHolder = valueHolder.next) {
|
||||
Object value = valueHolder.value;
|
||||
if (!(valueHolder instanceof PlayerDataScript.ToStringHelper.UnconditionalValueHolder)) {
|
||||
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();
|
||||
}
|
||||
|
||||
private PlayerDataScript.ToStringHelper.ValueHolder addHolder() {
|
||||
PlayerDataScript.ToStringHelper.ValueHolder valueHolder = new PlayerDataScript.ToStringHelper.ValueHolder();
|
||||
this.holderTail = this.holderTail.next = valueHolder;
|
||||
return valueHolder;
|
||||
}
|
||||
|
||||
private PlayerDataScript.ToStringHelper addHolder(@CheckForNull Object value) {
|
||||
PlayerDataScript.ToStringHelper.ValueHolder valueHolder = this.addHolder();
|
||||
valueHolder.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
private PlayerDataScript.ToStringHelper addHolder(String name, @CheckForNull Object value) {
|
||||
PlayerDataScript.ToStringHelper.ValueHolder valueHolder = this.addHolder();
|
||||
valueHolder.value = value;
|
||||
valueHolder.name = (String) Preconditions.checkNotNull(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
private PlayerDataScript.ToStringHelper.UnconditionalValueHolder addUnconditionalHolder() {
|
||||
PlayerDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = new PlayerDataScript.ToStringHelper.UnconditionalValueHolder();
|
||||
this.holderTail = this.holderTail.next = valueHolder;
|
||||
return valueHolder;
|
||||
}
|
||||
|
||||
private PlayerDataScript.ToStringHelper addUnconditionalHolder(Object value) {
|
||||
PlayerDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = this.addUnconditionalHolder();
|
||||
valueHolder.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
private PlayerDataScript.ToStringHelper addUnconditionalHolder(String name, Object value) {
|
||||
PlayerDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = this.addUnconditionalHolder();
|
||||
valueHolder.value = value;
|
||||
valueHolder.name = (String) Preconditions.checkNotNull(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
private static final class UnconditionalValueHolder extends PlayerDataScript.ToStringHelper.ValueHolder {
|
||||
private UnconditionalValueHolder() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
private static class ValueHolder {
|
||||
@CheckForNull
|
||||
String name;
|
||||
@CheckForNull
|
||||
Object value;
|
||||
@CheckForNull
|
||||
PlayerDataScript.ToStringHelper.ValueHolder next;
|
||||
|
||||
private ValueHolder() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static PlayerDataScript.ToStringHelper toStringHelper(Object self) {
|
||||
return new PlayerDataScript.ToStringHelper(self.getClass().getSimpleName());
|
||||
super.clearConsole();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user