/
SlotData.java
306 lines (236 loc) · 8.24 KB
/
SlotData.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
package com.craftyn.casinoslots.slot;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import com.craftyn.casinoslots.CasinoSlots;
public class SlotData {
private CasinoSlots plugin;
private HashMap<String, SlotMachine> slots = new HashMap<String, SlotMachine>();
public HashMap<Player, SlotMachine> creatingSlots = new HashMap<Player, SlotMachine>();
public HashMap<Player, SlotMachine> placingController = new HashMap<Player, SlotMachine>();
// Initialize SlotData
public SlotData(CasinoSlots plugin) {
this.plugin = plugin;
}
// Returns a slot machine
public SlotMachine getSlot(String name) {
return this.slots.get(name);
}
// Returns collection of all slot machines
public Collection<SlotMachine> getSlots() {
return this.slots.values();
}
/**
* Returns the amount of slots there are.
*
* @return The number of slots.
*/
public int getAmountofSlots() {
return this.slots.size();
}
// Registers a new slot machine
public void addSlot(SlotMachine slot) {
String name = slot.getName();
this.slots.put(name, slot);
}
// Returns true if slot machine exists
public Boolean isSlot(String name) {
if(this.slots.containsKey(name)) {
return true;
}
return false;
}
// Removes a slot machine
public void removeSlot(SlotMachine slot) {
this.slots.remove(slot.getName());
for(Block b : slot.getBlocks()) {
b.setTypeId(0);
}
slot.getController().setTypeId(0);
plugin.configData.slots.set("slots." + slot.getName(), null);
plugin.configData.saveSlots();
}
// Loads all slot machines into memory
public void loadSlots() {
Integer i = 0;
this.slots = new HashMap<String, SlotMachine>();
if(plugin.configData.slots.isConfigurationSection("slots")) {
Set<String> slots = plugin.configData.slots.getConfigurationSection("slots").getKeys(false);
if(!slots.isEmpty()) {
for(String name : slots) {
loadSlot(name);
i++;
}
}
}
plugin.log("Loaded " + i + " slot machines.");
}
// Writes slot machine data to disk
public void saveSlot(SlotMachine slot) {
String path = "slots." + slot.getName() + ".";
ArrayList<String> xyz = new ArrayList<String>();
for(Block b : slot.getBlocks()) {
xyz.add(b.getX() + "," + b.getY() + "," + b.getZ());
}
Block con = slot.getController();
String cXyz = con.getX() + "," + con.getY() + "," + con.getZ();
plugin.configData.slots.set(path + "name", slot.getName());
plugin.configData.slots.set(path + "type", slot.getType());
plugin.configData.slots.set(path + "owner", slot.getOwner());
plugin.configData.slots.set(path + "world", slot.getWorld());
plugin.configData.slots.set(path + "managed", slot.isManaged());
plugin.configData.slots.set(path + "funds", slot.getFunds());
plugin.configData.slots.set(path + "item", slot.isItem());
plugin.configData.slots.set(path + "itemID", slot.getItem());
plugin.configData.slots.set(path + "itemAmt", slot.getItemAmount());
plugin.configData.slots.set(path + "controller", cXyz);
plugin.configData.slots.set(path + "location", xyz);
plugin.configData.saveSlots();
}
// Loads a slot machine into memory
private void loadSlot(String name) {
String path = "slots." + name + ".";
String type = plugin.configData.slots.getString(path + "type");
String owner = plugin.configData.slots.getString(path + "owner");
String world = plugin.configData.slots.getString(path + "world");
Boolean managed = plugin.configData.slots.getBoolean(path + "managed");
Double funds = plugin.configData.slots.getDouble(path + "funds");
Boolean item = plugin.configData.slots.getBoolean(path + "item", false);
int itemID = plugin.configData.slots.getInt(path + "itemID", 0);
int itemAmt = plugin.configData.slots.getInt(path + "itemAmt", 0);
ArrayList<Block> blocks = getBlocks(name);
Block controller = getController(name);
//Get the chunks
String rChunk = getRchunk(blocks);
String cChunk = getCchunk(controller);
SlotMachine slot = new SlotMachine(name, type, owner, world, rChunk, cChunk, managed, blocks, controller, funds, item, itemID, itemAmt);
addSlot(slot);
}
// Gets reel blocks location from disk
private ArrayList<Block> getBlocks(String name) {
List<String> xyz = plugin.configData.slots.getStringList("slots." + name + ".location");
ArrayList<Block> blocks = new ArrayList<Block>();
World world = Bukkit.getWorld(plugin.configData.slots.getString("slots." + name + ".world", "world"));
if (world == null) {
plugin.error("The world for the slot '" + name + "' was null, please fix this and restart the server.");
plugin.disablePlugin();
}
for(String coord : xyz) {
String[] b = coord.split("\\,");
Location loc = new Location(world, Integer.parseInt(b[0]), Integer.parseInt(b[1]), Integer.parseInt(b[2]));
blocks.add(loc.getBlock());
loc.getChunk().load();
}
return blocks;
}
// Gets controller block from disk
private Block getController(String name) {
String location = plugin.configData.slots.getString("slots." + name + ".controller");
World world = Bukkit.getWorld(plugin.configData.slots.getString("slots." + name + ".world"));
String[] b = location.split("\\,");
Location loc = new Location(world, Integer.parseInt(b[0]), Integer.parseInt(b[1]), Integer.parseInt(b[2]));
Block controller = loc.getBlock();
return controller;
}
private String getRchunk(ArrayList<Block> blocks) {
Block b = blocks.get(1);
String c = b.getChunk().getX() + "," + b.getChunk().getZ();
return c;
}
private String getCchunk (Block c) {
String chunk = c.getChunk().getX() + "," + c.getChunk().getZ();
return chunk;
}
// Creates the slot machine in the world
public void createReel(Player player, BlockFace face, SlotMachine slot) {
Block center = player.getTargetBlock(null, 0);
ArrayList<Block> blocks = new ArrayList<Block>();
slot.setReelChunk(center.getChunk().getX() + "," + center.getChunk().getZ());
for(int i = 0; i < 3; i++) {
blocks.add(center.getRelative(getDirection(face, "left"), 2));
blocks.add(center);
blocks.add(center.getRelative(getDirection(face, "right"), 2));
center = center.getRelative(BlockFace.UP, 1);
}
for(Block b : blocks) {
b.setTypeId(57);
}
slot.setBlocks(blocks);
}
// Used for orienting the slot machine correctly
public BlockFace getDirection(BlockFace face, String direction) {
if(face == BlockFace.NORTH) {
if(direction.equalsIgnoreCase("left")) {
return BlockFace.EAST;
}
else if(direction.equalsIgnoreCase("right")) {
return BlockFace.WEST;
}
}
else if(face == BlockFace.SOUTH) {
if(direction.equalsIgnoreCase("left")) {
return BlockFace.WEST;
}
else if(direction.equalsIgnoreCase("right")) {
return BlockFace.EAST;
}
}
else if(face == BlockFace.WEST) {
if(direction.equalsIgnoreCase("left")) {
return BlockFace.SOUTH;
}
else if(direction.equalsIgnoreCase("right")) {
return BlockFace.NORTH;
}
}
else if(face == BlockFace.EAST) {
if(direction.equalsIgnoreCase("left")) {
return BlockFace.NORTH;
}
else if(direction.equalsIgnoreCase("right")) {
return BlockFace.SOUTH;
}
}
return BlockFace.SELF;
}
// If a player is creating slot machine
public Boolean isCreatingSlots(Player player) {
if(creatingSlots.containsKey(player)) {
return true;
}
return false;
}
// If a player is placing controller
public Boolean isPlacingController(Player player) {
if(placingController.containsKey(player)) {
return true;
}
return false;
}
// Toggles creating slots
public void toggleCreatingSlots(Player player, SlotMachine slot) {
if(this.creatingSlots.containsKey(player)) {
this.creatingSlots.remove(player);
}
else {
this.creatingSlots.put(player, slot);
}
}
// Toggles placing controller
public void togglePlacingController(Player player, SlotMachine slot) {
if(this.placingController.containsKey(player)) {
this.placingController.remove(player);
}
else {
this.placingController.put(player, slot);
}
}
}