Added option to send messages to console.

This commit is contained in:
ohAlee 2023-01-07 15:39:55 +01:00
parent e477912030
commit 2a2337aa11
6 changed files with 39 additions and 16 deletions

View File

@ -3,6 +3,7 @@ package it.ohalee.minecraftgpt;
import com.google.common.cache.*; import com.google.common.cache.*;
import it.ohalee.minecraftgpt.command.ChatCommand; import it.ohalee.minecraftgpt.command.ChatCommand;
import it.ohalee.minecraftgpt.handler.PlayerHandlers; import it.ohalee.minecraftgpt.handler.PlayerHandlers;
import it.ohalee.minecraftgpt.util.Messages;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -28,7 +29,7 @@ public class Main extends JavaPlugin {
} }
USER_TYPE.invalidate(notification.getKey()); USER_TYPE.invalidate(notification.getKey());
if (notification.getCause() == RemovalCause.EXPIRED) { 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(); }).build();
USER_TYPE = CacheBuilder.newBuilder().build(); USER_TYPE = CacheBuilder.newBuilder().build();

View File

@ -3,6 +3,7 @@ package it.ohalee.minecraftgpt.command;
import it.ohalee.minecraftgpt.Main; import it.ohalee.minecraftgpt.Main;
import it.ohalee.minecraftgpt.Type; import it.ohalee.minecraftgpt.Type;
import it.ohalee.minecraftgpt.conversation.TypeManager; import it.ohalee.minecraftgpt.conversation.TypeManager;
import it.ohalee.minecraftgpt.util.Messages;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -37,15 +38,14 @@ public class ChatCommand implements TabExecutor {
if (args.length >= 1) { if (args.length >= 1) {
type = Type.getType(args[0]); type = Type.getType(args[0]);
if (type == null) { if (type == null) {
player.sendMessage(plugin.getConfig().getString("command.invalid-type") player.sendMessage(Messages.format(plugin.getConfig().getString("command.invalid-type"))
.replace("&", "§")
.replace("{types}", String.join(", ", Arrays.stream(Type.values()).map(Enum::name).toArray(String[]::new)))); .replace("{types}", String.join(", ", Arrays.stream(Type.values()).map(Enum::name).toArray(String[]::new))));
return true; return true;
} }
} }
if (!player.hasPermission("minecraftgpt.command." + type.name().toLowerCase())) { 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; return true;
} }

View File

@ -2,6 +2,7 @@ package it.ohalee.minecraftgpt.conversation;
import it.ohalee.minecraftgpt.Main; import it.ohalee.minecraftgpt.Main;
import it.ohalee.minecraftgpt.Type; import it.ohalee.minecraftgpt.Type;
import it.ohalee.minecraftgpt.util.Messages;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class TypeManager { public class TypeManager {
@ -9,13 +10,13 @@ public class TypeManager {
public static void startConversation(Main plugin, Player player, Type type) { public static void startConversation(Main plugin, Player player, Type type) {
if (Main.CACHE.asMap().containsKey(player)) { if (Main.CACHE.asMap().containsKey(player)) {
Main.CACHE.invalidate(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; return;
} }
Main.USER_TYPE.put(player, type); Main.USER_TYPE.put(player, type);
Main.CACHE.put(player, new StringBuilder()); 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")));
} }
} }

View File

@ -3,6 +3,7 @@ package it.ohalee.minecraftgpt.handler;
import it.ohalee.minecraftgpt.Main; import it.ohalee.minecraftgpt.Main;
import it.ohalee.minecraftgpt.OpenAI; import it.ohalee.minecraftgpt.OpenAI;
import it.ohalee.minecraftgpt.Type; import it.ohalee.minecraftgpt.Type;
import it.ohalee.minecraftgpt.util.Messages;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -10,6 +11,8 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -44,10 +47,7 @@ public class PlayerHandlers implements Listener {
if (!plugin.getConfig().getBoolean("use-default-chat", false)) { if (!plugin.getConfig().getBoolean("use-default-chat", false)) {
e.setCancelled(true); e.setCancelled(true);
for (Player rec : recipients) sendMessage(format(list.get(0), e.getMessage(), player.getName()), recipients);
rec.sendMessage(list.get(0).replace("&", "§")
.replace("%message%", e.getMessage())
.replace("%player%", player.getName()));
} }
StringBuilder builder = Main.CACHE.getIfPresent(player); StringBuilder builder = Main.CACHE.getIfPresent(player);
@ -56,15 +56,22 @@ public class PlayerHandlers implements Listener {
OpenAI.getResponse(builder, e.getMessage()).whenComplete((response, throwable) -> { OpenAI.getResponse(builder, e.getMessage()).whenComplete((response, throwable) -> {
if (throwable != null) { if (throwable != null) {
throwable.printStackTrace(); throwable.printStackTrace();
player.sendMessage(plugin.getConfig().getString("command.error").replace("&", "§")); player.sendMessage(Messages.format(plugin.getConfig().getString("command.error")));
return; return;
} }
sendMessage(list.get(1), recipients);
for (Player rec : recipients)
rec.sendMessage(list.get(1).replace("&", "§")
.replace("%message%", response)
.replace("%player%", player.getName()));
}); });
} }
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<Player> players) {
for (Player player : players)
player.sendMessage(message);
if (plugin.getConfig().getBoolean("send-messages-to-console", true))
plugin.getServer().getConsoleSender().sendMessage(message);
}
} }

View File

@ -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("&", "§");
}
}

View File

@ -6,6 +6,7 @@ command:
disabled: "&cChatGPT has disconnected." disabled: "&cChatGPT has disconnected."
error: "&cAn error occurred while processing your message." error: "&cAn error occurred while processing your message."
send-messages-to-console: true
use-default-chat: false use-default-chat: false
format: format:
- "&b%player%: &7%message%" - "&b%player%: &7%message%"