Added floor caching and spawning, added boilerplate for changing floor color

This commit is contained in:
RangerMauve 2013-11-09 15:24:46 -05:00
parent b7361acb9b
commit bc082bb87d

View File

@ -15,6 +15,7 @@ import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep; import org.bukkit.entity.Sheep;
import org.bukkit.FireworkEffect; import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Builder; import org.bukkit.FireworkEffect.Builder;
import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -24,7 +25,7 @@ import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
public class DiscoParty { public class DiscoParty {
// Static properties // Static properties
static int defaultDuration = 300; // ticks for entire party static int defaultDuration = 300; // ticks for entire party
static int defaultPeriod = 10; // ticks per state change static int defaultPeriod = 10; // ticks per state change
@ -61,7 +62,6 @@ public class DiscoParty {
1.667f, 1.667f,
2.0f 2.0f
}; };
// Instance properties // Instance properties
private Random r = new Random(); private Random r = new Random();
private PartyEvents partyEvents; private PartyEvents partyEvents;
@ -78,12 +78,12 @@ public class DiscoParty {
private int duration, period, radius, sheep; private int duration, period, radius, sheep;
private int state = 0; // basically our own tick system private int state = 0; // basically our own tick system
private DiscoUpdater updater; private DiscoUpdater updater;
public DiscoParty(DiscoSheep parent, Player player) { public DiscoParty(DiscoSheep parent, Player player) {
this(parent); this(parent);
this.player = player; this.player = player;
} }
public DiscoParty(DiscoSheep parent) { public DiscoParty(DiscoSheep parent) {
this.parent = parent; this.parent = parent;
this.duration = DiscoParty.defaultDuration; this.duration = DiscoParty.defaultDuration;
@ -108,31 +108,39 @@ public class DiscoParty {
newParty.guestNumbers = this.getGuestNumbers(); newParty.guestNumbers = this.getGuestNumbers();
return newParty; return newParty;
} }
ArrayList<Sheep> getSheepList() { ArrayList<Sheep> getSheepList() {
return sheepList; return sheepList;
} }
ArrayList<Entity> getGuestList() { ArrayList<Entity> getGuestList() {
return guestList; return guestList;
} }
ArrayList<BlockState> getFloorCache() {
return this.floorBlockCache;
}
ArrayList<Block> getFloorBlocks() {
return this.floorBlocks;
}
public static HashMap<String, Integer> getDefaultGuestNumbers() { public static HashMap<String, Integer> getDefaultGuestNumbers() {
return defaultGuestNumbers; return defaultGuestNumbers;
} }
public HashMap<String, Integer> getGuestNumbers() { public HashMap<String, Integer> getGuestNumbers() {
return guestNumbers; return guestNumbers;
} }
public static HashMap<String, Integer> getMaxGuestNumbers() { public static HashMap<String, Integer> getMaxGuestNumbers() {
return maxGuestNumbers; return maxGuestNumbers;
} }
public int getSheep() { public int getSheep() {
return this.sheep; return this.sheep;
} }
public DiscoParty setPlayer(Player player) { public DiscoParty setPlayer(Player player) {
if (player != null) { if (player != null) {
this.player = player; this.player = player;
@ -141,7 +149,7 @@ public class DiscoParty {
throw new NullPointerException(); throw new NullPointerException();
} }
} }
public DiscoParty setDuration(int duration) throws IllegalArgumentException { public DiscoParty setDuration(int duration) throws IllegalArgumentException {
if (duration <= DiscoParty.maxDuration && duration > 0) { if (duration <= DiscoParty.maxDuration && duration > 0) {
this.duration = duration; this.duration = duration;
@ -150,7 +158,7 @@ public class DiscoParty {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} }
public DiscoParty setPeriod(int period) throws IllegalArgumentException { public DiscoParty setPeriod(int period) throws IllegalArgumentException {
if (period >= DiscoParty.minPeriod && period <= DiscoParty.maxPeriod) { if (period >= DiscoParty.minPeriod && period <= DiscoParty.maxPeriod) {
this.period = period; this.period = period;
@ -159,7 +167,7 @@ public class DiscoParty {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} }
public DiscoParty setRadius(int radius) throws IllegalArgumentException { public DiscoParty setRadius(int radius) throws IllegalArgumentException {
if (radius <= DiscoParty.maxRadius && radius > 0) { if (radius <= DiscoParty.maxRadius && radius > 0) {
this.radius = radius; this.radius = radius;
@ -168,7 +176,7 @@ public class DiscoParty {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} }
public DiscoParty setDenseRadius(int sheepNo) throws IllegalArgumentException { public DiscoParty setDenseRadius(int sheepNo) throws IllegalArgumentException {
Integer rand = (int) Math.floor(Math.sqrt(sheep / Math.PI)); Integer rand = (int) Math.floor(Math.sqrt(sheep / Math.PI));
if (rand > DiscoParty.maxRadius) { if (rand > DiscoParty.maxRadius) {
@ -177,11 +185,11 @@ public class DiscoParty {
if (rand < 1) { if (rand < 1) {
rand = 1; rand = 1;
} }
this.setRadius(rand); this.setRadius(rand);
return this; return this;
} }
public DiscoParty setSheep(int sheep) throws IllegalArgumentException { public DiscoParty setSheep(int sheep) throws IllegalArgumentException {
if (sheep <= DiscoParty.maxSheep && sheep > 0) { if (sheep <= DiscoParty.maxSheep && sheep > 0) {
this.sheep = sheep; this.sheep = sheep;
@ -190,22 +198,22 @@ public class DiscoParty {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} }
public DiscoParty setDoFireworks(boolean doFireworks) { public DiscoParty setDoFireworks(boolean doFireworks) {
this.doFireworks = doFireworks; this.doFireworks = doFireworks;
return this; return this;
} }
public DiscoParty setDoLightning(boolean doLightning) { public DiscoParty setDoLightning(boolean doLightning) {
this.doLightning = doLightning; this.doLightning = doLightning;
return this; return this;
} }
public DiscoParty setGuestNumber(String key, int n) throws IllegalArgumentException { public DiscoParty setGuestNumber(String key, int n) throws IllegalArgumentException {
if (getMaxGuestNumbers().containsKey(key.toUpperCase())) { if (getMaxGuestNumbers().containsKey(key.toUpperCase())) {
if (n <= getMaxGuestNumbers().get(key.toUpperCase()) && n >= 0) { // so that /ds defaults can take 0 as arg if (n <= getMaxGuestNumbers().get(key.toUpperCase()) && n >= 0) { // so that /ds defaults can take 0 as arg
getGuestNumbers().put(key, n); getGuestNumbers().put(key, n);
return this; return this;
} }
} }
@ -221,10 +229,10 @@ public class DiscoParty {
DiscoParty.defaultGuestNumbers = new HashMap<String, Integer>(this.getGuestNumbers()); DiscoParty.defaultGuestNumbers = new HashMap<String, Integer>(this.getGuestNumbers());
return this; return this;
} }
Location getRandomSpawnLocation(double x, double z, World world, int spawnRadius) { Location getRandomSpawnLocation(double x, double z, World world, int spawnRadius) {
Location loc; Location loc;
double y; double y;
/* random point on circle with polar coordinates /* random point on circle with polar coordinates
@ -235,11 +243,11 @@ public class DiscoParty {
x += rand * Math.cos(azimuth); x += rand * Math.cos(azimuth);
z += rand * Math.sin(azimuth); z += rand * Math.sin(azimuth);
y = this.player.getLocation().getY(); y = this.player.getLocation().getY();
loc = new Location(world, x, y, z); loc = new Location(world, x, y, z);
loc.setPitch(r.nextFloat() * 360 - 180); loc.setPitch(r.nextFloat() * 360 - 180);
loc.setYaw(0); loc.setYaw(0);
return loc; return loc;
} }
@ -247,8 +255,8 @@ public class DiscoParty {
void spawnAll(int sheep, int spawnRadius) { void spawnAll(int sheep, int spawnRadius) {
Location loc; Location loc;
World world = player.getWorld(); World world = player.getWorld();
double x = player.getLocation().getX(); double x = player.getLocation().getX();
double z = player.getLocation().getZ(); double z = player.getLocation().getZ();
for (int i = 0; i < sheep; i++) { for (int i = 0; i < sheep; i++) {
@ -260,14 +268,17 @@ public class DiscoParty {
for (Map.Entry entry : guestNumbers.entrySet()) { for (Map.Entry entry : guestNumbers.entrySet()) {
EntityType ent = EntityType.valueOf((String) entry.getKey()); EntityType ent = EntityType.valueOf((String) entry.getKey());
int num = (Integer) entry.getValue(); int num = (Integer) entry.getValue();
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
loc = getRandomSpawnLocation(x, z, world, spawnRadius); loc = getRandomSpawnLocation(x, z, world, spawnRadius);
spawnGuest(world, loc, ent); spawnGuest(world, loc, ent);
} }
} }
loc = player.getLocation();
this.spawnFloor(world, new Location(world,loc.getBlockX(),loc.getBlockY()-1,loc.getBlockZ()));
} }
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.setColor(discoColours[(r.nextInt(discoColours.length))]); newSheep.setColor(discoColours[(r.nextInt(discoColours.length))]);
@ -278,7 +289,7 @@ public class DiscoParty {
world.strikeLightningEffect(loc); world.strikeLightningEffect(loc);
} }
} }
void spawnGuest(World world, Location loc, EntityType type) { void spawnGuest(World world, Location loc, EntityType type) {
Entity newGuest = loc.getWorld().spawnEntity(loc, type); Entity newGuest = loc.getWorld().spawnEntity(loc, type);
getGuestList().add(newGuest); getGuestList().add(newGuest);
@ -286,6 +297,18 @@ public class DiscoParty {
world.strikeLightningEffect(loc); 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);
}
}
}
// Mark all guests for removal, then clear the array // Mark all guests for removal, then clear the array
void removeAll() { void removeAll() {
@ -295,7 +318,7 @@ public class DiscoParty {
for (Entity guest : getGuestList()) { for (Entity guest : getGuestList()) {
guest.remove(); guest.remove();
} }
for(BlockState block : this.floorBlockCache){ for (BlockState block : this.floorBlockCache) {
block.update(true); block.update(true);
} }
getSheepList().clear(); getSheepList().clear();
@ -307,7 +330,11 @@ public class DiscoParty {
void randomizeSheepColour(Sheep sheep) { void randomizeSheepColour(Sheep sheep) {
sheep.setColor(discoColours[(r.nextInt(discoColours.length))]); sheep.setColor(discoColours[(r.nextInt(discoColours.length))]);
} }
void randomizeFloorColor(Block block) {
// Randomize them colors, boi
}
void jump(Entity entity) { void jump(Entity entity) {
Vector orgVel = entity.getVelocity(); Vector orgVel = entity.getVelocity();
Vector newVel = (new Vector()).copy(orgVel); Vector newVel = (new Vector()).copy(orgVel);
@ -369,27 +396,27 @@ public class DiscoParty {
if (i == 17) { if (i == 17) {
c = Color.YELLOW; c = Color.YELLOW;
} }
return c; return c;
} }
void updateAll() { void updateAll() {
for (Sheep sheeple : getSheepList()) { for (Sheep sheeple : getSheepList()) {
randomizeSheepColour(sheeple); randomizeSheepColour(sheeple);
if (doFireworks && state % 8 == 0) { if (doFireworks && state % 8 == 0) {
if (r.nextDouble() < 0.50) { if (r.nextDouble() < 0.50) {
spawnRandomFireworkAtSheep(sheeple); spawnRandomFireworkAtSheep(sheeple);
} }
} }
if (doJump) { if (doJump) {
if (state % 2 == 0 && r.nextDouble() < 0.5) { if (state % 2 == 0 && r.nextDouble() < 0.5) {
jump(sheeple); jump(sheeple);
} }
} }
} }
for (Entity guest : getGuestList()) { for (Entity guest : getGuestList()) {
if (doJump) { if (doJump) {
if (state % 2 == 0 && r.nextDouble() < 0.5) { if (state % 2 == 0 && r.nextDouble() < 0.5) {
@ -397,23 +424,28 @@ public class DiscoParty {
} }
} }
} }
for (Block block : this.floorBlocks) {
this.randomizeFloorColor(block);
}
} }
private float getPentatonicNote() { private float getPentatonicNote() {
return DiscoParty.pentatonicNotes[r.nextInt(pentatonicNotes.length)]; return DiscoParty.pentatonicNotes[r.nextInt(pentatonicNotes.length)];
} }
void playSounds() { void playSounds() {
player.playSound(player.getLocation(), Sound.NOTE_BASS_DRUM, 0.75f, 1.0f); player.playSound(player.getLocation(), Sound.NOTE_BASS_DRUM, 0.75f, 1.0f);
if (this.state % 2 == 0) { if (this.state % 2 == 0) {
player.playSound(player.getLocation(), Sound.NOTE_SNARE_DRUM, 0.8f, 1.0f); player.playSound(player.getLocation(), Sound.NOTE_SNARE_DRUM, 0.8f, 1.0f);
} }
if ((this.state + 1) % 8 == 0) { if ((this.state + 1) % 8 == 0) {
player.playSound(player.getLocation(), Sound.NOTE_STICKS, 1.0f, 1.0f); player.playSound(player.getLocation(), Sound.NOTE_STICKS, 1.0f, 1.0f);
} }
} }
void randomizeFirework(Firework firework) { void randomizeFirework(Firework firework) {
Random r = new Random(); Random r = new Random();
Builder effect = FireworkEffect.builder(); Builder effect = FireworkEffect.builder();
@ -439,12 +471,12 @@ public class DiscoParty {
// apply it to the given firework // apply it to the given firework
firework.setFireworkMeta(meta); firework.setFireworkMeta(meta);
} }
void spawnRandomFireworkAtSheep(Sheep sheep) { void spawnRandomFireworkAtSheep(Sheep sheep) {
Firework firework = (Firework) sheep.getWorld().spawnEntity(sheep.getEyeLocation(), EntityType.FIREWORK); Firework firework = (Firework) sheep.getWorld().spawnEntity(sheep.getEyeLocation(), EntityType.FIREWORK);
randomizeFirework(firework); randomizeFirework(firework);
} }
void update() { void update() {
if (duration > 0) { if (duration > 0) {
updateAll(); updateAll();
@ -456,12 +488,12 @@ public class DiscoParty {
this.stopDisco(); this.stopDisco();
} }
} }
void scheduleUpdate() { void scheduleUpdate() {
updater = new DiscoUpdater(); updater = new DiscoUpdater();
updater.runTaskLater(parent, this.period); updater.runTaskLater(parent, this.period);
} }
void startDisco() { void startDisco() {
this.spawnAll(sheep, radius); this.spawnAll(sheep, radius);
this.scheduleUpdate(); this.scheduleUpdate();
@ -470,7 +502,7 @@ public class DiscoParty {
this.partyEvents = new PartyEvents(this.parent, this); this.partyEvents = new PartyEvents(this.parent, this);
parent.getServer().getPluginManager().registerEvents(this.partyEvents, this.parent); parent.getServer().getPluginManager().registerEvents(this.partyEvents, this.parent);
} }
void stopDisco() { void stopDisco() {
removeAll(); removeAll();
this.duration = 0; this.duration = 0;
@ -482,9 +514,9 @@ public class DiscoParty {
// stop listening // stop listening
HandlerList.unregisterAll(this.partyEvents); HandlerList.unregisterAll(this.partyEvents);
} }
class DiscoUpdater extends BukkitRunnable { class DiscoUpdater extends BukkitRunnable {
@Override @Override
public void run() { public void run() {
update(); update();