Skip to content

Commit 49656dd

Browse files
committed
Refactor to reduce complexity. Fix static code analysis bugs.
1 parent 66d3a9e commit 49656dd

File tree

7 files changed

+221
-223
lines changed

7 files changed

+221
-223
lines changed

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

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,33 @@ public void onEnable() {
104104
@Override
105105
public void allLoaded() {
106106
super.allLoaded();
107-
108107
loadBlockSettings();
109-
// Start pipeline
108+
initializePipelineAndManager();
109+
registerAllListeners();
110+
registerGameModeCommands();
111+
registerRequestHandlers();
112+
hookPlugin("WildStacker", this::hookWildStackers);
113+
hookAdvancedChests();
114+
hookPlugin("RoseStacker", this::hookRoseStackers);
115+
hookPlugin("UltimateStacker", this::hookUltimateStacker);
116+
117+
if (this.isEnabled()) {
118+
hookExtensions();
119+
}
120+
}
121+
122+
private void initializePipelineAndManager() {
110123
pipeliner = new Pipeliner(this);
111-
// Start Manager
112124
manager = new LevelsManager(this);
113-
// Register listeners
114-
this.registerListener(new IslandActivitiesListeners(this));
115-
this.registerListener(new JoinLeaveListener(this));
116-
this.registerListener(new MigrationListener(this));
125+
}
117126

118-
// Register commands for GameModes
127+
private void registerAllListeners() {
128+
registerListener(new IslandActivitiesListeners(this));
129+
registerListener(new JoinLeaveListener(this));
130+
registerListener(new MigrationListener(this));
131+
}
132+
133+
private void registerGameModeCommands() {
119134
registeredGameModes.clear();
120135
getPlugin().getAddonsManager().getGameModeAddons().stream()
121136
.filter(gm -> !settings.getGameModes().contains(gm.getDescription().getName())).forEach(gm -> {
@@ -124,26 +139,34 @@ public void allLoaded() {
124139
new PlaceholderManager(this).registerPlaceholders(gm);
125140
registeredGameModes.add(gm);
126141
});
127-
// Register request handlers
142+
}
143+
144+
private void registerRequestHandlers() {
128145
registerRequestHandler(new LevelRequestHandler(this));
129146
registerRequestHandler(new TopTenRequestHandler(this));
147+
}
130148

131-
// Check if WildStackers is enabled on the server
132-
// I only added support for counting blocks into the island level
133-
// Someone else can PR if they want spawners added to the Leveling system :)
134-
if (!settings.getDisabledPluginHooks().contains("WildStacker")) {
135-
stackersEnabled = Bukkit.getPluginManager().isPluginEnabled("WildStacker");
136-
if (stackersEnabled) {
137-
log("Hooked into WildStackers.");
138-
}
149+
/**
150+
* A helper that only executes the provided hookAction if the plugin is not disabled.
151+
*/
152+
private void hookPlugin(String pluginName, Runnable hookAction) {
153+
if (!settings.getDisabledPluginHooks().contains(pluginName)) {
154+
hookAction.run();
139155
}
156+
}
140157

141-
// Check if AdvancedChests is enabled on the server
158+
private void hookWildStackers() {
159+
stackersEnabled = Bukkit.getPluginManager().isPluginEnabled("WildStacker");
160+
if (stackersEnabled) {
161+
log("Hooked into WildStackers.");
162+
}
163+
}
164+
165+
private void hookAdvancedChests() {
142166
if (!settings.getDisabledPluginHooks().contains("AdvancedChests")) {
143167
Plugin advChest = Bukkit.getPluginManager().getPlugin("AdvancedChests");
144168
advChestEnabled = advChest != null;
145169
if (advChestEnabled) {
146-
// Check version
147170
if (compareVersions(advChest.getDescription().getVersion(), "23.0") > 0) {
148171
log("Hooked into AdvancedChests.");
149172
} else {
@@ -153,26 +176,20 @@ public void allLoaded() {
153176
}
154177
}
155178
}
179+
}
156180

157-
// Check if RoseStackers is enabled
158-
if (!settings.getDisabledPluginHooks().contains("RoseStacker")) {
159-
roseStackersEnabled = Bukkit.getPluginManager().isPluginEnabled("RoseStacker");
160-
if (roseStackersEnabled) {
161-
log("Hooked into RoseStackers.");
162-
}
181+
private void hookRoseStackers() {
182+
roseStackersEnabled = Bukkit.getPluginManager().isPluginEnabled("RoseStacker");
183+
if (roseStackersEnabled) {
184+
log("Hooked into RoseStackers.");
163185
}
186+
}
164187

165-
// Check if UltimateStacker is enabled
166-
if (!settings.getDisabledPluginHooks().contains("UltimateStacker")) {
167-
ultimateStackerEnabled = Bukkit.getPluginManager().isPluginEnabled("UltimateStacker");
168-
if (ultimateStackerEnabled) {
169-
log("Hooked into UltimateStacker.");
170-
}
188+
private void hookUltimateStacker() {
189+
ultimateStackerEnabled = Bukkit.getPluginManager().isPluginEnabled("UltimateStacker");
190+
if (ultimateStackerEnabled) {
191+
log("Hooked into UltimateStacker.");
171192
}
172-
173-
if (this.isEnabled()) {
174-
this.hookExtensions();
175-
}
176193
}
177194

178195
/**

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

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -449,77 +449,66 @@ private CompletableFuture<Boolean> scanChunk(List<Chunk> chunks) {
449449
record ChunkPair(World world, Chunk chunk, ChunkSnapshot chunkSnapshot) {
450450
}
451451

452-
/**
453-
* Count the blocks on the island. This method is run async.
454-
*
455-
* @param cp chunk to scan
456-
*/
457452
private void scanAsync(ChunkPair cp) {
458-
for (int x = 0; x < 16; x++) {
459-
// Check if the block coordinate is inside the protection zone and if not, don't
460-
// count it
461-
if (cp.chunkSnapshot.getX() * 16 + x < island.getMinProtectedX() || cp.chunkSnapshot.getX() * 16
462-
+ x >= island.getMinProtectedX() + island.getProtectionRange() * 2) {
463-
continue;
464-
}
465-
for (int z = 0; z < 16; z++) {
466-
// Check if the block coordinate is inside the protection zone and if not, don't
467-
// count it
468-
if (cp.chunkSnapshot.getZ() * 16 + z < island.getMinProtectedZ() || cp.chunkSnapshot.getZ() * 16
469-
+ z >= island.getMinProtectedZ() + island.getProtectionRange() * 2) {
470-
continue;
471-
}
472-
// Only count to the highest block in the world for some optimization
473-
for (int y = cp.world.getMinHeight(); y < cp.world.getMaxHeight(); y++) {
474-
BlockData blockData = cp.chunkSnapshot.getBlockData(x, y, z);
475-
Material m = blockData.getMaterial();
476-
if (m.isAir())
477-
continue;
478-
boolean belowSeaLevel = seaHeight > 0 && y <= seaHeight;
479-
Location loc = new Location(cp.world, (double) x + cp.chunkSnapshot.getX() * 16, y,
480-
(double) z + cp.chunkSnapshot.getZ() * 16);
481-
// Hook for ItemsAdder
482-
if (addon.isItemsAdder() && ItemsAdderHook.getInCustomRegion(loc) != null) {
483-
String namespacedId = ItemsAdderHook.getInCustomRegion(loc);
484-
checkBlock(namespacedId, belowSeaLevel);
485-
continue;
486-
}
453+
int chunkX = cp.chunkSnapshot.getX() * 16;
454+
int chunkZ = cp.chunkSnapshot.getZ() * 16;
455+
int minX = island.getMinProtectedX();
456+
int maxX = minX + island.getProtectionRange() * 2;
457+
int minZ = island.getMinProtectedZ();
458+
int maxZ = minZ + island.getProtectionRange() * 2;
487459

488-
// Slabs can be doubled, so check them twice
489-
if (Tag.SLABS.isTagged(m)) {
490-
Slab slab = (Slab) blockData;
491-
if (slab.getType().equals(Slab.Type.DOUBLE)) {
492-
checkBlock(m, belowSeaLevel);
460+
for (int x = 0; x < 16; x++) {
461+
int globalX = chunkX + x;
462+
if (globalX >= minX && globalX < maxX) {
463+
for (int z = 0; z < 16; z++) {
464+
int globalZ = chunkZ + z;
465+
if (globalZ >= minZ && globalZ < maxZ) {
466+
for (int y = cp.world.getMinHeight(); y < cp.world.getMaxHeight(); y++) {
467+
processBlock(cp, x, y, z, globalX, globalZ);
493468
}
494469
}
495-
// Hook for Wild Stackers (Blocks and Spawners Only) - this has to use the real
496-
// chunk
497-
if (addon.isStackersEnabled() && (m.equals(Material.CAULDRON) || m.equals(Material.SPAWNER))) {
498-
stackedBlocks.add(loc);
499-
}
500-
// Hook for UltimateStacker
501-
if (addon.isUltimateStackerEnabled() && !m.isAir()) {
502-
UltimateStackerCalc.addStackers(m, loc, results, belowSeaLevel, limitCountAndValue(m));
503-
}
470+
}
471+
}
472+
}
473+
}
504474

505-
// Scan chests
506-
if (addon.getSettings().isIncludeChests() && blockData instanceof Container) {
507-
chestBlocks.add(cp.chunk);
508-
}
475+
private void processBlock(ChunkPair cp, int x, int y, int z, int globalX, int globalZ) {
476+
BlockData blockData = cp.chunkSnapshot.getBlockData(x, y, z);
477+
Material m = blockData.getMaterial();
478+
if (m.isAir()) {
479+
return;
480+
}
481+
boolean belowSeaLevel = seaHeight > 0 && y <= seaHeight;
482+
Location loc = new Location(cp.world, globalX, y, globalZ);
509483

510-
// Spawners
511-
if (m == Material.SPAWNER) {
512-
// Stash the spawner because the type cannot be obtained from the chunk snapshot
513-
this.spawners.put(loc, belowSeaLevel);
514-
} else {
515-
// Add the value of the block's material
516-
checkBlock(m, belowSeaLevel);
517-
}
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);
518492
}
519493
}
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 {
506+
checkBlock(m, belowSeaLevel);
507+
}
520508
}
521509
}
522510

511+
523512
/**
524513
* Scan the next chunk on the island
525514
*

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

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.bukkit.Material;
99
import org.bukkit.entity.Player;
10+
import org.bukkit.inventory.ItemStack;
1011
import org.bukkit.inventory.PlayerInventory;
1112
import org.eclipse.jdt.annotation.NonNull;
1213

@@ -45,54 +46,57 @@ public void setup()
4546

4647

4748
@Override
48-
public boolean execute(User user, String label, List<String> args)
49-
{
50-
if (args.size() > 1)
51-
{
52-
this.showHelp(this, user);
49+
public boolean execute(User user, String label, List<String> args) {
50+
if (args.size() > 1) {
51+
showHelp(this, user);
5352
return false;
5453
}
5554

56-
if (args.isEmpty())
57-
{
58-
ValuePanel.openPanel(this.addon, this.getWorld(), user);
55+
if (args.isEmpty()) {
56+
ValuePanel.openPanel(addon, getWorld(), user);
57+
return true;
58+
}
59+
60+
String arg = args.get(0);
61+
if ("HAND".equalsIgnoreCase(arg)) {
62+
return executeHandCommand(user);
5963
}
60-
else if (args.get(0).equalsIgnoreCase("HAND"))
61-
{
62-
Player player = user.getPlayer();
63-
PlayerInventory inventory = player.getInventory();
64-
65-
if (!inventory.getItemInMainHand().getType().equals(Material.AIR))
66-
{
67-
// The user has something in their hand
68-
if (addon.isItemsAdder()) {
69-
Optional<String> id = ItemsAdderHook.getNamespacedId(inventory.getItemInMainHand());
70-
if (id.isPresent()) {
71-
this.printValue(user, id.get());
72-
return true;
73-
}
74-
}
75-
// Not ItemsAdder item
76-
this.printValue(user, inventory.getItemInMainHand().getType());
77-
}
78-
else
79-
{
80-
Utils.sendMessage(user, user.getTranslation("level.conversations.empty-hand"));
81-
}
82-
} else {
83-
Material material = Material.matchMaterial(args.get(0));
8464

85-
if (material == null) {
86-
Utils.sendMessage(user, user.getTranslation(this.getWorld(), "level.conversations.unknown-item",
87-
MATERIAL, args.get(0)));
88-
} else {
89-
this.printValue(user, material);
65+
executeMaterialCommand(user, arg);
66+
return true;
67+
}
68+
69+
private boolean executeHandCommand(User user) {
70+
Player player = user.getPlayer();
71+
PlayerInventory inventory = player.getInventory();
72+
ItemStack mainHandItem = inventory.getItemInMainHand();
73+
74+
if (mainHandItem.getType().equals(Material.AIR)) {
75+
Utils.sendMessage(user, user.getTranslation("level.conversations.empty-hand"));
76+
return true;
77+
}
78+
79+
if (addon.isItemsAdder()) {
80+
Optional<String> id = ItemsAdderHook.getNamespacedId(mainHandItem);
81+
if (id.isPresent()) {
82+
printValue(user, id.get());
83+
return true;
9084
}
9185
}
9286

87+
printValue(user, mainHandItem.getType());
9388
return true;
9489
}
9590

91+
private void executeMaterialCommand(User user, String arg) {
92+
Material material = Material.matchMaterial(arg);
93+
if (material == null) {
94+
Utils.sendMessage(user, user.getTranslation(getWorld(), "level.conversations.unknown-item", MATERIAL, arg));
95+
} else {
96+
printValue(user, material);
97+
}
98+
}
99+
96100
/**
97101
* This method prints value of the given material in chat.
98102
* @param user User who receives the message.

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,14 @@ 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 = (obj instanceof Keyed keyed) ? keyed.getKey().getKey() : (obj instanceof String str ? str : "");
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+
}
209216

210217
if (key.isEmpty()) {
211218
return null;

0 commit comments

Comments
 (0)