Individualized Player Scripts Config

This commit is contained in:
Kam committed 2025-04-09 01:55:34 -04:00
1 parent e8a3d4f369
commit 4fd0c31bee
19 files changed
+246 -131

No files matched your search

@@ -14,6 +14,7 @@ import noppes.npcs.api.item.IItemStack;
import noppes.npcs.constants.EnumGuiType;
import noppes.npcs.constants.EnumScriptType;
import noppes.npcs.containers.ContainerCustomGui;
import noppes.npcs.controllers.data.PlayerDataScript;
import noppes.npcs.scripted.NpcAPI;
import noppes.npcs.scripted.event.player.CustomGuiEvent;
import noppes.npcs.scripted.gui.ScriptGui;
@@ -76,53 +77,65 @@ public class CustomGuiController {
public static void onButton(CustomGuiEvent.ButtonEvent event) {
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
if (checkGui(event) && getOpenGui(player).getScriptHandler() != null) {
getOpenGui(player).getScriptHandler().callScript(EnumScriptType.CUSTOM_GUI_BUTTON, event);
if(player != null){
ScriptGui gui = getOpenGui(player);
if (checkGui(event) && gui != null) {
gui.getScriptHandler(player).callScript(EnumScriptType.CUSTOM_GUI_BUTTON, event);
}
}
NpcAPI.EVENT_BUS.post(event);
}
public static void onSlotChange(CustomGuiEvent.SlotEvent event) {
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
if (checkGui(event) && getOpenGui(player).getScriptHandler() != null) {
getOpenGui(player).getScriptHandler().callScript(EnumScriptType.CUSTOM_GUI_SLOT, event);
if(player != null){
ScriptGui gui = getOpenGui(player);
if (checkGui(event) && gui != null) {
gui.getScriptHandler(player).callScript(EnumScriptType.CUSTOM_GUI_SLOT, event);
}
}
NpcAPI.EVENT_BUS.post(event);
}
public static boolean onSlotClick(CustomGuiEvent.SlotClickEvent event) {
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
if (checkGui(event) && getOpenGui(player).getScriptHandler() != null) {
(getOpenGui(player).getScriptHandler()).callScript(EnumScriptType.CUSTOM_GUI_SLOT_CLICKED, event);
if(player != null){
ScriptGui gui = getOpenGui(player);
if (checkGui(event) && gui != null) {
gui.getScriptHandler(player).callScript(EnumScriptType.CUSTOM_GUI_SLOT_CLICKED, event);
}
}
return NpcAPI.EVENT_BUS.post(event);
}
public static void onCustomGuiUnfocused(CustomGuiEvent.UnfocusedEvent event) {
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
if (checkGui(event) && getOpenGui(player).getScriptHandler() != null) {
getOpenGui(player).getScriptHandler().callScript(EnumScriptType.CUSTOM_GUI_TEXTFIELD, event);
if(player != null){
ScriptGui gui = getOpenGui(player);
if (checkGui(event) && gui != null) {
gui.getScriptHandler(player).callScript(EnumScriptType.CUSTOM_GUI_TEXTFIELD, event);
}
}
NpcAPI.EVENT_BUS.post(event);
}
public static void onScrollClick(CustomGuiEvent.ScrollEvent event) {
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
if (checkGui(event) && getOpenGui(player).getScriptHandler() != null) {
getOpenGui(player).getScriptHandler().callScript(EnumScriptType.CUSTOM_GUI_SCROLL, event);
if(player != null){
ScriptGui gui = getOpenGui(player);
if (checkGui(event) && gui != null) {
gui.getScriptHandler(player).callScript(EnumScriptType.CUSTOM_GUI_SCROLL, event);
}
}
NpcAPI.EVENT_BUS.post(event);
}
public static void onClose(CustomGuiEvent.CloseEvent event) {
if (ScriptController.Instance.playerScripts != null) {
ScriptController.Instance.playerScripts.callScript(EnumScriptType.CUSTOM_GUI_CLOSED, event);
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
if(player != null){
PlayerDataScript handler = ScriptController.Instance.getPlayerScripts(player);
handler.callScript(EnumScriptType.CUSTOM_GUI_CLOSED, event);
}
NpcAPI.EVENT_BUS.post(event);
}
@@ -95,7 +95,7 @@ public class PartyController implements IPartyHandler {
Party party = this.parties.get(partyUUID);
if (party != null) {
PartyEvent.PartyDisbandEvent partyEvent = new PartyEvent.PartyDisbandEvent(party, party.getQuest());
EventHooks.onPartyDisband(partyEvent);
EventHooks.onPartyDisband(party, partyEvent);
for (UUID uuid : party.getPlayerUUIDs()) {
EntityPlayer player = NoppesUtilServer.getPlayer(uuid);
PlayerData playerData;
@@ -12,10 +12,7 @@ import net.minecraft.util.ChatComponentText;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.config.ConfigMain;
import noppes.npcs.controllers.data.Bank;
import noppes.npcs.controllers.data.PlayerBankData;
import noppes.npcs.controllers.data.PlayerData;
import noppes.npcs.controllers.data.PlayerMail;
import noppes.npcs.controllers.data.*;
import noppes.npcs.util.CacheHashMap;
import noppes.npcs.util.NBTJsonUtil;
@@ -295,6 +292,7 @@ public class PlayerDataController {
if (data == null) {
player.registerExtendedProperties("CustomNpcsData", data = new PlayerData());
data.player = player;
data.scriptData = new PlayerDataScript(player);
data.load();
}
@@ -11,9 +11,11 @@ import net.minecraft.server.MinecraftServer;
import net.minecraftforge.event.world.WorldEvent;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.api.entity.IPlayer;
import noppes.npcs.config.ConfigScript;
import noppes.npcs.controllers.data.ForgeDataScript;
import noppes.npcs.controllers.data.GlobalNPCDataScript;
import noppes.npcs.controllers.data.PlayerData;
import noppes.npcs.controllers.data.PlayerDataScript;
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.scripted.ScriptWorld;
@@ -148,6 +150,20 @@ public class ScriptController {
}
}
public PlayerDataScript getPlayerScripts(EntityPlayer player){
if(ConfigScript.IndividualPlayerScripts)
return PlayerData.get(player).scriptData;
return this.playerScripts;
}
public PlayerDataScript getPlayerScripts(IPlayer player){
if(ConfigScript.IndividualPlayerScripts)
return PlayerData.get((EntityPlayer) player.getMCEntity()).scriptData;
return this.playerScripts;
}
public boolean loadGlobalNPCScripts() {
this.globalNpcScripts.clear();
File file = this.npcScriptsFile();
@@ -51,6 +51,8 @@ public class PlayerData implements IExtendedEntityProperties, IPlayerData {
public DataSkinOverlays skinOverlays = new DataSkinOverlays(this);
public MagicData magicData = new MagicData();
public PlayerDataScript scriptData;
public EntityNPCInterface editingNpc;
public NBTTagCompound cloned;
@@ -26,8 +26,13 @@ public class PlayerDataScript implements INpcScriptHandler {
private EntityPlayer player;
private IPlayer playerAPI;
private long lastPlayerUpdate = 0L;
public long lastInited = -1L;
public boolean enabled = false;
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>();
public PlayerDataScript(EntityPlayer player) {
if (player != null) {
@@ -36,7 +41,9 @@ public class PlayerDataScript implements INpcScriptHandler {
}
public void clear() {
this.scripts = new ArrayList();
console = new TreeMap<Long, String>();
errored = new ArrayList<Integer>();
scripts = new ArrayList<ScriptContainer>();
}
public void readFromNBT(NBTTagCompound compound) {
@@ -54,6 +61,7 @@ public class PlayerDataScript implements INpcScriptHandler {
}
}
this.enabled = compound.getBoolean("ScriptEnabled");
console = NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10));
}
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
@@ -63,6 +71,7 @@ public class PlayerDataScript implements INpcScriptHandler {
}
compound.setString("ScriptLanguage", this.scriptLanguage);
compound.setBoolean("ScriptEnabled", this.enabled);
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(console));
return compound;
}
@@ -76,7 +85,42 @@ public class PlayerDataScript implements INpcScriptHandler {
@Override
public void callScript(String hookName, Event event) {
if (this.isEnabled()) {
if(!isEnabled())
return;
if(ConfigScript.IndividualPlayerScripts){
if(ScriptController.Instance.lastLoaded > lastInited || ScriptController.Instance.lastPlayerUpdate > lastPlayerUpdate){
lastInited = ScriptController.Instance.lastLoaded;
errored.clear();
if(player != null) {
scripts.clear();
for(ScriptContainer script : ScriptController.Instance.playerScripts.scripts){
ScriptContainer s = new ScriptContainer(this);
s.readFromNBT(script.writeToNBT(new NBTTagCompound()));
scripts.add(s);
}
}
lastPlayerUpdate = ScriptController.Instance.lastPlayerUpdate;
if (!Objects.equals(hookName, EnumScriptType.INIT.function) && event instanceof PlayerEvent) {
PlayerEvent playerEvent = (PlayerEvent) event;
EventHooks.onPlayerInit(this, playerEvent.player);
}
}
for(int i = 0; i < scripts.size(); i++){
ScriptContainer script = scripts.get(i);
if(errored.contains(i))
continue;
script.run(hookName, event);
if(script.errored){
errored.add(i);
}
for(Entry<Long, String> entry : script.console.entrySet()){
if(!console.containsKey(entry.getKey()))
console.put(entry.getKey(), " tab " + (i + 1) + ":\n" + entry.getValue());
}
script.console.clear();
}
} else {
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastPlayerUpdate > this.lastPlayerUpdate) {
this.lastInited = ScriptController.Instance.lastLoaded;
this.lastPlayerUpdate = ScriptController.Instance.lastPlayerUpdate;
@@ -146,6 +190,9 @@ public class PlayerDataScript implements INpcScriptHandler {
}
public Map<Long, String> getConsoleText() {
if(ConfigScript.IndividualPlayerScripts)
return console;
TreeMap<Long, String> map = new TreeMap<>();
int tab = 0;
for (ScriptContainer script : this.getScripts()) {
@@ -159,8 +206,12 @@ public class PlayerDataScript implements INpcScriptHandler {
}
public void clearConsole() {
for (ScriptContainer script : this.getScripts()) {
script.console.clear();
if(ConfigScript.IndividualPlayerScripts)
console.clear();
else {
for (ScriptContainer script : this.getScripts()) {
script.console.clear();
}
}
}
@@ -62,7 +62,7 @@ public class PlayerFactionData implements IPlayerFactionData {
}
public void increasePoints(int factionId, int points, EntityPlayer player) {
if (EventHooks.onFactionPoints(new FactionEvent.FactionPoints((IPlayer) NpcAPI.Instance().getIEntity((EntityPlayerMP) player), FactionController.getInstance().get(factionId), points < 0, points)))
if (EventHooks.onFactionPoints(player, new FactionEvent.FactionPoints((IPlayer) NpcAPI.Instance().getIEntity((EntityPlayerMP) player), FactionController.getInstance().get(factionId), points < 0, points)))
return;
if (!factionData.containsKey(factionId)) {