added code framework for uninvited guests

This commit is contained in:
Gibstick 2013-07-22 16:22:31 -04:00
parent e15eb397da
commit e73f0414d2
4 changed files with 117 additions and 37 deletions

View File

@ -5,10 +5,13 @@
*/ */
package ca.gibstick.discosheep; package ca.gibstick.discosheep;
import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep; import org.bukkit.entity.Sheep;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerShearEntityEvent; import org.bukkit.event.player.PlayerShearEntityEvent;
@ -39,7 +42,7 @@ public class BaaBaaBlockSheepEvents implements Listener {
// actually make sheep invincible // actually make sheep invincible
@EventHandler @EventHandler
public void onEntityDamageEvent(EntityDamageEvent e) { public void onLivingEntityDamageEvent(EntityDamageEvent e) {
if (e.getEntity() instanceof Sheep) { if (e.getEntity() instanceof Sheep) {
for (DiscoParty party : parent.getParties()) { for (DiscoParty party : parent.getParties()) {
if (party.getSheepList().contains((Sheep) e.getEntity())) { if (party.getSheepList().contains((Sheep) e.getEntity())) {
@ -50,6 +53,22 @@ public class BaaBaaBlockSheepEvents implements Listener {
} }
} }
} }
for (DiscoParty party : parent.getParties()) {
if (party.getGuestList().contains(e.getEntity())) {
e.setCancelled(true);
}
}
}
// prevent uninvited guests from targetting players
@EventHandler
public void onEntityTargetLivingEntityEvent(EntityTargetEvent e) {
for (DiscoParty party : parent.getParties()) {
if (party.getGuestList().contains(e.getEntity())) { // safe; event is only triggered by LivingEntity targetting LivingEntity
e.setCancelled(true);
}
}
} }
@EventHandler @EventHandler
@ -58,4 +77,11 @@ public class BaaBaaBlockSheepEvents implements Listener {
parent.stopParty(name); parent.stopParty(name);
// stop party on player quit or else it will CONTINUE FOR ETERNITY // stop party on player quit or else it will CONTINUE FOR ETERNITY
} }
@EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent e) {
Player player = e.getPlayer();
DiscoParty party = new DiscoParty(parent, player);
parent.partyOnJoin(player);
}
} }

View File

