Update from Git Manager GUI
This commit is contained in:
422
src/main/java/de/ticketsystem/commands/TicketCommand.java
Normal file
422
src/main/java/de/ticketsystem/commands/TicketCommand.java
Normal file
@@ -0,0 +1,422 @@
|
||||
|
||||
package de.ticketsystem.commands;
|
||||
|
||||
import de.ticketsystem.TicketPlugin;
|
||||
import de.ticketsystem.model.Ticket;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class TicketCommand implements CommandExecutor, TabCompleter {
|
||||
// Platzhalter für Admin-Kommandos
|
||||
private void handleMigrate(Player player, String[] args) {
|
||||
if (!player.hasPermission("ticket.admin")) {
|
||||
player.sendMessage(plugin.formatMessage("messages.no-permission"));
|
||||
return;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
player.sendMessage(plugin.color("&cBenutzung: /ticket migrate <tomysql|tofile>"));
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
int migrated = 0;
|
||||
String mode = args[1].toLowerCase();
|
||||
if (mode.equals("tomysql")) {
|
||||
migrated = plugin.getDatabaseManager().migrateToMySQL();
|
||||
} else if (mode.equals("tofile")) {
|
||||
migrated = plugin.getDatabaseManager().migrateToFile();
|
||||
} else {
|
||||
player.sendMessage(plugin.formatMessage("messages.unknown-mode"));
|
||||
return;
|
||||
}
|
||||
int finalMigrated = migrated;
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
if (finalMigrated > 0) {
|
||||
player.sendMessage(plugin.formatMessage("messages.migration-success")
|
||||
.replace("{count}", String.valueOf(finalMigrated)));
|
||||
} else {
|
||||
player.sendMessage(plugin.formatMessage("messages.migration-fail"));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private void handleExport(Player player, String[] args) {
|
||||
if (!player.hasPermission("ticket.admin")) {
|
||||
player.sendMessage(plugin.formatMessage("messages.no-permission"));
|
||||
return;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
player.sendMessage(plugin.color("&cBenutzung: /ticket export <Dateiname>"));
|
||||
return;
|
||||
}
|
||||
String filename = args[1];
|
||||
File exportFile = new File(plugin.getDataFolder(), filename);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
int count = plugin.getDatabaseManager().exportTickets(exportFile);
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
if (count > 0) {
|
||||
player.sendMessage(plugin.formatMessage("messages.export-success")
|
||||
.replace("{count}", String.valueOf(count)).replace("{file}", filename));
|
||||
} else {
|
||||
player.sendMessage(plugin.formatMessage("messages.export-fail"));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private void handleImport(Player player, String[] args) {
|
||||
if (!player.hasPermission("ticket.admin")) {
|
||||
player.sendMessage(plugin.formatMessage("messages.no-permission"));
|
||||
return;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
player.sendMessage(plugin.color("&cBenutzung: /ticket import <Dateiname>"));
|
||||
return;
|
||||
}
|
||||
String filename = args[1];
|
||||
File importFile = new File(plugin.getDataFolder(), filename);
|
||||
if (!importFile.exists()) {
|
||||
player.sendMessage(plugin.formatMessage("messages.file-not-found").replace("{file}", filename));
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
int count = plugin.getDatabaseManager().importTickets(importFile);
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
if (count > 0) {
|
||||
player.sendMessage(plugin.formatMessage("messages.import-success")
|
||||
.replace("{count}", String.valueOf(count)));
|
||||
} else {
|
||||
player.sendMessage(plugin.formatMessage("messages.import-fail"));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private void handleStats(Player player) {
|
||||
if (!player.hasPermission("ticket.admin")) {
|
||||
player.sendMessage(plugin.formatMessage("messages.no-permission"));
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
var stats = plugin.getDatabaseManager().getTicketStats();
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
player.sendMessage("§6--- Ticket Statistik ---");
|
||||
player.sendMessage("§eGesamt: §a" + stats.total + " §7| §eOffen: §a" + stats.open + " §7| §eGeschlossen: §a" + stats.closed + " §7| §eWeitergeleitet: §a" + stats.forwarded);
|
||||
player.sendMessage("§6Top Ersteller:");
|
||||
stats.byPlayer.entrySet().stream().sorted((a,b)->b.getValue()-a.getValue()).limit(5).forEach(e ->
|
||||
player.sendMessage("§e" + e.getKey() + ": §a" + e.getValue())
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ─────────────────────────── /ticket archive ────────────────────────────
|
||||
private void handleArchive(Player player) {
|
||||
if (!player.hasPermission("ticket.admin")) {
|
||||
player.sendMessage(plugin.formatMessage("messages.no-permission"));
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
int count = plugin.getDatabaseManager().archiveClosedTickets();
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
if (count > 0) {
|
||||
player.sendMessage(plugin.formatMessage("messages.archive-success")
|
||||
.replace("{count}", String.valueOf(count)));
|
||||
} else {
|
||||
player.sendMessage(plugin.formatMessage("messages.archive-fail"));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private final TicketPlugin plugin;
|
||||
|
||||
public TicketCommand(TicketPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command,
|
||||
String label, String[] args) {
|
||||
|
||||
if (!(sender instanceof Player player)) {
|
||||
sender.sendMessage("Dieser Befehl kann nur von Spielern ausgeführt werden.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length == 0) {
|
||||
plugin.getTicketManager().sendHelpMessage(player);
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (args[0].toLowerCase()) {
|
||||
case "create" -> handleCreate(player, args);
|
||||
case "list" -> handleList(player);
|
||||
case "claim" -> handleClaim(player, args);
|
||||
case "close" -> handleClose(player, args);
|
||||
case "forward" -> handleForward(player, args);
|
||||
case "reload" -> handleReload(player);
|
||||
case "migrate" -> handleMigrate(player, args);
|
||||
case "export" -> handleExport(player, args);
|
||||
case "import" -> handleImport(player, args);
|
||||
case "stats" -> handleStats(player);
|
||||
case "archive" -> handleArchive(player);
|
||||
default -> plugin.getTicketManager().sendHelpMessage(player);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// Methoden wie handleMigrate, handleCreate, handleList, handleClaim, handleClose, handleForward, handleReload, handleStats müssen auf Klassenebene stehen und dürfen nicht innerhalb von onCommand oder anderen Methoden verschachtelt sein.
|
||||
// Entferne alle verschachtelten Methoden und stelle sicher, dass jede Methode nur einmal und auf Klassenebene existiert.
|
||||
|
||||
// ─────────────────────────── /ticket create ────────────────────────────
|
||||
|
||||
private void handleCreate(Player player, String[] args) {
|
||||
if (!player.hasPermission("ticket.create")) {
|
||||
player.sendMessage(plugin.formatMessage("messages.no-permission"));
|
||||
return;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
player.sendMessage(plugin.color("&cBenutzung: /ticket create <Beschreibung>"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Cooldown-Check
|
||||
if (plugin.getTicketManager().hasCooldown(player.getUniqueId())) {
|
||||
long remaining = plugin.getTicketManager().getRemainingCooldown(player.getUniqueId());
|
||||
player.sendMessage(plugin.formatMessage("messages.cooldown")
|
||||
.replace("{seconds}", String.valueOf(remaining)));
|
||||
return;
|
||||
}
|
||||
|
||||
// Ticket-Limit-Check
|
||||
if (plugin.getTicketManager().hasReachedTicketLimit(player.getUniqueId())) {
|
||||
int max = plugin.getConfig().getInt("max-open-tickets-per-player", 2);
|
||||
player.sendMessage(plugin.color("&cDu hast bereits &e" + max + " &coffene Ticket(s). Bitte warte, bis dein Ticket bearbeitet wurde."));
|
||||
return;
|
||||
}
|
||||
|
||||
// Nachricht zusammenbauen
|
||||
String message = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
|
||||
int maxLen = plugin.getConfig().getInt("max-description-length", 100);
|
||||
if (message.length() > maxLen) {
|
||||
player.sendMessage(plugin.color("&cDeine Beschreibung ist zu lang! Maximal " + maxLen + " Zeichen."));
|
||||
return;
|
||||
}
|
||||
|
||||
// Ticket asynchron in DB speichern
|
||||
Ticket ticket = new Ticket(player.getUniqueId(), player.getName(), message, player.getLocation());
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
int id = plugin.getDatabaseManager().createTicket(ticket);
|
||||
if (id == -1) {
|
||||
player.sendMessage(plugin.color("&cFehler beim Erstellen des Tickets! Bitte wende dich an einen Admin."));
|
||||
return;
|
||||
}
|
||||
ticket.setId(id);
|
||||
plugin.getTicketManager().setCooldown(player.getUniqueId());
|
||||
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
player.sendMessage(plugin.formatMessage("messages.ticket-created")
|
||||
.replace("{id}", String.valueOf(id)));
|
||||
|
||||
// Team benachrichtigen
|
||||
plugin.getTicketManager().notifyTeam(ticket);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ─────────────────────────── /ticket list ──────────────────────────────
|
||||
|
||||
private void handleList(Player player) {
|
||||
if (!player.hasPermission("ticket.support") && !player.hasPermission("ticket.admin")) {
|
||||
player.sendMessage(plugin.formatMessage("messages.no-permission"));
|
||||
return;
|
||||
}
|
||||
// GUI öffnen (synchron, Datenbankabfrage läuft darin async)
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () ->
|
||||
Bukkit.getScheduler().runTask(plugin, () ->
|
||||
plugin.getTicketGUI().openGUI(player)));
|
||||
}
|
||||
|
||||
// ─────────────────────────── /ticket claim ─────────────────────────────
|
||||
|
||||
private void handleClaim(Player player, String[] args) {
|
||||
if (!player.hasPermission("ticket.support") && !player.hasPermission("ticket.admin")) {
|
||||
player.sendMessage(plugin.formatMessage("messages.no-permission"));
|
||||
return;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
player.sendMessage(plugin.color("&cBenutzung: /ticket claim <ID>"));
|
||||
return;
|
||||
}
|
||||
|
||||
int id;
|
||||
try { id = Integer.parseInt(args[1]); }
|
||||
catch (NumberFormatException e) {
|
||||
player.sendMessage(plugin.color("&cUngültige ID!"));
|
||||
return;
|
||||
}
|
||||
|
||||
final int ticketId = id;
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
boolean success = plugin.getDatabaseManager().claimTicket(
|
||||
ticketId, player.getUniqueId(), player.getName());
|
||||
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
if (!success) {
|
||||
player.sendMessage(plugin.formatMessage("messages.already-claimed"));
|
||||
return;
|
||||
}
|
||||
Ticket ticket = plugin.getDatabaseManager().getTicketById(ticketId);
|
||||
if (ticket == null) return;
|
||||
|
||||
player.sendMessage(plugin.formatMessage("messages.ticket-claimed")
|
||||
.replace("{id}", String.valueOf(ticketId))
|
||||
.replace("{player}", ticket.getCreatorName()));
|
||||
|
||||
plugin.getTicketManager().notifyCreatorClaimed(ticket);
|
||||
|
||||
// Zur Ticket-Position teleportieren
|
||||
if (ticket.getLocation() != null) {
|
||||
player.teleport(ticket.getLocation());
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ─────────────────────────── /ticket close ─────────────────────────────
|
||||
|
||||
private void handleClose(Player player, String[] args) {
|
||||
if (!player.hasPermission("ticket.support") && !player.hasPermission("ticket.admin")) {
|
||||
player.sendMessage(plugin.formatMessage("messages.no-permission"));
|
||||
return;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
player.sendMessage(plugin.color("&cBenutzung: /ticket close <ID>"));
|
||||
return;
|
||||
}
|
||||
|
||||
int id;
|
||||
try { id = Integer.parseInt(args[1]); }
|
||||
catch (NumberFormatException e) {
|
||||
player.sendMessage(plugin.color("&cUngültige ID!"));
|
||||
return;
|
||||
}
|
||||
|
||||
final int ticketId = id;
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
boolean success = plugin.getDatabaseManager().closeTicket(ticketId);
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
if (success) {
|
||||
player.sendMessage(plugin.formatMessage("messages.ticket-closed")
|
||||
.replace("{id}", String.valueOf(ticketId)));
|
||||
} else {
|
||||
player.sendMessage(plugin.formatMessage("messages.ticket-not-found"));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ─────────────────────────── /ticket forward ───────────────────────────
|
||||
|
||||
private void handleForward(Player player, String[] args) {
|
||||
if (!player.hasPermission("ticket.admin")) {
|
||||
player.sendMessage(plugin.formatMessage("messages.no-permission"));
|
||||
return;
|
||||
}
|
||||
if (args.length < 3) {
|
||||
player.sendMessage(plugin.color("&cBenutzung: /ticket forward <ID> <Spieler>"));
|
||||
return;
|
||||
}
|
||||
|
||||
int id;
|
||||
try { id = Integer.parseInt(args[1]); }
|
||||
catch (NumberFormatException e) {
|
||||
player.sendMessage(plugin.color("&cUngültige ID!"));
|
||||
return;
|
||||
}
|
||||
|
||||
Player target = Bukkit.getPlayer(args[2]);
|
||||
if (target == null || !target.isOnline()) {
|
||||
player.sendMessage(plugin.color("&cSpieler &e" + args[2] + " &cist nicht online!"));
|
||||
return;
|
||||
}
|
||||
|
||||
final int ticketId = id;
|
||||
final Player finalTarget = target;
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
boolean success = plugin.getDatabaseManager().forwardTicket(
|
||||
ticketId, finalTarget.getUniqueId(), finalTarget.getName());
|
||||
|
||||
if (success) {
|
||||
Ticket ticket = plugin.getDatabaseManager().getTicketById(ticketId);
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
player.sendMessage(plugin.formatMessage("messages.ticket-forwarded")
|
||||
.replace("{id}", String.valueOf(ticketId))
|
||||
.replace("{player}", finalTarget.getName()));
|
||||
if (ticket != null) plugin.getTicketManager().notifyForwardedTo(ticket);
|
||||
});
|
||||
} else {
|
||||
Bukkit.getScheduler().runTask(plugin, () ->
|
||||
player.sendMessage(plugin.formatMessage("messages.ticket-not-found")));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ─────────────────────────── /ticket reload ────────────────────────────
|
||||
|
||||
private void handleReload(Player player) {
|
||||
if (!player.hasPermission("ticket.admin")) {
|
||||
player.sendMessage(plugin.formatMessage("messages.no-permission"));
|
||||
return;
|
||||
}
|
||||
plugin.reloadConfig();
|
||||
player.sendMessage(plugin.color("&aKonfiguration wurde neu geladen."));
|
||||
}
|
||||
|
||||
// ─────────────────────────── Tab-Completion ────────────────────────────
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command command,
|
||||
String label, String[] args) {
|
||||
List<String> completions = new ArrayList<>();
|
||||
if (!(sender instanceof Player player)) return completions;
|
||||
|
||||
if (args.length == 1) {
|
||||
List<String> subs = new ArrayList<>();
|
||||
subs.add("create");
|
||||
if (player.hasPermission("ticket.support") || player.hasPermission("ticket.admin")) {
|
||||
subs.addAll(List.of("list", "claim", "close"));
|
||||
}
|
||||
if (player.hasPermission("ticket.admin")) {
|
||||
subs.addAll(List.of("forward", "reload"));
|
||||
}
|
||||
for (String s : subs) {
|
||||
if (s.startsWith(args[0].toLowerCase())) completions.add(s);
|
||||
}
|
||||
} else if (args.length == 3 && args[0].equalsIgnoreCase("forward")) {
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
if (p.getName().toLowerCase().startsWith(args[2].toLowerCase()))
|
||||
completions.add(p.getName());
|
||||
}
|
||||
}
|
||||
return completions;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user