fix player chat action

This commit is contained in:
Pyrbu 2023-06-13 23:51:25 +02:00
parent 7a3bf0deb0
commit fa890e3924
6 changed files with 32 additions and 11 deletions

@ -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) {

@ -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))))

@ -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<PlayerChatAction>, InteractionCommandHandler {
private final TaskScheduler scheduler;
public PlayerChatActionType() {
public PlayerChatActionType(TaskScheduler scheduler) {
this.scheduler = scheduler;
}
@Override
@ -25,7 +28,7 @@ public class PlayerChatActionType implements InteractionActionType<PlayerChatAct
@Override
public PlayerChatAction deserialize(String str) {
String[] split = str.split(";");
return new PlayerChatAction(new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), InteractionType.valueOf(split[2]), Long.parseLong(split[1]));
return new PlayerChatAction(scheduler, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), InteractionType.valueOf(split[2]), Long.parseLong(split[1]));
}
@Override
@ -48,7 +51,7 @@ public class PlayerChatActionType implements InteractionActionType<PlayerChatAct
InteractionType type = context.parse(InteractionType.class);
long cooldown = (long) (context.parse(Double.class) * 1000D);
String message = context.dumpAllArgs();
return new PlayerChatAction(message, type, cooldown);
return new PlayerChatAction(scheduler, message, type, cooldown);
}
@Override

@ -14,6 +14,16 @@ public class FoliaScheduler extends TaskScheduler {
super(plugin);
}
@Override
public void schedulePlayerChat(Player player, String chat) {
try {
Object scheduler = Reflections.FOLIA_GET_REGION_SCHEDULER.get().invoke(null);
Reflections.FOLIA_EXECUTE_REGION.get().invoke(scheduler, plugin, player.getLocation(), (Runnable) () -> player.chat(chat));
} catch (InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Override
public void schedulePlayerCommand(Player player, String command) {
try {

@ -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));

@ -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);