diff --git a/src/ca/gibstick/discosheep/DiscoParty.java b/src/ca/gibstick/discosheep/DiscoParty.java index eb9a647..7c02df5 100644 --- a/src/ca/gibstick/discosheep/DiscoParty.java +++ b/src/ca/gibstick/discosheep/DiscoParty.java @@ -79,12 +79,12 @@ public class DiscoParty { 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) { this.parent = parent; this.duration = DiscoParty.defaultDuration; @@ -109,39 +109,39 @@ public class DiscoParty { newParty.guestNumbers = this.getGuestNumbers(); return newParty; } - + ArrayList getSheepList() { return sheepList; } - + ArrayList getGuestList() { return guestList; } - + ArrayList getFloorCache() { return this.floorBlockCache; } - + ArrayList getFloorBlocks() { return this.floorBlocks; } - + public static HashMap getDefaultGuestNumbers() { return defaultGuestNumbers; } - + public HashMap getGuestNumbers() { return guestNumbers; } - + public static HashMap getMaxGuestNumbers() { return maxGuestNumbers; } - + public int getSheep() { return this.sheep; } - + public DiscoParty setPlayer(Player player) { if (player != null) { this.player = player; @@ -150,7 +150,7 @@ public class DiscoParty { throw new NullPointerException(); } } - + public DiscoParty setDuration(int duration) throws IllegalArgumentException { if (duration <= DiscoParty.maxDuration && duration > 0) { this.duration = duration; @@ -159,7 +159,7 @@ public class DiscoParty { throw new IllegalArgumentException(); } } - + public DiscoParty setPeriod(int period) throws IllegalArgumentException { if (period >= DiscoParty.minPeriod && period <= DiscoParty.maxPeriod) { this.period = period; @@ -168,7 +168,7 @@ public class DiscoParty { throw new IllegalArgumentException(); } } - + public DiscoParty setRadius(int radius) throws IllegalArgumentException { if (radius <= DiscoParty.maxRadius && radius > 0) { this.radius = radius; @@ -177,7 +177,7 @@ public class DiscoParty { throw new IllegalArgumentException(); } } - + public DiscoParty setDenseRadius(int sheepNo) throws IllegalArgumentException { Integer rand = (int) Math.floor(Math.sqrt(sheep / Math.PI)); if (rand > DiscoParty.maxRadius) { @@ -186,11 +186,11 @@ public class DiscoParty { if (rand < 1) { rand = 1; } - + this.setRadius(rand); return this; } - + public DiscoParty setSheep(int sheep) throws IllegalArgumentException { if (sheep <= DiscoParty.maxSheep && sheep > 0) { this.sheep = sheep; @@ -199,22 +199,22 @@ public class DiscoParty { throw new IllegalArgumentException(); } } - + public DiscoParty setDoFireworks(boolean doFireworks) { this.doFireworks = doFireworks; 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); - + return this; } } @@ -230,10 +230,10 @@ public class DiscoParty { DiscoParty.defaultGuestNumbers = new HashMap(this.getGuestNumbers()); return this; } - + Location getRandomSpawnLocation(double x, double z, World world, int spawnRadius) { Location loc; - + double y; /* random point on circle with polar coordinates @@ -244,11 +244,11 @@ public class DiscoParty { 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); - + return loc; } @@ -256,8 +256,8 @@ public class DiscoParty { 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++) { @@ -269,17 +269,17 @@ public class DiscoParty { 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); } } - + loc = player.getLocation(); - this.spawnFloor(world, new Location(world,loc.getBlockX(),loc.getBlockY()-1,loc.getBlockZ())); + 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))]); @@ -290,7 +290,7 @@ public class DiscoParty { world.strikeLightningEffect(loc); } } - + void spawnGuest(World world, Location loc, EntityType type) { Entity newGuest = loc.getWorld().spawnEntity(loc, type); getGuestList().add(newGuest); @@ -298,15 +298,17 @@ public class DiscoParty { 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); - this.getFloorCache().add(block.getState()); - block.setType(Material.WOOL); - this.getFloorBlocks().add(block); + if (block.getType() != Material.WOOL) { + this.getFloorCache().add(block.getState()); + block.setType(Material.WOOL); + this.getFloorBlocks().add(block); + } } } } @@ -331,12 +333,12 @@ public class DiscoParty { void randomizeSheepColour(Sheep sheep) { sheep.setColor(discoColours[(r.nextInt(discoColours.length))]); } - + void randomizeFloorColour(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); @@ -398,27 +400,27 @@ public class DiscoParty { if (i == 17) { c = Color.YELLOW; } - + return c; } - + void updateAll() { for (Sheep sheeple : getSheepList()) { randomizeSheepColour(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); } } } - + for (Entity guest : getGuestList()) { if (doJump) { if (state % 2 == 0 && r.nextDouble() < 0.5) { @@ -426,28 +428,28 @@ public class DiscoParty { } } } - + for (Block block : this.floorBlocks) { this.randomizeFloorColour(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); } - + if ((this.state + 1) % 8 == 0) { player.playSound(player.getLocation(), Sound.NOTE_STICKS, 1.0f, 1.0f); } } - + void randomizeFirework(Firework firework) { Random r = new Random(); Builder effect = FireworkEffect.builder(); @@ -473,12 +475,12 @@ public class DiscoParty { // apply it to the given firework firework.setFireworkMeta(meta); } - + void spawnRandomFireworkAtSheep(Sheep sheep) { Firework firework = (Firework) sheep.getWorld().spawnEntity(sheep.getEyeLocation(), EntityType.FIREWORK); randomizeFirework(firework); } - + void update() { if (duration > 0) { updateAll(); @@ -490,12 +492,12 @@ public class DiscoParty { this.stopDisco(); } } - + void scheduleUpdate() { updater = new DiscoUpdater(); updater.runTaskLater(parent, this.period); } - + void startDisco() { this.spawnAll(sheep, radius); this.scheduleUpdate(); @@ -504,7 +506,7 @@ public class DiscoParty { this.partyEvents = new PartyEvents(this.parent, this); parent.getServer().getPluginManager().registerEvents(this.partyEvents, this.parent); } - + void stopDisco() { removeAll(); this.duration = 0; @@ -516,9 +518,9 @@ public class DiscoParty { // stop listening HandlerList.unregisterAll(this.partyEvents); } - + class DiscoUpdater extends BukkitRunnable { - + @Override public void run() { update();