-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRpsGame.java
385 lines (356 loc) · 15.7 KB
/
RpsGame.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package me.nikl.mpgamebundle.rockpaperscissors;
import me.nikl.gamebox.GameBoxSettings;
import me.nikl.gamebox.nms.NmsFactory;
import me.nikl.gamebox.nms.NmsUtility;
import me.nikl.gamebox.utility.ItemStackUtility;
import me.nikl.gamebox.utility.Permission;
import me.nikl.gamebox.utility.Sound;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
/**
* @author Niklas Eicker
*/
public class RpsGame {
protected static RockPaperScissors rockPaperScissors;
private Player playerOne;
private Player playerTwo;
private RpsRules rules;
private Random random = new Random();
private boolean firstTurn = true;
protected Inventory inventory;
protected RpsLanguage language;
private int numberOfRounds;
private int winsOne = 0, winsTwo = 0;
private long stateEndTimeStamp;
private Status status = Status.CHOOSE;
private ChosenIcon firstIcon, secondIcon;
private int chooseTime = 10000;
private int waitTime = 5000;
private String currentFirstTitle, currentSecondTitle;
private NmsUtility nmsUtility = NmsFactory.getNmsUtility();
private boolean firstPlaySounds, secondPlaySounds;
private Sound wonSound = Sound.LEVEL_UP, lostSound = Sound.VILLAGER_NO, drawSound = Sound.VILLAGER_IDLE;
private GameTimer timer = new GameTimer(this);
// animation
private List<Integer> animationSteps = new ArrayList<>();
private int currentAnimationStep = -1;
public RpsGame(RockPaperScissors rockPaperScissors, RpsRules rule, Player player, Player player1) {
this.rules = rule;
waitTime = rules.getWaitTime();
chooseTime = rules.getChooseTime();
RpsGame.rockPaperScissors = rockPaperScissors;
this.inventory = rockPaperScissors.createInventory(54, "");
this.playerOne = player;
this.playerTwo = player1;
this.language = (RpsLanguage) rockPaperScissors.getGameLang();
this.numberOfRounds = rule.getNumberOfRounds();
firstPlaySounds = rockPaperScissors.getGameBox().getPluginManager().getPlayer(playerOne.getUniqueId()).isPlaySounds() && GameBoxSettings.playSounds;
secondPlaySounds = rockPaperScissors.getGameBox().getPluginManager().getPlayer(playerTwo.getUniqueId()).isPlaySounds() && GameBoxSettings.playSounds;
prepareInventory();
if (random.nextDouble() < 0.5) {
firstTurn = false;
}
stateEndTimeStamp = System.currentTimeMillis() + chooseTime;
currentFirstTitle = language.TITLE_CHOOSE;
currentSecondTitle = language.TITLE_CHOOSE;
playerOne.openInventory(inventory);
playerTwo.openInventory(inventory);
updateTitle();
timer.runTaskTimer(rockPaperScissors.getGameBox(), 4, 4);
}
private void prepareInventory() {
setHeads();
placeIndicators();
toggleIconSelection(true);
}
private void toggleIconSelection(boolean show) {
if (show) {
inventory.setItem(30, rockPaperScissors.getRock());
inventory.setItem(31, rockPaperScissors.getPaper());
inventory.setItem(32, rockPaperScissors.getScissors());
} else {
inventory.setItem(30, null);
inventory.setItem(31, null);
inventory.setItem(32, null);
}
}
private void placeIndicators() {
for (int row = 1; row < numberOfRounds/2 + numberOfRounds%2 + 1; row ++) {
inventory.setItem(row * 9 + 1, rockPaperScissors.getIndicatorPlaceholder());
inventory.setItem(row * 9 + 7, rockPaperScissors.getIndicatorPlaceholder());
}
}
private void updateTitle() {
if (playerTwo == null || playerOne == null) return;
String time = String.valueOf(((stateEndTimeStamp - System.currentTimeMillis())/1000)+1);
nmsUtility.updateInventoryTitle(playerTwo, currentSecondTitle.replace("%time%", time));
nmsUtility.updateInventoryTitle(playerOne, currentFirstTitle.replace("%time%", time));
}
protected void setHeads() {
ItemStack head = ItemStackUtility.getPlayerHead(playerOne.getName()).clone();
ItemStack headTwo = ItemStackUtility.getPlayerHead(playerTwo.getName()).clone();
ItemMeta meta = head.getItemMeta();
meta.setDisplayName(language.GAME_PLAYER_HEAD_NAME.replace("%player%", playerOne.getName()));
meta.setLore(new ArrayList<>());
head.setItemMeta(meta);
meta = headTwo.getItemMeta();
meta.setDisplayName(language.GAME_PLAYER_HEAD_NAME.replace("%player%", playerTwo.getName()));
meta.setLore(new ArrayList<>());
headTwo.setItemMeta(meta);
inventory.setItem(1, head);
inventory.setItem(7, headTwo);
}
public void onClick(InventoryClickEvent event) {
if (status != Status.CHOOSE) return;
if (event.getRawSlot() < 30 || event.getRawSlot() > 32) return;
boolean firstPlayer = event.getWhoClicked().getUniqueId().equals(playerOne.getUniqueId());
if ((firstPlayer && firstIcon != null) || (!firstPlayer && secondIcon != null)) return;
if (firstPlayer) {
firstIcon = ChosenIcon.fromSlot(event.getRawSlot());
} else {
secondIcon = ChosenIcon.fromSlot(event.getRawSlot());
}
String time = String.valueOf((stateEndTimeStamp - System.currentTimeMillis())/1000);
if (firstIcon != null && secondIcon != null) {
status = Status.WAIT;
toggleIconSelection(false);
displayChosenIcons();
stateEndTimeStamp = System.currentTimeMillis() + waitTime;
if (isDraw()) {
playSound(drawSound);
currentFirstTitle = language.TITLE_NEXT_ROUND_DRAW;
currentSecondTitle = language.TITLE_NEXT_ROUND_DRAW;
updateTitle();
firstIcon = null;
secondIcon = null;
return;
} else {
currentFirstTitle = language.TITLE_NEXT_ROUND;
currentSecondTitle = language.TITLE_NEXT_ROUND;
updateTitle();
}
if (firstWon()) {
winsOne ++;
animateWin(true);
playSound(true, wonSound);
playSound(false, lostSound);
} else {
winsTwo ++;
animateWin(false);
playSound(true, lostSound);
playSound(false, wonSound);
}
if (isGameOver()) {
onGameOver();
}
firstIcon = null;
secondIcon = null;
} else {
if (firstPlayer) {
currentFirstTitle = language.TITLE_WAIT;
} else {
currentSecondTitle = language.TITLE_WAIT;
}
updateTitle();
}
}
private boolean isGameOver() {
return (winsTwo + winsOne) == numberOfRounds ||
winsTwo == numberOfRounds - numberOfRounds/2 ||
winsOne == numberOfRounds - numberOfRounds/2;
}
private void onGameOver() {
status = Status.OVER;
if (winsOne == winsTwo) {
onDraw();
return;
}
boolean firstWon = winsOne>winsTwo;
Player winner = firstWon?playerOne:playerTwo;
Player loser = firstWon?playerTwo:playerOne;
onGameWon(winner);
nmsUtility.updateInventoryTitle(firstWon?playerTwo:playerOne, language.TITLE_LOST);
if (rockPaperScissors.getSettings().isEconEnabled()
&& rules.getMoneyToWin() > 0
&& !Permission.BYPASS_GAME.hasPermission(winner, rockPaperScissors.getGameID())) {
winner.sendMessage(language.PREFIX + language.GAME_WON_MONEY.replace("%reward%", String.valueOf(rules.getMoneyToWin())));
} else {
winner.sendMessage(language.PREFIX + language.GAME_WON);
}
loser.sendMessage(language.PREFIX + language.GAME_LOSE);
}
private void onGameWon(Player winner) {
nmsUtility.updateInventoryTitle(winner, language.TITLE_WON);
rockPaperScissors.onGameWon(winner, rules, 1);
}
private void onDraw() {
nmsUtility.updateInventoryTitle(playerTwo, language.TITLE_DRAW);
nmsUtility.updateInventoryTitle(playerOne, language.TITLE_DRAW);
}
protected void tick() {
if (!animationSteps.isEmpty()) {
if (currentAnimationStep > 0) inventory.setItem(currentAnimationStep, null);
currentAnimationStep = animationSteps.remove(animationSteps.size() - 1);
inventory.setItem(currentAnimationStep, rockPaperScissors.getIndicatorWon());
if (animationSteps.isEmpty()) currentAnimationStep = -1;
} else {
if (status == Status.OVER) timer.cancel();
}
if (status == Status.OVER) return;
updateTitle();
if (status == Status.WAIT && System.currentTimeMillis() > stateEndTimeStamp) {
status = Status.CHOOSE;
inventory.setItem(21, null);
inventory.setItem(23, null);
stateEndTimeStamp = System.currentTimeMillis() + chooseTime;
currentFirstTitle = language.TITLE_CHOOSE;
currentSecondTitle = language.TITLE_CHOOSE;
toggleIconSelection(true);
return;
}
if (status == Status.CHOOSE && System.currentTimeMillis() > stateEndTimeStamp) {
onTimeRanOut();
}
}
private void onTimeRanOut() {
status = Status.OVER;
if (firstIcon == null && secondIcon != null) {
currentFirstTitle = language.TITLE_LOST;
currentSecondTitle = language.TITLE_WON;
if (rockPaperScissors.getSettings().isEconEnabled()
&& rules.getMoneyToWin() > 0
&& !Permission.BYPASS_GAME.hasPermission(playerTwo, rockPaperScissors.getGameID())) {
playerTwo.sendMessage(language.PREFIX + language.GAME_WON_MONEY_TOO_SLOW.replace("%reward%", String.valueOf(rules.getMoneyToWin())).replace("%loser%", playerOne.getName()));
} else {
playerTwo.sendMessage(language.PREFIX + language.GAME_WON_TOO_SLOW.replace("%loser%", playerOne.getName()));
}
playerOne.sendMessage(language.PREFIX + language.GAME_TOO_SLOW);
onGameWon(playerTwo);
} else if (firstIcon != null && secondIcon == null) {
currentFirstTitle = language.TITLE_WON;
currentSecondTitle = language.TITLE_LOST;
if (rockPaperScissors.getSettings().isEconEnabled()
&& rules.getMoneyToWin() > 0
&& !Permission.BYPASS_GAME.hasPermission(playerOne, rockPaperScissors.getGameID())) {
playerOne.sendMessage(language.PREFIX + language.GAME_WON_MONEY_TOO_SLOW.replace("%reward%", String.valueOf(rules.getMoneyToWin())).replace("%loser%", playerTwo.getName()));
} else {
playerOne.sendMessage(language.PREFIX + language.GAME_WON_TOO_SLOW.replace("%loser%", playerTwo.getName()));
}
playerTwo.sendMessage(language.PREFIX + language.GAME_TOO_SLOW);
onGameWon(playerOne);
} else {
currentFirstTitle = language.TITLE_LOST;
currentSecondTitle = language.TITLE_LOST;
playerTwo.sendMessage(language.PREFIX + language.GAME_TOO_SLOW);
playerOne.sendMessage(language.PREFIX + language.GAME_TOO_SLOW);
}
updateTitle();
}
private void animateWin(boolean firstWon) {
int destinationSlot = (firstWon?1:7) + 9*(firstWon?winsOne:winsTwo);
if (destinationSlot > inventory.getSize()) {
return;
}
animationSteps.clear();
animationSteps.add(destinationSlot);
if (destinationSlot/9 < 3) {
animationSteps.add(firstWon?20:24);
} else if (destinationSlot/9 == 3) {
animationSteps.add(firstWon?29:33);
} else {
animationSteps.add(firstWon?38:42);
}
animationSteps.add(31 + (firstWon?-1:1));
animationSteps.add(31);
animationSteps.add(22);
}
private boolean firstWon() {
return firstIcon.winsAgainst(secondIcon);
}
private boolean isDraw() {
return firstIcon == secondIcon;
}
private void displayChosenIcons() {
inventory.setItem(21, firstIcon.getItem());
inventory.setItem(23, secondIcon.getItem());
}
public void onClose(UUID uuid) {
if (status == Status.OVER || playerTwo == null || playerOne == null) return;
boolean firstQuit = uuid.equals(playerOne.getUniqueId());
if (firstQuit) {
if (rockPaperScissors.getSettings().isEconEnabled()
&& rules.getMoneyToWin() > 0
&& !Permission.BYPASS_GAME.hasPermission(playerTwo, rockPaperScissors.getGameID())) {
playerTwo.sendMessage(language.PREFIX + language.GAME_WON_MONEY_GAVE_UP.replace("%loser%", playerOne.getName()).replace("%reward%", String.valueOf(rules.getMoneyToWin())));
} else {
playerTwo.sendMessage(language.PREFIX + language.GAME_OTHER_GAVE_UP.replace("%loser%", playerOne.getName()));
}
playerOne.sendMessage(language.PREFIX + language.GAME_GAVE_UP);
playerOne = null;
} else {
if (rockPaperScissors.getSettings().isEconEnabled()
&& rules.getMoneyToWin() > 0
&& !Permission.BYPASS_GAME.hasPermission(playerOne, rockPaperScissors.getGameID())) {
playerOne.sendMessage(language.PREFIX + language.GAME_WON_MONEY_GAVE_UP.replace("%loser%", playerTwo.getName()).replace("%reward%", String.valueOf(rules.getMoneyToWin())));
} else {
playerOne.sendMessage(language.PREFIX + language.GAME_OTHER_GAVE_UP.replace("%loser%", playerTwo.getName()));
}
playerTwo.sendMessage(language.PREFIX + language.GAME_GAVE_UP);
playerTwo = null;
}
onGameWon(firstQuit?playerTwo:playerOne);
status = Status.OVER;
}
private enum Status {
CHOOSE, WAIT, OVER
}
private enum ChosenIcon {
PAPER, ROCK, SCISSORS;
public static ChosenIcon fromSlot(int slot) {
if (slot == 30) return ROCK;
if (slot == 31) return PAPER;
if (slot == 32) return SCISSORS;
throw new IllegalArgumentException("Passed slot does not contain an icon!");
}
public ItemStack getItem() {
switch (this) {
case ROCK:
return rockPaperScissors.getRock();
case PAPER:
return rockPaperScissors.getPaper();
case SCISSORS:
return rockPaperScissors.getScissors();
default:
throw new RuntimeException("Unknown item");
}
}
public boolean winsAgainst(ChosenIcon icon) {
switch (icon) {
case SCISSORS:
return this == ROCK;
case PAPER:
return this == SCISSORS;
case ROCK:
return this == PAPER;
}
return false;
}
}
private void playSound(boolean first, Sound sound) {
if (!(first?firstPlaySounds:secondPlaySounds)) return;
Player player = first?playerOne:playerTwo;
player.playSound(player.getLocation(), sound.bukkitSound(), 0.5f, 0.5f);
}
private void playSound(Sound sound) {
playSound(true, sound);
playSound(false, sound);
}
}