Skip to content

Commit

Permalink
Code smell removal
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Nov 1, 2019
1 parent 6076f12 commit 64fcefe
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 109 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,14 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<version>0.8.3</version>
<configuration>
<append>true</append>
<excludes>
<!-- This is required to prevent Jacoco from adding synthetic fields
to a JavaBean class (causes errors in testing) -->
<exclude>**/*Names*</exclude>
</excludes>
</configuration>
<executions>
<execution>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/world/bentobox/greenhouses/Greenhouses.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Greenhouses extends Addon {
private Settings settings;
private RecipeManager recipes;
private final List<World> activeWorlds = new ArrayList<>();
public final static Flag GREENHOUSES = new Flag.Builder("GREENHOUSE", Material.GREEN_STAINED_GLASS).build();
public static final Flag GREENHOUSES = new Flag.Builder("GREENHOUSE", Material.GREEN_STAINED_GLASS).build();

/* (non-Javadoc)
* @see world.bentobox.bentobox.api.addons.Addon#onEnable()
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/world/bentobox/greenhouses/data/Greenhouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Greenhouse implements DataObject {
private String biomeRecipeName;

private boolean broken;

private Map<Material, Integer> missingBlocks;

/**
Expand All @@ -48,7 +48,7 @@ public Greenhouse() {}

public Greenhouse(World world, Walls walls, int ceilingHeight) {
this.location = new Location(world, walls.getMinX(), walls.getFloor(), walls.getMinZ());
Location location2 = new Location(world, walls.getMaxX() + 1, ceilingHeight + 1, walls.getMaxZ() + 1);
Location location2 = new Location(world, walls.getMaxX() + 1D, ceilingHeight + 1D, walls.getMaxZ() + 1D);
this.boundingBox = BoundingBox.of(location, location2);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package world.bentobox.greenhouses.greenhouse;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -31,18 +30,19 @@
import world.bentobox.greenhouses.managers.GreenhouseManager.GreenhouseResult;

public class BiomeRecipe implements Comparable<BiomeRecipe> {
private static final String CHANCE_FOR = "% chance for ";
private Greenhouses addon;
private Biome type;
private Material icon; // Biome icon for control panel
private int priority;
private String name;
private String friendlyName;

private final List<BlockFace> ADJ_BLOCKS = Arrays.asList( BlockFace.DOWN, BlockFace.EAST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.UP, BlockFace.WEST);
private final List<BlockFace> adjBlocks = Arrays.asList( BlockFace.DOWN, BlockFace.EAST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.UP, BlockFace.WEST);

// Content requirements
// Material, Type, Qty. There can be more than one type of material required
private final Map<Material, Integer> requiredBlocks = new HashMap<>();
private final Map<Material, Integer> requiredBlocks = new EnumMap<>(Material.class);
// Plants
private final TreeMap<Double, GreenhousePlant> plantTree = new TreeMap<>();

Expand All @@ -52,7 +52,7 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {

// Conversions
// Original Material, Original Type, New Material, New Type, Probability
private final Map<Material, GreenhouseBlockConversions> conversionBlocks = new HashMap<>();
private final Map<Material, GreenhouseBlockConversions> conversionBlocks = new EnumMap<>(Material.class);

private int mobLimit;
private int waterCoverage;
Expand Down Expand Up @@ -85,7 +85,7 @@ public BiomeRecipe(Greenhouses addon, Biome type, int priority) {
public void addConvBlocks(Material oldMaterial, Material newMaterial, double convChance, Material localMaterial) {
double probability = Math.min(convChance/100 , 1D);
conversionBlocks.put(oldMaterial, new GreenhouseBlockConversions(oldMaterial, newMaterial, probability, localMaterial));
addon.log(" " + convChance + "% chance for " + Util.prettifyText(oldMaterial.toString()) + " to convert to " + Util.prettifyText(newMaterial.toString()));
addon.log(" " + convChance + CHANCE_FOR + Util.prettifyText(oldMaterial.toString()) + " to convert to " + Util.prettifyText(newMaterial.toString()));
}


Expand All @@ -96,7 +96,7 @@ public void addConvBlocks(Material oldMaterial, Material newMaterial, double con
* @return true if add is successful
*/
public boolean addMobs(EntityType mobType, int mobProbability, Material mobSpawnOn) {
addon.log(" " + mobProbability + "% chance for " + Util.prettifyText(mobType.toString()) + " to spawn on " + Util.prettifyText(mobSpawnOn.toString())+ ".");
addon.log(" " + mobProbability + CHANCE_FOR + Util.prettifyText(mobType.toString()) + " to spawn on " + Util.prettifyText(mobSpawnOn.toString())+ ".");
double probability = ((double)mobProbability/100);
double lastProb = mobTree.isEmpty() ? 0D : mobTree.lastKey();
// Add up all the probabilities in the list so far
Expand Down Expand Up @@ -129,7 +129,7 @@ public boolean addPlants(Material plantMaterial, int plantProbability, Material
addon.logError("Plant chances add up to > 100% in " + type.toString() + " biome recipe! Skipping " + plantMaterial.toString());
return false;
}
addon.log(" " + plantProbability + "% chance for " + Util.prettifyText(plantMaterial.toString()) + " to grow on " + Util.prettifyText(plantGrowOn.toString()));
addon.log(" " + plantProbability + CHANCE_FOR + Util.prettifyText(plantMaterial.toString()) + " to grow on " + Util.prettifyText(plantGrowOn.toString()));
return true;
}

