From 28a27c9c29e86df2a4d626053a7b5edf8b7402a4 Mon Sep 17 00:00:00 2001 From: Gibstick Date: Sun, 21 Jul 2013 10:57:35 -0400 Subject: [PATCH] refactored permissions and commands to DiscoSheep class --- .../discosheep/BaaBaaBlockSheepEvents.java | 3 + src/ca/gibstick/discosheep/DiscoSheep.java | 96 ++++++++++++++ .../discosheep/DiscoSheepCommandExecutor.java | 123 ++---------------- 3 files changed, 110 insertions(+), 112 deletions(-) diff --git a/src/ca/gibstick/discosheep/BaaBaaBlockSheepEvents.java b/src/ca/gibstick/discosheep/BaaBaaBlockSheepEvents.java index 7d7ebc8..c0f7c43 100644 --- a/src/ca/gibstick/discosheep/BaaBaaBlockSheepEvents.java +++ b/src/ca/gibstick/discosheep/BaaBaaBlockSheepEvents.java @@ -5,10 +5,12 @@ */ package ca.gibstick.discosheep; +import org.bukkit.entity.Player; import org.bukkit.entity.Sheep; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerShearEntityEvent; @@ -19,6 +21,7 @@ import org.bukkit.event.player.PlayerShearEntityEvent; public class BaaBaaBlockSheepEvents implements Listener { DiscoSheep parent; + static DiscoSheepCommandExecutor CommExec; public BaaBaaBlockSheepEvents(DiscoSheep parent) { this.parent = parent; diff --git a/src/ca/gibstick/discosheep/DiscoSheep.java b/src/ca/gibstick/discosheep/DiscoSheep.java index fce1c54..4580135 100644 --- a/src/ca/gibstick/discosheep/DiscoSheep.java +++ b/src/ca/gibstick/discosheep/DiscoSheep.java @@ -4,10 +4,22 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; public final class DiscoSheep extends JavaPlugin { + + static final String PERMISSION_PARTY = "discosheep.party"; + static final String PERMISSION_ALL = "discosheep.partyall"; + static final String PERMISSION_FIREWORKS = "discosheep.fireworks"; + static final String PERMISSION_STOPALL = "discosheep.stopall"; + static final String PERMISSION_RELOAD = "discosheep.reload"; + static final String PERMISSION_OTHER = "discosheep.partyother"; + static final String PERMISSION_CHANGEPERIOD = "discosheep.changeperiod"; Map parties = new HashMap(); private BaaBaaBlockSheepEvents blockEvents = new BaaBaaBlockSheepEvents(this); @@ -106,4 +118,88 @@ public final class DiscoSheep extends JavaPlugin { this.getPartyMap().remove(name); } } + + /*-- Actual commands begin here --*/ + boolean helpCommand(CommandSender sender) { + sender.sendMessage(ChatColor.YELLOW + "DiscoSheep Help\n" + ChatColor.GRAY + " Subcommands\n" + ChatColor.WHITE + "me, stop, all, stopall\n" + "You do not need permission to use the \"stop\" command\n" + "other : start a party for the space-delimited list of players\n" + ChatColor.GRAY + " Arguments\n" + ChatColor.WHITE + "-n : set the number of sheep per player that spawn\n" + "-t : set the party duration in seconds\n" + "-p : set the number of ticks between each disco beat\n" + "-r : set radius of the area in which sheep can spawn\n" + "-fw: enables fireworks"); + return true; + } + + boolean stopMeCommand(CommandSender sender, DiscoSheepCommandExecutor discoSheepCommandExecutor) { + stopParty(sender.getName()); + return true; + } + + boolean stopAllCommand(CommandSender sender, DiscoSheepCommandExecutor discoSheepCommandExecutor) { + if (sender.hasPermission(DiscoSheep.PERMISSION_STOPALL)) { + stopAllParties(); + return true; + } else { + return noPermsMessage(sender, DiscoSheep.PERMISSION_STOPALL); + } + } + + boolean partyCommand(Player player, DiscoParty party, DiscoSheepCommandExecutor discoSheepCommandExecutor) { + if (player.hasPermission(DiscoSheep.PERMISSION_PARTY)) { + if (!hasParty(player.getName())) { + party.setPlayer(player); + party.startDisco(); + } else { + player.sendMessage(ChatColor.RED + "You already have a party. Are you underground?"); + } + return true; + } else { + return noPermsMessage(player, DiscoSheep.PERMISSION_PARTY); + } + } + + boolean reloadCommand(CommandSender sender, DiscoSheepCommandExecutor discoSheepCommandExecutor) { + if (sender.hasPermission(DiscoSheep.PERMISSION_RELOAD)) { + reloadConfigFromDisk(); + sender.sendMessage(ChatColor.GREEN + "DiscoSheep config reloaded from disk"); + return true; + } else { + return noPermsMessage(sender, DiscoSheep.PERMISSION_RELOAD); + } + } + + boolean partyOtherCommand(String[] players, CommandSender sender, DiscoParty party, DiscoSheepCommandExecutor discoSheepCommandExecutor) { + if (sender.hasPermission(DiscoSheep.PERMISSION_OTHER)) { + Player p; + for (String playerName : players) { + p = Bukkit.getServer().getPlayer(playerName); + if (p != null) { + if (!hasParty(p.getName())) { + DiscoParty individualParty = party.DiscoParty(p); + individualParty.startDisco(); + } + } else { + sender.sendMessage("Invalid player: " + playerName); + } + } + return true; + } else { + return noPermsMessage(sender, DiscoSheep.PERMISSION_OTHER); + } + } + + boolean partyAllCommand(CommandSender sender, DiscoParty party, DiscoSheepCommandExecutor discoSheepCommandExecutor) { + if (sender.hasPermission(DiscoSheep.PERMISSION_ALL)) { + for (Player p : Bukkit.getServer().getOnlinePlayers()) { + if (!hasParty(p.getName())) { + DiscoParty individualParty = party.DiscoParty(p); + individualParty.startDisco(); + p.sendMessage(ChatColor.RED + "LET'S DISCO!"); + } + } + return true; + } else { + return noPermsMessage(sender, DiscoSheep.PERMISSION_ALL); + } + } + + boolean noPermsMessage(CommandSender sender, String permission) { + sender.sendMessage(ChatColor.RED + "You do not have the permission node " + ChatColor.GRAY + permission); + return false; + } } diff --git a/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java b/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java index 43746f6..a24b672 100644 --- a/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java +++ b/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java @@ -15,18 +15,6 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { public DiscoSheepCommandExecutor(DiscoSheep parent) { this.parent = parent; } - private static final String PERMISSION_PARTY = "discosheep.party"; - private static final String PERMISSION_ALL = "discosheep.partyall"; - private static final String PERMISSION_FIREWORKS = "discosheep.fireworks"; - private static final String PERMISSION_STOPALL = "discosheep.stopall"; - private static final String PERMISSION_RELOAD = "discosheep.reload"; - private static final String PERMISSION_OTHER = "discosheep.partyother"; - private static final String PERMISSION_CHANGEPERIOD = "discosheep.changeperiod"; - - private boolean noPermsMessage(CommandSender sender, String permission) { - sender.sendMessage(ChatColor.RED + "You do not have the permission node " + ChatColor.GRAY + permission); - return false; - } private boolean parseNextArg(String[] args, int i, String compare) { if (i < args.length - 1) { @@ -66,95 +54,6 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { return Arrays.copyOfRange(args, i, j); } - /*-- Actual commands begin here --*/ - private boolean helpCommand(CommandSender sender) { - sender.sendMessage(ChatColor.YELLOW + "DiscoSheep Help\n" - + ChatColor.GRAY + " Subcommands\n" + ChatColor.WHITE - + "me, stop, all, stopall\n" - + "You do not need permission to use the \"stop\" command\n" - + "other : start a party for the space-delimited list of players\n" - + ChatColor.GRAY + " Arguments\n" + ChatColor.WHITE - + "-n : set the number of sheep per player that spawn\n" - + "-t : set the party duration in seconds\n" - + "-p : set the number of ticks between each disco beat\n" - + "-r : set radius of the area in which sheep can spawn\n" - + "-fw: enables fireworks"); - return true; - } - - private boolean reloadCommand(CommandSender sender) { - if (sender.hasPermission(PERMISSION_RELOAD)) { - parent.reloadConfigFromDisk(); - sender.sendMessage(ChatColor.GREEN + "DiscoSheep config reloaded from disk"); - return true; - } else { - return noPermsMessage(sender, PERMISSION_RELOAD); - } - } - - private boolean partyCommand(Player player, DiscoParty party) { - if (player.hasPermission(PERMISSION_PARTY)) { - if (!parent.hasParty(player.getName())) { - party.setPlayer(player); - party.startDisco(); - } else { - player.sendMessage(ChatColor.RED + "You already have a party. Are you underground?"); - } - return true; - } else { - return noPermsMessage(player, PERMISSION_PARTY); - } - } - - private boolean partyAllCommand(CommandSender sender, DiscoParty party) { - if (sender.hasPermission(PERMISSION_ALL)) { - for (Player p : Bukkit.getServer().getOnlinePlayers()) { - if (!parent.hasParty(p.getName())) { - DiscoParty individualParty = party.DiscoParty(p); - individualParty.startDisco(); - p.sendMessage(ChatColor.RED + "LET'S DISCO!"); - } - } - return true; - } else { - return noPermsMessage(sender, PERMISSION_ALL); - } - } - - private boolean stopAllCommand(CommandSender sender) { - if (sender.hasPermission(PERMISSION_STOPALL)) { - parent.stopAllParties(); - return true; - } else { - return noPermsMessage(sender, PERMISSION_STOPALL); - } - } - - private boolean stopMeCommand(CommandSender sender) { - parent.stopParty(sender.getName()); - return true; - } - - private boolean partyOtherCommand(String[] players, CommandSender sender, DiscoParty party) { - if (sender.hasPermission(PERMISSION_OTHER)) { - Player p; - for (String playerName : players) { - p = Bukkit.getServer().getPlayer(playerName); - if (p != null) { - if (!parent.hasParty(p.getName())) { - DiscoParty individualParty = party.DiscoParty(p); - individualParty.startDisco(); - } - } else { - sender.sendMessage("Invalid player: " + playerName); - } - } - return true; - } else { - return noPermsMessage(sender, PERMISSION_OTHER); - } - } - @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { @@ -171,13 +70,13 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { // prevent needless construction of parties if (args.length == 1) { if (args[0].equalsIgnoreCase("stopall")) { - return stopAllCommand(sender); + return parent.stopAllCommand(sender, this); } else if (args[0].equalsIgnoreCase("stop") && isPlayer) { - return stopMeCommand(sender); + return parent.stopMeCommand(sender, this); } else if (args[0].equalsIgnoreCase("help")) { - return helpCommand(sender); + return parent.helpCommand(sender); } else if (args[0].equalsIgnoreCase("reload")) { - return reloadCommand(sender); + return parent.reloadCommand(sender, this); } } @@ -187,10 +86,10 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { // omg I love argument parsing and I know the best way! for (int i = 1; i < args.length; i++) { if (args[i].equalsIgnoreCase("-fw")) { - if (sender.hasPermission(PERMISSION_FIREWORKS)) { + if (sender.hasPermission(DiscoSheep.PERMISSION_FIREWORKS)) { mainParty.setDoFireworks(true); } else { - return noPermsMessage(sender, PERMISSION_FIREWORKS); + return parent.noPermsMessage(sender,DiscoSheep.PERMISSION_FIREWORKS); } } else if (args[i].equalsIgnoreCase("-r")) { try { @@ -217,8 +116,8 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { return false; } } else if (args[i].equalsIgnoreCase("-p")) { - if (!sender.hasPermission(PERMISSION_CHANGEPERIOD)) { - return noPermsMessage(sender, PERMISSION_CHANGEPERIOD); + if (!sender.hasPermission(DiscoSheep.PERMISSION_CHANGEPERIOD)) { + return parent.noPermsMessage(sender, DiscoSheep.PERMISSION_CHANGEPERIOD); } try { mainParty.setPeriod(parseNextIntArg(args, i)); @@ -234,11 +133,11 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { if (args.length > 0) { if (args[0].equalsIgnoreCase("all")) { - return partyAllCommand(sender, mainParty); + return parent.partyAllCommand(sender, mainParty, this); } else if (args[0].equalsIgnoreCase("me") && isPlayer) { - return partyCommand(player, mainParty); + return parent.partyCommand(player, mainParty, this); } else if (args[0].equalsIgnoreCase("other")) { - return partyOtherCommand(parsePlayerList(args, 1), sender, mainParty); + return parent.partyOtherCommand(parsePlayerList(args, 1), sender, mainParty, this); } else { sender.sendMessage(ChatColor.RED + "Invalid argument (certain commands do not work from console)."); return false;