From fabf4be804a22ee84ec90f40f4340d8936baa998 Mon Sep 17 00:00:00 2001 From: ohAlee Date: Tue, 3 Jan 2023 17:45:26 +0100 Subject: [PATCH] Added broadcast and full conversation type --- .../java/it/ohalee/minecraftgpt/Type.java | 18 ++++++ .../minecraftgpt/command/ChatCommand.java | 62 +++++++++++++++++++ .../minecraftgpt/command/GPTCommand.java | 40 ------------ .../conversation/TypeManager.java | 21 +++++++ 4 files changed, 101 insertions(+), 40 deletions(-) create mode 100644 src/main/java/it/ohalee/minecraftgpt/Type.java create mode 100644 src/main/java/it/ohalee/minecraftgpt/command/ChatCommand.java delete mode 100644 src/main/java/it/ohalee/minecraftgpt/command/GPTCommand.java create mode 100644 src/main/java/it/ohalee/minecraftgpt/conversation/TypeManager.java diff --git a/src/main/java/it/ohalee/minecraftgpt/Type.java b/src/main/java/it/ohalee/minecraftgpt/Type.java new file mode 100644 index 0000000..818ee82 --- /dev/null +++ b/src/main/java/it/ohalee/minecraftgpt/Type.java @@ -0,0 +1,18 @@ +package it.ohalee.minecraftgpt; + +public enum Type { + + SINGLE, + BROADCAST, + FULL; + + public static Type getType(String type) { + return switch (type.toLowerCase()) { + case "single" -> Type.SINGLE; + case "broadcast" -> Type.BROADCAST; + case "full" -> Type.FULL; + default -> null; + }; + } + +} diff --git a/src/main/java/it/ohalee/minecraftgpt/command/ChatCommand.java b/src/main/java/it/ohalee/minecraftgpt/command/ChatCommand.java new file mode 100644 index 0000000..d124bee --- /dev/null +++ b/src/main/java/it/ohalee/minecraftgpt/command/ChatCommand.java @@ -0,0 +1,62 @@ +package it.ohalee.minecraftgpt.command; + +import it.ohalee.minecraftgpt.Main; +import it.ohalee.minecraftgpt.Type; +import it.ohalee.minecraftgpt.conversation.TypeManager; +import lombok.RequiredArgsConstructor; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.List; + +@RequiredArgsConstructor +public class ChatCommand implements TabExecutor { + + private final Main plugin; + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (!(sender instanceof Player player)) { + sender.sendMessage(ChatColor.RED + "Only players can use this command!"); + return true; + } + + if (args.length == 1 && args[0].equalsIgnoreCase("reload")) { + plugin.reloadConfig(); + sender.sendMessage(ChatColor.GREEN + "Config reloaded!"); + return true; + } + + Type type = Type.SINGLE; + if (args.length >= 1) { + type = Type.getType(args[0]); + if (type == null) { + player.sendMessage(plugin.getConfig().getString("command.invalid-type") + .replace("&", "§") + .replace("{types}", String.join(", ", Arrays.stream(Type.values()).map(Enum::name).toArray(String[]::new)))); + return true; + } + } + + if (!player.hasPermission("minecraftgpt.command." + type)) { + player.sendMessage(plugin.getConfig().getString("command.no-permission").replace("&", "§")); + return true; + } + + TypeManager.startConversation(plugin, player, type); + return true; + } + + @Override + public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + return Arrays.stream(Type.values()).map(type -> type.name().toLowerCase()).toList(); + } + + +} diff --git a/src/main/java/it/ohalee/minecraftgpt/command/GPTCommand.java b/src/main/java/it/ohalee/minecraftgpt/command/GPTCommand.java deleted file mode 100644 index 348a9af..0000000 --- a/src/main/java/it/ohalee/minecraftgpt/command/GPTCommand.java +++ /dev/null @@ -1,40 +0,0 @@ -package it.ohalee.minecraftgpt.command; - -import it.ohalee.minecraftgpt.Main; -import lombok.RequiredArgsConstructor; -import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -@RequiredArgsConstructor -public class GPTCommand implements CommandExecutor { - - private final Main plugin; - - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if (!(sender instanceof Player player)) { - sender.sendMessage(ChatColor.RED + "Only players can use this command!"); - return true; - } - - if (!player.hasPermission("minecraftgpt.command")) { - player.sendMessage(plugin.getConfig().getString("command.no-permission").replace("&", "§")); - return true; - } - - if (plugin.getCache().asMap().containsKey(player)) { - plugin.getCache().invalidate(player); - player.sendMessage(plugin.getConfig().getString("command.toggle.disabled").replace("&", "§")); - return true; - } - - plugin.getCache().put(player, new StringBuilder()); - player.sendMessage(plugin.getConfig().getString("command.toggle.enabled").replace("&", "§")); - return true; - } - -} diff --git a/src/main/java/it/ohalee/minecraftgpt/conversation/TypeManager.java b/src/main/java/it/ohalee/minecraftgpt/conversation/TypeManager.java new file mode 100644 index 0000000..81b9f17 --- /dev/null +++ b/src/main/java/it/ohalee/minecraftgpt/conversation/TypeManager.java @@ -0,0 +1,21 @@ +package it.ohalee.minecraftgpt.conversation; + +import it.ohalee.minecraftgpt.Main; +import it.ohalee.minecraftgpt.Type; +import org.bukkit.entity.Player; + +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("&", "§")); + return; + } + + Main.USER_TYPE.put(player, type); + Main.CACHE.put(player, new StringBuilder()); + player.sendMessage(plugin.getConfig().getString("command.toggle.enabled").replace("&", "§")); + } + +}