Huge restructuring to have multiple parties at a time.

This commit is contained in:
Georgiy 2013-06-30 19:54:27 -04:00
parent 112d3f0f08
commit 909ff02922
5 changed files with 184 additions and 133 deletions

View File

@ -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<Sheep> 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<Sheep> 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());
}
}

View File

@ -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<Sheep> sheepArray = new ArrayList<Sheep>();
private ArrayList<Player> playerArray = new ArrayList<Player>();
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<String,DiscoParty> parties = new HashMap<String,DiscoParty>();
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<Sheep> getSheep() {
return sheepArray;
public Map<String,DiscoParty> getPartyMap() {
return this.parties;
}
ArrayList<Player> getPlayers() {
return this.playerArray;
public List<DiscoParty> 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<Player> 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<Player> 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();
}
}

View File

@ -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<Player> players = new ArrayList<Player>();
players.add((Player)sender);
parent.startDisco(players);
if (sender instanceof Player) {
parent.startDisco((Player)sender);
}
return true;
}

View File

@ -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;
}

View File

@ -16,16 +16,20 @@ import org.bukkit.event.player.PlayerShearEntityEvent;
*/
public class SheepDeshearer implements Listener {
List<Sheep> 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);
}
}
}
}
}