2021-09-30 00:56:49 -05:00
|
|
|
package noppes.npcs.controllers.data;
|
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;
|
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;
|
2026-01-22 14:27:48 -05:00
|
|
|
import noppes.npcs.util.ScriptToStringHelper;
|
2022-05-11 16:43:14 -05:00
|
|
|
|
2025-07-23 14:22:52 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
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.TreeMap;
|
2022-05-11 16:43:14 -05:00
|
|
|
|
2026-01-22 14:27:48 -05:00
|
|
|
public class PlayerDataScript extends MultiScriptHandler {
|
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
|
|
|
|
2026-01-22 14:27:48 -05:00
|
|
|
// Static console/errored for IndividualPlayerScripts mode
|
|
|
|
|
private static Map<Long, String> staticConsole = new TreeMap<>();
|
|
|
|
|
private static List<Integer> errored = new ArrayList<>();
|
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() {
|
2026-01-22 23:05:47 +02:00
|
|
|
super.clear();
|
2026-01-22 14:27:48 -05:00
|
|
|
staticConsole = new TreeMap<>();
|
|
|
|
|
errored = new ArrayList<>();
|
2021-09-30 00:56:49 -05:00
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2026-01-22 14:27:48 -05:00
|
|
|
@Override
|
2021-09-30 00:56:49 -05:00
|
|
|
public void readFromNBT(NBTTagCompound compound) {
|
2026-01-22 14:27:48 -05:00
|
|
|
super.readFromNBT(compound);
|
|
|
|
|
staticConsole = NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10));
|
2021-09-30 00:56:49 -05:00
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2026-01-22 14:27:48 -05:00
|
|
|
@Override
|
2021-09-30 00:56:49 -05:00
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
2026-01-22 14:27:48 -05:00
|
|
|
super.writeToNBT(compound);
|
|
|
|
|
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(staticConsole));
|
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
|
2026-01-22 14:27:48 -05:00
|
|
|
protected boolean canRunScripts() {
|
|
|
|
|
return isEnabled();
|
2026-01-22 20:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2026-01-22 14:27:48 -05:00
|
|
|
public ScriptContext getContext() {
|
|
|
|
|
return ScriptContext.PLAYER;
|
2026-01-22 20:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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()) {
|
2026-01-22 14:27:48 -05:00
|
|
|
if (!staticConsole.containsKey(entry.getKey()))
|
|
|
|
|
staticConsole.put(entry.getKey(), " tab " + (i + 1) + ":\n" + entry.getValue());
|
2026-01-14 00:16:58 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 14:27:48 -05:00
|
|
|
@Override
|
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
|
|
|
|
2026-01-22 14:27:48 -05:00
|
|
|
@Override
|
2021-09-30 00:56:49 -05:00
|
|
|
public boolean getEnabled() {
|
|
|
|
|
return ScriptController.Instance.playerScripts.enabled;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2026-01-22 14:27:48 -05:00
|
|
|
@Override
|
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
|
|
|
|
2026-01-22 14:27:48 -05:00
|
|
|
@Override
|
2021-09-30 00:56:49 -05:00
|
|
|
public String getLanguage() {
|
|
|
|
|
return ScriptController.Instance.playerScripts.scriptLanguage;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2026-01-22 14:27:48 -05:00
|
|
|
@Override
|
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);
|
2026-01-22 14:27:48 -05:00
|
|
|
return ScriptToStringHelper.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
|
|
|
|
2026-01-22 14:27:48 -05:00
|
|
|
@Override
|
2021-10-17 16:11:29 -05:00
|
|
|
public Map<Long, String> getConsoleText() {
|
2026-01-18 03:05:02 +02:00
|
|
|
if (ConfigScript.IndividualPlayerScripts) {
|
2026-01-22 14:27:48 -05:00
|
|
|
return staticConsole;
|
2023-02-21 12:45:48 -06:00
|
|
|
}
|
2026-01-22 14:27:48 -05:00
|
|
|
return 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) {
|
2026-01-22 14:27:48 -05:00
|
|
|
staticConsole.clear();
|
2026-01-18 03:05:02 +02:00
|
|
|
return;
|
2023-02-21 12:45:48 -06:00
|
|
|
}
|
2026-01-22 14:27:48 -05:00
|
|
|
super.clearConsole();
|
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
|
|
|
}
|