Skip to content

Commit

Permalink
cleaned up the modreq command
Browse files Browse the repository at this point in the history
  • Loading branch information
svenwiltink committed Sep 6, 2019
1 parent 04e1988 commit 1ffec45
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 43 deletions.
12 changes: 12 additions & 0 deletions src/main/java/modreq/Message.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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, Map<String,String>parameters) {
String message = MessageType.formatWithParameters(parameters);
for (Player op : Bukkit.getOnlinePlayers()) {
if (op.hasPermission("modreq.check")) {
op.sendMessage(message);
}
}
}
}
12 changes: 11 additions & 1 deletion src/main/java/modreq/MessageType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.bukkit.ChatColor;

import java.util.Map;


public enum MessageType {
ERROR_PERMISSION(),
Expand Down Expand Up @@ -94,10 +96,18 @@ public String format(String PlayerName, String TicketNumber, String comment) {
return message;
}

public String formatWithParameters(Map<String, String> parameters) {
for (Map.Entry<String, String> 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;
}

}
2 changes: 0 additions & 2 deletions src/main/java/modreq/commands/ModhelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
}
75 changes: 36 additions & 39 deletions src/main/java/modreq/commands/ModreqCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String>() {{
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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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----"
Expand Down

0 comments on commit 1ffec45

Please sign in to comment.