diff --git a/main/src/main/java/org/minerift/ether/Ether.java b/main/src/main/java/org/minerift/ether/Ether.java new file mode 100644 index 0000000..3a6a1e9 --- /dev/null +++ b/main/src/main/java/org/minerift/ether/Ether.java @@ -0,0 +1,67 @@ +package org.minerift.ether; + +import org.bukkit.Bukkit; +import org.minerift.ether.config.Config; +import org.minerift.ether.config.ConfigRegistry; +import org.minerift.ether.config.exceptions.ConfigFileReadException; +import org.minerift.ether.config.types.ConfigType; +import org.minerift.ether.nms.NMSAccess; + +import java.io.IOException; +import java.util.function.Supplier; +import java.util.logging.Logger; + +// Provides static access to plugin components +// Must call load() before accessing any components +// TODO: replace all EtherPlugin references with Ether +// TODO: add loadNoPlugin() method to load an instance without a plugin +public class Ether { + + private static EtherPlugin plugin; + private static ConfigRegistry configRegistry; + private static Logger logger; + + private static NMSAccess nmsAccess; + + protected static void load(EtherPlugin inst) { + plugin = inst; + //Bukkit.getServer().reload(); // TODO: view implementation to see how plugins reload + logger = plugin.getLogger(); + + // Load configs + configRegistry = new ConfigRegistry(); + configRegistry.register(ConfigType.MAIN); + configRegistry.register(ConfigType.SCHEM_LIST); + + // Load NMS access + nmsAccess = new NMSAccess(); + } + + private Ether() {} + + public static ConfigRegistry getConfigRegistry() { + ensure(configRegistry != null, () -> new UnsupportedOperationException("configRegistry was not loaded!")); + return configRegistry; + } + + public static Logger getLogger() { + ensure(logger != null, () -> new UnsupportedOperationException("logger was not loaded!")); + return logger; + } + + public static NMSAccess getNMS() { + ensure(nmsAccess != null, () -> new UnsupportedOperationException("nmsAccess was not loaded!")); + return nmsAccess; + } + + public static > T getConfig(ConfigType type) { + ensure(configRegistry != null, () -> new UnsupportedOperationException("configRegistry was not loaded!")); + return configRegistry.get(type); + } + + private static void ensure(boolean predicate, Supplier ex) throws E { + if(!predicate) { + throw ex.get(); + } + } +} diff --git a/main/src/main/java/org/minerift/ether/EtherPlugin.java b/main/src/main/java/org/minerift/ether/EtherPlugin.java index 6eb3b89..4de3d64 100644 --- a/main/src/main/java/org/minerift/ether/EtherPlugin.java +++ b/main/src/main/java/org/minerift/ether/EtherPlugin.java @@ -1,11 +1,11 @@ package org.minerift.ether; import org.bukkit.plugin.java.JavaPlugin; -import org.minerift.ether.debug.*; +import org.minerift.ether.debug.NMSBlockScanDebugCommand; +import org.minerift.ether.debug.NMSChunkDebugCommand; +import org.minerift.ether.debug.NMSSetBlocksDebugCommand; +import org.minerift.ether.debug.SchematicDebugCommand; import org.minerift.ether.island.IslandManager; -import org.minerift.ether.nms.NMSAccess; -import org.minerift.ether.schematic.pasters.SpongeSchematicPaster; -import org.minerift.ether.schematic.types.SchematicType; import org.minerift.ether.work.WorkQueue; import java.util.logging.Level; @@ -14,13 +14,15 @@ public class EtherPlugin extends JavaPlugin { private static EtherPlugin INSTANCE = null; - private boolean isUsingWorldEdit; - private NMSAccess nmsAccess; private WorkQueue workQueue; private IslandManager islandManager; + public static EtherPlugin getInstance() { + return INSTANCE; + } + @Override public void onLoad() { @@ -28,10 +30,12 @@ public void onLoad() { @Override public void onEnable() { + INSTANCE = this; + Ether.load(INSTANCE); + // Load other stuff this.isUsingWorldEdit = false; // TODO - this.nmsAccess = new NMSAccess(); this.workQueue = new WorkQueue(); workQueue.start(); @@ -50,19 +54,19 @@ public void onEnable() { public void onDisable() { workQueue.close(); workQueue = null; - } - public static EtherPlugin getInstance() { - return INSTANCE; + // TODO: close ConfigManager } public boolean isUsingWorldEdit() { return isUsingWorldEdit; } + /* public NMSAccess getNMS() { return nmsAccess; } + */ public IslandManager getIslandManager() { return islandManager; diff --git a/main/src/main/java/org/minerift/ether/debug/NMSBlockScanDebugCommand.java b/main/src/main/java/org/minerift/ether/debug/NMSBlockScanDebugCommand.java index 7c9f5f4..a212cd7 100644 --- a/main/src/main/java/org/minerift/ether/debug/NMSBlockScanDebugCommand.java +++ b/main/src/main/java/org/minerift/ether/debug/NMSBlockScanDebugCommand.java @@ -6,7 +6,9 @@ import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.minerift.ether.Ether; import org.minerift.ether.EtherPlugin; +import org.minerift.ether.config.types.ConfigType; import org.minerift.ether.nms.NMSAccess; public class NMSBlockScanDebugCommand implements CommandExecutor { @@ -27,7 +29,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command mode = args[0].toUpperCase(); } - final NMSAccess nmsAccess = EtherPlugin.getInstance().getNMS(); + //final NMSAccess nmsAccess = EtherPlugin.getInstance().getNMS(); + final NMSAccess nmsAccess = Ether.getNMS(); switch (mode) { case "SEC" -> nmsAccess.testIslandScanIdea(plr.getLocation()); diff --git a/main/src/main/java/org/minerift/ether/debug/NMSChunkDebugCommand.java b/main/src/main/java/org/minerift/ether/debug/NMSChunkDebugCommand.java index b64ea7b..ad880b5 100644 --- a/main/src/main/java/org/minerift/ether/debug/NMSChunkDebugCommand.java +++ b/main/src/main/java/org/minerift/ether/debug/NMSChunkDebugCommand.java @@ -8,6 +8,7 @@ import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.minerift.ether.Ether; import org.minerift.ether.EtherPlugin; import org.minerift.ether.nms.NMSAccess; @@ -35,7 +36,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command Player plr = (Player) sender; World world = plr.getWorld(); - NMSAccess nmsAccess = EtherPlugin.getInstance().getNMS(); + //NMSAccess nmsAccess = EtherPlugin.getInstance().getNMS(); + final NMSAccess nmsAccess = Ether.getNMS(); int centerX = plr.getChunk().getX(); int centerZ = plr.getChunk().getZ(); diff --git a/main/src/main/java/org/minerift/ether/debug/NMSSetBlocksDebugCommand.java b/main/src/main/java/org/minerift/ether/debug/NMSSetBlocksDebugCommand.java index 1aadb53..d0edaf1 100644 --- a/main/src/main/java/org/minerift/ether/debug/NMSSetBlocksDebugCommand.java +++ b/main/src/main/java/org/minerift/ether/debug/NMSSetBlocksDebugCommand.java @@ -9,6 +9,7 @@ import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.minerift.ether.Ether; import org.minerift.ether.EtherPlugin; import org.minerift.ether.nms.NMSAccess; import org.minerift.ether.util.BukkitUtils; @@ -44,11 +45,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command } plr.sendMessage("Setting blocks..."); - NMSAccess nmsAccess = EtherPlugin.getInstance().getNMS(); + //NMSAccess nmsAccess = EtherPlugin.getInstance().getNMS(); + final NMSAccess nmsAccess = Ether.getNMS(); // Get cuboid and translate to player pos List cuboid = getTestCuboid(width, height, length); - cuboid.forEach(block -> block.getPos().add(BukkitUtils.getPosAsVec3i(plr.getLocation()))); + cuboid.forEach(block -> block.getPos().add(BukkitUtils.asVec3i(plr.getLocation()))); // Set blocks based on mode switch (mode) { diff --git a/main/src/main/java/org/minerift/ether/schematic/pasters/SpongeSchematicPaster.java b/main/src/main/java/org/minerift/ether/schematic/pasters/SpongeSchematicPaster.java index 3ad7a86..aa4eb95 100644 --- a/main/src/main/java/org/minerift/ether/schematic/pasters/SpongeSchematicPaster.java +++ b/main/src/main/java/org/minerift/ether/schematic/pasters/SpongeSchematicPaster.java @@ -4,7 +4,7 @@ import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.block.Biome; -import org.minerift.ether.EtherPlugin; +import org.minerift.ether.Ether; import org.minerift.ether.nms.NMSAccess; import org.minerift.ether.schematic.SchematicPasteOptions; import org.minerift.ether.schematic.types.SpongeSchematic; @@ -26,7 +26,8 @@ public void paste(SpongeSchematic schem, Vec3i pos, String worldName, SchematicP // Lazily set blocks schem.getBlocks().forEach(block -> block.getPos().add(worldPasteLoc)); // translate to proper pos - final NMSAccess nmsAccess = EtherPlugin.getInstance().getNMS(); + //final NMSAccess nmsAccess = EtherPlugin.getInstance().getNMS(); + final NMSAccess nmsAccess = Ether.getNMS(); nmsAccess.setBlocksAsyncLazy(schem.getBlocks(), world); // Set biomes