-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRpsManager.java
86 lines (74 loc) · 3.09 KB
/
RpsManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package me.nikl.mpgamebundle.rockpaperscissors;
import me.nikl.gamebox.data.toplist.SaveType;
import me.nikl.gamebox.game.exceptions.GameStartException;
import me.nikl.gamebox.game.manager.EasyManager;
import me.nikl.gamebox.game.rules.GameRule;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* @author Niklas Eicker
*/
public class RpsManager extends EasyManager {
private RockPaperScissors rockPaperScissors;
private RpsLanguage rpsLanguage;
private Map<String, RpsRules> gameTypes = new HashMap<>();
private Map<UUID, RpsGame> games = new HashMap<>();
public RpsManager(RockPaperScissors rockPaperScissors) {
this.rockPaperScissors = rockPaperScissors;
this.rpsLanguage = (RpsLanguage) rockPaperScissors.getGameLang();
}
@Override
public boolean isInGame(UUID uuid) {
return games.keySet().contains(uuid);
}
@Override
public void startGame(Player[] players, boolean b, String... strings) throws GameStartException {
RpsRules rule = gameTypes.get(strings[0]);
if (rule == null) {
throw new GameStartException(GameStartException.Reason.ERROR);
}
rockPaperScissors.payIfNecessary(players, rule.getCost());
RpsGame game = new RpsGame(rockPaperScissors, rule, players[0], players[1]);
games.put(players[1].getUniqueId(), game);
games.put(players[0].getUniqueId(), game);
}
@Override
public void removeFromGame(UUID uuid) {
RpsGame game = games.get(uuid);
if (game == null) return;
game.onClose(uuid);
games.remove(uuid);
}
@Override
public void loadGameRules(ConfigurationSection buttonSec, String buttonID) {
double cost = buttonSec.getDouble("cost", 0.);
double reward = buttonSec.getDouble("reward", 0.);
int tokens = buttonSec.getInt("tokens", 0);
boolean saveStats = buttonSec.getBoolean("saveStats", false);
boolean loseOnTimeOver = true;
int rounds = buttonSec.getInt("rounds", 5);
RpsRules rules = new RpsRules(buttonID, saveStats, SaveType.WINS, cost, reward, tokens, loseOnTimeOver, rounds);
rules.setWaitTime(buttonSec.getInt("waitPerTurn", 5) * 1000); // seconds to milliseconds
rules.setChooseTime(buttonSec.getInt("timePerTurn", 10) * 1000); // seconds to milliseconds
gameTypes.put(buttonID, rules);
}
@Override
public Map<String, ? extends GameRule> getGameRules() {
return gameTypes;
}
@Override
public void onInventoryClick(InventoryClickEvent inventoryClickEvent) {
RpsGame game = games.get(inventoryClickEvent.getWhoClicked().getUniqueId());
if (game == null) return;
game.onClick(inventoryClickEvent);
}
@Override
public void onInventoryClose(InventoryCloseEvent inventoryCloseEvent) {
removeFromGame(inventoryCloseEvent.getPlayer().getUniqueId());
}
}