Minor changes, whatever

This commit is contained in:
Georgiy 2013-06-30 21:00:43 -04:00
parent 9c44f3daec
commit 84d77f2f25
2 changed files with 149 additions and 148 deletions

View File

@ -1,138 +1,138 @@
package gibstick.bukkit.discosheep; package gibstick.bukkit.discosheep;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep; import org.bukkit.entity.Sheep;
/** /**
* *
* @author Georgiy * @author Georgiy
*/ */
public class DiscoParty { public class DiscoParty {
private DiscoSheep ds; private DiscoSheep ds;
private Player player; private Player player;
private ArrayList<Sheep> sheepList= new ArrayList<Sheep>(); private ArrayList<Sheep> sheepList= new ArrayList<Sheep>();
private int duration, frequency = 20, numSheep = 5; private int duration, frequency = 20, numSheep = 5;
private final int defaultDuration = 300; // ticks for entire party private final int defaultDuration = 300; // ticks for entire party
private final int defaultFrequency = 10; // ticks per state change private final int defaultFrequency = 10; // ticks per state change
private final int sheepSpawnRadius = 5; private final int sheepSpawnRadius = 5;
private final int defaultSheepAmount = 10; private final int defaultSheepAmount = 10;
private DiscoUpdater updater; private DiscoUpdater updater;
private static final DyeColor[] discoColours = { private static final DyeColor[] discoColours = {
DyeColor.RED, DyeColor.RED,
DyeColor.ORANGE, DyeColor.ORANGE,
DyeColor.YELLOW, DyeColor.YELLOW,
DyeColor.GREEN, DyeColor.GREEN,
DyeColor.BLUE, DyeColor.BLUE,
DyeColor.LIGHT_BLUE, DyeColor.LIGHT_BLUE,
DyeColor.PINK, DyeColor.PINK,
DyeColor.MAGENTA, DyeColor.MAGENTA,
DyeColor.LIME, DyeColor.LIME,
DyeColor.CYAN, DyeColor.CYAN,
DyeColor.PURPLE DyeColor.PURPLE
}; };
public DiscoParty(DiscoSheep parent, Player player) { public DiscoParty(DiscoSheep parent, Player player) {
this.ds = parent; this.ds = parent;
this.player = player; this.player = player;
} }
List<Sheep> getSheep() { List<Sheep> getSheep() {
return sheepList; return sheepList;
} }
void spawnSheep(World world, Location loc) { void spawnSheep(World world, Location loc) {
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.round(Math.random() * (discoColours.length - 1))]); newSheep.setColor(discoColours[(int) Math.round(Math.random() * (discoColours.length - 1))]);
getSheep().add(newSheep); getSheep().add(newSheep);
} }
// Spawn some number of sheep next to given player // Spawn some number of sheep next to given player
void spawnSheep(int num) { void spawnSheep(int num) {
Location loc; Location loc;
World world = player.getWorld(); World world = player.getWorld();
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
double x, y, z; double x, y, z;
// random x and z coordinates within a 5 block radius // random x and z coordinates within a 5 block radius
// safe y-coordinate // safe y-coordinate
x = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getX(); x = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getX();
z = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getZ(); z = -sheepSpawnRadius + (Math.random() * ((sheepSpawnRadius * 2) + 1)) + player.getLocation().getZ();
y = world.getHighestBlockYAt((int) x, (int) z); y = world.getHighestBlockYAt((int) x, (int) z);
loc = new Location(world, x, y, z); loc = new Location(world, x, y, z);
spawnSheep(world, loc); spawnSheep(world, loc);
} }
} }
// Mark all sheep in the sheep array for removal, then clear the array // Mark all sheep in the sheep array for removal, then clear the array
void removeAllSheep() { void removeAllSheep() {
for (Sheep sheep : getSheep()) { for (Sheep sheep : getSheep()) {
sheep.setHealth(0); sheep.setHealth(0);
sheep.remove(); sheep.remove();
} }
getSheep().clear(); getSheep().clear();
} }
// Set a random colour for all sheep in array // Set a random colour for all sheep in array
void randomizeSheepColours() { void randomizeSheepColours() {
for (Sheep sheep : getSheep()) { for (Sheep sheep : getSheep()) {
sheep.setColor(discoColours[(int) Math.round(Math.random() * (discoColours.length - 1))]); sheep.setColor(discoColours[(int) Math.round(Math.random() * (discoColours.length - 1))]);
} }
} }
void playSounds() { void playSounds() {
player.playSound(player.getLocation(), Sound.NOTE_BASS_DRUM, 1.0f, 1.0f); player.playSound(player.getLocation(), Sound.NOTE_BASS_DRUM, 1.0f, 1.0f);
player.playSound(player.getLocation(), Sound.BURP, frequency, (float) Math.random() + 1); player.playSound(player.getLocation(), Sound.BURP, frequency, (float) Math.random() + 1);
} }
void update() { void update() {
if (duration > 0) { if (duration > 0) {
randomizeSheepColours(); randomizeSheepColours();
playSounds(); playSounds();
duration -= frequency; duration -= frequency;
this.scheduleUpdate(); this.scheduleUpdate();
} else { } else {
this.stopDisco(); this.stopDisco();
} }
} }
void scheduleUpdate() { void scheduleUpdate() {
updater = new DiscoUpdater(this); updater = new DiscoUpdater(this);
updater.runTaskLater(ds, this.frequency); updater.runTaskLater(ds, this.frequency);
} }
void startDisco(int duration) { void startDisco(int duration) {
if (this.duration > 0) { if (this.duration > 0) {
stopDisco(); stopDisco();
} }
this.spawnSheep(this.defaultSheepAmount); this.spawnSheep(this.defaultSheepAmount);
this.frequency = this.defaultFrequency; this.frequency = this.defaultFrequency;
this.duration = this.defaultDuration; this.duration = this.defaultDuration;
this.scheduleUpdate(); this.scheduleUpdate();
ds.getPartyMap().put(this.player.getName(), this); ds.getPartyMap().put(this.player.getName(), this);
} }
void startDisco() { void startDisco() {
this.startDisco(this.defaultDuration); this.startDisco(this.defaultDuration);
} }
void stopDisco() { void stopDisco() {
removeAllSheep(); removeAllSheep();
this.duration = 0; this.duration = 0;
if (updater != null) { if (updater != null) {
updater.cancel(); updater.cancel();
} }
updater = null; updater = null;
ds.getParties().remove(this.player.getName()); ds.getPartyMap().remove(this.player.getName());
} }
} }

