-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTicTacToe.java
189 lines (165 loc) · 5.98 KB
/
TicTacToe.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
package me.nikl.mpgamebundle.tictactoe;
import me.nikl.gamebox.GameBox;
import me.nikl.gamebox.game.Game;
import me.nikl.gamebox.game.GameSettings;
import me.nikl.gamebox.utility.ItemStackUtility;
import me.nikl.mpgamebundle.GameBundle;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* @author Niklas Eicker
*/
public class TicTacToe extends Game {
private List<ItemStack> markers = new ArrayList<>();
private Random random = new Random();
private ItemStack sheetInnerItem;
private ItemStack sheetBorderItem;
private ItemStack npcHead;
public TicTacToe(GameBox gameBox, String gameID) {
super(gameBox, gameID);
}
public TicTacToe(GameBox gameBox) {
super(gameBox, GameBundle.TIC_TAC_TOE);
}
@Override
public void onDisable() {
}
private void setDefaultSheetBorderItem() {
ItemMeta meta;
sheetBorderItem = ItemStackUtility.getItemStack("paper");
sheetBorderItem.setAmount(1);
meta = sheetBorderItem.getItemMeta();
meta.setDisplayName("");
sheetBorderItem.setItemMeta(meta);
}
private void setDefaultSheetInnerItem() {
ItemMeta meta;
sheetInnerItem = ItemStackUtility.getItemStack("paper");
sheetInnerItem.setAmount(1);
meta = sheetInnerItem.getItemMeta();
meta.setDisplayName("Click to draw");
sheetInnerItem.setItemMeta(meta);
}
@Override
public void init() {
loadSheetItems();
loadMarkers();
npcHead = ItemStackUtility.getPlayerHead("MHF_Villager").clone();
ItemMeta meta = npcHead.getItemMeta();
meta.setLore(new ArrayList<>());
npcHead.setItemMeta(meta);
}
private void loadSheetItems() {
if (!config.isConfigurationSection("sheetBorder")) {
info("No 'sheetBorder' section found. Loading default...");
setDefaultSheetBorderItem();
} else {
sheetBorderItem = ItemStackUtility.loadItem(config.getConfigurationSection("sheetBorder"));
if (sheetBorderItem == null) {
info("Invalid 'sheetBorder' section found. Loading default...");
setDefaultSheetBorderItem();
}
}
if (!config.isConfigurationSection("sheetInner")) {
info("No 'sheetInner' section found. Loading default...");
setDefaultSheetInnerItem();
} else {
sheetInnerItem = ItemStackUtility.loadItem(config.getConfigurationSection("sheetInner"));
if (sheetInnerItem == null) {
info("Invalid 'sheetInner' section found. Loading default...");
setDefaultSheetInnerItem();
}
}
}
private void loadMarkers() {
if (!config.isConfigurationSection("markers")) {
info("No 'markers' section found. Loading default...");
loadDefaultMarkers();
return;
}
ConfigurationSection markersSection = config.getConfigurationSection("markers");
for (String key : markersSection.getKeys(false)) {
if (!markersSection.isConfigurationSection(key)) continue;
ItemStack marker = ItemStackUtility.loadItem(markersSection.getConfigurationSection(key));
if (marker == null) {
info("Invalid marker '" + key + "' found. Skipping...");
continue;
}
markers.add(marker);
}
if (markers.size() < 2) {
info("Less then two valid markers found. Loading default...");
loadDefaultMarkers();
}
}
private void loadDefaultMarkers() {
// ToDo: load default from the default config file
ItemStack markerOne = new ItemStack(Material.EMERALD, 1);
ItemMeta meta = markerOne.getItemMeta();
meta.setDisplayName(ChatColor.DARK_AQUA + "%player%'s mark");
markerOne.setItemMeta(meta);
ItemStack markerTwo = new ItemStack(Material.DIAMOND, 1);
meta = markerTwo.getItemMeta();
meta.setDisplayName(ChatColor.DARK_AQUA + "%player%'s mark");
markerTwo.setItemMeta(meta);
markers.add(markerOne);
markers.add(markerTwo);
}
@Override
public void loadSettings() {
gameSettings.setGameType(GameSettings.GameType.TWO_PLAYER);
}
@Override
public void loadLanguage() {
gameLang = new TttLanguage(this, true);
}
@Override
public void loadGameManager() {
gameManager = new TttManager(this);
}
public MarkerPair getRandomMarkerPair(String nameOne, String nameTwo) {
int indexOne = random.nextInt(markers.size());
int indexTwo = random.nextInt(markers.size());
while (indexOne == indexTwo) {
indexTwo = random.nextInt(markers.size());
}
ItemStack one = markers.get(indexOne).clone();
ItemMeta meta = one.getItemMeta();
meta.setDisplayName(meta.getDisplayName().replace("%player%", nameOne));
one.setItemMeta(meta);
ItemStack two = markers.get(indexTwo).clone();
meta = two.getItemMeta();
meta.setDisplayName(meta.getDisplayName().replace("%player%", nameTwo));
two.setItemMeta(meta);
return new MarkerPair(one, two);
}
public ItemStack getSheetBorderItem() {
return sheetBorderItem;
}
public ItemStack getSheetInnerItem() {
return sheetInnerItem;
}
public ItemStack getNpcHead() {
return npcHead;
}
public class MarkerPair {
private ItemStack one;
private ItemStack two;
public MarkerPair(ItemStack one, ItemStack two) {
this.one = one;
this.two = two;
}
public ItemStack getOne() {
return this.one;
}
public ItemStack getTwo() {
return this.two;
}
}
}