Remove singleton getter

This commit is contained in:
dfsek
2020-12-06 02:47:14 -07:00
parent d946ea9b15
commit 1940309954
18 changed files with 71 additions and 62 deletions

View File

@@ -10,6 +10,7 @@ import com.dfsek.terra.generation.TerraChunkGenerator;
import com.dfsek.terra.listeners.EventListener;
import com.dfsek.terra.listeners.SpigotListener;
import com.dfsek.terra.registry.ConfigRegistry;
import com.dfsek.terra.util.ConfigUtil;
import com.dfsek.terra.util.PaperUtil;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
@@ -27,23 +28,18 @@ import java.util.Objects;
public class Terra extends GaeaPlugin {
private static Terra instance;
private final Map<String, TerraChunkGenerator> generatorMap = new HashMap<>();
public static Terra getInstance() {
return instance;
}
@Override
public void onDisable() {
TerraChunkGenerator.saveAll();
}
@SuppressWarnings("deprecation")
@Override
public void onEnable() {
Debug.setMain(this); // Set debug logger.
instance = this; // Singleton B)
Debug.setLogger(getLogger()); // Set debug logger.
ConfigUtil.setMain(this); // Set ConfigLoader's instance for use in some loaders
saveDefaultConfig();
@@ -68,7 +64,7 @@ public class Terra extends GaeaPlugin {
long save = PluginConfig.getDataSaveInterval();
Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, TerraChunkGenerator::saveAll, save, save); // Schedule population data saving
Bukkit.getScheduler().runTaskTimerAsynchronously(this, TerraChunkGenerator::saveAll, save, save); // Schedule population data saving
Bukkit.getPluginManager().registerEvents(new EventListener(this), this); // Register master event listener
Bukkit.getPluginManager().registerEvents(new SpigotListener(this), this); // Register Spigot event listener, once Paper accepts StructureLocateEvent PR Spigot and Paper events will be separate.
@@ -81,7 +77,7 @@ public class Terra extends GaeaPlugin {
return generatorMap.computeIfAbsent(worldName, name -> {
WorldConfig c = new WorldConfig(worldName, id, this);
TerraWorld.loadWorld(c);
return new TerraChunkGenerator(c.getConfig());
return new TerraChunkGenerator(c.getConfig(), this);
});
}

View File

@@ -5,6 +5,7 @@ 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;
@@ -15,8 +16,8 @@ import java.util.function.Consumer;
*/
public class AsyncBiomeFinder extends AsyncFeatureFinder<Biome> {
public AsyncBiomeFinder(TerraBiomeGrid grid, Biome target, @NotNull Location origin, int startRadius, int maxRadius, Consumer<Vector> callback) {
super(grid, target, origin, startRadius, maxRadius, callback);
public AsyncBiomeFinder(TerraBiomeGrid grid, Biome target, @NotNull Location origin, int startRadius, int maxRadius, Consumer<Vector> callback, GaeaPlugin main) {
super(grid, target, origin, startRadius, maxRadius, callback, main);
}
/**

View File

@@ -1,12 +1,12 @@
package com.dfsek.terra.async;
import com.dfsek.terra.Terra;
import com.dfsek.terra.biome.grid.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,10 +20,12 @@ public abstract class AsyncFeatureFinder<T> implements Runnable {
protected final World world;
private final Consumer<Vector> callback;
protected int searchSize = 1;
protected final GaeaPlugin main;
public AsyncFeatureFinder(TerraBiomeGrid grid, T target, @NotNull Location origin, int startRadius, int maxRadius, Consumer<Vector> callback) {
public AsyncFeatureFinder(TerraBiomeGrid grid, T target, @NotNull Location origin, int startRadius, int maxRadius, Consumer<Vector> callback, GaeaPlugin main) {
this.grid = grid;
this.target = target;
this.main = main;
this.startRadius = startRadius;
this.maxRadius = maxRadius;
this.centerX = origin.getBlockX();
@@ -66,7 +68,7 @@ public abstract class AsyncFeatureFinder<T> implements Runnable {
toggle = !toggle;
}
Vector finalSpawn = found ? finalizeVector(new Vector(x, 0, z)) : null;
Bukkit.getScheduler().runTask(Terra.getInstance(), () -> callback.accept(finalSpawn));
Bukkit.getScheduler().runTask(main, () -> callback.accept(finalSpawn));
}

View File

@@ -10,6 +10,7 @@ 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;
@@ -19,8 +20,8 @@ import java.util.function.Consumer;
* Runnable to locate structures asynchronously
*/
public class AsyncStructureFinder extends AsyncFeatureFinder<TerraStructure> {
public AsyncStructureFinder(TerraBiomeGrid grid, TerraStructure target, @NotNull Location origin, int startRadius, int maxRadius, Consumer<Vector> callback) {
super(grid, target, origin, startRadius, maxRadius, callback);
public AsyncStructureFinder(TerraBiomeGrid grid, TerraStructure target, @NotNull Location origin, int startRadius, int maxRadius, Consumer<Vector> callback, GaeaPlugin main) {
super(grid, target, origin, startRadius, maxRadius, callback, main);
setSearchSize(target.getSpawn().getWidth() + 2 * target.getSpawn().getSeparation());
}

View File

@@ -1,6 +1,5 @@
package com.dfsek.terra.command.biome;
import com.dfsek.terra.Terra;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.async.AsyncBiomeFinder;
import com.dfsek.terra.biome.UserDefinedBiome;
@@ -45,15 +44,15 @@ public class BiomeLocateCommand extends WorldCommand {
LangUtil.send("command.biome.invalid", sender, id);
return true;
}
Bukkit.getScheduler().runTaskAsynchronously(Terra.getInstance(), new AsyncBiomeFinder(TerraWorld.getWorld(world).getGrid(), b, sender.getLocation().clone().multiply((1D / PluginConfig.getBiomeSearchResolution())), 0, maxRadius, location -> {
Bukkit.getScheduler().runTaskAsynchronously(getMain(), new AsyncBiomeFinder(TerraWorld.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) {
Location l = new Location(sender.getWorld(), location.getX(), sender.getLocation().getY(), location.getZ());
Bukkit.getScheduler().runTask(Terra.getInstance(), () -> sender.teleport(l));
Bukkit.getScheduler().runTask(getMain(), () -> sender.teleport(l));
}
} else LangUtil.send("command.biome.unable-to-locate", sender);
}));
}, getMain()));
return true;
}

View File

@@ -1,6 +1,5 @@
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;
@@ -24,7 +23,7 @@ public class RenderCommand extends WorldCommand {
try {
WorldImageGenerator g = new WorldImageGenerator(world, Integer.parseInt(args[0]), Integer.parseInt(args[1]));
g.drawWorld(sender.getLocation().getBlockX(), sender.getLocation().getBlockZ());
File file = new File(Terra.getInstance().getDataFolder() + File.separator + "export" + File.separator + "map" + File.separator + "map_" + System.currentTimeMillis() + ".png");
File file = new File(getMain().getDataFolder() + File.separator + "export" + File.separator + "map" + File.separator + "map_" + System.currentTimeMillis() + ".png");
//noinspection ResultOfMethodCallIgnored
file.mkdirs();
//noinspection ResultOfMethodCallIgnored

View File

@@ -1,6 +1,5 @@
package com.dfsek.terra.command.structure;
import com.dfsek.terra.Terra;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.structure.InitializationException;
import com.dfsek.terra.structure.Structure;
@@ -36,7 +35,7 @@ public class ExportCommand extends PlayerCommand {
return true;
}
try {
File file = new File(Terra.getInstance().getDataFolder() + File.separator + "export" + File.separator + "structures", args[0] + ".tstructure");
File file = new File(getMain().getDataFolder() + File.separator + "export" + File.separator + "structures", args[0] + ".tstructure");
//noinspection ResultOfMethodCallIgnored
file.getParentFile().mkdirs();
//noinspection ResultOfMethodCallIgnored

View File

@@ -1,6 +1,5 @@
package com.dfsek.terra.command.structure;
import com.dfsek.terra.Terra;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.async.AsyncStructureFinder;
import com.dfsek.terra.config.lang.LangUtil;
@@ -45,18 +44,18 @@ public class LocateCommand extends WorldCommand {
LangUtil.send("command.structure.invalid", sender, id);
return true;
}
Bukkit.getScheduler().runTaskAsynchronously(Terra.getInstance(), new AsyncStructureFinder(TerraWorld.getWorld(world).getGrid(), s, sender.getLocation(), 0, maxRadius, (location) -> {
Bukkit.getScheduler().runTaskAsynchronously(getMain(), new AsyncStructureFinder(TerraWorld.getWorld(world).getGrid(), s, sender.getLocation(), 0, maxRadius, (location) -> {
if(sender.isOnline()) {
if(location != null) {
sender.sendMessage("Located structure at (" + location.getBlockX() + ", " + location.getBlockZ() + ").");
if(tp) {
int finalX = location.getBlockX();
int finalZ = location.getBlockZ();
Bukkit.getScheduler().runTask(Terra.getInstance(), () -> sender.teleport(new Location(sender.getWorld(), finalX, sender.getLocation().getY(), finalZ)));
Bukkit.getScheduler().runTask(getMain(), () -> sender.teleport(new Location(sender.getWorld(), finalX, sender.getLocation().getY(), finalZ)));
}
} else sender.sendMessage("Unable to locate structure. ");
}
}));
}, getMain()));
return true;
}

View File

@@ -1,6 +1,5 @@
package com.dfsek.terra.command.structure.load;
import com.dfsek.terra.Terra;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -21,9 +20,9 @@ public class LoadCommand extends PlayerCommand implements DebugCommand {
super(parent);
}
public static List<String> getStructureNames() {
public List<String> getStructureNames() {
List<String> names = new ArrayList<>();
File structureDir = new File(Terra.getInstance().getDataFolder() + File.separator + "export" + File.separator + "structures");
File structureDir = new File(getMain().getDataFolder() + File.separator + "export" + File.separator + "structures");
if(!structureDir.exists()) return Collections.emptyList();
Path structurePath = structureDir.toPath();

View File

@@ -1,6 +1,5 @@
package com.dfsek.terra.command.structure.load;
import com.dfsek.terra.Terra;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.structure.Rotation;
import com.dfsek.terra.structure.Structure;
@@ -9,7 +8,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.command.DebugCommand;
import org.polydev.gaea.command.PlayerCommand;
import java.io.File;
import java.io.IOException;
@@ -18,7 +16,7 @@ import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class LoadFullCommand extends PlayerCommand implements DebugCommand {
public class LoadFullCommand extends LoadCommand implements DebugCommand {
private final boolean chunk;
public LoadFullCommand(org.polydev.gaea.command.Command parent, boolean chunk) {
@@ -36,7 +34,7 @@ public class LoadFullCommand extends PlayerCommand implements DebugCommand {
LangUtil.send("command.structure.invalid-rotation", sender, args[1]);
return true;
}
Structure struc = Structure.load(new File(Terra.getInstance().getDataFolder() + File.separator + "export" + File.separator + "structures", args[0] + ".tstructure"));
Structure struc = Structure.load(new File(getMain().getDataFolder() + File.separator + "export" + File.separator + "structures", args[0] + ".tstructure"));
if(chunk) struc.paste(sender.getLocation(), sender.getLocation().getChunk(), r);
else struc.paste(sender.getLocation(), r);
//sender.sendMessage(String.valueOf(struc.checkSpawns(sender.getLocation(), r)));
@@ -66,7 +64,7 @@ public class LoadFullCommand extends PlayerCommand implements DebugCommand {
public List<String> getTabCompletions(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] args) {
switch(args.length) {
case 1:
return LoadCommand.getStructureNames().stream().filter(string -> string.toUpperCase().startsWith(args[0].toUpperCase())).collect(Collectors.toList());
return getStructureNames().stream().filter(string -> string.toUpperCase().startsWith(args[0].toUpperCase())).collect(Collectors.toList());
case 2:
return Stream.of("0", "90", "180", "270").filter(string -> string.toUpperCase().startsWith(args[1].toUpperCase())).collect(Collectors.toList());
}

View File

@@ -1,6 +1,5 @@
package com.dfsek.terra.command.structure.load;
import com.dfsek.terra.Terra;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.structure.Structure;
import com.dfsek.terra.structure.StructureContainedBlock;
@@ -14,7 +13,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.command.DebugCommand;
import org.polydev.gaea.command.PlayerCommand;
import java.io.File;
import java.io.IOException;
@@ -22,7 +20,7 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class LoadRawCommand extends PlayerCommand implements DebugCommand {
public class LoadRawCommand extends LoadCommand implements DebugCommand {
public LoadRawCommand(org.polydev.gaea.command.Command parent) {
super(parent);
}
@@ -36,7 +34,7 @@ public class LoadRawCommand extends PlayerCommand implements DebugCommand {
@Override
public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
try {
Structure struc = Structure.load(new File(Terra.getInstance().getDataFolder() + File.separator + "export" + File.separator + "structures", args[0] + ".tstructure"));
Structure struc = Structure.load(new File(getMain().getDataFolder() + File.separator + "export" + File.separator + "structures", args[0] + ".tstructure"));
StructureInfo info = struc.getStructureInfo();
int centerX = info.getCenterX();
int centerZ = info.getCenterZ();
@@ -106,7 +104,7 @@ public class LoadRawCommand extends PlayerCommand implements DebugCommand {
@Override
public List<String> getTabCompletions(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] args) {
if(args.length == 1) {
return LoadCommand.getStructureNames().stream().filter(string -> string.toUpperCase().startsWith(args[0].toUpperCase())).collect(Collectors.toList());
return getStructureNames().stream().filter(string -> string.toUpperCase().startsWith(args[0].toUpperCase())).collect(Collectors.toList());
}
return Collections.emptyList();
}

View File

@@ -31,7 +31,7 @@ public final class LangUtil {
Debug.error("Report this to Terra!");
}
try {
language = new Language(new File(Terra.getInstance().getDataFolder(), "lang" + File.separator + langID + ".yml"));
language = new Language(new File(main.getDataFolder(), "lang" + File.separator + langID + ".yml"));
} catch(InvalidConfigurationException | IOException e) {
logger.severe("Unable to load language: " + langID);
e.printStackTrace();

View File

@@ -5,6 +5,7 @@ import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.tectonic.loading.TypeLoader;
import com.dfsek.terra.config.loaders.Types;
import com.dfsek.terra.generation.items.tree.TreeLayer;
import org.polydev.gaea.GaeaPlugin;
import org.polydev.gaea.math.FastNoiseLite;
import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.math.Range;
@@ -15,6 +16,12 @@ import java.util.Map;
@SuppressWarnings("unchecked")
public class TreeLayerLoader implements TypeLoader<TreeLayer> {
private final GaeaPlugin main;
public TreeLayerLoader(GaeaPlugin main) {
this.main = main;
}
@Override
public TreeLayer load(Type type, Object o, ConfigLoader configLoader) throws LoadException {
Map<String, Object> map = (Map<String, Object>) o;
@@ -26,9 +33,9 @@ public class TreeLayerLoader implements TypeLoader<TreeLayer> {
if(map.containsKey("simplex-frequency")) {
FastNoiseLite noiseLite = new FastNoiseLite();
noiseLite.setFrequency((Double) map.get("simplex-frequency"));
return new TreeLayer(density, range, items, noiseLite);
return new TreeLayer(density, range, items, noiseLite, main);
}
return new TreeLayer(density, range, items, null);
return new TreeLayer(density, range, items, null, main);
}
}

View File

@@ -1,25 +1,26 @@
package com.dfsek.terra.debug;
import com.dfsek.terra.config.base.PluginConfig;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.logging.Logger;
public class Debug {
public static JavaPlugin main;
public static Logger logger;
public static void setMain(JavaPlugin main) {
Debug.main = main;
public static void setLogger(Logger logger) {
Debug.logger = logger;
}
public static void info(String message) {
if(PluginConfig.isDebug()) main.getLogger().info(message);
if(PluginConfig.isDebug()) logger.info(message);
}
public static void warn(String message) {
if(PluginConfig.isDebug()) main.getLogger().warning(message);
if(PluginConfig.isDebug()) logger.warning(message);
}
public static void error(String message) {
if(PluginConfig.isDebug()) main.getLogger().severe(message);
if(PluginConfig.isDebug()) logger.severe(message);
}
public static void stack(Exception e) {

View File

@@ -1,6 +1,5 @@
package com.dfsek.terra.generation;
import com.dfsek.terra.Terra;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.biome.UserDefinedBiome;
@@ -28,6 +27,7 @@ import org.bukkit.block.data.type.Stairs;
import org.bukkit.generator.BlockPopulator;
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.GaeaChunkGenerator;
import org.polydev.gaea.generation.GenerationPhase;
@@ -51,12 +51,13 @@ import java.util.logging.Level;
public class TerraChunkGenerator extends GaeaChunkGenerator {
private static final Map<World, PopulationManager> popMap = new HashMap<>();
private final PopulationManager popMan = new PopulationManager(Terra.getInstance());
private final PopulationManager popMan;
private final ConfigPack configPack;
private boolean needsLoad = true;
public TerraChunkGenerator(ConfigPack c) {
public TerraChunkGenerator(ConfigPack c, GaeaPlugin main) {
super(ChunkInterpolator.InterpolationType.TRILINEAR);
popMan = new PopulationManager(main);
this.configPack = c;
popMan.attach(new OrePopulator());
popMan.attach(new TreePopulator());

View File

@@ -1,11 +1,11 @@
package com.dfsek.terra.generation.items.tree;
import com.dfsek.terra.Terra;
import com.dfsek.terra.generation.items.PlaceableLayer;
import com.dfsek.terra.procgen.math.Vector2;
import org.bukkit.Chunk;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.polydev.gaea.GaeaPlugin;
import org.polydev.gaea.math.FastNoiseLite;
import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.math.Range;
@@ -15,9 +15,11 @@ import org.polydev.gaea.tree.TreeType;
import java.util.Random;
public class TreeLayer extends PlaceableLayer<Tree> {
private final GaeaPlugin main;
public TreeLayer(double density, Range level, ProbabilityCollection<Tree> layer, FastNoiseLite noise) {
public TreeLayer(double density, Range level, ProbabilityCollection<Tree> layer, FastNoiseLite noise, GaeaPlugin main) {
super(density, level, layer, noise);
this.main = main;
}
@Override
@@ -28,8 +30,8 @@ public class TreeLayer extends PlaceableLayer<Tree> {
current = current.getRelative(BlockFace.DOWN);
if(item.getSpawnable().contains(current.getType())) {
if(item instanceof TreeType && current.getRelative(BlockFace.UP).isEmpty())
item.plant(current.getLocation().add(0, 1, 0), random, Terra.getInstance());
else if(!(item instanceof TreeType)) item.plant(current.getLocation(), random, Terra.getInstance());
item.plant(current.getLocation().add(0, 1, 0), random, main);
else if(!(item instanceof TreeType)) item.plant(current.getLocation(), random, main);
}
}
}

View File

@@ -21,7 +21,7 @@ import org.polydev.gaea.GaeaPlugin;
* Listener to load on Spigot servers, contains Villager crash prevention and hacky ender eye redirection.
* <p>
* (This is currently loaded on all servers; once Paper accepts the StructureLocateEvent PR this will only be loaded on servers without
* StructureLocateEvent.
* StructureLocateEvent).
*/
public class SpigotListener implements Listener {
private final GaeaPlugin main;
@@ -43,7 +43,7 @@ public class SpigotListener implements Listener {
AsyncStructureFinder finder = new AsyncStructureFinder(tw.getGrid(), config, e.getLocation(), 0, 500, location -> {
if(location != null) signal.setTargetLocation(location.toLocation(signal.getWorld()));
Debug.info("Location: " + location);
});
}, main);
finder.run(); // Do this synchronously so eye doesn't change direction several ticks after spawning.
} else
main.getLogger().warning("No overrides are defined for Strongholds. Ender Signals will not work correctly.");

View File

@@ -33,10 +33,17 @@ import org.bukkit.Material;
import org.bukkit.block.Biome;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.polydev.gaea.GaeaPlugin;
import org.polydev.gaea.math.ProbabilityCollection;
import org.polydev.gaea.math.Range;
public final class ConfigUtil {
private static GaeaPlugin main;
public static void setMain(GaeaPlugin main) {
ConfigUtil.main = main;
}
/**
* Register all Terra loaders to a {@link TypeRegistry}.
*
@@ -56,7 +63,7 @@ public final class ConfigUtil {
.registerLoader(Ore.Type.class, (t, o, l) -> Ore.Type.valueOf((String) o))
.registerLoader(OreConfig.class, new OreConfigLoader())
.registerLoader(NoiseBuilder.class, new NoiseBuilderLoader())
.registerLoader(TreeLayer.class, new TreeLayerLoader())
.registerLoader(TreeLayer.class, new TreeLayerLoader(main))
.registerLoader(MaterialSet.class, new MaterialSetLoader())
.registerLoader(OreHolder.class, new OreHolderLoader())
.registerLoader(Feature.class, new StructureFeatureLoader())