Refactored to make DiscoUpdater only call the update method on DiscoSheep and moved all the update logic to DiscoSheep.

This commit is contained in:
RangerMauve 2013-06-30 18:01:05 -04:00
parent a087e07052
commit 1c350e844d
2 changed files with 24 additions and 31 deletions

View File

@ -35,7 +35,7 @@ public final class DiscoSheep extends JavaPlugin {
private final int defaultSheepAmount = 10; private final int defaultSheepAmount = 10;
private final int defaultDuration = 1000;// ticks private final int defaultDuration = 1000;// ticks
private final int defaultFrequency = 20;// ticks per state change private final int defaultFrequency = 20;// ticks per state change
private int frequency = 0, duration = 0;
@Override @Override
public void onEnable() { public void onEnable() {
@ -52,7 +52,7 @@ public final class DiscoSheep extends JavaPlugin {
return sheepArray; return sheepArray;
} }
ArrayList<Player> getPlayers(){ ArrayList<Player> getPlayers() {
return this.playerArray; return this.playerArray;
} }
@ -60,7 +60,7 @@ public final class DiscoSheep extends JavaPlugin {
Sheep newSheep = (Sheep) world.spawnEntity(loc, EntityType.SHEEP); Sheep newSheep = (Sheep) world.spawnEntity(loc, EntityType.SHEEP);
newSheep.setMaxHealth(10000); newSheep.setMaxHealth(10000);
newSheep.setHealth(10000); newSheep.setHealth(10000);
newSheep.setColor(discoColours[(int) Math.random() * discoColours.length]); newSheep.setColor(discoColours[(int) Math.random() * discoColours.length]);
getSheep().add(newSheep); getSheep().add(newSheep);
} }
@ -105,22 +105,32 @@ public final class DiscoSheep extends JavaPlugin {
//TODO: Add sound playing here //TODO: Add sound playing here
} }
// Called after discosheep is stopped void update() {
void cleanUp() { if (duration > 0) {
removeAllSheep(); randomizeSheepColours();
this.playerArray.clear(); playSounds();
duration -= frequency;
this.scheduleUpdate();
} else {
this.stopDisco();
}
} }
void scheduleUpdate() { void scheduleUpdate() {
updater.runTaskLater(this, updater.frequency); new DiscoUpdater(this).runTaskLater(this, this.frequency);
} }
void startDisco(int duration, List<Player> players) { void startDisco(int duration, List<Player> players) {
if(this.duration > 0){
stopDisco();
}
this.playerArray.addAll(players); this.playerArray.addAll(players);
for(Player player : players){ for(Player player : players){
this.spawnSheep(player, this.defaultSheepAmount); this.spawnSheep(player, this.defaultSheepAmount);
} }
updater.start(duration,this.defaultFrequency); this.frequency = this.defaultFrequency;
this.duration = this.defaultDuration;
this.scheduleUpdate();
} }
void startDisco(List<Player> players) { void startDisco(List<Player> players) {
@ -128,6 +138,8 @@ public final class DiscoSheep extends JavaPlugin {
} }
void stopDisco() { void stopDisco() {
updater.stop(); removeAllSheep();
this.playerArray.clear();
this.duration = 0;
} }
} }

View File

@ -4,33 +4,14 @@ import org.bukkit.scheduler.BukkitRunnable;
public class DiscoUpdater extends BukkitRunnable { public class DiscoUpdater extends BukkitRunnable {
int frequency = 0, duration = 0;
private DiscoSheep parent; private DiscoSheep parent;
public DiscoUpdater(DiscoSheep parent) { public DiscoUpdater(DiscoSheep parent) {
this.parent = parent; this.parent = parent;
} }
public void stop() {
this.duration = 0;
parent.cleanUp();
}
public void start(int duration, int frequency) {
this.frequency = frequency;
this.duration = duration;
parent.scheduleUpdate();
}
@Override @Override
public void run() { public void run() {
if (duration > 0) { parent.update();
parent.randomizeSheepColours();
parent.playSounds();
duration -= frequency;
parent.scheduleUpdate();
} else {
this.stop();
}
} }
} }