diff --git a/src/ca/gibstick/discosheep/BaaBaaBlockSheepEvents.java b/src/ca/gibstick/discosheep/BaaBaaBlockSheepEvents.java deleted file mode 100644 index fe2948c..0000000 --- a/src/ca/gibstick/discosheep/BaaBaaBlockSheepEvents.java +++ /dev/null @@ -1,89 +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.EventPriority; -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; - - public BaaBaaBlockSheepEvents(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); - } - } - - } - - // 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); - } - } - } - - @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/DiscoParty.java b/src/ca/gibstick/discosheep/DiscoParty.java index 0564928..62528f5 100644 --- a/src/ca/gibstick/discosheep/DiscoParty.java +++ b/src/ca/gibstick/discosheep/DiscoParty.java @@ -15,7 +15,8 @@ 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; @@ -25,10 +26,10 @@ 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(); + 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; @@ -63,32 +64,34 @@ 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; this.radius = DiscoParty.defaultRadius; this.sheep = DiscoParty.defaultSheep; - this.guestNumbers = (HashMap) DiscoParty.getDefaultGuestNumbers().clone(); + this.guestNumbers = new HashMap(DiscoParty.defaultGuestNumbers); r = new Random(); } public DiscoParty(DiscoSheep parent) { - this.ds = parent; + this.parent = parent; this.duration = DiscoParty.defaultDuration; 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(); } // 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; @@ -103,7 +106,7 @@ public class DiscoParty { return sheepList; } - ArrayList getGuestList() { + ArrayList getGuestList() { return guestList; } @@ -203,7 +206,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; } @@ -263,7 +266,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); } @@ -272,7 +275,7 @@ public class DiscoParty { for (Sheep sheeple : getSheepList()) { sheeple.remove(); } - for (LivingEntity guest : getGuestList()) { + for (Entity guest : getGuestList()) { guest.remove(); } getSheepList().clear(); @@ -284,7 +287,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)); @@ -366,7 +369,7 @@ public class DiscoParty { } } - for (LivingEntity guest : getGuestList()) { + for (Entity guest : getGuestList()) { if (doJump) { if (state % 2 == 0 && r.nextDouble() < 0.5) { jump(guest); @@ -435,7 +438,7 @@ public class DiscoParty { void scheduleUpdate() { updater = new DiscoUpdater(this); - updater.runTaskLater(ds, this.period); + updater.runTaskLater(parent, this.period); } @Deprecated @@ -448,13 +451,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, this); + parent.getServer().getPluginManager().registerEvents(this.partyEvents, this.parent); } void stopDisco() { @@ -464,6 +470,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..99d4a92 100644 --- a/src/ca/gibstick/discosheep/DiscoSheep.java +++ b/src/ca/gibstick/discosheep/DiscoSheep.java @@ -12,25 +12,27 @@ 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 BaaBaaBlockSheepEvents blockEvents = new BaaBaaBlockSheepEvents(this); @Override public void onEnable() { getCommand("ds").setExecutor(new DiscoSheepCommandExecutor(this)); - getServer().getPluginManager().registerEvents(blockEvents, this); + getServer().getPluginManager().registerEvents(new GlobalEvents(this), this); + 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)); @@ -43,15 +45,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); @@ -65,6 +70,7 @@ public final class DiscoSheep extends JavaPlugin { getConfig().options().copyDefaults(true); saveConfig(); + 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")); @@ -91,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)); @@ -125,7 +132,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) { @@ -237,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; @@ -247,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 18efafc..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,14 +151,14 @@ 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) { + while (j < guests.length - 1) { try { mainParty.setGuestNumber(guests[j], getNextIntArg(guests, j)); } catch (IllegalArgumentException e) { diff --git a/src/ca/gibstick/discosheep/GlobalEvents.java b/src/ca/gibstick/discosheep/GlobalEvents.java new file mode 100644 index 0000000..79e847f --- /dev/null +++ b/src/ca/gibstick/discosheep/GlobalEvents.java @@ -0,0 +1,39 @@ +/* + * 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..1de12b8 --- /dev/null +++ b/src/ca/gibstick/discosheep/PartyEvents.java @@ -0,0 +1,71 @@ +package ca.gibstick.discosheep; + +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; + DiscoParty party; + /* + * 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 redundantly, yet we can still + * unregister the listeners when no parties are running. + */ + + public PartyEvents(DiscoSheep parent, DiscoParty party) { + this.parent = parent; + this.party = party; + } + + // prevent sheep shearing + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPlayerShear(PlayerShearEntityEvent e) { + if (e.getEntity() instanceof Sheep) { + + 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) { + if (party.getSheepList().contains((Sheep) e.getEntity())) { + { + party.jump(e.getEntity()); // for kicks + e.setCancelled(true); + } + } + + } + if (party.getGuestList().contains(e.getEntity())) { + party.jump(e.getEntity()); + e.setCancelled(true); + } + } + + // prevent uninvited guests from targetting players + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEntityTargetLivingEntityEvent(EntityTargetEvent e) { + + if (party.getGuestList().contains(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