refactored permissions and commands to DiscoSheep class

This commit is contained in:
Gibstick 2013-07-21 10:57:35 -04:00
parent d711136d83
commit 28a27c9c29
3 changed files with 110 additions and 112 deletions

View File

@ -5,10 +5,12 @@
*/ */
package ca.gibstick.discosheep; package ca.gibstick.discosheep;
import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep; import org.bukkit.entity.Sheep;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerShearEntityEvent; import org.bukkit.event.player.PlayerShearEntityEvent;
@ -19,6 +21,7 @@ import org.bukkit.event.player.PlayerShearEntityEvent;
public class BaaBaaBlockSheepEvents implements Listener { public class BaaBaaBlockSheepEvents implements Listener {
DiscoSheep parent; DiscoSheep parent;
static DiscoSheepCommandExecutor CommExec;
public BaaBaaBlockSheepEvents(DiscoSheep parent) { public BaaBaaBlockSheepEvents(DiscoSheep parent) {
this.parent = parent; this.parent = parent;

View File

@ -4,10 +4,22 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public final class DiscoSheep extends 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<String, DiscoParty> parties = new HashMap<String, DiscoParty>(); Map<String, DiscoParty> parties = new HashMap<String, DiscoParty>();
private BaaBaaBlockSheepEvents blockEvents = new BaaBaaBlockSheepEvents(this); private BaaBaaBlockSheepEvents blockEvents = new BaaBaaBlockSheepEvents(this);
@ -106,4 +118,88 @@ public final class DiscoSheep extends JavaPlugin {
this.getPartyMap().remove(name); 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 <players>: start a party for the space-delimited list of players\n" + ChatColor.GRAY + " Arguments\n" + ChatColor.WHITE + "-n <integer>: set the number of sheep per player that spawn\n" + "-t <integer>: set the party duration in seconds\n" + "-p <ticks>: set the number of ticks between each disco beat\n" + "-r <integer>: 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;
}
} }

View File

@ -15,18 +15,6 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
public DiscoSheepCommandExecutor(DiscoSheep parent) { public DiscoSheepCommandExecutor(DiscoSheep parent) {
this.parent = 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) { private boolean parseNextArg(String[] args, int i, String compare) {
if (i < args.length - 1) { if (i < args.length - 1) {
@ -66,95 +54,6 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
return Arrays.copyOfRange(args, i, j); 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 <players>: start a party for the space-delimited list of players\n"
+ ChatColor.GRAY + " Arguments\n" + ChatColor.WHITE
+ "-n <integer>: set the number of sheep per player that spawn\n"
+ "-t <integer>: set the party duration in seconds\n"
+ "-p <ticks>: set the number of ticks between each disco beat\n"
+ "-r <integer>: 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 @Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { 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 // prevent needless construction of parties
if (args.length == 1) { if (args.length == 1) {
if (args[0].equalsIgnoreCase("stopall")) { if (args[0].equalsIgnoreCase("stopall")) {
return stopAllCommand(sender); return parent.stopAllCommand(sender, this);
} else if (args[0].equalsIgnoreCase("stop") && isPlayer) { } else if (args[0].equalsIgnoreCase("stop") && isPlayer) {
return stopMeCommand(sender); return parent.stopMeCommand(sender, this);
} else if (args[0].equalsIgnoreCase("help")) { } else if (args[0].equalsIgnoreCase("help")) {
return helpCommand(sender); return parent.helpCommand(sender);
} else if (args[0].equalsIgnoreCase("reload")) { } 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! // omg I love argument parsing and I know the best way!
for (int i = 1; i < args.length; i++) { for (int i = 1; i < args.length; i++) {
if (args[i].equalsIgnoreCase("-fw")) { if (args[i].equalsIgnoreCase("-fw")) {
if (sender.hasPermission(PERMISSION_FIREWORKS)) { if (sender.hasPermission(DiscoSheep.PERMISSION_FIREWORKS)) {
mainParty.setDoFireworks(true); mainParty.setDoFireworks(true);
} else { } else {
return noPermsMessage(sender, PERMISSION_FIREWORKS); return parent.noPermsMessage(sender,DiscoSheep.PERMISSION_FIREWORKS);
} }
} else if (args[i].equalsIgnoreCase("-r")) { } else if (args[i].equalsIgnoreCase("-r")) {
try { try {
@ -217,8 +116,8 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
return false; return false;
} }
} else if (args[i].equalsIgnoreCase("-p")) { } else if (args[i].equalsIgnoreCase("-p")) {
if (!sender.hasPermission(PERMISSION_CHANGEPERIOD)) { if (!sender.hasPermission(DiscoSheep.PERMISSION_CHANGEPERIOD)) {
return noPermsMessage(sender, PERMISSION_CHANGEPERIOD); return parent.noPermsMessage(sender, DiscoSheep.PERMISSION_CHANGEPERIOD);
} }
try { try {
mainParty.setPeriod(parseNextIntArg(args, i)); mainParty.setPeriod(parseNextIntArg(args, i));
@ -234,11 +133,11 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
if (args.length > 0) { if (args.length > 0) {
if (args[0].equalsIgnoreCase("all")) { if (args[0].equalsIgnoreCase("all")) {
return partyAllCommand(sender, mainParty); return parent.partyAllCommand(sender, mainParty, this);
} else if (args[0].equalsIgnoreCase("me") && isPlayer) { } else if (args[0].equalsIgnoreCase("me") && isPlayer) {
return partyCommand(player, mainParty); return parent.partyCommand(player, mainParty, this);
} else if (args[0].equalsIgnoreCase("other")) { } else if (args[0].equalsIgnoreCase("other")) {
return partyOtherCommand(parsePlayerList(args, 1), sender, mainParty); return parent.partyOtherCommand(parsePlayerList(args, 1), sender, mainParty, this);
} else { } else {
sender.sendMessage(ChatColor.RED + "Invalid argument (certain commands do not work from console)."); sender.sendMessage(ChatColor.RED + "Invalid argument (certain commands do not work from console).");
return false; return false;