-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
GuiUtils.java
337 lines (287 loc) · 9.36 KB
/
GuiUtils.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
package world.bentobox.controlpanel.panels;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
/**
* This class contains static methods that is used through multiple GUIs.
*/
public class GuiUtils
{
// ---------------------------------------------------------------------
// Section: Border around GUIs
// ---------------------------------------------------------------------
/**
* This method creates border of black panes around given panel with 5 rows.
* @param panelBuilder PanelBuilder which must be filled with border blocks.
*/
public static void fillBorder(PanelBuilder panelBuilder)
{
GuiUtils.fillBorder(panelBuilder, 5, Material.BLACK_STAINED_GLASS_PANE);
}
/**
* This method sets black stained glass pane around Panel with given row count.
* @param panelBuilder object that builds Panel.
* @param rowCount in Panel.
*/
public static void fillBorder(PanelBuilder panelBuilder, int rowCount)
{
GuiUtils.fillBorder(panelBuilder, rowCount, Material.BLACK_STAINED_GLASS_PANE);
}
/**
* This method sets blocks with given Material around Panel with 5 rows.
* @param panelBuilder object that builds Panel.
* @param material that will be around Panel.
*/
public static void fillBorder(PanelBuilder panelBuilder, Material material)
{
GuiUtils.fillBorder(panelBuilder, 5, material);
}
/**
* This method sets blocks with given Material around Panel with given row count.
* @param panelBuilder object that builds Panel.
* @param rowCount in Panel.
* @param material that will be around Panel.
*/
public static void fillBorder(PanelBuilder panelBuilder, int rowCount, Material material)
{
// Only for useful filling.
if (rowCount < 3)
{
return;
}
for (int i = 0; i < 9 * rowCount; i++)
{
// First (i < 9) and last (i > 35) rows must be filled
// First column (i % 9 == 0) and last column (i % 9 == 8) also must be filled.
if (i < 9 || i > 9 * (rowCount - 1) || i % 9 == 0 || i % 9 == 8)
{
panelBuilder.item(i, BorderBlock.getPanelBorder(material));
}
}
}
// ---------------------------------------------------------------------
// Section: ItemStack transformations
// ---------------------------------------------------------------------
/**
* This BorderBlock is simple PanelItem but without item meta data.
*/
private static class BorderBlock extends PanelItem
{
private BorderBlock(ItemStack icon)
{
super(new PanelItemBuilder().
icon(icon.clone()).
name(" ").
description(Collections.emptyList()).
glow(false).
clickHandler(null));
}
/**
* This method retunrs BorderBlock with requested item stack.
* @param material of which broder must be created.
* @return PanelItem that acts like border.
*/
private static BorderBlock getPanelBorder(Material material)
{
ItemStack itemStack = new ItemStack(material);
itemStack.getItemMeta().setDisplayName(" ");
return new BorderBlock(itemStack);
}
}
/**
* Simple splitter
*
* @param string - string to be split
* @param warpLength - when warp should be affected.
* @param colour - boolean that indicate if colour should be translated.
* @return list of split strings
*/
public static List<String> stringSplit(String string, int warpLength, boolean colour)
{
// Remove all ending lines from string.
string = string.replaceAll("([\\r\\n])", "\\|");
if (colour)
{
string = ChatColor.translateAlternateColorCodes('&', string);
}
// Check length of lines
List<String> result = new ArrayList<>();
Arrays.stream(string.split("\\|")).
map(line -> Arrays.asList(wrap(line, warpLength).split(System.getProperty("line.separator")))).
forEach(result::addAll);
// Fix colors, as splitting my lost that information.
for (int i = 0, resultSize = result.size(); colour && i < resultSize; i++)
{
if (i > 0)
{
String lastColor = ChatColor.getLastColors(result.get(i - 1));
result.set(i, lastColor + result.get(i));
}
}
return result;
}
static String wrap(String text, int maxWidth) {
BreakIterator boundary = BreakIterator.getLineInstance();
boundary.setText(text);
StringBuilder wrappedText = new StringBuilder();
int start = boundary.first();
int end = boundary.next();
int lineLength = 0;
while (end != BreakIterator.DONE) {
String word = text.substring(start, end);
lineLength += word.length();
if (lineLength >= maxWidth) {
wrappedText.append("\n");
lineLength = word.length();
}
wrappedText.append(word);
start = end;
end = boundary.next();
}
return wrappedText.toString();
}
/**
* Simple splitter
*
* @param string - string to be split
* @param warpLength - when warp should be affected.
* @return list of split strings
*/
public static List<String> stringSplit(String string, int warpLength)
{
return GuiUtils.stringSplit(string, warpLength, true);
}
/**
* Simple splitter for all strings in list.
* @param stringList - list of string to be split
* @param warpLength - when warp should be affected.
* @return list of split strings
*/
public static List<String> stringSplit(List<String> stringList, int warpLength)
{
if (stringList.isEmpty())
{
return stringList;
}
List<String> newList = new ArrayList<>(stringList.size());
stringList.stream().map(string -> GuiUtils.stringSplit(string, warpLength)).forEach(newList::addAll);
return newList;
}
// ---------------------------------------------------------------------
// Section: Materials
// ---------------------------------------------------------------------
/**
* This method transforms material into item stack that can be displayed in users
* inventory.
* @param material Material which item stack must be returned.
* @return ItemStack that represents given material.
*/
public static ItemStack getMaterialItem(Material material)
{
return GuiUtils.getMaterialItem(material, 1);
}
/**
* This method transforms material into item stack that can be displayed in users
* inventory.
* @param material Material which item stack must be returned.
* @param amount Amount of ItemStack elements.
* @return ItemStack that represents given material.
*/
public static ItemStack getMaterialItem(Material material, int amount)
{
ItemStack itemStack;
// Process items that cannot be item-stacks.
if (material.name().contains("WALL_"))
{
// Materials that is attached to wall cannot be showed in GUI. But they should be in list.
itemStack = new ItemStack(Material.getMaterial(material.name().replace("WALL_", "")));
}
else if (material.name().startsWith("POTTED_"))
{
// Materials Potted elements cannot be in inventory.
itemStack = new ItemStack(Material.getMaterial(material.name().replace("POTTED_", "")));
}
else if (material.equals(Material.MELON_STEM) || material.equals(Material.ATTACHED_MELON_STEM))
{
itemStack = new ItemStack(Material.MELON_SEEDS);
}
else if (material.equals(Material.PUMPKIN_STEM) || material.equals(Material.ATTACHED_PUMPKIN_STEM))
{
itemStack = new ItemStack(Material.PUMPKIN_SEEDS);
}
else if (material.equals(Material.TALL_SEAGRASS))
{
itemStack = new ItemStack(Material.SEAGRASS);
}
else if (material.equals(Material.CARROTS))
{
itemStack = new ItemStack(Material.CARROT);
}
else if (material.equals(Material.BEETROOTS))
{
itemStack = new ItemStack(Material.BEETROOT);
}
else if (material.equals(Material.POTATOES))
{
itemStack = new ItemStack(Material.POTATO);
}
else if (material.equals(Material.COCOA))
{
itemStack = new ItemStack(Material.COCOA_BEANS);
}
else if (material.equals(Material.KELP_PLANT))
{
itemStack = new ItemStack(Material.KELP);
}
else if (material.equals(Material.REDSTONE_WIRE))
{
itemStack = new ItemStack(Material.REDSTONE);
}
else if (material.equals(Material.TRIPWIRE))
{
itemStack = new ItemStack(Material.STRING);
}
else if (material.equals(Material.FROSTED_ICE))
{
itemStack = new ItemStack(Material.ICE);
}
else if (material.equals(Material.END_PORTAL) || material.equals(Material.END_GATEWAY) || material.equals(Material.NETHER_PORTAL))
{
itemStack = new ItemStack(Material.PAPER);
}
else if (material.equals(Material.BUBBLE_COLUMN) || material.equals(Material.WATER))
{
itemStack = new ItemStack(Material.WATER_BUCKET);
}
else if (material.equals(Material.LAVA))
{
itemStack = new ItemStack(Material.LAVA_BUCKET);
}
else if (material.equals(Material.FIRE))
{
itemStack = new ItemStack(Material.FIRE_CHARGE);
}
else if (material.equals(Material.AIR) || material.equals(Material.CAVE_AIR) || material.equals(Material.VOID_AIR))
{
itemStack = new ItemStack(Material.GLASS_BOTTLE);
}
else if (material.equals(Material.PISTON_HEAD) || material.equals(Material.MOVING_PISTON))
{
itemStack = new ItemStack(Material.PISTON);
}
else
{
itemStack = new ItemStack(material);
}
itemStack.setAmount(amount);
return itemStack;
}
}