]>
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 ( ((float) yesVoters
.size() / (float) plugin
.getServer().getOnlinePlayers().length
) >= getRequired() )
69 applyVote( voteType
);
73 expireTask
= plugin
.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin
, new Runnable(){
79 plugin
.getServer().broadcastMessage("Time expired for voting on " + ((voteType
!= null) ? voteType
: " .. uhm *blush*"));
84 plugin
.getLogger().info("Timer expired but vote was not running :S");
88 }, plugin
.getConfig().getLong("vote.timeoutSeconds")*20L );
90 plugin
.getServer().broadcastMessage("A vote has started for " + voteType
.toLowerCase() );
94 public boolean isVoteRunning()
99 public String
getVoteType ()
104 public void removeVote(Player player
)
106 if ( ! voteRunning
|| (voteType
== null ) )
109 if ( yesVoters
.contains(player
.getName()) )
110 yesVoters
.remove(player
.getName());
112 if ( noVoters
.contains(player
.getName()) )
113 noVoters
.remove(player
.getName());
115 if ( yesVoters
.size() + noVoters
.size() == 0 )
118 else if ( ((float) yesVoters
.size() / (float) (plugin
.getServer().getOnlinePlayers().length
- 1) ) >= getRequired() )
119 applyVote( voteType
);
122 public boolean addVote(Player player
, boolean yes
)
124 if ( ! voteRunning
|| (voteType
== null ) )
127 if( yesVoters
.contains(player
.getName()) || noVoters
.contains(player
.getName()) )
129 player
.sendMessage("You have already cast your vote");
132 if (yes
== true) yesVoters
.add(player
.getName());
133 else noVoters
.add(player
.getName());
135 if ( ( yes
== true) && ( ((float) yesVoters
.size() / (float) plugin
.getServer().getOnlinePlayers().length
) >= getRequired() ) )
137 applyVote( voteType
);
142 if ( yesVoters
.size() + noVoters
.size() >= plugin
.getServer().getOnlinePlayers().length
)
144 plugin
.getServer().broadcastMessage("Vote failed for " + voteType
);
145 plugin
.getServer().getScheduler().cancelTask(expireTask
);
149 player
.sendMessage("Vote counted. Thank you.");
154 private void applyVote(String type
)
156 plugin
.getServer().getScheduler().cancelTask(expireTask
);
157 if ( ! voteRunning
|| (voteType
== null ) )
159 plugin
.getLogger().info("No vote running, but I was told to apply one. ERRR");
163 List
<World
> worlds
= plugin
.getServer().getWorlds();
164 for ( World w
: worlds
)
166 if ( voteType
.equalsIgnoreCase("sun"))
168 else if ( voteType
.equalsIgnoreCase("rain"))
170 else if ( voteType
.equalsIgnoreCase("storm"))
171 w
.setThundering(true);
172 else if ( voteType
.equalsIgnoreCase("day"))
173 w
.setTime( plugin
.getConfig().getInt("vote.time.dayStart") );
174 else if ( voteType
.equalsIgnoreCase("night"))
175 w
.setTime( plugin
.getConfig().getInt("vote.time.nightStart") );
177 plugin
.getServer().broadcastMessage("Vote succeeded for " + ((voteType
!= null) ? voteType
: " .. uhm *blush*"));
181 private float getRequired( )
184 if ( voteType
.equalsIgnoreCase("sun") || voteType
.equalsIgnoreCase("rain") || voteType
.equalsIgnoreCase("storm") )
185 req
= (float)plugin
.getConfig().getInt("vote.weather.requiredPercent") / 100.0F
;
186 else if ( voteType
.equalsIgnoreCase("day") || voteType
.equalsIgnoreCase("night") )
187 req
= (float)plugin
.getConfig().getInt("vote.time.requiredPercent") / 100.0F
;
192 private void clearState()