]>
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.");
130 plugin
.getLogger().info("Debugging: yesVoters.size() = " + yesVoters
.size() );
131 plugin
.getLogger().info("Debugging: getRequired() = " + getRequired() );
132 plugin
.getLogger().info("Debugging: getOnlinePlayers - 1 = " + (plugin
.getServer().getOnlinePlayers().length
- 1) );
133 plugin
.getLogger().info("Debugging: ratio = " + ( (float) yesVoters
.size() / (float) (plugin
.getServer().getOnlinePlayers().length
- 1) ) );
135 //if( checkRatio( ) )
136 if ( ((float) yesVoters
.size() / (float) (plugin
.getServer().getOnlinePlayers().length
- 1) ) >= getRequired() )
138 plugin
.getLogger().info("Debugging: after removing vote, ratio test returns TRUE, applying vote");
139 applyVote( voteType
);
141 plugin
.getLogger().info("Returning from removeVote()");
144 public boolean addVote(Player player
, boolean yes
)
146 if ( ! voteRunning
|| (voteType
== null ) )
149 if( yesVoters
.contains(player
.getName()) || noVoters
.contains(player
.getName()) )
151 player
.sendMessage("You have already cast your vote");
154 if (yes
== true) yesVoters
.add(player
.getName());
155 else noVoters
.add(player
.getName());
157 //if ( (yes == true) && checkRatio( ) )
158 if ( ( yes
== true) && ( ((float) yesVoters
.size() / (float) plugin
.getServer().getOnlinePlayers().length
) >= getRequired() ) )
160 applyVote( voteType
);
165 if ( yesVoters
.size() + noVoters
.size() >= plugin
.getServer().getOnlinePlayers().length
)
167 plugin
.getServer().broadcastMessage("Vote failed for " + voteType
);
168 plugin
.getServer().getScheduler().cancelTask(expireTask
);
172 player
.sendMessage("Vote counted. Thank you.");
177 private void applyVote(String type
)
179 plugin
.getServer().getScheduler().cancelTask(expireTask
);
180 if ( ! voteRunning
|| (voteType
== null ) )
182 plugin
.getLogger().info("No vote running, but I was told to apply one. ERRR");
186 List
<World
> worlds
= plugin
.getServer().getWorlds();
187 for ( World w
: worlds
)
189 if ( voteType
.equalsIgnoreCase("sun"))
191 else if ( voteType
.equalsIgnoreCase("rain"))
193 else if ( voteType
.equalsIgnoreCase("storm"))
194 w
.setThundering(true);
195 else if ( voteType
.equalsIgnoreCase("day"))
196 w
.setTime( plugin
.getConfig().getInt("vote.time.dayStart") );
197 else if ( voteType
.equalsIgnoreCase("night"))
198 w
.setTime( plugin
.getConfig().getInt("vote.time.nightStart") );
200 plugin
.getServer().broadcastMessage("Vote succeeded for " + ((voteType
!= null) ? voteType
: " .. uhm *blush*"));
204 private float getRequired( )
207 if ( voteType
.equalsIgnoreCase("sun") || voteType
.equalsIgnoreCase("rain") || voteType
.equalsIgnoreCase("storm") )
208 req
= (float)plugin
.getConfig().getInt("vote.weather.requiredPercent") / 100.0F
;
209 else if ( voteType
.equalsIgnoreCase("day") || voteType
.equalsIgnoreCase("night") )
210 req
= (float)plugin
.getConfig().getInt("vote.time.requiredPercent") / 100.0F
;
216 // Original approach...
217 private boolean checkRatio( )
219 float required = getRequired();
220 float ratio = (float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length;
221 if ( ratio >= required ) return true;
226 private boolean checkRatio( )
228 if ( ((float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length ) >= getRequired() )
233 // But, trying to do it inline instead
236 private void clearState()