Added broadcast and full conversation type

This commit is contained in:
ohAlee 2023-01-03 17:45:26 +01:00
parent 131a726da7
commit fabf4be804
4 changed files with 101 additions and 40 deletions

View File

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

View File

@ -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<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
return Arrays.stream(Type.values()).map(type -> type.name().toLowerCase()).toList();
}
}

View File

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

View File

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