added code framework for uninvited guests
This commit is contained in:
parent
e15eb397da
commit
e73f0414d2
@ -5,10 +5,13 @@
|
||||
*/
|
||||
package ca.gibstick.discosheep;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Sheep;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
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.PlayerShearEntityEvent;
|
||||
|
||||
@ -39,7 +42,7 @@ public class BaaBaaBlockSheepEvents implements Listener {
|
||||
|
||||
// actually make sheep invincible
|
||||
@EventHandler
|
||||
public void onEntityDamageEvent(EntityDamageEvent e) {
|
||||
public void onLivingEntityDamageEvent(EntityDamageEvent e) {
|
||||
if (e.getEntity() instanceof Sheep) {
|
||||
for (DiscoParty party : parent.getParties()) {
|
||||
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
|
||||
@ -58,4 +77,11 @@ public class BaaBaaBlockSheepEvents implements Listener {
|
||||
parent.stopParty(name);
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
package ca.gibstick.discosheep;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.logging.Level;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Location;
|
||||
@ -14,6 +16,7 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Sheep;
|
||||
import org.bukkit.FireworkEffect;
|
||||
import org.bukkit.FireworkEffect.Builder;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.inventory.meta.FireworkMeta;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
@ -26,16 +29,20 @@ public class DiscoParty {
|
||||
private DiscoSheep ds;
|
||||
private Player player;
|
||||
private ArrayList<Sheep> sheepList = new ArrayList<Sheep>();
|
||||
private ArrayList<LivingEntity> guestList = new ArrayList<LivingEntity>();
|
||||
static int defaultDuration = 300; // ticks for entire party
|
||||
static int defaultPeriod = 10; // ticks per state change
|
||||
static int defaultRadius = 5;
|
||||
static int defaultSheep = 10;
|
||||
static int defaultCreepers = 1;
|
||||
static float defaultSheepJump = 0.35f;
|
||||
static int maxDuration = 2400; // 120 seconds
|
||||
static int maxSheep = 100;
|
||||
static int maxRadius = 100;
|
||||
static int minPeriod = 5; // 0.25 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 doJump = true;
|
||||
private int duration, period, radius, sheep;
|
||||
@ -78,18 +85,22 @@ public class DiscoParty {
|
||||
// used for /ds other and /ds all
|
||||
public DiscoParty DiscoParty(Player player) {
|
||||
DiscoParty newParty = new DiscoParty(this.ds, player);
|
||||
newParty.setDoFireworks(this.doFireworks);
|
||||
newParty.setDuration(this.duration);
|
||||
newParty.setPeriod(this.period);
|
||||
newParty.setRadius(this.radius);
|
||||
newParty.setSheep(this.sheep);
|
||||
newParty.doFireworks = this.doFireworks;
|
||||
newParty.duration = this.duration;
|
||||
newParty.period = this.period;
|
||||
newParty.radius = this.radius;
|
||||
newParty.sheep = this.sheep;
|
||||
return newParty;
|
||||
}
|
||||
|
||||
List<Sheep> getSheepList() {
|
||||
ArrayList<Sheep> getSheepList() {
|
||||
return sheepList;
|
||||
}
|
||||
|
||||
ArrayList<LivingEntity> getGuestList() {
|
||||
return guestList;
|
||||
}
|
||||
|
||||
public int getSheep() {
|
||||
return this.sheep;
|
||||
}
|
||||
@ -166,30 +177,51 @@ public class DiscoParty {
|
||||
return this;
|
||||
}
|
||||
|
||||
// Spawn some number of sheep next to given player
|
||||
void spawnSheep(int num, int sheepSpawnRadius) {
|
||||
Location getRandomSpawnLocation(double x, double z, World world, int spawnRadius) {
|
||||
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;
|
||||
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
|
||||
* random number must be square rooted to obtain uniform distribution
|
||||
* otherwise the sheep are biased toward the centre */
|
||||
double r = Math.sqrt(Math.random()) * sheepSpawnRadius;
|
||||
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);
|
||||
double x = player.getLocation().getX();
|
||||
double z = player.getLocation().getZ();
|
||||
for (int i = 0; i < sheep; i++) {
|
||||
loc = getRandomSpawnLocation(x, z, world, spawnRadius);
|
||||
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) {
|
||||
@ -200,13 +232,22 @@ public class DiscoParty {
|
||||
getSheepList().add(newSheep);
|
||||
}
|
||||
|
||||
// Mark all sheep in the sheep array for removal, then clear the array
|
||||
void removeAllSheep() {
|
||||
void spawnGuest(World world, Location loc, EntityType type) {
|
||||
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()) {
|
||||
//sheeple.setHealth(0); // removed to make it more resilient to updates
|
||||
sheeple.remove();
|
||||
}
|
||||
for (LivingEntity guest : getGuestList()) {
|
||||
guest.remove();
|
||||
}
|
||||
getSheepList().clear();
|
||||
getGuestList().clear();
|
||||
}
|
||||
|
||||
// Set a random colour for all sheep in array
|
||||
@ -362,11 +403,12 @@ public class DiscoParty {
|
||||
updater.runTaskLater(ds, this.period);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
void startDisco(int duration, int sheepAmount, int radius, int period, boolean fireworks) {
|
||||
if (this.duration > 0) {
|
||||
stopDisco();
|
||||
}
|
||||
this.spawnSheep(sheepAmount, radius);
|
||||
this.spawnAll(sheepAmount, radius);
|
||||
this.doFireworks = fireworks;
|
||||
this.period = period;
|
||||
this.duration = duration;
|
||||
@ -375,13 +417,14 @@ public class DiscoParty {
|
||||
}
|
||||
|
||||
void startDisco() {
|
||||
this.spawnSheep(sheep, radius);
|
||||
this.guestNumbers.put("CREEPER", 5);
|
||||
this.spawnAll(sheep, radius);
|
||||
this.scheduleUpdate();
|
||||
ds.getPartyMap().put(this.player.getName(), this);
|
||||
}
|
||||
|
||||
void stopDisco() {
|
||||
removeAllSheep();
|
||||
removeAllGuests();
|
||||
this.duration = 0;
|
||||
if (updater != null) {
|
||||
updater.cancel();
|
||||
|
@ -2,7 +2,6 @@ package ca.gibstick.discosheep;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.bukkit.Bukkit;
|
||||
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_CHANGEDEFAULTS = "discosheep.changedefaults";
|
||||
static final String PERMISSION_SAVECONFIG = "discosheep.saveconfig";
|
||||
static final String PERMISSION_ONJOIN = "discosheep.onjoin";
|
||||
Map<String, DiscoParty> parties = new HashMap<String, DiscoParty>();
|
||||
private BaaBaaBlockSheepEvents blockEvents = new BaaBaaBlockSheepEvents(this);
|
||||
FileConfiguration config;
|
||||
@ -102,10 +102,11 @@ public final class DiscoSheep extends JavaPlugin {
|
||||
return this.parties;
|
||||
}
|
||||
|
||||
public synchronized List<DiscoParty> getParties() {
|
||||
public synchronized ArrayList<DiscoParty> getParties() {
|
||||
return new ArrayList(this.getPartyMap().values());
|
||||
}
|
||||
|
||||
|
||||
public void stopParty(String name) {
|
||||
if (this.hasParty(name)) {
|
||||
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) {
|
||||
if (sender.hasPermission(PERMISSION_CHANGEDEFAULTS)) {
|
||||
party.setDefaultsFromCurrent();
|
||||
|
@ -60,3 +60,6 @@ permissions:
|
||||
discosheep.saveconfig:
|
||||
description: Allows a player to save the config with current values set in memory
|
||||
default: op
|
||||
discosheep.partyonjoin:
|
||||
description: Gives a player a disco party on join
|
||||
default: false
|
Loading…
Reference in New Issue
Block a user