From 52d335ddf655ac907b9e184a89aa938d5ad85b09 Mon Sep 17 00:00:00 2001 From: Charlie Date: Mon, 28 Jul 2014 11:53:28 -0400 Subject: [PATCH] formatting --- .../ca/gibstick/discosheep/DiscoParty.java | 870 +++++++++--------- .../ca/gibstick/discosheep/DiscoSheep.java | 526 ++++++----- .../discosheep/DiscoSheepCommandExecutor.java | 304 +++--- .../ca/gibstick/discosheep/GlobalEvents.java | 30 +- .../ca/gibstick/discosheep/PartyEvents.java | 90 +- 5 files changed, 908 insertions(+), 912 deletions(-) diff --git a/src/main/java/ca/gibstick/discosheep/DiscoParty.java b/src/main/java/ca/gibstick/discosheep/DiscoParty.java index 7cb2a5b..4637b7b 100644 --- a/src/main/java/ca/gibstick/discosheep/DiscoParty.java +++ b/src/main/java/ca/gibstick/discosheep/DiscoParty.java @@ -26,502 +26,500 @@ import org.bukkit.util.Vector; public class DiscoParty { - // Static properties - 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 float defaultSheepJump = 0.35f; - 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 static HashMap defaultGuestNumbers = new HashMap(); - private static HashMap maxGuestNumbers = new HashMap(); - private static final DyeColor[] discoColours = { - DyeColor.RED, - DyeColor.ORANGE, - DyeColor.YELLOW, - DyeColor.GREEN, - DyeColor.BLUE, - DyeColor.LIGHT_BLUE, - DyeColor.PINK, - DyeColor.MAGENTA, - DyeColor.LIME, - DyeColor.CYAN, - DyeColor.PURPLE, - DyeColor.BLACK, - DyeColor.WHITE - }; - private static final float[] pentatonicNotes = { - 1.0f, - 1.125f, - 1.25f, - 1.5f, - 1.667f, - 2.0f - }; - // Instance properties - private Random r = new Random(); - private PartyEvents partyEvents; - private DiscoSheep parent; - private Player player; - private ArrayList sheepList = new ArrayList(); - private ArrayList guestList = new ArrayList(); - private ArrayList floorBlockCache = new ArrayList(); - private ArrayList floorBlocks = new ArrayList(); - private HashMap guestNumbers = new HashMap(); - private boolean doFireworks = false; - private boolean doJump = true; - private boolean doLightning = false; - private int duration, period, radius, sheep; - private int state = 0; // basically our own tick system - private DiscoUpdater updater; + // Static properties + 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 float defaultSheepJump = 0.35f; + 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 static HashMap defaultGuestNumbers = new HashMap(); + private static HashMap maxGuestNumbers = new HashMap(); + private static final DyeColor[] discoColours = { + DyeColor.RED, + DyeColor.ORANGE, + DyeColor.YELLOW, + DyeColor.GREEN, + DyeColor.BLUE, + DyeColor.LIGHT_BLUE, + DyeColor.PINK, + DyeColor.MAGENTA, + DyeColor.LIME, + DyeColor.CYAN, + DyeColor.PURPLE, + DyeColor.BLACK, + DyeColor.WHITE + }; + private static final float[] pentatonicNotes = { + 1.0f, + 1.125f, + 1.25f, + 1.5f, + 1.667f, + 2.0f + }; + // Instance properties + private Random r = new Random(); + private PartyEvents partyEvents; + private DiscoSheep parent; + private Player player; + private ArrayList sheepList = new ArrayList(); + private ArrayList guestList = new ArrayList(); + private ArrayList floorBlockCache = new ArrayList(); + private ArrayList floorBlocks = new ArrayList(); + private HashMap guestNumbers = new HashMap(); + private boolean doFireworks = false; + private boolean doJump = true; + private boolean doLightning = false; + private int duration, period, radius, sheep; + private int state = 0; // basically our own tick system + private DiscoUpdater updater; - public DiscoParty(DiscoSheep parent, Player player) { - this(parent); - this.player = player; - } + public DiscoParty(DiscoSheep parent, Player player) { + this(parent); + this.player = player; + } - public DiscoParty(DiscoSheep parent) { - this.parent = parent; - this.duration = DiscoParty.defaultDuration; - this.period = DiscoParty.defaultPeriod; - this.radius = DiscoParty.defaultRadius; - this.sheep = DiscoParty.defaultSheep; - this.guestNumbers = new HashMap(DiscoParty.defaultGuestNumbers); - r = new Random(); - } + public DiscoParty(DiscoSheep parent) { + this.parent = parent; + this.duration = DiscoParty.defaultDuration; + this.period = DiscoParty.defaultPeriod; + this.radius = DiscoParty.defaultRadius; + this.sheep = DiscoParty.defaultSheep; + this.guestNumbers = new HashMap(DiscoParty.defaultGuestNumbers); + r = new Random(); + } - // copy but with new player - // used for /ds other and /ds all - public DiscoParty clone(Player player) { - DiscoParty newParty; - newParty = new DiscoParty(this.parent, player); - newParty.doFireworks = this.doFireworks; - newParty.duration = this.duration; - newParty.period = this.period; - newParty.radius = this.radius; - newParty.sheep = this.sheep; - newParty.doLightning = this.doLightning; - newParty.guestNumbers = this.getGuestNumbers(); - return newParty; - } + // copy but with new player + // used for /ds other and /ds all + public DiscoParty clone(Player player) { + DiscoParty newParty; + newParty = new DiscoParty(this.parent, player); + newParty.doFireworks = this.doFireworks; + newParty.duration = this.duration; + newParty.period = this.period; + newParty.radius = this.radius; + newParty.sheep = this.sheep; + newParty.doLightning = this.doLightning; + newParty.guestNumbers = this.getGuestNumbers(); + return newParty; + } - ArrayList getSheepList() { - return sheepList; - } + ArrayList getSheepList() { + return sheepList; + } - ArrayList getGuestList() { - return guestList; - } + ArrayList getGuestList() { + return guestList; + } - ArrayList getFloorCache() { - return this.floorBlockCache; - } + ArrayList getFloorCache() { + return this.floorBlockCache; + } - ArrayList getFloorBlocks() { - return this.floorBlocks; - } + ArrayList getFloorBlocks() { + return this.floorBlocks; + } - public static HashMap getDefaultGuestNumbers() { - return defaultGuestNumbers; - } + public static HashMap getDefaultGuestNumbers() { + return defaultGuestNumbers; + } - public HashMap getGuestNumbers() { - return guestNumbers; - } + public HashMap getGuestNumbers() { + return guestNumbers; + } - public static HashMap getMaxGuestNumbers() { - return maxGuestNumbers; - } + public static HashMap getMaxGuestNumbers() { + return maxGuestNumbers; + } - public int getSheep() { - return this.sheep; - } + public int getSheep() { + return this.sheep; + } - public DiscoParty setPlayer(Player player) { - if (player != null) { - this.player = player; - return this; - } else { - throw new NullPointerException(); - } - } + public DiscoParty setPlayer(Player player) { + if (player != null) { + this.player = player; + return this; + } else { + throw new NullPointerException(); + } + } - public DiscoParty setDuration(int duration) throws IllegalArgumentException { - if (duration <= DiscoParty.maxDuration && duration > 0) { - this.duration = duration; - return this; - } else { - throw new IllegalArgumentException(); - } - } + public DiscoParty setDuration(int duration) throws IllegalArgumentException { + if (duration <= DiscoParty.maxDuration && duration > 0) { + this.duration = duration; + return this; + } else { + throw new IllegalArgumentException(); + } + } - public DiscoParty setPeriod(int period) throws IllegalArgumentException { - if (period >= DiscoParty.minPeriod && period <= DiscoParty.maxPeriod) { - this.period = period; - return this; - } else { - throw new IllegalArgumentException(); - } - } + public DiscoParty setPeriod(int period) throws IllegalArgumentException { + if (period >= DiscoParty.minPeriod && period <= DiscoParty.maxPeriod) { + this.period = period; + return this; + } else { + throw new IllegalArgumentException(); + } + } - public DiscoParty setRadius(int radius) throws IllegalArgumentException { - if (radius <= DiscoParty.maxRadius && radius > 0) { - this.radius = radius; - return this; - } else { - throw new IllegalArgumentException(); - } - } + public DiscoParty setRadius(int radius) throws IllegalArgumentException { + if (radius <= DiscoParty.maxRadius && radius > 0) { + this.radius = radius; + return this; + } else { + throw new IllegalArgumentException(); + } + } - public DiscoParty setDenseRadius(int sheepNo) throws IllegalArgumentException { - Integer rand = (int) Math.floor(Math.sqrt(sheep / Math.PI)); - if (rand > DiscoParty.maxRadius) { - rand = DiscoParty.maxRadius; - } - if (rand < 1) { - rand = 1; - } + public DiscoParty setDenseRadius(int sheepNo) throws IllegalArgumentException { + Integer rand = (int) Math.floor(Math.sqrt(sheep / Math.PI)); + if (rand > DiscoParty.maxRadius) { + rand = DiscoParty.maxRadius; + } + if (rand < 1) { + rand = 1; + } - this.setRadius(rand); - return this; - } + this.setRadius(rand); + return this; + } - public DiscoParty setSheep(int sheep) throws IllegalArgumentException { - if (sheep <= DiscoParty.maxSheep && sheep > 0) { - this.sheep = sheep; - return this; - } else { - throw new IllegalArgumentException(); - } - } + public DiscoParty setSheep(int sheep) throws IllegalArgumentException { + if (sheep <= DiscoParty.maxSheep && sheep > 0) { + this.sheep = sheep; + return this; + } else { + throw new IllegalArgumentException(); + } + } - public DiscoParty setDoFireworks(boolean doFireworks) { - this.doFireworks = doFireworks; - return this; - } + public DiscoParty setDoFireworks(boolean doFireworks) { + this.doFireworks = doFireworks; + return this; + } - public DiscoParty setDoLightning(boolean doLightning) { - this.doLightning = doLightning; - return this; - } + public DiscoParty setDoLightning(boolean doLightning) { + this.doLightning = doLightning; + return this; + } - public DiscoParty setGuestNumber(String key, int n) throws IllegalArgumentException { - if (getMaxGuestNumbers().containsKey(key.toUpperCase())) { - if (n <= getMaxGuestNumbers().get(key.toUpperCase()) && n >= 0) { // so that /ds defaults can take 0 as arg - getGuestNumbers().put(key, n); + public DiscoParty setGuestNumber(String key, int n) throws IllegalArgumentException { + if (getMaxGuestNumbers().containsKey(key.toUpperCase())) { + if (n <= getMaxGuestNumbers().get(key.toUpperCase()) && n >= 0) { // so that /ds defaults can take 0 as arg + getGuestNumbers().put(key, n); - return this; - } - } - throw new IllegalArgumentException(); - } + return this; + } + } + throw new IllegalArgumentException(); + } - // use current settings as new defaults - public DiscoParty setDefaultsFromCurrent() { - DiscoParty.defaultDuration = this.duration; - DiscoParty.defaultPeriod = this.period; - DiscoParty.defaultRadius = this.radius; - DiscoParty.defaultSheep = this.sheep; - DiscoParty.defaultGuestNumbers = new HashMap(this.getGuestNumbers()); - return this; - } + // use current settings as new defaults + public DiscoParty setDefaultsFromCurrent() { + DiscoParty.defaultDuration = this.duration; + DiscoParty.defaultPeriod = this.period; + DiscoParty.defaultRadius = this.radius; + DiscoParty.defaultSheep = this.sheep; + DiscoParty.defaultGuestNumbers = new HashMap(this.getGuestNumbers()); + return this; + } - Location getRandomSpawnLocation(double x, double z, World world, int spawnRadius) { - Location loc; + Location getRandomSpawnLocation(double x, double z, World world, int spawnRadius) { + Location loc; - double y; + double y; - /* random point on circle with polar coordinates - * random number must be square rooted to obtain uniform distribution - * otherwise the sheep are biased toward the centre */ - double rand = Math.sqrt(r.nextDouble()) * spawnRadius; - double azimuth = r.nextDouble() * 2 * Math.PI; // radians - x += rand * Math.cos(azimuth); - z += rand * Math.sin(azimuth); - y = this.player.getLocation().getY(); + /* random point on circle with polar coordinates + * random number must be square rooted to obtain uniform distribution + * otherwise the sheep are biased toward the centre */ + double rand = Math.sqrt(r.nextDouble()) * spawnRadius; + double azimuth = r.nextDouble() * 2 * Math.PI; // radians + x += rand * Math.cos(azimuth); + z += rand * Math.sin(azimuth); + y = this.player.getLocation().getY(); - loc = new Location(world, x, y, z); - loc.setPitch(r.nextFloat() * 360 - 180); - loc.setYaw(0); + loc = new Location(world, x, y, z); + loc.setPitch(r.nextFloat() * 360 - 180); + loc.setYaw(0); - return loc; - } + return loc; + } - // Spawn some number of guests next to given player - void spawnAll(int sheep, int spawnRadius) { - Location loc; - World world = player.getWorld(); + // Spawn some number of guests next to given player + void spawnAll(int sheep, int spawnRadius) { + Location loc; + World world = player.getWorld(); + double x = player.getLocation().getX(); + double z = player.getLocation().getZ(); + for (int i = 0; i < sheep; i++) { + loc = getRandomSpawnLocation(x, z, world, spawnRadius); + spawnSheep(world, loc); + } - double x = player.getLocation().getX(); - double z = player.getLocation().getZ(); - for (int i = 0; i < sheep; i++) { - loc = getRandomSpawnLocation(x, z, world, spawnRadius); - spawnSheep(world, loc); - } + // loop through hashmap of other guests and spawn accordingly + for (Map.Entry entry : guestNumbers.entrySet()) { + EntityType ent = EntityType.valueOf((String) entry.getKey()); + int num = (Integer) entry.getValue(); - // loop through hashmap of other guests and spawn accordingly - for (Map.Entry entry : guestNumbers.entrySet()) { - EntityType ent = EntityType.valueOf((String) entry.getKey()); - int num = (Integer) entry.getValue(); + for (int i = 0; i < num; i++) { + loc = getRandomSpawnLocation(x, z, world, spawnRadius); + spawnGuest(world, loc, ent); + } + } - for (int i = 0; i < num; i++) { - loc = getRandomSpawnLocation(x, z, world, spawnRadius); - spawnGuest(world, loc, ent); - } - } + loc = player.getLocation(); + //this.spawnFloor(world, new Location(world, loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ())); + } - loc = player.getLocation(); - //this.spawnFloor(world, new Location(world, loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ())); - } + void spawnSheep(World world, Location loc) { + Sheep newSheep = (Sheep) world.spawnEntity(loc, EntityType.SHEEP); + newSheep.setColor(discoColours[(r.nextInt(discoColours.length))]); + newSheep.setBreed(false); // this prevents breeding - no event listener required + newSheep.teleport(loc); // teleport is needed to set orientation + getSheepList().add(newSheep); + if (doLightning) { + world.strikeLightningEffect(loc); + } + } - void spawnSheep(World world, Location loc) { - Sheep newSheep = (Sheep) world.spawnEntity(loc, EntityType.SHEEP); - newSheep.setColor(discoColours[(r.nextInt(discoColours.length))]); - newSheep.setBreed(false); // this prevents breeding - no event listener required - newSheep.teleport(loc); // teleport is needed to set orientation - getSheepList().add(newSheep); - if (doLightning) { - world.strikeLightningEffect(loc); - } - } + void spawnGuest(World world, Location loc, EntityType type) { + Entity newGuest = loc.getWorld().spawnEntity(loc, type); + getGuestList().add(newGuest); + if (doLightning) { + world.strikeLightningEffect(loc); + } + } - void spawnGuest(World world, Location loc, EntityType type) { - Entity newGuest = loc.getWorld().spawnEntity(loc, type); - getGuestList().add(newGuest); - if (doLightning) { - world.strikeLightningEffect(loc); - } - } + void spawnFloor(World world, Location loc) { + // First we'll save the floor state + for (int x = loc.getBlockX() - this.radius; x < loc.getX() + this.radius; ++x) { + for (int z = loc.getBlockZ() - this.radius; z < loc.getZ() + this.radius; ++z) { + Block block = world.getBlockAt(x, loc.getBlockY(), z); + if (block.getType() != Material.WOOL) { + this.getFloorCache().add(block.getState()); + block.setType(Material.WOOL); + this.getFloorBlocks().add(block); + } + } + } + } - void spawnFloor(World world, Location loc) { - // First we'll save the floor state - for (int x = loc.getBlockX() - this.radius; x < loc.getX() + this.radius; ++x) { - for (int z = loc.getBlockZ() - this.radius; z < loc.getZ() + this.radius; ++z) { - Block block = world.getBlockAt(x, loc.getBlockY(), z); - if (block.getType() != Material.WOOL) { - this.getFloorCache().add(block.getState()); - block.setType(Material.WOOL); - this.getFloorBlocks().add(block); - } - } - } - } + // Mark all guests for removal, then clear the array + void removeAll() { + for (Sheep sheeple : getSheepList()) { + sheeple.remove(); + } + for (Entity guest : getGuestList()) { + guest.remove(); + } + for (BlockState block : this.floorBlockCache) { + block.update(true); + } + getSheepList().clear(); + getGuestList().clear(); + floorBlockCache.clear(); + } - // Mark all guests for removal, then clear the array - void removeAll() { - for (Sheep sheeple : getSheepList()) { - sheeple.remove(); - } - for (Entity guest : getGuestList()) { - guest.remove(); - } - for (BlockState block : this.floorBlockCache) { - block.update(true); - } - getSheepList().clear(); - getGuestList().clear(); - floorBlockCache.clear(); - } + // Set a random colour for all sheep in array + void randomizeSheepColour(Sheep sheep) { + sheep.setColor(discoColours[(r.nextInt(discoColours.length))]); + } - // Set a random colour for all sheep in array - void randomizeSheepColour(Sheep sheep) { - sheep.setColor(discoColours[(r.nextInt(discoColours.length))]); - } + void randomizeFloor(Block block) { + block.setType(Material.WOOL); + block.setData(discoColours[(r.nextInt(discoColours.length))].getData()); + } - void randomizeFloor(Block block) { - block.setType(Material.WOOL); - block.setData(discoColours[(r.nextInt(discoColours.length))].getData()); - } + void jump(Entity entity) { + Vector orgVel = entity.getVelocity(); + Vector newVel = (new Vector()).copy(orgVel); + newVel.add(new Vector(0, defaultSheepJump, 0)); + entity.setVelocity(newVel); + } - void jump(Entity entity) { - Vector orgVel = entity.getVelocity(); - Vector newVel = (new Vector()).copy(orgVel); - newVel.add(new Vector(0, defaultSheepJump, 0)); - entity.setVelocity(newVel); - } + // WHY ISN'T THERE A Color.getValue() ?!?!?!?! + private Color getColor(int i) { + Color c = null; + if (i == 1) { + c = Color.AQUA; + } + if (i == 2) { + c = Color.BLACK; + } + if (i == 3) { + c = Color.BLUE; + } + if (i == 4) { + c = Color.FUCHSIA; + } + if (i == 5) { + c = Color.GRAY; + } + if (i == 6) { + c = Color.GREEN; + } + if (i == 7) { + c = Color.LIME; + } + if (i == 8) { + c = Color.MAROON; + } + if (i == 9) { + c = Color.NAVY; + } + if (i == 10) { + c = Color.OLIVE; + } + if (i == 11) { + c = Color.ORANGE; + } + if (i == 12) { + c = Color.PURPLE; + } + if (i == 13) { + c = Color.RED; + } + if (i == 14) { + c = Color.SILVER; + } + if (i == 15) { + c = Color.TEAL; + } + if (i == 16) { + c = Color.WHITE; + } + if (i == 17) { + c = Color.YELLOW; + } - // WHY ISN'T THERE A Color.getValue() ?!?!?!?! - private Color getColor(int i) { - Color c = null; - if (i == 1) { - c = Color.AQUA; - } - if (i == 2) { - c = Color.BLACK; - } - if (i == 3) { - c = Color.BLUE; - } - if (i == 4) { - c = Color.FUCHSIA; - } - if (i == 5) { - c = Color.GRAY; - } - if (i == 6) { - c = Color.GREEN; - } - if (i == 7) { - c = Color.LIME; - } - if (i == 8) { - c = Color.MAROON; - } - if (i == 9) { - c = Color.NAVY; - } - if (i == 10) { - c = Color.OLIVE; - } - if (i == 11) { - c = Color.ORANGE; - } - if (i == 12) { - c = Color.PURPLE; - } - if (i == 13) { - c = Color.RED; - } - if (i == 14) { - c = Color.SILVER; - } - if (i == 15) { - c = Color.TEAL; - } - if (i == 16) { - c = Color.WHITE; - } - if (i == 17) { - c = Color.YELLOW; - } + return c; + } - return c; - } + void updateAll() { + for (Sheep sheeple : getSheepList()) { + randomizeSheepColour(sheeple); - void updateAll() { - for (Sheep sheeple : getSheepList()) { - randomizeSheepColour(sheeple); + if (doFireworks && state % 8 == 0) { + if (r.nextDouble() < 0.50) { + spawnRandomFireworkAtSheep(sheeple); + } + } - if (doFireworks && state % 8 == 0) { - if (r.nextDouble() < 0.50) { - spawnRandomFireworkAtSheep(sheeple); - } - } + if (doJump) { + if (state % 2 == 0 && r.nextDouble() < 0.5) { + jump(sheeple); + } + } + } - if (doJump) { - if (state % 2 == 0 && r.nextDouble() < 0.5) { - jump(sheeple); - } - } - } + for (Entity guest : getGuestList()) { + if (doJump) { + if (state % 2 == 0 && r.nextDouble() < 0.5) { + jump(guest); + } + } + } - for (Entity guest : getGuestList()) { - if (doJump) { - if (state % 2 == 0 && r.nextDouble() < 0.5) { - jump(guest); - } - } - } + /* for (Block block : this.floorBlocks) { + this.randomizeFloor(block); + }*/ + } - /* for (Block block : this.floorBlocks) { - this.randomizeFloor(block); - }*/ + private float getPentatonicNote() { + return DiscoParty.pentatonicNotes[r.nextInt(pentatonicNotes.length)]; + } - } + void playSounds() { + player.playSound(player.getLocation(), Sound.NOTE_BASS_DRUM, 0.75f, 1.0f); + if (this.state % 2 == 0) { + player.playSound(player.getLocation(), Sound.NOTE_SNARE_DRUM, 0.8f, 1.0f); + } - private float getPentatonicNote() { - return DiscoParty.pentatonicNotes[r.nextInt(pentatonicNotes.length)]; - } + if ((this.state + 1) % 8 == 0) { + player.playSound(player.getLocation(), Sound.NOTE_STICKS, 1.0f, 1.0f); + } + } - void playSounds() { - player.playSound(player.getLocation(), Sound.NOTE_BASS_DRUM, 0.75f, 1.0f); - if (this.state % 2 == 0) { - player.playSound(player.getLocation(), Sound.NOTE_SNARE_DRUM, 0.8f, 1.0f); - } + void randomizeFirework(Firework firework) { + Builder effect = FireworkEffect.builder(); + FireworkMeta meta = firework.getFireworkMeta(); - if ((this.state + 1) % 8 == 0) { - player.playSound(player.getLocation(), Sound.NOTE_STICKS, 1.0f, 1.0f); - } - } + // construct [1, 3] random colours + int numColours = r.nextInt(3) + 1; + Color[] colourArray = new Color[numColours]; + for (int i = 0; i < numColours; i++) { + colourArray[i] = getColor(r.nextInt(17) + 1); + } - void randomizeFirework(Firework firework) { - Builder effect = FireworkEffect.builder(); - FireworkMeta meta = firework.getFireworkMeta(); + // randomize effects + effect.withColor(colourArray); + effect.flicker(r.nextDouble() < 0.5); + effect.trail(r.nextDouble() < 0.5); + effect.with(FireworkEffect.Type.values()[r.nextInt(FireworkEffect.Type.values().length)]); - // construct [1, 3] random colours - int numColours = r.nextInt(3) + 1; - Color[] colourArray = new Color[numColours]; - for (int i = 0; i < numColours; i++) { - colourArray[i] = getColor(r.nextInt(17) + 1); - } + // set random effect and randomize power + meta.addEffect(effect.build()); + meta.setPower(r.nextInt(2) + 1); - // randomize effects - effect.withColor(colourArray); - effect.flicker(r.nextDouble() < 0.5); - effect.trail(r.nextDouble() < 0.5); - effect.with(FireworkEffect.Type.values()[r.nextInt(FireworkEffect.Type.values().length)]); + // apply it to the given firework + firework.setFireworkMeta(meta); + } - // set random effect and randomize power - meta.addEffect(effect.build()); - meta.setPower(r.nextInt(2) + 1); + void spawnRandomFireworkAtSheep(Sheep sheep) { + Firework firework = (Firework) sheep.getWorld().spawnEntity(sheep.getEyeLocation(), EntityType.FIREWORK); + randomizeFirework(firework); + } - // apply it to the given firework - firework.setFireworkMeta(meta); - } + void update() { + if (duration > 0) { + updateAll(); + playSounds(); + duration -= period; + this.scheduleUpdate(); + this.state = (this.state + 1) % 10000; + } else { + this.stopDisco(); + } + } - void spawnRandomFireworkAtSheep(Sheep sheep) { - Firework firework = (Firework) sheep.getWorld().spawnEntity(sheep.getEyeLocation(), EntityType.FIREWORK); - randomizeFirework(firework); - } + void scheduleUpdate() { + updater = new DiscoUpdater(); + updater.runTaskLater(parent, this.period); + } - void update() { - if (duration > 0) { - updateAll(); - playSounds(); - duration -= period; - this.scheduleUpdate(); - this.state = (this.state + 1) % 10000; - } else { - this.stopDisco(); - } - } + void startDisco() { + this.spawnAll(sheep, radius); + this.scheduleUpdate(); + parent.getPartyMap().put(this.player.getName(), this); + // start listening + this.partyEvents = new PartyEvents(this.parent, this); + parent.getServer().getPluginManager().registerEvents(this.partyEvents, this.parent); + } - void scheduleUpdate() { - updater = new DiscoUpdater(); - updater.runTaskLater(parent, this.period); - } + void stopDisco() { + removeAll(); + this.duration = 0; + if (updater != null) { + updater.cancel(); + } + updater = null; + parent.getPartyMap().remove(this.player.getName()); + // stop listening + HandlerList.unregisterAll(this.partyEvents); + } - void startDisco() { - this.spawnAll(sheep, radius); - this.scheduleUpdate(); - parent.getPartyMap().put(this.player.getName(), this); - // start listening - this.partyEvents = new PartyEvents(this.parent, this); - parent.getServer().getPluginManager().registerEvents(this.partyEvents, this.parent); - } + class DiscoUpdater extends BukkitRunnable { - void stopDisco() { - removeAll(); - this.duration = 0; - if (updater != null) { - updater.cancel(); - } - updater = null; - parent.getPartyMap().remove(this.player.getName()); - // stop listening - HandlerList.unregisterAll(this.partyEvents); - } - - class DiscoUpdater extends BukkitRunnable { - - @Override - public void run() { - update(); - } - } + @Override + public void run() { + update(); + } + } } diff --git a/src/main/java/ca/gibstick/discosheep/DiscoSheep.java b/src/main/java/ca/gibstick/discosheep/DiscoSheep.java index 9a7389f..1b87cb0 100644 --- a/src/main/java/ca/gibstick/discosheep/DiscoSheep.java +++ b/src/main/java/ca/gibstick/discosheep/DiscoSheep.java @@ -12,302 +12,300 @@ 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"; - static boolean partyOnJoin = false; - Map parties = new HashMap(); + 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 boolean partyOnJoin = false; + Map parties = new HashMap(); - @Override - public void onEnable() { - getCommand("ds").setExecutor(new DiscoSheepCommandExecutor(this)); - getServer().getPluginManager().registerEvents(new GlobalEvents(this), this); + @Override + public void onEnable() { + getCommand("ds").setExecutor(new DiscoSheepCommandExecutor(this)); + getServer().getPluginManager().registerEvents(new GlobalEvents(this), this); - getConfig().addDefault("on-join.enabled", partyOnJoin); - getConfig().addDefault("max.sheep", DiscoParty.maxSheep); - getConfig().addDefault("max.radius", DiscoParty.maxRadius); - getConfig().addDefault("max.duration", toSeconds_i(DiscoParty.maxDuration)); - getConfig().addDefault("max.period-ticks", DiscoParty.maxPeriod); - getConfig().addDefault("min.period-ticks", DiscoParty.minPeriod); - getConfig().addDefault("default.sheep", DiscoParty.defaultSheep); - getConfig().addDefault("default.radius", DiscoParty.defaultRadius); - getConfig().addDefault("default.duration", toSeconds_i(DiscoParty.defaultDuration)); - getConfig().addDefault("default.period-ticks", DiscoParty.defaultPeriod); - - /* - * Iterate through all live entities and create default configuration values for them - * excludes bosses and other mobs that throw NPE - */ - /* for (EntityType ent : EntityType.values()) { - if (ent.isAlive() - && !ent.equals(EntityType.ENDER_DRAGON) - && !ent.equals(EntityType.WITHER) - && !ent.equals(EntityType.PIG_ZOMBIE) - && !ent.equals(EntityType.OCELOT) - && !ent.equals(EntityType.CAVE_SPIDER) - && !ent.equals(EntityType.MAGMA_CUBE) - && !ent.equals(EntityType.MUSHROOM_COW) - && !ent.equals(EntityType.IRON_GOLEM) - && !ent.equals(EntityType.PLAYER)) { - getConfig().addDefault("default.guests." + ent.toString(), 0); - getConfig().addDefault("max.guests." + ent.toString(), 0); - } - }*/ - loadConfigFromDisk(); - } + getConfig().addDefault("on-join.enabled", partyOnJoin); + getConfig().addDefault("max.sheep", DiscoParty.maxSheep); + getConfig().addDefault("max.radius", DiscoParty.maxRadius); + getConfig().addDefault("max.duration", toSeconds_i(DiscoParty.maxDuration)); + getConfig().addDefault("max.period-ticks", DiscoParty.maxPeriod); + getConfig().addDefault("min.period-ticks", DiscoParty.minPeriod); + getConfig().addDefault("default.sheep", DiscoParty.defaultSheep); + getConfig().addDefault("default.radius", DiscoParty.defaultRadius); + getConfig().addDefault("default.duration", toSeconds_i(DiscoParty.defaultDuration)); + getConfig().addDefault("default.period-ticks", DiscoParty.defaultPeriod); - void loadConfigFromDisk() { - getConfig().options().copyDefaults(true); - saveConfig(); + /* + * Iterate through all live entities and create default configuration values for them + * excludes bosses and other mobs that throw NPE + */ + /* for (EntityType ent : EntityType.values()) { + if (ent.isAlive() + && !ent.equals(EntityType.ENDER_DRAGON) + && !ent.equals(EntityType.WITHER) + && !ent.equals(EntityType.PIG_ZOMBIE) + && !ent.equals(EntityType.OCELOT) + && !ent.equals(EntityType.CAVE_SPIDER) + && !ent.equals(EntityType.MAGMA_CUBE) + && !ent.equals(EntityType.MUSHROOM_COW) + && !ent.equals(EntityType.IRON_GOLEM) + && !ent.equals(EntityType.PLAYER)) { + getConfig().addDefault("default.guests." + ent.toString(), 0); + getConfig().addDefault("max.guests." + ent.toString(), 0); + } + }*/ + loadConfigFromDisk(); + } - partyOnJoin = getConfig().getBoolean("on-join.enabled"); - DiscoParty.maxSheep = getConfig().getInt("max.sheep"); - DiscoParty.maxRadius = getConfig().getInt("max.radius"); - DiscoParty.maxDuration = toTicks(getConfig().getInt("max.duration")); - DiscoParty.maxPeriod = getConfig().getInt("max.period-ticks"); - DiscoParty.minPeriod = getConfig().getInt("min.period-ticks"); - DiscoParty.defaultSheep = getConfig().getInt("default.sheep"); - DiscoParty.defaultRadius = getConfig().getInt("default.radius"); - DiscoParty.defaultDuration = toTicks(getConfig().getInt("default.duration")); - DiscoParty.defaultPeriod = getConfig().getInt("default.period-ticks"); + void loadConfigFromDisk() { + getConfig().options().copyDefaults(true); + saveConfig(); - /* for (String key : getConfig().getConfigurationSection("default.guests").getKeys(false)) { - DiscoParty.getDefaultGuestNumbers().put(key, getConfig().getInt("default.guests." + key)); - } + partyOnJoin = getConfig().getBoolean("on-join.enabled"); + DiscoParty.maxSheep = getConfig().getInt("max.sheep"); + DiscoParty.maxRadius = getConfig().getInt("max.radius"); + DiscoParty.maxDuration = toTicks(getConfig().getInt("max.duration")); + DiscoParty.maxPeriod = getConfig().getInt("max.period-ticks"); + DiscoParty.minPeriod = getConfig().getInt("min.period-ticks"); + DiscoParty.defaultSheep = getConfig().getInt("default.sheep"); + DiscoParty.defaultRadius = getConfig().getInt("default.radius"); + DiscoParty.defaultDuration = toTicks(getConfig().getInt("default.duration")); + DiscoParty.defaultPeriod = getConfig().getInt("default.period-ticks"); + + /* for (String key : getConfig().getConfigurationSection("default.guests").getKeys(false)) { + DiscoParty.getDefaultGuestNumbers().put(key, getConfig().getInt("default.guests." + key)); + } - for (String key : getConfig().getConfigurationSection("max.guests").getKeys(false)) { - DiscoParty.getMaxGuestNumbers().put(key, getConfig().getInt("max.guests." + key)); - }*/ + for (String key : getConfig().getConfigurationSection("max.guests").getKeys(false)) { + DiscoParty.getMaxGuestNumbers().put(key, getConfig().getInt("max.guests." + key)); + }*/ + } - } + void reloadConfigFromDisk() { + reloadConfig(); + loadConfigFromDisk(); + } - void reloadConfigFromDisk() { - reloadConfig(); - loadConfigFromDisk(); - } + void saveConfigToDisk() { + getConfig().set("on-join.enabled", partyOnJoin); + getConfig().set("default.sheep", DiscoParty.defaultSheep); + getConfig().set("default.radius", DiscoParty.defaultRadius); + getConfig().set("default.duration", toSeconds_i(DiscoParty.defaultDuration)); + getConfig().set("default.period-ticks", DiscoParty.defaultPeriod); - void saveConfigToDisk() { - getConfig().set("on-join.enabled", partyOnJoin); - getConfig().set("default.sheep", DiscoParty.defaultSheep); - getConfig().set("default.radius", DiscoParty.defaultRadius); - getConfig().set("default.duration", toSeconds_i(DiscoParty.defaultDuration)); - getConfig().set("default.period-ticks", DiscoParty.defaultPeriod); + /* for (Map.Entry entry : DiscoParty.getDefaultGuestNumbers().entrySet()) { + getConfig().set("default.guests." + entry.getKey(), entry.getValue()); + }*/ + saveConfig(); + } - /* for (Map.Entry entry : DiscoParty.getDefaultGuestNumbers().entrySet()) { - getConfig().set("default.guests." + entry.getKey(), entry.getValue()); - }*/ + @Override + public void onDisable() { + this.stopAllParties(); // or else the parties will continue FOREVER + } - saveConfig(); - } + static int toTicks(double seconds) { + return (int) Math.round(seconds * 20.0); + } - @Override - public void onDisable() { - this.stopAllParties(); // or else the parties will continue FOREVER - } + static double toSeconds(int ticks) { + return (double) Math.round(ticks / 20.0); + } - static int toTicks(double seconds) { - return (int) Math.round(seconds * 20.0); - } + static int toSeconds_i(int ticks) { + return (int) Math.round(ticks / 20.0); + } - static double toSeconds(int ticks) { - return (double) Math.round(ticks / 20.0); - } + public synchronized Map getPartyMap() { + return this.parties; + } - static int toSeconds_i(int ticks) { - return (int) Math.round(ticks / 20.0); - } + public synchronized ArrayList getParties() { + return new ArrayList(this.getPartyMap().values()); + } - public synchronized Map getPartyMap() { - return this.parties; - } + public void stopParty(String name) { + if (this.hasParty(name)) { + this.getParty(name).stopDisco(); + } + } - public synchronized ArrayList getParties() { - return new ArrayList(this.getPartyMap().values()); - } + public void stopAllParties() { + for (DiscoParty party : this.getParties()) { + party.stopDisco(); + } + } - public void stopParty(String name) { - if (this.hasParty(name)) { - this.getParty(name).stopDisco(); - } - } + public boolean hasParty(String name) { + return this.getPartyMap().containsKey(name); + } - public void stopAllParties() { - for (DiscoParty party : this.getParties()) { - party.stopDisco(); - } - } + public DiscoParty getParty(String name) { + return this.getPartyMap().get(name); + } - public boolean hasParty(String name) { - return this.getPartyMap().containsKey(name); - } + public void removeParty(String name) { + if (this.hasParty(name)) { + this.getPartyMap().remove(name); + } + } - public DiscoParty getParty(String name) { - return this.getPartyMap().get(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, save, reload, togglejoin\n" + + "other : 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 : set the number of sheep per player that spawn\n" + + "-t : set the party duration in seconds\n" + + "-p : set the number of ticks between each disco beat\n" + + "-r : set radius of the area in which sheep can spawn\n" + //+ "-g : set spawns for other mobs\n" + + "-l: enables lightning\n" + + "-fw: enables fireworks"); + return true; + } - public void removeParty(String name) { - if (this.hasParty(name)) { - this.getPartyMap().remove(name); - } - } + boolean stopMeCommand(CommandSender sender) { + stopParty(sender.getName()); + return true; + } - /*-- 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 : 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 : set the number of sheep per player that spawn\n" - + "-t : set the party duration in seconds\n" - + "-p : set the number of ticks between each disco beat\n" - + "-r : set radius of the area in which sheep can spawn\n" - //+ "-g : set spawns for other mobs\n" - + "-l: enables lightning\n" - + "-fw: enables fireworks"); - return true; - } + boolean stopAllCommand(CommandSender sender) { + if (sender.hasPermission(PERMISSION_STOPALL)) { + stopAllParties(); + return true; + } else { + return noPermsMessage(sender, PERMISSION_STOPALL); + } + } - boolean stopMeCommand(CommandSender sender) { - stopParty(sender.getName()); - return true; - } + 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 stopAllCommand(CommandSender sender) { - if (sender.hasPermission(PERMISSION_STOPALL)) { - stopAllParties(); - return true; - } else { - return noPermsMessage(sender, PERMISSION_STOPALL); - } - } + 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 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); - } - } + @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 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); - } - } + 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); - } - } + void partyOnJoin(Player player) { + if (!partyOnJoin) { + return; + } + if (player.hasPermission(PERMISSION_ONJOIN)) { + DiscoParty party = new DiscoParty(this, player); + party.startDisco(); + } + } - void partyOnJoin(Player player) { - if (!partyOnJoin) { - return; - } - if (player.hasPermission(PERMISSION_ONJOIN)) { - DiscoParty party = new DiscoParty(this, 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 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 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 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; + } - boolean clearGuests(DiscoParty party) { - party.getGuestNumbers().clear(); - return true; - } - - boolean noPermsMessage(CommandSender sender, String permission) { - sender.sendMessage(ChatColor.RED + "You do not have the permission node " + ChatColor.GRAY + permission); - return false; - } + boolean noPermsMessage(CommandSender sender, String permission) { + sender.sendMessage(ChatColor.RED + "You do not have the permission node " + ChatColor.GRAY + permission); + return false; + } } diff --git a/src/main/java/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java b/src/main/java/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java index f096ea0..35566ec 100644 --- a/src/main/java/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java +++ b/src/main/java/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java @@ -9,144 +9,144 @@ import org.bukkit.entity.Player; public class DiscoSheepCommandExecutor implements CommandExecutor { - private final DiscoSheep parent; + private final DiscoSheep parent; - public DiscoSheepCommandExecutor(DiscoSheep parent) { - this.parent = 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 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 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 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 - } + 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); - } + // 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) { + @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 + 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 + 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); - } - } + // 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); + // 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; - } + // 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); @@ -167,33 +167,33 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { // } // 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); - } - } + } 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 (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; - } - } + 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; - } + return false; + } } diff --git a/src/main/java/ca/gibstick/discosheep/GlobalEvents.java b/src/main/java/ca/gibstick/discosheep/GlobalEvents.java index 74ee35b..9445654 100644 --- a/src/main/java/ca/gibstick/discosheep/GlobalEvents.java +++ b/src/main/java/ca/gibstick/discosheep/GlobalEvents.java @@ -14,22 +14,22 @@ import org.bukkit.event.player.PlayerQuitEvent; public class GlobalEvents implements Listener { - DiscoSheep parent; + DiscoSheep parent; - public GlobalEvents(DiscoSheep parent) { - this.parent = parent; - } + public GlobalEvents(DiscoSheep parent) { + this.parent = parent; + } - @EventHandler (priority = EventPriority.MONITOR) - public void onPlayerQuitEvent(PlayerQuitEvent e) { - String name = e.getPlayer().getName(); - parent.stopParty(name); - // stop party on player quit or else it will CONTINUE FOR ETERNITY - } + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerQuitEvent(PlayerQuitEvent e) { + String name = e.getPlayer().getName(); + parent.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); - } + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerJoinEvent(PlayerJoinEvent e) { + Player player = e.getPlayer(); + parent.partyOnJoin(player); + } } diff --git a/src/main/java/ca/gibstick/discosheep/PartyEvents.java b/src/main/java/ca/gibstick/discosheep/PartyEvents.java index 1de12b8..2848410 100644 --- a/src/main/java/ca/gibstick/discosheep/PartyEvents.java +++ b/src/main/java/ca/gibstick/discosheep/PartyEvents.java @@ -14,58 +14,58 @@ import org.bukkit.event.player.PlayerShearEntityEvent; */ public class PartyEvents implements Listener { - DiscoSheep parent; - DiscoParty party; - /* - * There will be multiple instances of PartyEvents, - * and each instance will only listen for its own party. - * That way, we don't have multiple instances iterating through - * the entire parties hashmap redundantly, yet we can still - * unregister the listeners when no parties are running. - */ + DiscoSheep parent; + DiscoParty party; + /* + * There will be multiple instances of PartyEvents, + * and each instance will only listen for its own party. + * That way, we don't have multiple instances iterating through + * the entire parties hashmap redundantly, yet we can still + * unregister the listeners when no parties are running. + */ - public PartyEvents(DiscoSheep parent, DiscoParty party) { - this.parent = parent; - this.party = party; - } + public PartyEvents(DiscoSheep parent, DiscoParty party) { + this.parent = parent; + this.party = party; + } - // prevent sheep shearing - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onPlayerShear(PlayerShearEntityEvent e) { - if (e.getEntity() instanceof Sheep) { + // prevent sheep shearing + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPlayerShear(PlayerShearEntityEvent e) { + if (e.getEntity() instanceof Sheep) { - if (party.getSheepList().contains((Sheep) e.getEntity())) { - e.setCancelled(true); - } + if (party.getSheepList().contains((Sheep) e.getEntity())) { + e.setCancelled(true); + } - } - } + } + } - // actually make sheep and other guests invincible - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onLivingEntityDamageEvent(EntityDamageEvent e) { - if (e.getEntity() instanceof Sheep) { - if (party.getSheepList().contains((Sheep) e.getEntity())) { - { - party.jump(e.getEntity()); // for kicks - e.setCancelled(true); - } - } + // actually make sheep and other guests invincible + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onLivingEntityDamageEvent(EntityDamageEvent e) { + if (e.getEntity() instanceof Sheep) { + if (party.getSheepList().contains((Sheep) e.getEntity())) { + { + party.jump(e.getEntity()); // for kicks + e.setCancelled(true); + } + } - } - if (party.getGuestList().contains(e.getEntity())) { - party.jump(e.getEntity()); - e.setCancelled(true); - } - } + } + if (party.getGuestList().contains(e.getEntity())) { + party.jump(e.getEntity()); + e.setCancelled(true); + } + } - // prevent uninvited guests from targetting players - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void onEntityTargetLivingEntityEvent(EntityTargetEvent e) { + // prevent uninvited guests from targetting players + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEntityTargetLivingEntityEvent(EntityTargetEvent e) { - if (party.getGuestList().contains(e.getEntity())) { - e.setCancelled(true); - } + if (party.getGuestList().contains(e.getEntity())) { + e.setCancelled(true); + } - } + } }