From 43fd8815f5fb2237db67f76c4e81c9fe96285d21 Mon Sep 17 00:00:00 2001 From: Charlie Wang Date: Thu, 11 Jul 2013 11:28:20 -0400 Subject: [PATCH] rewrote command handling and added fireworks toggle switch --- .../bukkit/discosheep/DiscoParty.java | 10 ++- .../bukkit/discosheep/DiscoSheep.java | 4 +- .../discosheep/DiscoSheepCommandExecutor.java | 88 +++++++++++++++---- src/plugin.yml | 4 +- 4 files changed, 81 insertions(+), 25 deletions(-) diff --git a/src/gibstick/bukkit/discosheep/DiscoParty.java b/src/gibstick/bukkit/discosheep/DiscoParty.java index ccf5d1c..f62dcea 100644 --- a/src/gibstick/bukkit/discosheep/DiscoParty.java +++ b/src/gibstick/bukkit/discosheep/DiscoParty.java @@ -30,6 +30,7 @@ public class DiscoParty { private final int defaultFrequency = 10; // ticks per state change private final int sheepSpawnRadius = 5; private final int defaultSheepAmount = 10; + private boolean doFireworks = false; private int state = 0; private DiscoUpdater updater; private static final DyeColor[] discoColours = { @@ -157,7 +158,7 @@ public class DiscoParty { void updateAllSheep() { for (Sheep sheep : getSheep()) { randomizeSheepColour(sheep); - if (state % 8 == 0) { + if (doFireworks && state % 8 == 0) { spawnRandomFireworkAtSheep(sheep); } } @@ -222,10 +223,11 @@ public class DiscoParty { updater.runTaskLater(ds, this.frequency); } - void startDisco(int duration) { + void startDisco(int duration, boolean fireworks) { if (this.duration > 0) { stopDisco(); } + this.doFireworks = fireworks; this.spawnSheep(this.defaultSheepAmount); this.frequency = this.defaultFrequency; this.duration = this.defaultDuration; @@ -233,8 +235,8 @@ public class DiscoParty { ds.getPartyMap().put(this.player.getName(), this); } - void startDisco() { - this.startDisco(this.defaultDuration); + void startDisco(boolean fireworks) { + this.startDisco(this.defaultDuration, fireworks); } void stopDisco() { diff --git a/src/gibstick/bukkit/discosheep/DiscoSheep.java b/src/gibstick/bukkit/discosheep/DiscoSheep.java index a2d0236..a5487be 100644 --- a/src/gibstick/bukkit/discosheep/DiscoSheep.java +++ b/src/gibstick/bukkit/discosheep/DiscoSheep.java @@ -57,9 +57,9 @@ public final class DiscoSheep extends JavaPlugin { } } - public void startParty(Player player) { + public void startParty(Player player, boolean fireworksEnabled) { if (!hasParty(player.getName())) { - new DiscoParty(this, player).startDisco(); + new DiscoParty(this, player).startDisco(fireworksEnabled); } } } diff --git a/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java b/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java index fca8241..be9d81c 100644 --- a/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java +++ b/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java @@ -1,6 +1,7 @@ package gibstick.bukkit.discosheep; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.CommandExecutor; @@ -13,26 +14,81 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { public DiscoSheepCommandExecutor(DiscoSheep parent) { this.parent = parent; } + + private CommandSender curSender; + + 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_STOP = "discosheep.stop"; + private static final String PERMISSION_SUPER= "disosheep.*"; + + + private boolean senderHasPerm(String permission) { + return curSender.hasPermission(permission) || curSender.hasPermission(PERMISSION_SUPER); + } + + private void noPermsMessage(String permission) { + curSender.sendMessage(ChatColor.RED + "You do not have the permission node " + ChatColor.GRAY + permission); + } @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (args.length == 1) { - if ("all".equals(args[0])) { - for (Player player : Bukkit.getServer().getOnlinePlayers()) { - parent.startParty(player); - } - } - else if ("stop".equals(args[0])) { - parent.stopAllParties(); - } else { - sender.sendMessage("Invalid argument."); - return true; - } - } else { - if (sender instanceof Player) { - parent.startParty((Player) sender); + Player player = null; + boolean isPlayer = false; + boolean fireworks = false; + this.curSender = sender; + + if (sender instanceof Player) { + player = (Player) sender; + isPlayer = true; + } + + if (!senderHasPerm(PERMISSION_PARTY)) { + noPermsMessage(PERMISSION_PARTY); + return true; + } + + for (String arg : args) { + switch (arg) { + case "-fw": + if (senderHasPerm(PERMISSION_FIREWORKS)) { + fireworks = !fireworks; + } else { + noPermsMessage(PERMISSION_FIREWORKS); + } } } - return true; + + if (args.length > 0) { + switch (args[0]) { + case "all": + if (senderHasPerm(PERMISSION_ALL)) + for (Player p : Bukkit.getServer().getOnlinePlayers()) { + parent.startParty(p, fireworks); + p.sendMessage(ChatColor.RED + "LET'S DISCO!"); + } else { + noPermsMessage(PERMISSION_ALL); + } + return true; + case "stop": + if (senderHasPerm(PERMISSION_STOP)) { + parent.stopAllParties(); + } else { + noPermsMessage(PERMISSION_STOP); + } + return true; + case "me": + if (isPlayer) { + parent.startParty(player, fireworks); + return true; + } + default: + sender.sendMessage(ChatColor.RED + "Invalid argument."); + return true; + } + } + + return false; } } diff --git a/src/plugin.yml b/src/plugin.yml index 5101a14..e7b7fd5 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -4,6 +4,4 @@ version: 0.9 commands: ds: description: "Main DiscoSheep command" - usage: /ds - permission: discosheep.party - permission-message: You don't have permission \ No newline at end of file + usage: /ds [arguments] \ No newline at end of file