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); + } + } + } + +}