From fd6610f966d7153bccd623936e960e7fcc888805 Mon Sep 17 00:00:00 2001 From: GJ Date: Tue, 27 Mar 2012 17:09:23 -0400 Subject: [PATCH] Added McMMOPartyChangeEvent for API usage - fires whenever a player joins or leaves a party --- Changelog.txt | 1 + .../nossr50/commands/party/AcceptCommand.java | 22 ++++++- .../nossr50/commands/party/PartyCommand.java | 46 ++++++++++++- .../events/party/McMMOPartyChangeEvent.java | 65 +++++++++++++++++++ 4 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/events/party/McMMOPartyChangeEvent.java diff --git a/Changelog.txt b/Changelog.txt index cb114e7c4..34e78e33a 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -12,6 +12,7 @@ Version 1.3.04-dev + Added McMMOPlayerRepairCheckEvent for API usage - fires before repair process begins, can be cancelled. + Added ability to get skill level from McMMOPlayerExperience events + Added McMMOPartyTeleportEvent for API usage - fires before a successful teleportation would occur. + + Added McMMOPartyChangeEvent for API usage - fires whenever a player joins or leaves a party = Fixed Shake ability dropping bonemeal instead of ink for squids. = Fixed Green Terra & Super Breaker awarding 4x drops at high levels. = Fixed summoned ocelots never changing skins. diff --git a/src/main/java/com/gmail/nossr50/commands/party/AcceptCommand.java b/src/main/java/com/gmail/nossr50/commands/party/AcceptCommand.java index 645458b44..d868de26e 100644 --- a/src/main/java/com/gmail/nossr50/commands/party/AcceptCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/party/AcceptCommand.java @@ -1,5 +1,6 @@ package com.gmail.nossr50.commands.party; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -9,6 +10,8 @@ import org.bukkit.entity.Player; import com.gmail.nossr50.Users; import com.gmail.nossr50.mcPermissions; import com.gmail.nossr50.datatypes.PlayerProfile; +import com.gmail.nossr50.events.party.McMMOPartyChangeEvent; +import com.gmail.nossr50.events.party.McMMOPartyChangeEvent.EventReason; import com.gmail.nossr50.locale.mcLocale; import com.gmail.nossr50.party.Party; @@ -33,10 +36,25 @@ public class AcceptCommand implements CommandExecutor { Party Pinstance = Party.getInstance(); if (PP.inParty()) { + McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, PP.getParty(), PP.getInvite(), EventReason.CHANGED_PARTIES); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return true; + } + Pinstance.removeFromParty(player, PP); } - PP.acceptInvite(); - Pinstance.addToParty(player, PP, PP.getParty(), true); + else { + McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, null, PP.getInvite(), EventReason.JOINED_PARTY); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return true; + } + } + PP.acceptInvite(); + Pinstance.addToParty(player, PP, PP.getParty(), true); } else { player.sendMessage(mcLocale.getString("mcPlayerListener.NoInvites")); diff --git a/src/main/java/com/gmail/nossr50/commands/party/PartyCommand.java b/src/main/java/com/gmail/nossr50/commands/party/PartyCommand.java index 5761f2ef3..6fbfcd69e 100644 --- a/src/main/java/com/gmail/nossr50/commands/party/PartyCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/party/PartyCommand.java @@ -10,6 +10,8 @@ import org.bukkit.entity.Player; import com.gmail.nossr50.Users; import com.gmail.nossr50.mcPermissions; import com.gmail.nossr50.datatypes.PlayerProfile; +import com.gmail.nossr50.events.party.McMMOPartyChangeEvent; +import com.gmail.nossr50.events.party.McMMOPartyChangeEvent.EventReason; import com.gmail.nossr50.locale.mcLocale; import com.gmail.nossr50.party.Party; @@ -109,10 +111,18 @@ public class PartyCommand implements CommandExecutor { return true; } else if (args.length == 1) { if (args[0].equals("q") && PP.inParty()) { - Pinstance.removeFromParty(player, PP); + McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, PP.getParty(), null, EventReason.LEFT_PARTY); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return true; + } + + Pinstance.removeFromParty(player, PP); player.sendMessage(mcLocale.getString("mcPlayerListener.LeftParty")); return true; + } else if (args[0].equalsIgnoreCase("?")) { player.sendMessage(mcLocale.getString("Party.Help4", new Object[] { "/party " })); player.sendMessage(mcLocale.getString("Party.Help2", new Object[] { "/party " })); @@ -148,8 +158,23 @@ public class PartyCommand implements CommandExecutor { // Pinstance.dump(player); } else { if (PP.inParty()) { + McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, PP.getParty(), args[0], EventReason.CHANGED_PARTIES); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return true; + } + Pinstance.removeFromParty(player, PP); } + else { + McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, null, args[0], EventReason.JOINED_PARTY); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return true; + } + } Pinstance.addToParty(player, PP, args[0], false); return true; } @@ -186,6 +211,13 @@ public class PartyCommand implements CommandExecutor { } PlayerProfile tPP = Users.getProfile(tPlayer); + McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, tPP.getParty(), null, EventReason.KICKED_FROM_PARTY); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return true; + } + Pinstance.removeFromParty(tPlayer, tPP); tPlayer.sendMessage(mcLocale.getString("mcPlayerListener.LeftParty")); @@ -213,10 +245,22 @@ public class PartyCommand implements CommandExecutor { player.sendMessage(mcLocale.getString("Party.NotOwner")); } } else { + McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, PP.getParty(), args[0], EventReason.CHANGED_PARTIES); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return true; + } Pinstance.removeFromParty(player, PP); Pinstance.addToParty(player, PP, args[0], false, args[1]); } } else if (args.length == 2 && !PP.inParty()) { + McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, null, args[0], EventReason.JOINED_PARTY); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return true; + } Pinstance.addToParty(player, PP, args[0], false, args[1]); } diff --git a/src/main/java/com/gmail/nossr50/events/party/McMMOPartyChangeEvent.java b/src/main/java/com/gmail/nossr50/events/party/McMMOPartyChangeEvent.java new file mode 100644 index 000000000..09e644f39 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/party/McMMOPartyChangeEvent.java @@ -0,0 +1,65 @@ +package com.gmail.nossr50.events.party; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; + +public class McMMOPartyChangeEvent extends PlayerEvent implements Cancellable{ + + protected String oldParty; + protected String newParty; + protected EventReason reason; + protected boolean cancelled; + + public McMMOPartyChangeEvent(Player player, String oldParty, String newParty, EventReason reason) { + super(player); + newParty = newParty.replace(":", "."); + this.oldParty = oldParty; + this.newParty = newParty; + this.reason = reason; + this.cancelled = false; + } + + public String getOldParty() { + return oldParty; + } + + public String getNewParty() { + return newParty; + } + + public EventReason getReason() { + return reason; + } + + /** Rest of file is required boilerplate for custom events **/ + private static final HandlerList handlers = new HandlerList(); + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + /** Following are required for Cancellable **/ + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + public enum EventReason{ + JOINED_PARTY, + LEFT_PARTY, + KICKED_FROM_PARTY, + CHANGED_PARTIES; + } +}