From 12cbf80d552ed7bc4d6ab7069266576ef7178e0e Mon Sep 17 00:00:00 2001 From: Gibstick Date: Sun, 28 Jul 2013 12:30:45 -0400 Subject: [PATCH 1/7] move party-related listeners to new class, and only listen during parties --- src/ca/gibstick/discosheep/DiscoParty.java | 25 ++++--- src/ca/gibstick/discosheep/DiscoSheep.java | 6 +- src/ca/gibstick/discosheep/GlobalEvents.java | 40 +++++++++++ src/ca/gibstick/discosheep/PartyEvents.java | 70 ++++++++++++++++++++ 4 files changed, 131 insertions(+), 10 deletions(-) create mode 100644 src/ca/gibstick/discosheep/GlobalEvents.java create mode 100644 src/ca/gibstick/discosheep/PartyEvents.java diff --git a/src/ca/gibstick/discosheep/DiscoParty.java b/src/ca/gibstick/discosheep/DiscoParty.java index 0564928..bc84070 100644 --- a/src/ca/gibstick/discosheep/DiscoParty.java +++ b/src/ca/gibstick/discosheep/DiscoParty.java @@ -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 sheepList = new ArrayList(); private ArrayList guestList = new ArrayList(); @@ -42,6 +43,7 @@ public class DiscoParty { private HashMap guestNumbers = new HashMap(); private static HashMap defaultGuestNumbers = new HashMap(); private static HashMap maxGuestNumbers = new HashMap(); + 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); } } diff --git a/src/ca/gibstick/discosheep/DiscoSheep.java b/src/ca/gibstick/discosheep/DiscoSheep.java index e5ba679..487257f 100644 --- a/src/ca/gibstick/discosheep/DiscoSheep.java +++ b/src/ca/gibstick/discosheep/DiscoSheep.java @@ -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 parties = new HashMap(); - 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")); diff --git a/src/ca/gibstick/discosheep/GlobalEvents.java b/src/ca/gibstick/discosheep/GlobalEvents.java new file mode 100644 index 0000000..026c05a --- /dev/null +++ b/src/ca/gibstick/discosheep/GlobalEvents.java @@ -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); + } +} diff --git a/src/ca/gibstick/discosheep/PartyEvents.java b/src/ca/gibstick/discosheep/PartyEvents.java new file mode 100644 index 0000000..96ed93f --- /dev/null +++ b/src/ca/gibstick/discosheep/PartyEvents.java @@ -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); + } + } + } + +} From d8fe7b480d8ce5c53ea95ea94c754e99775b647d Mon Sep 17 00:00:00 2001 From: Gibstick Date: Sun, 28 Jul 2013 12:47:00 -0400 Subject: [PATCH 2/7] listeners only listen for their own parties --- src/ca/gibstick/discosheep/DiscoParty.java | 2 +- src/ca/gibstick/discosheep/PartyEvents.java | 50 ++++++++++----------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/ca/gibstick/discosheep/DiscoParty.java b/src/ca/gibstick/discosheep/DiscoParty.java index bc84070..6566cd2 100644 --- a/src/ca/gibstick/discosheep/DiscoParty.java +++ b/src/ca/gibstick/discosheep/DiscoParty.java @@ -460,7 +460,7 @@ public class DiscoParty { this.scheduleUpdate(); parent.getPartyMap().put(this.player.getName(), this); // start listening - this.partyEvents = new PartyEvents(this.parent); + this.partyEvents = new PartyEvents(this.parent, this); parent.getServer().getPluginManager().registerEvents(this.partyEvents, this.parent); } diff --git a/src/ca/gibstick/discosheep/PartyEvents.java b/src/ca/gibstick/discosheep/PartyEvents.java index 96ed93f..4ca8197 100644 --- a/src/ca/gibstick/discosheep/PartyEvents.java +++ b/src/ca/gibstick/discosheep/PartyEvents.java @@ -17,54 +17,54 @@ import org.bukkit.event.player.PlayerShearEntityEvent; public class PartyEvents implements Listener { DiscoSheep parent; + DiscoParty party; - public PartyEvents(DiscoSheep parent) { + public PartyEvents(DiscoSheep parent, DiscoParty party) { this.parent = parent; + this.party = party; } - + // prevent sheep shearing - @EventHandler (priority = EventPriority.HIGHEST, ignoreCancelled = true) + @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); - } + + if (party.getSheepList().contains((Sheep) e.getEntity())) { + e.setCancelled(true); } + } } // actually make sheep and other guests invincible - @EventHandler (priority = EventPriority.HIGHEST, ignoreCancelled = true) + @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); - } + 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); - } + + 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) + @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); - } + + if (party.getGuestList().contains(e.getEntity())) { // safe; event is only triggered by LivingEntity targetting LivingEntity + e.setCancelled(true); } + } - } From 7c640f1bc7022b058e6b8ff7765833c499d7019a Mon Sep 17 00:00:00 2001 From: Gibstick Date: Sun, 28 Jul 2013 13:47:16 -0400 Subject: [PATCH 3/7] excluded more mobs that NPE, fixed array out of bounds in parsing --- src/ca/gibstick/discosheep/DiscoSheep.java | 9 ++++++--- .../gibstick/discosheep/DiscoSheepCommandExecutor.java | 2 +- src/ca/gibstick/discosheep/PartyEvents.java | 9 ++++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ca/gibstick/discosheep/DiscoSheep.java b/src/ca/gibstick/discosheep/DiscoSheep.java index 487257f..c16263d 100644 --- a/src/ca/gibstick/discosheep/DiscoSheep.java +++ b/src/ca/gibstick/discosheep/DiscoSheep.java @@ -44,15 +44,18 @@ public final class DiscoSheep extends JavaPlugin { /* * Iterate through all live entities and create default configuration values for them - * excludes bosses and pigmen since they throw NPE for some reason - * excludes horses for 1.5.2 compatibility (also NPE) + * excludes bosses and other mobs that throw NPE */ for (EntityType ent : EntityType.values()) { if (ent.isAlive() && !ent.equals(EntityType.ENDER_DRAGON) && !ent.equals(EntityType.WITHER) && !ent.equals(EntityType.PIG_ZOMBIE) - && !ent.equals(EntityType.HORSE) + && !ent.equals(EntityType.OCELOT) + && !ent.equals(EntityType.CAVE_SPIDER) + && !ent.equals(EntityType.MAGMA_CUBE) + && !ent.equals(EntityType.MUSHROOM_COW) + && !ent.equals(EntityType.IRON_GOLEM) && !ent.equals(EntityType.PLAYER)) { getConfig().addDefault("default.guests." + ent.toString(), 0); getConfig().addDefault("max.guests." + ent.toString(), 0); diff --git a/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java b/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java index 18efafc..b5f3d5e 100644 --- a/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java +++ b/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java @@ -156,7 +156,7 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { String[] guests = getNextArgs(args, i + 1); int j = 0; - while (j < guests.length) { + while (j < guests.length - 1) { try { mainParty.setGuestNumber(guests[j], getNextIntArg(guests, j)); } catch (IllegalArgumentException e) { diff --git a/src/ca/gibstick/discosheep/PartyEvents.java b/src/ca/gibstick/discosheep/PartyEvents.java index 4ca8197..cc1ef9d 100644 --- a/src/ca/gibstick/discosheep/PartyEvents.java +++ b/src/ca/gibstick/discosheep/PartyEvents.java @@ -1,6 +1,5 @@ package ca.gibstick.discosheep; -import java.util.logging.Level; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Sheep; import org.bukkit.event.EventHandler; @@ -18,6 +17,12 @@ public class PartyEvents implements Listener { DiscoSheep parent; DiscoParty party; + /* + * There will be multiple isntances of PartyEvents, + * and each instance will only listen for its own party. + * That way, we don't have multiple instances iterating through + * the entire parties hashmap redunandtly + */ public PartyEvents(DiscoSheep parent, DiscoParty party) { this.parent = parent; @@ -54,8 +59,6 @@ public class PartyEvents implements Listener { party.jump((LivingEntity) e.getEntity()); e.setCancelled(true); } - - parent.getLogger().log(Level.INFO, "Debug: EVENT TRIGGERED"); } // prevent uninvited guests from targetting players From b7a83e1c29298f375383cffddf071c3c724d8404 Mon Sep 17 00:00:00 2001 From: Gibstick Date: Sun, 28 Jul 2013 14:29:29 -0400 Subject: [PATCH 4/7] use Entity instead of LivingEntity --- src/ca/gibstick/discosheep/DiscoParty.java | 14 +++++++------- src/ca/gibstick/discosheep/DiscoSheep.java | 2 +- src/ca/gibstick/discosheep/PartyEvents.java | 7 +++---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/ca/gibstick/discosheep/DiscoParty.java b/src/ca/gibstick/discosheep/DiscoParty.java index 6566cd2..b1b5427 100644 --- a/src/ca/gibstick/discosheep/DiscoParty.java +++ b/src/ca/gibstick/discosheep/DiscoParty.java @@ -15,7 +15,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.entity.Entity; import org.bukkit.event.HandlerList; import org.bukkit.inventory.meta.FireworkMeta; import org.bukkit.util.Vector; @@ -29,7 +29,7 @@ public class DiscoParty { private DiscoSheep parent; private Player player; private ArrayList sheepList = new ArrayList(); - private ArrayList guestList = new ArrayList(); + private ArrayList guestList = new ArrayList(); static int defaultDuration = 300; // ticks for entire party static int defaultPeriod = 10; // ticks per state change static int defaultRadius = 5; @@ -107,7 +107,7 @@ public class DiscoParty { return sheepList; } - ArrayList getGuestList() { + ArrayList getGuestList() { return guestList; } @@ -267,7 +267,7 @@ public class DiscoParty { } void spawnGuest(World world, Location loc, EntityType type) { - LivingEntity newGuest = (LivingEntity) world.spawnEntity(loc, type); + Entity newGuest = world.spawnEntity(loc, type); getGuestList().add(newGuest); } @@ -276,7 +276,7 @@ public class DiscoParty { for (Sheep sheeple : getSheepList()) { sheeple.remove(); } - for (LivingEntity guest : getGuestList()) { + for (Entity guest : getGuestList()) { guest.remove(); } getSheepList().clear(); @@ -288,7 +288,7 @@ public class DiscoParty { sheep.setColor(discoColours[(r.nextInt(discoColours.length))]); } - void jump(LivingEntity entity) { + void jump(Entity entity) { Vector orgVel = entity.getVelocity(); Vector newVel = (new Vector()).copy(orgVel); newVel.add(new Vector(0, defaultSheepJump, 0)); @@ -370,7 +370,7 @@ public class DiscoParty { } } - for (LivingEntity guest : getGuestList()) { + for (Entity guest : getGuestList()) { if (doJump) { if (state % 2 == 0 && r.nextDouble() < 0.5) { jump(guest); diff --git a/src/ca/gibstick/discosheep/DiscoSheep.java b/src/ca/gibstick/discosheep/DiscoSheep.java index c16263d..babee07 100644 --- a/src/ca/gibstick/discosheep/DiscoSheep.java +++ b/src/ca/gibstick/discosheep/DiscoSheep.java @@ -130,7 +130,7 @@ public final class DiscoSheep extends JavaPlugin { } public synchronized ArrayList getParties() { - return new ArrayList(this.getPartyMap().values()); + return new ArrayList(this.getPartyMap().values()); } public void stopParty(String name) { diff --git a/src/ca/gibstick/discosheep/PartyEvents.java b/src/ca/gibstick/discosheep/PartyEvents.java index cc1ef9d..9893e18 100644 --- a/src/ca/gibstick/discosheep/PartyEvents.java +++ b/src/ca/gibstick/discosheep/PartyEvents.java @@ -1,6 +1,5 @@ package ca.gibstick.discosheep; -import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Sheep; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -47,7 +46,7 @@ public class PartyEvents implements Listener { if (e.getEntity() instanceof Sheep) { if (party.getSheepList().contains((Sheep) e.getEntity())) { { - party.jump((LivingEntity) e.getEntity()); // for kicks + party.jump(e.getEntity()); // for kicks e.setCancelled(true); } } @@ -56,7 +55,7 @@ public class PartyEvents implements Listener { if (party.getGuestList().contains(e.getEntity())) { - party.jump((LivingEntity) e.getEntity()); + party.jump(e.getEntity()); e.setCancelled(true); } } @@ -65,7 +64,7 @@ public class PartyEvents implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onEntityTargetLivingEntityEvent(EntityTargetEvent e) { - if (party.getGuestList().contains(e.getEntity())) { // safe; event is only triggered by LivingEntity targetting LivingEntity + if (party.getGuestList().contains(e.getEntity())) { e.setCancelled(true); } From 2d3e98560f8eeaa93f04cc071e7ae5bf000e1081 Mon Sep 17 00:00:00 2001 From: Gibstick Date: Sun, 28 Jul 2013 17:29:22 -0400 Subject: [PATCH 5/7] no more warnings cause we don't clone() --- src/ca/gibstick/discosheep/DiscoParty.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ca/gibstick/discosheep/DiscoParty.java b/src/ca/gibstick/discosheep/DiscoParty.java index b1b5427..3d47ed2 100644 --- a/src/ca/gibstick/discosheep/DiscoParty.java +++ b/src/ca/gibstick/discosheep/DiscoParty.java @@ -74,7 +74,7 @@ public class DiscoParty { this.period = DiscoParty.defaultPeriod; this.radius = DiscoParty.defaultRadius; this.sheep = DiscoParty.defaultSheep; - this.guestNumbers = (HashMap) DiscoParty.getDefaultGuestNumbers().clone(); + this.guestNumbers = new HashMap(DiscoParty.defaultGuestNumbers); r = new Random(); } @@ -84,7 +84,7 @@ public class DiscoParty { this.period = DiscoParty.defaultPeriod; this.radius = DiscoParty.defaultRadius; this.sheep = DiscoParty.defaultSheep; - this.guestNumbers = (HashMap) DiscoParty.getDefaultGuestNumbers().clone(); + this.guestNumbers = new HashMap(DiscoParty.defaultGuestNumbers); r = new Random(); } @@ -207,7 +207,7 @@ public class DiscoParty { DiscoParty.defaultPeriod = this.period; DiscoParty.defaultRadius = this.radius; DiscoParty.defaultSheep = this.sheep; - DiscoParty.defaultGuestNumbers = (HashMap) this.getGuestNumbers().clone(); + DiscoParty.defaultGuestNumbers = new HashMap(this.getGuestNumbers()); return this; } From d76681400d2f0ca52796e815ea646a58c3e7e78a Mon Sep 17 00:00:00 2001 From: Gibstick Date: Sun, 28 Jul 2013 17:59:46 -0400 Subject: [PATCH 6/7] renamed permissions, added toggle onjoin command, deleted unused class (was already split up) --- .../discosheep/BaaBaaBlockSheepEvents.java | 90 ------------------- src/ca/gibstick/discosheep/DiscoParty.java | 1 - src/ca/gibstick/discosheep/DiscoSheep.java | 44 ++++++--- .../discosheep/DiscoSheepCommandExecutor.java | 6 +- src/ca/gibstick/discosheep/GlobalEvents.java | 1 - src/ca/gibstick/discosheep/PartyEvents.java | 7 +- src/plugin.yml | 55 ++++++------ 7 files changed, 68 insertions(+), 136 deletions(-) delete mode 100644 src/ca/gibstick/discosheep/BaaBaaBlockSheepEvents.java diff --git a/src/ca/gibstick/discosheep/BaaBaaBlockSheepEvents.java b/src/ca/gibstick/discosheep/BaaBaaBlockSheepEvents.java deleted file mode 100644 index 0ae93e0..0000000 --- a/src/ca/gibstick/discosheep/BaaBaaBlockSheepEvents.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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.LivingEntity; -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; - -/** - * - * @author Mauve - */ -public class BaaBaaBlockSheepEvents implements Listener { - - DiscoSheep parent; - static DiscoSheepCommandExecutor CommExec; - - public BaaBaaBlockSheepEvents(DiscoSheep parent) { - this.parent = parent; - } - - // prevent sheep shearing - @EventHandler - 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 - 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); - } - } - - } - - // 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 - 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 - public void onPlayerJoinEvent(PlayerJoinEvent e) { - Player player = e.getPlayer(); - DiscoParty party = new DiscoParty(parent, player); - parent.partyOnJoin(player); - } -} diff --git a/src/ca/gibstick/discosheep/DiscoParty.java b/src/ca/gibstick/discosheep/DiscoParty.java index 3d47ed2..62528f5 100644 --- a/src/ca/gibstick/discosheep/DiscoParty.java +++ b/src/ca/gibstick/discosheep/DiscoParty.java @@ -43,7 +43,6 @@ public class DiscoParty { private HashMap guestNumbers = new HashMap(); private static HashMap defaultGuestNumbers = new HashMap(); private static HashMap maxGuestNumbers = new HashMap(); - static boolean partyOnJoin = true; private boolean doFireworks = false; private boolean doJump = true; private int duration, period, radius, sheep; diff --git a/src/ca/gibstick/discosheep/DiscoSheep.java b/src/ca/gibstick/discosheep/DiscoSheep.java index babee07..2d7724d 100644 --- a/src/ca/gibstick/discosheep/DiscoSheep.java +++ b/src/ca/gibstick/discosheep/DiscoSheep.java @@ -12,17 +12,19 @@ import org.bukkit.plugin.java.JavaPlugin; public final class DiscoSheep extends JavaPlugin { - static final String PERMISSION_PARTY = "discosheep.party"; - static final String PERMISSION_ALL = "discosheep.partyall"; - static final String PERMISSION_FIREWORKS = "discosheep.fireworks"; - static final String PERMISSION_STOPALL = "discosheep.stopall"; - static final String PERMISSION_RELOAD = "discosheep.reload"; - static final String PERMISSION_OTHER = "discosheep.other"; - 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"; - static final String PERMISSION_SPAWNGUESTS = "discosheep.spawnguests"; + static final String PERMISSION_PARTY = "discosheep.party.me"; + static final String PERMISSION_ALL = "discosheep.party.all"; + static final String PERMISSION_FIREWORKS = "discosheep.party.fireworks"; + static final String PERMISSION_STOPALL = "discosheep.admin.stopall"; + static final String PERMISSION_RELOAD = "discosheep.admin.reload"; + static final String PERMISSION_OTHER = "discosheep.party.other"; + static final String PERMISSION_CHANGEPERIOD = "discosheep.party.changeperiod"; + static final String PERMISSION_CHANGEDEFAULTS = "discosheep.admin.changedefaults"; + static final String PERMISSION_SAVECONFIG = "discosheep.admin.saveconfig"; + static final String PERMISSION_ONJOIN = "discosheep.party.onjoin"; + static final String PERMISSION_SPAWNGUESTS = "discosheep.party.spawnguests"; + static final String PERMISSION_TOGGLEPARTYONJOIN = "discosheep.admin.toggleonjoin"; + static boolean partyOnJoin = true; Map parties = new HashMap(); private GlobalEvents globalEvents = new GlobalEvents(this); @@ -31,7 +33,7 @@ public final class DiscoSheep extends JavaPlugin { getCommand("ds").setExecutor(new DiscoSheepCommandExecutor(this)); getServer().getPluginManager().registerEvents(globalEvents, this); - getConfig().addDefault("party-on-join.enabled", DiscoParty.partyOnJoin); + getConfig().addDefault("party-on-join.enabled", partyOnJoin); getConfig().addDefault("max.sheep", DiscoParty.maxSheep); getConfig().addDefault("max.radius", DiscoParty.maxRadius); getConfig().addDefault("max.duration", toSeconds_i(DiscoParty.maxDuration)); @@ -69,7 +71,7 @@ public final class DiscoSheep extends JavaPlugin { getConfig().options().copyDefaults(true); saveConfig(); - DiscoParty.partyOnJoin = getConfig().getBoolean("party-on-join.enabled"); + 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")); @@ -252,12 +254,28 @@ public final class DiscoSheep extends JavaPlugin { } void partyOnJoin(Player player) { + if (!partyOnJoin) { + return; + } if (player.hasPermission(PERMISSION_ONJOIN)) { DiscoParty party = new DiscoParty(this, player); party.startDisco(); } } + boolean togglePartyOnJoinCommand(CommandSender sender) { + if (!sender.hasPermission(PERMISSION_TOGGLEPARTYONJOIN)) { + return noPermsMessage(sender, PERMISSION_TOGGLEPARTYONJOIN); + } + partyOnJoin = !partyOnJoin; + if (partyOnJoin) { + sender.sendMessage(ChatColor.GREEN + "DiscoSheep party on join functionality enabled."); + } else { + sender.sendMessage(ChatColor.GREEN + "DiscoSheep party on join functionality disabled."); + } + return true; + } + boolean setDefaultsCommand(CommandSender sender, DiscoParty party) { if (sender.hasPermission(PERMISSION_CHANGEDEFAULTS)) { party.setDefaultsFromCurrent(); diff --git a/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java b/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java index b5f3d5e..0ab8a32 100644 --- a/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java +++ b/src/ca/gibstick/discosheep/DiscoSheepCommandExecutor.java @@ -89,6 +89,8 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { return parent.reloadCommand(sender); } else if (args[0].equalsIgnoreCase("save") || args[0].equalsIgnoreCase("saveconfig")) { return parent.saveConfigCommand(sender); + } else if (args[0].equalsIgnoreCase("togglejoin")) { + return parent.togglePartyOnJoinCommand(sender); } } @@ -149,11 +151,11 @@ public class DiscoSheepCommandExecutor implements CommandExecutor { if (!sender.hasPermission(DiscoSheep.PERMISSION_SPAWNGUESTS)) { return parent.noPermsMessage(sender, DiscoSheep.PERMISSION_SPAWNGUESTS); } - + if (parseNextArg(args, i, "none")) { return parent.zeroGuests(mainParty); } - + String[] guests = getNextArgs(args, i + 1); int j = 0; while (j < guests.length - 1) { diff --git a/src/ca/gibstick/discosheep/GlobalEvents.java b/src/ca/gibstick/discosheep/GlobalEvents.java index 026c05a..79e847f 100644 --- a/src/ca/gibstick/discosheep/GlobalEvents.java +++ b/src/ca/gibstick/discosheep/GlobalEvents.java @@ -24,7 +24,6 @@ public class GlobalEvents implements Listener { this.parent = parent; } - @EventHandler (priority = EventPriority.MONITOR) public void onPlayerQuitEvent(PlayerQuitEvent e) { String name = e.getPlayer().getName(); diff --git a/src/ca/gibstick/discosheep/PartyEvents.java b/src/ca/gibstick/discosheep/PartyEvents.java index 9893e18..1de12b8 100644 --- a/src/ca/gibstick/discosheep/PartyEvents.java +++ b/src/ca/gibstick/discosheep/PartyEvents.java @@ -17,10 +17,11 @@ public class PartyEvents implements Listener { DiscoSheep parent; DiscoParty party; /* - * There will be multiple isntances of PartyEvents, + * There will be multiple instances of PartyEvents, * and each instance will only listen for its own party. * That way, we don't have multiple instances iterating through - * the entire parties hashmap redunandtly + * the entire parties hashmap redundantly, yet we can still + * unregister the listeners when no parties are running. */ public PartyEvents(DiscoSheep parent, DiscoParty party) { @@ -52,8 +53,6 @@ public class PartyEvents implements Listener { } } - - if (party.getGuestList().contains(e.getEntity())) { party.jump(e.getEntity()); e.setCancelled(true); diff --git a/src/plugin.yml b/src/plugin.yml index b003343..ff58069 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -16,55 +16,60 @@ permissions: default: op children: discosheep.party: true - discosheep.partyall: true - discosheep.fireworks: true - discosheep.stopall: true - discosheep.reload: true - discosheep.other: true - discosheep.changeperiod: true - discosheep.changedefaults: true - discosheep.saveconfig: true - discosheep.spawnguests: true + discosheep.admin: true + discosheep.party: + description: All permissions related to parties + defualt: op + children: + discosheep.party.me: true + discosheep.party.all: true + discosheep.party.fireworks: true + discosheep.party.other: true + discosheep.party.changeperiod: true + discosheep.party.spawnguests: true discosheep.admin: description: Suggested permissions for administrators default: op children: - discosheep.stopall: true - discosheep.reload: true - discosheep.changedefaults: true - discosheep.saveconfig: true - discosheep.party: + discosheep.admin.stopall: true + discosheep.admin.reload: true + discosheep.admin.changedefaults: true + discosheep.admin.saveconfig: true + discosheep.party.me: description: Allows a player to have a party of one default: op - discosheep.partyall: + discosheep.party.all: description: Allows a player to call a server-wide party default: op - discosheep.stopall: + discosheep.admin.stopall: description: Allows a player to stop all parties on the server default: op - discosheep.fireworks: + discosheep.party.fireworks: description: Allows a player to enable have parties with fireworks default: op - discosheep.reload: + discosheep.admin.reload: description: Allows a player to reload settings from config.yml default: op - discosheep.other: + discosheep.party.other: description: Allows a player to call parties for other people, including themselves. default: op children: - discosheep.party: true - discosheep.changeperiod: + discosheep.party.me: true + discosheep.party.changeperiod: description: Allows a player to use the -p switch default: op - discosheep.changedefaults: + discosheep.admin.changedefaults: description: Allows a player to change the default settings default: op - discosheep.saveconfig: + discosheep.admin.saveconfig: description: Allows a player to save the config with current values set in memory default: op - discosheep.partyonjoin: + discosheep.party.onjoin: description: Gives a player a disco party on join default: false - discosheep.spawnguests: + discosheep.party.spawnguests: description: Allow a player to spawn other mobs + default: op + discosheep.admin.toggleonjoin: + description: Allow a player to toggle party-on-join functionality (force disable) default: op \ No newline at end of file From 4ac1e7a758bd9483980de5a912e6c5dc784a93f7 Mon Sep 17 00:00:00 2001 From: Gibstick Date: Sun, 28 Jul 2013 19:01:30 -0400 Subject: [PATCH 7/7] renamed config entry for party-on-join, registered events with better method --- src/ca/gibstick/discosheep/DiscoSheep.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ca/gibstick/discosheep/DiscoSheep.java b/src/ca/gibstick/discosheep/DiscoSheep.java index 2d7724d..99d4a92 100644 --- a/src/ca/gibstick/discosheep/DiscoSheep.java +++ b/src/ca/gibstick/discosheep/DiscoSheep.java @@ -26,14 +26,13 @@ public final class DiscoSheep extends JavaPlugin { static final String PERMISSION_TOGGLEPARTYONJOIN = "discosheep.admin.toggleonjoin"; static boolean partyOnJoin = true; Map parties = new HashMap(); - private GlobalEvents globalEvents = new GlobalEvents(this); @Override public void onEnable() { getCommand("ds").setExecutor(new DiscoSheepCommandExecutor(this)); - getServer().getPluginManager().registerEvents(globalEvents, this); + getServer().getPluginManager().registerEvents(new GlobalEvents(this), this); - getConfig().addDefault("party-on-join.enabled", partyOnJoin); + getConfig().addDefault("on-join.enabled", partyOnJoin); getConfig().addDefault("max.sheep", DiscoParty.maxSheep); getConfig().addDefault("max.radius", DiscoParty.maxRadius); getConfig().addDefault("max.duration", toSeconds_i(DiscoParty.maxDuration)); @@ -71,7 +70,7 @@ public final class DiscoSheep extends JavaPlugin { getConfig().options().copyDefaults(true); saveConfig(); - partyOnJoin = getConfig().getBoolean("party-on-join.enabled"); + partyOnJoin = getConfig().getBoolean("on-join.enabled"); DiscoParty.maxSheep = getConfig().getInt("max.sheep"); DiscoParty.maxRadius = getConfig().getInt("max.radius"); DiscoParty.maxDuration = toTicks(getConfig().getInt("max.duration")); @@ -98,6 +97,7 @@ public final class DiscoSheep extends JavaPlugin { } void saveConfigToDisk() { + getConfig().set("on-join.enabled", partyOnJoin); getConfig().set("default.sheep", DiscoParty.defaultSheep); getConfig().set("default.radius", DiscoParty.defaultRadius); getConfig().set("default.duration", toSeconds_i(DiscoParty.defaultDuration)); @@ -244,7 +244,7 @@ public final class DiscoSheep extends JavaPlugin { if (!hasParty(p.getName())) { DiscoParty individualParty = party.DiscoParty(p); individualParty.startDisco(); - p.sendMessage(ChatColor.RED + "LET'S DISCO!"); + p.sendMessage(ChatColor.RED + "LET'S DISCO!!"); } } return true;