Skip to content

Commit 2e282b8

Browse files
tastybentoclaude
andcommitted
Address SonarCloud findings on #419
- S1192: extract the duplicated "click-to-add" literal into Constants.CLICK_TO_ADD and use it in all three spots. - S6204: MultiBiomeSelector.getElements collects with toCollection(ArrayList::new) — the list must stay mutable because UnifiedMultiSelector's constructor sorts it in place (Stream.toList() would be immutable and throw at runtime). - S7158: use StringBuilder.isEmpty() in Utils.prettifyBiome. - S6541: split the REQUIRED_BIOMES branch out of the oversized createIslandRequirementButton into createBiomeRequirementButton and openBiomeSelector helpers. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NKxodNE4h3TsSHMqDEeC8v
1 parent 76a35d1 commit 2e282b8

4 files changed

Lines changed: 70 additions & 44 deletions

File tree

src/main/java/world/bentobox/challenges/panel/admin/EditChallengePanel.java

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -930,46 +930,7 @@ private PanelItem createIslandRequirementButton(RequirementButton button) {
930930
description.add(this.user.getTranslation(Constants.CLICK_TO_CHANGE));
931931
}
932932
case REQUIRED_BIOMES -> {
933-
if (requirements.getRequiredBiomes().isEmpty()) {
934-
description.add(this.user.getTranslation(reference + "none"));
935-
} else {
936-
description.add(this.user.getTranslation(reference + Constants.TITLE_KEY));
937-
requirements.getRequiredBiomes()
938-
.forEach(biomeKey -> description.add(this.user.getTranslation(reference + "list", "[biome]",
939-
Utils.prettifyBiome(biomeKey))));
940-
}
941-
942-
icon = new ItemStack(Material.GRASS_BLOCK);
943-
clickHandler = (panel, user, clickType, slot) -> {
944-
if (clickType.isRightClick()) {
945-
// Right click clears the biome list.
946-
requirements.getRequiredBiomes().clear();
947-
this.build();
948-
} else {
949-
// Left click opens the selector, hiding already-chosen biomes.
950-
Set<Biome> excluded = requirements.getRequiredBiomes().stream()
951-
.map(key -> Registry.BIOME.get(NamespacedKey.fromString(key)))
952-
.filter(Objects::nonNull).collect(Collectors.toSet());
953-
954-
MultiBiomeSelector.open(this.user, excluded, (status, biomes) -> {
955-
if (Boolean.TRUE.equals(status) && biomes != null) {
956-
biomes.forEach(biome -> requirements.getRequiredBiomes()
957-
.add(MultiBiomeSelector.biomeKey(biome)));
958-
}
959-
960-
this.build();
961-
});
962-
}
963-
return true;
964-
};
965-
glow = false;
966-
967-
description.add("");
968-
description.add(this.user.getTranslation(Constants.TIPS + "click-to-add"));
969-
970-
if (!requirements.getRequiredBiomes().isEmpty()) {
971-
description.add(this.user.getTranslation(Constants.TIPS + "right-click-to-clear"));
972-
}
933+
return this.createBiomeRequirementButton(requirements, reference, name);
973934
}
974935
default -> {
975936
icon = new ItemStack(Material.PAPER);
@@ -981,6 +942,67 @@ private PanelItem createIslandRequirementButton(RequirementButton button) {
981942
.clickHandler(clickHandler).build();
982943
}
983944

945+
946+
/**
947+
* Creates the "Required Biomes" button for island challenges. Left-click opens the biome
948+
* selector (hiding already-chosen biomes); right-click clears the list.
949+
*
950+
* @param requirements the island requirements being edited.
951+
* @param reference the translation reference for this button.
952+
* @param name the pre-translated button name.
953+
* @return the PanelItem for the biome requirement button.
954+
*/
955+
private PanelItem createBiomeRequirementButton(IslandRequirements requirements, String reference, String name) {
956+
final List<String> description = new ArrayList<>();
957+
958+
if (requirements.getRequiredBiomes().isEmpty()) {
959+
description.add(this.user.getTranslation(reference + "none"));
960+
} else {
961+
description.add(this.user.getTranslation(reference + Constants.TITLE_KEY));
962+
requirements.getRequiredBiomes().forEach(biomeKey -> description.add(
963+
this.user.getTranslation(reference + "list", "[biome]", Utils.prettifyBiome(biomeKey))));
964+
}
965+
966+
description.add("");
967+
description.add(this.user.getTranslation(Constants.CLICK_TO_ADD));
968+
969+
if (!requirements.getRequiredBiomes().isEmpty()) {
970+
description.add(this.user.getTranslation(Constants.TIPS + "right-click-to-clear"));
971+
}
972+
973+
return new PanelItemBuilder().icon(new ItemStack(Material.GRASS_BLOCK)).name(name).description(description)
974+
.glow(false).clickHandler((panel, user, clickType, slot) -> {
975+
if (clickType.isRightClick()) {
976+
requirements.getRequiredBiomes().clear();
977+
this.build();
978+
} else {
979+
this.openBiomeSelector(requirements);
980+
}
981+
return true;
982+
}).build();
983+
}
984+
985+
986+
/**
987+
* Opens the biome selector for the given island requirements, excluding already-chosen
988+
* biomes, and adds the chosen biomes back to the requirement before rebuilding the panel.
989+
*
990+
* @param requirements the island requirements being edited.
991+
*/
992+
private void openBiomeSelector(IslandRequirements requirements) {
993+
Set<Biome> excluded = requirements.getRequiredBiomes().stream()
994+
.map(key -> Registry.BIOME.get(NamespacedKey.fromString(key)))
995+
.filter(Objects::nonNull).collect(Collectors.toSet());
996+
997+
MultiBiomeSelector.open(this.user, excluded, (status, biomes) -> {
998+
if (Boolean.TRUE.equals(status) && biomes != null) {
999+
biomes.forEach(biome -> requirements.getRequiredBiomes().add(MultiBiomeSelector.biomeKey(biome)));
1000+
}
1001+
1002+
this.build();
1003+
});
1004+
}
1005+
9841006
/**
9851007
* This method creates buttons for inventory requirements menu.
9861008
*
@@ -1138,7 +1160,7 @@ private PanelItem createInventoryRequirementButton(RequirementButton button) {
11381160
glow = false;
11391161

11401162
description.add("");
1141-
description.add(this.user.getTranslation(Constants.TIPS + "click-to-add"));
1163+
description.add(this.user.getTranslation(Constants.CLICK_TO_ADD));
11421164
}
11431165
case REMOVE_IGNORED_META -> {
11441166
icon = new ItemStack(Material.RED_SHULKER_BOX);
@@ -1843,7 +1865,7 @@ private PanelItem createRewardButton(RewardButton button) {
18431865
glow = false;
18441866

18451867
description.add("");
1846-
description.add(this.user.getTranslation(Constants.TIPS + "click-to-add"));
1868+
description.add(this.user.getTranslation(Constants.CLICK_TO_ADD));
18471869
}
18481870
case REMOVE_IGNORED_META -> {
18491871
icon = new ItemStack(Material.RED_SHULKER_BOX);

src/main/java/world/bentobox/challenges/panel/util/MultiBiomeSelector.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package world.bentobox.challenges.panel.util;
22

3+
import java.util.ArrayList;
34
import java.util.Collection;
45
import java.util.Comparator;
56
import java.util.HashSet;
@@ -58,10 +59,11 @@ public static void open(User user, BiConsumer<Boolean, Collection<Biome>> consum
5859

5960
@Override
6061
protected List<Biome> getElements() {
62+
// A mutable list is required: UnifiedMultiSelector's constructor sorts it in place.
6163
return StreamSupport.stream(Registry.BIOME.spliterator(), false)
6264
.filter(biome -> excluded == null || !excluded.contains(biome))
6365
.sorted(Comparator.comparing(MultiBiomeSelector::biomeKey))
64-
.collect(Collectors.toList());
66+
.collect(Collectors.toCollection(ArrayList::new));
6567
}
6668

6769
@Override

src/main/java/world/bentobox/challenges/utils/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ public class Constants
249249

250250
public static final String CLICK_TO_CHANGE = TIPS + "click-to-change";
251251

252+
public static final String CLICK_TO_ADD = TIPS + "click-to-add";
253+
252254
public static final String CLICK_TO_TOGGLE = TIPS + "click-to-toggle";
253255

254256
public static final String CLICK_TO_OPEN = TIPS + "click-to-open";

src/main/java/world/bentobox/challenges/utils/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public static String prettifyBiome(String key)
280280
continue;
281281
}
282282

283-
if (builder.length() > 0)
283+
if (!builder.isEmpty())
284284
{
285285
builder.append(' ');
286286
}

0 commit comments

Comments
 (0)