Skip to content

Commit

Permalink
Begin decoupling Ether components from Bukkit JavaPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
avaerian committed Sep 18, 2023
1 parent 444de25 commit a6af587
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 16 deletions.
67 changes: 67 additions & 0 deletions main/src/main/java/org/minerift/ether/Ether.java
Original file line number Diff line number Diff line change
@@ -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 extends Config<T>> T getConfig(ConfigType<T> type) {
ensure(configRegistry != null, () -> new UnsupportedOperationException("configRegistry was not loaded!"));
return configRegistry.get(type);
}

private static <E extends Exception> void ensure(boolean predicate, Supplier<E> ex) throws E {
if(!predicate) {
throw ex.get();
}
}
}
24 changes: 14 additions & 10 deletions main/src/main/java/org/minerift/ether/EtherPlugin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,24 +14,28 @@ 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() {

}

@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();

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BlockArchetype> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit a6af587

Please sign in to comment.