]>
git.defcon.no Git - YAVote/blob - src/no/defcon/yavote/Votemanager.java
1 package no
.defcon
.yavote
;
3 import java
.util
.ArrayList
;
6 import org
.bukkit
.World
;
7 import org
.bukkit
.entity
.Player
;
8 import org
.bukkit
.plugin
.java
.JavaPlugin
;
10 public class Votemanager
{
11 private JavaPlugin plugin
;
12 private boolean voteRunning
;
13 private String voteType
;
14 private int expireTask
;
15 private List
<String
> yesVoters
;
16 private List
<String
> noVoters
;
19 public Votemanager(JavaPlugin plugin
) {
22 this.voteRunning
= false;
25 public boolean cancelVote( )
29 plugin
.getServer().broadcastMessage("Currently running vote is being canceled.");
30 plugin
.getServer().getScheduler().cancelTask(expireTask
);
37 public boolean startVote( String type
, Player p
)
39 if ( this.voteRunning
)
42 if ( ! (type
.equalsIgnoreCase("sun") ||
43 type
.equalsIgnoreCase("rain") ||
44 type
.equalsIgnoreCase("storm") ||
45 type
.equalsIgnoreCase("day") ||
46 type
.equalsIgnoreCase("night") ))
48 plugin
.getLogger().info("Tried to start a vote of invalid type. Code error!");
55 if ( yesVoters
== null )
56 yesVoters
= new ArrayList
<String
>();
60 if ( noVoters
== null )
61 noVoters
= new ArrayList
<String
>();
65 yesVoters
.add(p
.getName());
67 //if ( checkRatio( ) )
68 if ( ((float) yesVoters
.size() / (float) plugin
.getServer().getOnlinePlayers().length
) >= getRequired() )
70 applyVote( voteType
);
74 expireTask
= plugin
.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin
, new Runnable(){
80 plugin
.getServer().broadcastMessage("Time expired for voting on " + ((voteType
!= null) ? voteType
: " .. uhm *blush*"));
85 plugin
.getLogger().info("Timer expired but vote was not running :S");
89 }, plugin
.getConfig().getLong("vote.timeoutSeconds")*20L );
91 plugin
.getServer().broadcastMessage("A vote has started for " + voteType
.toLowerCase() );
95 public boolean isVoteRunning()
100 public String
getVoteType ()
105 public void removeVote(Player player
)
107 if ( ! voteRunning
|| (voteType
== null ) )
110 plugin
.getLogger().info("Debugging removeVote() with player: " + player
.getName() );
111 if ( yesVoters
.contains(player
.getName()) )
113 yesVoters
.remove(player
.getName());
114 plugin
.getLogger().info("Debugging, removeVote() removed YES vote for " + player
.getName() );
117 if ( noVoters
.contains(player
.getName()) )
119 noVoters
.remove(player
.getName());
120 plugin
.getLogger().info("Debugging, removeVote() removed NO vote for " + player
.getName() );
123 if ( yesVoters
.size() + noVoters
.size() == 0 )
125 plugin
.getLogger().info("Debugging: No voters remaining in lists. Cancel vote.");
129 //if( checkRatio( ) )
130 if ( ((float) yesVoters
.size() / (float) plugin
.getServer().getOnlinePlayers().length
-1 ) >= getRequired() )
132 plugin
.getLogger().info("Debugging: after removing vote, ratio test returns TRUE, applying vote");
133 applyVote( voteType
);
135 plugin
.getLogger().info("Returning from removeVote()");
138 public boolean addVote(Player player
, boolean yes
)
140 if ( ! voteRunning
|| (voteType
== null ) )
143 if( yesVoters
.contains(player
.getName()) || noVoters
.contains(player
.getName()) )
145 player
.sendMessage("You have already cast your vote");
148 if (yes
== true) yesVoters
.add(player
.getName());
149 else noVoters
.add(player
.getName());
151 //if ( (yes == true) && checkRatio( ) )
152 if ( ( yes
== true) && ( ((float) yesVoters
.size() / (float) plugin
.getServer().getOnlinePlayers().length
) >= getRequired() ) )
154 applyVote( voteType
);
159 if ( yesVoters
.size() + noVoters
.size() >= plugin
.getServer().getOnlinePlayers().length
)
161 plugin
.getServer().broadcastMessage("Vote failed for " + voteType
);
162 plugin
.getServer().getScheduler().cancelTask(expireTask
);
166 player
.sendMessage("Vote counted. Thank you.");
171 private void applyVote(String type
)
173 plugin
.getServer().getScheduler().cancelTask(expireTask
);
174 if ( ! voteRunning
|| (voteType
== null ) )
176 plugin
.getLogger().info("No vote running, but I was told to apply one. ERRR");
180 List
<World
> worlds
= plugin
.getServer().getWorlds();
181 for ( World w
: worlds
)
183 if ( voteType
.equalsIgnoreCase("sun"))
185 else if ( voteType
.equalsIgnoreCase("rain"))
187 else if ( voteType
.equalsIgnoreCase("storm"))
188 w
.setThundering(true);
189 else if ( voteType
.equalsIgnoreCase("day"))
190 w
.setTime( plugin
.getConfig().getInt("vote.time.dayStart") );
191 else if ( voteType
.equalsIgnoreCase("night"))
192 w
.setTime( plugin
.getConfig().getInt("vote.time.nightStart") );
194 plugin
.getServer().broadcastMessage("Vote succeeded for " + ((voteType
!= null) ? voteType
: " .. uhm *blush*"));
198 private float getRequired( )
201 if ( voteType
.equalsIgnoreCase("sun") || voteType
.equalsIgnoreCase("rain") || voteType
.equalsIgnoreCase("storm") )
202 req
= (float)plugin
.getConfig().getInt("vote.weather.requiredPercent") / 100.0F
;
203 else if ( voteType
.equalsIgnoreCase("day") || voteType
.equalsIgnoreCase("night") )
204 req
= (float)plugin
.getConfig().getInt("vote.time.requiredPercent") / 100.0F
;
210 // Original approach...
211 private boolean checkRatio( )
213 float required = getRequired();
214 float ratio = (float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length;
215 if ( ratio >= required ) return true;
220 private boolean checkRatio( )
222 if ( ((float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length ) >= getRequired() )
227 // But, trying to do it inline instead
230 private void clearState()