Refactored commands to use sk89q's command lib

plugin main class now more or less follows the singleton pattern
This commit is contained in:
Charlie 2014-07-29 16:40:31 -04:00
parent 52d335ddf6
commit 60cd6f268b
7 changed files with 440 additions and 368 deletions

View File

@ -73,7 +73,7 @@
</artifactSet>
<generateUniqueDependencyReducedPom>false</generateUniqueDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached> <!-- Makes Netbeans shut up -->
<shadedClassifierName>unshaded</shadedClassifierName>
<shadedClassifierName>shaded</shadedClassifierName>
</configuration>
</execution>
</executions>

View File

@ -0,0 +1,223 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ca.gibstick.discosheep;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.minecraft.util.commands.NestedCommand;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
*
* @author Charlie
*/
public class DiscoCommands {
static final String PERMISSION_PARTY = "discosheep.party.me";
static final String PERMISSION_ALL = "discosheep.party.all";
static final String PERMISSION_FIREWORKS = "discosheep.party.fireworks";
static final String PERMISSION_STOPALL = "discosheep.admin.stopall";
static final String PERMISSION_RELOAD = "discosheep.admin.reload";
static final String PERMISSION_OTHER = "discosheep.party.other";
static final String PERMISSION_CHANGEPERIOD = "discosheep.party.changeperiod";
static final String PERMISSION_CHANGEDEFAULTS = "discosheep.admin.changedefaults";
static final String PERMISSION_SAVECONFIG = "discosheep.admin.saveconfig";
static final String PERMISSION_ONJOIN = "discosheep.party.onjoin";
static final String PERMISSION_SPAWNGUESTS = "discosheep.party.spawnguests";
static final String PERMISSION_TOGGLEPARTYONJOIN = "discosheep.admin.toggleonjoin";
static final String PERMISSION_LIGHTNING = "discosheep.party.lightning";
static final String FLAGS = ":n:t:p:rlf";
private static final DiscoSheep plugin = DiscoSheep.getInstance();
public static class ParentCommand {
@Command(aliases = {"discosheep", "ds"}, desc = "Main Discosheep Command", min = 0, max = -1)
@NestedCommand(DiscoCommands.class)
public static void DiscoCommand(final CommandContext args, CommandSender sender) throws CommandException {
}
}
private static void parsePartyFlags(DiscoParty party, final CommandContext args, CommandSender sender) throws IllegalArgumentException {
party.setDuration(args.getFlagInteger('t', DiscoParty.defaultDuration));
party.setPeriod(args.getFlagInteger('p', DiscoParty.defaultPeriod));
party.setRadius(args.getFlagInteger('r', DiscoParty.defaultRadius));
party.setSheep(args.getFlagInteger('n', DiscoParty.defaultSheep));
// handle the special case of radius flag arg "dense"
/*String radiusArg = args.getFlag('r');
if ("dense".equals(radiusArg)) {
party.setDenseRadius(party.getSheep());
} else {
party.setRadius(Integer.parseInt(radiusArg));
}*/
if (sender.hasPermission(PERMISSION_FIREWORKS)) {
party.setDoFireworks(args.hasFlag('f'));
} else {
plugin.noPermsMessage(sender, PERMISSION_FIREWORKS);
}
if (sender.hasPermission(PERMISSION_LIGHTNING)) {
party.setDoLightning(args.hasFlag('l'));
} else {
plugin.noPermsMessage(sender, PERMISSION_LIGHTNING);
}
}
@Command(aliases = {"test"}, desc = "Test command", usage = "No arguments", min = 0, max = 0)
public static void test(final CommandContext args, CommandSender sender) throws CommandException {
sender.sendMessage("TESTING");
}
@Command(aliases = {"stop", "stoppls", "wtf"}, desc = "Stop your own disco party", usage = "No arguments", min = 0, max = 0)
public static void stopMeCommand(final CommandContext args, CommandSender sender) throws CommandException {
plugin.stopParty(sender.getName());
}
@Command(aliases = {"stopall"}, desc = "Stop all disco parties on the server", usage = "No arguments", min = 0, max = 0)
@CommandPermissions(value = PERMISSION_STOPALL)
public static void stopAllCommand(final CommandContext args, CommandSender sender) throws CommandException {
plugin.stopAllParties();
}
@Command(aliases = {"reload"}, desc = "Reload DiscoSheep configuration from file", usage = "No arguments", min = 0, max = 0)
public static void reloadCommand(final CommandContext args, CommandSender sender) {
plugin.reloadConfigFromDisk();
sender.sendMessage(ChatColor.GREEN + "DiscoSheep config reloaded from file.");
}
@Command(
aliases = {"me", "party", "partay", "turnup"},
desc = "Start your own private DiscoParty",
usage = "[optional flags]",
min = 0,
max = -1,
flags = FLAGS
)
@CommandPermissions(value = PERMISSION_PARTY)
public static void partyCommand(final CommandContext args, CommandSender sender) {
if (!(sender instanceof Player)) {
sender.sendMessage("You must be a player to have a party");
return;
}
Player player = (Player) sender;
if (!plugin.hasParty(player.getName())) {
DiscoParty party = new DiscoParty(player);
parsePartyFlags(party, args, sender);
party.startDisco();
} else {
player.sendMessage(ChatColor.RED + "You already have a party.");
}
}
@SuppressWarnings("deprecation")
// UUIDs not necessary since DiscoSheep only lasts for one session at most
// and permissions will handle onJoin DiscoSheep
@Command(
aliases = {"other", "yall"},
desc = "Start a party for other players",
usage = "[optional flags]",
min = 0,
max = -1,
flags = FLAGS
)
@CommandPermissions(value = PERMISSION_OTHER)
public static void partyOtherCommand(CommandContext args, CommandSender sender) {
DiscoParty party = new DiscoParty();
Player p;
String players[] = args.getSlice(0);
parsePartyFlags(party, args, sender);
for (String playerName : players) {
p = Bukkit.getServer().getPlayer(playerName);
if (p != null) {
if (!plugin.hasParty(p.getName())) {
DiscoParty individualParty = party.clone(p);
individualParty.startDisco();
}
} else {
sender.sendMessage("Invalid player: " + playerName);
}
}
}
@Command(
aliases = {"all", "allturnup"},
desc = "Start a party for all players on the server",
usage = "[optional flags]",
min = 0,
max = -1,
flags = FLAGS
)
@CommandPermissions(value = PERMISSION_ALL)
public static void partyAllCommand(final CommandContext args, CommandSender sender) {
DiscoParty party = new DiscoParty();
parsePartyFlags(party, args, sender);
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (!plugin.hasParty(p.getName())) {
DiscoParty individualParty = party.clone(p);
individualParty.startDisco();
p.sendMessage(ChatColor.RED + "LET'S DISCO!!");
}
}
}
@Command(
aliases = {"togglejoin", "toggleonjoin"},
desc = "Start a party for all players on the server",
usage = "[optional flags]",
min = 0,
max = -1,
flags = FLAGS
)
@CommandPermissions(value = PERMISSION_TOGGLEPARTYONJOIN)
public static void togglePartyOnJoinCommand(final CommandContext args, CommandSender sender) {
boolean result = plugin.toggleOnJoin();
if (result) {
sender.sendMessage(ChatColor.GREEN + "DiscoSheep party on join functionality enabled.");
} else {
sender.sendMessage(ChatColor.GREEN + "DiscoSheep party on join functionality disabled.");
}
}
@Command(
aliases = {"defaults", "setdefaults"},
desc = "Change the default party settings",
usage = "[optional flags]",
min = 0,
max = -1,
flags = FLAGS
)
@CommandPermissions(value = PERMISSION_CHANGEDEFAULTS)
public static void setDefaultsCommand(final CommandContext args, CommandSender sender) {
DiscoParty party = new DiscoParty();
parsePartyFlags(party, args, sender);
party.setDefaultsFromCurrent();
sender.sendMessage(ChatColor.GREEN + "DiscoSheep configured with new defaults (not saved to disk yet)");
}
@Command(
aliases = {"defaults", "setdefaults"},
desc = "Change the default party settings",
usage = "[optional flags]",
min = 0,
max = -1,
flags = FLAGS
)
@CommandPermissions(value = PERMISSION_SAVECONFIG)
public static void saveConfigCommand(final CommandContext args, CommandSender sender) {
plugin.saveConfigToDisk();
sender.sendMessage(ChatColor.GREEN + "DiscoSheep config saved to disk");
}
}

