From fa890e3924449a9e9ac1bfe2bbe76312ebd258b1 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Tue, 13 Jun 2023 23:51:25 +0200 Subject: [PATCH] fix player chat action --- .../znpcsplus/interaction/ActionRegistry.java | 2 +- .../interaction/playerchat/PlayerChatAction.java | 16 +++++++++------- .../playerchat/PlayerChatActionType.java | 9 ++++++--- .../pyr/znpcsplus/scheduling/FoliaScheduler.java | 10 ++++++++++ .../znpcsplus/scheduling/SpigotScheduler.java | 5 +++++ .../pyr/znpcsplus/scheduling/TaskScheduler.java | 1 + 6 files changed, 32 insertions(+), 11 deletions(-) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/ActionRegistry.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/ActionRegistry.java index a6cfe00..8f4a8c7 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/ActionRegistry.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/ActionRegistry.java @@ -27,7 +27,7 @@ public class ActionRegistry { register(new PlayerCommandActionType(taskScheduler)); register(new SwitchServerActionType(bungeeConnector)); register(new MessageActionType(adventure, textSerializer)); - register(new PlayerChatActionType()); + register(new PlayerChatActionType(taskScheduler)); } public void register(InteractionActionType type) { diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playerchat/PlayerChatAction.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playerchat/PlayerChatAction.java index 3871ef3..7882646 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playerchat/PlayerChatAction.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playerchat/PlayerChatAction.java @@ -3,42 +3,44 @@ package lol.pyr.znpcsplus.interaction.playerchat; import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.znpcsplus.api.interaction.InteractionType; import lol.pyr.znpcsplus.interaction.InteractionAction; +import lol.pyr.znpcsplus.scheduling.TaskScheduler; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.format.NamedTextColor; -import net.kyori.adventure.text.format.TextDecoration; import org.bukkit.entity.Player; public class PlayerChatAction extends InteractionAction { private final String message; + private final TaskScheduler scheduler; - public PlayerChatAction(String message, InteractionType interactionType, long delay) { + public PlayerChatAction(TaskScheduler scheduler, String message, InteractionType interactionType, long delay) { super(delay, interactionType); this.message = message; + this.scheduler = scheduler; } @Override public void run(Player player) { - player.chat(message.replace("{player}", player.getName()) + scheduler.schedulePlayerChat(player, message.replace("{player}", player.getName()) .replace("{uuid}", player.getUniqueId().toString())); } @Override public Component getInfo(String id, int index, CommandContext context) { return Component.text(index + ") ", NamedTextColor.GOLD) - .append(Component.text("[EDIT]", NamedTextColor.DARK_GREEN, TextDecoration.BOLD) + .append(Component.text("[EDIT]", NamedTextColor.DARK_GREEN) .hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, Component.text("Click to edit this action", NamedTextColor.GRAY))) .clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + context.getLabel() + " action edit " + id + " " + index + " playerchat " + " " + getInteractionType().name() + " " + getCooldown()/1000 + " " + message)) - .append(Component.text(" | ", NamedTextColor.GRAY).decoration(TextDecoration.BOLD, false)) - .append(Component.text("[DELETE]", NamedTextColor.RED, TextDecoration.BOLD) + .append(Component.text(" | ", NamedTextColor.GRAY)) + .append(Component.text("[DELETE]", NamedTextColor.RED) .hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, Component.text("Click to delete this action", NamedTextColor.GRAY))) .clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + context.getLabel() + " action delete " + id + " " + index))) - .append(Component.text(" | ", NamedTextColor.GRAY).style(style -> style.decoration(TextDecoration.BOLD, false))) + .append(Component.text(" | ", NamedTextColor.GRAY)) .append(Component.text("Player Chat: ", NamedTextColor.GREEN) .hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, Component.text("Click Type: " + getInteractionType().name() + " Cooldown: " + getCooldown()/1000, NamedTextColor.GREEN)))) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playerchat/PlayerChatActionType.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playerchat/PlayerChatActionType.java index 4d1853d..5815ea2 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playerchat/PlayerChatActionType.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playerchat/PlayerChatActionType.java @@ -6,6 +6,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType; import lol.pyr.znpcsplus.interaction.InteractionAction; import lol.pyr.znpcsplus.interaction.InteractionActionType; import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; +import lol.pyr.znpcsplus.scheduling.TaskScheduler; import java.nio.charset.StandardCharsets; import java.util.Base64; @@ -13,8 +14,10 @@ import java.util.Collections; import java.util.List; public class PlayerChatActionType implements InteractionActionType, InteractionCommandHandler { + private final TaskScheduler scheduler; - public PlayerChatActionType() { + public PlayerChatActionType(TaskScheduler scheduler) { + this.scheduler = scheduler; } @Override @@ -25,7 +28,7 @@ public class PlayerChatActionType implements InteractionActionType player.chat(chat)); + } catch (InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + @Override public void schedulePlayerCommand(Player player, String command) { try { diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/scheduling/SpigotScheduler.java b/plugin/src/main/java/lol/pyr/znpcsplus/scheduling/SpigotScheduler.java index 21096c5..3af2e68 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/scheduling/SpigotScheduler.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/scheduling/SpigotScheduler.java @@ -9,6 +9,11 @@ public class SpigotScheduler extends TaskScheduler { super(plugin); } + @Override + public void schedulePlayerChat(Player player, String chat) { + runSyncGlobal(() -> player.chat(chat)); + } + @Override public void schedulePlayerCommand(Player player, String command) { runSyncGlobal(() -> Bukkit.dispatchCommand(player, command)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/scheduling/TaskScheduler.java b/plugin/src/main/java/lol/pyr/znpcsplus/scheduling/TaskScheduler.java index ddb71b9..cf6d19d 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/scheduling/TaskScheduler.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/scheduling/TaskScheduler.java @@ -10,6 +10,7 @@ public abstract class TaskScheduler { this.plugin = plugin; } + public abstract void schedulePlayerChat(Player player, String message); public abstract void schedulePlayerCommand(Player player, String command); public abstract void runSyncGlobal(Runnable runnable); public abstract void runLaterAsync(Runnable runnable, long delay);