diff --git a/src/main/java/it/ohalee/minecraftgpt/Main.java b/src/main/java/it/ohalee/minecraftgpt/Main.java index 82b099c..1f4cc09 100644 --- a/src/main/java/it/ohalee/minecraftgpt/Main.java +++ b/src/main/java/it/ohalee/minecraftgpt/Main.java @@ -3,6 +3,7 @@ package it.ohalee.minecraftgpt; import com.google.common.cache.*; import it.ohalee.minecraftgpt.command.ChatCommand; import it.ohalee.minecraftgpt.handler.PlayerHandlers; +import it.ohalee.minecraftgpt.util.Messages; import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -28,7 +29,7 @@ public class Main extends JavaPlugin { } USER_TYPE.invalidate(notification.getKey()); if (notification.getCause() == RemovalCause.EXPIRED) { - notification.getKey().sendMessage(getConfig().getString("command.toggle.disabled").replace("&", "§")); + notification.getKey().sendMessage(Messages.format(getConfig().getString("command.toggle.disabled"))); } }).build(); USER_TYPE = CacheBuilder.newBuilder().build(); diff --git a/src/main/java/it/ohalee/minecraftgpt/command/ChatCommand.java b/src/main/java/it/ohalee/minecraftgpt/command/ChatCommand.java index e57023f..baff1b6 100644 --- a/src/main/java/it/ohalee/minecraftgpt/command/ChatCommand.java +++ b/src/main/java/it/ohalee/minecraftgpt/command/ChatCommand.java @@ -3,6 +3,7 @@ package it.ohalee.minecraftgpt.command; import it.ohalee.minecraftgpt.Main; import it.ohalee.minecraftgpt.Type; import it.ohalee.minecraftgpt.conversation.TypeManager; +import it.ohalee.minecraftgpt.util.Messages; import lombok.RequiredArgsConstructor; import org.bukkit.ChatColor; import org.bukkit.command.Command; @@ -37,15 +38,14 @@ public class ChatCommand implements TabExecutor { if (args.length >= 1) { type = Type.getType(args[0]); if (type == null) { - player.sendMessage(plugin.getConfig().getString("command.invalid-type") - .replace("&", "§") + player.sendMessage(Messages.format(plugin.getConfig().getString("command.invalid-type")) .replace("{types}", String.join(", ", Arrays.stream(Type.values()).map(Enum::name).toArray(String[]::new)))); return true; } } if (!player.hasPermission("minecraftgpt.command." + type.name().toLowerCase())) { - player.sendMessage(plugin.getConfig().getString("command.no-permission").replace("&", "§")); + player.sendMessage(Messages.format(plugin.getConfig().getString("command.no-permission"))); return true; } diff --git a/src/main/java/it/ohalee/minecraftgpt/conversation/TypeManager.java b/src/main/java/it/ohalee/minecraftgpt/conversation/TypeManager.java index 81b9f17..43f855b 100644 --- a/src/main/java/it/ohalee/minecraftgpt/conversation/TypeManager.java +++ b/src/main/java/it/ohalee/minecraftgpt/conversation/TypeManager.java @@ -2,6 +2,7 @@ package it.ohalee.minecraftgpt.conversation; import it.ohalee.minecraftgpt.Main; import it.ohalee.minecraftgpt.Type; +import it.ohalee.minecraftgpt.util.Messages; import org.bukkit.entity.Player; public class TypeManager { @@ -9,13 +10,13 @@ public class TypeManager { public static void startConversation(Main plugin, Player player, Type type) { if (Main.CACHE.asMap().containsKey(player)) { Main.CACHE.invalidate(player); - player.sendMessage(plugin.getConfig().getString("command.toggle.disabled").replace("&", "§")); + player.sendMessage(Messages.format(plugin.getConfig().getString("command.toggle.disabled"))); return; } Main.USER_TYPE.put(player, type); Main.CACHE.put(player, new StringBuilder()); - player.sendMessage(plugin.getConfig().getString("command.toggle.enabled").replace("&", "§")); + player.sendMessage(Messages.format(plugin.getConfig().getString("command.toggle.enabled"))); } } diff --git a/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java b/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java index 83b4dc9..fe7d867 100644 --- a/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java +++ b/src/main/java/it/ohalee/minecraftgpt/handler/PlayerHandlers.java @@ -3,6 +3,7 @@ package it.ohalee.minecraftgpt.handler; import it.ohalee.minecraftgpt.Main; import it.ohalee.minecraftgpt.OpenAI; import it.ohalee.minecraftgpt.Type; +import it.ohalee.minecraftgpt.util.Messages; import lombok.RequiredArgsConstructor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -10,6 +11,8 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerQuitEvent; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.Collections; @@ -44,10 +47,7 @@ public class PlayerHandlers implements Listener { if (!plugin.getConfig().getBoolean("use-default-chat", false)) { e.setCancelled(true); - for (Player rec : recipients) - rec.sendMessage(list.get(0).replace("&", "§") - .replace("%message%", e.getMessage()) - .replace("%player%", player.getName())); + sendMessage(format(list.get(0), e.getMessage(), player.getName()), recipients); } StringBuilder builder = Main.CACHE.getIfPresent(player); @@ -56,15 +56,22 @@ public class PlayerHandlers implements Listener { OpenAI.getResponse(builder, e.getMessage()).whenComplete((response, throwable) -> { if (throwable != null) { throwable.printStackTrace(); - player.sendMessage(plugin.getConfig().getString("command.error").replace("&", "§")); + player.sendMessage(Messages.format(plugin.getConfig().getString("command.error"))); return; } - - for (Player rec : recipients) - rec.sendMessage(list.get(1).replace("&", "§") - .replace("%message%", response) - .replace("%player%", player.getName())); + sendMessage(list.get(1), recipients); }); } + private String format(String str, String message, String player) { + return Messages.format(str).replace("%message%", message).replace("%player%", player); + } + + private void sendMessage(String message, Collection players) { + for (Player player : players) + player.sendMessage(message); + if (plugin.getConfig().getBoolean("send-messages-to-console", true)) + plugin.getServer().getConsoleSender().sendMessage(message); + } + } diff --git a/src/main/java/it/ohalee/minecraftgpt/util/Messages.java b/src/main/java/it/ohalee/minecraftgpt/util/Messages.java new file mode 100644 index 0000000..2441c92 --- /dev/null +++ b/src/main/java/it/ohalee/minecraftgpt/util/Messages.java @@ -0,0 +1,13 @@ +package it.ohalee.minecraftgpt.util; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class Messages { + + public static @NotNull String format(@Nullable String str) { + if (str == null) return "Error - Bad Config"; + return str.replace("&", "§"); + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 3297cb3..e93892e 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -6,6 +6,7 @@ command: disabled: "&cChatGPT has disconnected." error: "&cAn error occurred while processing your message." +send-messages-to-console: true use-default-chat: false format: - "&b%player%: &7%message%"