-
Notifications
You must be signed in to change notification settings - Fork 42
/
SpigotPlayerMinesGUI.java
342 lines (260 loc) · 13.9 KB
/
SpigotPlayerMinesGUI.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
package tech.mcprison.prison.spigot.gui.mine;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import com.cryptomorin.xseries.XMaterial;
import me.clip.placeholderapi.PlaceholderAPI;
import tech.mcprison.prison.Prison;
import tech.mcprison.prison.mines.PrisonMines;
import tech.mcprison.prison.mines.data.Mine;
import tech.mcprison.prison.mines.data.PrisonSortableResults;
import tech.mcprison.prison.mines.managers.MineManager.MineSortOrder;
import tech.mcprison.prison.output.Output;
import tech.mcprison.prison.spigot.SpigotUtil;
import tech.mcprison.prison.spigot.configs.GuiConfig;
import tech.mcprison.prison.spigot.game.SpigotPlayer;
import tech.mcprison.prison.spigot.gui.SpigotGUIMenuTools;
import tech.mcprison.prison.spigot.gui.SpigotGUIMenuTools.GUIMenuPageData;
import tech.mcprison.prison.spigot.gui.guiutility.Button;
import tech.mcprison.prison.spigot.gui.guiutility.ButtonLore;
import tech.mcprison.prison.spigot.gui.guiutility.PrisonGUI;
import tech.mcprison.prison.spigot.gui.rank.SpigotGUIMessages;
import tech.mcprison.prison.spigot.nbt.PrisonNBTUtil;
import tech.mcprison.prison.util.Text;
/**
* @author GABRYCA
* @author RoyalBlueRanger (rBluer)
*/
public class SpigotPlayerMinesGUI
extends SpigotGUIMessages {
private final Player p;
private final SpigotPlayer spigotPlayer;
private final String permissionWarpPlugin = guiConfig.getString("Options.Mines.PermissionWarpPlugin");
private final String statusUnlockedMine = guiRanksLoreUnlockedMsg();
private final String clickToTeleport = guiRanksLoreClickToTeleportMsg();
private final String statusLockedMine = guiRanksLoreLockedMsg();
private int page;
private String cmdPage;
private String cmdReturn;
private final boolean placeholderAPINotNull =
Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null ||
Bukkit.getPluginManager().getPlugin("PlaceholdersAPI") != null;
public SpigotPlayerMinesGUI(Player p, int page, String cmdPage, String cmdReturn ) {
this.p = p;
this.spigotPlayer = new SpigotPlayer(p);
this.page = page;
this.cmdPage = cmdPage;
this.cmdReturn = cmdReturn;
}
public void open() {
// Get the mines - In sort order, minus any marked as suppressed
PrisonSortableResults mines = PrisonMines.getInstance().getMines( MineSortOrder.sortOrder );
int totalArraySize = mines.getSortedList().size();
GUIMenuPageData guiPageData = SpigotGUIMenuTools.getInstance()
.createGUIPageObject( totalArraySize, page, cmdPage, cmdReturn );
List<Mine> minesDisplay = mines.getSortedList().subList( guiPageData.getPosStart(), guiPageData.getPosEnd() );
// // Get the dimensions and if needed increases them
// int dimension = (int) Math.ceil(mines.getSortedList().size() / 9D) * 9;
//
// // If the inventory is empty
// if (dimension == 0){
// Output.get().sendWarn(new SpigotPlayer(p), messages.getString(MessagesConfig.StringID.spigot_message_gui_mines_empty));
// p.closeInventory();
// return;
// }
//
// // If the dimension's too big, don't open the GUI
// if (dimension > 54){
// Output.get().sendWarn(new SpigotPlayer(p), messages.getString(MessagesConfig.StringID.spigot_message_gui_mines_too_many));
// p.closeInventory();
// return;
// }
GuiConfig guiConfigClass = new GuiConfig();
guiConfig = guiConfigClass.getFileGuiConfig();
String permission = Text.translateAmpColorCodes(permissionWarpPlugin);
// Create GUI but use the gui title as defined within the ConfigGui.yml file:
PrisonGUI gui = new PrisonGUI(p, guiPageData.getDimension(), guiConfig.getString("Options.Titles.PlayerMinesGUI"));
String guiItemNameDefaultSetting = guiConfig.getString( "Options.Mines.GuiItemNameDefault" );
// Load the generic mine LORE that would be displayed first:
List<String> configCustomLore = guiConfig.getStringList("EditableLore.Mines");
String noMineAccessBlockType = guiConfig.getString( "Options.Mines.MaterialType.NoMineAccess" );
if ( noMineAccessBlockType == null ) {
noMineAccessBlockType = XMaterial.REDSTONE_BLOCK.name();
}
else {
// Validate that it is valid, otherwise use redstone_block:
try {
XMaterial.matchXMaterial( noMineAccessBlockType ).orElse(null);
}
catch (Exception e) {
noMineAccessBlockType = XMaterial.REDSTONE_BLOCK.name();
}
}
// Make the buttons for every Mine with info
for (Mine m : minesDisplay) {
// for (Mine m : mines.getSortedList()) {
String guiItemName = guiConfig.getString( "Options.Mines.GuiItemNames." + m.getName() );
// Init the lore array with default values for ladders
ButtonLore minesLore = new ButtonLore();
// If a mine has custom LORE, then try to load it:
String mineLoreKey = "EditableLore.Mine." + m.getName();
List<String> mineLore = new ArrayList<>( configCustomLore );
List<String> mineLore2 = guiConfig.getStringList( mineLoreKey );
mineLore.addAll( mineLore2 );
XMaterial xMat = XMaterial.matchXMaterial( noMineAccessBlockType ).orElse(null);
// XMaterial xMat = XMaterial.REDSTONE_BLOCK;
// Bug: Cannot safely use Material due to variants prior to bukkit v1.13:
// Material material;
String guiItemNameDefault =
(guiItemNameDefaultSetting == null || guiItemNameDefaultSetting.trim().length() == 0) ?
m.getName() :
guiItemNameDefaultSetting
.replace( "{mineName}", m.getName() )
.replace( "{mineTag}", m.getTag() );
// Get Mine Name. First use 'guiItemName' if not null, then try to use 'guiItemNameDefault'
// if not null, and then use the mine's tag, or if that's null, then use the name:
String mineName =
guiItemName != null ? guiItemName :
guiItemNameDefault != null ? guiItemNameDefault :
m.getTag() != null ? m.getTag() :
m.getName();
// // Add mineName lore for TP.
// minesLore.addLineLoreAction( "&3" + mineName );
boolean hasMineAccess = m.hasMiningAccess(spigotPlayer);
String permMineAccess = permission + m.getName();
boolean hasPermMine = p.hasPermission( permMineAccess );
String permAccess = permission.substring(0, permission.length() - 1);
boolean hasPerm = p.hasPermission( permAccess );
String lockStatus = statusLockedMine;
// If the player has permission to access the mine, then see if there is a custom
// block set for the mine... otherwise it will use XMaterial.COAL_ORE:
if ( hasMineAccess ||
hasPermMine ||
hasPerm )
{
if ( !hasMineAccess ) {
Output.get().logInfo(
"GUI Player Mines: Has access to mine %s through perms: %s=%s OR %s=%s",
m.getName(),
permMineAccess, Boolean.toString(hasPermMine),
permAccess, Boolean.toString(hasPerm)
);
}
// Default to COAL_ORE since the player has access to the mine:
{
xMat = XMaterial.COAL_ORE;
String defaultMineAccessXmat = guiConfig.getString(
"Options.Mines.MaterialType.HasMineAccess","COAL_ORE");
try {
XMaterial xMatTemp = SpigotUtil.getXMaterial( defaultMineAccessXmat );
if ( xMatTemp != null ) {
xMat = xMatTemp;
}
}
catch (Exception e) {
}
}
// xMat = XMaterial.COAL_ORE;
// The valid names to use for Options.Mines.MaterialType.<MaterialName> must be
// based upon the XMaterial enumeration name, or supported past names.
// Material mineMaterial = null;
String materialTypeStr = guiConfig.getString("Options.Mines.MaterialType." + m.getName());
if ( materialTypeStr != null && materialTypeStr.trim().length() > 0 ) {
XMaterial xMatTemp = SpigotUtil.getXMaterial( materialTypeStr );
if ( xMatTemp == null ) {
Output.get().logInfo( "Warning: A block was specified for mine '%s' but it was " +
"unable to be mapped to a valid XMaterial type. Key = " +
"[Options.Mines.MaterialType.%s] value = " +
"[%s] Please use valid material names as found in the XMaterial " +
"source on git hub: " +
"https://github.com/CryptoMorin/XSeries/blob/master/src/main/java/" +
"com/cryptomorin/xseries/XMaterial.java ",
m.getName(), m.getName(), materialTypeStr );
}
else {
xMat = xMatTemp;
}
}
lockStatus = statusUnlockedMine;
// material = ( mineMaterial == null ? Material.COAL_ORE : mineMaterial);
// minesLore.addLineLoreDescription( statusUnlockedMine );
// minesLore.addLineLoreAction( clickToTeleport );
}
else {
xMat = XMaterial.REDSTONE_BLOCK;
// material = XMaterial.REDSTONE_BLOCK.parseMaterial();
// minesLore.addLineLoreDescription( statusLockedMine );
}
// Get mine Tag, but make sure it is valid and the mine's name is not null:
String mineTag = mineName == null ? "-no mine name-" : mineName;
if ( m.getTag() != null &&
!m.getTag().equalsIgnoreCase("null") &&
!m.getTag().equalsIgnoreCase("none") ) {
mineTag = m.getTag();
}
DecimalFormat iFmt = Prison.get().getDecimalFormatInt();
for (String stringValue : mineLore) {
double volume = ( m.isVirtual() ? 0 : m.getBounds().getTotalBlockCount() );
double remaining = volume * m.getPercentRemainingBlockCount() / 100.0;
String dimensions = ( m.isVirtual() ? "virtual" : m.getBounds().getDimensions() );
stringValue = stringValue.replace( "{mineName}", mineName );
stringValue = stringValue.replace( "{mineTag}", mineTag );
stringValue = stringValue.replace( "{mineSize}", dimensions );
stringValue = stringValue.replace( "{mineVolume}", iFmt.format( volume ));
stringValue = stringValue.replace( "{mineRemaining}", iFmt.format( remaining ));
stringValue = stringValue.replace( "{mineRemainingPercent}", iFmt.format( m.getPercentRemainingBlockCount() ));
stringValue = stringValue.replace( "{clickToTeleport}", clickToTeleport );
stringValue = stringValue.replace( "{lockStatus}", lockStatus );
stringValue = stringValue.replace( "{playerCount}", iFmt.format( m.getPlayerCount()) );
if ( m.getRank() == null ) {
stringValue = stringValue.replace( "{linkedRank}", "Not linked" );
}
else {
stringValue = stringValue.replace( "{linkedRank}", m.getRank().getTag() );
}
minesLore.addLineLoreAction( stringValue );
}
if ( placeholderAPINotNull ) {
List<String> lores = PlaceholderAPI.setPlaceholders(
Bukkit.getOfflinePlayer( p.getUniqueId()),
minesLore.getLoreAction());
minesLore.setLoreAction( lores );
}
Button itemMine = new Button(null, xMat, minesLore, mineName );
// Button itemMine = new Button(null, xMat, minesLore, "&3" + mineTag);
String mineTeleportCommand =
Output.stringFormat(
"mines tp %s %s",
m.getName(),
p.getName() );
// Before adding the button, add an NBT tag for the command and mine name:
// PrisonNBTUtil nbtUtil = new PrisonNBTUtil();
// NBTItem nbtItem = nbtUtil == null ? null : nbtUtil.getNBT( itemMine.getButtonItem());
PrisonNBTUtil.setNBTBoolean( itemMine.getButtonItem(), SpigotGUIMenuTools.GUI_MENU_TOOLS_NBT_ENABLED, true);
PrisonNBTUtil.setNBTString( itemMine.getButtonItem(), SpigotGUIMenuTools.GUI_MENU_TOOLS_NBT_COMMAND, mineTeleportCommand );
PrisonNBTUtil.setNBTString( itemMine.getButtonItem(), SpigotGUIMenuTools.GUI_MENU_TOOLS_NBT_MINE_NAME, m.getName() );
// Add the button to the inventory.
gui.addButton( itemMine );
// String mineTag = m.getTag();
//
// // Check if mineName's null (which shouldn't be) and do actions.
// if (mineName != null) {
//
// if (mineTag == null || mineTag.equalsIgnoreCase("null")){
// mineTag = mineName;
// }
//
// // Add the button to the inventory.
// gui.addButton(new Button(null, xMat, minesLore, "&3" + mineTag));
//// gui.addButton(new Button(null, XMaterial.matchXMaterial(material), minesLore, "&3" + mineTag));
// }
}
// Add the page controls:
// The controls for the standard menu are in positions: 4, 5, and 6:
SpigotGUIMenuTools.getInstance().addMenuPageButtonsStandard( gui, guiPageData );
// Open the GUI.
gui.open();
}
}