diff --git a/src/main/java/com/dfsek/terra/Terra.java b/src/main/java/com/dfsek/terra/Terra.java index 2c42037b3..daa667782 100644 --- a/src/main/java/com/dfsek/terra/Terra.java +++ b/src/main/java/com/dfsek/terra/Terra.java @@ -7,8 +7,8 @@ import com.dfsek.terra.biome.palette.PaletteLayer; import com.dfsek.terra.carving.CarverPalette; import com.dfsek.terra.command.TerraCommand; import com.dfsek.terra.command.structure.LocateCommand; +import com.dfsek.terra.config.base.ConfigPack; import com.dfsek.terra.config.base.PluginConfig; -import com.dfsek.terra.config.base.WorldConfig; import com.dfsek.terra.config.lang.LangUtil; import com.dfsek.terra.config.loaders.ImageLoaderLoader; import com.dfsek.terra.config.loaders.MaterialSetLoader; @@ -45,6 +45,7 @@ import com.dfsek.terra.util.StructureTypeEnum; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.block.Biome; import org.bukkit.block.data.BlockData; import org.bukkit.command.PluginCommand; @@ -65,8 +66,16 @@ import java.util.Objects; public class Terra extends GaeaPlugin { private final Map generatorMap = new HashMap<>(); + private final Map worldMap = new HashMap<>(); + private final Map worlds = new HashMap<>(); private final ConfigRegistry registry = new ConfigRegistry(); + public void invalidate() { + worldMap.clear(); + worlds.clear(); + registry.clear(); + } + @Override public void onDisable() { TerraChunkGenerator.saveAll(); @@ -76,16 +85,14 @@ public class Terra extends GaeaPlugin { public void onEnable() { Debug.setLogger(getLogger()); // Set debug logger. - saveDefaultConfig(); Metrics metrics = new Metrics(this, 9017); // Set up bStats. - metrics.addCustomChart(new Metrics.SingleLineChart("worlds", TerraWorld::numWorlds)); // World number chart. + metrics.addCustomChart(new Metrics.SingleLineChart("worlds", worldMap::size)); // World number chart. PluginConfig.load(this); // Load master config.yml LangUtil.load(PluginConfig.getLanguage(), this); // Load language. - TerraWorld.invalidate(); // Clear/set up world cache. registry.loadAll(this); // Load all config packs. PluginCommand c = Objects.requireNonNull(getCommand("terra")); @@ -111,9 +118,9 @@ public class Terra extends GaeaPlugin { @Override public @Nullable ChunkGenerator getDefaultWorldGenerator(@NotNull String worldName, @Nullable String id) { return generatorMap.computeIfAbsent(worldName, name -> { - WorldConfig c = new WorldConfig(worldName, id, this); - TerraWorld.loadWorld(c); - return new TerraChunkGenerator(c.getConfig(), this); + if(!registry.contains(id)) throw new IllegalArgumentException("No such config pack \"" + id + "\""); + worlds.put(worldName, registry.get(id)); + return new TerraChunkGenerator(registry.get(id), this); }); } @@ -162,4 +169,8 @@ public class Terra extends GaeaPlugin { public ConfigRegistry getRegistry() { return registry; } + + public TerraWorld getWorld(World w) { + return worldMap.computeIfAbsent(w, world -> new TerraWorld(w, worlds.get(w.getName()))); + } } diff --git a/src/main/java/com/dfsek/terra/TerraWorld.java b/src/main/java/com/dfsek/terra/TerraWorld.java index 9cc395346..987a656c3 100644 --- a/src/main/java/com/dfsek/terra/TerraWorld.java +++ b/src/main/java/com/dfsek/terra/TerraWorld.java @@ -6,7 +6,6 @@ import com.dfsek.terra.biome.grid.master.TerraRadialBiomeGrid; import com.dfsek.terra.biome.grid.master.TerraStandardBiomeGrid; import com.dfsek.terra.config.base.ConfigPack; import com.dfsek.terra.config.base.ConfigPackTemplate; -import com.dfsek.terra.config.base.WorldConfig; import com.dfsek.terra.config.builder.biomegrid.BiomeGridBuilder; import com.dfsek.terra.debug.Debug; import com.dfsek.terra.generation.TerraChunkGenerator; @@ -14,24 +13,16 @@ import org.bukkit.Bukkit; import org.bukkit.World; import org.polydev.gaea.biome.BiomeGrid; -import java.util.HashMap; -import java.util.Map; - public class TerraWorld { - private static final Map map = new HashMap<>(); - private static final Map loaded = new HashMap<>(); - private final TerraBiomeGrid grid; private final BiomeZone zone; private final ConfigPack config; - private final WorldConfig worldConfig; private boolean safe; - private TerraWorld(World w) { + public TerraWorld(World w, ConfigPack c) { safe = true; - worldConfig = loaded.get(w.getName()); - config = worldConfig.getConfig(); + config = c; ConfigPackTemplate template = config.getTemplate(); @@ -42,7 +33,7 @@ public class TerraWorld { String partName = template.getGrids().get(i); try { BiomeGridBuilder g = config.getBiomeGrid(partName); - BiomeGrid b = g.build(w, worldConfig); + BiomeGrid b = g.build(w, c); definedGrids[i] = b; } catch(NullPointerException e) { safe = false; @@ -53,30 +44,14 @@ public class TerraWorld { Bukkit.getLogger().severe("Terrain will NOT generate properly at this point. Correct your config before using your server!"); } } - zone = new BiomeZone(w, worldConfig, definedGrids); + zone = new BiomeZone(w, c, definedGrids); if(template.getGridType().equals(TerraBiomeGrid.Type.RADIAL)) { - BiomeGrid internal = config.getBiomeGrid(template.getRadialInternalGrid()).build(w, worldConfig); + BiomeGrid internal = config.getBiomeGrid(template.getRadialInternalGrid()).build(w, c); grid = new TerraRadialBiomeGrid(w, template.getGridFreqX(), template.getGridFreqZ(), zone, config, template.getRadialGridRadius(), internal); } else grid = new TerraStandardBiomeGrid(w, template.getGridFreqX(), template.getGridFreqZ(), zone, config); } - public static void loadWorld(WorldConfig w) { - loaded.put(w.getWorldID(), w); - } - - public static synchronized TerraWorld getWorld(World w) { - return map.computeIfAbsent(w, TerraWorld::new); - } - - public static synchronized void invalidate() { - map.clear(); - } - - public static int numWorlds() { - return map.size(); - } - public static boolean isTerraWorld(World w) { return w.getGenerator() instanceof TerraChunkGenerator; } @@ -89,10 +64,6 @@ public class TerraWorld { return config; } - public WorldConfig getWorldConfig() { - return worldConfig; - } - public BiomeZone getZone() { return zone; } diff --git a/src/main/java/com/dfsek/terra/async/AsyncBiomeFinder.java b/src/main/java/com/dfsek/terra/async/AsyncBiomeFinder.java index 539b0923d..b02cf8abd 100644 --- a/src/main/java/com/dfsek/terra/async/AsyncBiomeFinder.java +++ b/src/main/java/com/dfsek/terra/async/AsyncBiomeFinder.java @@ -1,11 +1,11 @@ package com.dfsek.terra.async; +import com.dfsek.terra.Terra; import com.dfsek.terra.biome.grid.master.TerraBiomeGrid; import com.dfsek.terra.config.base.PluginConfig; import org.bukkit.Location; import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; -import org.polydev.gaea.GaeaPlugin; import org.polydev.gaea.biome.Biome; import org.polydev.gaea.generation.GenerationPhase; @@ -16,7 +16,7 @@ import java.util.function.Consumer; */ public class AsyncBiomeFinder extends AsyncFeatureFinder { - public AsyncBiomeFinder(TerraBiomeGrid grid, Biome target, @NotNull Location origin, int startRadius, int maxRadius, Consumer callback, GaeaPlugin main) { + public AsyncBiomeFinder(TerraBiomeGrid grid, Biome target, @NotNull Location origin, int startRadius, int maxRadius, Consumer callback, Terra main) { super(grid, target, origin, startRadius, maxRadius, callback, main); } diff --git a/src/main/java/com/dfsek/terra/async/AsyncFeatureFinder.java b/src/main/java/com/dfsek/terra/async/AsyncFeatureFinder.java index 71761701b..d26418bf5 100644 --- a/src/main/java/com/dfsek/terra/async/AsyncFeatureFinder.java +++ b/src/main/java/com/dfsek/terra/async/AsyncFeatureFinder.java @@ -1,12 +1,12 @@ package com.dfsek.terra.async; +import com.dfsek.terra.Terra; import com.dfsek.terra.biome.grid.master.TerraBiomeGrid; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; -import org.polydev.gaea.GaeaPlugin; import java.util.function.Consumer; @@ -20,9 +20,9 @@ public abstract class AsyncFeatureFinder implements Runnable { protected final World world; private final Consumer callback; protected int searchSize = 1; - protected final GaeaPlugin main; + protected final Terra main; - public AsyncFeatureFinder(TerraBiomeGrid grid, T target, @NotNull Location origin, int startRadius, int maxRadius, Consumer callback, GaeaPlugin main) { + public AsyncFeatureFinder(TerraBiomeGrid grid, T target, @NotNull Location origin, int startRadius, int maxRadius, Consumer callback, Terra main) { this.grid = grid; this.target = target; this.main = main; diff --git a/src/main/java/com/dfsek/terra/async/AsyncStructureFinder.java b/src/main/java/com/dfsek/terra/async/AsyncStructureFinder.java index 87afc4fc4..aa2d203f0 100644 --- a/src/main/java/com/dfsek/terra/async/AsyncStructureFinder.java +++ b/src/main/java/com/dfsek/terra/async/AsyncStructureFinder.java @@ -1,5 +1,6 @@ package com.dfsek.terra.async; +import com.dfsek.terra.Terra; import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.biome.grid.master.TerraBiomeGrid; import com.dfsek.terra.generation.items.TerraStructure; @@ -10,7 +11,6 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; -import org.polydev.gaea.GaeaPlugin; import org.polydev.gaea.util.FastRandom; import java.util.Random; @@ -20,7 +20,7 @@ import java.util.function.Consumer; * Runnable to locate structures asynchronously */ public class AsyncStructureFinder extends AsyncFeatureFinder { - public AsyncStructureFinder(TerraBiomeGrid grid, TerraStructure target, @NotNull Location origin, int startRadius, int maxRadius, Consumer callback, GaeaPlugin main) { + public AsyncStructureFinder(TerraBiomeGrid grid, TerraStructure target, @NotNull Location origin, int startRadius, int maxRadius, Consumer callback, Terra main) { super(grid, target, origin, startRadius, maxRadius, callback, main); setSearchSize(target.getSpawn().getWidth() + 2 * target.getSpawn().getSeparation()); } @@ -42,7 +42,7 @@ public class AsyncStructureFinder extends AsyncFeatureFinder { for(int y = target.getSpawnStart().get(r2); y > target.getBound().getMin(); y--) { if(!target.getBound().isInRange(y)) return false; spawn.setY(y); - if(!struc.checkSpawns(spawn, rotation)) continue; + if(!struc.checkSpawns(spawn, rotation, main)) continue; return true; } return false; diff --git a/src/main/java/com/dfsek/terra/biome/BiomeZone.java b/src/main/java/com/dfsek/terra/biome/BiomeZone.java index 2631789da..a907599d2 100644 --- a/src/main/java/com/dfsek/terra/biome/BiomeZone.java +++ b/src/main/java/com/dfsek/terra/biome/BiomeZone.java @@ -1,6 +1,7 @@ package com.dfsek.terra.biome; -import com.dfsek.terra.config.base.WorldConfig; +import com.dfsek.terra.config.base.ConfigPack; +import com.dfsek.terra.config.base.ConfigPackTemplate; import com.dfsek.terra.image.ImageLoader; import org.bukkit.World; import org.jetbrains.annotations.Nullable; @@ -21,16 +22,17 @@ public class BiomeZone { private final boolean useImage; private final ImageLoader.Channel channel; - public BiomeZone(World w, WorldConfig wc, BiomeGrid[] grids) { + public BiomeZone(World w, ConfigPack wc, BiomeGrid[] grids) { this.noise = new FastNoiseLite((int) w.getSeed() + 2); this.noise.setNoiseType(FastNoiseLite.NoiseType.OpenSimplex2); this.noise.setFractalType(FastNoiseLite.FractalType.FBm); this.noise.setFractalOctaves(4); - this.noise.setFrequency(1D / wc.getConfig().getTemplate().getZoneFreq()); + ConfigPackTemplate t = wc.getTemplate(); + this.noise.setFrequency(1D / t.getZoneFreq()); this.grids = grids; - imageLoader = wc.imageLoader; - useImage = wc.fromImage; - channel = wc.zoneChannel; + imageLoader = t.getImageLoader(); + useImage = t.isFromImage(); + channel = t.getZoneChannel(); } /** diff --git a/src/main/java/com/dfsek/terra/biome/grid/UserDefinedGrid.java b/src/main/java/com/dfsek/terra/biome/grid/UserDefinedGrid.java index 98b8f6c64..c2404cc68 100644 --- a/src/main/java/com/dfsek/terra/biome/grid/UserDefinedGrid.java +++ b/src/main/java/com/dfsek/terra/biome/grid/UserDefinedGrid.java @@ -1,6 +1,7 @@ package com.dfsek.terra.biome.grid; -import com.dfsek.terra.config.base.WorldConfig; +import com.dfsek.terra.config.base.ConfigPack; +import com.dfsek.terra.config.base.ConfigPackTemplate; import com.dfsek.terra.image.ImageLoader; import org.bukkit.Location; import org.bukkit.World; @@ -15,13 +16,14 @@ public class UserDefinedGrid extends BiomeGrid { private final ImageLoader.Channel channelX; private final ImageLoader.Channel channelZ; - public UserDefinedGrid(World w, double freq1, double freq2, Biome[][] b, WorldConfig c) { + public UserDefinedGrid(World w, double freq1, double freq2, Biome[][] b, ConfigPack c) { super(w, freq1, freq2, b.length, b[0].length); super.setGrid(b); - imageLoader = c.imageLoader; - fromImage = c.fromImage; - channelX = c.biomeXChannel; - channelZ = c.biomeZChannel; + ConfigPackTemplate t = c.getTemplate(); + imageLoader = t.getImageLoader(); + fromImage = t.isFromImage(); + channelX = t.getBiomeXChannel(); + channelZ = t.getBiomeZChannel(); } @Override diff --git a/src/main/java/com/dfsek/terra/carving/CarverCache.java b/src/main/java/com/dfsek/terra/carving/CarverCache.java index d5a1d8580..eca106309 100644 --- a/src/main/java/com/dfsek/terra/carving/CarverCache.java +++ b/src/main/java/com/dfsek/terra/carving/CarverCache.java @@ -1,6 +1,6 @@ package com.dfsek.terra.carving; -import com.dfsek.terra.TerraWorld; +import com.dfsek.terra.Terra; import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.biome.grid.master.TerraBiomeGrid; import com.dfsek.terra.config.base.PluginConfig; @@ -22,15 +22,17 @@ public class CarverCache { private final World w; private final Map> carvers = new HashMap<>(); + private final Terra main; - public CarverCache(World w) { + public CarverCache(World w, Terra main) { this.w = w; + this.main = main; } public List getPoints(int chunkX, int chunkZ, UserDefinedCarver carver) { if(carvers.size() > PluginConfig.getCacheSize() * 2) carvers.clear(); return carvers.computeIfAbsent((((long) chunkX) << 32) | (chunkZ & 0xffffffffL), key -> { - TerraBiomeGrid grid = TerraWorld.getWorld(w).getGrid(); + TerraBiomeGrid grid = main.getWorld(w).getGrid(); if(carver.isChunkCarved(w, chunkX, chunkZ, new FastRandom(MathUtil.getCarverChunkSeed(chunkX, chunkZ, w.getSeed() + carver.hashCode())))) { long seed = MathUtil.getCarverChunkSeed(chunkX, chunkZ, w.getSeed()); carver.getSeedVar().setValue(seed); diff --git a/src/main/java/com/dfsek/terra/carving/UserDefinedCarver.java b/src/main/java/com/dfsek/terra/carving/UserDefinedCarver.java index 4cc1659cd..83977a9b4 100644 --- a/src/main/java/com/dfsek/terra/carving/UserDefinedCarver.java +++ b/src/main/java/com/dfsek/terra/carving/UserDefinedCarver.java @@ -1,6 +1,6 @@ package com.dfsek.terra.carving; -import com.dfsek.terra.TerraWorld; +import com.dfsek.terra.Terra; import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.config.templates.BiomeTemplate; import com.dfsek.terra.config.templates.CarverTemplate; @@ -43,8 +43,9 @@ public class UserDefinedCarver extends Carver { private double step = 2; private Range recalc = new Range(8, 10); private double recalcMagnitude = 3; + private final Terra main; - public UserDefinedCarver(Range height, Range length, double[] start, double[] mutate, List radii, Scope parent, long hash, int topCut, int bottomCut, CarverTemplate config) throws ParseException { + public UserDefinedCarver(Range height, Range length, double[] start, double[] mutate, List radii, Scope parent, long hash, int topCut, int bottomCut, CarverTemplate config, Terra main) throws ParseException { super(height.getMin(), height.getMax()); this.length = length; this.start = start; @@ -53,6 +54,7 @@ public class UserDefinedCarver extends Carver { this.topCut = topCut; this.bottomCut = bottomCut; this.config = config; + this.main = main; Parser p = new Parser(); @@ -99,7 +101,7 @@ public class UserDefinedCarver extends Carver { @Override public void carve(int chunkX, int chunkZ, World w, BiConsumer consumer) { - CarverCache cache = cacheMap.computeIfAbsent(w, CarverCache::new); + CarverCache cache = cacheMap.computeIfAbsent(w, world -> new CarverCache(world, main)); int carvingRadius = getCarvingRadius(); for(int x = chunkX - carvingRadius; x <= chunkX + carvingRadius; x++) { for(int z = chunkZ - carvingRadius; z <= chunkZ + carvingRadius; z++) { @@ -119,7 +121,7 @@ public class UserDefinedCarver extends Carver { @Override public boolean isChunkCarved(World w, int chunkX, int chunkZ, Random random) { - BiomeTemplate conf = ((UserDefinedBiome) TerraWorld.getWorld(w).getGrid().getBiome((chunkX << 4) + 8, (chunkZ << 4) + 8, GenerationPhase.POPULATE)).getConfig(); + BiomeTemplate conf = ((UserDefinedBiome) main.getWorld(w).getGrid().getBiome((chunkX << 4) + 8, (chunkZ << 4) + 8, GenerationPhase.POPULATE)).getConfig(); if(conf.getCarvers().get(this) != null) { return new FastRandom(random.nextLong() + hash).nextInt(100) < conf.getCarvers().get(this); } diff --git a/src/main/java/com/dfsek/terra/command/ReloadCommand.java b/src/main/java/com/dfsek/terra/command/ReloadCommand.java index 41fdf1ffa..688ba2fd1 100644 --- a/src/main/java/com/dfsek/terra/command/ReloadCommand.java +++ b/src/main/java/com/dfsek/terra/command/ReloadCommand.java @@ -1,7 +1,6 @@ package com.dfsek.terra.command; import com.dfsek.terra.Terra; -import com.dfsek.terra.TerraWorld; import com.dfsek.terra.config.base.PluginConfig; import com.dfsek.terra.config.lang.LangUtil; import org.bukkit.command.CommandSender; @@ -32,7 +31,7 @@ public class ReloadCommand extends Command implements DebugCommand { PluginConfig.load(getMain()); LangUtil.load(PluginConfig.getLanguage(), getMain()); // Load language. if(!((Terra) getMain()).getRegistry().loadAll((Terra) getMain())) LangUtil.send("command.reload-error", sender); - TerraWorld.invalidate(); + ((Terra) getMain()).invalidate(); LangUtil.send("command.reload", sender); return true; } diff --git a/src/main/java/com/dfsek/terra/command/biome/BiomeCommand.java b/src/main/java/com/dfsek/terra/command/biome/BiomeCommand.java index 69eb48f59..5a956e8b8 100644 --- a/src/main/java/com/dfsek/terra/command/biome/BiomeCommand.java +++ b/src/main/java/com/dfsek/terra/command/biome/BiomeCommand.java @@ -1,6 +1,6 @@ package com.dfsek.terra.command.biome; -import com.dfsek.terra.TerraWorld; +import com.dfsek.terra.Terra; import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.biome.grid.master.TerraBiomeGrid; import com.dfsek.terra.config.lang.LangUtil; @@ -23,7 +23,7 @@ public class BiomeCommand extends WorldCommand { @Override public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World w) { - TerraBiomeGrid grid = TerraWorld.getWorld(sender.getWorld()).getGrid(); + TerraBiomeGrid grid = ((Terra) getMain()).getWorld(sender.getWorld()).getGrid(); UserDefinedBiome biome = (UserDefinedBiome) grid.getBiome(sender.getLocation(), GenerationPhase.POPULATE); LangUtil.send("command.biome.in", sender, biome.getID()); return true; diff --git a/src/main/java/com/dfsek/terra/command/biome/BiomeInfoCommand.java b/src/main/java/com/dfsek/terra/command/biome/BiomeInfoCommand.java index 546989ac2..4a9025444 100644 --- a/src/main/java/com/dfsek/terra/command/biome/BiomeInfoCommand.java +++ b/src/main/java/com/dfsek/terra/command/biome/BiomeInfoCommand.java @@ -1,6 +1,6 @@ package com.dfsek.terra.command.biome; -import com.dfsek.terra.TerraWorld; +import com.dfsek.terra.Terra; import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.carving.UserDefinedCarver; import com.dfsek.terra.config.base.ConfigPack; @@ -28,7 +28,7 @@ public class BiomeInfoCommand extends WorldCommand { @Override public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) { String id = args[0]; - ConfigPack cfg = TerraWorld.getWorld(world).getConfig(); + ConfigPack cfg = ((Terra) getMain()).getWorld(world).getConfig(); UserDefinedBiome b; try { b = cfg.getBiome(id); @@ -87,7 +87,7 @@ public class BiomeInfoCommand extends WorldCommand { public List getTabCompletions(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) { if(!(sender instanceof Player) || !(((Player) sender).getWorld().getGenerator() instanceof TerraChunkGenerator)) return Collections.emptyList(); - List ids = TerraWorld.getWorld(((Player) sender).getWorld()).getConfig().getBiomeIDs(); + List ids = ((Terra) getMain()).getWorld(((Player) sender).getWorld()).getConfig().getBiomeIDs(); if(args.length == 1) return ids.stream().filter(string -> string.toUpperCase().startsWith(args[0].toUpperCase())).collect(Collectors.toList()); return Collections.emptyList(); diff --git a/src/main/java/com/dfsek/terra/command/biome/BiomeLocateCommand.java b/src/main/java/com/dfsek/terra/command/biome/BiomeLocateCommand.java index 31bf72e38..59e81d1f2 100644 --- a/src/main/java/com/dfsek/terra/command/biome/BiomeLocateCommand.java +++ b/src/main/java/com/dfsek/terra/command/biome/BiomeLocateCommand.java @@ -1,6 +1,6 @@ package com.dfsek.terra.command.biome; -import com.dfsek.terra.TerraWorld; +import com.dfsek.terra.Terra; import com.dfsek.terra.async.AsyncBiomeFinder; import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.config.base.PluginConfig; @@ -39,12 +39,12 @@ public class BiomeLocateCommand extends WorldCommand { } UserDefinedBiome b; try { - b = TerraWorld.getWorld(world).getConfig().getBiome(id); + b = ((Terra) getMain()).getWorld(world).getConfig().getBiome(id); } catch(IllegalArgumentException | NullPointerException e) { LangUtil.send("command.biome.invalid", sender, id); return true; } - Bukkit.getScheduler().runTaskAsynchronously(getMain(), new AsyncBiomeFinder(TerraWorld.getWorld(world).getGrid(), b, sender.getLocation().clone().multiply((1D / PluginConfig.getBiomeSearchResolution())), 0, maxRadius, location -> { + Bukkit.getScheduler().runTaskAsynchronously(getMain(), new AsyncBiomeFinder(((Terra) getMain()).getWorld(world).getGrid(), b, sender.getLocation().clone().multiply((1D / PluginConfig.getBiomeSearchResolution())), 0, maxRadius, location -> { if(location != null) { LangUtil.send("command.biome.biome-found", sender, String.valueOf(location.getBlockX()), String.valueOf(location.getBlockZ())); if(tp) { @@ -52,7 +52,7 @@ public class BiomeLocateCommand extends WorldCommand { Bukkit.getScheduler().runTask(getMain(), () -> sender.teleport(l)); } } else LangUtil.send("command.biome.unable-to-locate", sender); - }, getMain())); + }, (Terra) getMain())); return true; } @@ -75,7 +75,7 @@ public class BiomeLocateCommand extends WorldCommand { public List getTabCompletions(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) { if(!(sender instanceof Player) || !(((Player) sender).getWorld().getGenerator() instanceof TerraChunkGenerator)) return Collections.emptyList(); - List ids = TerraWorld.getWorld(((Player) sender).getWorld()).getConfig().getBiomeIDs(); + List ids = ((Terra) getMain()).getWorld(((Player) sender).getWorld()).getConfig().getBiomeIDs(); if(args.length == 1) return ids.stream().filter(string -> string.toUpperCase().startsWith(args[0].toUpperCase())).collect(Collectors.toList()); return Collections.emptyList(); diff --git a/src/main/java/com/dfsek/terra/command/image/RenderCommand.java b/src/main/java/com/dfsek/terra/command/image/RenderCommand.java index ac21563e4..28f6536e8 100644 --- a/src/main/java/com/dfsek/terra/command/image/RenderCommand.java +++ b/src/main/java/com/dfsek/terra/command/image/RenderCommand.java @@ -1,5 +1,6 @@ package com.dfsek.terra.command.image; +import com.dfsek.terra.Terra; import com.dfsek.terra.config.lang.LangUtil; import com.dfsek.terra.image.WorldImageGenerator; import org.bukkit.World; @@ -21,7 +22,7 @@ public class RenderCommand extends WorldCommand { @Override public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String label, @NotNull String[] args, World world) { try { - WorldImageGenerator g = new WorldImageGenerator(world, Integer.parseInt(args[0]), Integer.parseInt(args[1])); + WorldImageGenerator g = new WorldImageGenerator(world, Integer.parseInt(args[0]), Integer.parseInt(args[1]), (Terra) getMain()); g.drawWorld(sender.getLocation().getBlockX(), sender.getLocation().getBlockZ()); File file = new File(getMain().getDataFolder() + File.separator + "export" + File.separator + "map" + File.separator + "map_" + System.currentTimeMillis() + ".png"); //noinspection ResultOfMethodCallIgnored diff --git a/src/main/java/com/dfsek/terra/command/image/gui/RawGUICommand.java b/src/main/java/com/dfsek/terra/command/image/gui/RawGUICommand.java index 68e978625..0a966e79e 100644 --- a/src/main/java/com/dfsek/terra/command/image/gui/RawGUICommand.java +++ b/src/main/java/com/dfsek/terra/command/image/gui/RawGUICommand.java @@ -1,6 +1,6 @@ package com.dfsek.terra.command.image.gui; -import com.dfsek.terra.TerraWorld; +import com.dfsek.terra.Terra; import com.dfsek.terra.config.base.PluginConfig; import com.dfsek.terra.config.lang.LangUtil; import com.dfsek.terra.image.ImageLoader; @@ -25,9 +25,9 @@ public class RawGUICommand extends WorldCommand { LangUtil.send("command.image.gui.debug", sender); return true; } - ImageLoader loader = TerraWorld.getWorld(world).getWorldConfig().imageLoader; - if(loader != null) loader.debug(false, sender.getWorld()); - else ImageLoader.debugWorld(false, world); + ImageLoader loader = ((Terra) getMain()).getWorld(world).getConfig().getTemplate().getImageLoader(); + if(loader != null) loader.debug(false, sender.getWorld(), (Terra) getMain()); + else ImageLoader.debugWorld(false, world, (Terra) getMain()); return true; } diff --git a/src/main/java/com/dfsek/terra/command/image/gui/StepGUICommand.java b/src/main/java/com/dfsek/terra/command/image/gui/StepGUICommand.java index 0cc1f9929..5a74619ad 100644 --- a/src/main/java/com/dfsek/terra/command/image/gui/StepGUICommand.java +++ b/src/main/java/com/dfsek/terra/command/image/gui/StepGUICommand.java @@ -1,6 +1,6 @@ package com.dfsek.terra.command.image.gui; -import com.dfsek.terra.TerraWorld; +import com.dfsek.terra.Terra; import com.dfsek.terra.config.base.PluginConfig; import com.dfsek.terra.config.lang.LangUtil; import com.dfsek.terra.image.ImageLoader; @@ -25,9 +25,9 @@ public class StepGUICommand extends WorldCommand { LangUtil.send("command.image.gui.debug", sender); return true; } - ImageLoader loader = TerraWorld.getWorld(world).getWorldConfig().imageLoader; - if(loader != null) loader.debug(true, sender.getWorld()); - else ImageLoader.debugWorld(true, world); + ImageLoader loader = ((Terra) getMain()).getWorld(world).getConfig().getTemplate().getImageLoader(); + if(loader != null) loader.debug(true, sender.getWorld(), (Terra) getMain()); + else ImageLoader.debugWorld(true, world, (Terra) getMain()); return true; } diff --git a/src/main/java/com/dfsek/terra/command/structure/LocateCommand.java b/src/main/java/com/dfsek/terra/command/structure/LocateCommand.java index 93d8cc69a..407a6f10a 100644 --- a/src/main/java/com/dfsek/terra/command/structure/LocateCommand.java +++ b/src/main/java/com/dfsek/terra/command/structure/LocateCommand.java @@ -1,6 +1,6 @@ package com.dfsek.terra.command.structure; -import com.dfsek.terra.TerraWorld; +import com.dfsek.terra.Terra; import com.dfsek.terra.async.AsyncStructureFinder; import com.dfsek.terra.config.lang.LangUtil; import com.dfsek.terra.generation.TerraChunkGenerator; @@ -39,12 +39,12 @@ public class LocateCommand extends WorldCommand { } TerraStructure s; try { - s = Objects.requireNonNull(TerraWorld.getWorld(world).getConfig().getStructure(id)); + s = Objects.requireNonNull(((Terra) getMain()).getWorld(world).getConfig().getStructure(id)); } catch(IllegalArgumentException | NullPointerException e) { LangUtil.send("command.structure.invalid", sender, id); return true; } - Bukkit.getScheduler().runTaskAsynchronously(getMain(), new AsyncStructureFinder(TerraWorld.getWorld(world).getGrid(), s, sender.getLocation(), 0, maxRadius, (location) -> { + Bukkit.getScheduler().runTaskAsynchronously(getMain(), new AsyncStructureFinder(((Terra) getMain()).getWorld(world).getGrid(), s, sender.getLocation(), 0, maxRadius, (location) -> { if(sender.isOnline()) { if(location != null) { sender.sendMessage("Located structure at (" + location.getBlockX() + ", " + location.getBlockZ() + ")."); @@ -55,7 +55,7 @@ public class LocateCommand extends WorldCommand { } } else sender.sendMessage("Unable to locate structure. "); } - }, getMain())); + }, (Terra) getMain())); return true; } @@ -78,7 +78,7 @@ public class LocateCommand extends WorldCommand { public List getTabCompletions(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) { if(!(sender instanceof Player) || !(((Player) sender).getWorld().getGenerator() instanceof TerraChunkGenerator)) return Collections.emptyList(); - List ids = TerraWorld.getWorld(((Player) sender).getWorld()).getConfig().getStructureIDs(); + List ids = ((Terra) getMain()).getWorld(((Player) sender).getWorld()).getConfig().getStructureIDs(); if(args.length == 1) return ids.stream().filter(string -> string.toUpperCase().startsWith(args[0].toUpperCase())).collect(Collectors.toList()); return Collections.emptyList(); diff --git a/src/main/java/com/dfsek/terra/command/structure/SpawnCommand.java b/src/main/java/com/dfsek/terra/command/structure/SpawnCommand.java index 7e9243cdf..b9fd6534d 100644 --- a/src/main/java/com/dfsek/terra/command/structure/SpawnCommand.java +++ b/src/main/java/com/dfsek/terra/command/structure/SpawnCommand.java @@ -1,5 +1,6 @@ package com.dfsek.terra.command.structure; +import com.dfsek.terra.Terra; import com.dfsek.terra.structure.StructureSpawnRequirement; import org.bukkit.Location; import org.bukkit.World; @@ -24,9 +25,9 @@ public class SpawnCommand extends WorldCommand implements DebugCommand { int x = p.getBlockX(); int y = p.getBlockY(); int z = p.getBlockZ(); - boolean air = StructureSpawnRequirement.AIR.getInstance(world).matches(x, y, z); - boolean ground = StructureSpawnRequirement.LAND.getInstance(world).matches(x, y, z); - boolean sea = StructureSpawnRequirement.OCEAN.getInstance(world).matches(x, y, z); + boolean air = StructureSpawnRequirement.AIR.getInstance(world, (Terra) getMain()).matches(x, y, z); + boolean ground = StructureSpawnRequirement.LAND.getInstance(world, (Terra) getMain()).matches(x, y, z); + boolean sea = StructureSpawnRequirement.OCEAN.getInstance(world, (Terra) getMain()).matches(x, y, z); sender.sendMessage("AIR: " + air + "\nLAND: " + ground + "\nOCEAN: " + sea); return true; diff --git a/src/main/java/com/dfsek/terra/config/base/ConfigPack.java b/src/main/java/com/dfsek/terra/config/base/ConfigPack.java index c21072e2f..e5955aca9 100644 --- a/src/main/java/com/dfsek/terra/config/base/ConfigPack.java +++ b/src/main/java/com/dfsek/terra/config/base/ConfigPack.java @@ -116,7 +116,7 @@ public class ConfigPack { throw new FileMissingException("No pack.yml file found in " + folder.getAbsolutePath(), e); } - load(new FolderLoader(folder.toPath()), l); + load(new FolderLoader(folder.toPath()), l, main); } public ConfigPack(ZipFile file, Terra main) throws ConfigException { @@ -140,10 +140,10 @@ public class ConfigPack { selfLoader.load(template, stream); - load(new ZIPLoader(file), l); + load(new ZIPLoader(file), l, main); } - private void load(Loader loader, long start) throws ConfigException { + private void load(Loader loader, long start, Terra main) throws ConfigException { for(Map.Entry var : template.getVariables().entrySet()) { varScope.create(var.getKey()).setValue(var.getValue()); } @@ -153,7 +153,7 @@ public class ConfigPack { .open("palettes").then(streams -> buildAll(new PaletteFactory(), paletteRegistry, abstractConfigLoader.load(streams, PaletteTemplate::new))).close() .open("ores").then(streams -> buildAll(new OreFactory(), oreRegistry, abstractConfigLoader.load(streams, OreTemplate::new))).close() .open("flora").then(streams -> buildAll(new FloraFactory(), floraRegistry, abstractConfigLoader.load(streams, FloraTemplate::new))).close() - .open("carving").then(streams -> buildAll(new CarverFactory(this), carverRegistry, abstractConfigLoader.load(streams, CarverTemplate::new))).close() + .open("carving").then(streams -> buildAll(new CarverFactory(this, main), carverRegistry, abstractConfigLoader.load(streams, CarverTemplate::new))).close() .open("structures/trees").then(streams -> buildAll(new TreeFactory(), treeRegistry, abstractConfigLoader.load(streams, TreeTemplate::new))).close() .open("structures/single").then(streams -> buildAll(new StructureFactory(), structureRegistry, abstractConfigLoader.load(streams, StructureTemplate::new))).close() .open("biomes").then(streams -> buildAll(new BiomeFactory(this), biomeRegistry, abstractConfigLoader.load(streams, () -> new BiomeTemplate(this)))).close() diff --git a/src/main/java/com/dfsek/terra/config/base/ConfigPackTemplate.java b/src/main/java/com/dfsek/terra/config/base/ConfigPackTemplate.java index c574ada56..a916ca01a 100644 --- a/src/main/java/com/dfsek/terra/config/base/ConfigPackTemplate.java +++ b/src/main/java/com/dfsek/terra/config/base/ConfigPackTemplate.java @@ -6,6 +6,7 @@ import com.dfsek.tectonic.config.ValidatedConfigTemplate; import com.dfsek.tectonic.exception.ValidationException; import com.dfsek.terra.biome.grid.master.TerraBiomeGrid; import com.dfsek.terra.generation.config.NoiseBuilder; +import com.dfsek.terra.image.ImageLoader; import com.dfsek.terra.util.StructureTypeEnum; import java.util.HashMap; @@ -111,6 +112,25 @@ public class ConfigPackTemplate implements ValidatedConfigTemplate { @Default private String internalGrid = null; + @Value("image.enable") + @Default + private boolean fromImage = false; + + @Value("image.channels.biome-x") + @Default + private ImageLoader.Channel biomeXChannel = ImageLoader.Channel.RED; + @Value("image.channels.biome-z") + @Default + private ImageLoader.Channel biomeZChannel = ImageLoader.Channel.GREEN; + @Value("image.channels.zone") + @Default + private ImageLoader.Channel zoneChannel = ImageLoader.Channel.BLUE; + + @Value("image") + @Default + private ImageLoader imageLoader = null; + + public String getVersion() { return version; } @@ -211,10 +231,32 @@ public class ConfigPackTemplate implements ValidatedConfigTemplate { return gridType; } + public ImageLoader.Channel getBiomeXChannel() { + return biomeXChannel; + } + + public ImageLoader.Channel getBiomeZChannel() { + return biomeZChannel; + } + + public boolean isFromImage() { + return fromImage; + } + + public ImageLoader.Channel getZoneChannel() { + return zoneChannel; + } + + public ImageLoader getImageLoader() { + return imageLoader; + } + @Override public boolean validate() throws ValidationException { if(gridType.equals(TerraBiomeGrid.Type.RADIAL) && internalGrid == null) throw new ValidationException("No internal BiomeGrid specified"); + if(biomeZChannel.equals(biomeXChannel) || zoneChannel.equals(biomeXChannel) || zoneChannel.equals(biomeZChannel)) + throw new ValidationException("2 objects share the same image channels: biome-x and biome-z"); return true; } } diff --git a/src/main/java/com/dfsek/terra/config/base/WorldConfig.java b/src/main/java/com/dfsek/terra/config/base/WorldConfig.java deleted file mode 100644 index 57118e5bd..000000000 --- a/src/main/java/com/dfsek/terra/config/base/WorldConfig.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.dfsek.terra.config.base; - -import com.dfsek.tectonic.annotations.Default; -import com.dfsek.tectonic.annotations.Value; -import com.dfsek.tectonic.config.ValidatedConfigTemplate; -import com.dfsek.tectonic.exception.ConfigException; -import com.dfsek.tectonic.exception.ValidationException; -import com.dfsek.tectonic.loading.ConfigLoader; -import com.dfsek.terra.Terra; -import com.dfsek.terra.image.ImageLoader; -import org.apache.commons.io.FileUtils; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.Objects; - -public class WorldConfig implements ValidatedConfigTemplate { - private static final ConfigLoader LOADER = new ConfigLoader(); - - private final String worldID; - private final String configID; - @Value("image.enable") - @Default - public boolean fromImage = false; - - @Value("image.channels.biome-x") - @Default - public ImageLoader.Channel biomeXChannel = ImageLoader.Channel.RED; - @Value("image.channels.biome-z") - @Default - public ImageLoader.Channel biomeZChannel = ImageLoader.Channel.GREEN; - @Value("image.channels.zone") - @Default - public ImageLoader.Channel zoneChannel = ImageLoader.Channel.BLUE; - - @Value("image") - @Default - public ImageLoader imageLoader = null; - private ConfigPack tConfig; - - public WorldConfig(String w, String configID, Terra main) { - main.registerAllLoaders(LOADER); - this.worldID = w; - this.configID = configID; - load(main); - } - - public void load(Terra main) { - tConfig = main.getRegistry().get(configID); - - if(tConfig == null) throw new IllegalStateException("No such config pack \"" + configID + "\""); - - File file = new File(main.getDataFolder() + File.separator + "worlds", worldID + ".yml"); - - try { - if(!file.exists()) { - //noinspection ResultOfMethodCallIgnored - file.getParentFile().mkdirs(); - FileUtils.copyInputStreamToFile(Objects.requireNonNull(main.getResource("world.yml")), file); - } - LOADER.load(this, new FileInputStream(file)); - } catch(IOException e) { - throw new IllegalStateException("Unable to load configuration.", e); - } catch(ConfigException e) { - throw new IllegalStateException("Unable to proceed due to fatal configuration error.", e); - } - } - - public String getWorldID() { - return worldID; - } - - public ConfigPack getConfig() { - return tConfig; - } - - @Override - public boolean validate() throws ValidationException { - if(biomeZChannel.equals(biomeXChannel) || zoneChannel.equals(biomeXChannel) || zoneChannel.equals(biomeZChannel)) - throw new ValidationException("2 objects share the same image channels: biome-x and biome-z"); - return true; - } -} diff --git a/src/main/java/com/dfsek/terra/config/builder/biomegrid/BiomeGridBuilder.java b/src/main/java/com/dfsek/terra/config/builder/biomegrid/BiomeGridBuilder.java index 76faee1f3..ce203c754 100644 --- a/src/main/java/com/dfsek/terra/config/builder/biomegrid/BiomeGridBuilder.java +++ b/src/main/java/com/dfsek/terra/config/builder/biomegrid/BiomeGridBuilder.java @@ -1,9 +1,9 @@ package com.dfsek.terra.config.builder.biomegrid; -import com.dfsek.terra.config.base.WorldConfig; +import com.dfsek.terra.config.base.ConfigPack; import org.bukkit.World; import org.polydev.gaea.biome.BiomeGrid; public interface BiomeGridBuilder { - BiomeGrid build(World world, WorldConfig config); + BiomeGrid build(World world, ConfigPack config); } diff --git a/src/main/java/com/dfsek/terra/config/builder/biomegrid/SingleGridBuilder.java b/src/main/java/com/dfsek/terra/config/builder/biomegrid/SingleGridBuilder.java index b1c0af95b..1776c6ef4 100644 --- a/src/main/java/com/dfsek/terra/config/builder/biomegrid/SingleGridBuilder.java +++ b/src/main/java/com/dfsek/terra/config/builder/biomegrid/SingleGridBuilder.java @@ -1,7 +1,7 @@ package com.dfsek.terra.config.builder.biomegrid; import com.dfsek.terra.biome.grid.SingleBiomeGrid; -import com.dfsek.terra.config.base.WorldConfig; +import com.dfsek.terra.config.base.ConfigPack; import org.bukkit.World; import org.polydev.gaea.biome.Biome; @@ -13,7 +13,7 @@ public class SingleGridBuilder implements BiomeGridBuilder { } @Override - public SingleBiomeGrid build(World world, WorldConfig config) { + public SingleBiomeGrid build(World world, ConfigPack config) { return new SingleBiomeGrid(world, biome); } } diff --git a/src/main/java/com/dfsek/terra/config/builder/biomegrid/UserDefinedGridBuilder.java b/src/main/java/com/dfsek/terra/config/builder/biomegrid/UserDefinedGridBuilder.java index 6df57f2c9..2b3b2d9b1 100644 --- a/src/main/java/com/dfsek/terra/config/builder/biomegrid/UserDefinedGridBuilder.java +++ b/src/main/java/com/dfsek/terra/config/builder/biomegrid/UserDefinedGridBuilder.java @@ -1,7 +1,7 @@ package com.dfsek.terra.config.builder.biomegrid; import com.dfsek.terra.biome.grid.UserDefinedGrid; -import com.dfsek.terra.config.base.WorldConfig; +import com.dfsek.terra.config.base.ConfigPack; import org.bukkit.World; import org.polydev.gaea.biome.Biome; @@ -12,7 +12,7 @@ public class UserDefinedGridBuilder implements BiomeGridBuilder { private Biome[][] biomes; @Override - public UserDefinedGrid build(World world, WorldConfig config) { + public UserDefinedGrid build(World world, ConfigPack config) { return new UserDefinedGrid(world, 1D / xFreq, 1D / zFreq, biomes, config); } diff --git a/src/main/java/com/dfsek/terra/config/factories/CarverFactory.java b/src/main/java/com/dfsek/terra/config/factories/CarverFactory.java index 7dc6ac3e9..3ff6b375d 100644 --- a/src/main/java/com/dfsek/terra/config/factories/CarverFactory.java +++ b/src/main/java/com/dfsek/terra/config/factories/CarverFactory.java @@ -1,6 +1,7 @@ package com.dfsek.terra.config.factories; import com.dfsek.tectonic.exception.LoadException; +import com.dfsek.terra.Terra; import com.dfsek.terra.carving.UserDefinedCarver; import com.dfsek.terra.config.base.ConfigPack; import com.dfsek.terra.config.templates.CarverTemplate; @@ -12,9 +13,11 @@ import java.util.List; public class CarverFactory implements TerraFactory { private final ConfigPack pack; + private final Terra main; - public CarverFactory(ConfigPack pack) { + public CarverFactory(ConfigPack pack, Terra main) { this.pack = pack; + this.main = main; } @Override @@ -25,7 +28,7 @@ public class CarverFactory implements TerraFactory getDefaultPopulators(@NotNull World world) { - return Arrays.asList(new CavePopulator(), new StructurePopulator(), popMan); + return Arrays.asList(new CavePopulator(main), new StructurePopulator(main), popMan); } @Override diff --git a/src/main/java/com/dfsek/terra/generation/items/tree/TerraTree.java b/src/main/java/com/dfsek/terra/generation/items/tree/TerraTree.java index 5204b4ca6..6c7f52172 100644 --- a/src/main/java/com/dfsek/terra/generation/items/tree/TerraTree.java +++ b/src/main/java/com/dfsek/terra/generation/items/tree/TerraTree.java @@ -1,5 +1,6 @@ package com.dfsek.terra.generation.items.tree; +import com.dfsek.terra.Terra; import com.dfsek.terra.procgen.math.Vector2; import com.dfsek.terra.structure.Rotation; import com.dfsek.terra.structure.Structure; @@ -26,12 +27,12 @@ public class TerraTree implements Tree { } @Override - public boolean plant(Location location, Random random, JavaPlugin javaPlugin) { + public boolean plant(Location location, Random random, JavaPlugin main) { Location mut = location.clone().subtract(0, yOffset, 0); if(!spawnable.contains(location.getBlock().getType())) return false; Structure struc = structure.get(random); Rotation rotation = Rotation.fromDegrees(random.nextInt(4) * 90); - if(!struc.checkSpawns(mut, rotation)) return false; + if(!struc.checkSpawns(mut, rotation, (Terra) main)) return false; struc.paste(mut, rotation); return true; } diff --git a/src/main/java/com/dfsek/terra/image/ImageLoader.java b/src/main/java/com/dfsek/terra/image/ImageLoader.java index 0af39313f..cc3601c8b 100644 --- a/src/main/java/com/dfsek/terra/image/ImageLoader.java +++ b/src/main/java/com/dfsek/terra/image/ImageLoader.java @@ -1,6 +1,6 @@ package com.dfsek.terra.image; -import com.dfsek.terra.TerraWorld; +import com.dfsek.terra.Terra; import com.dfsek.terra.biome.BiomeZone; import com.dfsek.terra.biome.grid.master.TerraBiomeGrid; import com.dfsek.terra.config.base.PluginConfig; @@ -25,18 +25,18 @@ public class ImageLoader { this.align = align; } - public static void debugWorld(boolean genStep, World w) { + public static void debugWorld(boolean genStep, World w, Terra main) { if(!PluginConfig.isDebug()) return; - BufferedImage newImg = new WorldImageGenerator(w, 1024, 1024).drawWorld(0, 0).getDraw(); - if(genStep) newImg = redrawStepped(newImg, w, Align.CENTER); - DebugGUI debugGUI = new DebugGUI(newImg); + BufferedImage newImg = new WorldImageGenerator(w, 1024, 1024, main).drawWorld(0, 0).getDraw(); + if(genStep) newImg = redrawStepped(newImg, w, Align.CENTER, main); + DebugGUI debugGUI = new DebugGUI(newImg, main); debugGUI.start(); } - private static BufferedImage redrawStepped(BufferedImage original, World w, Align align) { + private static BufferedImage redrawStepped(BufferedImage original, World w, Align align, Terra main) { BufferedImage newImg = copyImage(original); - TerraBiomeGrid tb = TerraWorld.getWorld(w).getGrid(); - BiomeZone z = TerraWorld.getWorld(w).getZone(); + TerraBiomeGrid tb = main.getWorld(w).getGrid(); + BiomeZone z = main.getWorld(w).getZone(); for(int x = 0; x < newImg.getWidth(); x++) { for(int y = 0; y < newImg.getHeight(); y++) { double[] noise; @@ -60,13 +60,13 @@ public class ImageLoader { return b; } - public void debug(boolean genStep, World w) { + public void debug(boolean genStep, World w, Terra main) { if(!PluginConfig.isDebug()) return; BufferedImage newImg = copyImage(image); if(genStep) { - newImg = redrawStepped(image, w, align); + newImg = redrawStepped(image, w, align, main); } - DebugGUI debugGUI = new DebugGUI(newImg); + DebugGUI debugGUI = new DebugGUI(newImg, main); debugGUI.start(); } diff --git a/src/main/java/com/dfsek/terra/image/WorldImageGenerator.java b/src/main/java/com/dfsek/terra/image/WorldImageGenerator.java index 68fcedb0e..2ae7188f0 100644 --- a/src/main/java/com/dfsek/terra/image/WorldImageGenerator.java +++ b/src/main/java/com/dfsek/terra/image/WorldImageGenerator.java @@ -1,5 +1,6 @@ package com.dfsek.terra.image; +import com.dfsek.terra.Terra; import com.dfsek.terra.TerraWorld; import com.dfsek.terra.biome.grid.master.TerraBiomeGrid; import org.bukkit.World; @@ -14,14 +15,16 @@ import java.io.IOException; public class WorldImageGenerator { private final World w; private final BufferedImage draw; + private final Terra main; - public WorldImageGenerator(World w, int width, int height) { + public WorldImageGenerator(World w, int width, int height, Terra main) { draw = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); this.w = w; + this.main = main; } public WorldImageGenerator drawWorld(int centerX, int centerZ) { - TerraWorld tw = TerraWorld.getWorld(w); + TerraWorld tw = main.getWorld(w); TerraBiomeGrid tb = tw.getGrid(); int imY = 0; for(int y = centerZ - (draw.getHeight() / 2); y < centerZ + (draw.getHeight() / 2); y++) { diff --git a/src/main/java/com/dfsek/terra/listeners/EventListener.java b/src/main/java/com/dfsek/terra/listeners/EventListener.java index fcb880782..1d0af7394 100644 --- a/src/main/java/com/dfsek/terra/listeners/EventListener.java +++ b/src/main/java/com/dfsek/terra/listeners/EventListener.java @@ -1,5 +1,6 @@ package com.dfsek.terra.listeners; +import com.dfsek.terra.Terra; import com.dfsek.terra.TerraWorld; import com.dfsek.terra.config.base.ConfigPack; import com.dfsek.terra.debug.Debug; @@ -11,7 +12,6 @@ import org.bukkit.block.data.BlockData; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.world.StructureGrowEvent; -import org.polydev.gaea.GaeaPlugin; import org.polydev.gaea.tree.Tree; import org.polydev.gaea.tree.TreeType; import org.polydev.gaea.util.FastRandom; @@ -20,16 +20,16 @@ import org.polydev.gaea.util.FastRandom; * Listener for events on all implementations. */ public class EventListener implements Listener { - private final GaeaPlugin main; + private final Terra main; - public EventListener(GaeaPlugin main) { + public EventListener(Terra main) { this.main = main; } @EventHandler public void onSaplingGrow(StructureGrowEvent e) { if(!TerraWorld.isTerraWorld(e.getWorld())) return; - TerraWorld tw = TerraWorld.getWorld(e.getWorld()); + TerraWorld tw = main.getWorld(e.getWorld()); ConfigPack c = tw.getConfig(); if(c.getTemplate().isDisableSaplings()) return; e.setCancelled(true); diff --git a/src/main/java/com/dfsek/terra/listeners/SpigotListener.java b/src/main/java/com/dfsek/terra/listeners/SpigotListener.java index 4db5f26f8..0e1147a7d 100644 --- a/src/main/java/com/dfsek/terra/listeners/SpigotListener.java +++ b/src/main/java/com/dfsek/terra/listeners/SpigotListener.java @@ -1,5 +1,6 @@ package com.dfsek.terra.listeners; +import com.dfsek.terra.Terra; import com.dfsek.terra.TerraWorld; import com.dfsek.terra.async.AsyncStructureFinder; import com.dfsek.terra.debug.Debug; @@ -15,7 +16,6 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.EntitySpawnEvent; import org.bukkit.event.entity.VillagerAcquireTradeEvent; import org.bukkit.event.entity.VillagerCareerChangeEvent; -import org.polydev.gaea.GaeaPlugin; /** * Listener to load on Spigot servers, contains Villager crash prevention and hacky ender eye redirection. @@ -24,9 +24,9 @@ import org.polydev.gaea.GaeaPlugin; * StructureLocateEvent). */ public class SpigotListener implements Listener { - private final GaeaPlugin main; + private final Terra main; - public SpigotListener(GaeaPlugin main) { + public SpigotListener(Terra main) { this.main = main; } @@ -36,7 +36,7 @@ public class SpigotListener implements Listener { if(e.getEntityType().equals(EntityType.ENDER_SIGNAL)) { Debug.info("Detected Ender Signal..."); if(!TerraWorld.isTerraWorld(e.getEntity().getWorld())) return; - TerraWorld tw = TerraWorld.getWorld(e.getEntity().getWorld()); + TerraWorld tw = main.getWorld(e.getEntity().getWorld()); EnderSignal signal = (EnderSignal) entity; TerraStructure config = tw.getConfig().getStructureLocatable(StructureTypeEnum.STRONGHOLD); if(config != null) { diff --git a/src/main/java/com/dfsek/terra/population/CavePopulator.java b/src/main/java/com/dfsek/terra/population/CavePopulator.java index cc1ae4648..329ca243a 100644 --- a/src/main/java/com/dfsek/terra/population/CavePopulator.java +++ b/src/main/java/com/dfsek/terra/population/CavePopulator.java @@ -1,5 +1,6 @@ package com.dfsek.terra.population; +import com.dfsek.terra.Terra; import com.dfsek.terra.TerraProfiler; import com.dfsek.terra.TerraWorld; import com.dfsek.terra.carving.UserDefinedCarver; @@ -24,15 +25,20 @@ import java.util.Random; import java.util.Set; public class CavePopulator extends BlockPopulator { + private final Terra main; private static final Map shiftStorage = new HashMap<>(); // Persist BlockData created for shifts, to avoid re-calculating each time. private static final BlockData AIR = Material.AIR.createBlockData(); + public CavePopulator(Terra main) { + this.main = main; + } + @SuppressWarnings("try") @Override public void populate(@NotNull World world, @NotNull Random r, @NotNull Chunk chunk) { try(ProfileFuture ignored = TerraProfiler.fromWorld(world).measure("CaveTime")) { Random random = PopulationUtil.getRandom(chunk); - TerraWorld tw = TerraWorld.getWorld(world); + TerraWorld tw = main.getWorld(world); if(!tw.isSafe()) return; ConfigPack config = tw.getConfig(); diff --git a/src/main/java/com/dfsek/terra/population/FloraPopulator.java b/src/main/java/com/dfsek/terra/population/FloraPopulator.java index 0d7531ed3..324179d06 100644 --- a/src/main/java/com/dfsek/terra/population/FloraPopulator.java +++ b/src/main/java/com/dfsek/terra/population/FloraPopulator.java @@ -1,5 +1,6 @@ package com.dfsek.terra.population; +import com.dfsek.terra.Terra; import com.dfsek.terra.TerraProfiler; import com.dfsek.terra.TerraWorld; import com.dfsek.terra.biome.UserDefinedBiome; @@ -22,11 +23,17 @@ import java.util.Random; * Populates Flora and Trees */ public class FloraPopulator extends GaeaBlockPopulator { + private final Terra main; + + public FloraPopulator(Terra main) { + this.main = main; + } + @SuppressWarnings("try") @Override public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk chunk) { try(ProfileFuture ignored = TerraProfiler.fromWorld(world).measure("FloraTime")) { - TerraWorld tw = TerraWorld.getWorld(world); + TerraWorld tw = main.getWorld(world); if(!tw.isSafe()) return; TerraBiomeGrid grid = tw.getGrid(); Map> layers = new HashMap<>(); diff --git a/src/main/java/com/dfsek/terra/population/OrePopulator.java b/src/main/java/com/dfsek/terra/population/OrePopulator.java index 5590241f2..b9ac9a311 100644 --- a/src/main/java/com/dfsek/terra/population/OrePopulator.java +++ b/src/main/java/com/dfsek/terra/population/OrePopulator.java @@ -1,5 +1,6 @@ package com.dfsek.terra.population; +import com.dfsek.terra.Terra; import com.dfsek.terra.TerraProfiler; import com.dfsek.terra.TerraWorld; import com.dfsek.terra.biome.UserDefinedBiome; @@ -18,18 +19,24 @@ import org.polydev.gaea.util.FastRandom; import java.util.Random; public class OrePopulator extends GaeaBlockPopulator { + private final Terra main; + + public OrePopulator(Terra main) { + this.main = main; + } + @SuppressWarnings("try") @Override public void populate(@NotNull World world, @NotNull Random r, @NotNull Chunk chunk) { try(ProfileFuture ignored = TerraProfiler.fromWorld(world).measure("OreTime")) { - TerraWorld tw = TerraWorld.getWorld(world); + TerraWorld tw = main.getWorld(world); if(!tw.isSafe()) return; for(int cx = -1; cx <= 1; cx++) { for(int cz = -1; cz <= 1; cz++) { Random random = new FastRandom(MathUtil.getCarverChunkSeed(chunk.getX() + cx, chunk.getZ() + cz, world.getSeed())); int originX = ((chunk.getX() + cx) << 4); int originZ = ((chunk.getZ() + cz) << 4); - Biome b = TerraWorld.getWorld(world).getGrid().getBiome(originX + 8, originZ + 8, GenerationPhase.POPULATE); + Biome b = main.getWorld(world).getGrid().getBiome(originX + 8, originZ + 8, GenerationPhase.POPULATE); BiomeTemplate config = ((UserDefinedBiome) b).getConfig(); int finalCx = cx; int finalCz = cz; diff --git a/src/main/java/com/dfsek/terra/population/StructurePopulator.java b/src/main/java/com/dfsek/terra/population/StructurePopulator.java index c85529d44..482a7aaa2 100644 --- a/src/main/java/com/dfsek/terra/population/StructurePopulator.java +++ b/src/main/java/com/dfsek/terra/population/StructurePopulator.java @@ -1,5 +1,6 @@ package com.dfsek.terra.population; +import com.dfsek.terra.Terra; import com.dfsek.terra.TerraProfiler; import com.dfsek.terra.TerraWorld; import com.dfsek.terra.biome.UserDefinedBiome; @@ -28,6 +29,11 @@ import org.polydev.gaea.util.FastRandom; import java.util.Random; public class StructurePopulator extends BlockPopulator { + private final Terra main; + + public StructurePopulator(Terra main) { + this.main = main; + } @SuppressWarnings("try") @Override @@ -36,7 +42,7 @@ public class StructurePopulator extends BlockPopulator { Random random = PopulationUtil.getRandom(chunk); int cx = (chunk.getX() << 4); int cz = (chunk.getZ() << 4); - TerraWorld tw = TerraWorld.getWorld(world); + TerraWorld tw = main.getWorld(world); if(!tw.isSafe()) return; TerraBiomeGrid grid = tw.getGrid(); ConfigPack config = tw.getConfig(); @@ -50,7 +56,7 @@ public class StructurePopulator extends BlockPopulator { for(int y = conf.getSpawnStart().get(r2); y > 0; y--) { if(!conf.getBound().isInRange(y)) continue structure; spawn.setY(y); - if(!struc.checkSpawns(spawn, rotation)) continue; + if(!struc.checkSpawns(spawn, rotation, main)) continue; double horizontal = struc.getStructureInfo().getMaxHorizontal(); if(FastMath.abs((cx + 8) - spawn.getBlockX()) <= horizontal && FastMath.abs((cz + 8) - spawn.getBlockZ()) <= horizontal) { struc.paste(spawn, chunk, rotation); diff --git a/src/main/java/com/dfsek/terra/population/TreePopulator.java b/src/main/java/com/dfsek/terra/population/TreePopulator.java index e85ab6988..db5bc02c1 100644 --- a/src/main/java/com/dfsek/terra/population/TreePopulator.java +++ b/src/main/java/com/dfsek/terra/population/TreePopulator.java @@ -1,5 +1,6 @@ package com.dfsek.terra.population; +import com.dfsek.terra.Terra; import com.dfsek.terra.TerraProfiler; import com.dfsek.terra.TerraWorld; import com.dfsek.terra.biome.UserDefinedBiome; @@ -18,6 +19,11 @@ import java.util.Random; public class TreePopulator extends GaeaBlockPopulator { + private final Terra main; + + public TreePopulator(Terra main) { + this.main = main; + } private static int offset(Random r, int i) { return FastMath.min(FastMath.max(i + r.nextInt(3) - 1, 0), 15); @@ -27,7 +33,7 @@ public class TreePopulator extends GaeaBlockPopulator { @SuppressWarnings("try") public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk chunk) { try(ProfileFuture ignored = TerraProfiler.fromWorld(world).measure("TreeTime")) { - TerraWorld tw = TerraWorld.getWorld(world); + TerraWorld tw = main.getWorld(world); if(!tw.isSafe()) return; TerraBiomeGrid grid = tw.getGrid(); for(int x = 0; x < 16; x += 2) { diff --git a/src/main/java/com/dfsek/terra/structure/Structure.java b/src/main/java/com/dfsek/terra/structure/Structure.java index 8ab1e0bf6..02c32115a 100644 --- a/src/main/java/com/dfsek/terra/structure/Structure.java +++ b/src/main/java/com/dfsek/terra/structure/Structure.java @@ -1,5 +1,6 @@ package com.dfsek.terra.structure; +import com.dfsek.terra.Terra; import com.dfsek.terra.debug.Debug; import com.dfsek.terra.procgen.math.Vector2; import com.dfsek.terra.util.structure.RotationUtil; @@ -284,10 +285,10 @@ public class Structure implements Serializable { } } - public boolean checkSpawns(Location origin, Rotation r) { + public boolean checkSpawns(Location origin, Rotation r, Terra main) { for(StructureContainedBlock b : spawns) { Vector2 rot = getRotatedCoords(new Vector2(b.getX() - structureInfo.getCenterX(), b.getZ() - structureInfo.getCenterZ()), r); - if(!b.getRequirement().getInstance(origin.getWorld()).matches((int) rot.getX() + origin.getBlockX(), origin.getBlockY() + b.getY(), (int) rot.getZ() + origin.getBlockZ())) + if(!b.getRequirement().getInstance(origin.getWorld(), main).matches((int) rot.getX() + origin.getBlockX(), origin.getBlockY() + b.getY(), (int) rot.getZ() + origin.getBlockZ())) return false; } return true; diff --git a/src/main/java/com/dfsek/terra/structure/StructureSpawnRequirement.java b/src/main/java/com/dfsek/terra/structure/StructureSpawnRequirement.java index c1c55dc08..18ef6605c 100644 --- a/src/main/java/com/dfsek/terra/structure/StructureSpawnRequirement.java +++ b/src/main/java/com/dfsek/terra/structure/StructureSpawnRequirement.java @@ -1,5 +1,6 @@ package com.dfsek.terra.structure; +import com.dfsek.terra.Terra; import com.dfsek.terra.structure.spawn.AirSpawn; import com.dfsek.terra.structure.spawn.BlankSpawn; import com.dfsek.terra.structure.spawn.LandSpawn; @@ -12,26 +13,26 @@ import java.io.Serializable; public enum StructureSpawnRequirement implements Serializable { AIR { @Override - public Requirement getInstance(World world) { - return new AirSpawn(world); + public Requirement getInstance(World world, Terra main) { + return new AirSpawn(world, main); } }, OCEAN { @Override - public Requirement getInstance(World world) { - return new OceanSpawn(world); + public Requirement getInstance(World world, Terra main) { + return new OceanSpawn(world, main); } }, LAND { @Override - public Requirement getInstance(World world) { - return new LandSpawn(world); + public Requirement getInstance(World world, Terra main) { + return new LandSpawn(world, main); } }, BLANK { @Override - public Requirement getInstance(World world) { + public Requirement getInstance(World world, Terra main) { return new BlankSpawn(); } }; private static final long serialVersionUID = -175639605885943679L; - public abstract Requirement getInstance(World world); + public abstract Requirement getInstance(World world, Terra main); } diff --git a/src/main/java/com/dfsek/terra/structure/spawn/AirSpawn.java b/src/main/java/com/dfsek/terra/structure/spawn/AirSpawn.java index ee1037798..cfb37f30b 100644 --- a/src/main/java/com/dfsek/terra/structure/spawn/AirSpawn.java +++ b/src/main/java/com/dfsek/terra/structure/spawn/AirSpawn.java @@ -1,5 +1,6 @@ package com.dfsek.terra.structure.spawn; +import com.dfsek.terra.Terra; import com.dfsek.terra.TerraWorld; import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.config.templates.BiomeTemplate; @@ -8,17 +9,17 @@ import org.bukkit.World; import org.polydev.gaea.generation.GenerationPhase; public class AirSpawn extends Requirement { - public AirSpawn(World world) { - super(world); + public AirSpawn(World world, Terra main) { + super(world, main); } @Override public boolean matches(int x, int y, int z) { - TerraWorld tw = TerraWorld.getWorld(getWorld()); + TerraWorld tw = main.getWorld(world); UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE); BiomeTemplate c = b.getConfig(); if(y <= c.getSeaLevel()) return false; double elevation = ((WorldGenerator) b.getGenerator()).getElevation(x, z); - return b.getGenerator().getNoise(getNoise(), getWorld(), x, y, z) + elevation <= 0; + return b.getGenerator().getNoise(getNoise(), world, x, y, z) + elevation <= 0; } } diff --git a/src/main/java/com/dfsek/terra/structure/spawn/BlankSpawn.java b/src/main/java/com/dfsek/terra/structure/spawn/BlankSpawn.java index f943f08da..910d53cee 100644 --- a/src/main/java/com/dfsek/terra/structure/spawn/BlankSpawn.java +++ b/src/main/java/com/dfsek/terra/structure/spawn/BlankSpawn.java @@ -2,7 +2,7 @@ package com.dfsek.terra.structure.spawn; public class BlankSpawn extends Requirement { public BlankSpawn() { - super(null); + super(null, null); } @Override diff --git a/src/main/java/com/dfsek/terra/structure/spawn/LandSpawn.java b/src/main/java/com/dfsek/terra/structure/spawn/LandSpawn.java index ffc6132b6..696cda4ec 100644 --- a/src/main/java/com/dfsek/terra/structure/spawn/LandSpawn.java +++ b/src/main/java/com/dfsek/terra/structure/spawn/LandSpawn.java @@ -1,5 +1,6 @@ package com.dfsek.terra.structure.spawn; +import com.dfsek.terra.Terra; import com.dfsek.terra.TerraWorld; import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.generation.config.WorldGenerator; @@ -7,15 +8,15 @@ import org.bukkit.World; import org.polydev.gaea.generation.GenerationPhase; public class LandSpawn extends Requirement { - public LandSpawn(World world) { - super(world); + public LandSpawn(World world, Terra main) { + super(world, main); } @Override public boolean matches(int x, int y, int z) { - TerraWorld tw = TerraWorld.getWorld(getWorld()); + TerraWorld tw = main.getWorld(world); UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE); double elevation = ((WorldGenerator) b.getGenerator()).getElevation(x, z); - return b.getGenerator().getNoise(getNoise(), getWorld(), x, y, z) + elevation > 0; + return b.getGenerator().getNoise(getNoise(), world, x, y, z) + elevation > 0; } } diff --git a/src/main/java/com/dfsek/terra/structure/spawn/OceanSpawn.java b/src/main/java/com/dfsek/terra/structure/spawn/OceanSpawn.java index 936a51940..d17815d50 100644 --- a/src/main/java/com/dfsek/terra/structure/spawn/OceanSpawn.java +++ b/src/main/java/com/dfsek/terra/structure/spawn/OceanSpawn.java @@ -1,5 +1,6 @@ package com.dfsek.terra.structure.spawn; +import com.dfsek.terra.Terra; import com.dfsek.terra.TerraWorld; import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.config.templates.BiomeTemplate; @@ -8,17 +9,17 @@ import org.bukkit.World; import org.polydev.gaea.generation.GenerationPhase; public class OceanSpawn extends Requirement { - public OceanSpawn(World world) { - super(world); + public OceanSpawn(World world, Terra main) { + super(world, main); } @Override public boolean matches(int x, int y, int z) { - TerraWorld tw = TerraWorld.getWorld(getWorld()); + TerraWorld tw = main.getWorld(world); UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE); BiomeTemplate c = b.getConfig(); if(y > c.getSeaLevel()) return false; double elevation = ((WorldGenerator) b.getGenerator()).getElevation(x, z); - return b.getGenerator().getNoise(getNoise(), getWorld(), x, y, z) + elevation <= 0; + return b.getGenerator().getNoise(getNoise(), world, x, y, z) + elevation <= 0; } } diff --git a/src/main/java/com/dfsek/terra/structure/spawn/Requirement.java b/src/main/java/com/dfsek/terra/structure/spawn/Requirement.java index 5c4a64248..0fd0b4174 100644 --- a/src/main/java/com/dfsek/terra/structure/spawn/Requirement.java +++ b/src/main/java/com/dfsek/terra/structure/spawn/Requirement.java @@ -1,5 +1,6 @@ package com.dfsek.terra.structure.spawn; +import com.dfsek.terra.Terra; import com.dfsek.terra.generation.TerraChunkGenerator; import org.bukkit.World; import org.polydev.gaea.math.FastNoiseLite; @@ -7,10 +8,12 @@ import org.polydev.gaea.math.FastNoiseLite; import java.util.Objects; public abstract class Requirement { - private final World world; + protected final World world; + protected final Terra main; - public Requirement(World world) { + public Requirement(World world, Terra main) { this.world = world; + this.main = main; } public abstract boolean matches(int x, int y, int z);