Fixed sonar cloud code smells.

This commit is contained in:
tastybento 2020-07-05 16:52:03 -07:00
parent 45577e446f
commit ad69385607
6 changed files with 88 additions and 59 deletions

View File

@ -192,7 +192,6 @@ public class Level extends Addon implements Listener {
// Disable
logError("Level blockconfig.yml settings could not load! Addon disabled.");
setState(State.DISABLED);
return;
}
}

View File

@ -455,7 +455,7 @@ public class LevelsManager {
* @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());

View File

@ -3,6 +3,7 @@ package world.bentobox.level.config;
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;
@ -15,67 +16,93 @@ import org.bukkit.configuration.file.YamlConfiguration;
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
*/
@ -83,10 +110,10 @@ public class BlockConfig {
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

View File

@ -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;
@ -213,7 +214,7 @@ public class LevelsData implements DataObject {
*/
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);
@ -224,7 +225,7 @@ public class LevelsData implements DataObject {
*/
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);

View File

@ -5,7 +5,7 @@ package world.bentobox.level.panels;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
@ -47,7 +47,7 @@ public class DetailsGUITab implements Tab, ClickHandler {
*/
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);
@ -133,7 +133,7 @@ public class DetailsGUITab implements Tab, ClickHandler {
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) {

View File

@ -10,6 +10,8 @@ import world.bentobox.level.Level;
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) {
@ -32,12 +34,12 @@ public class LevelRequestHandler extends AddonRequestHandler {
*/
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));
}
}