cleaned up code, inlined senderHasPerm, condensed help command output

This commit is contained in:
Gibstick 2013-07-21 09:13:48 -04:00
parent 1336cc1aaf
commit 5841b5a7f4
5 changed files with 58 additions and 69 deletions

View File

@ -1,6 +1,7 @@
/* /*
* To change this template, choose Tools | Templates * BaaBaaBlockSheep have you any wool?
* and open the template in the editor. * Nope, event got cancelled.
* Also listens to other events, not just sheep events
*/ */
package ca.gibstick.discosheep; package ca.gibstick.discosheep;
@ -42,7 +43,7 @@ public class BaaBaaBlockSheepEvents implements Listener {
for (DiscoParty party : parent.getParties()) { for (DiscoParty party : parent.getParties()) {
if (party.getSheep().contains((Sheep) e.getEntity())) { if (party.getSheep().contains((Sheep) e.getEntity())) {
{ {
party.jumpSheep((Sheep) e.getEntity()); party.jumpSheep((Sheep) e.getEntity()); // for kicks
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -53,7 +54,7 @@ public class BaaBaaBlockSheepEvents implements Listener {
@EventHandler @EventHandler
public void onPlayerQuitEvent(PlayerQuitEvent e) { public void onPlayerQuitEvent(PlayerQuitEvent e) {
String name = e.getPlayer().getName(); String name = e.getPlayer().getName();
parent.stopParty(name); parent.stopParty(name);
// stop party on player quit or else it will CONTINUE FOR ETERNITY
} }
} }

View File

@ -39,7 +39,7 @@ public class DiscoParty {
static int maxPeriod = 40; // 2.0 seconds static int maxPeriod = 40; // 2.0 seconds
private boolean doFireworks = false; private boolean doFireworks = false;
private boolean doJump = true; private boolean doJump = true;
private int state = 0; private int state = 0; // basically our own tick system
private DiscoUpdater updater; private DiscoUpdater updater;
private static final DyeColor[] discoColours = { private static final DyeColor[] discoColours = {
DyeColor.RED, DyeColor.RED,
@ -75,11 +75,7 @@ public class DiscoParty {
} }
// copy but with new player // copy but with new player
/** // used for /ds other and /ds all
*
* @param player The new player to be stored
* @return A copy of the class with the new player
*/
public DiscoParty DiscoParty(Player player) { public DiscoParty DiscoParty(Player player) {
DiscoParty newParty = new DiscoParty(this.ds, player); DiscoParty newParty = new DiscoParty(this.ds, player);
newParty.setDoFireworks(this.doFireworks); newParty.setDoFireworks(this.doFireworks);
@ -154,15 +150,16 @@ public class DiscoParty {
double z = player.getLocation().getZ(); double z = player.getLocation().getZ();
double y; double y;
// random point on circle with polar coordinates /* random point on circle with polar coordinates
double r = Math.sqrt(Math.random()) * sheepSpawnRadius; // sqrt for uniform distribution * random number must be square rooted to obtain uniform distribution
* otherwise the sheep are biased toward the centre */
double r = Math.sqrt(Math.random()) * sheepSpawnRadius;
double azimuth = Math.random() * 2 * Math.PI; // radians double azimuth = Math.random() * 2 * Math.PI; // radians
x += r * Math.cos(azimuth); x += r * Math.cos(azimuth);
z += r * Math.sin(azimuth); z += r * Math.sin(azimuth);
y = world.getHighestBlockYAt((int) x, (int) z); y = world.getHighestBlockYAt((int) x, (int) z);
loc = new Location(world, x, y, z); loc = new Location(world, x, y, z);
//loc.setPitch((float) ((180f / Math.PI) * Math.atan((loc.getX() - player.getLocation().getX()) / (loc.getZ() - player.getLocation().getZ()))));
loc.setPitch((float) Math.random() * 360 - 180); loc.setPitch((float) Math.random() * 360 - 180);
loc.setYaw(0); loc.setYaw(0);
spawnSheep(world, loc); spawnSheep(world, loc);
@ -172,15 +169,15 @@ public class DiscoParty {
void spawnSheep(World world, Location loc) { void spawnSheep(World world, Location loc) {
Sheep newSheep = (Sheep) world.spawnEntity(loc, EntityType.SHEEP); Sheep newSheep = (Sheep) world.spawnEntity(loc, EntityType.SHEEP);
newSheep.setColor(discoColours[(int) (Math.random() * (discoColours.length - 1))]); newSheep.setColor(discoColours[(int) (Math.random() * (discoColours.length - 1))]);
newSheep.setBreed(false); newSheep.setBreed(false); // this prevents breeding - no event listener required
newSheep.teleport(loc); // for sheep orientation newSheep.teleport(loc); // teleport is needed to set orientation
getSheep().add(newSheep); getSheep().add(newSheep);
} }
// Mark all sheep in the sheep array for removal, then clear the array // Mark all sheep in the sheep array for removal, then clear the array
void removeAllSheep() { void removeAllSheep() {
for (Sheep sheeple : getSheep()) { for (Sheep sheeple : getSheep()) {
sheeple.setHealth(0); //sheeple.setHealth(0); // removed to make it more resilient to updates
sheeple.remove(); sheeple.remove();
} }
getSheep().clear(); getSheep().clear();
@ -198,6 +195,7 @@ public class DiscoParty {
sheep.setVelocity(newVel); sheep.setVelocity(newVel);
} }
// WHY ISN'T THERE A Color.getValue() ?!?!?!?!
private Color getColor(int i) { private Color getColor(int i) {
Color c = null; Color c = null;
if (i == 1) { if (i == 1) {
@ -323,7 +321,11 @@ public class DiscoParty {
playSounds(); playSounds();
duration -= period; duration -= period;
this.scheduleUpdate(); this.scheduleUpdate();
if (state < 10000) {
this.state++; this.state++;
} else {
state = 1; // prevent overflow
}
} else { } else {
this.stopDisco(); this.stopDisco();
} }

View File

@ -57,7 +57,7 @@ public final class DiscoSheep extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
this.stopAllParties(); this.stopAllParties(); // or else the parties will continue FOREVER
this.config = null; this.config = null;
} }
@ -106,16 +106,4 @@ public final class DiscoSheep extends JavaPlugin {
this.getPartyMap().remove(name); this.getPartyMap().remove(name);
} }
} }
/*public void startParty(Player player, int duration, int sheepAmount, int radius, int period, boolean fireworksEnabled) {
* if (!hasParty(player.getName())) {
* DiscoParty ds = new DiscoParty(this, player);
* ds.setDuration(duration);
* ds.setSheep(sheepAmount);
* ds.setRadius(radius);
* ds.setPeriod(period);
* ds.setDoFireworks(fireworksEnabled);
* ds.startDisco();
* }
* }*/
} }

View File

@ -23,11 +23,6 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
private static final String PERMISSION_OTHER = "discosheep.partyother"; private static final String PERMISSION_OTHER = "discosheep.partyother";
private static final String PERMISSION_CHANGEPERIOD = "discosheep.changeperiod"; private static final String PERMISSION_CHANGEPERIOD = "discosheep.changeperiod";
//private static final String DELIM = "[ ]+";
private boolean senderHasPerm(CommandSender sender, String permission) {
return sender.hasPermission(permission);
}
private boolean noPermsMessage(CommandSender sender, String permission) { private boolean noPermsMessage(CommandSender sender, String permission) {
sender.sendMessage(ChatColor.RED + "You do not have the permission node " + ChatColor.GRAY + permission); sender.sendMessage(ChatColor.RED + "You do not have the permission node " + ChatColor.GRAY + permission);
return false; return false;
@ -45,20 +40,24 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
try { try {
return Integer.parseInt(args[i + 1]); return Integer.parseInt(args[i + 1]);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return -1; return -1; // so that it fails limit checks elsewhere
} }
} }
return -1; return -1; // ibid
} }
private Double parseNextDoubleArg(String[] args, int i) { private Double parseNextDoubleArg(String[] args, int i) {
if (i < args.length - 1) { if (i < args.length - 1) {
try {
return Double.parseDouble(args[i + 1]); return Double.parseDouble(args[i + 1]);
} catch (NumberFormatException e) {
return -1.0d; // so that it fais limit checks elsewhere
} }
return -1.0d; }
return -1.0d; // ibid
} }
// extract a list of players from a list of arguments // return portion of the array that contains the list of players
private String[] parsePlayerList(String[] args, int i) { private String[] parsePlayerList(String[] args, int i) {
int j = i; int j = i;
while (j < args.length && !args[j].startsWith("-")) { while (j < args.length && !args[j].startsWith("-")) {
@ -71,10 +70,8 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
private boolean helpCommand(CommandSender sender) { private boolean helpCommand(CommandSender sender) {
sender.sendMessage(ChatColor.YELLOW + "DiscoSheep Help\n" sender.sendMessage(ChatColor.YELLOW + "DiscoSheep Help\n"
+ ChatColor.GRAY + " Subcommands\n" + ChatColor.WHITE + ChatColor.GRAY + " Subcommands\n" + ChatColor.WHITE
+ "me: start a party for yourself\n" + "me, stop, all, stopall\n"
+ "stop: stop your own party\n" + "You do not need permission to use the \"stop\" command\n"
+ "all: start a party for all players on the server\n"
+ "stopall: stop all parties (takes no arguments)\n"
+ "other <players>: start a party for the space-delimited list of players\n" + "other <players>: start a party for the space-delimited list of players\n"
+ ChatColor.GRAY + " Arguments\n" + ChatColor.WHITE + ChatColor.GRAY + " Arguments\n" + ChatColor.WHITE
+ "-n <integer>: set the number of sheep per player that spawn\n" + "-n <integer>: set the number of sheep per player that spawn\n"
@ -86,7 +83,7 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
} }
private boolean reloadCommand(CommandSender sender) { private boolean reloadCommand(CommandSender sender) {
if (senderHasPerm(sender, PERMISSION_RELOAD)) { if (sender.hasPermission(PERMISSION_RELOAD)) {
parent.reloadConfigFromDisk(); parent.reloadConfigFromDisk();
sender.sendMessage(ChatColor.GREEN + "DiscoSheep config reloaded from disk"); sender.sendMessage(ChatColor.GREEN + "DiscoSheep config reloaded from disk");
return true; return true;
@ -96,12 +93,12 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
} }
private boolean partyCommand(Player player, DiscoParty party) { private boolean partyCommand(Player player, DiscoParty party) {
if (senderHasPerm(player, PERMISSION_PARTY)) { if (player.hasPermission(PERMISSION_PARTY)) {
if (!parent.hasParty(player.getName())) { if (!parent.hasParty(player.getName())) {
party.setPlayer(player); party.setPlayer(player);
party.startDisco(); party.startDisco();
} else { } else {
player.sendMessage("You already have a party. Are you underground?"); player.sendMessage(ChatColor.RED + "You already have a party. Are you underground?");
} }
return true; return true;
} else { } else {
@ -110,7 +107,7 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
} }
private boolean partyAllCommand(CommandSender sender, DiscoParty party) { private boolean partyAllCommand(CommandSender sender, DiscoParty party) {
if (senderHasPerm(sender, PERMISSION_ALL)) { if (sender.hasPermission(PERMISSION_ALL)) {
for (Player p : Bukkit.getServer().getOnlinePlayers()) { for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (!parent.hasParty(p.getName())) { if (!parent.hasParty(p.getName())) {
DiscoParty individualParty = party.DiscoParty(p); DiscoParty individualParty = party.DiscoParty(p);
@ -125,7 +122,7 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
} }
private boolean stopAllCommand(CommandSender sender) { private boolean stopAllCommand(CommandSender sender) {
if (senderHasPerm(sender, PERMISSION_STOPALL)) { if (sender.hasPermission(PERMISSION_STOPALL)) {
parent.stopAllParties(); parent.stopAllParties();
return true; return true;
} else { } else {
@ -139,7 +136,7 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
} }
private boolean partyOtherCommand(String[] players, CommandSender sender, DiscoParty party) { private boolean partyOtherCommand(String[] players, CommandSender sender, DiscoParty party) {
if (senderHasPerm(sender, PERMISSION_OTHER)) { if (sender.hasPermission(PERMISSION_OTHER)) {
Player p; Player p;
for (String playerName : players) { for (String playerName : players) {
p = Bukkit.getServer().getPlayer(playerName); p = Bukkit.getServer().getPlayer(playerName);
@ -167,9 +164,11 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
if (sender instanceof Player) { if (sender instanceof Player) {
player = (Player) sender; player = (Player) sender;
isPlayer = true; isPlayer = true;
} } // check isPlayer before "stop" and "me" commands
// check for commands that don't need a party // check for commands that don't need a party
// so that we get them out of the way, and
// 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 stopAllCommand(sender);
@ -182,19 +181,20 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
} }
} }
DiscoParty parentParty = new DiscoParty(parent); // construct a main party; all other parties will copy from this
DiscoParty mainParty = new DiscoParty(parent);
// 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 (senderHasPerm(sender, PERMISSION_FIREWORKS)) { if (sender.hasPermission(PERMISSION_FIREWORKS)) {
parentParty.setDoFireworks(true); mainParty.setDoFireworks(true);
} else { } else {
return noPermsMessage(sender, PERMISSION_FIREWORKS); return noPermsMessage(sender, PERMISSION_FIREWORKS);
} }
} else if (args[i].equalsIgnoreCase("-r")) { } else if (args[i].equalsIgnoreCase("-r")) {
try { try {
parentParty.setRadius(parseNextIntArg(args, i)); mainParty.setRadius(parseNextIntArg(args, i));
//sender.sendMessage("RADIUS OK");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
sender.sendMessage("Radius must be an integer within the range [1, " sender.sendMessage("Radius must be an integer within the range [1, "
+ DiscoParty.maxRadius + "]"); + DiscoParty.maxRadius + "]");
@ -202,8 +202,7 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
} }
} else if (args[i].equalsIgnoreCase("-n")) { } else if (args[i].equalsIgnoreCase("-n")) {
try { try {
parentParty.setSheep(parseNextIntArg(args, i)); mainParty.setSheep(parseNextIntArg(args, i));
//sender.sendMessage("SHEEP OK");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
sender.sendMessage("The number of sheep must be an integer within the range [1, " sender.sendMessage("The number of sheep must be an integer within the range [1, "
+ DiscoParty.maxSheep + "]"); + DiscoParty.maxSheep + "]");
@ -211,20 +210,18 @@ public class DiscoSheepCommandExecutor implements CommandExecutor {
} }
} else if (args[i].equalsIgnoreCase("-t")) { } else if (args[i].equalsIgnoreCase("-t")) {
try { try {
parentParty.setDuration(parent.toTicks(parseNextIntArg(args, i))); mainParty.setDuration(parent.toTicks(parseNextIntArg(args, i)));
//sender.sendMessage("DURATION OK");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
sender.sendMessage("The duration in seconds must be an integer within the range [1, " sender.sendMessage("The duration in seconds must be an integer within the range [1, "
+ parent.toSeconds(DiscoParty.maxDuration) + "]"); + parent.toSeconds(DiscoParty.maxDuration) + "]");
return false; return false;
} }
} else if (args[i].equalsIgnoreCase("-p")) { } else if (args[i].equalsIgnoreCase("-p")) {
if (!senderHasPerm(sender, PERMISSION_CHANGEPERIOD)) { if (!sender.hasPermission(PERMISSION_CHANGEPERIOD)) {
return noPermsMessage(sender, PERMISSION_CHANGEPERIOD); return noPermsMessage(sender, PERMISSION_CHANGEPERIOD);
} }
try { try {
parentParty.setPeriod(parseNextIntArg(args, i)); mainParty.setPeriod(parseNextIntArg(args, i));
//sender.sendMessage("PERIOD OK");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
sender.sendMessage( sender.sendMessage(
"The period in ticks must be within the range [" "The period in ticks must be within the range ["
@ -237,11 +234,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, parentParty); return partyAllCommand(sender, mainParty);
} else if (args[0].equalsIgnoreCase("me") && isPlayer) { } else if (args[0].equalsIgnoreCase("me") && isPlayer) {
return partyCommand(player, parentParty); return partyCommand(player, mainParty);
} else if (args[0].equalsIgnoreCase("other")) { } else if (args[0].equalsIgnoreCase("other")) {
return partyOtherCommand(parsePlayerList(args, 1), sender, parentParty); return partyOtherCommand(parsePlayerList(args, 1), sender, mainParty);
} 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;

View File

@ -10,6 +10,7 @@ commands:
Use /ds help for more information Use /ds help for more information
To stop your party, use /ds stop. To stop your party, use /ds stop.
permissions: permissions:
# If default is set to false, console will not have permission!
discosheep.*: discosheep.*:
description: Permission node for all DiscoSheep commands description: Permission node for all DiscoSheep commands
default: op default: op