Skip to content

Commit 629efd1

Browse files
committed
Complexity reduction
1 parent 49656dd commit 629efd1

File tree

4 files changed

+101
-71
lines changed

4 files changed

+101
-71
lines changed

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

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -478,36 +478,57 @@ private void processBlock(ChunkPair cp, int x, int y, int z, int globalX, int gl
478478
if (m.isAir()) {
479479
return;
480480
}
481+
481482
boolean belowSeaLevel = seaHeight > 0 && y <= seaHeight;
482483
Location loc = new Location(cp.world, globalX, y, globalZ);
483484

484-
if (addon.isItemsAdder() && ItemsAdderHook.getInCustomRegion(loc) != null) {
485-
String namespacedId = ItemsAdderHook.getInCustomRegion(loc);
486-
checkBlock(namespacedId, belowSeaLevel);
487-
} else {
488-
if (Tag.SLABS.isTagged(m)) {
489-
Slab slab = (Slab) blockData;
490-
if (slab.getType().equals(Slab.Type.DOUBLE)) {
491-
checkBlock(m, belowSeaLevel);
492-
}
493-
}
494-
if (addon.isStackersEnabled() && (m.equals(Material.CAULDRON) || m.equals(Material.SPAWNER))) {
495-
stackedBlocks.add(loc);
496-
}
497-
if (addon.isUltimateStackerEnabled() && !m.isAir()) {
498-
UltimateStackerCalc.addStackers(m, loc, results, belowSeaLevel, limitCountAndValue(m));
499-
}
500-
if (addon.getSettings().isIncludeChests() && blockData instanceof Container) {
501-
chestBlocks.add(cp.chunk);
502-
}
503-
if (m == Material.SPAWNER) {
504-
spawners.put(loc, belowSeaLevel);
505-
} else {
485+
String customRegionId = addon.isItemsAdder() ? ItemsAdderHook.getInCustomRegion(loc) : null;
486+
if (customRegionId != null) {
487+
checkBlock(customRegionId, belowSeaLevel);
488+
return;
489+
}
490+
491+
processSlabs(blockData, m, belowSeaLevel);
492+
processStackers(loc, m);
493+
processUltimateStacker(m, loc, belowSeaLevel);
494+
processChests(cp, blockData);
495+
processSpawnerOrBlock(m, loc, belowSeaLevel);
496+
}
497+
498+
private void processSlabs(BlockData blockData, Material m, boolean belowSeaLevel) {
499+
if (Tag.SLABS.isTagged(m)) {
500+
Slab slab = (Slab) blockData;
501+
if (slab.getType().equals(Slab.Type.DOUBLE)) {
506502
checkBlock(m, belowSeaLevel);
507503
}
508504
}
509505
}
510506

507+
private void processStackers(Location loc, Material m) {
508+
if (addon.isStackersEnabled() && (m.equals(Material.CAULDRON) || m.equals(Material.SPAWNER))) {
509+
stackedBlocks.add(loc);
510+
}
511+
}
512+
513+
private void processUltimateStacker(Material m, Location loc, boolean belowSeaLevel) {
514+
if (addon.isUltimateStackerEnabled() && !m.isAir()) {
515+
UltimateStackerCalc.addStackers(m, loc, results, belowSeaLevel, limitCountAndValue(m));
516+
}
517+
}
518+
519+
private void processChests(ChunkPair cp, BlockData blockData) {
520+
if (addon.getSettings().isIncludeChests() && blockData instanceof Container) {
521+
chestBlocks.add(cp.chunk);
522+
}
523+
}
524+
525+
private void processSpawnerOrBlock(Material m, Location loc, boolean belowSeaLevel) {
526+
if (m == Material.SPAWNER) {
527+
spawners.put(loc, belowSeaLevel);
528+
} else {
529+
checkBlock(m, belowSeaLevel);
530+
}
531+
}
511532

512533
/**
513534
* Scan the next chunk on the island

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,33 @@ public boolean execute(User user, String label, List<String> args) {
5959

6060
String arg = args.get(0);
6161
if ("HAND".equalsIgnoreCase(arg)) {
62-
return executeHandCommand(user);
62+
executeHandCommand(user);
63+
return true;
6364
}
6465

6566
executeMaterialCommand(user, arg);
6667
return true;
6768
}
6869

69-
private boolean executeHandCommand(User user) {
70+
private void executeHandCommand(User user) {
7071
Player player = user.getPlayer();
7172
PlayerInventory inventory = player.getInventory();
7273
ItemStack mainHandItem = inventory.getItemInMainHand();
7374

74-
if (mainHandItem.getType().equals(Material.AIR)) {
75+
if (mainHandItem.getType() == Material.AIR) {
7576
Utils.sendMessage(user, user.getTranslation("level.conversations.empty-hand"));
76-
return true;
77+
return;
7778
}
7879

7980
if (addon.isItemsAdder()) {
8081
Optional<String> id = ItemsAdderHook.getNamespacedId(mainHandItem);
8182
if (id.isPresent()) {
8283
printValue(user, id.get());
83-
return true;
84+
return;
8485
}
8586
}
8687

8788
printValue(user, mainHandItem.getType());
88-
return true;
8989
}
9090

9191
private void executeMaterialCommand(User user, String arg) {

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,16 @@ public Map<World, Map<EntityType, Integer>> getWorldSpawnerValues() {
205205
*/
206206
public Integer getValue(World world, Object obj) {
207207
// Extract the key based on the type of obj
208-
String key;
209-
if (obj instanceof Keyed keyed) {
210-
key = keyed.getKey().getKey();
211-
} else if (obj instanceof String str) {
212-
key = str;
213-
} else {
214-
key = "";
215-
}
208+
String key = switch (obj) {
209+
case Keyed keyed -> keyed.getKey().getKey();
210+
case String str -> str;
211+
default -> "";
212+
};
216213

217214
if (key.isEmpty()) {
218215
return null;
219216
}
220-
// Convert entity types to spawners
217+
221218
if (obj instanceof EntityType) {
222219
key = key.concat(SPAWNER);
223220
}

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

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -637,56 +637,68 @@ private PanelItem createBlockButton(ItemTemplateRecord template, TemplatedPanel.
637637
return this.createMaterialButton(template, this.blockCountList.get(index));
638638
}
639639

640-
/**
641-
* This method creates button for block.
642-
*
643-
* @param template the template of the button
644-
* @param blockCount count
645-
* @return PanelItem button
646-
*/
647640
private PanelItem createMaterialButton(ItemTemplateRecord template, BlockRec blockCount) {
648641
PanelItemBuilder builder = new PanelItemBuilder();
649-
if (template.icon() != null)
642+
643+
// Set icon and amount based on the template and block count.
644+
if (template.icon() != null) {
650645
builder.icon(template.icon().clone());
651-
if (blockCount.value() < 64)
646+
}
647+
if (blockCount.value() < 64) {
652648
builder.amount(blockCount.value());
649+
}
650+
653651
final String ref = "level.gui.buttons.material.";
654652
BlockDataRec d = getBlockData(blockCount.key());
655-
if (d.icon() != null)
653+
654+
// Override icon from block data if available.
655+
if (d.icon() != null) {
656656
builder.icon(d.icon());
657+
}
658+
659+
// Set name using the title translation if provided.
657660
if (template.title() != null) {
658661
builder.name(this.user.getTranslation(this.world, template.title(), TextVariables.NUMBER,
659662
String.valueOf(blockCount.value()), "[material]", d.displayMaterial()));
660663
}
664+
665+
// Build and set description if provided.
661666
if (template.description() != null) {
662-
builder.description(this.user
663-
.getTranslation(this.world, template.description(), "[description]",
664-
Utils.prettifyDescription(blockCount.key(), this.user) + d.extraDesc(), "[id]",
665-
this.user.isOp() ? d.blockId() : "", "[value]",
666-
d.blockValue() > 0
667-
? this.user.getTranslationOrNothing(
668-
ref + "value", TextVariables.NUMBER, String.valueOf(d.blockValue()))
669-
: "",
670-
"[calculated]",
671-
((long) Math.min(d.blockLimit() > 0 ? d.blockLimit() : Integer.MAX_VALUE,
672-
blockCount.value()) * d.blockValue()) > 0
673-
? this.user.getTranslationOrNothing(ref + "calculated",
674-
TextVariables.NUMBER,
675-
String.valueOf((long) Math.min(
676-
d.blockLimit() > 0 ? d.blockLimit() : Integer.MAX_VALUE,
677-
blockCount.value()) * d.blockValue()))
678-
: "",
679-
"[limit]",
680-
d.blockLimit() > 0 ? this.user.getTranslationOrNothing(
681-
ref + "limit", TextVariables.NUMBER, String.valueOf(d.blockLimit())) : "",
682-
"[count]",
683-
this.user.getTranslationOrNothing(ref + "count", TextVariables.NUMBER,
684-
String.valueOf(blockCount.value())))
685-
.replaceAll("(?m)^[ \\t]*\\r?\\n", "").replaceAll("(?<!\\\\)\\|", "\n").replace("\\\\\\|", "|"));
667+
String description = buildDescription(template.description(), blockCount, d, ref);
668+
builder.description(description);
686669
}
670+
687671
return builder.build();
688672
}
689673

674+
private String buildDescription(String descriptionTemplate, BlockRec blockCount, BlockDataRec d, String ref) {
675+
String desc = Utils.prettifyDescription(blockCount.key(), this.user) + d.extraDesc();
676+
String id = this.user.isOp() ? d.blockId() : "";
677+
String value = d.blockValue() > 0
678+
? this.user.getTranslationOrNothing(ref + "value", TextVariables.NUMBER, String.valueOf(d.blockValue()))
679+
: "";
680+
681+
long calculatedRaw = (long) Math.min(d.blockLimit() > 0 ? d.blockLimit() : Integer.MAX_VALUE,
682+
blockCount.value()) * d.blockValue();
683+
String calculated = calculatedRaw > 0
684+
? this.user.getTranslationOrNothing(ref + "calculated", TextVariables.NUMBER,
685+
String.valueOf(calculatedRaw))
686+
: "";
687+
688+
String limit = d.blockLimit() > 0
689+
? this.user.getTranslationOrNothing(ref + "limit", TextVariables.NUMBER, String.valueOf(d.blockLimit()))
690+
: "";
691+
692+
String count = this.user.getTranslationOrNothing(ref + "count", TextVariables.NUMBER,
693+
String.valueOf(blockCount.value()));
694+
695+
String translated = this.user.getTranslation(this.world, descriptionTemplate, "[description]", desc, "[id]", id,
696+
"[value]", value, "[calculated]", calculated, "[limit]", limit, "[count]", count);
697+
698+
return translated.replaceAll("(?m)^[ \\t]*\\r?\\n", "").replaceAll("(?<!\\\\)\\|", "\n").replace("\\\\\\|",
699+
"|");
700+
}
701+
690702
private record BlockDataRec(ItemStack icon, String blockId, int blockValue, int blockLimit, String displayMaterial,
691703
String extraDesc) {
692704
}

0 commit comments

Comments
 (0)