Skip to content

Commit

Permalink
Fixed sonar cloud code smells.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jul 5, 2020
1 parent 45577e4 commit ad69385
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 59 deletions.
1 change: 0 additions & 1 deletion src/main/java/world/bentobox/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ private void loadBlockSettings() {
// Disable
logError("Level blockconfig.yml settings could not load! Addon disabled.");
setState(State.DISABLED);
return;
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/world/bentobox/level/LevelsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public void setIslandLevel(@NonNull World world, @NonNull UUID targetPlayer, lon
* @param owner - owner of the island
* @param r - results of the calculation
*/
private void setIslandResults(World world, @Nullable UUID owner, Results r) {
private void setIslandResults(World world, @NonNull UUID owner, Results r) {
LevelsData ld = levelsCache.computeIfAbsent(owner, LevelsData::new);
ld.setLevel(world, r.getLevel());
ld.setUwCount(world, r.getUwCount());
Expand Down
123 changes: 75 additions & 48 deletions src/main/java/world/bentobox/level/config/BlockConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -15,78 +16,104 @@

import world.bentobox.level.Level;

/**
* Contains all the block values, limits and world differences
* @author tastybento
*
*/
public class BlockConfig {

private Map<Material, Integer> blockLimits = new HashMap<>();
private Map<Material, Integer> blockValues = new HashMap<>();
private Map<Material, Integer> blockLimits = new EnumMap<>(Material.class);
private Map<Material, Integer> blockValues = new EnumMap<>(Material.class);
private final Map<World, Map<Material, Integer>> worldBlockValues = new HashMap<>();
private Level addon;

public BlockConfig(Level level, YamlConfiguration blockValues, File file) throws IOException {

/**
* Loads block limits, values and world settings and then saves them again
* @param addon - addon
* @param blockValues - yaml configuration file containing the block values
* @param file - the file representing the yaml config. Will be saved after readong.
* @throws IOException - if there is an error
*/
public BlockConfig(Level addon, YamlConfiguration blockValues, File file) throws IOException {
this.addon = addon;
if (blockValues.isConfigurationSection("limits")) {
HashMap<Material, Integer> bl = new HashMap<>();
ConfigurationSection limits = Objects.requireNonNull(blockValues.getConfigurationSection("limits"));
for (String material : limits.getKeys(false)) {
try {
Material mat = Material.valueOf(material);
bl.put(mat, limits.getInt(material, 0));
} catch (Exception e) {
level.logWarning("Unknown material (" + material + ") in blockconfig.yml Limits section. Skipping...");
}
}
setBlockLimits(bl);
setBlockLimits(loadBlockLimits(blockValues));
}
if (blockValues.isConfigurationSection("blocks")) {
ConfigurationSection blocks = Objects.requireNonNull(blockValues.getConfigurationSection("blocks"));
Map<Material, Integer> bv = new HashMap<>();
// Update blockvalues to latest settings
Arrays.stream(Material.values()).filter(Material::isBlock)
.filter(m -> !m.name().startsWith("LEGACY_"))
.filter(m -> !m.isAir())
.filter(m -> !m.equals(Material.WATER))
.forEach(m -> {
if (!blocks.contains(m.name(), true)) {
blocks.set(m.name(), 1);
}
bv.put(m, blocks.getInt(m.name(), 1));
});
setBlockValues(bv);
setBlockValues(loadBlockValues(blockValues));
} else {
level.logWarning("No block values in blockconfig.yml! All island levels will be zero!");
addon.logWarning("No block values in blockconfig.yml! All island levels will be zero!");
}
// Worlds
if (blockValues.isConfigurationSection("worlds")) {
ConfigurationSection worlds = Objects.requireNonNull(blockValues.getConfigurationSection("worlds"));
for (String world : Objects.requireNonNull(worlds).getKeys(false)) {
World bWorld = Bukkit.getWorld(world);
if (bWorld != null) {
ConfigurationSection worldValues = worlds.getConfigurationSection(world);
for (String material : Objects.requireNonNull(worldValues).getKeys(false)) {
Material mat = Material.valueOf(material);
Map<Material, Integer> values = worldBlockValues.getOrDefault(bWorld, new HashMap<>());
values.put(mat, worldValues.getInt(material));
worldBlockValues.put(bWorld, values);
}
} else {
level.logWarning("Level Addon: No such world in blockconfig.yml : " + world);
}
}
loadWorlds(blockValues);
}
// All done
blockValues.save(file);
}

private void loadWorlds(YamlConfiguration blockValues2) {
ConfigurationSection worlds = Objects.requireNonNull(blockValues2.getConfigurationSection("worlds"));
for (String world : Objects.requireNonNull(worlds).getKeys(false)) {
World bWorld = Bukkit.getWorld(world);
if (bWorld != null) {
ConfigurationSection worldValues = worlds.getConfigurationSection(world);
for (String material : Objects.requireNonNull(worldValues).getKeys(false)) {
Material mat = Material.valueOf(material);
Map<Material, Integer> values = worldBlockValues.getOrDefault(bWorld, new EnumMap<>(Material.class));
values.put(mat, worldValues.getInt(material));
worldBlockValues.put(bWorld, values);
}
} else {
addon.logWarning("Level Addon: No such world in blockconfig.yml : " + world);
}
}

}

private Map<Material, Integer> loadBlockValues(YamlConfiguration blockValues2) {
ConfigurationSection blocks = Objects.requireNonNull(blockValues2.getConfigurationSection("blocks"));
Map<Material, Integer> bv = new EnumMap<>(Material.class);
// Update blockvalues to latest settings
Arrays.stream(Material.values()).filter(Material::isBlock)
.filter(m -> !m.name().startsWith("LEGACY_"))
.filter(m -> !m.isAir())
.filter(m -> !m.equals(Material.WATER))
.forEach(m -> {
if (!blocks.contains(m.name(), true)) {
blocks.set(m.name(), 1);
}
bv.put(m, blocks.getInt(m.name(), 1));
});
return bv;
}

private Map<Material, Integer> loadBlockLimits(YamlConfiguration blockValues2) {
Map<Material, Integer> bl = new EnumMap<>(Material.class);
ConfigurationSection limits = Objects.requireNonNull(blockValues2.getConfigurationSection("limits"));
for (String material : limits.getKeys(false)) {
try {
Material mat = Material.valueOf(material);
bl.put(mat, limits.getInt(material, 0));
} catch (Exception e) {
addon.logWarning("Unknown material (" + material + ") in blockconfig.yml Limits section. Skipping...");
}
}
return bl;
}

/**
* @return the blockLimits
*/
public final Map<Material, Integer> getBlockLimits() {
return blockLimits;
}
/**
* @param blockLimits2 the blockLimits to set
* @param bl the blockLimits to set
*/
private void setBlockLimits(HashMap<Material, Integer> blockLimits2) {
this.blockLimits = blockLimits2;
private void setBlockLimits(Map<Material, Integer> bl) {
this.blockLimits = bl;
}
/**
* @return the blockValues
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/world/bentobox/level/objects/LevelsData.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package world.bentobox.level.objects;

import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -213,7 +214,7 @@ public long getPointsToNextLevel(World world) {
*/
public void setUwCount(World world, Multiset<Material> uwCount) {
if (this.uwCount == null) this.uwCount = new HashMap<>();
Map<Material, Integer> count = new HashMap<>();
Map<Material, Integer> count = new EnumMap<>(Material.class);
uwCount.forEach(m -> count.put(m, uwCount.count(m)));

this.uwCount.put(world.getName(), count);
Expand All @@ -224,7 +225,7 @@ public void setUwCount(World world, Multiset<Material> uwCount) {
*/
public void setMdCount(World world, Multiset<Material> mdCount) {
if (this.mdCount == null) this.mdCount = new HashMap<>();
Map<Material, Integer> count = new HashMap<>();
Map<Material, Integer> count = new EnumMap<>(Material.class);
mdCount.forEach(m -> count.put(m, mdCount.count(m)));

this.mdCount.put(world.getName(), count);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/world/bentobox/level/panels/DetailsGUITab.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -47,7 +47,7 @@ public enum DetailsType {
*/
private static final Map<Material, Material> M2I;
static {
Map<Material, Material> m2i = new HashMap<>();
Map<Material, Material> m2i = new EnumMap<>(Material.class);
m2i.put(Material.WATER, Material.WATER_BUCKET);
m2i.put(Material.LAVA, Material.LAVA_BUCKET);
m2i.put(Material.AIR, Material.BLACK_STAINED_GLASS_PANE);
Expand Down Expand Up @@ -133,7 +133,7 @@ private void generateReport(DetailsType type) {
items = new ArrayList<>();
LevelsData ld = addon.getManager().getLevelsData(island.getOwner());
// Get the items from the report
Map<Material, Integer> sumTotal = new HashMap<>();
Map<Material, Integer> sumTotal = new EnumMap<>(Material.class);
sumTotal.putAll(ld.getMdCount(world));
sumTotal.putAll(ld.getUwCount(world));
switch(type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

public class LevelRequestHandler extends AddonRequestHandler {

private static final Object WORLD_NAME = "world-name";
private static final Object PLAYER = "player";
private final Level addon;

public LevelRequestHandler(Level addon) {
Expand All @@ -32,12 +34,12 @@ public Object handle(Map<String, Object> map) {
*/

if (map == null || map.isEmpty()
|| map.get("world-name") == null || !(map.get("world-name") instanceof String)
|| map.get("player") == null || !(map.get("player") instanceof UUID)
|| Bukkit.getWorld((String) map.get("world-name")) == null) {
|| map.get(WORLD_NAME) == null || !(map.get(WORLD_NAME) instanceof String)
|| map.get(PLAYER) == null || !(map.get(PLAYER) instanceof UUID)
|| Bukkit.getWorld((String) map.get(WORLD_NAME)) == null) {
return 0L;
}

return addon.getManager().getIslandLevel(Bukkit.getWorld((String) map.get("world-name")), (UUID) map.get("player"));
return addon.getManager().getIslandLevel(Bukkit.getWorld((String) map.get(WORLD_NAME)), (UUID) map.get(PLAYER));
}
}

0 comments on commit ad69385

Please sign in to comment.