Skip to content

Commit 4d92cc6

Browse files
tastybentoclaude
andcommitted
Reduce complexity of onPlayerMove() and getWorld() by extracting helpers
AcidEffect: extracts isExemptFromAcid(), handleRainExposure(), and startAcidBurn() from onPlayerMove() to flatten nesting and separate concerns. AcidIsland: extracts getWorldName() and configureSpawnRates() from getWorld() to bring each method within SonarCloud's cognitive complexity limit. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a453d2d commit 4d92cc6

2 files changed

Lines changed: 50 additions & 60 deletions

File tree

src/main/java/world/bentobox/acidisland/AcidIsland.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -153,34 +153,29 @@ public void createWorlds() {
153153
* @return world loaded or generated
154154
*/
155155
private World getWorld(String worldName2, Environment env, @Nullable ChunkGenerator chunkGenerator2) {
156-
// Set world name
157-
worldName2 = env.equals(World.Environment.NETHER) ? worldName2 + NETHER : worldName2;
158-
worldName2 = env.equals(World.Environment.THE_END) ? worldName2 + THE_END : worldName2;
159-
WorldCreator wc = WorldCreator.name(worldName2).environment(env).type(WorldType.NORMAL);
156+
String name = getWorldName(worldName2, env);
157+
WorldCreator wc = WorldCreator.name(name).environment(env).type(WorldType.NORMAL);
160158
World w = settings.isUseOwnGenerator() ? wc.createWorld() : wc.generator(chunkGenerator2).createWorld();
161-
// Set spawn rates
162159
if (w != null && getSettings() != null) {
163-
if (getSettings().getSpawnLimitMonsters() > 0) {
164-
w.setSpawnLimit(SpawnCategory.MONSTER, getSettings().getSpawnLimitMonsters());
165-
}
166-
if (getSettings().getSpawnLimitAmbient() > 0) {
167-
w.setSpawnLimit(SpawnCategory.AMBIENT, getSettings().getSpawnLimitAmbient());
168-
}
169-
if (getSettings().getSpawnLimitAnimals() > 0) {
170-
w.setSpawnLimit(SpawnCategory.ANIMAL, getSettings().getSpawnLimitAnimals());
171-
}
172-
if (getSettings().getSpawnLimitWaterAnimals() > 0) {
173-
w.setSpawnLimit(SpawnCategory.WATER_ANIMAL, getSettings().getSpawnLimitWaterAnimals());
174-
}
175-
if (getSettings().getTicksPerAnimalSpawns() > 0) {
176-
w.setTicksPerSpawns(SpawnCategory.ANIMAL, getSettings().getTicksPerAnimalSpawns());
177-
}
178-
if (getSettings().getTicksPerMonsterSpawns() > 0) {
179-
w.setTicksPerSpawns(SpawnCategory.MONSTER, getSettings().getTicksPerMonsterSpawns());
180-
}
160+
configureSpawnRates(w);
181161
}
182162
return w;
163+
}
164+
165+
private String getWorldName(String base, Environment env) {
166+
if (env.equals(World.Environment.NETHER)) return base + NETHER;
167+
if (env.equals(World.Environment.THE_END)) return base + THE_END;
168+
return base;
169+
}
183170

171+
private void configureSpawnRates(World w) {
172+
AISettings s = getSettings();
173+
if (s.getSpawnLimitMonsters() > 0) w.setSpawnLimit(SpawnCategory.MONSTER, s.getSpawnLimitMonsters());
174+
if (s.getSpawnLimitAmbient() > 0) w.setSpawnLimit(SpawnCategory.AMBIENT, s.getSpawnLimitAmbient());
175+
if (s.getSpawnLimitAnimals() > 0) w.setSpawnLimit(SpawnCategory.ANIMAL, s.getSpawnLimitAnimals());
176+
if (s.getSpawnLimitWaterAnimals() > 0) w.setSpawnLimit(SpawnCategory.WATER_ANIMAL, s.getSpawnLimitWaterAnimals());
177+
if (s.getTicksPerAnimalSpawns() > 0) w.setTicksPerSpawns(SpawnCategory.ANIMAL, s.getTicksPerAnimalSpawns());
178+
if (s.getTicksPerMonsterSpawns() > 0) w.setTicksPerSpawns(SpawnCategory.MONSTER, s.getTicksPerMonsterSpawns());
184179
}
185180

186181
@Override

src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -108,58 +108,53 @@ public void onSeaBounce(PlayerMoveEvent e) {
108108
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
109109
public void onPlayerMove(PlayerMoveEvent e) {
110110
Player player = e.getPlayer();
111-
// Fast checks
112-
if ((addon.getSettings().getAcidRainDamage() == 0 && addon.getSettings().getAcidDamage() == 0)
113-
|| player.isDead() || player.getGameMode().equals(GameMode.CREATIVE)
111+
if (isExemptFromAcid(player)) {
112+
return;
113+
}
114+
handleRainExposure(player);
115+
if (!burningPlayers.containsKey(player) && !isSafeFromAcid(player)) {
116+
startAcidBurn(player);
117+
}
118+
}
119+
120+
private boolean isExemptFromAcid(Player player) {
121+
return (addon.getSettings().getAcidRainDamage() == 0 && addon.getSettings().getAcidDamage() == 0)
122+
|| player.isDead()
123+
|| player.getGameMode().equals(GameMode.CREATIVE)
114124
|| player.getGameMode().equals(GameMode.SPECTATOR)
115125
|| addon.getPlayers().isInTeleport(player.getUniqueId())
116126
|| !Util.sameWorld(addon.getOverWorld(), player.getWorld())
117127
|| (!player.isOp() && player.hasPermission("acidisland.mod.noburn"))
118-
|| (player.isOp() && !addon.getSettings().isAcidDamageOp())) {
128+
|| (player.isOp() && !addon.getSettings().isAcidDamageOp());
129+
}
130+
131+
private void handleRainExposure(Player player) {
132+
if (addon.getSettings().getAcidRainDamage() <= 0D || !addon.getOverWorld().hasStorm()) {
119133
return;
120134
}
121-
// Slow checks
122-
// Check for acid rain
123-
if (addon.getSettings().getAcidRainDamage() > 0D && addon.getOverWorld().hasStorm()) {
124-
if (isSafeFromRain(player)) {
125-
wetPlayers.remove(player);
126-
} else if (!wetPlayers.containsKey(player)) {
127-
// Start hurting them
128-
// Add to the list
129-
wetPlayers.put(player, System.currentTimeMillis() + addon.getSettings().getAcidDamageDelay() * 1000);
130-
// This runnable continuously hurts the player even if
131-
// they are not
132-
// moving but are in acid rain.
133-
134-
new BukkitRunnable() {
135-
@Override
136-
public void run() {
137-
// Check if it is still raining or player is safe or dead or there is no damage
138-
if (checkForRain(player)) {
139-
this.cancel();
140-
}
141-
135+
if (isSafeFromRain(player)) {
136+
wetPlayers.remove(player);
137+
} else if (!wetPlayers.containsKey(player)) {
138+
wetPlayers.put(player, System.currentTimeMillis() + addon.getSettings().getAcidDamageDelay() * 1000);
139+
new BukkitRunnable() {
140+
@Override
141+
public void run() {
142+
if (checkForRain(player)) {
143+
this.cancel();
142144
}
143-
}.runTaskTimer(addon.getPlugin(), 0L, 20L);
144-
}
145-
146-
}
147-
// If they are already burning in acid then return
148-
if (burningPlayers.containsKey(player) || isSafeFromAcid(player)) {
149-
return;
145+
}
146+
}.runTaskTimer(addon.getPlugin(), 0L, 20L);
150147
}
151-
// ACID!
152-
// Put the player into the acid list
148+
}
149+
150+
private void startAcidBurn(Player player) {
153151
burningPlayers.put(player, System.currentTimeMillis() + addon.getSettings().getAcidDamageDelay() * 1000);
154-
// This runnable continuously hurts the player even if they are not
155-
// moving but are in acid.
156152
new BukkitRunnable() {
157153
@Override
158154
public void run() {
159155
if (continuouslyHurtPlayer(player)) {
160156
this.cancel();
161157
}
162-
163158
}
164159
}.runTaskTimer(addon.getPlugin(), 0L, 20L);
165160
}

0 commit comments

Comments
 (0)