move party-related listeners to new class, and only listen during parties

This commit is contained in:
Gibstick 2013-07-28 12:30:45 -04:00
parent 7fb34e92d6
commit 12cbf80d55
4 changed files with 131 additions and 10 deletions

View File

@ -16,6 +16,7 @@ import org.bukkit.entity.Sheep;
import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Builder;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.util.Vector;
@ -25,7 +26,7 @@ import org.bukkit.util.Vector;
*/
public class DiscoParty {
private DiscoSheep ds;
private DiscoSheep parent;
private Player player;
private ArrayList<Sheep> sheepList = new ArrayList<Sheep>();
private ArrayList<LivingEntity> guestList = new ArrayList<LivingEntity>();
@ -42,6 +43,7 @@ public class DiscoParty {
private HashMap<String, Integer> guestNumbers = new HashMap<String, Integer>();
private static HashMap<String, Integer> defaultGuestNumbers = new HashMap<String, Integer>();
private static HashMap<String, Integer> maxGuestNumbers = new HashMap<String, Integer>();
static boolean partyOnJoin = true;
private boolean doFireworks = false;
private boolean doJump = true;
private int duration, period, radius, sheep;
@ -63,9 +65,10 @@ public class DiscoParty {
DyeColor.WHITE
};
private Random r;
private PartyEvents partyEvents;
public DiscoParty(DiscoSheep parent, Player player) {
this.ds = parent;
this.parent = parent;
this.player = player;
this.duration = DiscoParty.defaultDuration;
this.period = DiscoParty.defaultPeriod;
@ -76,7 +79,7 @@ public class DiscoParty {
}
public DiscoParty(DiscoSheep parent) {
this.ds = parent;
this.parent = parent;
this.duration = DiscoParty.defaultDuration;
this.period = DiscoParty.defaultPeriod;
this.radius = DiscoParty.defaultRadius;
@ -88,7 +91,8 @@ public class DiscoParty {
// copy but with new player
// used for /ds other and /ds all
public DiscoParty DiscoParty(Player player) {
DiscoParty newParty = new DiscoParty(this.ds, player);
DiscoParty newParty;
newParty = new DiscoParty(this.parent, player);
newParty.doFireworks = this.doFireworks;
newParty.duration = this.duration;
newParty.period = this.period;
@ -435,7 +439,7 @@ public class DiscoParty {
void scheduleUpdate() {
updater = new DiscoUpdater(this);
updater.runTaskLater(ds, this.period);
updater.runTaskLater(parent, this.period);
}
@Deprecated
@ -448,13 +452,16 @@ public class DiscoParty {
this.period = period;
this.duration = duration;
this.scheduleUpdate();
ds.getPartyMap().put(this.player.getName(), this);
parent.getPartyMap().put(this.player.getName(), this);
}
void startDisco() {
this.spawnAll(sheep, radius);
this.scheduleUpdate();
ds.getPartyMap().put(this.player.getName(), this);
parent.getPartyMap().put(this.player.getName(), this);
// start listening
this.partyEvents = new PartyEvents(this.parent);
parent.getServer().getPluginManager().registerEvents(this.partyEvents, this.parent);
}
void stopDisco() {
@ -464,6 +471,8 @@ public class DiscoParty {
updater.cancel();
}
updater = null;
ds.getPartyMap().remove(this.player.getName());
parent.getPartyMap().remove(this.player.getName());
// stop listening
HandlerList.unregisterAll(this.partyEvents);
}
}

View File

@ -24,13 +24,14 @@ public final class DiscoSheep extends JavaPlugin {
static final String PERMISSION_ONJOIN = "discosheep.onjoin";
static final String PERMISSION_SPAWNGUESTS = "discosheep.spawnguests";
Map<String, DiscoParty> parties = new HashMap<String, DiscoParty>();
private BaaBaaBlockSheepEvents blockEvents = new BaaBaaBlockSheepEvents(this);
private GlobalEvents globalEvents = new GlobalEvents(this);
@Override
public void onEnable() {
getCommand("ds").setExecutor(new DiscoSheepCommandExecutor(this));
getServer().getPluginManager().registerEvents(blockEvents, this);
getServer().getPluginManager().registerEvents(globalEvents, this);
getConfig().addDefault("party-on-join.enabled", DiscoParty.partyOnJoin);
getConfig().addDefault("max.sheep", DiscoParty.maxSheep);
getConfig().addDefault("max.radius", DiscoParty.maxRadius);
getConfig().addDefault("max.duration", toSeconds_i(DiscoParty.maxDuration));
@ -65,6 +66,7 @@ public final class DiscoSheep extends JavaPlugin {
getConfig().options().copyDefaults(true);
saveConfig();
DiscoParty.partyOnJoin = getConfig().getBoolean("party-on-join.enabled");
DiscoParty.maxSheep = getConfig().getInt("max.sheep");
DiscoParty.maxRadius = getConfig().getInt("max.radius");
DiscoParty.maxDuration = toTicks(getConfig().getInt("max.duration"));

View File

@ -0,0 +1,40 @@
/*
* BaaBaaBlockSheep have you any wool?
* Nope, event got cancelled.
* Also listens to other events, not just sheep events
*/
package ca.gibstick.discosheep;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
/**
*
* @author Mauve
*/
public class GlobalEvents implements Listener {
DiscoSheep parent;
public GlobalEvents(DiscoSheep parent) {
this.parent = parent;
}
@EventHandler (priority = EventPriority.MONITOR)
public void onPlayerQuitEvent(PlayerQuitEvent e) {
String name = e.getPlayer().getName();
parent.stopParty(name);
// stop party on player quit or else it will CONTINUE FOR ETERNITY
}
@EventHandler (priority = EventPriority.MONITOR)
public void onPlayerJoinEvent(PlayerJoinEvent e) {
Player player = e.getPlayer();
parent.partyOnJoin(player);
}
}

View File

@ -0,0 +1,70 @@
package ca.gibstick.discosheep;
import java.util.logging.Level;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Sheep;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.player.PlayerShearEntityEvent;
/**
*
* @author Charlie
*/
public class PartyEvents implements Listener {
DiscoSheep parent;
public PartyEvents(DiscoSheep parent) {
this.parent = parent;
}
// prevent sheep shearing
@EventHandler (priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerShear(PlayerShearEntityEvent e) {
if (e.getEntity() instanceof Sheep) {
for (DiscoParty party : parent.getParties()) {
if (party.getSheepList().contains((Sheep) e.getEntity())) {
e.setCancelled(true);
}
}
}
}
// actually make sheep and other guests invincible
@EventHandler (priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onLivingEntityDamageEvent(EntityDamageEvent e) {
if (e.getEntity() instanceof Sheep) {
for (DiscoParty party : parent.getParties()) {
if (party.getSheepList().contains((Sheep) e.getEntity())) {
{
party.jump((LivingEntity) e.getEntity()); // for kicks
e.setCancelled(true);
}
}
}
}
for (DiscoParty party : parent.getParties()) {
if (party.getGuestList().contains(e.getEntity())) {
party.jump((LivingEntity) e.getEntity());
e.setCancelled(true);
}
}
parent.getLogger().log(Level.INFO, "Debug: EVENT TRIGGERED");
}
// prevent uninvited guests from targetting players
@EventHandler (priority = EventPriority.HIGHEST, ignoreCancelled = true)
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);
}
}
}
}