From a4f1b299221d775c7bca933fe619e5a6e298a19f Mon Sep 17 00:00:00 2001 From: cmastudios Date: Fri, 20 Sep 2013 16:11:44 -0500 Subject: [PATCH] Properly implement War event API, some cleaning. I changed some of the new event API code from @BenMenking to use more Bukkit provided frameworks like Player and use basic java structures instead of checking integers. --- .../main/java/com/tommytony/war/Warzone.java | 48 ++++++++----------- .../com/tommytony/war/config/TeamKind.java | 9 ++++ .../war/event/WarEntityListener.java | 10 +++- .../war/event/WarPlayerDeathEvent.java | 36 +++++++++++--- .../war/event/WarPlayerKillEvent.java | 31 ------------ .../war/event/WarPlayerThiefEvent.java | 19 ++++---- .../tommytony/war/event/WarScoreCapEvent.java | 20 ++++---- .../tommytony/war/event/WarTeamWinEvent.java | 26 ---------- .../war/event/WarThiefDeathEvent.java | 36 -------------- 9 files changed, 90 insertions(+), 145 deletions(-) delete mode 100644 war/src/main/java/com/tommytony/war/event/WarPlayerKillEvent.java delete mode 100644 war/src/main/java/com/tommytony/war/event/WarTeamWinEvent.java delete mode 100644 war/src/main/java/com/tommytony/war/event/WarThiefDeathEvent.java diff --git a/war/src/main/java/com/tommytony/war/Warzone.java b/war/src/main/java/com/tommytony/war/Warzone.java index f3354de..d1ef755 100644 --- a/war/src/main/java/com/tommytony/war/Warzone.java +++ b/war/src/main/java/com/tommytony/war/Warzone.java @@ -6,10 +6,13 @@ import java.util.Iterator; import java.util.List; import java.util.logging.Level; +import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.OfflinePlayer; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -20,6 +23,9 @@ import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.LeatherArmorMeta; +import org.bukkit.scoreboard.DisplaySlot; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Scoreboard; import org.getspout.spoutapi.SpoutManager; import org.getspout.spoutapi.player.SpoutPlayer; @@ -30,12 +36,11 @@ import com.tommytony.war.config.TeamConfigBag; import com.tommytony.war.config.TeamKind; import com.tommytony.war.config.WarzoneConfig; import com.tommytony.war.config.WarzoneConfigBag; +import com.tommytony.war.event.WarBattleWinEvent; import com.tommytony.war.event.WarPlayerDeathEvent; import com.tommytony.war.event.WarPlayerLeaveEvent; import com.tommytony.war.event.WarPlayerThiefEvent; import com.tommytony.war.event.WarScoreCapEvent; -import com.tommytony.war.event.WarTeamWinEvent; -import com.tommytony.war.event.WarThiefDeathEvent; import com.tommytony.war.job.InitZoneJob; import com.tommytony.war.job.LoadoutResetJob; import com.tommytony.war.job.LogKillsDeathsJob; @@ -58,15 +63,6 @@ import com.tommytony.war.utility.PotionEffectHelper; import com.tommytony.war.volume.BlockInfo; import com.tommytony.war.volume.Volume; import com.tommytony.war.volume.ZoneVolume; -import org.apache.commons.lang.Validate; -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.scoreboard.DisplaySlot; -import org.bukkit.scoreboard.Objective; -import org.bukkit.scoreboard.Scoreboard; -import java.util.Map; -import org.bukkit.OfflinePlayer; -import org.bukkit.inventory.meta.LeatherArmorMeta; /** * @@ -989,20 +985,15 @@ public class Warzone { // whoever didn't lose, reward them // + List winningTeams = new ArrayList(teams.size()); for( Team t : teams ) { if( !t.getPlayers().contains(player)) { - //War.war.getServer().getLogger().info("WAR: team " + t.getName() + " are winners!"); - ArrayList winnerNames = new ArrayList(); - for(Player name : t.getPlayers()) { - winnerNames.add(name.getName()); - //War.war.getServer().getLogger().info("WAR: adding " + name.getName() + " to the list"); - } - - WarTeamWinEvent event1 = new WarTeamWinEvent(winnerNames.toArray(new String[winnerNames.size()])); - War.war.getServer().getPluginManager().callEvent(event1); + winningTeams.add(t); } } - + WarBattleWinEvent event1 = new WarBattleWinEvent(this, winningTeams); + War.war.getServer().getPluginManager().callEvent(event1); + if (!scores.equals("")) { for (Team t : teams) { t.teamcast("New scores - " + scores); @@ -1115,9 +1106,6 @@ public class Warzone { // Decrement lifepool playerTeam.setRemainingLives(remaining - 1); - - WarPlayerDeathEvent event1 = new WarPlayerDeathEvent(player.getName()); - War.war.getServer().getPluginManager().callEvent(event1); // Lifepool empty warning if (remaining - 1 == 0) { @@ -1269,7 +1257,7 @@ public class Warzone { // Flags public void addFlagThief(Team lostFlagTeam, String flagThief) { this.flagThieves.put(flagThief, lostFlagTeam); - WarPlayerThiefEvent event1 = new WarPlayerThiefEvent(flagThief, WarPlayerThiefEvent.STOLE_FLAG); + WarPlayerThiefEvent event1 = new WarPlayerThiefEvent(Bukkit.getPlayerExact(flagThief), WarPlayerThiefEvent.StolenObject.FLAG); War.war.getServer().getPluginManager().callEvent(event1); } @@ -1291,7 +1279,7 @@ public class Warzone { // Bomb public void addBombThief(Bomb bomb, String bombThief) { this.bombThieves.put(bombThief, bomb); - WarPlayerThiefEvent event1 = new WarPlayerThiefEvent(bombThief, WarPlayerThiefEvent.STOLE_BOMB); + WarPlayerThiefEvent event1 = new WarPlayerThiefEvent(Bukkit.getPlayerExact(bombThief), WarPlayerThiefEvent.StolenObject.BOMB); War.war.getServer().getPluginManager().callEvent(event1); } @@ -1314,7 +1302,7 @@ public class Warzone { public void addCakeThief(Cake cake, String cakeThief) { this.cakeThieves.put(cakeThief, cake); - WarPlayerThiefEvent event1 = new WarPlayerThiefEvent(cakeThief, WarPlayerThiefEvent.STOLE_CAKE); + WarPlayerThiefEvent event1 = new WarPlayerThiefEvent(Bukkit.getPlayerExact(cakeThief), WarPlayerThiefEvent.StolenObject.CAKE); War.war.getServer().getPluginManager().callEvent(event1); } @@ -1351,7 +1339,11 @@ public class Warzone { public void handleScoreCapReached(String winnersStr) { // Score cap reached. Reset everything. this.isEndOfGame = true; - WarScoreCapEvent event1 = new WarScoreCapEvent(winnersStr.split(" ")); + List winningTeams = new ArrayList(teams.size()); + for (String team : winnersStr.split(" ")) { + winningTeams.add(this.getTeamByKind(TeamKind.getTeam(team))); + } + WarScoreCapEvent event1 = new WarScoreCapEvent(winningTeams); War.war.getServer().getPluginManager().callEvent(event1); new ScoreCapReachedJob(this, winnersStr).run(); // run inventory and teleports immediately to avoid inv reset problems diff --git a/war/src/main/java/com/tommytony/war/config/TeamKind.java b/war/src/main/java/com/tommytony/war/config/TeamKind.java index cf5cea2..f0bbf79 100644 --- a/war/src/main/java/com/tommytony/war/config/TeamKind.java +++ b/war/src/main/java/com/tommytony/war/config/TeamKind.java @@ -136,4 +136,13 @@ public enum TeamKind { public MaterialData getBlockData() { return new Wool(this.dyeColor); } + + public static TeamKind getTeam(String teamName) { + for (TeamKind team : TeamKind.values()) { + if (team.toString().equalsIgnoreCase(teamName)) { + return team; + } + } + return null; + } } diff --git a/war/src/main/java/com/tommytony/war/event/WarEntityListener.java b/war/src/main/java/com/tommytony/war/event/WarEntityListener.java index 28e3930..e57f2b7 100644 --- a/war/src/main/java/com/tommytony/war/event/WarEntityListener.java +++ b/war/src/main/java/com/tommytony/war/event/WarEntityListener.java @@ -182,6 +182,8 @@ public class WarEntityListener implements Listener { War.war.getKillstreakReward().rewardPlayer(a, defenderWarzone.getKillCount(a.getName())); } } + WarPlayerDeathEvent event1 = new WarPlayerDeathEvent(defenderWarzone, d, a, event.getCause()); + War.war.getServer().getPluginManager().callEvent(event1); defenderWarzone.handleDeath(d); if (!defenderWarzone.getWarzoneConfig().getBoolean(WarzoneConfig.REALDEATHS)) { // fast respawn, don't really die @@ -194,6 +196,8 @@ public class WarEntityListener implements Listener { Bomb bomb = defenderWarzone.getBombForThief(d.getName()); // Kill the bomber + WarPlayerDeathEvent event1 = new WarPlayerDeathEvent(defenderWarzone, d, null, event.getCause()); + War.war.getServer().getPluginManager().callEvent(event1); defenderWarzone.handleDeath(d); if (defenderWarzone.getWarzoneConfig().getBoolean(WarzoneConfig.REALDEATHS)) { @@ -289,7 +293,8 @@ public class WarEntityListener implements Listener { team.teamcast(deathMessage); } } - + WarPlayerDeathEvent event1 = new WarPlayerDeathEvent(defenderWarzone, d, null, event.getCause()); + War.war.getServer().getPluginManager().callEvent(event1); defenderWarzone.handleDeath(d); if (!defenderWarzone.getWarzoneConfig().getBoolean(WarzoneConfig.REALDEATHS)) { @@ -468,7 +473,8 @@ public class WarEntityListener implements Listener { teamToMsg.teamcast(deathMessage); } } - + WarPlayerDeathEvent event1 = new WarPlayerDeathEvent(zone, player, null, event.getCause()); + War.war.getServer().getPluginManager().callEvent(event1); zone.handleDeath(player); if (!zone.getWarzoneConfig().getBoolean(WarzoneConfig.REALDEATHS)) { diff --git a/war/src/main/java/com/tommytony/war/event/WarPlayerDeathEvent.java b/war/src/main/java/com/tommytony/war/event/WarPlayerDeathEvent.java index 75ad1b0..b54410c 100644 --- a/war/src/main/java/com/tommytony/war/event/WarPlayerDeathEvent.java +++ b/war/src/main/java/com/tommytony/war/event/WarPlayerDeathEvent.java @@ -1,20 +1,44 @@ package com.tommytony.war.event; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.bukkit.event.entity.EntityDamageEvent.DamageCause; + +import com.tommytony.war.Warzone; public class WarPlayerDeathEvent extends Event { private static final HandlerList handlers = new HandlerList(); - private String victim; - - public WarPlayerDeathEvent(String victim) { + private Player victim; + private Warzone zone; + private Entity killer; + private DamageCause cause; + + public WarPlayerDeathEvent(Warzone zone, Player victim, Entity killer, + DamageCause cause) { + this.zone = zone; this.victim = victim; + this.killer = killer; + this.cause = cause; } - - public String getVictim() { + + public Warzone getZone() { + return zone; + } + + public Entity getKiller() { + return killer; + } + + public DamageCause getCause() { + return cause; + } + + public Player getVictim() { return victim; } - + @Override public HandlerList getHandlers() { return handlers; diff --git a/war/src/main/java/com/tommytony/war/event/WarPlayerKillEvent.java b/war/src/main/java/com/tommytony/war/event/WarPlayerKillEvent.java deleted file mode 100644 index 75f5d62..0000000 --- a/war/src/main/java/com/tommytony/war/event/WarPlayerKillEvent.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.tommytony.war.event; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class WarPlayerKillEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - private String attacker, victim; - - public WarPlayerKillEvent(String attacker, String victim) { - this.attacker = attacker; - this.victim = victim; - } - - public String getAttacker() { - return attacker; - } - - public String getVictim() { - return victim; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} diff --git a/war/src/main/java/com/tommytony/war/event/WarPlayerThiefEvent.java b/war/src/main/java/com/tommytony/war/event/WarPlayerThiefEvent.java index cc20d17..6283681 100644 --- a/war/src/main/java/com/tommytony/war/event/WarPlayerThiefEvent.java +++ b/war/src/main/java/com/tommytony/war/event/WarPlayerThiefEvent.java @@ -1,23 +1,24 @@ package com.tommytony.war.event; +import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; public class WarPlayerThiefEvent extends Event { private static final HandlerList handlers = new HandlerList(); - private String player; - private int type; + private Player player; + private StolenObject type; - public WarPlayerThiefEvent(String thief, int stolen) { + public WarPlayerThiefEvent(Player thief, StolenObject stolen) { this.player = thief; this.type = stolen; } - public String getThief() { + public Player getThief() { return player; } - public int getStolenObject() { + public StolenObject getStolenObject() { return type; } @@ -30,7 +31,9 @@ public class WarPlayerThiefEvent extends Event { return handlers; } - public static final int STOLE_FLAG = 0; - public static final int STOLE_BOMB = 1; - public static final int STOLE_CAKE = 2; + public enum StolenObject { + FLAG, + BOMB, + CAKE + } } diff --git a/war/src/main/java/com/tommytony/war/event/WarScoreCapEvent.java b/war/src/main/java/com/tommytony/war/event/WarScoreCapEvent.java index e339781..9c22654 100644 --- a/war/src/main/java/com/tommytony/war/event/WarScoreCapEvent.java +++ b/war/src/main/java/com/tommytony/war/event/WarScoreCapEvent.java @@ -1,20 +1,24 @@ package com.tommytony.war.event; +import java.util.List; + import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import com.tommytony.war.Team; + public class WarScoreCapEvent extends Event { private static final HandlerList handlers = new HandlerList(); - private String[] members; - - public WarScoreCapEvent(String[] members) { - this.members = members; + private List winningTeams; + + public List getWinningTeams() { + return winningTeams; } - - public String[] getWinners() { - return members; + + public WarScoreCapEvent(List winningTeams) { + this.winningTeams = winningTeams; } - + @Override public HandlerList getHandlers() { return handlers; diff --git a/war/src/main/java/com/tommytony/war/event/WarTeamWinEvent.java b/war/src/main/java/com/tommytony/war/event/WarTeamWinEvent.java deleted file mode 100644 index cc17d2c..0000000 --- a/war/src/main/java/com/tommytony/war/event/WarTeamWinEvent.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.tommytony.war.event; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class WarTeamWinEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - private String[] members; - - public WarTeamWinEvent(String[] members) { - this.members = members; - } - - public String[] getWinners() { - return members; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} diff --git a/war/src/main/java/com/tommytony/war/event/WarThiefDeathEvent.java b/war/src/main/java/com/tommytony/war/event/WarThiefDeathEvent.java deleted file mode 100644 index 3ce8c99..0000000 --- a/war/src/main/java/com/tommytony/war/event/WarThiefDeathEvent.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.tommytony.war.event; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class WarThiefDeathEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - private String player; - private int type; - - public WarThiefDeathEvent(String player, int type) { - this.player = player; - this.type = type; - } - - public String getThiefName() { - return player; - } - - public int getThiefType() { - return type; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - - public static final int FLAG_THIEF = 0; - public static final int BOMB_THIEF = 1; - public static final int CAKE_THIEF = 2; -}