Implement Profile Events to Player Scripts

This commit is contained in:
Kam committed 2025-03-11 17:16:54 -04:00
1 parent 9ac8ce3389
commit 495fd98bfd
6 files changed
+143 -94

No files matched your search

@@ -4,10 +4,7 @@ import kamkeel.npcs.controllers.data.profile.*;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.CustomNpcs;
import noppes.npcs.CustomNpcsPermissions;
import noppes.npcs.LogWriter;
import noppes.npcs.NoppesUtilServer;
import noppes.npcs.*;
import noppes.npcs.api.entity.IPlayer;
import noppes.npcs.api.handler.IPlayerData;
import noppes.npcs.api.handler.data.IProfile;
@@ -15,8 +12,11 @@ import noppes.npcs.api.handler.IProfileHandler;
import noppes.npcs.api.handler.data.ISlot;
import noppes.npcs.config.ConfigMain;
import noppes.npcs.controllers.QuestController;
import noppes.npcs.controllers.ScriptController;
import noppes.npcs.controllers.data.PlayerData;
import noppes.npcs.controllers.data.PlayerDataScript;
import noppes.npcs.controllers.data.PlayerQuestData;
import noppes.npcs.scripted.NpcAPI;
import noppes.npcs.util.CustomNPCsThreader;
import noppes.npcs.util.NBTJsonUtil;
@@ -48,6 +48,7 @@ public class ProfileController implements IProfileHandler {
private static final String MSG_SLOT_ALREADY_ACTIVE = "Slot is already active.";
private static final String MSG_REGION_NOT_ALLOWED = "Profile switching not allowed from your current location.";
private static final String MSG_CHANGE_SUCCESS = "Slot changed successfully.";
private static final String MSG_CANCELLED = "Operation cancelled.";
public static Map<String, IProfileData> profileTypes = new HashMap<>();
public static Map<UUID, Profile> activeProfiles = new HashMap<>();
@@ -362,7 +363,21 @@ public class ProfileController implements IProfileHandler {
if (!profile.getSlots().containsKey(slotId)) {
return ProfileOperation.error(MSG_SLOT_NOT_EXIST);
}
PlayerDataScript handler = ScriptController.Instance.playerScripts;
if(profile.player != null){
IPlayer scriptPlayer = (IPlayer) NpcAPI.Instance().getIEntity(profile.player);
if(EventHooks.onProfileRemove(handler, scriptPlayer, profile, slotId, false))
return ProfileOperation.error(MSG_CANCELLED);
}
profile.getSlots().remove(slotId);
if(profile.player != null){
IPlayer scriptPlayer = (IPlayer) NpcAPI.Instance().getIEntity(profile.player);
EventHooks.onProfileRemove(handler, scriptPlayer, profile, slotId, true);
}
return ProfileOperation.success(MSG_REMOVE_SUCCESS);
}
@@ -386,12 +401,23 @@ public class ProfileController implements IProfileHandler {
}
}
}
PlayerDataScript handler = ScriptController.Instance.playerScripts;
if(profile.player != null){
IPlayer scriptPlayer = (IPlayer) NpcAPI.Instance().getIEntity(profile.player);
if(EventHooks.onProfileCreate(handler, scriptPlayer, profile, newSlotId, false))
return ProfileOperation.error(MSG_CANCELLED);
}
Slot newSlot = new Slot(newSlotId, "Slot " + newSlotId);
newSlot.setLastLoaded(System.currentTimeMillis());
profile.getSlots().put(newSlotId, newSlot);
if(profile.player != null){
verifySlotQuests(profile.player);
save(profile.player, profile);
IPlayer scriptPlayer = (IPlayer) NpcAPI.Instance().getIEntity(profile.player);
EventHooks.onProfileCreate(handler, scriptPlayer, profile, newSlotId, true);
}
return ProfileOperation.success(MSG_NEW_SLOT_CREATED);
}
@@ -437,6 +463,7 @@ public class ProfileController implements IProfileHandler {
return ProfileOperation.error(MSG_REGION_NOT_ALLOWED);
}
}
List<IProfileData> dataList = new ArrayList<>(profileTypes.values());
dataList.sort(Comparator.comparingInt(IProfileData::getSwitchPriority));
for (IProfileData pd : dataList) {
@@ -444,9 +471,20 @@ public class ProfileController implements IProfileHandler {
return pd.verifySwitch(profile.player);
}
}
int prevSlot = profile.getCurrentSlotId();
PlayerDataScript handler = ScriptController.Instance.playerScripts;
IPlayer scriptPlayer = (IPlayer) NpcAPI.Instance().getIEntity(profile.player);
if(EventHooks.onProfileChange(handler, scriptPlayer, profile, newSlotId, prevSlot, false))
return ProfileOperation.error(MSG_CANCELLED);
saveSlotData(profile.player);
profile.currentSlotId = newSlotId;
loadSlotData(profile.player);
EventHooks.onProfileChange(handler, scriptPlayer, profile, newSlotId, prevSlot, true);
} else {
return ProfileOperation.error(MSG_PLAYER_NOT_FOUND);
}
+18 -18
View File
@@ -389,6 +389,24 @@ public class EventHooks {
return NpcAPI.EVENT_BUS.post(event);
}
public static boolean onProfileChange(PlayerDataScript handler, IPlayer player, IProfile profile, int newSlot, int prevSlot, boolean post) {
PlayerEvent.ProfileEvent.Changed event = new PlayerEvent.ProfileEvent.Changed(player, profile, newSlot, prevSlot, post);
handler.callScript(EnumScriptType.PROFILE_CHANGE, event);
return NpcAPI.EVENT_BUS.post(event);
}
public static boolean onProfileRemove(PlayerDataScript handler, IPlayer player, IProfile profile, int slot, boolean post) {
PlayerEvent.ProfileEvent.Removed event = new PlayerEvent.ProfileEvent.Removed(player, profile, slot, post);
handler.callScript(EnumScriptType.PROFILE_REMOVE, event);
return NpcAPI.EVENT_BUS.post(event);
}
public static boolean onProfileCreate(PlayerDataScript handler, IPlayer player, IProfile profile, int slot, boolean post) {
PlayerEvent.ProfileEvent.Create event = new PlayerEvent.ProfileEvent.Create(player, profile, slot, post);
handler.callScript(EnumScriptType.PROFILE_CREATE, event);
return NpcAPI.EVENT_BUS.post(event);
}
public static boolean onStartUsingItem(PlayerDataScript handler, IPlayer player, int duration, ItemStack item){
PlayerEvent.StartUsingItem event = new PlayerEvent.StartUsingItem(player, item, duration);
handler.callScript(EnumScriptType.START_USING_ITEM, event);
@@ -919,22 +937,4 @@ public class EventHooks {
}
postAnimationEvent(new AnimationEvent.FrameEvent.Exited(animation, frame));
}
public static boolean onProfileChange(PlayerDataScript handler, IPlayer player, IProfile profile, int slot) {
PlayerEvent.ProfileEvent.Changed event = new PlayerEvent.ProfileEvent.Changed(player, profile, slot, profile.getCurrentSlotId());
handler.callScript(EnumScriptType.PROFILE_CHANGE, event);
return NpcAPI.EVENT_BUS.post(event);
}
public static boolean onProfileRemove(PlayerDataScript handler, IPlayer player, IProfile profile, int slot) {
PlayerEvent.ProfileEvent.Removed event = new PlayerEvent.ProfileEvent.Removed(player, profile, slot);
handler.callScript(EnumScriptType.PROFILE_REMOVE, event);
return NpcAPI.EVENT_BUS.post(event);
}
public static boolean onProfileCreate(PlayerDataScript handler, IPlayer player, IProfile profile, int slot) {
PlayerEvent.ProfileEvent.Create event = new PlayerEvent.ProfileEvent.Create(player, profile, slot);
handler.callScript(EnumScriptType.PROFILE_CREATE, event);
return NpcAPI.EVENT_BUS.post(event);
}
}
@@ -4,6 +4,7 @@ import kamkeel.npcs.network.packets.request.script.PlayerScriptPacket;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.NBTTags;
import noppes.npcs.constants.EnumScriptType;
import noppes.npcs.controllers.ScriptContainer;
import noppes.npcs.controllers.data.PlayerDataScript;
@@ -13,69 +14,72 @@ public class GuiScriptPlayers extends GuiScriptInterface {
private PlayerDataScript script = new PlayerDataScript((EntityPlayer)null);
public GuiScriptPlayers() {
hookList.add("init");
hookList.add("tick");
hookList.add("interact");
hookList.add("attack");
hookList.add("attacked");
hookList.add("damagedEntity");
hookList.add("damaged");
hookList.add("kills");
hookList.add("killed");
hookList.add("drop");
hookList.add("respawn");
hookList.add("breakBlock");
hookList.add("chat");
hookList.add("login");
hookList.add("logout");
hookList.add("keyPressed");
hookList.add("mouseClicked");
hookList.add("toss");
hookList.add("pickUp");
hookList.add("pickupXP");
hookList.add("rangedCharge");
hookList.add("rangedLaunched");
hookList.add("timer");
hookList.add("startItem");
hookList.add("usingItem");
hookList.add("stopItem");
hookList.add("finishItem");
hookList.add("containerOpen");
hookList.add("useHoe");
hookList.add("bonemeal");
hookList.add("fillBucket");
hookList.add("jump");
hookList.add("fall");
hookList.add("wakeUp");
hookList.add("sleep");
hookList.add("playSound");
hookList.add("lightning");
hookList.add("changedDim");
hookList.add("questStart");
hookList.add("questCompleted");
hookList.add("questTurnIn");
hookList.add("factionPoints");
hookList.add("dialogOpen");
hookList.add("dialogOption");
hookList.add("dialogClose");
hookList.add("scriptCommand");
hookList.add("customGuiClosed");
hookList.add("customGuiButton");
hookList.add("customGuiSlot");
hookList.add("customGuiSlotClicked");
hookList.add("customGuiScroll");
hookList.add("customGuiTextfield");
hookList.add("partyQuestCompleted");
hookList.add("partyQuestSet");
hookList.add("partyQuestTurnedIn");
hookList.add("partyInvite");
hookList.add("partyKick");
hookList.add("partyLeave");
hookList.add("partyDisband");
hookList.add("animationStart");
hookList.add("animationEnd");
hookList.add("frameEnter");
hookList.add("frameExit");
hookList.add(EnumScriptType.INIT.function);
hookList.add(EnumScriptType.TICK.function);
hookList.add(EnumScriptType.INTERACT.function);
hookList.add(EnumScriptType.ATTACK.function);
hookList.add(EnumScriptType.ATTACKED.function);
hookList.add(EnumScriptType.DAMAGED_ENTITY.function);
hookList.add(EnumScriptType.DAMAGED.function);
hookList.add(EnumScriptType.KILLS.function);
hookList.add(EnumScriptType.KILLED.function);
hookList.add(EnumScriptType.DROP.function);
hookList.add(EnumScriptType.RESPAWN.function);
hookList.add(EnumScriptType.BREAK_BLOCK.function);
hookList.add(EnumScriptType.CHAT.function);
hookList.add(EnumScriptType.LOGIN.function);
hookList.add(EnumScriptType.LOGOUT.function);
hookList.add(EnumScriptType.KEY_PRESSED.function);
hookList.add(EnumScriptType.MOUSE_CLICKED.function);
hookList.add(EnumScriptType.TOSS.function);
hookList.add(EnumScriptType.PICKUP.function);
hookList.add(EnumScriptType.PICKUP_XP.function);
hookList.add(EnumScriptType.RANGED_CHARGE.function);
hookList.add(EnumScriptType.RANGED_LAUNCHED.function);
hookList.add(EnumScriptType.TIMER.function);
hookList.add(EnumScriptType.START_USING_ITEM.function);
hookList.add(EnumScriptType.USING_ITEM.function);
hookList.add(EnumScriptType.STOP_USING_ITEM.function);
hookList.add(EnumScriptType.FINISH_USING_ITEM.function);
hookList.add(EnumScriptType.CONTAINER_OPEN.function);
hookList.add(EnumScriptType.USE_HOE.function);
hookList.add(EnumScriptType.BONEMEAL.function);
hookList.add(EnumScriptType.FILL_BUCKET.function);
hookList.add(EnumScriptType.JUMP.function);
hookList.add(EnumScriptType.FALL.function);
hookList.add(EnumScriptType.WAKE_UP.function);
hookList.add(EnumScriptType.SLEEP.function);
hookList.add(EnumScriptType.PLAYSOUND.function);
hookList.add(EnumScriptType.LIGHTNING.function);
hookList.add(EnumScriptType.CHANGED_DIM.function);
hookList.add(EnumScriptType.QUEST_START.function);
hookList.add(EnumScriptType.QUEST_COMPLETED.function);
hookList.add(EnumScriptType.QUEST_TURNIN.function);
hookList.add(EnumScriptType.FACTION_POINTS.function);
hookList.add(EnumScriptType.DIALOG_OPEN.function);
hookList.add(EnumScriptType.DIALOG_OPTION.function);
hookList.add(EnumScriptType.DIALOG_CLOSE.function);
hookList.add(EnumScriptType.SCRIPT_COMMAND.function);
hookList.add(EnumScriptType.CUSTOM_GUI_CLOSED.function);
hookList.add(EnumScriptType.CUSTOM_GUI_BUTTON.function);
hookList.add(EnumScriptType.CUSTOM_GUI_SLOT.function);
hookList.add(EnumScriptType.CUSTOM_GUI_SLOT_CLICKED.function);
hookList.add(EnumScriptType.CUSTOM_GUI_SCROLL.function);
hookList.add(EnumScriptType.CUSTOM_GUI_TEXTFIELD.function);
hookList.add(EnumScriptType.PARTY_QUEST_COMPLETED.function);
hookList.add(EnumScriptType.PARTY_QUEST_SET.function);
hookList.add(EnumScriptType.PARTY_QUEST_TURNED_IN.function);
hookList.add(EnumScriptType.PARTY_INVITE.function);
hookList.add(EnumScriptType.PARTY_KICK.function);
hookList.add(EnumScriptType.PARTY_LEAVE.function);
hookList.add(EnumScriptType.PARTY_DISBAND.function);
hookList.add(EnumScriptType.ANIMATION_START.function);
hookList.add(EnumScriptType.ANIMATION_END.function);
hookList.add(EnumScriptType.ANIMATION_FRAME_ENTER.function);
hookList.add(EnumScriptType.ANIMATION_FRAME_EXIT.function);
hookList.add(EnumScriptType.PROFILE_CHANGE.function);
hookList.add(EnumScriptType.PROFILE_REMOVE.function);
hookList.add(EnumScriptType.PROFILE_CREATE.function);
this.handler = this.script;
PlayerScriptPacket.Get();
@@ -1011,11 +1011,13 @@ public class PlayerEvent extends CustomNPCsEvent implements IPlayerEvent {
public static class ProfileEvent extends PlayerEvent implements IPlayerEvent.ProfileEvent {
public final IProfile profile;
public final int slot;
public final boolean post;
public ProfileEvent(IPlayer player, IProfile profile, int slot) {
public ProfileEvent(IPlayer player, IProfile profile, int slot, boolean post) {
super(player);
this.profile = profile;
this.slot = slot;
this.post = post;
}
public String getHookName() {
@@ -1032,12 +1034,17 @@ public class PlayerEvent extends CustomNPCsEvent implements IPlayerEvent {
return this.slot;
}
@Override
public boolean isPost() {
return this.post;
}
@Cancelable
public static class Changed extends PlayerEvent.ProfileEvent implements IPlayerEvent.ProfileEvent.Changed {
public final int prevSlot;
public Changed(IPlayer player, IProfile profile, int slot, int prevSlot) {
super(player, profile, slot);
public Changed(IPlayer player, IProfile profile, int slot, int prevSlot, boolean post) {
super(player, profile, slot, post);
this.prevSlot = prevSlot;
}
@@ -1054,8 +1061,8 @@ public class PlayerEvent extends CustomNPCsEvent implements IPlayerEvent {
@Cancelable
public static class Removed extends PlayerEvent.ProfileEvent implements IPlayerEvent.ProfileEvent.Removed {
public Removed(IPlayer player, IProfile profile, int slot) {
super(player, profile, slot);
public Removed(IPlayer player, IProfile profile, int slot, boolean post) {
super(player, profile, slot, post);
}
public String getHookName() {
@@ -1066,8 +1073,8 @@ public class PlayerEvent extends CustomNPCsEvent implements IPlayerEvent {
@Cancelable
public static class Create extends PlayerEvent.ProfileEvent implements IPlayerEvent.ProfileEvent.Create {
public Create(IPlayer player, IProfile profile, int slot) {
super(player, profile, slot);
public Create(IPlayer player, IProfile profile, int slot, boolean post) {
super(player, profile, slot, post);
}
public String getHookName() {