From: Jon Langseth Date: Tue, 8 May 2012 12:34:39 +0000 (+0200) Subject: Adding trigger for Logout/Quit handling. Should correct ratio-tests when players... X-Git-Url: https://git.defcon.no/?a=commitdiff_plain;h=b7b8828e5c4c4ad90b00ed84f617ecc62caf4587;p=YAVote Adding trigger for Logout/Quit handling. Should correct ratio-tests when players log out.. Also, changed ratio-check... --- diff --git a/src/no/defcon/yavote/Votemanager.java b/src/no/defcon/yavote/Votemanager.java index 2212b7a..76bb4e8 100644 --- a/src/no/defcon/yavote/Votemanager.java +++ b/src/no/defcon/yavote/Votemanager.java @@ -101,6 +101,31 @@ public class Votemanager { return voteType; } + public void removeVote(Player player ) + { + if ( ! voteRunning || (voteType == null ) ) + return; + + plugin.getLogger().info("Debugging removeVote() with player: " + player.getName() ); + if ( yesVoters.contains(player.getName()) ) + { + yesVoters.remove(player.getName()); + plugin.getLogger().info("Debugging, removeVote() removed YES vote for " + player.getName() ); + } + + if ( noVoters.contains(player.getName()) ) + { + noVoters.remove(player.getName()); + plugin.getLogger().info("Debugging, removeVote() removed NO vote for " + player.getName() ); + } + if( checkRatio( ) ) + { + plugin.getLogger().info("Debugging: after removing vote, checkRatio() returns TRUE, applying vote"); + applyVote( voteType ); + } + plugin.getLogger().info("Returning from removeVote()"); + } + public boolean addVote(Player player, boolean yes) { if ( ! voteRunning || (voteType == null ) ) @@ -169,7 +194,7 @@ public class Votemanager { required = (float)plugin.getConfig().getInt("vote.time.requiredPercent") / 100.0F; float ratio = (float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length; - if ( ratio > required ) return true; + if ( ratio >= required ) return true; return false; } diff --git a/src/no/defcon/yavote/YAVote.java b/src/no/defcon/yavote/YAVote.java index f12edb3..98433ba 100644 --- a/src/no/defcon/yavote/YAVote.java +++ b/src/no/defcon/yavote/YAVote.java @@ -10,7 +10,11 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.PluginLogger; -public class YAVote extends JavaPlugin { +import org.bukkit.event.Listener; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerQuitEvent; + +public class YAVote extends JavaPlugin implements Listener { public PluginLogger logger; private Votemanager manager; @@ -51,8 +55,18 @@ public class YAVote extends JavaPlugin { logger.info("Creating configuration file"); saveConfig(); } + + getServer().getPluginManager().registerEvents(this, this); + logger.info("Version " + getDescription().getVersion() + " is enabled!"); } + + @EventHandler + public void onLogout(PlayerQuitEvent event) + { + logger.info("Triggered onLogout() with " + getServer().getOnlinePlayers().length + " players online."); + manager.removeVote( event.getPlayer() ); + } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){ if(cmd.getName().equalsIgnoreCase("vote")){