From 1ffec45186e95596ceba88731bd9ded8dab982ad Mon Sep 17 00:00:00 2001 From: Sven wiltink Date: Sat, 31 Aug 2019 23:35:47 +0200 Subject: [PATCH] cleaned up the modreq command --- src/main/java/modreq/Message.java | 12 +++ src/main/java/modreq/MessageType.java | 12 ++- .../java/modreq/commands/ModhelpCommand.java | 2 - .../java/modreq/commands/ModreqCommand.java | 75 +++++++++---------- src/main/resources/messages.yml | 3 +- 5 files changed, 61 insertions(+), 43 deletions(-) diff --git a/src/main/java/modreq/Message.java b/src/main/java/modreq/Message.java index 178eb68..b793f77 100644 --- a/src/main/java/modreq/Message.java +++ b/src/main/java/modreq/Message.java @@ -1,7 +1,10 @@ package modreq; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import java.util.Map; + public class Message { public static void sendToPlayer(MessageType MessageType, Player player, int ticket, String comment) { @@ -15,4 +18,13 @@ public static void sendToPlayer(MessageType MessageType, Player player) { public static void sendToPlayer(MessageType MessageType, Player player, String number) { player.sendMessage(MessageType.format(player.getName(), number, "")); } + + public static void sendToAdmins(MessageType MessageType, Mapparameters) { + String message = MessageType.formatWithParameters(parameters); + for (Player op : Bukkit.getOnlinePlayers()) { + if (op.hasPermission("modreq.check")) { + op.sendMessage(message); + } + } + } } diff --git a/src/main/java/modreq/MessageType.java b/src/main/java/modreq/MessageType.java index 9544c28..cf80dd0 100644 --- a/src/main/java/modreq/MessageType.java +++ b/src/main/java/modreq/MessageType.java @@ -2,6 +2,8 @@ import org.bukkit.ChatColor; +import java.util.Map; + public enum MessageType { ERROR_PERMISSION(), @@ -94,10 +96,18 @@ public String format(String PlayerName, String TicketNumber, String comment) { return message; } + public String formatWithParameters(Map parameters) { + for (Map.Entry entry : parameters.entrySet()) { + message = message.replace("&" + entry.getKey(), entry.getValue()); + } + + message = ChatColor.translateAlternateColorCodes('&', message); + return message; + } + private MessageType() { String a = this.name().toLowerCase().replace("_", "."); String b = ModReq.getInstance().Messages.getString(a, ModReq.getInstance().getDefaultMessages().getString(a)); message = b; } - } diff --git a/src/main/java/modreq/commands/ModhelpCommand.java b/src/main/java/modreq/commands/ModhelpCommand.java index 5459d2f..3f5b5e4 100644 --- a/src/main/java/modreq/commands/ModhelpCommand.java +++ b/src/main/java/modreq/commands/ModhelpCommand.java @@ -84,7 +84,5 @@ public void Null(CommandSender sender, String[] args) { sender.sendMessage(ChatColor.GOLD + "/mods " + ChatColor.WHITE + plugin.Messages.getString("info.mods")); } sender.sendMessage(ChatColor.GOLD + "/modhelp " + ChatColor.WHITE + plugin.Messages.getString("info.modhelp")); - return; - } } diff --git a/src/main/java/modreq/commands/ModreqCommand.java b/src/main/java/modreq/commands/ModreqCommand.java index c0778a5..79eb58b 100644 --- a/src/main/java/modreq/commands/ModreqCommand.java +++ b/src/main/java/modreq/commands/ModreqCommand.java @@ -18,7 +18,10 @@ package modreq.commands; import java.sql.SQLException; +import java.util.HashMap; +import modreq.Message; +import modreq.MessageType; import modreq.ModReq; import modreq.Status; import modreq.korik.Utils; @@ -41,60 +44,54 @@ public ModreqCommand(ModReq instance) { } @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, - String[] args) { + public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args) { tickets = plugin.getTicketHandler(); - if (sender instanceof Player) { - Player p = (Player) sender; - if (p.hasPermission("modreq.request")) { - if (args.length == 0) { - sender.sendMessage(ModReq.format(ModReq.getInstance().Messages.getString("error.message"), "", "","")); - return true; - } else { - int ticketsfromplayer; - try { - ticketsfromplayer = tickets.getTicketsFromPlayer(p, - sender.getName(), Status.OPEN); - if (plugin.getConfig().getInt("maximum-open-tickets") > ticketsfromplayer) { - String message = Utils.join(args, " ", 0); - int id = savereq(message, sender,((Player) sender).getLocation()); - sendMessageToAdmins(ModReq.format(ModReq.getInstance().Messages.getString("staff.all.ticket-submitted"), sender.getName(), Integer.toString(id),"")); - p.sendMessage(ModReq.format(ModReq.getInstance().Messages.getString("player.submit"), "", "","")); - return true; - } else { - p.sendMessage(ModReq.format(ModReq.getInstance().Messages.getString("error.ticket.toomany"), "", "","")); - return true; - } - } catch (SQLException e) { - e.printStackTrace(); - } - } - } + if (!(sender instanceof Player)) { + sender.sendMessage("This command can only be run as a player"); + return true; } - return false; - } + Player p = (Player) sender; + if (p.hasPermission("modreq.request")) { + if (args.length == 0) { + sender.sendMessage(ModReq.format(ModReq.getInstance().Messages.getString("error.message"), "", "", "")); + return true; + } + try { + int ticketsfromplayer = tickets.getTicketsFromPlayer(p, sender.getName(), Status.OPEN); + if (ticketsfromplayer >= plugin.getConfig().getInt("maximum-open-tickets")) { + Message.sendToPlayer(MessageType.ERROR_TICKET_TOOMANY, p); + return true; + } + String message = Utils.join(args, " ", 0); + int id = savereq(message, p); + final String idString = Integer.toString(id); - public void sendMessageToAdmins(String message) {// sends a message to all - // online players with the - // modreq.check permission - for (Player op : Bukkit.getOnlinePlayers()) { - if (op.hasPermission("modreq.check")) { - op.sendMessage(message); + Message.sendToAdmins(MessageType.STAFF_ALL_TICKETSUBMITTED, new HashMap() {{ + put("player", sender.getName()); + put("number", idString); + }}); + + Message.sendToPlayer(MessageType.PLAYER_SUBMIT, p); + return true; + } catch (SQLException e) { + e.printStackTrace(); } } + return false; } - private int savereq(String message, CommandSender sender, Location loc) {// save + private int savereq(String message, Player sender) {// save String time = ModReq.getTimeString(); + Location loc = sender.getLocation(); String location = loc.getWorld().getName() + " @ " + Math.round(loc.getX()) + " " + Math.round(loc.getY()) + " " + Math.round(loc.getZ()); try { - int id = tickets.addTicket(sender.getName(), message, time, Status.OPEN, location); - return id; + return tickets.addTicket(sender.getName(), message, time, Status.OPEN, location); } catch (SQLException e) { + e.printStackTrace(); } return 0; } diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 3cf9e28..32698db 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -44,7 +44,8 @@ staff: all: comment: "&b&player &ajust commented on ticket (&6#&number&a)" notification: "&b&number &aopen tickets are waiting for you!" - ticket-submitted: "&b&player &ajust submitted ticket &6#&number." + ticket-submitted: "&b&player &ajust submitted ticket &6#&number." #There for compatibility reasons with version < 2.5 + ticketsubmitted: "&b&player &ajust submitted ticket &6#&number." headers-footers: status: header: "&6----List-of-your-last-5-tickets----"