2021-09-30 00:56:49 -05:00
|
|
|
package noppes.npcs.controllers.data;
|
2023-01-12 21:33:06 -05:00
|
|
|
|
2021-09-30 21:44:30 -05:00
|
|
|
import com.google.common.base.Preconditions;
|
2023-01-12 21:33:06 -05:00
|
|
|
import cpw.mods.fml.common.eventhandler.Event;
|
2026-01-22 20:05:07 +02:00
|
|
|
import kamkeel.npcs.network.packets.request.script.PlayerScriptPacket;
|
2021-09-30 00:56:49 -05:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
|
import noppes.npcs.EventHooks;
|
|
|
|
|
import noppes.npcs.NBTTags;
|
2026-01-22 20:05:07 +02:00
|
|
|
import noppes.npcs.api.handler.IScriptHookHandler;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.api.entity.IPlayer;
|
|
|
|
|
import noppes.npcs.config.ConfigScript;
|
2021-09-30 00:56:49 -05:00
|
|
|
import noppes.npcs.constants.EnumScriptType;
|
2026-01-22 20:05:07 +02:00
|
|
|
import noppes.npcs.constants.ScriptContext;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.controllers.ScriptContainer;
|
2021-09-30 00:56:49 -05:00
|
|
|
import noppes.npcs.controllers.ScriptController;
|
2026-01-22 20:51:46 +02:00
|
|
|
import noppes.npcs.janino.impl.JaninoPlayerScript;
|
2022-05-11 22:08:31 -05:00
|
|
|
import noppes.npcs.scripted.NpcAPI;
|
2025-04-08 22:37:26 -04:00
|
|
|
import noppes.npcs.scripted.event.player.PlayerEvent;
|
2022-05-11 16:43:14 -05:00
|
|
|
|
2021-09-30 21:44:30 -05:00
|
|
|
import javax.annotation.CheckForNull;
|
2023-01-12 21:33:06 -05:00
|
|
|
import java.lang.reflect.Array;
|
2025-07-23 14:22:52 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
2023-01-12 21:33:06 -05:00
|
|
|
import java.util.Map.Entry;
|
2025-07-23 14:22:52 -04:00
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.OptionalDouble;
|
|
|
|
|
import java.util.OptionalInt;
|
|
|
|
|
import java.util.OptionalLong;
|
|
|
|
|
import java.util.TreeMap;
|
2022-05-11 16:43:14 -05:00
|
|
|
|
2026-01-22 20:05:07 +02:00
|
|
|
public class PlayerDataScript implements IScriptHandlerPacket {
|
2026-01-13 17:34:14 +02:00
|
|
|
public List<IScriptUnit> scripts = new ArrayList<>();
|
2021-12-16 04:05:05 -06:00
|
|
|
public String scriptLanguage = "ECMAScript";
|
2021-09-30 00:56:49 -05:00
|
|
|
private EntityPlayer player;
|
2021-10-21 22:14:12 -05:00
|
|
|
private IPlayer playerAPI;
|
2021-09-30 00:56:49 -05:00
|
|
|
private long lastPlayerUpdate = 0L;
|
2025-04-09 01:55:34 -04:00
|
|
|
|
|
|
|
|
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>();
|
2021-12-18 08:34:30 -06:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript(EntityPlayer player) {
|
2025-03-13 17:14:45 -04:00
|
|
|
if (player != null) {
|
2021-12-16 04:05:05 -06:00
|
|
|
this.player = player;
|
|
|
|
|
}
|
2021-09-30 00:56:49 -05:00
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 00:56:49 -05:00
|
|
|
public void clear() {
|
2025-04-09 01:55:34 -04:00
|
|
|
console = new TreeMap<Long, String>();
|
|
|
|
|
errored = new ArrayList<Integer>();
|
2026-01-13 17:34:14 +02:00
|
|
|
scripts = new ArrayList<>();
|
2021-09-30 00:56:49 -05:00
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 00:56:49 -05:00
|
|
|
public void readFromNBT(NBTTagCompound compound) {
|
2023-02-19 13:38:43 -06:00
|
|
|
if (compound.hasKey("Scripts")) {
|
2026-01-13 17:34:14 +02:00
|
|
|
this.scripts = new ArrayList<>(NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this));
|
2023-02-19 13:38:43 -06:00
|
|
|
} else {
|
2026-01-13 17:34:14 +02:00
|
|
|
this.scripts = new ArrayList<>(NBTTags.GetScript(compound, this));
|
2023-02-19 13:38:43 -06:00
|
|
|
}
|
2021-09-30 01:48:54 -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";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-30 01:48:54 -05:00
|
|
|
this.enabled = compound.getBoolean("ScriptEnabled");
|
2025-04-09 01:55:34 -04:00
|
|
|
console = NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10));
|
2021-09-30 00:56:49 -05:00
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 00:56:49 -05:00
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
2025-03-13 17:14:45 -04:00
|
|
|
compound.setInteger("TotalScripts", this.scripts.size());
|
2023-02-19 13:38:43 -06:00
|
|
|
for (int i = 0; i < this.scripts.size(); i++) {
|
2025-03-13 17:14:45 -04:00
|
|
|
compound.setTag("Tab" + i, this.scripts.get(i).writeToNBT(new NBTTagCompound()));
|
2023-02-19 13:38:43 -06:00
|
|
|
}
|
2021-09-30 01:48:54 -05:00
|
|
|
compound.setString("ScriptLanguage", this.scriptLanguage);
|
|
|
|
|
compound.setBoolean("ScriptEnabled", this.enabled);
|
2025-04-09 01:55:34 -04:00
|
|
|
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(console));
|
2021-09-30 00:56:49 -05:00
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 13:56:32 -05:00
|
|
|
public boolean isEnabled() {
|
|
|
|
|
return ConfigScript.GlobalPlayerScripts && ScriptController.Instance.playerScripts.enabled && ScriptController.HasStart && (this.player == null || !this.player.worldObj.isRemote);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 20:05:07 +02:00
|
|
|
@Override
|
|
|
|
|
public ScriptContext getContext() {
|
|
|
|
|
return ScriptContext.PLAYER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getHookContext() {
|
|
|
|
|
return IScriptHookHandler.CONTEXT_PLAYER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void requestData() {
|
|
|
|
|
PlayerScriptPacket.Get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void sendSavePacket(int index, int totalCount, NBTTagCompound nbt) {
|
|
|
|
|
PlayerScriptPacket.Save(index, totalCount, nbt);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 13:56:32 -05:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void callScript(String hookName, Event event) {
|
2025-05-20 13:53:26 -04:00
|
|
|
if (!isEnabled())
|
2025-04-09 01:55:34 -04:00
|
|
|
return;
|
|
|
|
|
|
2025-05-20 13:53:26 -04:00
|
|
|
if (ConfigScript.IndividualPlayerScripts) {
|
|
|
|
|
if (ScriptController.Instance.lastLoaded > lastInited || ScriptController.Instance.lastPlayerUpdate > lastPlayerUpdate) {
|
2025-04-09 01:55:34 -04:00
|
|
|
lastInited = ScriptController.Instance.lastLoaded;
|
|
|
|
|
errored.clear();
|
2025-05-20 13:53:26 -04:00
|
|
|
if (player != null) {
|
2025-04-09 01:55:34 -04:00
|
|
|
scripts.clear();
|
2026-01-13 17:34:14 +02:00
|
|
|
for (IScriptUnit script : ScriptController.Instance.playerScripts.scripts) {
|
2026-01-13 22:16:51 +02:00
|
|
|
// Clone the script unit by writing to NBT and reading back with factory
|
|
|
|
|
NBTTagCompound nbt = script.writeToNBT(new NBTTagCompound());
|
|
|
|
|
IScriptUnit cloned = IScriptUnit.createFromNBT(nbt, this);
|
|
|
|
|
scripts.add(cloned);
|
2025-04-09 01:55:34 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lastPlayerUpdate = ScriptController.Instance.lastPlayerUpdate;
|
|
|
|
|
if (!Objects.equals(hookName, EnumScriptType.INIT.function) && event instanceof PlayerEvent) {
|
|
|
|
|
PlayerEvent playerEvent = (PlayerEvent) event;
|
|
|
|
|
EventHooks.onPlayerInit(this, playerEvent.player);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-20 13:53:26 -04:00
|
|
|
for (int i = 0; i < scripts.size(); i++) {
|
2026-01-13 17:34:14 +02:00
|
|
|
IScriptUnit script = scripts.get(i);
|
2025-05-20 13:53:26 -04:00
|
|
|
if (errored.contains(i))
|
2025-04-09 01:55:34 -04:00
|
|
|
continue;
|
2026-01-14 00:16:58 +02:00
|
|
|
script.run(hookName, event);
|
|
|
|
|
if (script.hasErrored()) {
|
|
|
|
|
errored.add(i);
|
2025-04-09 01:55:34 -04:00
|
|
|
}
|
2026-01-14 00:16:58 +02:00
|
|
|
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();
|
2025-04-09 01:55:34 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
2021-12-16 21:54:05 -06:00
|
|
|
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastPlayerUpdate > this.lastPlayerUpdate) {
|
2021-09-30 00:56:49 -05:00
|
|
|
this.lastInited = ScriptController.Instance.lastLoaded;
|
|
|
|
|
this.lastPlayerUpdate = ScriptController.Instance.lastPlayerUpdate;
|
2021-12-18 08:34:30 -06:00
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
for (IScriptUnit script : this.scripts) {
|
|
|
|
|
if (script instanceof ScriptContainer)
|
|
|
|
|
((ScriptContainer) script).errored = false;
|
2023-02-21 12:45:48 -06:00
|
|
|
}
|
|
|
|
|
|
2025-04-08 22:37:26 -04:00
|
|
|
if (!Objects.equals(hookName, EnumScriptType.INIT.function) && event instanceof PlayerEvent) {
|
|
|
|
|
PlayerEvent playerEvent = (PlayerEvent) event;
|
2022-05-11 22:08:31 -05:00
|
|
|
EventHooks.onPlayerInit(this, playerEvent.player);
|
2021-09-30 00:56:49 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
for (IScriptUnit script : this.scripts) {
|
2026-01-14 00:16:58 +02:00
|
|
|
if (script == null || script.hasErrored() || !script.hasCode())
|
2023-02-21 12:45:48 -06:00
|
|
|
continue;
|
2026-01-14 00:16:58 +02:00
|
|
|
script.run(hookName, event);
|
2021-09-30 00:56:49 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isClient() {
|
2025-01-22 18:54:00 -06:00
|
|
|
return this.player != null && this.player.isClientWorld();
|
2021-09-30 00:56:49 -05:00
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 00:56:49 -05:00
|
|
|
public boolean getEnabled() {
|
|
|
|
|
return ScriptController.Instance.playerScripts.enabled;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 00:56:49 -05:00
|
|
|
public void setEnabled(boolean bo) {
|
2021-10-15 09:17:31 -05:00
|
|
|
ScriptController.Instance.playerScripts.enabled = bo;
|
2021-12-16 04:05:05 -06:00
|
|
|
this.enabled = bo;
|
2021-09-30 00:56:49 -05:00
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 00:56:49 -05:00
|
|
|
public String getLanguage() {
|
|
|
|
|
return ScriptController.Instance.playerScripts.scriptLanguage;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 00:56:49 -05:00
|
|
|
public void setLanguage(String lang) {
|
|
|
|
|
this.scriptLanguage = lang;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
public void setScripts(List<IScriptUnit> list) {
|
2023-02-19 16:22:28 -06:00
|
|
|
this.scripts = list;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
public List<IScriptUnit> getScripts() {
|
2021-09-30 00:56:49 -05:00
|
|
|
return this.scripts;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-17 16:11:29 -05:00
|
|
|
public String noticeString() {
|
2025-03-13 17:14:45 -04:00
|
|
|
if (this.player == null) {
|
2021-10-17 16:11:29 -05:00
|
|
|
return "Global script";
|
|
|
|
|
}
|
2026-01-18 03:05:02 +02:00
|
|
|
BlockPos pos = new BlockPos(this.player);
|
|
|
|
|
return PlayerDataScript.toStringHelper(this.player).add("x", pos.getX()).add("y", pos.getY()).add("z", pos.getZ()).toString();
|
2021-10-17 16:11:29 -05:00
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-21 22:14:12 -05:00
|
|
|
public IPlayer getPlayer() {
|
|
|
|
|
if (this.playerAPI == null) {
|
2022-05-11 22:08:31 -05:00
|
|
|
this.playerAPI = (IPlayer) NpcAPI.Instance().getIEntity(this.player);
|
2021-10-17 16:11:29 -05:00
|
|
|
}
|
|
|
|
|
return this.playerAPI;
|
|
|
|
|
}
|
2023-02-19 15:20:51 -06:00
|
|
|
|
2021-10-17 16:11:29 -05:00
|
|
|
public Map<Long, String> getConsoleText() {
|
2026-01-18 03:05:02 +02:00
|
|
|
if (ConfigScript.IndividualPlayerScripts) {
|
2025-04-09 01:55:34 -04:00
|
|
|
return console;
|
2023-02-21 12:45:48 -06:00
|
|
|
}
|
2026-01-22 20:05:07 +02:00
|
|
|
return IScriptHandlerPacket.super.getConsoleText();
|
2021-10-17 16:11:29 -05:00
|
|
|
}
|
2023-02-19 15:20:51 -06:00
|
|
|
|
2026-01-18 03:05:02 +02:00
|
|
|
@Override
|
2021-10-17 16:11:29 -05:00
|
|
|
public void clearConsole() {
|
2026-01-18 03:05:02 +02:00
|
|
|
if (ConfigScript.IndividualPlayerScripts) {
|
2025-04-09 01:55:34 -04:00
|
|
|
console.clear();
|
2026-01-18 03:05:02 +02:00
|
|
|
return;
|
2023-02-21 12:45:48 -06:00
|
|
|
}
|
2026-01-22 20:05:07 +02:00
|
|
|
IScriptHandlerPacket.super.clearConsole();
|
2021-10-17 16:11:29 -05:00
|
|
|
}
|
2023-02-19 15:20:51 -06:00
|
|
|
|
2021-09-30 21:44:30 -05:00
|
|
|
public static final class ToStringHelper {
|
|
|
|
|
private final String className;
|
2021-10-11 06:49:30 -05:00
|
|
|
private final PlayerDataScript.ToStringHelper.ValueHolder holderHead;
|
|
|
|
|
private PlayerDataScript.ToStringHelper.ValueHolder holderTail;
|
2021-09-30 21:44:30 -05:00
|
|
|
private boolean omitNullValues;
|
|
|
|
|
private boolean omitEmptyValues;
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 21:44:30 -05:00
|
|
|
private ToStringHelper(String className) {
|
2021-10-11 06:49:30 -05:00
|
|
|
this.holderHead = new PlayerDataScript.ToStringHelper.ValueHolder();
|
2021-09-30 21:44:30 -05:00
|
|
|
this.holderTail = this.holderHead;
|
|
|
|
|
this.omitNullValues = false;
|
|
|
|
|
this.omitEmptyValues = false;
|
|
|
|
|
this.className = (String) Preconditions.checkNotNull(className);
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper omitNullValues() {
|
2021-09-30 21:44:30 -05:00
|
|
|
this.omitNullValues = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper add(String name, @CheckForNull Object value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addHolder(name, value);
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper add(String name, boolean value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper add(String name, char value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper add(String name, double value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper add(String name, float value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper add(String name, int value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper add(String name, long value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(name, String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper addValue(@CheckForNull Object value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addHolder(value);
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper addValue(boolean value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper addValue(char value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper addValue(double value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper addValue(float value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper addValue(int value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript.ToStringHelper addValue(long value) {
|
2021-09-30 21:44:30 -05:00
|
|
|
return this.addUnconditionalHolder(String.valueOf(value));
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 21:44:30 -05:00
|
|
|
private static boolean isEmpty(Object value) {
|
|
|
|
|
if (value instanceof CharSequence) {
|
2025-03-13 17:14:45 -04:00
|
|
|
return ((CharSequence) value).length() == 0;
|
2021-09-30 21:44:30 -05:00
|
|
|
} else if (value instanceof Collection) {
|
2025-03-13 17:14:45 -04:00
|
|
|
return ((Collection) value).isEmpty();
|
2021-09-30 21:44:30 -05:00
|
|
|
} else if (value instanceof Map) {
|
2025-03-13 17:14:45 -04:00
|
|
|
return ((Map) value).isEmpty();
|
2021-09-30 21:44:30 -05:00
|
|
|
} else if (value instanceof Optional) {
|
2025-03-13 17:14:45 -04:00
|
|
|
return !((Optional) value).isPresent();
|
2021-09-30 21:44:30 -05:00
|
|
|
} else if (value instanceof OptionalInt) {
|
2025-03-13 17:14:45 -04:00
|
|
|
return !((OptionalInt) value).isPresent();
|
2021-09-30 21:44:30 -05:00
|
|
|
} else if (value instanceof OptionalLong) {
|
2025-03-13 17:14:45 -04:00
|
|
|
return !((OptionalLong) value).isPresent();
|
2021-09-30 21:44:30 -05:00
|
|
|
} else if (value instanceof OptionalDouble) {
|
2025-03-13 17:14:45 -04:00
|
|
|
return !((OptionalDouble) value).isPresent();
|
2021-09-30 21:44:30 -05:00
|
|
|
} else if (value instanceof com.google.common.base.Optional) {
|
2025-03-13 17:14:45 -04:00
|
|
|
return !((com.google.common.base.Optional) value).isPresent();
|
2021-09-30 21:44:30 -05:00
|
|
|
} else if (value.getClass().isArray()) {
|
|
|
|
|
return Array.getLength(value) == 0;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 21:44:30 -05:00
|
|
|
public String toString() {
|
|
|
|
|
boolean omitNullValuesSnapshot = this.omitNullValues;
|
|
|
|
|
boolean omitEmptyValuesSnapshot = this.omitEmptyValues;
|
|
|
|
|
String nextSeparator = "";
|
|
|
|
|
StringBuilder builder = (new StringBuilder(32)).append(this.className).append('{');
|
2025-03-13 17:14:45 -04:00
|
|
|
for (PlayerDataScript.ToStringHelper.ValueHolder valueHolder = this.holderHead.next; valueHolder != null; valueHolder = valueHolder.next) {
|
2021-09-30 21:44:30 -05:00
|
|
|
Object value = valueHolder.value;
|
2021-10-11 06:49:30 -05:00
|
|
|
if (!(valueHolder instanceof PlayerDataScript.ToStringHelper.UnconditionalValueHolder)) {
|
2021-09-30 21:44:30 -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();
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
private PlayerDataScript.ToStringHelper.ValueHolder addHolder() {
|
|
|
|
|
PlayerDataScript.ToStringHelper.ValueHolder valueHolder = new PlayerDataScript.ToStringHelper.ValueHolder();
|
2021-09-30 21:44:30 -05:00
|
|
|
this.holderTail = this.holderTail.next = valueHolder;
|
|
|
|
|
return valueHolder;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
private PlayerDataScript.ToStringHelper addHolder(@CheckForNull Object value) {
|
|
|
|
|
PlayerDataScript.ToStringHelper.ValueHolder valueHolder = this.addHolder();
|
2021-09-30 21:44:30 -05:00
|
|
|
valueHolder.value = value;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
private PlayerDataScript.ToStringHelper addHolder(String name, @CheckForNull Object value) {
|
|
|
|
|
PlayerDataScript.ToStringHelper.ValueHolder valueHolder = this.addHolder();
|
2021-09-30 21:44:30 -05:00
|
|
|
valueHolder.value = value;
|
2025-03-13 17:14:45 -04:00
|
|
|
valueHolder.name = (String) Preconditions.checkNotNull(name);
|
2021-09-30 21:44:30 -05:00
|
|
|
return this;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
private PlayerDataScript.ToStringHelper.UnconditionalValueHolder addUnconditionalHolder() {
|
|
|
|
|
PlayerDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = new PlayerDataScript.ToStringHelper.UnconditionalValueHolder();
|
2021-09-30 21:44:30 -05:00
|
|
|
this.holderTail = this.holderTail.next = valueHolder;
|
|
|
|
|
return valueHolder;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
private PlayerDataScript.ToStringHelper addUnconditionalHolder(Object value) {
|
|
|
|
|
PlayerDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = this.addUnconditionalHolder();
|
2021-09-30 21:44:30 -05:00
|
|
|
valueHolder.value = value;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
private PlayerDataScript.ToStringHelper addUnconditionalHolder(String name, Object value) {
|
|
|
|
|
PlayerDataScript.ToStringHelper.UnconditionalValueHolder valueHolder = this.addUnconditionalHolder();
|
2021-09-30 21:44:30 -05:00
|
|
|
valueHolder.value = value;
|
2025-03-13 17:14:45 -04:00
|
|
|
valueHolder.name = (String) Preconditions.checkNotNull(name);
|
2021-09-30 21:44:30 -05:00
|
|
|
return this;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
private static final class UnconditionalValueHolder extends PlayerDataScript.ToStringHelper.ValueHolder {
|
2021-09-30 21:44:30 -05:00
|
|
|
private UnconditionalValueHolder() {
|
|
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 21:44:30 -05:00
|
|
|
private static class ValueHolder {
|
|
|
|
|
@CheckForNull
|
|
|
|
|
String name;
|
|
|
|
|
@CheckForNull
|
|
|
|
|
Object value;
|
|
|
|
|
@CheckForNull
|
2021-10-11 06:49:30 -05:00
|
|
|
PlayerDataScript.ToStringHelper.ValueHolder next;
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-09-30 21:44:30 -05:00
|
|
|
private ValueHolder() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public static PlayerDataScript.ToStringHelper toStringHelper(Object self) {
|
|
|
|
|
return new PlayerDataScript.ToStringHelper(self.getClass().getSimpleName());
|
2021-09-30 21:44:30 -05:00
|
|
|
}
|
2026-01-22 20:51:46 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IScriptUnit createJaninoScriptUnit() {
|
|
|
|
|
return new JaninoPlayerScript();
|
|
|
|
|
}
|
2024-02-21 00:31:17 -05:00
|
|
|
}
|