From 1de049f7570e42d9740c989c8953d29626b4afe4 Mon Sep 17 00:00:00 2001 From: GitGideon Date: Mon, 22 Jan 2018 18:22:51 +0100 Subject: [PATCH] Changed some things, added permissions per command --- .../customcommands/CustomCommands.java | 33 +++-- src/main/resources/config.yml | 129 ++++++++++-------- 2 files changed, 97 insertions(+), 65 deletions(-) diff --git a/src/main/java/net/poweredbyawesome/customcommands/CustomCommands.java b/src/main/java/net/poweredbyawesome/customcommands/CustomCommands.java index 904a4b8..66d35e6 100644 --- a/src/main/java/net/poweredbyawesome/customcommands/CustomCommands.java +++ b/src/main/java/net/poweredbyawesome/customcommands/CustomCommands.java @@ -10,10 +10,11 @@ import org.bukkit.plugin.java.JavaPlugin; import java.util.ArrayList; +import java.util.List; public final class CustomCommands extends JavaPlugin implements Listener { - public ArrayList cmds = new ArrayList<>(); + public List cmds = new ArrayList<>(); @Override public void onEnable() { @@ -23,34 +24,46 @@ public void onEnable() { } public void loadCommands() { - for (String s : getConfig().getKeys(false)) { - cmds.add(s.toLowerCase()); - } + getConfig().getKeys(false).forEach(s -> cmds.add(s.toLowerCase())); } @EventHandler public void onCommand(PlayerCommandPreprocessEvent ev) { String command = ev.getMessage().replace("/",""); - if (cmds.contains(command)) { + if (cmds.contains(command.toLowerCase())) { ev.setCancelled(true); - for (String s : getConfig().getStringList(command)) { + Player player = ev.getPlayer(); + if (getConfig().contains(command + ".permission") && + !player.hasPermission(getConfig().getString(command + ".permission"))) { + if (getConfig().contains(command + ".no-permission-message")) + player.sendMessage(ChatColor.translateAlternateColorCodes('&', + getConfig().getString(command + ".no-permission-message"))); + else + player.sendMessage(ChatColor.DARK_RED + "You don't have permission for that command"); + return; + } + + for (String s : getConfig().getStringList(command + ".commands")) { if (s.startsWith("/") || s.startsWith("\\")) { if (s.startsWith("/")) { - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), format(s.replace("/", ""), ev.getPlayer())); + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), format(s.replace("/", ""), player)); } if (s.startsWith("\\")) { - ev.getPlayer().performCommand(format(s.replace("\\", ""), ev.getPlayer())); + player.performCommand(format(s.replace("\\", ""), player)); } } else { if (s.contains("<*>")) { String multiplier = s.split("<*>")[1]; if (StringUtils.isNumeric(multiplier)) { for (int i = 0; i < Integer.valueOf(s.split("<*>")[1]); i++) { - ev.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', format(s.replace("<*>","").replace(multiplier, ""), ev.getPlayer()))); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', + format(s.replace("<*>", "").replace(multiplier, ""), player))); } + } else { + getLogger().info("The command '" + command + "' doesn't have a valid number multiplier!"); } } else { - ev.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', format(s, ev.getPlayer()))); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', format(s, player))); } } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9ef6ff1..693f686 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,84 +1,103 @@ -#Please use lowercase +# Please use lowercase # <*>100 will send the message 100x # / will run a command from the console # \ will make the player run the command. # will be replaced accordingly. # vote: - - '&a----------&f[&6Vote&f]&a----------' - - '&aPlanetMinecruft: &chttp://poweredbyhate.net/' - - '&aSwagDoritos: &chttp://poweredbyhate.net/' - - '&aSpigotVotes: &chttp://poweredbyhate.net/' - - '&a----------&f[&6Vote&f]&a----------' + commands: + - '&a----------&f[&6Vote&f]&a----------' + - '&aPlanetMinecraft: &chttp://poweredbyhate.net/' + - '&aSwagDoritos: &chttp://poweredbyhate.net/' + - '&aSpigotVotes: &chttp://poweredbyhate.net/' + - '&a----------&f[&6Vote&f]&a----------' +# The following command is an example of using permissions with your custom commands. discord: - - "&8&m-----------------------------------&f" - - " " - - "&cYou can join our Discord here!" - - "&c&7https://discord.gg/SvFEBGW" - - " " - - "&8&m-----------------------------------&f" + commands: + - "&8&m-----------------------------------&f" + - " " + - "&cYou can join our Discord here!" + - "&c&7https://discord.gg/SvFEBGW" + - " " + - "&8&m-----------------------------------&f" + permission: "customcommands.discord" + # no-permission-message is optional, if it's not enabled it'll give a generic no permission message + no-permission-message: "&4You don't have permission to join our Discord, you first need to be rank Trusted or higher!" motd: - - "&8&m-----------------------------------&f" - - " " - - "&aWelcome back, &f" - - "&cDiscord: &fhttps://discord.gg/SvFEBGW" - - "&cTwitter: &f@LaxWasHere" - - "&cStore: &fhttps://www.paypal.me/LaxWasHere" - - "&cYoutube: &fhttps://www.youtube.com/LaxWasHere" - - " " - - "&8&m-----------------------------------&f" + commands: + - "&8&m-----------------------------------&f" + - " " + - "&aWelcome back, &f" + - "&cDiscord: &fhttps://discord.gg/SvFEBGW" + - "&cTwitter: &f@LaxWasHere" + - "&cStore: &fhttps://www.paypal.me/LaxWasHere" + - "&cYoutube: &fhttps://www.youtube.com/LaxWasHere" + - " " + - "&8&m-----------------------------------&f" twitter: - - "&8&m-----------------------------------&f" - - " " - - "&cFollow us on twitter!" - - "&7@LaxWasHere" - - " " - - "&8&m-----------------------------------&f" + commands: + - "&8&m-----------------------------------&f" + - " " + - "&cFollow us on twitter!" + - "&7@LaxWasHere" + - " " + - "&8&m-----------------------------------&f" donate: - - "&8&m-----------------------------------&f" - - " " - - "&cDonate to our server!" - - "&chttps://www.paypal.me/LaxWasHere" - - " " - - "&8&m-----------------------------------&f" + commands: + - "&8&m-----------------------------------&f" + - " " + - "&cDonate to our server!" + - "&chttps://www.paypal.me/LaxWasHere" + - " " + - "&8&m-----------------------------------&f" forums: - - "&8&m-----------------------------------&f" - - " " - - "&cCheck out our forums!" - - "&7https://www.spigotmc.org" - - " " - - "&8&m-----------------------------------&f" + commands: + - "&8&m-----------------------------------&f" + - " " + - "&cCheck out our forums!" + - "&7https://www.spigotmc.org" + - " " + - "&8&m-----------------------------------&f" youtube: - - "&8&m-----------------------------------&f" - - " " - - "&cSubscribe to our youtube channel!" - - "&7https://www.youtube.com/LaxWasHere" - - " " - - "&8&m-----------------------------------&f" + commands: + - "&8&m-----------------------------------&f" + - " " + - "&cSubscribe to our youtube channel!" + - "&7https://www.youtube.com/LaxWasHere" + - " " + - "&8&m-----------------------------------&f" -pleasekickme: #/ will run the command from the console - - "/kick &cYou asked for it!" +pleasekickme: # / will run the command from the console + commands: + - "/kick &cYou asked for it!" -iatemycat: #\ will run the command from the player - - '\me I ate my cat ' +iatemycat: # \ will run the command from the player + commands: + - '\me I ate my cat ' clearmychat: - - " <*>100" #This will send an empty message 100x + commands: + - " <*>100" # This will send an empty message 100x amiawesome: - - "&cYes <*>3" #This will '&cYes' 3x + commands: + - "&cYes <*>3" # This will '&cYes' 3x laxwashere: - - '&aLaxWasHere, yes' + commands: + - "&aLaxWasHere, yes" electroniccat: - - '&aLast known sighting:&c http://i.giphy.com/GFHJXPCoVQEec.gif' + commands: + - "&aLast known sighting:&c http://i.giphy.com/GFHJXPCoVQEec.gif" -#opme: #THIS AS IT WILL ACTUALLY OP THE PLAYER. DO NOT UNCOMMENT! -# - "/op " +#opme: # THIS WILL OP ANY PLAYER THAT DOES THIS COMMAND, DON'T UNCOMMENT UNLESS YOU KNOW WHAT YOU'RE DOING +# commands: +# - "/op " +# permission: "customcommands.opme" \ No newline at end of file