Expand All @@ -150,16 +150,16 @@ public void addReqBlocks(Material blockMaterial, int blockQty) {
public Set<GreenhouseResult> checkRecipe(Greenhouse gh) {
Set<GreenhouseResult> result = new HashSet<>();
long area = gh.getArea();
Map<Material, Integer> blockCount = new HashMap<>();
Map<Material, Integer> blockCount = new EnumMap<>(Material.class);
// Look through the greenhouse and count what is in there
for (int y = gh.getFloorHeight(); y< gh.getCeilingHeight();y++) {
for (int x = (int) (gh.getBoundingBox().getMinX()+1); x < gh.getBoundingBox().getMaxX(); x++) {
for (int z = (int) (gh.getBoundingBox().getMinZ()+1); z < gh.getBoundingBox().getMaxZ(); z++) {
Block b = gh.getWorld().getBlockAt(x, y, z);
Material type = b.getType();
if (!type.equals(Material.AIR)) {
blockCount.putIfAbsent(type, 0);
blockCount.merge(type, 1, Integer::sum);
Material t = b.getType();
if (!t.equals(Material.AIR)) {
blockCount.putIfAbsent(t, 0);
blockCount.merge(t, 1, Integer::sum);
}
}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ public void convertBlock(Block b) {
return;
}
// Check if the block is in the right area, up, down, n,s,e,w
if (ADJ_BLOCKS.stream().map(b::getRelative).map(Block::getType).anyMatch(m -> bc.getLocalMaterial() == null || m == bc.getLocalMaterial())) {
if (adjBlocks.stream().map(b::getRelative).map(Block::getType).anyMatch(m -> bc.getLocalMaterial() == null || m == bc.getLocalMaterial())) {
// Convert!
b.setType(bc.getNewMaterial());
}
Expand Down Expand Up @@ -476,18 +476,20 @@ public int compareTo(BiomeRecipe o) {
return Integer.compare(o.getPriority(), this.getPriority());
}


/**
* @return true if this recipe has no mobs that may spawn
*/
public boolean noMobs() {
return mobTree == null ? false : mobTree.isEmpty();
return mobTree.isEmpty();
}

/**
* @return the mob types that may spawn due to this recipe
*/
public Set<EntityType> getMobTypes() {
return mobTree == null ? Collections.emptySet() : mobTree.values().stream().map(GreenhouseMob::getMobType).collect(Collectors.toSet());
return mobTree.values().stream().map(GreenhouseMob::getMobType).collect(Collectors.toSet());
}


}
44 changes: 25 additions & 19 deletions src/main/java/world/bentobox/greenhouses/greenhouse/Roof.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ public class Roof {
private int maxZ;
private final int height;
private boolean roofFound;
public final static List<Material> ROOFBLOCKS = Arrays.stream(Material.values())
.filter(Material::isBlock) // Blocks only, no items
.filter(m -> !m.name().contains("DOOR")) // No doors
.filter(m -> m.name().contains("TRAPDOOR") // All trapdoors
|| m.name().contains("GLASS") // All glass blocks
|| m.equals(Material.HOPPER) // Hoppers
|| m.equals(Material.GLOWSTONE)) // Glowstone
.collect(Collectors.toList());

/**
* @return a list of valid roof blocks
*/
public static List<Material> getRoofBlocks() {
return Arrays.stream(Material.values())
.filter(Material::isBlock) // Blocks only, no items
.filter(m -> !m.name().contains("DOOR")) // No doors
.filter(m -> m.name().contains("TRAPDOOR") // All trapdoors
|| m.name().contains("GLASS") // All glass blocks
|| m.equals(Material.HOPPER) // Hoppers
|| m.equals(Material.GLOWSTONE)) // Glowstone
.collect(Collectors.toList());
}

/**
* Finds a roof from a starting location under the roof and characterizes it
* @param loc - starting location
Expand All @@ -55,10 +62,9 @@ public Roof(Location loc) {
if (!Walls.isWallBlock(b.getType())) {
// Look up
for (int y = roofY; y < world.getMaxHeight(); y++) {
if (ROOFBLOCKS.contains(world.getBlockAt(x,y,z).getType())) {
if (getRoofBlocks().contains(world.getBlockAt(x,y,z).getType())) {
roofFound = true;
loc = new Location(world,x,y,z);
//plugin.logger(3,"Roof block found at " + x + " " + y + " " + z + " of type " + loc.getBlock().getType().toString());
break;
}
}
Expand All @@ -76,7 +82,7 @@ public Roof(Location loc) {
break;
}
}
//}

// Record the height
this.height = loc.getBlockY();
// Now we have a roof block, find how far we can go NSWE
Expand Down Expand Up @@ -118,7 +124,7 @@ private void expandCoords(World world, Vector height) {
Location maxz = height.toLocation(world);
Location minz = height.toLocation(world);
int limit = 0;
while (ROOFBLOCKS
while (getRoofBlocks()
.contains(world.getBlockAt(maxx).getType()) && limit < 100) {
limit++;
maxx.add(new Vector(1,0,0));
Expand All @@ -127,52 +133,52 @@ private void expandCoords(World world, Vector height) {
maxX = maxx.getBlockX()-1;
}

while (ROOFBLOCKS.contains(world.getBlockAt(minx).getType()) && limit < 200) {
while (getRoofBlocks().contains(world.getBlockAt(minx).getType()) && limit < 200) {
limit++;
minx.subtract(new Vector(1,0,0));
}
if (minx.getBlockX() + 1 < minX) {
minX = minx.getBlockX() + 1;
}

while (ROOFBLOCKS.contains(world.getBlockAt(maxz).getType()) && limit < 300) {
while (getRoofBlocks().contains(world.getBlockAt(maxz).getType()) && limit < 300) {
limit++;
maxz.add(new Vector(0,0,1));
}
if (maxz.getBlockZ() - 1 > maxZ) {
maxZ = maxz.getBlockZ() - 1;
}

while (ROOFBLOCKS.contains(world.getBlockAt(minz).getType()) && limit < 400) {
while (getRoofBlocks().contains(world.getBlockAt(minz).getType()) && limit < 400) {
limit++;
minz.subtract(new Vector(0,0,1));
}
if (minz.getBlockZ() + 1 < minZ) {
minZ = minz.getBlockZ() + 1;
}
}

/**
* @return the minX
*/
public int getMinX() {
return minX;
}

/**
* @return the maxX
*/
public int getMaxX() {
return maxX;
}

/**
* @return the minZ
*/
public int getMinZ() {
return minZ;
}

/**
* @return the maxZ
*/
Expand Down
35 changes: 20 additions & 15 deletions src/main/java/world/bentobox/greenhouses/greenhouse/Walls.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ public class Walls {
private int maxZ;
private int floor;

public final static List<Material> WALL_BLOCKS = Arrays.stream(Material.values())
.filter(Material::isBlock) // Blocks only, no items
.filter(m -> !m.name().contains("TRAPDOOR")) // No trap doors
.filter(m -> m.name().contains("DOOR") // All doors
|| m.name().contains("GLASS") // All glass blocks
|| m.equals(Material.HOPPER) // Hoppers
|| m.equals(Material.GLOWSTONE)) // Glowstone
.collect(Collectors.toList());
public static final List<BlockFace> ORDINALS = Arrays.asList(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST);

public final static List<BlockFace> ORDINALS = Arrays.asList(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST);
/**
* @return list of valid wall blocks
*/
public static List<Material> getWallBlocks() {
return Arrays.stream(Material.values())
.filter(Material::isBlock) // Blocks only, no items
.filter(m -> !m.name().contains("TRAPDOOR")) // No trap doors
.filter(m -> m.name().contains("DOOR") // All doors
|| m.name().contains("GLASS") // All glass blocks
|| m.equals(Material.HOPPER) // Hoppers
|| m.equals(Material.GLOWSTONE)) // Glowstone
.collect(Collectors.toList());
}

public Walls(Roof roof) {
// The player is under the roof
Expand Down Expand Up @@ -63,25 +68,25 @@ public Walls(Roof roof) {
switch (bf) {
case EAST:
// positive x
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
if (getWallBlocks().contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
stopMaxX = true;
}
break;
case WEST:
// negative x
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
if (getWallBlocks().contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
stopMinX = true;
}
break;
case NORTH:
// negative Z
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
if (getWallBlocks().contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
stopMinZ = true;
}
break;
case SOUTH:
// positive Z
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
if (getWallBlocks().contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
stopMaxZ = true;
}
break;
Expand Down Expand Up @@ -135,7 +140,7 @@ private int getFloorY(World world, int y, int minX, int maxX, int minZ, int maxZ
wallBlockCount = 0;
for (int x = minX; x <= maxX; x++) {
for (int z = minZ; z <= maxZ; z++) {
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getType())) {
if (getWallBlocks().contains(world.getBlockAt(x, y, z).getType())) {
wallBlockCount++;
}
}
Expand Down Expand Up @@ -184,7 +189,7 @@ public int getFloor() {
}

public static boolean isWallBlock(Material blockType) {
return WALL_BLOCKS.contains(blockType);
return getWallBlocks().contains(blockType);
}

/**
Expand Down

0 comments on commit 64fcefe

Please sign in to comment.