Skip to content

Commit 53beedc

Browse files
committed
Fixed up value panel
1 parent 745be0b commit 53beedc

File tree

5 files changed

+390
-315
lines changed

5 files changed

+390
-315
lines changed

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

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class IslandLevelCalculator {
6060
private final Level addon;
6161
private final Queue<Pair<Integer, Integer>> chunksToCheck;
6262
private final Island island;
63-
private final Map<Material, Integer> limitCount;
63+
private final Map<Object, Integer> limitCount;
6464
private final CompletableFuture<Results> r;
6565

6666
private final Results results;
@@ -89,7 +89,7 @@ public IslandLevelCalculator(Level addon, Island island, CompletableFuture<Resul
8989
results = new Results();
9090
duration = System.currentTimeMillis();
9191
chunksToCheck = getChunksToScan(island);
92-
this.limitCount = new EnumMap<>(addon.getBlockConfig().getBlockLimits());
92+
this.limitCount = new HashMap<>();
9393
// Get the initial island level
9494
results.initialLevel.set(addon.getInitialIslandLevel(island));
9595
// Set up the worlds
@@ -159,10 +159,7 @@ private void checkBlock(Material mat, boolean belowSeaLevel) {
159159
* @param belowSeaLevel - true if below sea level
160160
*/
161161
private void checkSpawner(EntityType et, boolean belowSeaLevel) {
162-
if (limitCount(Material.SPAWNER) == 0) {
163-
return;
164-
}
165-
Integer count = addon.getBlockConfig().getValue(island.getWorld(), et);
162+
Integer count = limitCount(et);
166163
if (count != null) {
167164
if (belowSeaLevel) {
168165
results.underWaterBlockCount.addAndGet(count);
@@ -247,14 +244,9 @@ private List<String> getReport() {
247244
while (it.hasNext()) {
248245

249246
Entry<Object> type = it.next();
250-
Material m = type.getElement() instanceof Material mat ? mat : Material.SPAWNER;
251-
Integer limit = addon.getBlockConfig().getBlockLimits().get(m);
247+
Integer limit = addon.getBlockConfig().getLimit(type);
252248
String explain = ")";
253-
if (limit == null) {
254-
limit = addon.getBlockConfig().getBlockLimits().get(m);
255-
explain = " - All types)";
256-
}
257-
reportLines.add(Util.prettifyText(m.name()) + ": " + String.format("%,d", type.getCount())
249+
reportLines.add(Util.prettifyText(type.toString()) + ": " + String.format("%,d", type.getCount())
258250
+ " blocks (max " + limit + explain);
259251
}
260252
reportLines.add(LINE_BREAK);
@@ -271,10 +263,10 @@ public Results getResults() {
271263
/**
272264
* Get value of a material World blocks trump regular block values
273265
*
274-
* @param md - Material to check
275-
* @return value of a material
266+
* @param md - Material or EntityType to check
267+
* @return value
276268
*/
277-
private int getValue(Material md) {
269+
private int getValue(Object md) {
278270
Integer value = addon.getBlockConfig().getValue(island.getWorld(), md);
279271
if (value == null) {
280272
// Not in config
@@ -335,24 +327,28 @@ private void roseStackerCheck(Chunk chunk) {
335327
}
336328

337329
/**
338-
* Checks if a block has been limited or not and whether a block has any value
339-
* or not
340-
*
341-
* @param md Material
342-
* @return value of the block if can be counted
330+
* Checks if the given object (Material or EntityType) has reached its limit.
331+
* If it hasn't, the object's value is returned and its count is incremented.
332+
* If the object is not a Material or EntityType, or if it has exceeded its limit, 0 is returned.
333+
*
334+
* @param obj A Material or EntityType
335+
* @return The object's value if within limit, otherwise 0.
343336
*/
344-
private int limitCount(Material md) {
345-
if (limitCount.containsKey(md)) {
346-
int count = limitCount.get(md);
347-
if (count > 0) {
348-
limitCount.put(md, --count);
349-
return getValue(md);
350-
} else {
351-
results.ofCount.add(md);
352-
return 0;
353-
}
354-
}
355-
return getValue(md);
337+
private int limitCount(Object obj) {
338+
// Only process if obj is a Material or EntityType
339+
if (!(obj instanceof Material) && !(obj instanceof EntityType))
340+
return 0;
341+
342+
Integer limit = addon.getBlockConfig().getLimit(obj);
343+
if (limit == null)
344+
return getValue(obj);
345+
346+
int count = limitCount.getOrDefault(obj, 0);
347+
if (count > limit)
348+
return 0;
349+
350+
limitCount.put(obj, count + 1);
351+
return getValue(obj);
356352
}
357353

358354
/**

src/main/java/world/bentobox/level/commands/IslandValueCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ private void printValue(User user, Material material)
123123
int count = lvData.getMdCount().getOrDefault(material, 0) + lvData.getUwCount().getOrDefault(material, 0);
124124
user.sendMessage("level.conversations.you-have", TextVariables.NUMBER,
125125
String.valueOf(count));
126-
int limit = this.addon.getBlockConfig().getBlockLimits().getOrDefault(material, -1);
127-
if (limit > 0) {
126+
Integer limit = this.addon.getBlockConfig().getLimit(material);
127+
if (limit != null) {
128128
user.sendMessage("level.conversations.you-can-place", TextVariables.NUMBER,
129129
String.valueOf(limit));
130130
}

src/main/java/world/bentobox/level/config/BlockConfig.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
public class BlockConfig {
2828

2929
private static final String SPAWNER = "_SPAWNER";
30-
private Map<Material, Integer> blockLimits = new EnumMap<>(Material.class);
30+
private Map<String, Integer> blockLimits = new HashMap<>();
3131
private Map<Material, Integer> blockValues = new EnumMap<>(Material.class);
3232
private final Map<World, Map<Material, Integer>> worldBlockValues = new HashMap<>();
3333
private final Map<World, Map<EntityType, Integer>> worldSpawnerValues = new HashMap<>();
@@ -45,7 +45,9 @@ public class BlockConfig {
4545
public BlockConfig(Level addon, YamlConfiguration blockValues, File file) throws IOException {
4646
this.addon = addon;
4747
if (blockValues.isConfigurationSection("limits")) {
48-
setBlockLimits(loadBlockLimits(blockValues));
48+
for (String key : blockValues.getConfigurationSection("limits").getKeys(false)) {
49+
blockLimits.put(key, blockValues.getConfigurationSection("limits").getInt(key));
50+
}
4951
}
5052
if (blockValues.isConfigurationSection("blocks")) {
5153
setBlockValues(loadBlockValues(blockValues));
@@ -144,31 +146,19 @@ private Map<EntityType, Integer> loadSpawnerValues(YamlConfiguration blockValues
144146
return bv;
145147
}
146148

147-
private Map<Material, Integer> loadBlockLimits(YamlConfiguration blockValues2) {
148-
Map<Material, Integer> bl = new EnumMap<>(Material.class);
149-
ConfigurationSection limits = Objects.requireNonNull(blockValues2.getConfigurationSection("limits"));
150-
for (String material : limits.getKeys(false)) {
151-
try {
152-
Material mat = Material.valueOf(material);
153-
bl.put(mat, limits.getInt(material, 0));
154-
} catch (Exception e) {
155-
addon.logError("Unknown material (" + material + ") in blockconfig.yml Limits section. Skipping...");
156-
}
157-
}
158-
return bl;
159-
}
160-
161149
/**
162-
* @return the blockLimits
150+
* Return the limits for any particular material or entity type
151+
* @param obj material or entity type
152+
* @return the limit or null if there isn't one
163153
*/
164-
public final Map<Material, Integer> getBlockLimits() {
165-
return blockLimits;
166-
}
167-
/**
168-
* @param bl the blockLimits to set
169-
*/
170-
private void setBlockLimits(Map<Material, Integer> bl) {
171-
this.blockLimits = bl;
154+
public Integer getLimit(Object obj) {
155+
if (obj instanceof Material m) {
156+
return blockLimits.get(m.name());
157+
}
158+
if (obj instanceof EntityType et) {
159+
return blockLimits.get(et.name().concat(SPAWNER));
160+
}
161+
return null;
172162
}
173163
/**
174164
* @return the blockValues

0 commit comments

Comments
 (0)