From 909ff029223302267d5a47d4938573c18f4994e2 Mon Sep 17 00:00:00 2001 From: Georgiy Date: Sun, 30 Jun 2013 19:54:27 -0400 Subject: [PATCH] Huge restructuring to have multiple parties at a time. --- .../bukkit/discosheep/DiscoParty.java | 142 +++++++++++++++++ .../bukkit/discosheep/DiscoSheep.java | 149 ++++-------------- .../discosheep/DiscoSheepCommandExecutor.java | 8 +- .../bukkit/discosheep/DiscoUpdater.java | 4 +- .../bukkit/discosheep/SheepDeshearer.java | 14 +- 5 files changed, 184 insertions(+), 133 deletions(-) create mode 100644 src/gibstick/bukkit/discosheep/DiscoParty.java diff --git a/src/gibstick/bukkit/discosheep/DiscoParty.java b/src/gibstick/bukkit/discosheep/DiscoParty.java new file mode 100644 index 0000000..6c3aea2 --- /dev/null +++ b/src/gibstick/bukkit/discosheep/DiscoParty.java @@ -0,0 +1,142 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package gibstick.bukkit.discosheep; + +import java.util.ArrayList; +import java.util.List; +import org.bukkit.DyeColor; +import org.bukkit.Location; +import org.bukkit.Sound; +import org.bukkit.World; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.entity.Sheep; + +/** + * + * @author Georgiy + */ +public class DiscoParty { + + private DiscoSheep ds; + private Player player; + private ArrayList sheepList; + private int duration, frequency = 20, numSheep = 5; + private final int defaultDuration = 300; // ticks for entire party + private final int defaultFrequency = 10; // ticks per state change + private final int sheepSpawnRadius = 5; + private final int defaultSheepAmount = 10; + private DiscoUpdater updater; + 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 + }; + + public DiscoParty(DiscoSheep parent, Player player) { + this.ds = parent; + this.player = player; + } + + List getSheep() { + return sheepList; + } + + void spawnSheep(World world, Location loc) { + Sheep newSheep = (Sheep) world.spawnEntity(loc, EntityType.SHEEP); + newSheep.setMaxHealth(10000); + newSheep.setHealth(10000); + newSheep.setColor(discoColours[(int) Math.round(Math.random() * (discoColours.length - 1))]); + getSheep().add(newSheep); + } + + // Spawn some number of sheep next to given player + void spawnSheep(int num) { + Location loc; + World world = player.getWorld(); + + for (int i = 0; i < num; i++) { + double x, y, z; + + // random x and z coordinates within a 5 block radius + // safe y-coordinate + x = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getX(); + z = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getZ(); + y = world.getHighestBlockYAt((int) x, (int) z); + loc = new Location(world, x, y, z); + spawnSheep(world, loc); + } + } + + // Mark all sheep in the sheep array for removal, then clear the array + void removeAllSheep() { + for (Sheep sheep : getSheep()) { + sheep.setHealth(0); + sheep.remove(); + } + getSheep().clear(); + } + + // Set a random colour for all sheep in array + void randomizeSheepColours() { + for (Sheep sheep : getSheep()) { + sheep.setColor(discoColours[(int) Math.round(Math.random() * (discoColours.length - 1))]); + } + } + + void playSounds() { + player.playSound(player.getLocation(), Sound.NOTE_BASS_DRUM, 1.0f, 1.0f); + player.playSound(player.getLocation(), Sound.BURP, frequency, (float) Math.random() + 1); + } + + void update() { + if (duration > 0) { + randomizeSheepColours(); + playSounds(); + duration -= frequency; + this.scheduleUpdate(); + } else { + this.stopDisco(); + } + } + + void scheduleUpdate() { + updater = new DiscoUpdater(this); + updater.runTaskLater(ds, this.frequency); + } + + void startDisco(int duration) { + if (this.duration > 0) { + stopDisco(); + } + this.spawnSheep(this.defaultSheepAmount); + this.frequency = this.defaultFrequency; + this.duration = this.defaultDuration; + this.scheduleUpdate(); + ds.getPartyMap().put(this.player.getName(), this); + } + + void startDisco() { + this.startDisco(this.defaultDuration); + } + + void stopDisco() { + removeAllSheep(); + this.duration = 0; + if (updater != null) { + updater.cancel(); + } + updater = null; + ds.getParties().remove(this.player.getName()); + } +} diff --git a/src/gibstick/bukkit/discosheep/DiscoSheep.java b/src/gibstick/bukkit/discosheep/DiscoSheep.java index a020b87..9b9fb7b 100644 --- a/src/gibstick/bukkit/discosheep/DiscoSheep.java +++ b/src/gibstick/bukkit/discosheep/DiscoSheep.java @@ -1,41 +1,17 @@ package gibstick.bukkit.discosheep; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import org.bukkit.DyeColor; -import org.bukkit.Location; -import org.bukkit.Sound; -import org.bukkit.World; -import org.bukkit.entity.EntityType; +import java.util.Map; import org.bukkit.entity.Player; -import org.bukkit.entity.Sheep; import org.bukkit.plugin.java.JavaPlugin; public final class DiscoSheep extends JavaPlugin { - private ArrayList sheepArray = new ArrayList(); - private ArrayList playerArray = new ArrayList(); - private SheepDeshearer deshear = new SheepDeshearer(sheepArray); - 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 - }; // array of accetable disco colours (order not important) - private DiscoUpdater updater; - // radius for random sheep spawns around player - private final int sheepSpawnRadius = 5; - private final int defaultSheepAmount = 10; - private final int defaultDuration = 300; // ticks for entire party - private final int defaultFrequency = 10; // ticks per state change - private int frequency = 0, duration = 0; + Map parties = new HashMap(); + private SheepDeshearer deshear = new SheepDeshearer(this); + // array of accetable disco colours (order not important) @Override public void onEnable() { @@ -47,102 +23,35 @@ public final class DiscoSheep extends JavaPlugin { public void onDisable() { } - ArrayList getSheep() { - return sheepArray; + public Map getPartyMap() { + return this.parties; } - - ArrayList getPlayers() { - return this.playerArray; + + public List getParties(){ + return new ArrayList(this.parties.values()); } - - void spawnSheep(World world, Location loc) { - Sheep newSheep = (Sheep) world.spawnEntity(loc, EntityType.SHEEP); - newSheep.setMaxHealth(10000); - newSheep.setHealth(10000); - newSheep.setColor(discoColours[(int)Math.round(Math.random() * (discoColours.length - 1))]); - getSheep().add(newSheep); + + public void stopParty(String name){ + if(this.hasParty(name)){ + this.getParty(name).stopDisco(); + } } - - // Spawn some number of sheep next to given player - void spawnSheep(Player player, int num) { - Location loc; - World world = player.getWorld(); - - for (int i = 0; i < num; i++) { - double x, y, z; - - // random x and z coordinates within a 5 block radius - // safe y-coordinate - x = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getX(); - z = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getZ(); - y = world.getHighestBlockYAt((int) x, (int) z); - loc = new Location(world, x, y, z); - spawnSheep(world, loc); + + public boolean hasParty(String name){ + return this.parties.containsKey(name); + } + + public DiscoParty getParty(String name){ + return this.parties.get(name); + } + + public void removeParty(String name){ + if(this.hasParty(name)){ + this.parties.remove(name); } } - // Mark all sheep in the sheep array for removal, then clear the array - void removeAllSheep() { - for (Sheep sheep : getSheep()) { - sheep.setHealth(0); - sheep.remove(); - } - getSheep().clear(); - } - - // Set a random colour for all sheep in array - void randomizeSheepColours() { - for (Sheep sheep : getSheep()) { - sheep.setColor(discoColours[(int)Math.round(Math.random() * (discoColours.length - 1))]); - } - } - - void playSounds() { - for(Player player: getPlayers()){ - player.playSound(player.getLocation(), Sound.NOTE_BASS_DRUM, 1.0f, 1.0f); - player.playSound(player.getLocation(), Sound.BURP, frequency,(float) Math.random() + 1); - } - } - - void update() { - if (duration > 0) { - randomizeSheepColours(); - playSounds(); - duration -= frequency; - this.scheduleUpdate(); - } else { - this.stopDisco(); - } - } - - void scheduleUpdate() { - updater = new DiscoUpdater(this); - updater.runTaskLater(this, this.frequency); - } - - void startDisco(int duration, List players) { - if (this.duration > 0) { - stopDisco(); - } - this.playerArray.addAll(players); - for (Player player : players) { - this.spawnSheep(player, this.defaultSheepAmount); - } - this.frequency = this.defaultFrequency; - this.duration = this.defaultDuration; - this.scheduleUpdate(); - } - - void startDisco(List players) { - this.startDisco(this.defaultDuration, players); - } - - void stopDisco() { - removeAllSheep(); - this.playerArray.clear(); - this.duration = 0; - if(updater != null) - updater.cancel(); - updater = null; + public void startDisco(Player player) { + new DiscoParty(this, player).startDisco(); } } diff --git a/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java b/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java index f786f36..9a0b746 100644 --- a/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java +++ b/src/gibstick/bukkit/discosheep/DiscoSheepCommandExecutor.java @@ -1,7 +1,5 @@ package gibstick.bukkit.discosheep; -import java.util.ArrayList; -import java.util.List; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.CommandExecutor; @@ -17,10 +15,8 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if(sender instanceof Player){ - List players = new ArrayList(); - players.add((Player)sender); - parent.startDisco(players); + if (sender instanceof Player) { + parent.startDisco((Player)sender); } return true; } diff --git a/src/gibstick/bukkit/discosheep/DiscoUpdater.java b/src/gibstick/bukkit/discosheep/DiscoUpdater.java index 2e21052..4e6842c 100644 --- a/src/gibstick/bukkit/discosheep/DiscoUpdater.java +++ b/src/gibstick/bukkit/discosheep/DiscoUpdater.java @@ -4,9 +4,9 @@ import org.bukkit.scheduler.BukkitRunnable; public class DiscoUpdater extends BukkitRunnable { - private DiscoSheep parent; + private DiscoParty parent; - public DiscoUpdater(DiscoSheep parent) { + public DiscoUpdater(DiscoParty parent) { this.parent = parent; } diff --git a/src/gibstick/bukkit/discosheep/SheepDeshearer.java b/src/gibstick/bukkit/discosheep/SheepDeshearer.java index d382550..2df53a8 100644 --- a/src/gibstick/bukkit/discosheep/SheepDeshearer.java +++ b/src/gibstick/bukkit/discosheep/SheepDeshearer.java @@ -16,16 +16,20 @@ import org.bukkit.event.player.PlayerShearEntityEvent; */ public class SheepDeshearer implements Listener { - List sheep; + DiscoSheep parent; - public SheepDeshearer(List sheep) { - this.sheep = sheep; + public SheepDeshearer(DiscoSheep parent) { + this.parent = parent; } @EventHandler public void onPlayerShear(PlayerShearEntityEvent e) { - if (e.getEntity() instanceof Sheep && sheep.contains((Sheep) e.getEntity())) { - e.setCancelled(true); + if (e.getEntity() instanceof Sheep){ + for(DiscoParty party : parent.getParties()){ + if(party.getSheep().contains((Sheep)e.getEntity())){ + e.setCancelled(true); + } + } } } }