-
Notifications
You must be signed in to change notification settings - Fork 2
/
MapBuilder.java
332 lines (288 loc) · 9.27 KB
/
MapBuilder.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
package me.lagbug.xprotect.spigot.common.builders;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapCursorCollection;
import org.bukkit.map.MapFont;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import org.bukkit.map.MapView.Scale;
public class MapBuilder {
private MapView map;
private List<Text> texts;
private BufferedImage image;
private MapCursorCollection cursors;
private boolean rendered;
private boolean renderOnce;
private boolean isNewestVersion = false;
private final String[] newVersions = {"1.13", "1.14", "1.15", "1.16", "1.17", "1.18", "1.19"};
public MapBuilder() {
cursors = new MapCursorCollection();
texts = new ArrayList<>();
rendered = false;
renderOnce = true;
String version = Bukkit.getVersion();
//Adding support for newest versions
for (String currentVersion : newVersions) {
if (version.contains(currentVersion)) {
isNewestVersion = true;
}
}
}
/**
* Get the image that's being used
*
* @return the image used
*/
public BufferedImage getImage() {
return image;
}
/**
* Set an image to be used
*
* @param image the buffered image to use
* @return the instance of this class
*/
public MapBuilder setImage(@Nonnull BufferedImage image) {
this.image = image;
return this;
}
/**
* Set and image to be used
*
* @param x, y the coordinates to add the text
* @param font the font to be used
* @param text the string that will be displayed
* @return the instance of this class
*/
public MapBuilder addText(@Nonnull int x, @Nonnull int y, @Nonnull MapFont font, @Nonnull String text) {
this.texts.add(new Text(x, y, font, text));
return this;
}
/**
* Gets the list of all the texts used
*
* @return a List of all the texts
*/
public List<Text> getTexts() {
return texts;
}
/**
* Adds a cursor to the map
*
* @param x, y the coordinates to add the cursor
* @param direction the direction to display the cursor
* @param type the type of the cursor
* @return the instance of this class
*/
@SuppressWarnings("deprecation")
public MapBuilder addCursor(@Nonnull int x, @Nonnull int y, @Nonnull CursorDirection direction, @Nonnull CursorType type) {
cursors.addCursor(x, y, (byte) direction.getId(), (byte) type.getId());
return this;
}
/**
* Gets all the currently used cursors
*
* @return a MapCursorCollection with all current cursors
*/
public MapCursorCollection getCursors() {
return cursors;
}
/**
* Sets whether the image should only be rendered once.
* Good for static images and reduces lag.
*
* @param renderOnce the value to determine if it's going to be rendered once
* @return the instance of this class
*/
public MapBuilder setRenderOnce(@Nonnull boolean renderOnce) {
this.renderOnce = renderOnce;
return this;
}
/**
* Builds an ItemStack of the map.
*
* @return the ItemStack of the map containing what's been set from the above methods
*/
@SuppressWarnings("deprecation")
public ItemStack build() {
ItemStack item = new ItemStack(isNewestVersion ? Material.MAP : Material.valueOf("MAP"));
map = Bukkit.createMap(Bukkit.getWorlds().get(0));
map.setScale(Scale.NORMAL);
map.getRenderers().forEach(map::removeRenderer);
map.addRenderer(new MapRenderer() {
@Override
public void render(MapView mapView, MapCanvas mapCanvas, Player player) {
if (rendered && renderOnce) {
return;
}
if (player != null && player.isOnline()) {
if (image != null) {
mapCanvas.drawImage(0, 0, image);
}
if (!texts.isEmpty()) {
texts.forEach(text -> mapCanvas.drawText(text.getX(), text.getY(), text.getFont(), text.getMessage()));
}
if (cursors.size() > 0) {
mapCanvas.setCursors(cursors);
}
rendered = true;
}
}
});
if (isNewestVersion) {
MapMeta mapMeta = (MapMeta) item.getItemMeta();
mapMeta.setMapView(map);
item.setItemMeta(mapMeta);
} else {
item.setDurability(getMapId(map));
}
return item;
}
/**
* Gets a map id cross-version using reflection
*
* @param mapView the map to get the id
* @return the instance of this class
*/
private short getMapId(@Nonnull MapView mapView) {
try {
return (short) mapView.getId();
} catch (NoSuchMethodError ex) {
try {
return (short) Class.forName("org.bukkit.map.MapView").getMethod("getId", (Class<?>[]) new Class[0])
.invoke(mapView, new Object[0]);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | ClassNotFoundException e) {
e.printStackTrace();
return -1;
}
}
}
/**
* An enum containing user friendly cursor directions. Instead of using the integers, you can instead use this enum
*/
public enum CursorDirection {
SOUTH(0), SOUTH_WEST_SOUTH(1), SOUTH_WEST(2), SOUTH_WEST_WEST(3), WEST(4), NORTH_WEST_WEST(5), NORTH_WEST(6),
NORTH_WEST_NORTH(7), NORTH(8), NORTH_EAST_NORTH(9), NORTH_EAST(10), NORTH_EAST_EAST(11), EAST(12),
SOUTH_EAST_EAST(13), SOUNT_EAST(14), SOUTH_EAST_SOUTH(15);
private final int id;
private CursorDirection(@Nonnull int id) {
this.id = id;
}
/**
* Returns the actual integer to use
*
* @return the integer of the specified enum type
*/
public int getId() {
return this.id;
}
}
/**
* An enum containing user friendly cursor types. Instead of using the integers, you can instead use this enum
*/
public enum CursorType {
WHITE_POINTER(0), GREEN_POINTER(1), RED_POINTER(2), BLUE_POINTER(3), WHITE_CLOVER(4), RED_BOLD_POINTER(5),
WHITE_DOT(6), LIGHT_BLUE_SQUARE(7);
private final int id;
private CursorType(@Nonnull int id) {
this.id = id;
}
/**
* Returns the actual integer to use
*
* @return the integer of the specified enum type
*/
public int getId() {
return this.id;
}
}
}
/**
* A storage class to save text information to later be used in order to write in maps
*/
class Text {
private int x;
private int y;
private MapFont font;
private String message;
public Text(@Nonnull int x, @Nonnull int y, @Nonnull MapFont font, @Nonnull String message) {
setX(x);
setY(y);
setFont(font);
setMessage(message);
}
/**
* Gets the x position for the text to be displayed
*
* @return the x position
*/
public int getX() {
return x;
}
/**
* Sets the x position of the text to display it
*
* @param x the x postion
*/
public void setX(@Nonnull int x) {
this.x = x;
}
/**
* Gets the y position for the text to be displayed
*
* @return the y position
*/
public int getY() {
return y;
}
/**
* Sets the y position of the text to display it
*
* @param y the y position
*/
public void setY(@Nonnull int y) {
this.y = y;
}
/**
* Gets the font to be used
*
* @return the MapFont that is used
*/
public MapFont getFont() {
return font;
}
/**
* Sets what font should be used
*
* @param font the actual font
*/
public void setFont(@Nonnull MapFont font) {
this.font = font;
}
/**
* Gets what test will be displayed
*
* @return the text
*/
public String getMessage() {
return message;
}
/**
* Sets what text will be displayed
*
* @param message the actual text
*/
public void setMessage(@Nonnull String message) {
this.message = message;
}
}