From 0a7bd06579f8c79b5cf3320628c62cb1910b94ab Mon Sep 17 00:00:00 2001 From: Charlie Wang Date: Thu, 11 Jul 2013 23:25:48 -0400 Subject: [PATCH] added arguments for period, duration, number, and radius --- .../bukkit/discosheep/DiscoParty.java | 20 +++++--- .../bukkit/discosheep/DiscoSheep.java | 48 +++++++++++++++---- .../discosheep/DiscoSheepCommandExecutor.java | 45 +++++++++++++---- 3 files changed, 90 insertions(+), 23 deletions(-) diff --git a/src/gibstick/bukkit/discosheep/DiscoParty.java b/src/gibstick/bukkit/discosheep/DiscoParty.java index 9655273..98af7c8 100644 --- a/src/gibstick/bukkit/discosheep/DiscoParty.java +++ b/src/gibstick/bukkit/discosheep/DiscoParty.java @@ -25,11 +25,17 @@ public class DiscoParty { private DiscoSheep ds; private Player player; private ArrayList sheepList = new ArrayList(); - private int duration, frequency = 20, numSheep = 5; - static final int defaultDuration = 300; // ticks for entire party - static final int defaultFrequency = 10; // ticks per state change - static final int defaultSheepSpawnRadius = 5; - static final int defaultSheepAmount = 10; + private int duration, frequency = 20; + private int numSheep = 5; + static int defaultDuration = 300; // ticks for entire party + static int defaultPeriod = 10; // ticks per state change + static int defaultRadius = 5; + static int defaultSheep = 10; + static int maxDuration = 2400; // 120 seconds + static int maxSheep = 100; + static int maxRadius = 100; + static int minPeriod = 5; // 0.25 seconds + static int maxPeriod = 40; // 2.0 seconds private boolean doFireworks = false; private int state = 0; private DiscoUpdater updater; @@ -223,13 +229,13 @@ public class DiscoParty { updater.runTaskLater(ds, this.frequency); } - void startDisco(int duration, int sheepAmount, int radius, boolean fireworks) { + void startDisco(int duration, int sheepAmount, int radius, int frequency, boolean fireworks) { if (this.duration > 0) { stopDisco(); } this.doFireworks = fireworks; this.spawnSheep(sheepAmount, radius); - this.frequency = this.defaultFrequency; + this.frequency = frequency; this.duration = duration; this.scheduleUpdate(); ds.getPartyMap().put(this.player.getName(), this); diff --git a/src/gibstick/bukkit/discosheep/DiscoSheep.java b/src/gibstick/bukkit/discosheep/DiscoSheep.java index d776a26..e4d8d8f 100644 --- a/src/gibstick/bukkit/discosheep/DiscoSheep.java +++ b/src/gibstick/bukkit/discosheep/DiscoSheep.java @@ -4,6 +4,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.bukkit.configuration.MemoryConfiguration; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -16,12 +18,45 @@ public final class DiscoSheep extends JavaPlugin { public void onEnable() { getCommand("ds").setExecutor(new DiscoSheepCommandExecutor(this)); getServer().getPluginManager().registerEvents(blockEvents, this); + + FileConfiguration config = this.getConfig(); + + config.addDefault("max.sheep", DiscoParty.maxSheep); + config.addDefault("max.radius", DiscoParty.maxRadius); + config.addDefault("max.duration", DiscoParty.maxDuration); + config.addDefault("max.period", DiscoParty.maxPeriod); + config.addDefault("min.period", DiscoParty.minPeriod); + config.addDefault("default.sheep", DiscoParty.defaultSheep); + config.addDefault("default.radius", DiscoParty.defaultRadius); + config.addDefault("default.duration", DiscoParty.defaultDuration); + config.addDefault("default.period", DiscoParty.defaultPeriod); + config.options().copyDefaults(true); + + saveConfig(); + + DiscoParty.maxSheep = getConfig().getInt("max.sheep"); + DiscoParty.maxRadius = getConfig().getInt("max.radius"); + DiscoParty.maxDuration = getConfig().getInt("max.duration"); + DiscoParty.maxPeriod = getConfig().getInt("max.period"); + DiscoParty.minPeriod = getConfig().getInt("min.period"); + DiscoParty.defaultSheep = getConfig().getInt("default.sheep"); + DiscoParty.defaultRadius = getConfig().getInt("default.radius"); + DiscoParty.defaultDuration = getConfig().getInt("default.duration"); + DiscoParty.defaultPeriod = getConfig().getInt("default.period"); } @Override public void onDisable() { this.stopAllParties(); } + + int toTicks(double seconds) { + return (int) Math.round(seconds * 20.0); + } + + double toSeconds(int ticks) { + return (double) Math.round(ticks / 20.0); + } public synchronized Map getPartyMap() { return this.parties; @@ -36,9 +71,9 @@ public final class DiscoSheep extends JavaPlugin { this.getParty(name).stopDisco(); } } - - public void stopAllParties(){ - for(DiscoParty party :this.getParties()){ + + public void stopAllParties() { + for (DiscoParty party : this.getParties()) { party.stopDisco(); } } @@ -57,12 +92,9 @@ public final class DiscoSheep extends JavaPlugin { } } - public void startParty(Player player, int duration, int sheepAmount, int radius, boolean fireworksEnabled) { + public void startParty(Player player, int duration, int sheepAmount, int radius, int period, boolean fireworksEnabled) { if (!hasParty(player.getName())) { - new DiscoParty(this, player).startDisco(duration, sheepAmount, radius, fireworksEnabled); - } else { - player.sendMessage("You has party"); + new DiscoParty(this, player).startDisco(duration, sheepAmount, radius, period, fireworksEnabled); } } - } diff --git a/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java b/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java index ead77a3..8ca3cfa 100644 --- a/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java +++ b/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java @@ -42,14 +42,22 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { return -1; } + private Double parseNextDoubleArg(String[] args, int i) { + if (i < args.length - 1) { + return Double.parseDouble(args[i + 1]); + } + return -1.0d; + } + @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { Player player = null; boolean isPlayer = false; boolean fireworks = false; - int sheepNumber = DiscoParty.defaultSheepAmount; - int radius = DiscoParty.defaultSheepSpawnRadius; + int sheepNumber = DiscoParty.defaultSheep; + int radius = DiscoParty.defaultRadius; int duration = DiscoParty.defaultDuration; + int period = DiscoParty.defaultPeriod; if (sender instanceof Player) { player = (Player) sender; @@ -71,15 +79,36 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { } else if (args[i].equalsIgnoreCase("-r")) { radius = parseNextIntArg(args, i); - if (radius < 1 || radius > 100) { - sender.sendMessage("Radius must be an integer within the range [1, 100]"); + if (radius < 1 || radius > DiscoParty.maxRadius) { + sender.sendMessage("Radius must be an integer within the range [1, " + + DiscoParty.maxRadius + "]"); return true; } } else if (args[i].equalsIgnoreCase("-n")) { sheepNumber = parseNextIntArg(args, i); - if (sheepNumber < 1 || sheepNumber > 100) { - sender.sendMessage("The number of sheep must be an integer within the range [1, 100]"); + if (sheepNumber < 1 || sheepNumber > DiscoParty.maxSheep) { + sender.sendMessage("The number of sheep must be an integer within the range [1, " + + DiscoParty.maxSheep + "]"); + return true; + } + } else if (args[i].equalsIgnoreCase("-t")) { + duration = parseNextIntArg(args, i); + + if (duration < 1 || duration > parent.toSeconds(DiscoParty.maxDuration)) { + sender.sendMessage("The duration in ticks must be an integer within the range [1, " + + parent.toSeconds(DiscoParty.maxDuration) + "]"); + return true; + } + } else if (args[i].equalsIgnoreCase("-p")) { + period = parseNextIntArg(args, i); + + if (period < DiscoParty.minPeriod || period > DiscoParty.maxPeriod) { + sender.sendMessage( + "The period in ticks must be within the range [" + + DiscoParty.minPeriod + ", " + + DiscoParty.maxPeriod + "]"); + return true; } } } @@ -89,7 +118,7 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { if (args[0].equalsIgnoreCase("all")) { if (senderHasPerm(sender, PERMISSION_ALL)) { for (Player p : Bukkit.getServer().getOnlinePlayers()) { - parent.startParty(p, duration, sheepNumber, radius, fireworks); + parent.startParty(p, duration, sheepNumber, radius, period, fireworks); p.sendMessage(ChatColor.RED + "LET'S DISCO!"); } } else { @@ -105,7 +134,7 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { return true; } else if (args[0].equalsIgnoreCase("me")) { if (isPlayer) { - parent.startParty(player, duration, sheepNumber, radius, fireworks); + parent.startParty(player, duration, sheepNumber, radius, period, fireworks); return true; } } else {