mirror of
https://github.com/BentoBoxWorld/Greenhouses.git
synced 2025-02-01 12:51:31 +01:00
Better. Needs tests to fix bugs.
This commit is contained in:
parent
71383b0adb
commit
3a05faace0
@ -19,6 +19,17 @@ import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
|
|||||||
*/
|
*/
|
||||||
public class Greenhouse implements DataObject {
|
public class Greenhouse implements DataObject {
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Object#toString()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Greenhouse [uniqueId=" + uniqueId + ", location=" + location + ", footprint=" + footprint
|
||||||
|
+ ", ceilingHeight=" + ceilingHeight + ", originalBiome=" + originalBiome + ", greenhouseBiome="
|
||||||
|
+ greenhouseBiome + ", roofHopperLocation=" + roofHopperLocation + ", biomeRecipe=" + biomeRecipe.getName()
|
||||||
|
+ ", broken=" + broken + "]";
|
||||||
|
}
|
||||||
|
|
||||||
private String uniqueId = UUID.randomUUID().toString();
|
private String uniqueId = UUID.randomUUID().toString();
|
||||||
private Location location;
|
private Location location;
|
||||||
private Rectangle footprint;
|
private Rectangle footprint;
|
||||||
@ -192,4 +203,19 @@ public class Greenhouse implements DataObject {
|
|||||||
return this.getLocation().getWorld();
|
return this.getLocation().getWorld();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a location is inside the greenhouse
|
||||||
|
* @param location2 - location to check
|
||||||
|
* @return true if inside the greenhouse
|
||||||
|
*/
|
||||||
|
public boolean contains(Location location2) {
|
||||||
|
return (location.getWorld().equals(location2.getWorld())
|
||||||
|
&& location2.getBlockY() <= this.ceilingHeight
|
||||||
|
&& location2.getBlockY() >= this.getFloorHeight()
|
||||||
|
&& location2.getBlockX() >= (int)this.footprint.getMinX()
|
||||||
|
&& location2.getBlockX() <= (int)this.footprint.getMaxX()
|
||||||
|
&& location2.getBlockZ() >= (int)this.footprint.getMinY()
|
||||||
|
&& location2.getBlockZ() <= (int)this.footprint.getMaxY());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -227,4 +227,13 @@ public class Walls {
|
|||||||
public static boolean isWallBlock(Material blockType) {
|
public static boolean isWallBlock(Material blockType) {
|
||||||
return WALL_BLOCKS.contains(blockType);
|
return WALL_BLOCKS.contains(blockType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Object#toString()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Walls [minX=" + minX + ", maxX=" + maxX + ", minZ=" + minZ + ", maxZ=" + maxZ + ", floor=" + floor
|
||||||
|
+ "]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import java.awt.Rectangle;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
@ -32,7 +33,7 @@ public class GreenhouseFinder {
|
|||||||
}
|
}
|
||||||
// Find the walls
|
// Find the walls
|
||||||
Walls walls = new Walls(roof);
|
Walls walls = new Walls(roof);
|
||||||
|
Bukkit.getLogger().info("DEBUG: walls = " + walls.toString());
|
||||||
// Make the initial greenhouse
|
// Make the initial greenhouse
|
||||||
gh = new Greenhouse(location.getWorld(), new Rectangle(walls.getMinX(), walls.getMinZ(), walls.getMaxX(), walls.getMaxZ()), walls.getFloor(), roof.getHeight());
|
gh = new Greenhouse(location.getWorld(), new Rectangle(walls.getMinX(), walls.getMinZ(), walls.getMaxX(), walls.getMaxZ()), walls.getFloor(), roof.getHeight());
|
||||||
// Set the original biome
|
// Set the original biome
|
||||||
|
@ -12,11 +12,13 @@ import world.bentobox.bentobox.database.Database;
|
|||||||
import world.bentobox.greenhouses.Greenhouses;
|
import world.bentobox.greenhouses.Greenhouses;
|
||||||
import world.bentobox.greenhouses.data.Greenhouse;
|
import world.bentobox.greenhouses.data.Greenhouse;
|
||||||
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
|
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
|
||||||
import world.bentobox.greenhouses.managers.GreenhouseMap.AddResult;
|
|
||||||
|
|
||||||
public class GreenhouseManager {
|
public class GreenhouseManager {
|
||||||
|
|
||||||
// No result = success
|
/**
|
||||||
|
* Result of greenhouse making
|
||||||
|
*
|
||||||
|
*/
|
||||||
public enum GreenhouseResult {
|
public enum GreenhouseResult {
|
||||||
FAIL_NO_ROOF,
|
FAIL_NO_ROOF,
|
||||||
FAIL_BELOW,
|
FAIL_BELOW,
|
||||||
@ -33,7 +35,12 @@ public class GreenhouseManager {
|
|||||||
FAIL_NO_ICE,
|
FAIL_NO_ICE,
|
||||||
FAIL_INSUFFICIENT_WATER,
|
FAIL_INSUFFICIENT_WATER,
|
||||||
FAIL_INSUFFICIENT_LAVA,
|
FAIL_INSUFFICIENT_LAVA,
|
||||||
FAIL_INSUFFICIENT_ICE
|
FAIL_INSUFFICIENT_ICE,
|
||||||
|
FAIL_NO_ISLAND,
|
||||||
|
FAIL_OVERLAPPING,
|
||||||
|
NULL,
|
||||||
|
SUCCESS,
|
||||||
|
FAIL_NO_RECIPE_FOUND
|
||||||
}
|
}
|
||||||
|
|
||||||
private Greenhouses addon;
|
private Greenhouses addon;
|
||||||
@ -60,7 +67,7 @@ public class GreenhouseManager {
|
|||||||
public void loadGreenhouses() {
|
public void loadGreenhouses() {
|
||||||
addon.log("Loading greenhouses...");
|
addon.log("Loading greenhouses...");
|
||||||
handler.loadObjects().forEach(g -> {
|
handler.loadObjects().forEach(g -> {
|
||||||
AddResult result = map.addGreenhouse(g);
|
GreenhouseResult result = map.addGreenhouse(g);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case FAIL_NO_ISLAND:
|
case FAIL_NO_ISLAND:
|
||||||
case FAIL_OVERLAPPING:
|
case FAIL_OVERLAPPING:
|
||||||
@ -141,14 +148,15 @@ public class GreenhouseManager {
|
|||||||
}
|
}
|
||||||
return new GhResult().setFinder(finder).setResults(resultSet);
|
return new GhResult().setFinder(finder).setResults(resultSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try ordered recipes
|
// Try ordered recipes
|
||||||
addon.getRecipes().getBiomeRecipes().stream().sorted()
|
resultSet.add(addon.getRecipes().getBiomeRecipes().stream().sorted()
|
||||||
.filter(r -> r.checkRecipe(finder.getGh()).isEmpty()).findFirst()
|
.filter(r -> r.checkRecipe(finder.getGh()).isEmpty()).findFirst()
|
||||||
.ifPresent(r -> {
|
.map(r -> {
|
||||||
// Success - set recipe and add to map
|
// Success - set recipe and add to map
|
||||||
finder.getGh().setBiomeRecipe(r);
|
finder.getGh().setBiomeRecipe(r);
|
||||||
map.addGreenhouse(finder.getGh());
|
return map.addGreenhouse(finder.getGh());
|
||||||
});
|
}).orElse(GreenhouseResult.FAIL_NO_RECIPE_FOUND));
|
||||||
return new GhResult().setFinder(finder).setResults(resultSet);
|
return new GhResult().setFinder(finder).setResults(resultSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,20 +7,16 @@ import java.util.Map;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
|
||||||
import world.bentobox.bentobox.database.objects.Island;
|
import world.bentobox.bentobox.database.objects.Island;
|
||||||
import world.bentobox.greenhouses.Greenhouses;
|
import world.bentobox.greenhouses.Greenhouses;
|
||||||
import world.bentobox.greenhouses.data.Greenhouse;
|
import world.bentobox.greenhouses.data.Greenhouse;
|
||||||
|
import world.bentobox.greenhouses.managers.GreenhouseManager.GreenhouseResult;
|
||||||
|
|
||||||
public class GreenhouseMap {
|
public class GreenhouseMap {
|
||||||
|
|
||||||
public enum AddResult {
|
|
||||||
SUCCESS,
|
|
||||||
FAIL_OVERLAPPING,
|
|
||||||
FAIL_NO_ISLAND,
|
|
||||||
NULL
|
|
||||||
}
|
|
||||||
private Greenhouses addon;
|
private Greenhouses addon;
|
||||||
private Map<Island, List<Greenhouse>> greenhouses = new HashMap<>();
|
private Map<Island, List<Greenhouse>> greenhouses = new HashMap<>();
|
||||||
|
|
||||||
@ -34,22 +30,22 @@ public class GreenhouseMap {
|
|||||||
/**
|
/**
|
||||||
* Try to add a greenhouse
|
* Try to add a greenhouse
|
||||||
* @param greenhouse - greenhouse object
|
* @param greenhouse - greenhouse object
|
||||||
* @return {@link AddResult#SUCCESS}, {@link AddResult#FAIL_NO_ISLAND} or {@link AddResult#FAIL_OVERLAPPING}
|
* @return result {@link GreenhouseResult}
|
||||||
*/
|
*/
|
||||||
public AddResult addGreenhouse(Greenhouse greenhouse) {
|
public GreenhouseResult addGreenhouse(Greenhouse greenhouse) {
|
||||||
if (greenhouse.getLocation() == null) {
|
if (greenhouse.getLocation() == null) {
|
||||||
return AddResult.NULL;
|
return GreenhouseResult.NULL;
|
||||||
}
|
}
|
||||||
return addon.getIslands().getIslandAt(greenhouse.getLocation()).map(i -> {
|
return addon.getIslands().getIslandAt(greenhouse.getLocation()).map(i -> {
|
||||||
greenhouses.putIfAbsent(i, new ArrayList<>());
|
greenhouses.putIfAbsent(i, new ArrayList<>());
|
||||||
// Check if overlapping
|
// Check if overlapping
|
||||||
if (!isOverlapping(greenhouse)) {
|
if (!isOverlapping(greenhouse)) {
|
||||||
greenhouses.get(i).add(greenhouse);
|
greenhouses.get(i).add(greenhouse);
|
||||||
return AddResult.SUCCESS;
|
return GreenhouseResult.SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
return AddResult.FAIL_OVERLAPPING;
|
return GreenhouseResult.FAIL_OVERLAPPING;
|
||||||
}
|
}
|
||||||
}).orElse(AddResult.FAIL_NO_ISLAND);
|
}).orElse(GreenhouseResult.FAIL_NO_ISLAND);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,10 +58,20 @@ public class GreenhouseMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Optional<Greenhouse> getXZGreenhouse(Location location) {
|
private Optional<Greenhouse> getXZGreenhouse(Location location) {
|
||||||
return addon.getIslands().getIslandAt(location).map(i -> {
|
return addon.getIslands().getIslandAt(location)
|
||||||
greenhouses.putIfAbsent(i, new ArrayList<>());
|
.filter(i -> greenhouses.containsKey(i))
|
||||||
return greenhouses.get(i).stream().filter(g -> g.getFootprint().contains(location.getX(), location.getY())).findFirst();
|
.map(i -> {
|
||||||
}).orElse(Optional.empty());
|
for (Greenhouse gh : greenhouses.get(i)) {
|
||||||
|
Bukkit.getLogger().info("Trying " + location);
|
||||||
|
Bukkit.getLogger().info(gh.toString());
|
||||||
|
if (gh.contains(location)) {
|
||||||
|
Bukkit.getLogger().info("inside gh");
|
||||||
|
return Optional.of(gh);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Bukkit.getLogger().info("None found");
|
||||||
|
return null;
|
||||||
|
}).orElse(Optional.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,6 +14,7 @@ import org.bukkit.configuration.InvalidConfigurationException;
|
|||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.entity.EntityType;
|
import org.bukkit.entity.EntityType;
|
||||||
|
|
||||||
|
import world.bentobox.bentobox.util.Util;
|
||||||
import world.bentobox.greenhouses.Greenhouses;
|
import world.bentobox.greenhouses.Greenhouses;
|
||||||
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
|
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
|
||||||
|
|
||||||
@ -76,7 +77,7 @@ public class RecipeManager {
|
|||||||
b.setPermission(biomeRecipe.getString("permission",""));
|
b.setPermission(biomeRecipe.getString("permission",""));
|
||||||
// Set the icon
|
// Set the icon
|
||||||
b.setIcon(Material.valueOf(biomeRecipe.getString("icon", "SAPLING")));
|
b.setIcon(Material.valueOf(biomeRecipe.getString("icon", "SAPLING")));
|
||||||
b.setFriendlyName(ChatColor.translateAlternateColorCodes('&', biomeRecipe.getString("friendlyname", "")));
|
b.setFriendlyName(ChatColor.translateAlternateColorCodes('&', biomeRecipe.getString("friendlyname", Util.prettifyText(type))));
|
||||||
// A value of zero on these means that there must be NO coverage, e.g., desert. If the value is not present, then the default is -1
|
// A value of zero on these means that there must be NO coverage, e.g., desert. If the value is not present, then the default is -1
|
||||||
b.setWatercoverage(biomeRecipe.getInt("watercoverage",-1));
|
b.setWatercoverage(biomeRecipe.getInt("watercoverage",-1));
|
||||||
b.setLavacoverage(biomeRecipe.getInt("lavacoverage",-1));
|
b.setLavacoverage(biomeRecipe.getInt("lavacoverage",-1));
|
||||||
|
@ -56,10 +56,10 @@ public class MakeCommand extends CompositeCommand {
|
|||||||
}
|
}
|
||||||
GhResult result = ((Greenhouses)this.getAddon()).getManager().tryToMakeGreenhouse(location, null);
|
GhResult result = ((Greenhouses)this.getAddon()).getManager().tryToMakeGreenhouse(location, null);
|
||||||
|
|
||||||
if (result.getResults().isEmpty()) {
|
if (result.getResults().contains(GreenhouseResult.SUCCESS)) {
|
||||||
// Success
|
// Success
|
||||||
user.sendMessage("general.success");
|
user.sendMessage("general.success");
|
||||||
user.sendRawMessage(result.getFinder().getGh().getBiomeRecipe().getFriendlyName());
|
user.sendRawMessage(result.getFinder().getGh().getBiomeRecipe().getName());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
result.getResults().forEach(r -> sendErrorMessage(user, r));
|
result.getResults().forEach(r -> sendErrorMessage(user, r));
|
||||||
|
Loading…
Reference in New Issue
Block a user