Skip to content

Commit 3ca769f

Browse files
committed
Fix display of blocks on initial opening of detail display
1 parent 5a88d28 commit 3ca769f

File tree

8 files changed

+76
-33
lines changed

8 files changed

+76
-33
lines changed

src/main/java/world/bentobox/level/LevelsManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ public IslandLevels getLevelsData(@NonNull Island island) {
256256
if (ld != null) {
257257
levelsCache.put(id, ld);
258258
} else {
259+
// Clean up just in case
259260
handler.deleteID(id);
260261
levelsCache.put(id, new IslandLevels(id));
261262
}

src/main/java/world/bentobox/level/calculators/Results.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import java.util.concurrent.atomic.AtomicInteger;
55
import java.util.concurrent.atomic.AtomicLong;
66

7-
import org.bukkit.Material;
8-
97
import com.google.common.collect.HashMultiset;
108
import com.google.common.collect.Multiset;
119

src/main/java/world/bentobox/level/calculators/UltimateStackerCalc.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import com.craftaro.ultimatestacker.api.UltimateStackerApi;
77
import com.craftaro.ultimatestacker.api.utils.Stackable;
88

9-
import world.bentobox.bentobox.BentoBox;
10-
119
/**
1210
* Isolates UltimateStacker imports so that they are only loaded if the plugin exists
1311
*/

src/main/java/world/bentobox/level/objects/IslandLevels.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package world.bentobox.level.objects;
22

33
import java.util.HashMap;
4+
import java.util.Locale;
45
import java.util.Map;
56

7+
import org.bukkit.Material;
8+
import org.bukkit.entity.EntityType;
9+
610
import com.google.gson.annotations.Expose;
711

812
import world.bentobox.bentobox.database.objects.DataObject;
@@ -172,6 +176,9 @@ public Map<Object, Integer> getUwCount() {
172176
* @param map the uwCount to set
173177
*/
174178
public void setUwCount(Map<Object, Integer> map) {
179+
// Loaded objects come in as strings, so need to be converted to Material Or EntityTypes
180+
uwCount = convertMap(uwCount);
181+
175182
this.uwCount = map;
176183
}
177184

@@ -180,9 +187,42 @@ public void setUwCount(Map<Object, Integer> map) {
180187
* @return the mdCount
181188
*/
182189
public Map<Object, Integer> getMdCount() {
190+
// Loaded objects come in as strings, so need to be converted to Material Or EntityTypes
191+
mdCount = convertMap(mdCount);
183192
return mdCount;
184193
}
185194

195+
private Map<Object, Integer> convertMap(Map<Object, Integer> mdCount) {
196+
Map<Object, Integer> convertedMap = new HashMap<>();
197+
198+
for (Map.Entry<Object, Integer> entry : mdCount.entrySet()) {
199+
Object key = entry.getKey();
200+
Integer value = entry.getValue();
201+
202+
if (key instanceof String) {
203+
String keyStr = (String) key;
204+
// First, try converting to Material
205+
Material material = Material.matchMaterial(keyStr);
206+
if (material != null) {
207+
convertedMap.put(material, value);
208+
} else {
209+
// Fallback to converting to EntityType (using uppercase as enum constants are uppercase)
210+
try {
211+
EntityType entityType = EntityType.valueOf(keyStr.toUpperCase(Locale.ENGLISH));
212+
convertedMap.put(entityType, value);
213+
} catch (IllegalArgumentException ex) {
214+
// No valid Material or EntityType found.
215+
convertedMap.put(key, value); // Leave the key unchanged.
216+
}
217+
}
218+
} else {
219+
// If the key is not a String, add it directly.
220+
convertedMap.put(key, value);
221+
}
222+
}
223+
return convertedMap;
224+
}
225+
186226
/**
187227
* All blocks except for underwater blocks
188228
* @param mdCount the mdCount to set

src/main/java/world/bentobox/level/panels/DetailsPanel.java

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private DetailsPanel(Level addon, World world, User user) {
6161
this.levelsData = null;
6262
}
6363

64-
// By default no-filters are active.
64+
// Default Filters
6565
this.activeTab = Tab.VALUE_BLOCKS;
6666
this.activeFilter = Filter.NAME;
6767
this.materialCountList = new ArrayList<>(Material.values().length);
@@ -139,12 +139,12 @@ private void updateFilters() {
139139
}
140140
// Remove any hidden blocks
141141
materialCountMap.keySet().removeIf(this.addon.getBlockConfig()::isHiddenBlock);
142-
143142
// Remove any zero amount items
144143
materialCountMap.values().removeIf(i -> i < 1);
145144

146145
if (this.activeTab == Tab.VALUE_BLOCKS) {
147146
// Remove zero-value blocks
147+
// TODO BUG IS HERE - ALL REMOVED
148148
materialCountMap.entrySet().removeIf(en -> Optional
149149
.ofNullable(this.addon.getBlockConfig().getValue(world, en.getKey())).orElse(0) == 0);
150150
}
@@ -155,7 +155,7 @@ private void updateFilters() {
155155
.collect(Collectors.toList()));
156156

157157
}
158-
158+
// Sort and filter
159159
Comparator<Pair<Object, Integer>> sorter;
160160

161161
switch (this.activeFilter) {
@@ -205,7 +205,6 @@ private void updateFilters() {
205205
}
206206

207207
this.materialCountList.sort(sorter);
208-
209208
this.pageIndex = 0;
210209
}
211210

@@ -519,7 +518,7 @@ private PanelItem createPreviousButton(ItemTemplateRecord template, TemplatedPan
519518
*/
520519
private PanelItem createMaterialButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
521520
if (this.materialCountList.isEmpty()) {
522-
// Does not contain any generators.
521+
// Nothing to show
523522
return null;
524523
}
525524

@@ -546,45 +545,49 @@ private PanelItem createMaterialButton(ItemTemplateRecord template, Pair<Object,
546545
if (template.icon() != null) {
547546
builder.icon(template.icon().clone());
548547
}
549-
// Show amount, if < 64
548+
// Show amount if less than 64.
550549
if (materialCount.getValue() < 64) {
551550
builder.amount(materialCount.getValue());
552551
}
553552

554553
final String reference = "level.gui.buttons.material.";
555-
String description = "";
554+
Object key = materialCount.getKey();
555+
String description = Utils.prettifyDescription(key, this.user);
556556
String blockId = "";
557-
Object key = materialCount.getKey(); // Can be a Material or EntityType
557+
int blockValue = 0;
558+
int blockLimit = 0;
559+
String value = "";
560+
String limit = "";
561+
String displayMaterial = Utils.prettifyObject(key, this.user);
562+
558563
if (key instanceof Material m) {
564+
// Material-specific settings.
559565
builder.icon(PanelUtils.getMaterialItem(m));
560-
if (template.title() != null) {
561-
builder.name(this.user.getTranslation(this.world, template.title(), TextVariables.NUMBER,
562-
String.valueOf(materialCount.getValue()), "[material]", Utils.prettifyObject(m, this.user)));
563-
}
564-
description = Utils.prettifyDescription(m, this.user);
565566
blockId = this.user.getTranslationOrNothing(reference + "id", "[id]", m.name());
566-
567+
blockValue = this.addon.getBlockConfig().getBlockValues().getOrDefault(m, 0);
568+
blockLimit = this.addon.getBlockConfig().getBlockLimits().getOrDefault(m, 0);
567569
} else if (key instanceof EntityType e) {
570+
// EntityType-specific settings.
568571
builder.icon(PanelUtils.getEntityEgg(e));
569-
if (template.title() != null) {
570-
builder.name(this.user.getTranslation(this.world, template.title(), TextVariables.NUMBER,
571-
String.valueOf(materialCount.getValue()), "[material]", Utils.prettifyObject(e, this.user)));
572-
}
573-
description = Utils.prettifyDescription(e, this.user);
574-
blockId = this.user.getTranslationOrNothing(reference + "id", "[id]", e.name());
572+
description += this.user.getTranslation(this.world, "level.gui.buttons.spawner.block-name"); // Put spawner on the end
573+
blockId = this.user.getTranslationOrNothing(reference + "id", "[id]", e.name() + "_SPAWNER");
574+
blockValue = this.addon.getBlockConfig().getSpawnerValues().getOrDefault(e, 0);
575+
blockLimit = this.addon.getBlockConfig().getBlockLimits().getOrDefault(Material.SPAWNER, 0);
576+
}
577+
578+
if (template.title() != null) {
579+
builder.name(this.user.getTranslation(this.world, template.title(), TextVariables.NUMBER,
580+
String.valueOf(materialCount.getValue()), "[material]", displayMaterial));
575581
}
576582

577-
int blockValue = this.addon.getBlockConfig().getBlockValues().getOrDefault(key, 0);
578-
String value = blockValue > 0
583+
value = blockValue > 0
579584
? this.user.getTranslationOrNothing(reference + "value", TextVariables.NUMBER,
580585
String.valueOf(blockValue))
581-
: "";
582-
583-
int blockLimit = this.addon.getBlockConfig().getBlockLimits().getOrDefault(key, 0);
584-
String limit = blockLimit > 0
586+
: "";
587+
limit = blockLimit > 0
585588
? this.user.getTranslationOrNothing(reference + "limit", TextVariables.NUMBER,
586589
String.valueOf(blockLimit))
587-
: "";
590+
: "";
588591

589592
String count = this.user.getTranslationOrNothing(reference + "count", TextVariables.NUMBER,
590593
String.valueOf(materialCount.getValue()));
@@ -594,6 +597,10 @@ private PanelItem createMaterialButton(ItemTemplateRecord template, Pair<Object,
594597
String valueText = calculatedValue > 0 ? this.user.getTranslationOrNothing(reference + "calculated",
595598
TextVariables.NUMBER, String.valueOf(calculatedValue)) : "";
596599

600+
// Hide block ID unless user is an operator.
601+
if (!user.isOp()) {
602+
blockId = "";
603+
}
597604
if (template.description() != null) {
598605
builder.description(this.user
599606
.getTranslation(this.world, template.description(), "[description]", description, "[id]", blockId,

src/main/java/world/bentobox/level/util/Utils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.bukkit.Material;
1313
import org.bukkit.entity.EntityType;
1414
import org.bukkit.permissions.PermissionAttachmentInfo;
15-
import org.eclipse.jdt.annotation.Nullable;
1615

1716
import world.bentobox.bentobox.api.user.User;
1817
import world.bentobox.bentobox.hooks.LangUtilsHook;

src/main/resources/locales/en-US.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ level:
142142
name: "&f&l Spawners"
143143
description: |-
144144
&7 Display only spawners.
145+
block-name: "&b Spawner"
145146
filters:
146147
name:
147148
name: "&f&l Sort by Name"

src/test/java/world/bentobox/level/LevelsManagerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.nio.file.Files;
1717
import java.nio.file.Path;
1818
import java.util.ArrayList;
19-
import java.util.Collections;
2019
import java.util.Comparator;
2120
import java.util.List;
2221
import java.util.Map;

0 commit comments

Comments
 (0)