@ -1,8 +1,10 @@
package ca.gibstick.discosheep; package ca.gibstick.discosheep;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.HashMap;
import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.logging.Level;
import org.bukkit.Color; import org.bukkit.Color;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import org.bukkit.Location; import org.bukkit.Location;
@ -14,6 +16,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.entity.LivingEntity;
import org.bukkit.inventory.meta.FireworkMeta; import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@ -26,16 +29,20 @@ 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 ArrayList<LivingEntity> guestList = new ArrayList<LivingEntity>();
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
static int defaultRadius = 5; static int defaultRadius = 5;
static int defaultSheep = 10; static int defaultSheep = 10;
static int defaultCreepers = 1;
static float defaultSheepJump = 0.35f; static float defaultSheepJump = 0.35f;
static int maxDuration = 2400; // 120 seconds static int maxDuration = 2400; // 120 seconds
static int maxSheep = 100; static int maxSheep = 100;
static int maxRadius = 100; static int maxRadius = 100;
static int minPeriod = 5; // 0.25 seconds static int minPeriod = 5; // 0.25 seconds
static int maxPeriod = 40; // 2.0 seconds static int maxPeriod = 40; // 2.0 seconds
static int maxCreepers = 5;
private HashMap<String, Integer> guestNumbers = new HashMap<String, Integer>();
private boolean doFireworks = false; private boolean doFireworks = false;
private boolean doJump = true; private boolean doJump = true;
private int duration, period, radius, sheep; private int duration, period, radius, sheep;
@ -78,18 +85,22 @@ public class DiscoParty {
// used for /ds other and /ds all // used for /ds other and /ds all
public DiscoParty DiscoParty(Player player) { public DiscoParty DiscoParty(Player player) {
DiscoParty newParty = new DiscoParty(this.ds, player); DiscoParty newParty = new DiscoParty(this.ds, player);
newParty.setDoFireworks(this.doFireworks); newParty.doFireworks = this.doFireworks;
newParty.setDuration(this.duration); newParty.duration = this.duration;
newParty.setPeriod(this.period); newParty.period = this.period;
newParty.setRadius(this.radius); newParty.radius = this.radius;
newParty.setSheep(this.sheep); newParty.sheep = this.sheep;
return newParty; return newParty;
} }
List<Sheep> getSheepList() { ArrayList<Sheep> getSheepList() {
return sheepList; return sheepList;
} }
ArrayList<LivingEntity> getGuestList() {
return guestList;
}
public int getSheep() { public int getSheep() {
return this.sheep; return this.sheep;
} }
@ -166,30 +177,51 @@ public class DiscoParty {
return this; return this;
} }
// Spawn some number of sheep next to given player Location getRandomSpawnLocation(double x, double z, World world, int spawnRadius) {
void spawnSheep(int num, int sheepSpawnRadius) { Location loc;
double y;
/* random point on circle with polar coordinates
* random number must be square rooted to obtain uniform distribution
* otherwise the sheep are biased toward the centre */
double r = Math.sqrt(Math.random()) * spawnRadius;
double azimuth = Math.random() * 2 * Math.PI; // radians
x += r * Math.cos(azimuth);
z += r * Math.sin(azimuth);
y = world.getHighestBlockYAt((int) x, (int) z);
loc = new Location(world, x, y, z);
loc.setPitch((float) Math.random() * 360 - 180);
loc.setYaw(0);
return loc;
}
// Spawn some number of guests next to given player
void spawnAll(int sheep, int spawnRadius) {
Location loc; Location loc;
World world = player.getWorld(); World world = player.getWorld();
for (int i = 0; i < num; i++) {
double x = player.getLocation().getX();
double z = player.getLocation().getZ();
double y;
/* random point on circle with polar coordinates double x = player.getLocation().getX();
* random number must be square rooted to obtain uniform distribution double z = player.getLocation().getZ();
* otherwise the sheep are biased toward the centre */ for (int i = 0; i < sheep; i++) {
double r = Math.sqrt(Math.random()) * sheepSpawnRadius; loc = getRandomSpawnLocation(x, z, world, spawnRadius);
double azimuth = Math.random() * 2 * Math.PI; // radians
x += r * Math.cos(azimuth);
z += r * Math.sin(azimuth);
y = world.getHighestBlockYAt((int) x, (int) z);
loc = new Location(world, x, y, z);
loc.setPitch((float) Math.random() * 360 - 180);
loc.setYaw(0);
spawnSheep(world, loc); spawnSheep(world, loc);
} }
// loop through hashmap of other guests and spawn accordingly
for (Map.Entry entry : guestNumbers.entrySet()) {
EntityType ent = EntityType.fromName((String) entry.getKey());
int num = (Integer) entry.getValue();
for (int i = 0; i < num; i++) {
loc = getRandomSpawnLocation(x, z, world, spawnRadius);
spawnGuest(world, loc, ent);
}
}
} }
void spawnSheep(World world, Location loc) { void spawnSheep(World world, Location loc) {
@ -200,13 +232,22 @@ public class DiscoParty {
getSheepList().add(newSheep); getSheepList().add(newSheep);
} }
// Mark all sheep in the sheep array for removal, then clear the array void spawnGuest(World world, Location loc, EntityType type) {
void removeAllSheep() { LivingEntity newGuest = (LivingEntity) world.spawnEntity(loc, type);
getGuestList().add(newGuest);
ds.getLogger().log(Level.INFO, "SPAWNING GUEST");
}
// Mark all guests for removal, then clear the array
void removeAllGuests() {
for (Sheep sheeple : getSheepList()) { for (Sheep sheeple : getSheepList()) {
//sheeple.setHealth(0); // removed to make it more resilient to updates
sheeple.remove(); sheeple.remove();
} }
for (LivingEntity guest : getGuestList()) {
guest.remove();
}
getSheepList().clear(); getSheepList().clear();
getGuestList().clear();
} }
// Set a random colour for all sheep in array // Set a random colour for all sheep in array
@ -362,11 +403,12 @@ public class DiscoParty {
updater.runTaskLater(ds, this.period); updater.runTaskLater(ds, this.period);
} }
@Deprecated
void startDisco(int duration, int sheepAmount, int radius, int period, boolean fireworks) { void startDisco(int duration, int sheepAmount, int radius, int period, boolean fireworks) {
if (this.duration > 0) { if (this.duration > 0) {
stopDisco(); stopDisco();
} }
this.spawnSheep(sheepAmount, radius); this.spawnAll(sheepAmount, radius);
this.doFireworks = fireworks; this.doFireworks = fireworks;
this.period = period; this.period = period;
this.duration = duration; this.duration = duration;
@ -375,13 +417,14 @@ public class DiscoParty {
} }
void startDisco() { void startDisco() {
this.spawnSheep(sheep, radius); this.guestNumbers.put("CREEPER", 5);
this.spawnAll(sheep, radius);
this.scheduleUpdate(); this.scheduleUpdate();
ds.getPartyMap().put(this.player.getName(), this); ds.getPartyMap().put(this.player.getName(), this);
} }
void stopDisco() { void stopDisco() {
removeAllSheep(); removeAllGuests();
this.duration = 0; this.duration = 0;
if (updater != null) { if (updater != null) {
updater.cancel(); updater.cancel();

View File

@ -2,7 +2,6 @@ package ca.gibstick.discosheep;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -22,6 +21,7 @@ public final class DiscoSheep extends JavaPlugin {
static final String PERMISSION_CHANGEPERIOD = "discosheep.changeperiod"; static final String PERMISSION_CHANGEPERIOD = "discosheep.changeperiod";
static final String PERMISSION_CHANGEDEFAULTS = "discosheep.changedefaults"; static final String PERMISSION_CHANGEDEFAULTS = "discosheep.changedefaults";
static final String PERMISSION_SAVECONFIG = "discosheep.saveconfig"; static final String PERMISSION_SAVECONFIG = "discosheep.saveconfig";
static final String PERMISSION_ONJOIN = "discosheep.onjoin";
Map<String, DiscoParty> parties = new HashMap<String, DiscoParty>(); Map<String, DiscoParty> parties = new HashMap<String, DiscoParty>();
private BaaBaaBlockSheepEvents blockEvents = new BaaBaaBlockSheepEvents(this); private BaaBaaBlockSheepEvents blockEvents = new BaaBaaBlockSheepEvents(this);
FileConfiguration config; FileConfiguration config;
@ -102,10 +102,11 @@ public final class DiscoSheep extends JavaPlugin {
return this.parties; return this.parties;
} }
public synchronized List<DiscoParty> getParties() { public synchronized ArrayList<DiscoParty> getParties() {
return new ArrayList(this.getPartyMap().values()); return new ArrayList(this.getPartyMap().values());
} }
public void stopParty(String name) { public void stopParty(String name) {
if (this.hasParty(name)) { if (this.hasParty(name)) {
this.getParty(name).stopDisco(); this.getParty(name).stopDisco();
@ -211,6 +212,13 @@ public final class DiscoSheep extends JavaPlugin {
} }
} }
void partyOnJoin(Player player) {
if (player.hasPermission(PERMISSION_ONJOIN)) {
DiscoParty party = new DiscoParty(this, player);
party.startDisco();
}
}
boolean setDefaultsCommand(CommandSender sender, DiscoParty party) { boolean setDefaultsCommand(CommandSender sender, DiscoParty party) {
if (sender.hasPermission(PERMISSION_CHANGEDEFAULTS)) { if (sender.hasPermission(PERMISSION_CHANGEDEFAULTS)) {
party.setDefaultsFromCurrent(); party.setDefaultsFromCurrent();

View File

@ -60,3 +60,6 @@ permissions:
discosheep.saveconfig: discosheep.saveconfig:
description: Allows a player to save the config with current values set in memory description: Allows a player to save the config with current values set in memory
default: op default: op
discosheep.partyonjoin:
description: Gives a player a disco party on join
default: false