View File

@ -15,18 +15,18 @@ import org.bukkit.event.player.PlayerShearEntityEvent;
* @author Mauve * @author Mauve
*/ */
public class SheepDeshearer implements Listener { public class SheepDeshearer implements Listener {
DiscoSheep parent; DiscoSheep parent;
public SheepDeshearer(DiscoSheep parent) { public SheepDeshearer(DiscoSheep parent) {
this.parent = parent; this.parent = parent;
} }
@EventHandler @EventHandler
public void onPlayerShear(PlayerShearEntityEvent e) { public void onPlayerShear(PlayerShearEntityEvent e) {
if (e.getEntity() instanceof Sheep){ if (e.getEntity() instanceof Sheep) {
for(DiscoParty party : parent.getParties()){ for (DiscoParty party : parent.getParties()) {
if(party.getSheep().contains((Sheep)e.getEntity())){ if (party.getSheep().contains((Sheep) e.getEntity())) {
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -34,10 +34,11 @@ public class SheepDeshearer implements Listener {
} }
@EventHandler @EventHandler
public void onCreatureSpawn(CreatureSpawnEvent e){ public void onCreatureSpawn(CreatureSpawnEvent e) {
if(e.getEntity() instanceof Sheep && e.getSpawnReason().equals(CreatureSpawnEvent.SpawnReason.BREEDING)){ if (e.getEntity() instanceof Sheep &&
for(DiscoParty party : parent.getParties()){ e.getSpawnReason().equals(CreatureSpawnEvent.SpawnReason.BREEDING)) {
if(party.getSheep().contains((Sheep)e.getEntity())){ for (DiscoParty party : parent.getParties()) {
if (party.getSheep().contains((Sheep) e.getEntity())) {
e.setCancelled(true); e.setCancelled(true);
} }
} }