]>
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("day") ||
45 type
.equalsIgnoreCase("night") ))
47 plugin
.getLogger().info("Tried to start a vote of invalid type. Code error!");
54 if ( yesVoters
== null )
55 yesVoters
= new ArrayList
<String
>();
59 if ( noVoters
== null )
60 noVoters
= new ArrayList
<String
>();
64 yesVoters
.add(p
.getName());
68 applyVote( voteType
);
72 expireTask
= plugin
.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin
, new Runnable(){
78 plugin
.getServer().broadcastMessage("Time expired for voting on " + ((voteType
!= null) ? voteType
: " .. uhm *blush*"));
83 plugin
.getLogger().info("Timer expired but vote was not running :S");
87 }, plugin
.getConfig().getLong("vote.timeoutSeconds")*20L );
89 plugin
.getServer().broadcastMessage("A vote has started for " + voteType
.toLowerCase() );
93 public boolean isVoteRunning()
98 public String
getVoteType ()
103 public boolean addVote(Player player
, boolean yes
)
105 if ( ! voteRunning
|| (voteType
== null ) )
108 if( yesVoters
.contains(player
.getName()) || noVoters
.contains(player
.getName()) )
110 player
.sendMessage("You have already cast your vote");
113 if (yes
== true) yesVoters
.add(player
.getName());
114 else noVoters
.add(player
.getName());
116 if ( (yes
== true) && checkRatio( ) )
118 applyVote( voteType
);
123 if ( yesVoters
.size() + noVoters
.size() >= plugin
.getServer().getOnlinePlayers().length
)
125 plugin
.getServer().broadcastMessage("Vote failed for " + voteType
);
126 plugin
.getServer().getScheduler().cancelTask(expireTask
);
130 player
.sendMessage("Vote counted. Thank you.");
135 private void applyVote(String type
)
137 plugin
.getServer().getScheduler().cancelTask(expireTask
);
138 if ( ! voteRunning
|| (voteType
== null ) )
140 plugin
.getLogger().info("No vote running, but I was told to apply one. ERRR");
144 List
<World
> worlds
= plugin
.getServer().getWorlds();
145 for ( World w
: worlds
)
147 if ( voteType
.equalsIgnoreCase("sun"))
149 else if ( voteType
.equalsIgnoreCase("rain"))
151 else if ( voteType
.equalsIgnoreCase("day"))
152 w
.setTime( plugin
.getConfig().getInt("vote.time.dayStart") );
153 else if ( voteType
.equalsIgnoreCase("night"))
154 w
.setTime( plugin
.getConfig().getInt("vote.time.nightStart") );
156 plugin
.getServer().broadcastMessage("Vote succeeded for " + ((voteType
!= null) ? voteType
: " .. uhm *blush*"));
160 private boolean checkRatio( )
162 float required
= 0.5F
;
163 if ( voteType
.equalsIgnoreCase("sun") || voteType
.equalsIgnoreCase("rain") )
164 required
= (float)plugin
.getConfig().getInt("vote.weather.requiredPercent") / 100.0F
;
165 else if ( voteType
.equalsIgnoreCase("day") || voteType
.equalsIgnoreCase("night") )
166 required
= (float)plugin
.getConfig().getInt("vote.time.requiredPercent") / 100.0F
;
168 float ratio
= (float) yesVoters
.size() / (float) plugin
.getServer().getOnlinePlayers().length
;
169 if ( ratio
> required
) return true;
173 private void clearState()