Update to BentoBox 2.0.0 API

This commit is contained in:
tastybento 2023-11-18 14:51:09 -08:00
parent 1a4077be8c
commit 77884f0a11
3 changed files with 898 additions and 1009 deletions

View File

@ -356,7 +356,7 @@ public class LevelsManager {
public int getRank(@NonNull World world, UUID uuid) { public int getRank(@NonNull World world, UUID uuid) {
createAndCleanRankings(world); createAndCleanRankings(world);
Stream<Entry<UUID, Long>> stream = topTenLists.get(world).getTopTen().entrySet().stream() Stream<Entry<UUID, Long>> stream = topTenLists.get(world).getTopTen().entrySet().stream()
.filter(e -> addon.getIslands().isOwner(world, e.getKey())).filter(l -> l.getValue() > 0) .filter(e -> addon.getIslands().hasIsland(world, e.getKey())).filter(l -> l.getValue() > 0)
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())); .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
return (int) (stream.takeWhile(x -> !x.getKey().equals(uuid)).map(Map.Entry::getKey).count() + 1); return (int) (stream.takeWhile(x -> !x.getKey().equals(uuid)).map(Map.Entry::getKey).count() + 1);
} }

View File

@ -1,6 +1,5 @@
package world.bentobox.level.panels; package world.bentobox.level.panels;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
@ -30,40 +29,32 @@ import world.bentobox.level.Level;
import world.bentobox.level.objects.IslandLevels; import world.bentobox.level.objects.IslandLevels;
import world.bentobox.level.util.Utils; import world.bentobox.level.util.Utils;
/** /**
* This class opens GUI that shows generator view for user. * This class opens GUI that shows generator view for user.
*/ */
public class DetailsPanel public class DetailsPanel {
{
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Internal Constructor // Section: Internal Constructor
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
/** /**
* This is internal constructor. It is used internally in current class to avoid creating objects everywhere. * This is internal constructor. It is used internally in current class to avoid
* creating objects everywhere.
* *
* @param addon Level object * @param addon Level object
* @param world World where user is operating * @param world World where user is operating
* @param user User who opens panel * @param user User who opens panel
*/ */
private DetailsPanel(Level addon, private DetailsPanel(Level addon, World world, User user) {
World world,
User user)
{
this.addon = addon; this.addon = addon;
this.world = world; this.world = world;
this.user = user; this.user = user;
this.island = this.addon.getIslands().getIsland(world, user); this.island = this.addon.getIslands().getIsland(world, user);
if (this.island != null) if (this.island != null) {
{
this.levelsData = this.addon.getManager().getLevelsData(this.island); this.levelsData = this.addon.getManager().getLevelsData(this.island);
} } else {
else
{
this.levelsData = null; this.levelsData = null;
} }
@ -75,21 +66,17 @@ public class DetailsPanel
this.updateFilters(); this.updateFilters();
} }
/** /**
* This method builds this GUI. * This method builds this GUI.
*/ */
private void build() private void build() {
{ if (this.island == null || this.levelsData == null) {
if (this.island == null || this.levelsData == null)
{
// Nothing to see. // Nothing to see.
Utils.sendMessage(this.user, this.user.getTranslation("general.errors.no-island")); Utils.sendMessage(this.user, this.user.getTranslation("general.errors.no-island"));
return; return;
} }
if (this.levelsData.getMdCount().isEmpty() && this.levelsData.getUwCount().isEmpty()) if (this.levelsData.getMdCount().isEmpty() && this.levelsData.getUwCount().isEmpty()) {
{
// Nothing to see. // Nothing to see.
Utils.sendMessage(this.user, this.user.getTranslation("level.conversations.no-data")); Utils.sendMessage(this.user, this.user.getTranslation("level.conversations.no-data"));
return; return;
@ -117,16 +104,13 @@ public class DetailsPanel
panelBuilder.build(); panelBuilder.build();
} }
/** /**
* This method updates filter of elements based on tabs. * This method updates filter of elements based on tabs.
*/ */
private void updateFilters() private void updateFilters() {
{
this.materialCountList.clear(); this.materialCountList.clear();
switch (this.activeTab) switch (this.activeTab) {
{
case ALL_BLOCKS -> { case ALL_BLOCKS -> {
Map<Material, Integer> materialCountMap = new EnumMap<>(Material.class); Map<Material, Integer> materialCountMap = new EnumMap<>(Material.class);
@ -136,9 +120,8 @@ public class DetailsPanel
this.levelsData.getUwCount().forEach((material, count) -> materialCountMap.put(material, this.levelsData.getUwCount().forEach((material, count) -> materialCountMap.put(material,
materialCountMap.computeIfAbsent(material, key -> 0) + count)); materialCountMap.computeIfAbsent(material, key -> 0) + count));
materialCountMap.entrySet().stream().sorted((Map.Entry.comparingByKey())). materialCountMap.entrySet().stream().sorted((Map.Entry.comparingByKey()))
forEachOrdered(entry -> .forEachOrdered(entry -> this.materialCountList.add(new Pair<>(entry.getKey(), entry.getValue())));
this.materialCountList.add(new Pair<>(entry.getKey(), entry.getValue())));
} }
case ABOVE_SEA_LEVEL -> this.levelsData.getMdCount().entrySet().stream().sorted((Map.Entry.comparingByKey())) case ABOVE_SEA_LEVEL -> this.levelsData.getMdCount().entrySet().stream().sorted((Map.Entry.comparingByKey()))
.forEachOrdered(entry -> this.materialCountList.add(new Pair<>(entry.getKey(), entry.getValue()))); .forEachOrdered(entry -> this.materialCountList.add(new Pair<>(entry.getKey(), entry.getValue())));
@ -157,57 +140,44 @@ public class DetailsPanel
Comparator<Pair<Material, Integer>> sorter; Comparator<Pair<Material, Integer>> sorter;
switch (this.activeFilter) switch (this.activeFilter) {
{ case COUNT -> {
case COUNT -> sorter = (o1, o2) -> {
{ if (o1.getValue().equals(o2.getValue())) {
sorter = (o1, o2) ->
{
if (o1.getValue().equals(o2.getValue()))
{
String o1Name = Utils.prettifyObject(o1.getKey(), this.user); String o1Name = Utils.prettifyObject(o1.getKey(), this.user);
String o2Name = Utils.prettifyObject(o2.getKey(), this.user); String o2Name = Utils.prettifyObject(o2.getKey(), this.user);
return String.CASE_INSENSITIVE_ORDER.compare(o1Name, o2Name); return String.CASE_INSENSITIVE_ORDER.compare(o1Name, o2Name);
} } else {
else
{
return Integer.compare(o2.getValue(), o1.getValue()); return Integer.compare(o2.getValue(), o1.getValue());
} }
}; };
} }
case VALUE -> case VALUE -> {
{ sorter = (o1, o2) -> {
sorter = (o1, o2) ->
{
int blockLimit = this.addon.getBlockConfig().getBlockLimits().getOrDefault(o1.getKey(), 0); int blockLimit = this.addon.getBlockConfig().getBlockLimits().getOrDefault(o1.getKey(), 0);
int o1Count = blockLimit > 0 ? Math.min(o1.getValue(), blockLimit) : o1.getValue(); int o1Count = blockLimit > 0 ? Math.min(o1.getValue(), blockLimit) : o1.getValue();
blockLimit = this.addon.getBlockConfig().getBlockLimits().getOrDefault(o2.getKey(), 0); blockLimit = this.addon.getBlockConfig().getBlockLimits().getOrDefault(o2.getKey(), 0);
int o2Count = blockLimit > 0 ? Math.min(o2.getValue(), blockLimit) : o2.getValue(); int o2Count = blockLimit > 0 ? Math.min(o2.getValue(), blockLimit) : o2.getValue();
long o1Value = (long) o1Count * long o1Value = (long) o1Count
this.addon.getBlockConfig().getBlockValues().getOrDefault(o1.getKey(), 0); * this.addon.getBlockConfig().getBlockValues().getOrDefault(o1.getKey(), 0);
long o2Value = (long) o2Count * long o2Value = (long) o2Count
this.addon.getBlockConfig().getBlockValues().getOrDefault(o2.getKey(), 0); * this.addon.getBlockConfig().getBlockValues().getOrDefault(o2.getKey(), 0);
if (o1Value == o2Value) if (o1Value == o2Value) {
{
String o1Name = Utils.prettifyObject(o1.getKey(), this.user); String o1Name = Utils.prettifyObject(o1.getKey(), this.user);
String o2Name = Utils.prettifyObject(o2.getKey(), this.user); String o2Name = Utils.prettifyObject(o2.getKey(), this.user);
return String.CASE_INSENSITIVE_ORDER.compare(o1Name, o2Name); return String.CASE_INSENSITIVE_ORDER.compare(o1Name, o2Name);
} } else {
else
{
return Long.compare(o2Value, o1Value); return Long.compare(o2Value, o1Value);
} }
}; };
} }
default -> default -> {
{ sorter = (o1, o2) -> {
sorter = (o1, o2) ->
{
String o1Name = Utils.prettifyObject(o1.getKey(), this.user); String o1Name = Utils.prettifyObject(o1.getKey(), this.user);
String o2Name = Utils.prettifyObject(o2.getKey(), this.user); String o2Name = Utils.prettifyObject(o2.getKey(), this.user);
@ -221,12 +191,10 @@ public class DetailsPanel
this.pageIndex = 0; this.pageIndex = 0;
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Tab Button Type // Section: Tab Button Type
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
/** /**
* Create tab button panel item. * Create tab button panel item.
* *
@ -234,24 +202,20 @@ public class DetailsPanel
* @param slot the slot * @param slot the slot
* @return the panel item * @return the panel item
*/ */
private PanelItem createTabButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) private PanelItem createTabButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
{
PanelItemBuilder builder = new PanelItemBuilder(); PanelItemBuilder builder = new PanelItemBuilder();
if (template.icon() != null) if (template.icon() != null) {
{
// Set icon // Set icon
builder.icon(template.icon().clone()); builder.icon(template.icon().clone());
} }
if (template.title() != null) if (template.title() != null) {
{
// Set title // Set title
builder.name(this.user.getTranslation(this.world, template.title())); builder.name(this.user.getTranslation(this.world, template.title()));
} }
if (template.description() != null) if (template.description() != null) {
{
// Set description // Set description
builder.description(this.user.getTranslation(this.world, template.description())); builder.description(this.user.getTranslation(this.world, template.description()));
} }
@ -261,17 +225,13 @@ public class DetailsPanel
// Get only possible actions, by removing all inactive ones. // Get only possible actions, by removing all inactive ones.
List<ItemTemplateRecord.ActionRecords> activeActions = new ArrayList<>(template.actions()); List<ItemTemplateRecord.ActionRecords> activeActions = new ArrayList<>(template.actions());
activeActions.removeIf(action -> activeActions.removeIf(action -> "VIEW".equalsIgnoreCase(action.actionType()) && this.activeTab == tab);
"VIEW".equalsIgnoreCase(action.actionType()) && this.activeTab == tab);
// Add Click handler // Add Click handler
builder.clickHandler((panel, user, clickType, i) -> builder.clickHandler((panel, user, clickType, i) -> {
{ for (ItemTemplateRecord.ActionRecords action : activeActions) {
for (ItemTemplateRecord.ActionRecords action : activeActions)
{
if ((clickType == action.clickType() || ClickType.UNKNOWN.equals(action.clickType())) if ((clickType == action.clickType() || ClickType.UNKNOWN.equals(action.clickType()))
&& "VIEW".equalsIgnoreCase(action.actionType())) && "VIEW".equalsIgnoreCase(action.actionType())) {
{
this.activeTab = tab; this.activeTab = tab;
// Update filters. // Update filters.
@ -284,15 +244,12 @@ public class DetailsPanel
}); });
// Collect tooltips. // Collect tooltips.
List<String> tooltips = activeActions.stream(). List<String> tooltips = activeActions.stream().filter(action -> action.tooltip() != null)
filter(action -> action.tooltip() != null). .map(action -> this.user.getTranslation(this.world, action.tooltip())).filter(text -> !text.isBlank())
map(action -> this.user.getTranslation(this.world, action.tooltip())). .collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
filter(text -> !text.isBlank()).
collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
// Add tooltips. // Add tooltips.
if (!tooltips.isEmpty()) if (!tooltips.isEmpty()) {
{
// Empty line and tooltips. // Empty line and tooltips.
builder.description(""); builder.description("");
builder.description(tooltips); builder.description(tooltips);
@ -303,7 +260,6 @@ public class DetailsPanel
return builder.build(); return builder.build();
} }
/** /**
* Create next button panel item. * Create next button panel item.
* *
@ -311,77 +267,61 @@ public class DetailsPanel
* @param slot the slot * @param slot the slot
* @return the panel item * @return the panel item
*/ */
private PanelItem createFilterButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) private PanelItem createFilterButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
{
PanelItemBuilder builder = new PanelItemBuilder(); PanelItemBuilder builder = new PanelItemBuilder();
if (template.icon() != null) if (template.icon() != null) {
{
// Set icon // Set icon
builder.icon(template.icon().clone()); builder.icon(template.icon().clone());
} }
Filter filter; Filter filter;
if (slot.amountMap().getOrDefault("FILTER", 0) > 1) if (slot.amountMap().getOrDefault("FILTER", 0) > 1) {
{
filter = Enums.getIfPresent(Filter.class, String.valueOf(template.dataMap().get("filter"))).or(Filter.NAME); filter = Enums.getIfPresent(Filter.class, String.valueOf(template.dataMap().get("filter"))).or(Filter.NAME);
} } else {
else
{
filter = this.activeFilter; filter = this.activeFilter;
} }
final String reference = "level.gui.buttons.filters."; final String reference = "level.gui.buttons.filters.";
if (template.title() != null) if (template.title() != null) {
{
// Set title // Set title
builder.name(this.user.getTranslation(this.world, template.title().replace("[filter]", filter.name().toLowerCase()))); builder.name(this.user.getTranslation(this.world,
} template.title().replace("[filter]", filter.name().toLowerCase())));
else } else {
{
builder.name(this.user.getTranslation(this.world, reference + filter.name().toLowerCase() + ".name")); builder.name(this.user.getTranslation(this.world, reference + filter.name().toLowerCase() + ".name"));
} }
if (template.description() != null) if (template.description() != null) {
{
// Set description // Set description
builder.description(this.user.getTranslation(this.world, template.description().replace("[filter]", filter.name().toLowerCase()))); builder.description(this.user.getTranslation(this.world,
} template.description().replace("[filter]", filter.name().toLowerCase())));
else } else {
{ builder.name(
builder.name(this.user.getTranslation(this.world, reference + filter.name().toLowerCase() + ".description")); this.user.getTranslation(this.world, reference + filter.name().toLowerCase() + ".description"));
} }
// Get only possible actions, by removing all inactive ones. // Get only possible actions, by removing all inactive ones.
List<ItemTemplateRecord.ActionRecords> activeActions = new ArrayList<>(template.actions()); List<ItemTemplateRecord.ActionRecords> activeActions = new ArrayList<>(template.actions());
// Add Click handler // Add Click handler
builder.clickHandler((panel, user, clickType, i) -> builder.clickHandler((panel, user, clickType, i) -> {
{ for (ItemTemplateRecord.ActionRecords action : activeActions) {
for (ItemTemplateRecord.ActionRecords action : activeActions) if (clickType == action.clickType() || ClickType.UNKNOWN.equals(action.clickType())) {
{ if ("UP".equalsIgnoreCase(action.actionType())) {
if (clickType == action.clickType() || ClickType.UNKNOWN.equals(action.clickType()))
{
if ("UP".equalsIgnoreCase(action.actionType()))
{
this.activeFilter = Utils.getNextValue(Filter.values(), filter); this.activeFilter = Utils.getNextValue(Filter.values(), filter);
// Update filters. // Update filters.
this.updateFilters(); this.updateFilters();
this.build(); this.build();
} } else if ("DOWN".equalsIgnoreCase(action.actionType())) {
else if ("DOWN".equalsIgnoreCase(action.actionType()))
{
this.activeFilter = Utils.getPreviousValue(Filter.values(), filter); this.activeFilter = Utils.getPreviousValue(Filter.values(), filter);
// Update filters. // Update filters.
this.updateFilters(); this.updateFilters();
this.build(); this.build();
} } else if ("SELECT".equalsIgnoreCase(action.actionType())) {
else if ("SELECT".equalsIgnoreCase(action.actionType()))
{
this.activeFilter = filter; this.activeFilter = filter;
// Update filters. // Update filters.
@ -395,15 +335,12 @@ public class DetailsPanel
}); });
// Collect tooltips. // Collect tooltips.
List<String> tooltips = activeActions.stream(). List<String> tooltips = activeActions.stream().filter(action -> action.tooltip() != null)
filter(action -> action.tooltip() != null). .map(action -> this.user.getTranslation(this.world, action.tooltip())).filter(text -> !text.isBlank())
map(action -> this.user.getTranslation(this.world, action.tooltip())). .collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
filter(text -> !text.isBlank()).
collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
// Add tooltips. // Add tooltips.
if (!tooltips.isEmpty()) if (!tooltips.isEmpty()) {
{
// Empty line and tooltips. // Empty line and tooltips.
builder.description(""); builder.description("");
builder.description(tooltips); builder.description(tooltips);
@ -414,12 +351,10 @@ public class DetailsPanel
return builder.build(); return builder.build();
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Create common buttons // Section: Create common buttons
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
/** /**
* Create next button panel item. * Create next button panel item.
* *
@ -427,13 +362,11 @@ public class DetailsPanel
* @param slot the slot * @param slot the slot
* @return the panel item * @return the panel item
*/ */
private PanelItem createNextButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) private PanelItem createNextButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
{
long size = this.materialCountList.size(); long size = this.materialCountList.size();
if (size <= slot.amountMap().getOrDefault("BLOCK", 1) || if (size <= slot.amountMap().getOrDefault("BLOCK", 1)
1.0 * size / slot.amountMap().getOrDefault("BLOCK", 1) <= this.pageIndex + 1) || 1.0 * size / slot.amountMap().getOrDefault("BLOCK", 1) <= this.pageIndex + 1) {
{
// There are no next elements // There are no next elements
return null; return null;
} }
@ -442,37 +375,30 @@ public class DetailsPanel
PanelItemBuilder builder = new PanelItemBuilder(); PanelItemBuilder builder = new PanelItemBuilder();
if (template.icon() != null) if (template.icon() != null) {
{
ItemStack clone = template.icon().clone(); ItemStack clone = template.icon().clone();
if (Boolean.TRUE.equals(template.dataMap().getOrDefault("indexing", false))) if (Boolean.TRUE.equals(template.dataMap().getOrDefault("indexing", false))) {
{
clone.setAmount(nextPageIndex); clone.setAmount(nextPageIndex);
} }
builder.icon(clone); builder.icon(clone);
} }
if (template.title() != null) if (template.title() != null) {
{
builder.name(this.user.getTranslation(this.world, template.title())); builder.name(this.user.getTranslation(this.world, template.title()));
} }
if (template.description() != null) if (template.description() != null) {
{ builder.description(this.user.getTranslation(this.world, template.description(), TextVariables.NUMBER,
builder.description(this.user.getTranslation(this.world, template.description(), String.valueOf(nextPageIndex)));
TextVariables.NUMBER, String.valueOf(nextPageIndex)));
} }
// Add ClickHandler // Add ClickHandler
builder.clickHandler((panel, user, clickType, i) -> builder.clickHandler((panel, user, clickType, i) -> {
{ for (ItemTemplateRecord.ActionRecords action : template.actions()) {
for (ItemTemplateRecord.ActionRecords action : template.actions()) if ((clickType == action.clickType() || ClickType.UNKNOWN.equals(action.clickType()))
{ && "NEXT".equalsIgnoreCase(action.actionType())) {
if ((clickType == action.clickType() || ClickType.UNKNOWN.equals(action.clickType())) &&
"NEXT".equalsIgnoreCase(action.actionType()))
{
this.pageIndex++; this.pageIndex++;
this.build(); this.build();
} }
@ -483,15 +409,12 @@ public class DetailsPanel
}); });
// Collect tooltips. // Collect tooltips.
List<String> tooltips = template.actions().stream(). List<String> tooltips = template.actions().stream().filter(action -> action.tooltip() != null)
filter(action -> action.tooltip() != null). .map(action -> this.user.getTranslation(this.world, action.tooltip())).filter(text -> !text.isBlank())
map(action -> this.user.getTranslation(this.world, action.tooltip())). .collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
filter(text -> !text.isBlank()).
collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
// Add tooltips. // Add tooltips.
if (!tooltips.isEmpty()) if (!tooltips.isEmpty()) {
{
// Empty line and tooltips. // Empty line and tooltips.
builder.description(""); builder.description("");
builder.description(tooltips); builder.description(tooltips);
@ -500,7 +423,6 @@ public class DetailsPanel
return builder.build(); return builder.build();
} }
/** /**
* Create previous button panel item. * Create previous button panel item.
* *
@ -508,10 +430,8 @@ public class DetailsPanel
* @param slot the slot * @param slot the slot
* @return the panel item * @return the panel item
*/ */
private PanelItem createPreviousButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) private PanelItem createPreviousButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
{ if (this.pageIndex == 0) {
if (this.pageIndex == 0)
{
// There are no next elements // There are no next elements
return null; return null;
} }
@ -520,37 +440,30 @@ public class DetailsPanel
PanelItemBuilder builder = new PanelItemBuilder(); PanelItemBuilder builder = new PanelItemBuilder();
if (template.icon() != null) if (template.icon() != null) {
{
ItemStack clone = template.icon().clone(); ItemStack clone = template.icon().clone();
if (Boolean.TRUE.equals(template.dataMap().getOrDefault("indexing", false))) if (Boolean.TRUE.equals(template.dataMap().getOrDefault("indexing", false))) {
{
clone.setAmount(previousPageIndex); clone.setAmount(previousPageIndex);
} }
builder.icon(clone); builder.icon(clone);
} }
if (template.title() != null) if (template.title() != null) {
{
builder.name(this.user.getTranslation(this.world, template.title())); builder.name(this.user.getTranslation(this.world, template.title()));
} }
if (template.description() != null) if (template.description() != null) {
{ builder.description(this.user.getTranslation(this.world, template.description(), TextVariables.NUMBER,
builder.description(this.user.getTranslation(this.world, template.description(), String.valueOf(previousPageIndex)));
TextVariables.NUMBER, String.valueOf(previousPageIndex)));
} }
// Add ClickHandler // Add ClickHandler
builder.clickHandler((panel, user, clickType, i) -> builder.clickHandler((panel, user, clickType, i) -> {
{ for (ItemTemplateRecord.ActionRecords action : template.actions()) {
for (ItemTemplateRecord.ActionRecords action : template.actions())
{
if ((clickType == action.clickType() || ClickType.UNKNOWN.equals(action.clickType())) if ((clickType == action.clickType() || ClickType.UNKNOWN.equals(action.clickType()))
&& "PREVIOUS".equalsIgnoreCase(action.actionType())) && "PREVIOUS".equalsIgnoreCase(action.actionType())) {
{
this.pageIndex--; this.pageIndex--;
this.build(); this.build();
} }
@ -561,15 +474,12 @@ public class DetailsPanel
}); });
// Collect tooltips. // Collect tooltips.
List<String> tooltips = template.actions().stream(). List<String> tooltips = template.actions().stream().filter(action -> action.tooltip() != null)
filter(action -> action.tooltip() != null). .map(action -> this.user.getTranslation(this.world, action.tooltip())).filter(text -> !text.isBlank())
map(action -> this.user.getTranslation(this.world, action.tooltip())). .collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
filter(text -> !text.isBlank()).
collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
// Add tooltips. // Add tooltips.
if (!tooltips.isEmpty()) if (!tooltips.isEmpty()) {
{
// Empty line and tooltips. // Empty line and tooltips.
builder.description(""); builder.description("");
builder.description(tooltips); builder.description(tooltips);
@ -578,12 +488,10 @@ public class DetailsPanel
return builder.build(); return builder.build();
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Create Material Button // Section: Create Material Button
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
/** /**
* Create material button panel item. * Create material button panel item.
* *
@ -591,18 +499,15 @@ public class DetailsPanel
* @param slot the slot * @param slot the slot
* @return the panel item * @return the panel item
*/ */
private PanelItem createMaterialButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) private PanelItem createMaterialButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
{ if (this.materialCountList.isEmpty()) {
if (this.materialCountList.isEmpty())
{
// Does not contain any generators. // Does not contain any generators.
return null; return null;
} }
int index = this.pageIndex * slot.amountMap().getOrDefault("BLOCK", 1) + slot.slot(); int index = this.pageIndex * slot.amountMap().getOrDefault("BLOCK", 1) + slot.slot();
if (index >= this.materialCountList.size()) if (index >= this.materialCountList.size()) {
{
// Out of index. // Out of index.
return null; return null;
} }
@ -610,7 +515,6 @@ public class DetailsPanel
return this.createMaterialButton(template, this.materialCountList.get(index)); return this.createMaterialButton(template, this.materialCountList.get(index));
} }
/** /**
* This method creates button for material. * This method creates button for material.
* *
@ -618,102 +522,84 @@ public class DetailsPanel
* @param materialCount materialCount which button must be created. * @param materialCount materialCount which button must be created.
* @return PanelItem for generator tier. * @return PanelItem for generator tier.
*/ */
private PanelItem createMaterialButton(ItemTemplateRecord template, private PanelItem createMaterialButton(ItemTemplateRecord template, Pair<Material, Integer> materialCount) {
Pair<Material, Integer> materialCount)
{
PanelItemBuilder builder = new PanelItemBuilder(); PanelItemBuilder builder = new PanelItemBuilder();
if (template.icon() != null) if (template.icon() != null) {
{
builder.icon(template.icon().clone()); builder.icon(template.icon().clone());
} } else {
else
{
builder.icon(PanelUtils.getMaterialItem(materialCount.getKey())); builder.icon(PanelUtils.getMaterialItem(materialCount.getKey()));
} }
if (materialCount.getValue() < 64) if (materialCount.getValue() < 64) {
{
builder.amount(materialCount.getValue()); builder.amount(materialCount.getValue());
} }
if (template.title() != null) if (template.title() != null) {
{ builder.name(this.user.getTranslation(this.world, template.title(), TextVariables.NUMBER,
builder.name(this.user.getTranslation(this.world, template.title(), String.valueOf(materialCount.getValue()), "[material]",
TextVariables.NUMBER, String.valueOf(materialCount.getValue()), Utils.prettifyObject(materialCount.getKey(), this.user)));
"[material]", Utils.prettifyObject(materialCount.getKey(), this.user)));
} }
String description = Utils.prettifyDescription(materialCount.getKey(), this.user); String description = Utils.prettifyDescription(materialCount.getKey(), this.user);
final String reference = "level.gui.buttons.material."; final String reference = "level.gui.buttons.material.";
String blockId = this.user.getTranslationOrNothing(reference + "id", String blockId = this.user.getTranslationOrNothing(reference + "id", "[id]", materialCount.getKey().name());
"[id]", materialCount.getKey().name());
int blockValue = this.addon.getBlockConfig().getBlockValues().getOrDefault(materialCount.getKey(), 0); int blockValue = this.addon.getBlockConfig().getBlockValues().getOrDefault(materialCount.getKey(), 0);
String value = blockValue > 0 ? this.user.getTranslationOrNothing(reference + "value", String value = blockValue > 0
TextVariables.NUMBER, String.valueOf(blockValue)) : ""; ? this.user.getTranslationOrNothing(reference + "value", TextVariables.NUMBER,
String.valueOf(blockValue))
: "";
int blockLimit = this.addon.getBlockConfig().getBlockLimits().getOrDefault(materialCount.getKey(), 0); int blockLimit = this.addon.getBlockConfig().getBlockLimits().getOrDefault(materialCount.getKey(), 0);
String limit = blockLimit > 0 ? this.user.getTranslationOrNothing(reference + "limit", String limit = blockLimit > 0
TextVariables.NUMBER, String.valueOf(blockLimit)) : ""; ? this.user.getTranslationOrNothing(reference + "limit", TextVariables.NUMBER,
String.valueOf(blockLimit))
: "";
String count = this.user.getTranslationOrNothing(reference + "count", String count = this.user.getTranslationOrNothing(reference + "count", TextVariables.NUMBER,
TextVariables.NUMBER, String.valueOf(materialCount.getValue())); String.valueOf(materialCount.getValue()));
long calculatedValue = (long) Math.min(blockLimit > 0 ? blockLimit : Integer.MAX_VALUE, materialCount.getValue()) * blockValue; long calculatedValue = (long) Math.min(blockLimit > 0 ? blockLimit : Integer.MAX_VALUE,
materialCount.getValue()) * blockValue;
String valueText = calculatedValue > 0 ? this.user.getTranslationOrNothing(reference + "calculated", String valueText = calculatedValue > 0 ? this.user.getTranslationOrNothing(reference + "calculated",
TextVariables.NUMBER, String.valueOf(calculatedValue)) : ""; TextVariables.NUMBER, String.valueOf(calculatedValue)) : "";
if (template.description() != null) if (template.description() != null) {
{ builder.description(this.user
builder.description(this.user.getTranslation(this.world, template.description(), .getTranslation(this.world, template.description(), "[description]", description, "[id]", blockId,
"[description]", description, "[value]", value, "[calculated]", valueText, "[limit]", limit, "[count]", count)
"[id]", blockId, .replaceAll("(?m)^[ \\t]*\\r?\\n", "").replaceAll("(?<!\\\\)\\|", "\n").replace("\\\\\\|", "|"));
"[value]", value,
"[calculated]", valueText,
"[limit]", limit,
"[count]", count).
replaceAll("(?m)^[ \\t]*\\r?\\n", "").
replaceAll("(?<!\\\\)\\|", "\n").
replace("\\\\\\|", "|"));
} }
return builder.build(); return builder.build();
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Other Methods // Section: Other Methods
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
/** /**
* This method is used to open UserPanel outside this class. It will be much easier to open panel with single method * This method is used to open UserPanel outside this class. It will be much
* call then initializing new object. * easier to open panel with single method call then initializing new object.
* *
* @param addon Level object * @param addon Level object
* @param world World where user is operating * @param world World where user is operating
* @param user User who opens panel * @param user User who opens panel
*/ */
public static void openPanel(Level addon, public static void openPanel(Level addon, World world, User user) {
World world,
User user)
{
new DetailsPanel(addon, world, user).build(); new DetailsPanel(addon, world, user).build();
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Enums // Section: Enums
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
/** /**
* This enum holds possible tabs for current gui. * This enum holds possible tabs for current gui.
*/ */
private enum Tab private enum Tab {
{
/** /**
* All block Tab * All block Tab
*/ */
@ -732,12 +618,10 @@ public class DetailsPanel
SPAWNER SPAWNER
} }
/** /**
* Sorting order of blocks. * Sorting order of blocks.
*/ */
private enum Filter private enum Filter {
{
/** /**
* By name * By name
*/ */
@ -752,7 +636,6 @@ public class DetailsPanel
COUNT COUNT
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Variables // Section: Variables
// --------------------------------------------------------------------- // ---------------------------------------------------------------------

View File

@ -82,7 +82,6 @@ public class LevelsManagerTest {
@Mock @Mock
private Settings pluginSettings; private Settings pluginSettings;
// Class under test // Class under test
private LevelsManager lm; private LevelsManager lm;
@Mock @Mock
@ -114,12 +113,11 @@ public class LevelsManagerTest {
@Mock @Mock
private BukkitScheduler scheduler; private BukkitScheduler scheduler;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() {
// This has to be done beforeClass otherwise the tests will interfere with each other // This has to be done beforeClass otherwise the tests will interfere with each
// other
handler = mock(AbstractDatabaseHandler.class); handler = mock(AbstractDatabaseHandler.class);
// Database // Database
PowerMockito.mockStatic(DatabaseSetup.class); PowerMockito.mockStatic(DatabaseSetup.class);
@ -164,8 +162,7 @@ public class LevelsManagerTest {
when(island.getWorld()).thenReturn(world); when(island.getWorld()).thenReturn(world);
when(island.getUniqueId()).thenReturn(UUID.randomUUID().toString()); when(island.getUniqueId()).thenReturn(UUID.randomUUID().toString());
// Default to uuid's being island owners // Default to uuid's being island owners
when(im.isOwner(eq(world), any())).thenReturn(true); when(im.hasIsland(eq(world), any(UUID.class))).thenReturn(true);
when(im.getOwner(any(), any(UUID.class))).thenAnswer(in -> in.getArgument(1, UUID.class));
when(im.getIsland(world, uuid)).thenReturn(island); when(im.getIsland(world, uuid)).thenReturn(island);
when(im.getIslandById(anyString())).thenReturn(Optional.of(island)); when(im.getIslandById(anyString())).thenReturn(Optional.of(island));
@ -247,15 +244,13 @@ public class LevelsManagerTest {
private static void deleteAll(File file) throws IOException { private static void deleteAll(File file) throws IOException {
if (file.exists()) { if (file.exists()) {
Files.walk(file.toPath()) Files.walk(file.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
} }
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#calculateLevel(UUID, world.bentobox.bentobox.database.objects.Island)}. * Test method for
* {@link world.bentobox.level.LevelsManager#calculateLevel(UUID, world.bentobox.bentobox.database.objects.Island)}.
*/ */
@Test @Test
public void testCalculateLevel() { public void testCalculateLevel() {
@ -283,7 +278,8 @@ public class LevelsManagerTest {
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#getInitialLevel(world.bentobox.bentobox.database.objects.Island)}. * Test method for
* {@link world.bentobox.level.LevelsManager#getInitialLevel(world.bentobox.bentobox.database.objects.Island)}.
*/ */
@Test @Test
public void testGetInitialLevel() { public void testGetInitialLevel() {
@ -291,7 +287,8 @@ public class LevelsManagerTest {
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#getIslandLevel(org.bukkit.World, java.util.UUID)}. * Test method for
* {@link world.bentobox.level.LevelsManager#getIslandLevel(org.bukkit.World, java.util.UUID)}.
*/ */
@Test @Test
public void testGetIslandLevel() { public void testGetIslandLevel() {
@ -299,7 +296,8 @@ public class LevelsManagerTest {
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#getPointsToNextString(org.bukkit.World, java.util.UUID)}. * Test method for
* {@link world.bentobox.level.LevelsManager#getPointsToNextString(org.bukkit.World, java.util.UUID)}.
*/ */
@Test @Test
public void testGetPointsToNextString() { public void testGetPointsToNextString() {
@ -310,7 +308,8 @@ public class LevelsManagerTest {
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#getIslandLevelString(org.bukkit.World, java.util.UUID)}. * Test method for
* {@link world.bentobox.level.LevelsManager#getIslandLevelString(org.bukkit.World, java.util.UUID)}.
*/ */
@Test @Test
public void testGetIslandLevelString() { public void testGetIslandLevelString() {
@ -318,7 +317,8 @@ public class LevelsManagerTest {
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#getLevelsData(java.util.UUID)}. * Test method for
* {@link world.bentobox.level.LevelsManager#getLevelsData(java.util.UUID)}.
*/ */
@Test @Test
public void testGetLevelsData() { public void testGetLevelsData() {
@ -342,7 +342,8 @@ public class LevelsManagerTest {
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#getTopTen(org.bukkit.World, int)}. * Test method for
* {@link world.bentobox.level.LevelsManager#getTopTen(org.bukkit.World, int)}.
*/ */
@Test @Test
public void testGetTopTenEmpty() { public void testGetTopTenEmpty() {
@ -351,7 +352,8 @@ public class LevelsManagerTest {
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#getTopTen(org.bukkit.World, int)}. * Test method for
* {@link world.bentobox.level.LevelsManager#getTopTen(org.bukkit.World, int)}.
*/ */
@Test @Test
public void testGetTopTen() { public void testGetTopTen() {
@ -367,14 +369,15 @@ public class LevelsManagerTest {
*/ */
@Test @Test
public void testGetTopTenNoOwners() { public void testGetTopTenNoOwners() {
when(im.isOwner(eq(world), any())).thenReturn(false); when(im.hasIsland(eq(world), any(UUID.class))).thenReturn(false);
testLoadTopTens(); testLoadTopTens();
Map<UUID, Long> tt = lm.getTopTen(world, Level.TEN); Map<UUID, Long> tt = lm.getTopTen(world, Level.TEN);
assertTrue(tt.isEmpty()); assertTrue(tt.isEmpty());
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#hasTopTenPerm(org.bukkit.World, java.util.UUID)}. * Test method for
* {@link world.bentobox.level.LevelsManager#hasTopTenPerm(org.bukkit.World, java.util.UUID)}.
*/ */
@Test @Test
public void testHasTopTenPerm() { public void testHasTopTenPerm() {
@ -398,7 +401,8 @@ public class LevelsManagerTest {
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#removeEntry(org.bukkit.World, java.util.UUID)}. * Test method for
* {@link world.bentobox.level.LevelsManager#removeEntry(org.bukkit.World, java.util.UUID)}.
*/ */
@Test @Test
public void testRemoveEntry() { public void testRemoveEntry() {
@ -411,7 +415,8 @@ public class LevelsManagerTest {
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#setInitialIslandLevel(world.bentobox.bentobox.database.objects.Island, long)}. * Test method for
* {@link world.bentobox.level.LevelsManager#setInitialIslandLevel(world.bentobox.bentobox.database.objects.Island, long)}.
*/ */
@Test @Test
public void testSetInitialIslandLevel() { public void testSetInitialIslandLevel() {
@ -420,7 +425,8 @@ public class LevelsManagerTest {
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#setIslandLevel(org.bukkit.World, java.util.UUID, long)}. * Test method for
* {@link world.bentobox.level.LevelsManager#setIslandLevel(org.bukkit.World, java.util.UUID, long)}.
*/ */
@Test @Test
public void testSetIslandLevel() { public void testSetIslandLevel() {
@ -429,9 +435,9 @@ public class LevelsManagerTest {
} }
/** /**
* Test method for {@link world.bentobox.level.LevelsManager#getRank(World, UUID)} * Test method for
* {@link world.bentobox.level.LevelsManager#getRank(World, UUID)}
*/ */
@Test @Test
public void testGetRank() { public void testGetRank() {