View File

@ -65,7 +65,7 @@ public class DiscoParty {
// Instance properties
private Random r = new Random();
private PartyEvents partyEvents;
private DiscoSheep parent;
private final DiscoSheep parent = DiscoSheep.getInstance();
private Player player;
private ArrayList<Sheep> sheepList = new ArrayList<Sheep>();
private ArrayList<Entity> guestList = new ArrayList<Entity>();
@ -79,13 +79,12 @@ public class DiscoParty {
private int state = 0; // basically our own tick system
private DiscoUpdater updater;
public DiscoParty(DiscoSheep parent, Player player) {
this(parent);
public DiscoParty(Player player) {
this();
this.player = player;
}
public DiscoParty(DiscoSheep parent) {
this.parent = parent;
public DiscoParty() {
this.duration = DiscoParty.defaultDuration;
this.period = DiscoParty.defaultPeriod;
this.radius = DiscoParty.defaultRadius;
@ -98,7 +97,7 @@ public class DiscoParty {
// used for /ds other and /ds all
public DiscoParty clone(Player player) {
DiscoParty newParty;
newParty = new DiscoParty(this.parent, player);
newParty = new DiscoParty(player);
newParty.doFireworks = this.doFireworks;
newParty.duration = this.duration;
newParty.period = this.period;

View File

@ -1,37 +1,84 @@
package ca.gibstick.discosheep;
import com.sk89q.bukkit.util.CommandsManagerRegistration;
import com.sk89q.minecraft.util.commands.CommandPermissionsException;
import com.sk89q.minecraft.util.commands.CommandUsageException;
import com.sk89q.minecraft.util.commands.CommandsManager;
import com.sk89q.minecraft.util.commands.MissingNestedCommandException;
import com.sk89q.minecraft.util.commands.WrappedCommandException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public final class DiscoSheep extends JavaPlugin {
static final String PERMISSION_PARTY = "discosheep.party.me";
static final String PERMISSION_ALL = "discosheep.party.all";
static final String PERMISSION_FIREWORKS = "discosheep.party.fireworks";
static final String PERMISSION_STOPALL = "discosheep.admin.stopall";
static final String PERMISSION_RELOAD = "discosheep.admin.reload";
static final String PERMISSION_OTHER = "discosheep.party.other";
static final String PERMISSION_CHANGEPERIOD = "discosheep.party.changeperiod";
static final String PERMISSION_CHANGEDEFAULTS = "discosheep.admin.changedefaults";
static final String PERMISSION_SAVECONFIG = "discosheep.admin.saveconfig";
static final String PERMISSION_ONJOIN = "discosheep.party.onjoin";
static final String PERMISSION_SPAWNGUESTS = "discosheep.party.spawnguests";
static final String PERMISSION_TOGGLEPARTYONJOIN = "discosheep.admin.toggleonjoin";
static final String PERMISSION_LIGHTNING = "discosheep.party.lightning";
private static DiscoSheep instance;
static boolean partyOnJoin = false;
Map<String, DiscoParty> parties = new HashMap<String, DiscoParty>();
private CommandsManager<CommandSender> commands;
public static DiscoSheep getInstance() {
if (instance == null) {
instance = new DiscoSheep();
return instance;
}
return instance;
}
private void setupCommands() {
this.commands = new CommandsManager<CommandSender>() {
@Override
public boolean hasPermission(CommandSender sender, String perm) {
return sender instanceof ConsoleCommandSender || sender.hasPermission(perm);
}
};
CommandsManagerRegistration cmdRegister = new CommandsManagerRegistration(this, this.commands);
cmdRegister.register(DiscoCommands.ParentCommand.class);
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
try {
this.commands.execute(cmd.getName(), args, sender, sender);
} catch (CommandPermissionsException e) {
sender.sendMessage(ChatColor.RED + "You don't have permission.");
} catch (MissingNestedCommandException e) {
sender.sendMessage(ChatColor.RED + e.getUsage());
} catch (CommandUsageException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
sender.sendMessage(ChatColor.RED + e.getUsage());
} catch (WrappedCommandException e) {
if (e.getCause() instanceof NumberFormatException) {
sender.sendMessage(ChatColor.RED + "Number expected, string received instead.");
} else {
sender.sendMessage(ChatColor.RED + "An error has occurred. See console.");
e.printStackTrace();
}
} catch (CommandException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
} catch (com.sk89q.minecraft.util.commands.CommandException ex) {
Logger.getLogger(DiscoSheep.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}
@Override
public void onEnable() {
getCommand("ds").setExecutor(new DiscoSheepCommandExecutor(this));
getServer().getPluginManager().registerEvents(new GlobalEvents(this), this);
instance = this;
//getCommand("ds").setExecutor(new DiscoSheepCommandExecutor(this));
setupCommands();
getServer().getPluginManager().registerEvents(new GlobalEvents(), this);
getConfig().addDefault("on-join.enabled", partyOnJoin);
getConfig().addDefault("max.sheep", DiscoParty.maxSheep);
@ -111,6 +158,8 @@ public final class DiscoSheep extends JavaPlugin {
@Override
public void onDisable() {
this.stopAllParties(); // or else the parties will continue FOREVER
instance = null;
commands = null;
}
static int toTicks(double seconds) {
@ -153,6 +202,11 @@ public final class DiscoSheep extends JavaPlugin {
return this.getPartyMap().get(name);
}
public boolean toggleOnJoin() {
DiscoSheep.partyOnJoin = !DiscoSheep.partyOnJoin;
return DiscoSheep.partyOnJoin;
}
public void removeParty(String name) {
if (this.hasParty(name)) {
this.getPartyMap().remove(name);
@ -160,145 +214,144 @@ public final class DiscoSheep extends JavaPlugin {
}
/*-- 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, save, reload, togglejoin\n"
+ "other <players>: start a party for the space-delimited list of players\n"
+ "defaults: Change the default settings for parties (takes normal arguments)\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"
//+ "-g <mob> <number>: set spawns for other mobs\n"
+ "-l: enables lightning\n"
+ "-fw: enables fireworks");
return true;
}
/*boolean helpCommand(CommandSender sender) {
sender.sendMessage(ChatColor.YELLOW
+ "DiscoSheep Help\n"
+ ChatColor.GRAY
+ " Subcommands\n"
+ ChatColor.WHITE + "me, stop, all, stopall, save, reload, togglejoin\n"
+ "other <players>: start a party for the space-delimited list of players\n"
+ "defaults: Change the default settings for parties (takes normal arguments)\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"
//+ "-g <mob> <number>: set spawns for other mobs\n"
+ "-l: enables lightning\n"
+ "-fw: enables fireworks");
return true;
}
boolean stopMeCommand(CommandSender sender) {
stopParty(sender.getName());
return true;
}
boolean stopMeCommand(CommandSender sender) {
stopParty(sender.getName());
return true;
}
boolean stopAllCommand(CommandSender sender) {
if (sender.hasPermission(PERMISSION_STOPALL)) {
stopAllParties();
return true;
} else {
return noPermsMessage(sender, PERMISSION_STOPALL);
}
}
boolean stopAllCommand(CommandSender sender) {
if (sender.hasPermission(PERMISSION_STOPALL)) {
stopAllParties();
return true;
} else {
return noPermsMessage(sender, PERMISSION_STOPALL);
}
}
boolean partyCommand(Player player, DiscoParty party) {
if (player.hasPermission(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, PERMISSION_PARTY);
}
}
boolean partyCommand(Player player, DiscoParty party) {
if (player.hasPermission(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, PERMISSION_PARTY);
}
}
boolean reloadCommand(CommandSender sender) {
if (sender.hasPermission(PERMISSION_RELOAD)) {
reloadConfigFromDisk();
sender.sendMessage(ChatColor.GREEN + "DiscoSheep config reloaded from disk");
return true;
} else {
return noPermsMessage(sender, PERMISSION_RELOAD);
}
}
boolean reloadCommand(CommandSender sender) {
if (sender.hasPermission(PERMISSION_RELOAD)) {
reloadConfigFromDisk();
sender.sendMessage(ChatColor.GREEN + "DiscoSheep config reloaded from disk");
return true;
} else {
return noPermsMessage(sender, PERMISSION_RELOAD);
}
}
@SuppressWarnings("deprecation")
// UUIDs not necessary since DiscoSheep only lasts for one session at most
// and permissions will handle onJoin DiscoSheep
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 (!hasParty(p.getName())) {
DiscoParty individualParty = party.clone(p);
individualParty.startDisco();
}
} else {
sender.sendMessage("Invalid player: " + playerName);
}
}
return true;
} else {
return noPermsMessage(sender, PERMISSION_OTHER);
}
}
@SuppressWarnings("deprecation")
// UUIDs not necessary since DiscoSheep only lasts for one session at most
// and permissions will handle onJoin DiscoSheep
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 (!hasParty(p.getName())) {
DiscoParty individualParty = party.clone(p);
individualParty.startDisco();
}
} else {
sender.sendMessage("Invalid player: " + playerName);
}
}
return true;
} else {
return noPermsMessage(sender, PERMISSION_OTHER);
}
}
boolean partyAllCommand(CommandSender sender, DiscoParty party) {
if (sender.hasPermission(PERMISSION_ALL)) {
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (!hasParty(p.getName())) {
DiscoParty individualParty = party.clone(p);
individualParty.startDisco();
p.sendMessage(ChatColor.RED + "LET'S DISCO!!");
}
}
return true;
} else {
return noPermsMessage(sender, PERMISSION_ALL);
}
}
boolean partyAllCommand(CommandSender sender, DiscoParty party) {
if (sender.hasPermission(PERMISSION_ALL)) {
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (!hasParty(p.getName())) {
DiscoParty individualParty = party.clone(p);
individualParty.startDisco();
p.sendMessage(ChatColor.RED + "LET'S DISCO!!");
}
}
return true;
} else {
return noPermsMessage(sender, PERMISSION_ALL);
}
}
boolean togglePartyOnJoinCommand(CommandSender sender) {
if (!sender.hasPermission(PERMISSION_TOGGLEPARTYONJOIN)) {
return noPermsMessage(sender, PERMISSION_TOGGLEPARTYONJOIN);
}
partyOnJoin = !partyOnJoin;
if (partyOnJoin) {
sender.sendMessage(ChatColor.GREEN + "DiscoSheep party on join functionality enabled.");
} else {
sender.sendMessage(ChatColor.GREEN + "DiscoSheep party on join functionality disabled.");
}
return true;
}
boolean setDefaultsCommand(CommandSender sender, DiscoParty party) {
if (sender.hasPermission(PERMISSION_CHANGEDEFAULTS)) {
party.setDefaultsFromCurrent();
sender.sendMessage(ChatColor.GREEN + "DiscoSheep configured with new defaults (not saved to disk yet)");
return true;
} else {
return noPermsMessage(sender, PERMISSION_CHANGEDEFAULTS);
}
}
boolean saveConfigCommand(CommandSender sender) {
if (sender.hasPermission(PERMISSION_SAVECONFIG)) {
saveConfigToDisk();
sender.sendMessage(ChatColor.GREEN + "DiscoSheep config saved to disk");
return true;
} else {
return noPermsMessage(sender, PERMISSION_SAVECONFIG);
}
}*/
void partyOnJoin(Player player) {
if (!partyOnJoin) {
return;
}
if (player.hasPermission(PERMISSION_ONJOIN)) {
DiscoParty party = new DiscoParty(this, player);
if (player.hasPermission(DiscoCommands.PERMISSION_ONJOIN)) {
DiscoParty party = new DiscoParty(player);
party.startDisco();
}
}
boolean togglePartyOnJoinCommand(CommandSender sender) {
if (!sender.hasPermission(PERMISSION_TOGGLEPARTYONJOIN)) {
return noPermsMessage(sender, PERMISSION_TOGGLEPARTYONJOIN);
}
partyOnJoin = !partyOnJoin;
if (partyOnJoin) {
sender.sendMessage(ChatColor.GREEN + "DiscoSheep party on join functionality enabled.");
} else {
sender.sendMessage(ChatColor.GREEN + "DiscoSheep party on join functionality disabled.");
}
return true;
}
boolean setDefaultsCommand(CommandSender sender, DiscoParty party) {
if (sender.hasPermission(PERMISSION_CHANGEDEFAULTS)) {
party.setDefaultsFromCurrent();
sender.sendMessage(ChatColor.GREEN + "DiscoSheep configured with new defaults (not saved to disk yet)");
return true;
} else {
return noPermsMessage(sender, PERMISSION_CHANGEDEFAULTS);
}
}
boolean saveConfigCommand(CommandSender sender) {
if (sender.hasPermission(PERMISSION_SAVECONFIG)) {
saveConfigToDisk();
sender.sendMessage(ChatColor.GREEN + "DiscoSheep config saved to disk");
return true;
} else {
return noPermsMessage(sender, PERMISSION_SAVECONFIG);
}
}
boolean clearGuests(DiscoParty party) {
party.getGuestNumbers().clear();
return true;

View File

@ -1,199 +0,0 @@
package ca.gibstick.discosheep;
import java.util.Arrays;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class DiscoSheepCommandExecutor implements CommandExecutor {
private final DiscoSheep parent;
public DiscoSheepCommandExecutor(DiscoSheep parent) {
this.parent = parent;
}
private boolean parseNextArg(String[] args, int i, String compare) {
if (i < args.length - 1) {
return args[i + 1].equalsIgnoreCase(compare);
}
return false;
}
private String getNextArg(String[] args, int i) {
if (i < args.length - 1) {
return args[i + 1];
} else {
return null;
}
}
private int getNextIntArg(String[] args, int i) {
if (i < args.length - 1) {
try {
return Integer.parseInt(args[i + 1]);
} catch (NumberFormatException e) {
return -1; // so that it fails limit checks elsewhere
}
}
return -1; // ibid
}
private Double getNextDoubleArg(String[] args, int i) {
if (i < args.length - 1) {
try {
return Double.parseDouble(args[i + 1]);
} catch (NumberFormatException e) {
return -1.0d; // so that it fais limit checks elsewhere
}
}
return -1.0d; // ibid
}
// return portion of the array that contains space-separated args,
// stopping at the end of the array or the next -switch
private String[] getNextArgs(String[] args, int i) {
int j = i;
while (j < args.length && !args[j].startsWith("-")) {
j++;
}
return Arrays.copyOfRange(args, i, j);
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player player = null;
boolean isPlayer = false;
boolean specialRadius = false;
// flag to determine if we calculate a radius so that the sheep spawn densely in an area
if (sender instanceof Player) {
player = (Player) sender;
isPlayer = true;
} // check isPlayer before "stop" and "me" commands
// 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[0].equalsIgnoreCase("stopall")) {
return parent.stopAllCommand(sender);
} else if (args[0].equalsIgnoreCase("stop") && isPlayer) {
return parent.stopMeCommand(sender);
} else if (args[0].equalsIgnoreCase("help")) {
return parent.helpCommand(sender);
} else if (args[0].equalsIgnoreCase("reload")) {
return parent.reloadCommand(sender);
} else if (args[0].equalsIgnoreCase("save") || args[0].equalsIgnoreCase("saveconfig")) {
return parent.saveConfigCommand(sender);
} else if (args[0].equalsIgnoreCase("togglejoin")) {
return parent.togglePartyOnJoinCommand(sender);
}
}
// 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++) {
if (args[i].equalsIgnoreCase("-fw")) {
if (sender.hasPermission(DiscoSheep.PERMISSION_FIREWORKS)) {
mainParty.setDoFireworks(true);
} else {
return parent.noPermsMessage(sender, DiscoSheep.PERMISSION_FIREWORKS);
}
} else if (args[i].equalsIgnoreCase("-r")) {
if (parseNextArg(args, i, "dense")) {
specialRadius = true;
}
if (!specialRadius) {
try {
mainParty.setRadius(getNextIntArg(args, i));
} catch (IllegalArgumentException e) {
sender.sendMessage("Radius must be an integer within the range [1, "
+ DiscoParty.maxRadius + "]");
return false;
}
}
} else if (args[i].equalsIgnoreCase("-n")) {
try {
mainParty.setSheep(getNextIntArg(args, i));
} catch (IllegalArgumentException e) {
sender.sendMessage("The number of sheep must be an integer within the range [1, "
+ DiscoParty.maxSheep + "]");
return false;
}
} else if (args[i].equalsIgnoreCase("-t")) {
try {
mainParty.setDuration(parent.toTicks(getNextIntArg(args, i)));
} catch (IllegalArgumentException e) {
sender.sendMessage("The duration in seconds must be an integer within the range [1, "
+ parent.toSeconds(DiscoParty.maxDuration) + "]");
return false;
}
} else if (args[i].equalsIgnoreCase("-p")) {
if (!sender.hasPermission(DiscoSheep.PERMISSION_CHANGEPERIOD)) {
return parent.noPermsMessage(sender, DiscoSheep.PERMISSION_CHANGEPERIOD);
}
try {
mainParty.setPeriod(getNextIntArg(args, i));
} catch (IllegalArgumentException e) {
sender.sendMessage(
"The period in ticks must be within the range ["
+ DiscoParty.minPeriod + ", "
+ DiscoParty.maxPeriod + "]");
return false;
}
// } else if (args[i].equalsIgnoreCase("-g")) {
// if (!sender.hasPermission(DiscoSheep.PERMISSION_SPAWNGUESTS)) {
// return parent.noPermsMessage(sender, DiscoSheep.PERMISSION_SPAWNGUESTS);
// }
//
// if (parseNextArg(args, i, "none")) {
// return parent.clearGuests(mainParty);
// }
//
// String[] guests = getNextArgs(args, i + 1);
// int j = 0;
// while (j < guests.length - 1) {
// try {
// mainParty.setGuestNumber(guests[j], getNextIntArg(guests, j));
// } catch (IllegalArgumentException e) {
// sender.sendMessage(ChatColor.RED + "Invalid arguments: " + ChatColor.WHITE + guests[j] + ", " + guests[j + 1]
// + ".\nEither a name typo or a number that is not within limits.");
// }
// j += 2; // skip over two arguments, since they come in pairs of entity-number
// }
} else if (args[i].equalsIgnoreCase("-l")) {
if (!sender.hasPermission(DiscoSheep.PERMISSION_LIGHTNING)) {
return parent.noPermsMessage(sender, DiscoSheep.PERMISSION_LIGHTNING);
}
mainParty.setDoLightning(true);
}
}
if (specialRadius) {
mainParty.setDenseRadius(mainParty.getSheep());
}
if (args.length > 0) {
if (args[0].equalsIgnoreCase("all")) {
return parent.partyAllCommand(sender, mainParty);
} else if (args[0].equalsIgnoreCase("me") && isPlayer) {
return parent.partyCommand(player, mainParty);
} else if (args[0].equalsIgnoreCase("other")) {
return parent.partyOtherCommand(getNextArgs(args, 1), sender, mainParty);
} else if (args[0].equalsIgnoreCase("defaults")) {
return parent.setDefaultsCommand(sender, mainParty);
} else {
sender.sendMessage(ChatColor.RED + "Invalid argument (certain commands do not work from console).");
return false;
}
}
return false;
}
}

View File

@ -14,22 +14,18 @@ import org.bukkit.event.player.PlayerQuitEvent;
public class GlobalEvents implements Listener {
DiscoSheep parent;
public GlobalEvents(DiscoSheep parent) {
this.parent = parent;
}
DiscoSheep plugin = DiscoSheep.getInstance();
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuitEvent(PlayerQuitEvent e) {
String name = e.getPlayer().getName();
parent.stopParty(name);
plugin.stopParty(name);
// stop party on player quit or else it will CONTINUE FOR ETERNITY
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoinEvent(PlayerJoinEvent e) {
Player player = e.getPlayer();
parent.partyOnJoin(player);
plugin.partyOnJoin(player);
}
}

View File

@ -1,14 +1,14 @@
name: DiscoSheep
main: ca.gibstick.discosheep.DiscoSheep
authors: [Gibstick, RangerMauve]
version: 1.1.1
version: 1.1.2
commands:
ds:
description: "Main DiscoSheep command"
usage: |
<command> <subcommand> [arguments]
Use /ds help for more information
To stop your party, use /ds stop.
#ds:
#description: "Main DiscoSheep command"
#usage: |
#<command> <subcommand> [arguments]
#Use /ds help for more information
#To stop your party, use /ds stop.
permissions:
# If default is set to false, console will not have permission!
discosheep.*: