From cc6fdce63a7973213f0502918c351e536a025941 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sat, 31 Aug 2024 22:54:35 -0700 Subject: [PATCH 01/18] Use dependency injection --- .../net/countercraft/movecraft/Movecraft.java | 294 ++++-------------- .../movecraft/async/AsyncManager.java | 41 ++- .../movecraft/craft/ChunkManager.java | 6 +- .../movecraft/listener/BlockListener.java | 4 + .../listener/CraftPilotListener.java | 3 + .../listener/CraftReleaseListener.java | 3 + .../movecraft/listener/InteractListener.java | 8 +- .../movecraft/listener/PlayerListener.java | 8 +- .../movecraft/localisation/I18nSupport.java | 24 +- .../mapUpdater/MapUpdateManager.java | 23 +- .../movecraft/sign/AscendSign.java | 3 + .../movecraft/sign/CraftSign.java | 8 +- .../movecraft/sign/CruiseSign.java | 3 + .../movecraft/sign/DescendSign.java | 3 + .../countercraft/movecraft/sign/HelmSign.java | 3 + .../countercraft/movecraft/sign/MoveSign.java | 4 + .../countercraft/movecraft/sign/NameSign.java | 5 + .../movecraft/sign/PilotSign.java | 5 + .../movecraft/sign/RelativeMoveSign.java | 4 + .../movecraft/sign/ReleaseSign.java | 4 + .../movecraft/sign/RemoteSign.java | 4 + .../movecraft/sign/ScuttleSign.java | 5 +- .../movecraft/sign/SpeedSign.java | 6 +- .../movecraft/sign/SubcraftRotateSign.java | 8 +- .../movecraft/sign/TeleportSign.java | 4 + .../support/SmoothTeleportFactory.java | 42 +++ .../support/WorldHandlerFactory.java | 41 +++ api/build.gradle.kts | 1 + .../countercraft/movecraft/WorldHandler.java | 1 + .../movecraft/config/DataPackService.java | 121 +++++++ .../movecraft/config/Settings.java | 1 + .../movecraft/config/SettingsService.java | 79 +++++ .../lifecycle/ListenerLifecycleService.java | 24 ++ .../movecraft/lifecycle/Service.java | 6 + .../movecraft/lifecycle/ServiceHost.java | 23 ++ .../movecraft/lifecycle/Worker.java | 13 + .../lifecycle/WorkerServiceHost.java | 38 +++ .../movecraft/processing/WorldManager.java | 23 +- .../movecraft/support/VersionInfo.java | 13 + .../movecraft/support/VersionProvider.java | 24 ++ gradle/libs.versions.toml | 2 + 41 files changed, 666 insertions(+), 269 deletions(-) create mode 100644 Movecraft/src/main/java/net/countercraft/movecraft/support/SmoothTeleportFactory.java create mode 100644 Movecraft/src/main/java/net/countercraft/movecraft/support/WorldHandlerFactory.java create mode 100644 api/src/main/java/net/countercraft/movecraft/config/DataPackService.java create mode 100644 api/src/main/java/net/countercraft/movecraft/config/SettingsService.java create mode 100644 api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleService.java create mode 100644 api/src/main/java/net/countercraft/movecraft/lifecycle/Service.java create mode 100644 api/src/main/java/net/countercraft/movecraft/lifecycle/ServiceHost.java create mode 100644 api/src/main/java/net/countercraft/movecraft/lifecycle/Worker.java create mode 100644 api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerServiceHost.java create mode 100644 api/src/main/java/net/countercraft/movecraft/support/VersionInfo.java create mode 100644 api/src/main/java/net/countercraft/movecraft/support/VersionProvider.java diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index fe69859c1..3d165288f 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -17,37 +17,34 @@ package net.countercraft.movecraft; -import io.papermc.paper.datapack.Datapack; import net.countercraft.movecraft.async.AsyncManager; import net.countercraft.movecraft.commands.*; +import net.countercraft.movecraft.config.DataPackService; import net.countercraft.movecraft.config.Settings; +import net.countercraft.movecraft.config.SettingsService; import net.countercraft.movecraft.craft.ChunkManager; import net.countercraft.movecraft.craft.CraftManager; -import net.countercraft.movecraft.craft.datatag.CraftDataTagRegistry; import net.countercraft.movecraft.features.contacts.ContactsCommand; import net.countercraft.movecraft.features.contacts.ContactsManager; import net.countercraft.movecraft.features.contacts.ContactsSign; import net.countercraft.movecraft.features.fading.WreckManager; import net.countercraft.movecraft.features.status.StatusManager; import net.countercraft.movecraft.features.status.StatusSign; +import net.countercraft.movecraft.lifecycle.ServiceHost; +import net.countercraft.movecraft.lifecycle.WorkerServiceHost; import net.countercraft.movecraft.listener.*; import net.countercraft.movecraft.localisation.I18nSupport; import net.countercraft.movecraft.mapUpdater.MapUpdateManager; import net.countercraft.movecraft.processing.WorldManager; import net.countercraft.movecraft.sign.*; -import net.countercraft.movecraft.util.BukkitTeleport; -import net.countercraft.movecraft.util.Tags; -import org.bukkit.Bukkit; -import org.bukkit.Material; +import net.countercraft.movecraft.support.SmoothTeleportFactory; +import net.countercraft.movecraft.support.WorldHandlerFactory; import org.bukkit.plugin.java.JavaPlugin; +import org.int4.dirk.api.Injector; +import org.int4.dirk.di.Injectors; import org.jetbrains.annotations.NotNull; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; import java.util.logging.Logger; public class Movecraft extends JavaPlugin { @@ -55,10 +52,7 @@ public class Movecraft extends JavaPlugin { private Logger logger; private boolean shuttingDown; - private WorldHandler worldHandler; - private SmoothTeleport smoothTeleport; - private AsyncManager asyncManager; - private WreckManager wreckManager; + private Injector injector; public static synchronized Movecraft getInstance() { return instance; @@ -67,137 +61,38 @@ public static synchronized Movecraft getInstance() { @Override public void onDisable() { shuttingDown = true; + injector.getInstance(ServiceHost.class).stopAll(); + injector = null; } @Override public void onEnable() { - // Read in config - Settings.LOCALE = getConfig().getString("Locale"); - Settings.Debug = getConfig().getBoolean("Debug", false); - Settings.DisableNMSCompatibilityCheck = getConfig().getBoolean("IReallyKnowWhatIAmDoing", false); - Settings.DisableSpillProtection = getConfig().getBoolean("DisableSpillProtection", false); - Settings.DisableIceForm = getConfig().getBoolean("DisableIceForm", true); - Settings.ReleaseOnDeath = getConfig().getBoolean("ReleaseOnDeath", false); - - String[] localisations = {"en", "cz", "nl", "fr"}; - for (String s : localisations) { - if (!new File(getDataFolder() - + "/localisation/movecraftlang_" + s + ".properties").exists()) { - saveResource("localisation/movecraftlang_" + s + ".properties", false); - } - } - I18nSupport.init(); - - - // if the PilotTool is specified in the config.yml file, use it - String pilotTool = getConfig().getString("PilotTool"); - if (pilotTool != null) { - Material material = Material.getMaterial(pilotTool); - if (material != null) { - logger.info("Recognized PilotTool setting of: " + pilotTool); - Settings.PilotTool = material; - } - else { - logger.info("No PilotTool setting, using default of stick"); - } - } - else { - logger.info("No PilotTool setting, using default of stick"); - } - - String minecraftVersion = getServer().getMinecraftVersion(); - getLogger().info("Loading support for " + minecraftVersion); - try { - final Class worldHandlerClazz = Class.forName("net.countercraft.movecraft.compat." + WorldHandler.getPackageName(minecraftVersion) + ".IWorldHandler"); - // Check if we have a NMSHandler class at that location. - if (WorldHandler.class.isAssignableFrom(worldHandlerClazz)) { // Make sure it actually implements NMS - worldHandler = (WorldHandler) worldHandlerClazz.getConstructor().newInstance(); // Set our handler - - // Try to setup the smooth teleport handler - try { - final Class smoothTeleportClazz = Class.forName("net.countercraft.movecraft.support." + WorldHandler.getPackageName(minecraftVersion) + ".ISmoothTeleport"); - if (SmoothTeleport.class.isAssignableFrom(smoothTeleportClazz)) { - smoothTeleport = (SmoothTeleport) smoothTeleportClazz.getConstructor().newInstance(); - } - else { - smoothTeleport = new BukkitTeleport(); // Fall back to bukkit teleportation - getLogger().warning("Did not find smooth teleport, falling back to bukkit teleportation provider."); - } - } - catch (final ReflectiveOperationException e) { - if (Settings.Debug) { - e.printStackTrace(); - } - smoothTeleport = new BukkitTeleport(); // Fall back to bukkit teleportation - getLogger().warning("Falling back to bukkit teleportation provider."); - } - } - } - catch (final Exception e) { - e.printStackTrace(); - getLogger().severe("Could not find support for this version."); - if (!Settings.DisableNMSCompatibilityCheck) { - // Disable ourselves and exit - setEnabled(false); - return; - } - else { - // Server owner claims to know what they are doing, warn them of the possible consequences - getLogger().severe("WARNING!\n\t" - + "Running Movecraft on an incompatible version can corrupt your world and break EVERYTHING!\n\t" - + "We provide no support for any issues."); - } - } - - - Settings.SinkCheckTicks = getConfig().getDouble("SinkCheckTicks", 100.0); - Settings.ManOverboardTimeout = getConfig().getInt("ManOverboardTimeout", 30); - Settings.ManOverboardDistSquared = Math.pow(getConfig().getDouble("ManOverboardDistance", 1000), 2); - Settings.SilhouetteViewDistance = getConfig().getInt("SilhouetteViewDistance", 200); - Settings.SilhouetteBlockCount = getConfig().getInt("SilhouetteBlockCount", 20); - Settings.ProtectPilotedCrafts = getConfig().getBoolean("ProtectPilotedCrafts", false); - Settings.MaxRemoteSigns = getConfig().getInt("MaxRemoteSigns", -1); - Settings.CraftsUseNetherPortals = getConfig().getBoolean("CraftsUseNetherPortals", false); - Settings.RequireCreatePerm = getConfig().getBoolean("RequireCreatePerm", false); - Settings.RequireNamePerm = getConfig().getBoolean("RequireNamePerm", true); - Settings.FadeWrecksAfter = getConfig().getInt("FadeWrecksAfter", 0); - Settings.FadeTickCooldown = getConfig().getInt("FadeTickCooldown", 20); - Settings.FadePercentageOfWreckPerCycle = getConfig().getDouble("FadePercentageOfWreckPerCycle", 10.0); - if (getConfig().contains("ExtraFadeTimePerBlock")) { - Map temp = getConfig().getConfigurationSection("ExtraFadeTimePerBlock").getValues(false); - for (String str : temp.keySet()) { - Set materials = Tags.parseMaterials(str); - for (Material m : materials) { - Settings.ExtraFadeTimePerBlock.put(m, (Integer) temp.get(str)); - } - } - } - - Settings.ForbiddenRemoteSigns = new HashSet<>(); - for(String s : getConfig().getStringList("ForbiddenRemoteSigns")) { - Settings.ForbiddenRemoteSigns.add(s.toLowerCase()); - } - + injector = Injectors.manual(); + injector.registerInstance(getLogger()); + injector.registerInstance(this); + injector.register(AsyncManager.class); + injector.register(MapUpdateManager.class); + injector.register(SmoothTeleportFactory.class); + injector.register(WorldHandlerFactory.class); + injector.registerInstance(WorldManager.INSTANCE); + injector.register(WreckManager.class); + injector.register(I18nSupport.class); + injector.register(SettingsService.class); + + // TODO: make this work somehow if(shuttingDown && Settings.IGNORE_RESET) { logger.severe("Movecraft is incompatible with the reload command. Movecraft has shut down and will restart when the server is restarted."); logger.severe("If you wish to use the reload command and Movecraft, you may disable this check inside the config.yml by setting 'safeReload: false'"); getPluginLoader().disablePlugin(this); + return; } - // Startup procedure - boolean datapackInitialized = isDatapackEnabled() || initializeDatapack(); - asyncManager = new AsyncManager(); - asyncManager.runTaskTimer(this, 0, 1); - MapUpdateManager.getInstance().runTaskTimer(this, 0, 1); - - - CraftManager.initialize(datapackInitialized); - Bukkit.getScheduler().runTaskTimer(this, WorldManager.INSTANCE::run, 0,1); - wreckManager = new WreckManager(WorldManager.INSTANCE); - - getServer().getPluginManager().registerEvents(new InteractListener(), this); + // TODO: DI for CraftManager + injector.register(DataPackService.class); + CraftManager.initialize(injector.getInstance(DataPackService.class).isDatapackInitialized()); + //TODO: migrate to aikar or brigadier commands, left in place for now getCommand("movecraft").setExecutor(new MovecraftCommand()); getCommand("release").setExecutor(new ReleaseCommand()); getCommand("pilot").setExecutor(new PilotCommand()); @@ -209,26 +104,30 @@ public void onEnable() { getCommand("crafttype").setExecutor(new CraftTypeCommand()); getCommand("craftinfo").setExecutor(new CraftInfoCommand()); - getServer().getPluginManager().registerEvents(new BlockListener(), this); - getServer().getPluginManager().registerEvents(new PlayerListener(), this); - getServer().getPluginManager().registerEvents(new ChunkManager(), this); - getServer().getPluginManager().registerEvents(new AscendSign(), this); - getServer().getPluginManager().registerEvents(new CraftSign(), this); - getServer().getPluginManager().registerEvents(new CruiseSign(), this); - getServer().getPluginManager().registerEvents(new DescendSign(), this); - getServer().getPluginManager().registerEvents(new HelmSign(), this); - getServer().getPluginManager().registerEvents(new MoveSign(), this); - getServer().getPluginManager().registerEvents(new NameSign(), this); - getServer().getPluginManager().registerEvents(new PilotSign(), this); - getServer().getPluginManager().registerEvents(new RelativeMoveSign(), this); - getServer().getPluginManager().registerEvents(new ReleaseSign(), this); - getServer().getPluginManager().registerEvents(new RemoteSign(), this); - getServer().getPluginManager().registerEvents(new SpeedSign(), this); - getServer().getPluginManager().registerEvents(new SubcraftRotateSign(), this); - getServer().getPluginManager().registerEvents(new TeleportSign(), this); - getServer().getPluginManager().registerEvents(new ScuttleSign(), this); - getServer().getPluginManager().registerEvents(new CraftPilotListener(), this); - getServer().getPluginManager().registerEvents(new CraftReleaseListener(), this); + injector.register(InteractListener.class); + injector.register(BlockListener.class); + injector.register(PlayerListener.class); + injector.register(ChunkManager.class); + + // Signs + injector.register(AscendSign.class); + injector.register(CraftSign.class); + injector.register(CruiseSign.class); + injector.register(DescendSign.class); + injector.register(HelmSign.class); + injector.register(MoveSign.class); + injector.register(NameSign.class); + injector.register(PilotSign.class); + injector.register(RelativeMoveSign.class); + injector.register(ReleaseSign.class); + injector.register(RemoteSign.class); + injector.register(SpeedSign.class); + injector.register(SubcraftRotateSign.class); + injector.register(TeleportSign.class); + injector.register(ScuttleSign.class); + + injector.register(CraftPilotListener.class); + injector.register(CraftReleaseListener.class); var contactsManager = new ContactsManager(); contactsManager.runTaskTimerAsynchronously(this, 0, 20); @@ -241,7 +140,11 @@ public void onEnable() { getServer().getPluginManager().registerEvents(statusManager, this); getServer().getPluginManager().registerEvents(new StatusSign(), this); - logger.info("[V " + getDescription().getVersion() + "] has been enabled."); + // Lifecycle management + injector.register(WorkerServiceHost.class); + injector.getInstance(ServiceHost.class).startAll(); + + logger.info("[V %s] has been enabled.".formatted(getDescription().getVersion())); } @Override @@ -252,92 +155,19 @@ public void onLoad() { saveDefaultConfig(); } - private boolean initializeDatapack() { - File datapackDirectory = null; - for(var world : getServer().getWorlds()) { - datapackDirectory = new File(world.getWorldFolder(), "datapacks"); - if(datapackDirectory.exists()) - break; - } - if(datapackDirectory == null) { - logger.severe("Failed to initialize Movecraft data pack due to first time world initialization."); - return false; - } - if(!datapackDirectory.exists()) { - logger.info("Creating a datapack directory at " + datapackDirectory.getPath()); - if(!datapackDirectory.mkdir()) { - logger.severe("Failed to create datapack directory!"); - return false; - } - } - else if(new File(datapackDirectory, "movecraft-data.zip").exists()) { - logger.warning("Conflicting datapack already exists in " + datapackDirectory.getPath() + ". If you would like to regenerate the datapack, delete the existing one."); - return false; - } - if(!datapackDirectory.canWrite()) { - logger.warning("Missing permissions to write to world directory."); - return false; - } - - try(var stream = new FileOutputStream(new File(datapackDirectory, "movecraft-data.zip")); - var pack = getResource("movecraft-data.zip")) { - if(pack == null) { - logger.severe("No internal datapack found, report this."); - return false; - } - pack.transferTo(stream); - } - catch(IOException e) { - e.printStackTrace(); - return false; - } - logger.info("Saved default Movecraft datapack."); - - getServer().dispatchCommand(getServer().createCommandSender(response -> {}), "datapack list"); // list datapacks to trigger the server to check - for (Datapack datapack : getServer().getDatapackManager().getPacks()) { - if (!datapack.getName().equals("file/movecraft-data.zip")) - continue; - - if (!datapack.isEnabled()) { - datapack.setEnabled(true); - logger.info("Datapack enabled."); - } - break; - } - - if (!isDatapackEnabled()) { - logger.severe("Failed to automatically load movecraft datapack. Check if it exists."); - setEnabled(false); - return false; - } - return true; - } - - private boolean isDatapackEnabled() { - getServer().dispatchCommand(getServer().createCommandSender(response -> {}), "datapack list"); // list datapacks to trigger the server to check - for (Datapack datapack : getServer().getDatapackManager().getPacks()) { - if (!datapack.getName().equals("file/movecraft-data.zip")) - continue; - - return datapack.isEnabled(); - } - return false; - } - - public WorldHandler getWorldHandler(){ - return worldHandler; + return injector.getInstance(WorldHandler.class); } public SmoothTeleport getSmoothTeleport() { - return smoothTeleport; + return injector.getInstance(SmoothTeleport.class); } public AsyncManager getAsyncManager() { - return asyncManager; + return injector.getInstance(AsyncManager.class); } public @NotNull WreckManager getWreckManager(){ - return wreckManager; + return injector.getInstance(WreckManager.class); } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java index 7825b86e2..6070801a1 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java @@ -21,6 +21,7 @@ import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; +import net.countercraft.movecraft.lifecycle.Service; import net.countercraft.movecraft.async.rotation.RotationTask; import net.countercraft.movecraft.async.translation.TranslationTask; import net.countercraft.movecraft.craft.Craft; @@ -34,26 +35,36 @@ import net.kyori.adventure.text.Component; import org.bukkit.World; import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.WeakHashMap; +import java.util.*; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @Deprecated -public class AsyncManager extends BukkitRunnable { - private final Map ownershipMap = new HashMap<>(); - private final BlockingQueue finishedAlgorithms = new LinkedBlockingQueue<>(); - private final Set clearanceSet = new HashSet<>(); - private final Map cooldownCache = new WeakHashMap<>(); +public class AsyncManager extends BukkitRunnable implements Service { + private final Map ownershipMap; + private final BlockingQueue finishedAlgorithms; + private final Set clearanceSet; + private final Map cooldownCache; + private final @NotNull Plugin plugin; + private final @NotNull MapUpdateManager mapUpdateManager; + + public AsyncManager(@NotNull Plugin plugin, @NotNull MapUpdateManager mapUpdateManager) { + this.plugin = Objects.requireNonNull(plugin); + this.mapUpdateManager = mapUpdateManager; + ownershipMap = new HashMap<>(); + finishedAlgorithms = new LinkedBlockingQueue<>(); + clearanceSet = new HashSet<>(); + cooldownCache = new WeakHashMap<>(); + } - public AsyncManager() {} + @Override + public void start(){ + this.runTaskTimer(plugin, 0, 1); + } public void submitTask(AsyncTask task, Craft c) { if (c.isNotProcessing()) { @@ -120,14 +131,14 @@ private boolean processTranslation(@NotNull final TranslationTask task, @NotNull if (task.isCollisionExplosion()) { c.setHitBox(task.getNewHitBox()); c.setFluidLocations(task.getNewFluidList()); - MapUpdateManager.getInstance().scheduleUpdates(task.getUpdates()); + mapUpdateManager.scheduleUpdates(task.getUpdates()); CraftManager.getInstance().addReleaseTask(c); return true; } return false; } // The craft is clear to move, perform the block updates - MapUpdateManager.getInstance().scheduleUpdates(task.getUpdates()); + mapUpdateManager.scheduleUpdates(task.getUpdates()); c.setHitBox(task.getNewHitBox()); c.setFluidLocations(task.getNewFluidList()); @@ -153,7 +164,7 @@ private boolean processRotation(@NotNull final RotationTask task, @NotNull final } - MapUpdateManager.getInstance().scheduleUpdates(task.getUpdates()); + mapUpdateManager.scheduleUpdates(task.getUpdates()); c.setHitBox(task.getNewHitBox()); c.setFluidLocations(task.getNewFluidList()); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/craft/ChunkManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/craft/ChunkManager.java index 65d0497b9..1d8c3a2d9 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/craft/ChunkManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/craft/ChunkManager.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.craft; +import jakarta.inject.Inject; import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftChunk; import net.countercraft.movecraft.MovecraftLocation; @@ -19,7 +20,10 @@ @Deprecated public class ChunkManager implements Listener { - + + @Inject + public ChunkManager(){} + private static final Set chunks = new HashSet<>(); public static void addChunksToLoad(Iterable list) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/BlockListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/BlockListener.java index 0ce37d5ef..b4ed2427c 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/BlockListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/BlockListener.java @@ -17,6 +17,7 @@ package net.countercraft.movecraft.listener; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.Craft; @@ -52,6 +53,9 @@ import org.jetbrains.annotations.NotNull; public class BlockListener implements Listener { + @Inject + public BlockListener(){} + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onBlockBreak(@NotNull BlockBreakEvent e) { if (!Settings.ProtectPilotedCrafts) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java index 5901cdace..55fb4131d 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftPilotListener.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.listener; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.events.CraftPilotEvent; @@ -10,6 +11,8 @@ import org.jetbrains.annotations.NotNull; public class CraftPilotListener implements Listener { + @Inject + public CraftPilotListener(){} @EventHandler(ignoreCancelled = true) public void onCraftPilot(@NotNull CraftPilotEvent event) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftReleaseListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftReleaseListener.java index 26c933cdd..bcccacdca 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftReleaseListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/CraftReleaseListener.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.listener; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.events.CraftReleaseEvent; @@ -10,6 +11,8 @@ import org.jetbrains.annotations.NotNull; public class CraftReleaseListener implements Listener { + @Inject + public CraftReleaseListener(){} @EventHandler public void onDisassembly(@NotNull CraftReleaseEvent event) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/InteractListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/InteractListener.java index b12233858..332cf361a 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/InteractListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/InteractListener.java @@ -17,6 +17,7 @@ package net.countercraft.movecraft.listener; +import jakarta.inject.Inject; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.PlayerCraft; @@ -37,7 +38,12 @@ import java.util.WeakHashMap; public final class InteractListener implements Listener { - private final Map timeMap = new WeakHashMap<>(); + private final Map timeMap; + + @Inject + public InteractListener() { + timeMap = new WeakHashMap<>(); + } @EventHandler(priority = EventPriority.LOWEST) // LOWEST so that it runs before the other events public void onPlayerInteract(@NotNull PlayerInteractEvent e) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/listener/PlayerListener.java b/Movecraft/src/main/java/net/countercraft/movecraft/listener/PlayerListener.java index b9f829be1..efe47b202 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/listener/PlayerListener.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/listener/PlayerListener.java @@ -17,6 +17,7 @@ package net.countercraft.movecraft.listener; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.Craft; @@ -42,7 +43,12 @@ import java.util.WeakHashMap; public class PlayerListener implements Listener { - private final Map timeToReleaseAfter = new WeakHashMap<>(); + private final Map timeToReleaseAfter; + + @Inject + public PlayerListener() { + timeToReleaseAfter = new WeakHashMap<>(); + } private Set checkCraftBorders(Craft craft) { Set mergePoints = new HashSet<>(); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java b/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java index 1a324425b..4d4a601f9 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java @@ -19,8 +19,10 @@ import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.config.Settings; +import net.countercraft.movecraft.lifecycle.Service; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; +import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -34,10 +36,28 @@ import java.util.Properties; import java.util.logging.Level; -public class I18nSupport { +public class I18nSupport implements Service { private static Properties languageFile; + private final @NotNull Plugin plugin; - public static void init() { + public I18nSupport(@NotNull Plugin plugin){ + this.plugin = plugin; + } + + @Override + public void start() { + String[] localisations = {"en", "cz", "nl", "fr"}; + for (String locale : localisations) { + var file = new File("%s/localisation/movecraftlang_%s.properties".formatted(plugin.getDataFolder(), locale)); + if (!file.exists()) { + plugin.saveResource("localisation/movecraftlang_%s.properties".formatted(locale), false); + } + } + + init(); + } + + private static void init() { languageFile = new Properties(); File localisationDirectory = new File(Movecraft.getInstance().getDataFolder().getAbsolutePath() + "/localisation"); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java index a590212a3..fb813df67 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java @@ -18,8 +18,10 @@ package net.countercraft.movecraft.mapUpdater; import net.countercraft.movecraft.Movecraft; +import net.countercraft.movecraft.lifecycle.Service; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.mapUpdater.update.UpdateCommand; +import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; @@ -30,19 +32,18 @@ import java.util.logging.Logger; @Deprecated -public class MapUpdateManager extends BukkitRunnable { +public class MapUpdateManager extends BukkitRunnable implements Service { + private final Queue updates; + private final @NotNull Plugin plugin; - private final Queue updates = new ConcurrentLinkedQueue<>(); -// private final Queue updates = new LinkedBlockingQueue<>(); - //private PriorityQueue updateQueue = new PriorityQueue<>(); - - //@Deprecated - //public HashMap blockUpdatesPerCraft = new HashMap<>(); - - private MapUpdateManager() { } + public MapUpdateManager(@NotNull Plugin plugin) { + this.plugin = plugin; + this.updates = new ConcurrentLinkedQueue<>(); + } - public static MapUpdateManager getInstance() { - return MapUpdateManagerHolder.INSTANCE; + @Override + public void start(){ + this.runTaskTimer(plugin, 0, 1); } public void run() { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java index e59193e3b..4e4afad6f 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; @@ -20,6 +21,8 @@ import org.jetbrains.annotations.NotNull; public class AscendSign implements Listener { + @Inject + public AscendSign(){} @EventHandler public void onCraftDetect(CraftDetectEvent event){ diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java index ec5a977c5..2d82d4671 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; @@ -37,7 +38,12 @@ import java.util.Set; public final class CraftSign implements Listener { - private final Set piloting = new HashSet<>(); + private final Set piloting; + + @Inject + public CraftSign() { + piloting = new HashSet<>(); + } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignChange(@NotNull SignChangeEvent event) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java index fd4efec28..a779b9c57 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.config.Settings; @@ -24,6 +25,8 @@ import org.jetbrains.annotations.NotNull; public final class CruiseSign implements Listener { + @Inject + public CruiseSign(){} @EventHandler public void onCraftDetect(@NotNull CraftDetectEvent event) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java index 2d638689b..ec66576ce 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; @@ -19,6 +20,8 @@ import org.jetbrains.annotations.NotNull; public final class DescendSign implements Listener{ + @Inject + public DescendSign(){} @EventHandler public void onCraftDetect(CraftDetectEvent event){ diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java index 74eef63fa..d62987a8d 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftRotation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; @@ -18,6 +19,8 @@ import org.jetbrains.annotations.NotNull; public final class HelmSign implements Listener { + @Inject + public HelmSign(){} @EventHandler public void onSignChange(SignChangeEvent event){ diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java index 37c0f00d4..7e83ca1f5 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.localisation.I18nSupport; @@ -16,6 +17,9 @@ public final class MoveSign implements Listener{ private static final String HEADER = "Move:"; + @Inject + public MoveSign(){} + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/NameSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/NameSign.java index 787f3711c..25c21d417 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/NameSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/NameSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.Craft; @@ -25,6 +26,10 @@ public final class NameSign implements Listener { private static final String HEADER = "Name:"; + + @Inject + public NameSign(){} + @EventHandler public void onCraftDetect(@NotNull CraftDetectEvent event) { Craft c = event.getCraft(); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/PilotSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/PilotSign.java index 9ed6cd230..bb124f1fb 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/PilotSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/PilotSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import org.bukkit.ChatColor; import org.bukkit.block.Block; import org.bukkit.block.Sign; @@ -13,6 +14,10 @@ public final class PilotSign implements Listener { private static final String HEADER = "Pilot:"; + + @Inject + public PilotSign(){} + @EventHandler public final void onSignChange(SignChangeEvent event){ if (event.getLine(0).equalsIgnoreCase(HEADER)) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java index 2e187d3fb..a9f23a8ca 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.localisation.I18nSupport; @@ -16,6 +17,9 @@ public final class RelativeMoveSign implements Listener{ private static final String HEADER = "RMove:"; + @Inject + public RelativeMoveSign(){} + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java index 91e9aa544..50f978d04 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.events.CraftReleaseEvent; @@ -16,6 +17,9 @@ public final class ReleaseSign implements Listener{ private static final String HEADER = "Release"; + @Inject + public ReleaseSign(){} + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java index f9bad8835..79eb9d5c5 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.Craft; @@ -28,6 +29,9 @@ public final class RemoteSign implements Listener{ private static final String HEADER = "Remote Sign"; + @Inject + public RemoteSign(){} + @EventHandler public final void onSignChange(SignChangeEvent event) { if (!event.getLine(0).equalsIgnoreCase(HEADER)) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java index 39f9fd215..e8e33ee7f 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.SinkingCraft; @@ -23,9 +24,11 @@ import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; public class ScuttleSign implements Listener { - private static final String HEADER = "Scuttle"; + @Inject + public ScuttleSign(){} + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { if (event.getAction() != Action.RIGHT_CLICK_BLOCK) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java index 1d77fee88..4d3cd5ed1 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; @@ -21,7 +22,10 @@ import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -public final class SpeedSign implements Listener{ +public final class SpeedSign implements Listener { + @Inject + public SpeedSign(){} + @EventHandler public void onCraftDetect(CraftDetectEvent event){ World world = event.getCraft().getWorld(); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java index c8469fa38..c2ffc3e28 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.MovecraftRotation; @@ -30,7 +31,12 @@ public final class SubcraftRotateSign implements Listener { private static final String HEADER = "Subcraft Rotate"; - private final Set rotating = new HashSet<>(); + private final Set rotating; + + @Inject + public SubcraftRotateSign() { + rotating = new HashSet<>(); + } @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java index 4f41f6db5..d6dc2a182 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.sign; +import jakarta.inject.Inject; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; @@ -19,6 +20,9 @@ public final class TeleportSign implements Listener { private static final String HEADER = "Teleport:"; + @Inject + public TeleportSign(){} + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/support/SmoothTeleportFactory.java b/Movecraft/src/main/java/net/countercraft/movecraft/support/SmoothTeleportFactory.java new file mode 100644 index 000000000..4ece5b3f2 --- /dev/null +++ b/Movecraft/src/main/java/net/countercraft/movecraft/support/SmoothTeleportFactory.java @@ -0,0 +1,42 @@ +package net.countercraft.movecraft.support; + +import jakarta.inject.Provider; +import net.countercraft.movecraft.SmoothTeleport; +import net.countercraft.movecraft.config.Settings; +import net.countercraft.movecraft.util.BukkitTeleport; +import org.jetbrains.annotations.NotNull; + +import java.util.logging.Logger; + +public class SmoothTeleportFactory implements Provider { + private final @NotNull Logger logger; + private final @NotNull VersionInfo versionInfo; + + public SmoothTeleportFactory(@NotNull Logger logger, @NotNull VersionInfo versionInfo) { + this.logger = logger; + this.versionInfo = versionInfo; + } + + public SmoothTeleport get(){ + try { + // Try to set up the smooth teleport handler + final Class smoothTeleportClazz = Class.forName("net.countercraft.movecraft.support." + versionInfo.getPackageName() + ".ISmoothTeleport"); + if (SmoothTeleport.class.isAssignableFrom(smoothTeleportClazz)) { + return (SmoothTeleport) smoothTeleportClazz.getConstructor().newInstance(); + } + + // Fall back to bukkit teleportation + logger.warning("Did not find smooth teleport, falling back to bukkit teleportation provider."); + + return new BukkitTeleport(); + } catch (final ReflectiveOperationException e) { + // Fall back to bukkit teleportation + logger.warning("Falling back to bukkit teleportation provider."); + if (Settings.Debug) { + e.printStackTrace(); + } + + return new BukkitTeleport(); + } + } +} diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/support/WorldHandlerFactory.java b/Movecraft/src/main/java/net/countercraft/movecraft/support/WorldHandlerFactory.java new file mode 100644 index 000000000..606bcf6ec --- /dev/null +++ b/Movecraft/src/main/java/net/countercraft/movecraft/support/WorldHandlerFactory.java @@ -0,0 +1,41 @@ +package net.countercraft.movecraft.support; + +import jakarta.inject.Provider; +import net.countercraft.movecraft.WorldHandler; +import net.countercraft.movecraft.config.Settings; +import org.jetbrains.annotations.NotNull; + +import java.util.logging.Logger; + +public class WorldHandlerFactory implements Provider { + private final @NotNull Logger logger; + private final @NotNull VersionInfo versionInfo; + + public WorldHandlerFactory(@NotNull Logger logger, @NotNull VersionInfo versionInfo) { + this.logger = logger; + this.versionInfo = versionInfo; + } + + @Override + public WorldHandler get() { + try { + final Class worldHandlerClazz = Class.forName("net.countercraft.movecraft.compat." + versionInfo.getPackageName() + ".IWorldHandler"); + // Check if we have a NMSHandler class at that location. + if (WorldHandler.class.isAssignableFrom(worldHandlerClazz)) { // Make sure it actually implements NMS + return (WorldHandler) worldHandlerClazz.getConstructor().newInstance(); // Set our handler + } + } catch (final Exception e) { + if (!Settings.DisableNMSCompatibilityCheck) { + throw new IllegalStateException("Could not find support for version %s.".formatted(versionInfo.version())); + } + } + + // Server owner claims to know what they are doing, warn them of the possible consequences + logger.severe(""" + WARNING! + Running Movecraft on an incompatible version can corrupt your world and break EVERYTHING! + We provide no support for any issues."""); + + return null; + } +} diff --git a/api/build.gradle.kts b/api/build.gradle.kts index 9c72f6524..49f01cd96 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -16,6 +16,7 @@ dependencies { api(libs.it.unimi.dsi.fastutil) api(libs.net.kyori.adventure.api) api(libs.net.kyori.adventure.platform.bukkit) + api(libs.org.int4j.dirk.dirk.di) testImplementation(libs.org.junit.jupiter.junit.jupiter.api) testImplementation(libs.junit.junit) testImplementation(libs.org.hamcrest.hamcrest.library) diff --git a/api/src/main/java/net/countercraft/movecraft/WorldHandler.java b/api/src/main/java/net/countercraft/movecraft/WorldHandler.java index 3b72e569f..d4e629cc3 100644 --- a/api/src/main/java/net/countercraft/movecraft/WorldHandler.java +++ b/api/src/main/java/net/countercraft/movecraft/WorldHandler.java @@ -20,6 +20,7 @@ public abstract class WorldHandler { @Deprecated(forRemoval = true) public abstract void setAccessLocation(@NotNull InventoryView inventoryView, @NotNull Location location); // Not needed for 1.20+, remove when dropping support for 1.18.2 + @Deprecated public static @NotNull String getPackageName(@NotNull String minecraftVersion) { String[] parts = minecraftVersion.split("\\."); if (parts.length < 2) diff --git a/api/src/main/java/net/countercraft/movecraft/config/DataPackService.java b/api/src/main/java/net/countercraft/movecraft/config/DataPackService.java new file mode 100644 index 000000000..19649d12a --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/config/DataPackService.java @@ -0,0 +1,121 @@ +package net.countercraft.movecraft.config; + +import io.papermc.paper.datapack.Datapack; +import net.countercraft.movecraft.lifecycle.Service; +import org.bukkit.Server; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.logging.Logger; + +public class DataPackService implements Service { + private final @NotNull Plugin plugin; + private final @NotNull Logger logger; + private boolean isInitialized; + + public DataPackService(@NotNull Plugin plugin, @NotNull Logger logger) { + this.plugin = plugin; + this.logger = logger; + } + + @Override + public void start() { + isInitialized = isDatapackEnabled() || initializeDatapack(); + } + + public boolean isDatapackInitialized(){ + return isInitialized; + } + + private boolean initializeDatapack() { + File datapackDirectory = null; + Server server = plugin.getServer(); + + for(var world : server.getWorlds()) { + datapackDirectory = new File(world.getWorldFolder(), "datapacks"); + if(datapackDirectory.exists()) { + break; + } + } + + if(datapackDirectory == null) { + logger.severe("Failed to initialize Movecraft data pack due to first time world initialization."); + + return false; + } + + if(!datapackDirectory.exists()) { + logger.info("Creating a datapack directory at " + datapackDirectory.getPath()); + if(!datapackDirectory.mkdir()) { + logger.severe("Failed to create datapack directory!"); + + return false; + } + } else if(new File(datapackDirectory, "movecraft-data.zip").exists()) { + logger.warning("Conflicting datapack already exists in " + datapackDirectory.getPath() + ". If you would like to regenerate the datapack, delete the existing one."); + + return false; + } + + if(!datapackDirectory.canWrite()) { + logger.warning("Missing permissions to write to world directory."); + + return false; + } + + try(var stream = new FileOutputStream(new File(datapackDirectory, "movecraft-data.zip")); + var pack = plugin.getResource("movecraft-data.zip")) { + if(pack == null) { + logger.severe("No internal datapack found, report this."); + + return false; + } + + pack.transferTo(stream); + } + catch(IOException e) { + e.printStackTrace(); + + return false; + } + + logger.info("Saved default Movecraft datapack."); + server.dispatchCommand(server.createCommandSender(response -> {}), "datapack list"); // list datapacks to trigger the server to check + for (Datapack datapack : server.getDatapackManager().getPacks()) { + if (!datapack.getName().equals("file/movecraft-data.zip")) { + continue; + } + + if (!datapack.isEnabled()) { + datapack.setEnabled(true); + logger.info("Datapack enabled."); + } + + break; + } + + if (!isDatapackEnabled()) { + throw new IllegalStateException("Failed to automatically load movecraft datapack. Check if it exists."); + } + + return true; + } + + private boolean isDatapackEnabled() { + Server server = plugin.getServer(); + server.dispatchCommand(server.createCommandSender(response -> {}), "datapack list"); // list datapacks to trigger the server to check + for (Datapack datapack : server.getDatapackManager().getPacks()) { + if (!datapack.getName().equals("file/movecraft-data.zip")) { + continue; + } + + return datapack.isEnabled(); + } + + return false; + } + +} diff --git a/api/src/main/java/net/countercraft/movecraft/config/Settings.java b/api/src/main/java/net/countercraft/movecraft/config/Settings.java index 122d0b5a5..d69a94d76 100644 --- a/api/src/main/java/net/countercraft/movecraft/config/Settings.java +++ b/api/src/main/java/net/countercraft/movecraft/config/Settings.java @@ -23,6 +23,7 @@ import java.util.HashSet; import java.util.Map; +// TODO: De-static public class Settings { public static boolean IGNORE_RESET = false; public static boolean Debug = false; diff --git a/api/src/main/java/net/countercraft/movecraft/config/SettingsService.java b/api/src/main/java/net/countercraft/movecraft/config/SettingsService.java new file mode 100644 index 000000000..c1f9a6470 --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/config/SettingsService.java @@ -0,0 +1,79 @@ +package net.countercraft.movecraft.config; + +import jakarta.inject.Inject; +import jakarta.inject.Provider; +import net.countercraft.movecraft.lifecycle.Service; +import net.countercraft.movecraft.util.Tags; +import org.bukkit.Material; +import org.bukkit.configuration.Configuration; +import org.int4.dirk.api.Injector; +import org.int4.dirk.di.Injectors; +import org.jetbrains.annotations.NotNull; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.logging.Logger; + +public class SettingsService implements Service { + private final @NotNull Configuration configuration; + private final @NotNull Logger logger; + + @Inject + public SettingsService(@NotNull Configuration configuration, @NotNull Logger logger) { + this.configuration = configuration; + this.logger = logger; + } + + @Override + public void start() { + Settings.LOCALE = configuration.getString("Locale"); + Settings.Debug = configuration.getBoolean("Debug", false); + Settings.DisableNMSCompatibilityCheck = configuration.getBoolean("IReallyKnowWhatIAmDoing", false); + Settings.DisableSpillProtection = configuration.getBoolean("DisableSpillProtection", false); + Settings.DisableIceForm = configuration.getBoolean("DisableIceForm", true); + Settings.ReleaseOnDeath = configuration.getBoolean("ReleaseOnDeath", false); + Settings.SinkCheckTicks = configuration.getDouble("SinkCheckTicks", 100.0); + Settings.ManOverboardTimeout = configuration.getInt("ManOverboardTimeout", 30); + Settings.ManOverboardDistSquared = Math.pow(configuration.getDouble("ManOverboardDistance", 1000), 2); + Settings.SilhouetteViewDistance = configuration.getInt("SilhouetteViewDistance", 200); + Settings.SilhouetteBlockCount = configuration.getInt("SilhouetteBlockCount", 20); + Settings.ProtectPilotedCrafts = configuration.getBoolean("ProtectPilotedCrafts", false); + Settings.MaxRemoteSigns = configuration.getInt("MaxRemoteSigns", -1); + Settings.CraftsUseNetherPortals = configuration.getBoolean("CraftsUseNetherPortals", false); + Settings.RequireCreatePerm = configuration.getBoolean("RequireCreatePerm", false); + Settings.RequireNamePerm = configuration.getBoolean("RequireNamePerm", true); + Settings.FadeWrecksAfter = configuration.getInt("FadeWrecksAfter", 0); + Settings.FadeTickCooldown = configuration.getInt("FadeTickCooldown", 20); + Settings.FadePercentageOfWreckPerCycle = configuration.getDouble("FadePercentageOfWreckPerCycle", 10.0); + + // if the PilotTool is specified in the config.yml file, use it + String pilotTool = configuration.getString("PilotTool"); + if (pilotTool != null) { + Material material = Material.getMaterial(pilotTool); + if (material != null) { + logger.info("Recognized PilotTool setting of: " + pilotTool); + Settings.PilotTool = material; + } + else { + logger.info("No PilotTool setting, using default of stick"); + } + } + else { + logger.info("No PilotTool setting, using default of stick"); + } + + if (configuration.contains("ExtraFadeTimePerBlock")) { + Map temp = configuration.getConfigurationSection("ExtraFadeTimePerBlock").getValues(false); + for (String str : temp.keySet()) { + Set materials = Tags.parseMaterials(str); + for (Material m : materials) { + Settings.ExtraFadeTimePerBlock.put(m, (Integer) temp.get(str)); + } + } + } + + Settings.ForbiddenRemoteSigns = new HashSet<>(configuration.getStringList("ForbiddenRemoteSigns")); + } + +} diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleService.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleService.java new file mode 100644 index 000000000..9d53931ec --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleService.java @@ -0,0 +1,24 @@ +package net.countercraft.movecraft.lifecycle; + +import jakarta.inject.Inject; +import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class ListenerLifecycleService implements Service { + private final @NotNull List listeners; + private final @NotNull Plugin plugin; + + @Inject + public ListenerLifecycleService(@NotNull List listeners, @NotNull Plugin plugin){ + this.listeners = listeners; + this.plugin = plugin; + } + + @Override + public void start() { + listeners.forEach(listener -> plugin.getServer().getPluginManager().registerEvents(listener, plugin)); + } +} diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/Service.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/Service.java new file mode 100644 index 000000000..543678977 --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/Service.java @@ -0,0 +1,6 @@ +package net.countercraft.movecraft.lifecycle; + +public interface Service { + default void start(){} + default void stop(){} +} diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/ServiceHost.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/ServiceHost.java new file mode 100644 index 000000000..03973f01b --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/ServiceHost.java @@ -0,0 +1,23 @@ +package net.countercraft.movecraft.lifecycle; + +import jakarta.inject.Inject; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class ServiceHost { + private final @NotNull List services; + + @Inject + public ServiceHost(@NotNull List services) { + this.services = services; + } + + public void startAll(){ + services.forEach(Service::start); + } + + public void stopAll(){ + services.forEach(Service::stop); + } +} diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/Worker.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/Worker.java new file mode 100644 index 000000000..a1b89cba5 --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/Worker.java @@ -0,0 +1,13 @@ +package net.countercraft.movecraft.lifecycle; + +public interface Worker { + default int getDelay(){ + return 1; + } + + boolean isAsync(); + + int getPeriod(); + + void run(); +} diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerServiceHost.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerServiceHost.java new file mode 100644 index 000000000..b8c1ee5d1 --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerServiceHost.java @@ -0,0 +1,38 @@ +package net.countercraft.movecraft.lifecycle; + +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitTask; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class WorkerServiceHost implements Service { + private final @NotNull Plugin plugin; + private final @NotNull List workers; + private @NotNull List tasks; + + public WorkerServiceHost(@NotNull Plugin plugin, @NotNull List workers) { + this.plugin = plugin; + this.workers = workers; + tasks = List.of(); + } + + @Override + public void start() { + tasks = workers.stream().map(worker -> { + if(worker.isAsync()){ + return plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, worker::run, worker.getDelay(), worker.getPeriod()); + } else { + return plugin.getServer().getScheduler().runTaskTimer(plugin, worker::run, worker.getDelay(), worker.getPeriod()); + } + }).collect(Collectors.toList()); + } + + @Override + public void stop() { + var oldTasks = tasks; + tasks = List.of(); + oldTasks.forEach(BukkitTask::cancel); + } +} diff --git a/api/src/main/java/net/countercraft/movecraft/processing/WorldManager.java b/api/src/main/java/net/countercraft/movecraft/processing/WorldManager.java index 74378240d..894b20171 100644 --- a/api/src/main/java/net/countercraft/movecraft/processing/WorldManager.java +++ b/api/src/main/java/net/countercraft/movecraft/processing/WorldManager.java @@ -1,8 +1,13 @@ package net.countercraft.movecraft.processing; +import jakarta.inject.Inject; +import net.countercraft.movecraft.lifecycle.Service; +import net.countercraft.movecraft.lifecycle.Worker; import net.countercraft.movecraft.processing.effects.Effect; import net.countercraft.movecraft.util.CompletableFutureTask; import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -20,9 +25,13 @@ /** * */ -public final class WorldManager implements Executor { - +public final class WorldManager implements Executor, Worker { + /** + * @deprecated Prefer dependency injection over static accessors + */ + @Deprecated public static final WorldManager INSTANCE = new WorldManager(); + private static final Runnable POISON = new Runnable() { @Override public void run() {/* No-op */} @@ -39,6 +48,16 @@ public String toString(){ private WorldManager(){} + @Override + public boolean isAsync() { + return false; + } + + @Override + public int getPeriod() { + return 1; + } + public void run() { if(!Bukkit.isPrimaryThread()){ throw new RuntimeException("WorldManager must be executed on the main thread."); diff --git a/api/src/main/java/net/countercraft/movecraft/support/VersionInfo.java b/api/src/main/java/net/countercraft/movecraft/support/VersionInfo.java new file mode 100644 index 000000000..a03412dea --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/support/VersionInfo.java @@ -0,0 +1,13 @@ +package net.countercraft.movecraft.support; + +import org.jetbrains.annotations.NotNull; + +public record VersionInfo(String version) { + public @NotNull String getPackageName() { + String[] parts = version.split("\\."); + if (parts.length < 2) + throw new IllegalArgumentException(); + + return "v1_" + parts[1]; + } +} diff --git a/api/src/main/java/net/countercraft/movecraft/support/VersionProvider.java b/api/src/main/java/net/countercraft/movecraft/support/VersionProvider.java new file mode 100644 index 000000000..6a820773c --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/support/VersionProvider.java @@ -0,0 +1,24 @@ +package net.countercraft.movecraft.support; + +import jakarta.inject.Provider; +import net.countercraft.movecraft.WorldHandler; +import net.countercraft.movecraft.config.Settings; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +import java.util.logging.Logger; + +public class VersionProvider implements Provider { + private final @NotNull Plugin plugin; + + public VersionProvider(@NotNull Plugin plugin) { + this.plugin = plugin; + } + + @Override + public VersionInfo get() { + String minecraftVersion = plugin.getServer().getMinecraftVersion(); + + return new VersionInfo(minecraftVersion); + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8ab52af61..541dfbd79 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,6 +8,7 @@ junit-junit = "4.13.2" net-kyori-adventure-api = "4.17.0" net-kyori-adventure-platform-bukkit = "4.3.2" org-hamcrest-hamcrest-library = "1.3" +org-int4j-dirk-dirk-di = "1.0.0-beta1" org-jetbrains-annotations = "24.1.0" org-junit-jupiter-junit-jupiter-api = "5.10.2" org-openjdk-jmh-jmh-core = "1.37" @@ -22,6 +23,7 @@ junit-junit = { module = "junit:junit", version.ref = "junit-junit" } net-kyori-adventure-api = { module = "net.kyori:adventure-api", version.ref = "net-kyori-adventure-api" } net-kyori-adventure-platform-bukkit = { module = "net.kyori:adventure-platform-bukkit", version.ref = "net-kyori-adventure-platform-bukkit" } org-hamcrest-hamcrest-library = { module = "org.hamcrest:hamcrest-library", version.ref = "org-hamcrest-hamcrest-library" } +org-int4j-dirk-dirk-di = { module = "org.int4.dirk:dirk-di", version.ref = "org-int4j-dirk-dirk-di" } org-jetbrains-annotations = { module = "org.jetbrains:annotations", version.ref = "org-jetbrains-annotations" } org-junit-jupiter-junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "org-junit-jupiter-junit-jupiter-api" } org-openjdk-jmh-jmh-core = { module = "org.openjdk.jmh:jmh-core", version.ref = "org-openjdk-jmh-jmh-core" } From 878884e09778060afba4ec9d36f44376577a7bfe Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sat, 31 Aug 2024 22:58:40 -0700 Subject: [PATCH 02/18] Use worker in more applicabl areas --- .../movecraft/async/AsyncManager.java | 20 ++++++++++++------ .../mapUpdater/MapUpdateManager.java | 21 +++++++++++-------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java index 6070801a1..9e5f81e44 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java @@ -18,6 +18,7 @@ package net.countercraft.movecraft.async; import com.google.common.collect.Lists; +import jakarta.inject.Inject; import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; @@ -31,6 +32,7 @@ import net.countercraft.movecraft.craft.SinkingCraft; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.events.CraftReleaseEvent; +import net.countercraft.movecraft.lifecycle.Worker; import net.countercraft.movecraft.mapUpdater.MapUpdateManager; import net.kyori.adventure.text.Component; import org.bukkit.World; @@ -44,7 +46,7 @@ import java.util.concurrent.LinkedBlockingQueue; @Deprecated -public class AsyncManager extends BukkitRunnable implements Service { +public class AsyncManager implements Worker { private final Map ownershipMap; private final BlockingQueue finishedAlgorithms; private final Set clearanceSet; @@ -52,6 +54,7 @@ public class AsyncManager extends BukkitRunnable implements Service { private final @NotNull Plugin plugin; private final @NotNull MapUpdateManager mapUpdateManager; + @Inject public AsyncManager(@NotNull Plugin plugin, @NotNull MapUpdateManager mapUpdateManager) { this.plugin = Objects.requireNonNull(plugin); this.mapUpdateManager = mapUpdateManager; @@ -61,11 +64,6 @@ public AsyncManager(@NotNull Plugin plugin, @NotNull MapUpdateManager mapUpdateM cooldownCache = new WeakHashMap<>(); } - @Override - public void start(){ - this.runTaskTimer(plugin, 0, 1); - } - public void submitTask(AsyncTask task, Craft c) { if (c.isNotProcessing()) { c.setProcessing(true); @@ -323,6 +321,16 @@ private void processSinking() { } } + @Override + public boolean isAsync() { + return false; + } + + @Override + public int getPeriod() { + return 1; + } + public void run() { clearAll(); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java index fb813df67..cdaeda3df 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java @@ -17,12 +17,11 @@ package net.countercraft.movecraft.mapUpdater; +import jakarta.inject.Inject; import net.countercraft.movecraft.Movecraft; -import net.countercraft.movecraft.lifecycle.Service; import net.countercraft.movecraft.config.Settings; +import net.countercraft.movecraft.lifecycle.Worker; import net.countercraft.movecraft.mapUpdater.update.UpdateCommand; -import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; import java.util.Collection; @@ -32,18 +31,22 @@ import java.util.logging.Logger; @Deprecated -public class MapUpdateManager extends BukkitRunnable implements Service { +public class MapUpdateManager implements Worker { private final Queue updates; - private final @NotNull Plugin plugin; - public MapUpdateManager(@NotNull Plugin plugin) { - this.plugin = plugin; + @Inject + public MapUpdateManager() { this.updates = new ConcurrentLinkedQueue<>(); } @Override - public void start(){ - this.runTaskTimer(plugin, 0, 1); + public boolean isAsync() { + return false; + } + + @Override + public int getPeriod() { + return 1; } public void run() { From a96568832f97e043b1c235f8c6b474ca44762fc6 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sat, 31 Aug 2024 23:04:25 -0700 Subject: [PATCH 03/18] Add listener service --- .../src/main/java/net/countercraft/movecraft/Movecraft.java | 2 ++ .../countercraft/movecraft/mapUpdater/MapUpdateManager.java | 5 ----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 3d165288f..f81befd8d 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -30,6 +30,7 @@ import net.countercraft.movecraft.features.fading.WreckManager; import net.countercraft.movecraft.features.status.StatusManager; import net.countercraft.movecraft.features.status.StatusSign; +import net.countercraft.movecraft.lifecycle.ListenerLifecycleService; import net.countercraft.movecraft.lifecycle.ServiceHost; import net.countercraft.movecraft.lifecycle.WorkerServiceHost; import net.countercraft.movecraft.listener.*; @@ -142,6 +143,7 @@ public void onEnable() { // Lifecycle management injector.register(WorkerServiceHost.class); + injector.register(ListenerLifecycleService.class); injector.getInstance(ServiceHost.class).startAll(); logger.info("[V %s] has been enabled.".formatted(getDescription().getVersion())); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java index cdaeda3df..d12a22bc6 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/mapUpdater/MapUpdateManager.java @@ -100,9 +100,4 @@ public void scheduleUpdates(@NotNull UpdateCommand... updates){ public void scheduleUpdates(@NotNull Collection updates){ this.updates.addAll(updates); } - - private static class MapUpdateManagerHolder { - private static final MapUpdateManager INSTANCE = new MapUpdateManager(); - } - } From fb80cc24381a5f257cbf0aab483948f50b007321 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sat, 31 Aug 2024 23:11:31 -0700 Subject: [PATCH 04/18] DI for CraftManager --- .../net/countercraft/movecraft/Movecraft.java | 7 +++--- .../movecraft/craft/CraftManager.java | 24 ++++++++++++------- .../movecraft/config/DataPackService.java | 1 + 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index f81befd8d..05a1d6c4e 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -23,6 +23,7 @@ import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.config.SettingsService; import net.countercraft.movecraft.craft.ChunkManager; +import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.features.contacts.ContactsCommand; import net.countercraft.movecraft.features.contacts.ContactsManager; @@ -89,9 +90,8 @@ public void onEnable() { return; } - // TODO: DI for CraftManager injector.register(DataPackService.class); - CraftManager.initialize(injector.getInstance(DataPackService.class).isDatapackInitialized()); + injector.register(CraftManager.class); //TODO: migrate to aikar or brigadier commands, left in place for now getCommand("movecraft").setExecutor(new MovecraftCommand()); @@ -144,8 +144,9 @@ public void onEnable() { // Lifecycle management injector.register(WorkerServiceHost.class); injector.register(ListenerLifecycleService.class); - injector.getInstance(ServiceHost.class).startAll(); + // Startup + injector.getInstance(ServiceHost.class).startAll(); logger.info("[V %s] has been enabled.".formatted(getDescription().getVersion())); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java index 1db3db008..176171883 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java @@ -19,11 +19,13 @@ import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; +import net.countercraft.movecraft.config.DataPackService; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.events.CraftReleaseEvent; import net.countercraft.movecraft.events.CraftSinkEvent; import net.countercraft.movecraft.events.TypesReloadedEvent; import net.countercraft.movecraft.exception.NonCancellableReleaseException; +import net.countercraft.movecraft.lifecycle.Service; import net.countercraft.movecraft.localisation.I18nSupport; import net.countercraft.movecraft.processing.CachedMovecraftWorld; import net.countercraft.movecraft.processing.MovecraftWorld; @@ -59,19 +61,17 @@ import static net.countercraft.movecraft.util.ChatUtils.ERROR_PREFIX; -public class CraftManager implements Iterable{ +public class CraftManager implements Iterable, Service { private static CraftManager instance; + /** + * @deprecated Prefer DI + */ + @Deprecated public static CraftManager getInstance() { return instance; } - public static void initialize(boolean loadCraftTypes) { - instance = new CraftManager(loadCraftTypes); - } - - - /** * Set of all crafts on the server, weakly ordered by their hashcode. * Note: Crafts are added in detection via the addCraft method, and removed in the removeCraft method. @@ -91,13 +91,19 @@ public static void initialize(boolean loadCraftTypes) { @NotNull private Set craftTypes; - private CraftManager(boolean loadCraftTypes) { - if(loadCraftTypes) + private CraftManager(@NotNull DataPackService dataPackService) { + if(dataPackService.isDatapackInitialized()) craftTypes = loadCraftTypes(); else craftTypes = new HashSet<>(); } + + @Override + public void start() { + instance = this; + } + @NotNull private Set loadCraftTypes() { File craftsFile = new File(Movecraft.getInstance().getDataFolder().getAbsolutePath() + "/types"); diff --git a/api/src/main/java/net/countercraft/movecraft/config/DataPackService.java b/api/src/main/java/net/countercraft/movecraft/config/DataPackService.java index 19649d12a..979d4b041 100644 --- a/api/src/main/java/net/countercraft/movecraft/config/DataPackService.java +++ b/api/src/main/java/net/countercraft/movecraft/config/DataPackService.java @@ -27,6 +27,7 @@ public void start() { } public boolean isDatapackInitialized(){ + // TODO: Async return isInitialized; } From 1850a267cebce7f1f8583cbe4f9255189bcdbc1d Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sat, 31 Aug 2024 23:17:26 -0700 Subject: [PATCH 05/18] Status and contacts --- .../net/countercraft/movecraft/Movecraft.java | 12 ++++-------- .../features/contacts/ContactsManager.java | 17 ++++++++++++++++- .../features/contacts/ContactsSign.java | 4 ++++ .../features/status/StatusManager.java | 17 ++++++++++++++++- .../movecraft/features/status/StatusSign.java | 4 ++++ 5 files changed, 44 insertions(+), 10 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 05a1d6c4e..747529c7d 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -130,16 +130,12 @@ public void onEnable() { injector.register(CraftPilotListener.class); injector.register(CraftReleaseListener.class); - var contactsManager = new ContactsManager(); - contactsManager.runTaskTimerAsynchronously(this, 0, 20); - getServer().getPluginManager().registerEvents(contactsManager, this); - getServer().getPluginManager().registerEvents(new ContactsSign(), this); + injector.register(ContactsManager.class); + injector.register(ContactsSign.class); getCommand("contacts").setExecutor(new ContactsCommand()); - var statusManager = new StatusManager(); - statusManager.runTaskTimerAsynchronously(this, 0, 1); - getServer().getPluginManager().registerEvents(statusManager, this); - getServer().getPluginManager().registerEvents(new StatusSign(), this); + injector.register(StatusManager.class); + injector.register(StatusSign.class); // Lifecycle management injector.register(WorkerServiceHost.class); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java index 2647e6f00..ce80ed4c0 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.features.contacts; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.*; import net.countercraft.movecraft.craft.datatag.CraftDataTagContainer; @@ -10,6 +11,7 @@ import net.countercraft.movecraft.exception.EmptyHitBoxException; import net.countercraft.movecraft.features.contacts.events.LostContactEvent; import net.countercraft.movecraft.features.contacts.events.NewContactEvent; +import net.countercraft.movecraft.lifecycle.Worker; import net.countercraft.movecraft.localisation.I18nSupport; import net.kyori.adventure.sound.Sound; import net.kyori.adventure.text.Component; @@ -27,9 +29,22 @@ import java.util.*; -public class ContactsManager extends BukkitRunnable implements Listener { +public class ContactsManager implements Listener, Worker { private static final CraftDataTagKey> RECENT_CONTACTS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "recent-contacts"), craft -> new WeakHashMap<>()); + @Inject + public ContactsManager(){} + + @Override + public boolean isAsync() { + return true; + } + + @Override + public int getPeriod() { + return 20; + } + @Override public void run() { runContacts(); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsSign.java index 83f52d845..e565b20ef 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.features.contacts; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.type.CraftType; @@ -21,6 +22,9 @@ public class ContactsSign implements Listener { private static final String HEADER = "Contacts:"; + @Inject + public ContactsSign(){} + @EventHandler public void onCraftDetect(@NotNull CraftDetectEvent event) { World world = event.getCraft().getWorld(); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusManager.java index 5a9068f01..4da745ab1 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusManager.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.features.status; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.Craft; @@ -10,6 +11,7 @@ import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.craft.type.RequiredBlockEntry; import net.countercraft.movecraft.features.status.events.CraftStatusUpdateEvent; +import net.countercraft.movecraft.lifecycle.Worker; import net.countercraft.movecraft.localisation.I18nSupport; import net.countercraft.movecraft.processing.WorldManager; import net.countercraft.movecraft.processing.effects.Effect; @@ -31,9 +33,22 @@ import java.util.Map; import java.util.function.Supplier; -public class StatusManager extends BukkitRunnable implements Listener { +public class StatusManager implements Listener, Worker { private static final CraftDataTagKey LAST_STATUS_CHECK = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "last-status-check"), craft -> System.currentTimeMillis()); + @Inject + public StatusManager(){} + + @Override + public boolean isAsync() { + return true; + } + + @Override + public int getPeriod() { + return 1; + } + @Override public void run() { for (Craft c : CraftManager.getInstance().getCrafts()) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusSign.java index d8a488576..c3fd3c7fc 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusSign.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.features.status; +import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.type.CraftType; @@ -24,6 +25,9 @@ public final class StatusSign implements Listener { + @Inject + public StatusSign(){} + @EventHandler public void onCraftDetect(CraftDetectEvent event) { World world = event.getCraft().getWorld(); From 8951b90a09e015c720084f76aa32d7f883bf563a Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sat, 31 Aug 2024 23:19:31 -0700 Subject: [PATCH 06/18] Remove static references --- .../features/contacts/ContactsManager.java | 16 ++++++++-------- .../movecraft/features/status/StatusManager.java | 9 ++++++--- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java index ce80ed4c0..d1c9ce06b 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java @@ -3,7 +3,6 @@ import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.*; -import net.countercraft.movecraft.craft.datatag.CraftDataTagContainer; import net.countercraft.movecraft.craft.datatag.CraftDataTagKey; import net.countercraft.movecraft.craft.datatag.CraftDataTagRegistry; import net.countercraft.movecraft.craft.type.CraftType; @@ -23,7 +22,6 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -31,9 +29,11 @@ public class ContactsManager implements Listener, Worker { private static final CraftDataTagKey> RECENT_CONTACTS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "recent-contacts"), craft -> new WeakHashMap<>()); - + private final @NotNull CraftManager craftManager; @Inject - public ContactsManager(){} + public ContactsManager(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @Override public boolean isAsync() { @@ -56,7 +56,7 @@ private void runContacts() { if (w == null) continue; - Set craftsInWorld = CraftManager.getInstance().getCraftsInWorld(w); + Set craftsInWorld = craftManager.getCraftsInWorld(w); for (Craft base : craftsInWorld) { if (base instanceof SinkingCraft || base instanceof SubCraft) continue; @@ -137,7 +137,7 @@ private void runRecentContacts() { if (w == null) continue; - for (PlayerCraft base : CraftManager.getInstance().getPlayerCraftsInWorld(w)) { + for (PlayerCraft base : craftManager.getPlayerCraftsInWorld(w)) { if (base.getHitBox().isEmpty()) continue; @@ -249,7 +249,7 @@ public void onCraftSink(@NotNull CraftSinkEvent e) { } private void remove(Craft base) { - for (Craft other : CraftManager.getInstance().getCrafts()) { + for (Craft other : craftManager.getCrafts()) { List contacts = other.getDataTag(Craft.CONTACTS); if (contacts.contains(base)) continue; @@ -258,7 +258,7 @@ private void remove(Craft base) { other.setDataTag(Craft.CONTACTS, contacts); } - for (Craft other : CraftManager.getInstance().getCrafts()) { + for (Craft other : craftManager.getCrafts()) { Map recentContacts = other.getDataTag(RECENT_CONTACTS); if (!recentContacts.containsKey(other)) continue; diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusManager.java index 4da745ab1..7555458b9 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/features/status/StatusManager.java @@ -35,9 +35,12 @@ public class StatusManager implements Listener, Worker { private static final CraftDataTagKey LAST_STATUS_CHECK = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "last-status-check"), craft -> System.currentTimeMillis()); + private final @NotNull CraftManager craftManager; @Inject - public StatusManager(){} + public StatusManager(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @Override public boolean isAsync() { @@ -51,7 +54,7 @@ public int getPeriod() { @Override public void run() { - for (Craft c : CraftManager.getInstance().getCrafts()) { + for (Craft c : craftManager.getCrafts()) { long ticksElapsed = (System.currentTimeMillis() - c.getDataTag(LAST_STATUS_CHECK)) / 50; if (ticksElapsed <= Settings.SinkCheckTicks) continue; @@ -204,7 +207,7 @@ public void onCraftStatusUpdate(@NotNull CraftStatusUpdateEvent e) { if (sinking) { craft.getAudience().sendMessage(I18nSupport.getInternationalisedComponent("Player - Craft is sinking")); craft.setCruising(false); - CraftManager.getInstance().sink(craft); + craftManager.sink(craft); } } } From 19eedfab7425743e334df9a13e625fdb71ae03f1 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sat, 31 Aug 2024 23:24:25 -0700 Subject: [PATCH 07/18] signs don't use static manager --- .../net/countercraft/movecraft/Movecraft.java | 2 -- .../movecraft/sign/AscendSign.java | 14 ++++++++----- .../movecraft/sign/CraftSign.java | 16 +++++++------- .../movecraft/sign/CruiseSign.java | 12 +++++++---- .../movecraft/sign/DescendSign.java | 16 ++++++++------ .../countercraft/movecraft/sign/HelmSign.java | 18 +++++++++------- .../countercraft/movecraft/sign/MoveSign.java | 21 +++++++++++-------- .../movecraft/sign/RelativeMoveSign.java | 21 +++++++++++-------- .../movecraft/sign/ReleaseSign.java | 9 +++++--- .../movecraft/sign/RemoteSign.java | 7 +++++-- .../movecraft/sign/ScuttleSign.java | 11 ++++++---- .../movecraft/sign/SpeedSign.java | 9 ++++++-- .../movecraft/sign/SubcraftRotateSign.java | 12 ++++++----- .../movecraft/sign/TeleportSign.java | 11 ++++++---- 14 files changed, 110 insertions(+), 69 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 747529c7d..3c3bc7100 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -23,7 +23,6 @@ import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.config.SettingsService; import net.countercraft.movecraft.craft.ChunkManager; -import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.features.contacts.ContactsCommand; import net.countercraft.movecraft.features.contacts.ContactsManager; @@ -46,7 +45,6 @@ import org.int4.dirk.di.Injectors; import org.jetbrains.annotations.NotNull; -import java.io.File; import java.util.logging.Logger; public class Movecraft extends JavaPlugin { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java index 4e4afad6f..6d0ddb1c7 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java @@ -21,8 +21,12 @@ import org.jetbrains.annotations.NotNull; public class AscendSign implements Listener { + private final @NotNull CraftManager craftManager; + @Inject - public AscendSign(){} + public AscendSign(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @EventHandler public void onCraftDetect(CraftDetectEvent event){ @@ -57,10 +61,10 @@ public void onSignClickEvent(@NotNull PlayerInteractEvent event){ Sign sign = (Sign) block.getState(); if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Ascend: OFF")) { event.setCancelled(true); - if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) { + if (craftManager.getCraftByPlayer(event.getPlayer()) == null) { return; } - Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft c = craftManager.getCraftByPlayer(event.getPlayer()); if (!c.getType().getBoolProperty(CraftType.CAN_CRUISE)) { return; } @@ -74,7 +78,7 @@ public void onSignClickEvent(@NotNull PlayerInteractEvent event){ c.resetSigns(sign); if (!c.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) { - CraftManager.getInstance().addReleaseTask(c); + craftManager.addReleaseTask(c); } return; } @@ -82,7 +86,7 @@ public void onSignClickEvent(@NotNull PlayerInteractEvent event){ return; } event.setCancelled(true); - Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft c = craftManager.getCraftByPlayer(event.getPlayer()); if (c == null || !c.getType().getBoolProperty(CraftType.CAN_CRUISE)) { return; } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java index 2d82d4671..f5301c8d6 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java @@ -39,15 +39,17 @@ public final class CraftSign implements Listener { private final Set piloting; + private final @NotNull CraftManager craftManager; @Inject - public CraftSign() { + public CraftSign(@NotNull CraftManager craftManager) { + this.craftManager = craftManager; piloting = new HashSet<>(); } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignChange(@NotNull SignChangeEvent event) { - if (CraftManager.getInstance().getCraftTypeFromString(event.getLine(0)) == null) + if (craftManager.getCraftTypeFromString(event.getLine(0)) == null) return; if (!Settings.RequireCreatePerm) @@ -69,7 +71,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; Sign sign = (Sign) state; - CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(ChatColor.stripColor(sign.getLine(0))); + CraftType craftType = craftManager.getCraftTypeFromString(ChatColor.stripColor(sign.getLine(0))); if (craftType == null) return; @@ -90,7 +92,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { // Attempt to run detection World world = event.getClickedBlock().getWorld(); - CraftManager.getInstance().detect( + craftManager.detect( startPoint, craftType, (type, w, p, parents) -> { assert p != null; // Note: This only passes in a non-null player. @@ -142,15 +144,15 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { @Override public void run() { craft.setCruising(false); - CraftManager.getInstance().sink(craft); + craftManager.sink(craft); } }.runTaskLater(Movecraft.getInstance(), (craftType.getIntProperty(CraftType.CRUISE_ON_PILOT_LIFETIME))); } else { // Release old craft if it exists - Craft oldCraft = CraftManager.getInstance().getCraftByPlayer(player); + Craft oldCraft = craftManager.getCraftByPlayer(player); if (oldCraft != null) - CraftManager.getInstance().release(oldCraft, CraftReleaseEvent.Reason.PLAYER, false); + craftManager.release(oldCraft, CraftReleaseEvent.Reason.PLAYER, false); } } ); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java index a779b9c57..0808a8515 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java @@ -25,8 +25,12 @@ import org.jetbrains.annotations.NotNull; public final class CruiseSign implements Listener { + private final @NotNull CraftManager craftManager; + @Inject - public CruiseSign(){} + public CruiseSign(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @EventHandler public void onCraftDetect(@NotNull CraftDetectEvent event) { @@ -60,7 +64,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { String line = ChatColor.stripColor(sign.getLine(0)); if (line.equalsIgnoreCase("Cruise: OFF")) { event.setCancelled(true); - Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft c = craftManager.getCraftByPlayer(event.getPlayer()); if (c == null || !c.getType().getBoolProperty(CraftType.CAN_CRUISE)) return; if (!(sign.getBlockData() instanceof Directional)) @@ -74,12 +78,12 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { c.setCruising(true); c.resetSigns(sign); if (!c.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) { - CraftManager.getInstance().addReleaseTask(c); + craftManager.addReleaseTask(c); } } else if (line.equalsIgnoreCase("Cruise: ON")) { event.setCancelled(true); - Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft c = craftManager.getCraftByPlayer(event.getPlayer()); if (c == null || !c.getType().getBoolProperty(CraftType.CAN_CRUISE)) return; diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java index ec66576ce..82387e6b6 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java @@ -19,9 +19,13 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.jetbrains.annotations.NotNull; -public final class DescendSign implements Listener{ +public final class DescendSign implements Listener { + private final @NotNull CraftManager craftManager; + @Inject - public DescendSign(){} + public DescendSign(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @EventHandler public void onCraftDetect(CraftDetectEvent event){ @@ -54,10 +58,10 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { Sign sign = (Sign) state; if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Descend: OFF")) { event.setCancelled(true); - if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) { + if (craftManager.getCraftByPlayer(event.getPlayer()) == null) { return; } - Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft c = craftManager.getCraftByPlayer(event.getPlayer()); if (!c.getType().getBoolProperty(CraftType.CAN_CRUISE)) { return; } @@ -71,13 +75,13 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { c.resetSigns(sign); if (!c.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) { - CraftManager.getInstance().addReleaseTask(c); + craftManager.addReleaseTask(c); } return; } if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Descend: ON")) { event.setCancelled(true); - Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft c = craftManager.getCraftByPlayer(event.getPlayer()); if (c != null && c.getType().getBoolProperty(CraftType.CAN_CRUISE)) { sign.setLine(0, "Descend: OFF"); sign.update(true); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java index d62987a8d..99cb10cd7 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java @@ -19,8 +19,12 @@ import org.jetbrains.annotations.NotNull; public final class HelmSign implements Listener { + private final @NotNull CraftManager craftManager; + @Inject - public HelmSign(){} + public HelmSign(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @EventHandler public void onSignChange(SignChangeEvent event){ @@ -53,7 +57,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft craft = craftManager.getCraftByPlayer(event.getPlayer()); if (craft == null) { return; } @@ -81,20 +85,20 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; if (craft.getType().getBoolProperty(CraftType.ROTATE_AT_MIDPOINT)) { - CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).rotate(rotation, craft.getHitBox().getMidPoint()); + craftManager.getCraftByPlayer(event.getPlayer()).rotate(rotation, craft.getHitBox().getMidPoint()); } else { - CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).rotate(rotation, MathUtils.bukkit2MovecraftLoc(sign.getLocation())); + craftManager.getCraftByPlayer(event.getPlayer()).rotate(rotation, MathUtils.bukkit2MovecraftLoc(sign.getLocation())); } //timeMap.put(event.getPlayer(), System.currentTimeMillis()); //TODO: Lower speed while turning - /*int curTickCooldown = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getCurTickCooldown(); - int baseTickCooldown = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getCruiseTickCooldown(); + /*int curTickCooldown = craftManager.getCraftByPlayer(event.getPlayer()).getCurTickCooldown(); + int baseTickCooldown = craftManager.getCraftByPlayer(event.getPlayer()).getType().getCruiseTickCooldown(); if (curTickCooldown * 2 > baseTickCooldown) curTickCooldown = baseTickCooldown; else curTickCooldown = curTickCooldown * 2;*/ - //CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).setCurTickCooldown(curTickCooldown); // lose half your speed when turning + //craftManager.getCraftByPlayer(event.getPlayer()).setCurTickCooldown(curTickCooldown); // lose half your speed when turning } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java index 7e83ca1f5..08bb393e5 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java @@ -16,9 +16,12 @@ public final class MoveSign implements Listener{ private static final String HEADER = "Move:"; + private final @NotNull CraftManager craftManager; @Inject - public MoveSign(){} + public MoveSign(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { @@ -34,19 +37,19 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) { + if (craftManager.getCraftByPlayer(event.getPlayer()) == null) { return; } /*Long time = timeMap.get(event.getPlayer()); if (time != null) { long ticksElapsed = (System.currentTimeMillis() - time) / 50; - Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft craft = craftManager.getCraftByPlayer(event.getPlayer()); // if the craft should go slower underwater, make time pass // more slowly there if (craft.getType().getHalfSpeedUnderwater() && craft.getMinY() < craft.getW().getSeaLevel()) { ticksElapsed = ticksElapsed >> 1; } - if (Math.abs(ticksElapsed) < CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getTickCooldown()) { + if (Math.abs(ticksElapsed) < craftManager.getCraftByPlayer(event.getPlayer()).getType().getTickCooldown()) { event.setCancelled(true); return; } @@ -55,7 +58,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { int dx = Integer.parseInt(numbers[0]); int dy = Integer.parseInt(numbers[1]); int dz = Integer.parseInt(numbers[2]); - int maxMove = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getIntProperty(CraftType.MAX_STATIC_MOVE); + int maxMove = craftManager.getCraftByPlayer(event.getPlayer()).getType().getIntProperty(CraftType.MAX_STATIC_MOVE); if (dx > maxMove) dx = maxMove; @@ -70,15 +73,15 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { if (dz < 0 - maxMove) dz = 0 - maxMove; - if (!event.getPlayer().hasPermission("movecraft." + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { + if (!event.getPlayer().hasPermission("movecraft." + craftManager.getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { event.getPlayer().sendMessage( I18nSupport.getInternationalisedString("Insufficient Permissions")); return; } - if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getBoolProperty(CraftType.CAN_STATIC_MOVE)) { - CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).translate(dx, dy, dz); + if (craftManager.getCraftByPlayer(event.getPlayer()).getType().getBoolProperty(CraftType.CAN_STATIC_MOVE)) { + craftManager.getCraftByPlayer(event.getPlayer()).translate(dx, dy, dz); //timeMap.put(event.getPlayer(), System.currentTimeMillis()); - CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).setLastCruiseUpdate(System.currentTimeMillis()); + craftManager.getCraftByPlayer(event.getPlayer()).setLastCruiseUpdate(System.currentTimeMillis()); } } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java index a9f23a8ca..f96d174d1 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java @@ -16,9 +16,12 @@ public final class RelativeMoveSign implements Listener{ private static final String HEADER = "RMove:"; + private final @NotNull CraftManager craftManager; @Inject - public RelativeMoveSign(){} + public RelativeMoveSign(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { @@ -34,20 +37,20 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) { + if (craftManager.getCraftByPlayer(event.getPlayer()) == null) { return; } /*Long time = timeMap.get(event.getPlayer()); if (time != null) { long ticksElapsed = (System.currentTimeMillis() - time) / 50; - Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft craft = craftManager.getCraftByPlayer(event.getPlayer()); // if the craft should go slower underwater, make time pass // more slowly there if (craft.getType().getHalfSpeedUnderwater() && craft.getMinY() < craft.getW().getSeaLevel()) { ticksElapsed = ticksElapsed >> 1; } - if (Math.abs(ticksElapsed) < CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getTickCooldown()) { + if (Math.abs(ticksElapsed) < craftManager.getCraftByPlayer(event.getPlayer()).getType().getTickCooldown()) { event.setCancelled(true); return; } @@ -64,7 +67,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { // positive // = // forwards - int maxMove = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getIntProperty(CraftType.MAX_STATIC_MOVE); + int maxMove = craftManager.getCraftByPlayer(event.getPlayer()).getType().getIntProperty(CraftType.MAX_STATIC_MOVE); if (dLeftRight > maxMove) dLeftRight = maxMove; @@ -103,15 +106,15 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { break; } - if (!event.getPlayer().hasPermission("movecraft." + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { + if (!event.getPlayer().hasPermission("movecraft." + craftManager.getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { event.getPlayer().sendMessage( I18nSupport.getInternationalisedString("Insufficient Permissions")); return; } - if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getBoolProperty(CraftType.CAN_STATIC_MOVE)) { - CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).translate(dx, dy, dz); + if (craftManager.getCraftByPlayer(event.getPlayer()).getType().getBoolProperty(CraftType.CAN_STATIC_MOVE)) { + craftManager.getCraftByPlayer(event.getPlayer()).translate(dx, dy, dz); //timeMap.put(event.getPlayer(), System.currentTimeMillis()); - CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).setLastCruiseUpdate(System.currentTimeMillis()); + craftManager.getCraftByPlayer(event.getPlayer()).setLastCruiseUpdate(System.currentTimeMillis()); } } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java index 50f978d04..5ebc07d9d 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java @@ -16,9 +16,12 @@ public final class ReleaseSign implements Listener{ private static final String HEADER = "Release"; + private final @NotNull CraftManager craftManager; @Inject - public ReleaseSign(){} + public ReleaseSign(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { @@ -34,10 +37,10 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft craft = craftManager.getCraftByPlayer(event.getPlayer()); if (craft == null) { return; } - CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.PLAYER, false); + craftManager.release(craft, CraftReleaseEvent.Reason.PLAYER, false); } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java index 79eb9d5c5..ae2c4644d 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java @@ -28,9 +28,12 @@ public final class RemoteSign implements Listener{ private static final String HEADER = "Remote Sign"; + private final @NotNull CraftManager craftManager; @Inject - public RemoteSign(){} + public RemoteSign(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @EventHandler public final void onSignChange(SignChangeEvent event) { @@ -61,7 +64,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { } event.setCancelled(true); Craft foundCraft = null; - for (PlayerCraft tcraft : CraftManager.getInstance().getPlayerCraftsInWorld(event.getClickedBlock().getWorld())) { + for (PlayerCraft tcraft : craftManager.getPlayerCraftsInWorld(event.getClickedBlock().getWorld())) { if (MathUtils.locationInHitBox(tcraft.getHitBox(), event.getClickedBlock().getLocation())) { // don't use a craft with a null player. This is // mostly to avoid trying to use subcrafts diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java index e8e33ee7f..ccb05bf28 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java @@ -25,9 +25,12 @@ public class ScuttleSign implements Listener { private static final String HEADER = "Scuttle"; + private final @NotNull CraftManager craftManager; @Inject - public ScuttleSign(){} + public ScuttleSign(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { @@ -45,14 +48,14 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft craft = craftManager.getCraftByPlayer(event.getPlayer()); if (craft == null) { if (!event.getPlayer().hasPermission("movecraft.commands.scuttle.others")) { event.getPlayer().sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); return; } - craft = MathUtils.fastNearestCraftToLoc(CraftManager.getInstance().getCrafts(), + craft = MathUtils.fastNearestCraftToLoc(craftManager.getCrafts(), event.getClickedBlock().getLocation()); if (craft == null) return; @@ -79,7 +82,7 @@ private void scuttle(Craft craft, CommandSender commandSender){ return; craft.setCruising(false); - CraftManager.getInstance().sink(craft); + craftManager.sink(craft); commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Scuttle - Scuttle Activated")); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java index 4d3cd5ed1..9269e4669 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java @@ -21,10 +21,15 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; +import org.jetbrains.annotations.NotNull; public final class SpeedSign implements Listener { + private final @NotNull CraftManager craftManager; + @Inject - public SpeedSign(){} + public SpeedSign(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @EventHandler public void onCraftDetect(CraftDetectEvent event){ @@ -78,7 +83,7 @@ public void onSignClick(PlayerInteractEvent event) { event.setCancelled(true); Player player = event.getPlayer(); - Craft craft = CraftManager.getInstance().getCraftByPlayer(player); + Craft craft = craftManager.getCraftByPlayer(player); if (craft == null) return; diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java index c2ffc3e28..1b42a7cfc 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java @@ -32,9 +32,11 @@ public final class SubcraftRotateSign implements Listener { private static final String HEADER = "Subcraft Rotate"; private final Set rotating; + private final @NotNull CraftManager craftManager; @Inject - public SubcraftRotateSign() { + public SubcraftRotateSign(@NotNull CraftManager craftManager) { + this.craftManager = craftManager; rotating = new HashSet<>(); } @@ -66,7 +68,7 @@ else if (event.getAction() == Action.LEFT_CLICK_BLOCK) // rotate subcraft String craftTypeStr = ChatColor.stripColor(sign.getLine(1)); - CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(craftTypeStr); + CraftType craftType = craftManager.getCraftTypeFromString(craftTypeStr); if (craftType == null) return; if (ChatColor.stripColor(sign.getLine(2)).equals("") @@ -81,7 +83,7 @@ else if (event.getAction() == Action.LEFT_CLICK_BLOCK) return; } - Craft playerCraft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + Craft playerCraft = craftManager.getCraftByPlayer(event.getPlayer()); if (playerCraft != null) { if (!playerCraft.isNotProcessing()) { event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Detection - Parent Craft is busy")); @@ -100,7 +102,7 @@ public void run() { Player player = event.getPlayer(); World world = event.getClickedBlock().getWorld(); - CraftManager.getInstance().detect( + craftManager.detect( startPoint, craftType, (type, w, p, parents) -> { if (parents.size() > 1) @@ -130,7 +132,7 @@ public void run() { var newHitbox = parent.getHitBox().union(craft.getHitBox()); parent.setHitBox(newHitbox); } - CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.SUB_CRAFT, false); + craftManager.release(craft, CraftReleaseEvent.Reason.SUB_CRAFT, false); } }.runTaskLater(Movecraft.getInstance(), 3); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java index d6dc2a182..7d2b4fafc 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java @@ -19,9 +19,12 @@ public final class TeleportSign implements Listener { private static final String HEADER = "Teleport:"; + private final @NotNull CraftManager craftManager; @Inject - public TeleportSign(){} + public TeleportSign(@NotNull CraftManager craftManager){ + this.craftManager = craftManager; + } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { @@ -37,7 +40,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) { + if (craftManager.getCraftByPlayer(event.getPlayer()) == null) { return; } @@ -60,11 +63,11 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { World world = Bukkit.getWorld(w); if (world == null) world = sign.getWorld(); - if (!event.getPlayer().hasPermission("movecraft." + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { + if (!event.getPlayer().hasPermission("movecraft." + craftManager.getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions")); return; } - final Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); + final Craft c = craftManager.getCraftByPlayer(event.getPlayer()); if (c == null || !c.getType().getBoolProperty(CraftType.CAN_TELEPORT)) { return; } From 61198abe3f3d067d77c484cea60f6d858bb78e50 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sun, 1 Sep 2024 06:35:38 -0700 Subject: [PATCH 08/18] Revert most signs --- .../net/countercraft/movecraft/Movecraft.java | 31 +++++++++---------- .../movecraft/sign/AscendSign.java | 15 +++------ .../movecraft/sign/CraftSign.java | 22 +++++-------- .../movecraft/sign/CruiseSign.java | 13 ++------ .../movecraft/sign/DescendSign.java | 17 +++------- .../countercraft/movecraft/sign/HelmSign.java | 19 ++++-------- .../countercraft/movecraft/sign/MoveSign.java | 23 +++++--------- .../countercraft/movecraft/sign/NameSign.java | 5 --- .../movecraft/sign/PilotSign.java | 5 --- .../movecraft/sign/RelativeMoveSign.java | 23 +++++--------- .../movecraft/sign/ReleaseSign.java | 11 ++----- .../movecraft/sign/RemoteSign.java | 9 +----- .../movecraft/sign/ScuttleSign.java | 14 +++------ .../movecraft/sign/SpeedSign.java | 13 ++------ .../movecraft/sign/SubcraftRotateSign.java | 18 +++-------- .../movecraft/sign/TeleportSign.java | 13 ++------ 16 files changed, 73 insertions(+), 178 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 3c3bc7100..b4cd085e4 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -108,22 +108,21 @@ public void onEnable() { injector.register(PlayerListener.class); injector.register(ChunkManager.class); - // Signs - injector.register(AscendSign.class); - injector.register(CraftSign.class); - injector.register(CruiseSign.class); - injector.register(DescendSign.class); - injector.register(HelmSign.class); - injector.register(MoveSign.class); - injector.register(NameSign.class); - injector.register(PilotSign.class); - injector.register(RelativeMoveSign.class); - injector.register(ReleaseSign.class); - injector.register(RemoteSign.class); - injector.register(SpeedSign.class); - injector.register(SubcraftRotateSign.class); - injector.register(TeleportSign.class); - injector.register(ScuttleSign.class); + getServer().getPluginManager().registerEvents(new AscendSign(), this); + getServer().getPluginManager().registerEvents(new CraftSign(), this); + getServer().getPluginManager().registerEvents(new CruiseSign(), this); + getServer().getPluginManager().registerEvents(new DescendSign(), this); + getServer().getPluginManager().registerEvents(new HelmSign(), this); + getServer().getPluginManager().registerEvents(new MoveSign(), this); + getServer().getPluginManager().registerEvents(new NameSign(), this); + getServer().getPluginManager().registerEvents(new PilotSign(), this); + getServer().getPluginManager().registerEvents(new RelativeMoveSign(), this); + getServer().getPluginManager().registerEvents(new ReleaseSign(), this); + getServer().getPluginManager().registerEvents(new RemoteSign(), this); + getServer().getPluginManager().registerEvents(new SpeedSign(), this); + getServer().getPluginManager().registerEvents(new SubcraftRotateSign(), this); + getServer().getPluginManager().registerEvents(new TeleportSign(), this); + getServer().getPluginManager().registerEvents(new ScuttleSign(), this); injector.register(CraftPilotListener.class); injector.register(CraftReleaseListener.class); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java index 6d0ddb1c7..e59193e3b 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/AscendSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; @@ -21,12 +20,6 @@ import org.jetbrains.annotations.NotNull; public class AscendSign implements Listener { - private final @NotNull CraftManager craftManager; - - @Inject - public AscendSign(@NotNull CraftManager craftManager){ - this.craftManager = craftManager; - } @EventHandler public void onCraftDetect(CraftDetectEvent event){ @@ -61,10 +54,10 @@ public void onSignClickEvent(@NotNull PlayerInteractEvent event){ Sign sign = (Sign) block.getState(); if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Ascend: OFF")) { event.setCancelled(true); - if (craftManager.getCraftByPlayer(event.getPlayer()) == null) { + if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) { return; } - Craft c = craftManager.getCraftByPlayer(event.getPlayer()); + Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); if (!c.getType().getBoolProperty(CraftType.CAN_CRUISE)) { return; } @@ -78,7 +71,7 @@ public void onSignClickEvent(@NotNull PlayerInteractEvent event){ c.resetSigns(sign); if (!c.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) { - craftManager.addReleaseTask(c); + CraftManager.getInstance().addReleaseTask(c); } return; } @@ -86,7 +79,7 @@ public void onSignClickEvent(@NotNull PlayerInteractEvent event){ return; } event.setCancelled(true); - Craft c = craftManager.getCraftByPlayer(event.getPlayer()); + Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); if (c == null || !c.getType().getBoolProperty(CraftType.CAN_CRUISE)) { return; } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java index f5301c8d6..ec5a977c5 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CraftSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; @@ -38,18 +37,11 @@ import java.util.Set; public final class CraftSign implements Listener { - private final Set piloting; - private final @NotNull CraftManager craftManager; - - @Inject - public CraftSign(@NotNull CraftManager craftManager) { - this.craftManager = craftManager; - piloting = new HashSet<>(); - } + private final Set piloting = new HashSet<>(); @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignChange(@NotNull SignChangeEvent event) { - if (craftManager.getCraftTypeFromString(event.getLine(0)) == null) + if (CraftManager.getInstance().getCraftTypeFromString(event.getLine(0)) == null) return; if (!Settings.RequireCreatePerm) @@ -71,7 +63,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; Sign sign = (Sign) state; - CraftType craftType = craftManager.getCraftTypeFromString(ChatColor.stripColor(sign.getLine(0))); + CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(ChatColor.stripColor(sign.getLine(0))); if (craftType == null) return; @@ -92,7 +84,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { // Attempt to run detection World world = event.getClickedBlock().getWorld(); - craftManager.detect( + CraftManager.getInstance().detect( startPoint, craftType, (type, w, p, parents) -> { assert p != null; // Note: This only passes in a non-null player. @@ -144,15 +136,15 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { @Override public void run() { craft.setCruising(false); - craftManager.sink(craft); + CraftManager.getInstance().sink(craft); } }.runTaskLater(Movecraft.getInstance(), (craftType.getIntProperty(CraftType.CRUISE_ON_PILOT_LIFETIME))); } else { // Release old craft if it exists - Craft oldCraft = craftManager.getCraftByPlayer(player); + Craft oldCraft = CraftManager.getInstance().getCraftByPlayer(player); if (oldCraft != null) - craftManager.release(oldCraft, CraftReleaseEvent.Reason.PLAYER, false); + CraftManager.getInstance().release(oldCraft, CraftReleaseEvent.Reason.PLAYER, false); } } ); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java index 0808a8515..fd4efec28 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/CruiseSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.config.Settings; @@ -25,12 +24,6 @@ import org.jetbrains.annotations.NotNull; public final class CruiseSign implements Listener { - private final @NotNull CraftManager craftManager; - - @Inject - public CruiseSign(@NotNull CraftManager craftManager){ - this.craftManager = craftManager; - } @EventHandler public void onCraftDetect(@NotNull CraftDetectEvent event) { @@ -64,7 +57,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { String line = ChatColor.stripColor(sign.getLine(0)); if (line.equalsIgnoreCase("Cruise: OFF")) { event.setCancelled(true); - Craft c = craftManager.getCraftByPlayer(event.getPlayer()); + Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); if (c == null || !c.getType().getBoolProperty(CraftType.CAN_CRUISE)) return; if (!(sign.getBlockData() instanceof Directional)) @@ -78,12 +71,12 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { c.setCruising(true); c.resetSigns(sign); if (!c.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) { - craftManager.addReleaseTask(c); + CraftManager.getInstance().addReleaseTask(c); } } else if (line.equalsIgnoreCase("Cruise: ON")) { event.setCancelled(true); - Craft c = craftManager.getCraftByPlayer(event.getPlayer()); + Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); if (c == null || !c.getType().getBoolProperty(CraftType.CAN_CRUISE)) return; diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java index 82387e6b6..2d638689b 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/DescendSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; @@ -19,13 +18,7 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.jetbrains.annotations.NotNull; -public final class DescendSign implements Listener { - private final @NotNull CraftManager craftManager; - - @Inject - public DescendSign(@NotNull CraftManager craftManager){ - this.craftManager = craftManager; - } +public final class DescendSign implements Listener{ @EventHandler public void onCraftDetect(CraftDetectEvent event){ @@ -58,10 +51,10 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { Sign sign = (Sign) state; if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Descend: OFF")) { event.setCancelled(true); - if (craftManager.getCraftByPlayer(event.getPlayer()) == null) { + if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) { return; } - Craft c = craftManager.getCraftByPlayer(event.getPlayer()); + Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); if (!c.getType().getBoolProperty(CraftType.CAN_CRUISE)) { return; } @@ -75,13 +68,13 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { c.resetSigns(sign); if (!c.getType().getBoolProperty(CraftType.MOVE_ENTITIES)) { - craftManager.addReleaseTask(c); + CraftManager.getInstance().addReleaseTask(c); } return; } if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("Descend: ON")) { event.setCancelled(true); - Craft c = craftManager.getCraftByPlayer(event.getPlayer()); + Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); if (c != null && c.getType().getBoolProperty(CraftType.CAN_CRUISE)) { sign.setLine(0, "Descend: OFF"); sign.update(true); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java index 99cb10cd7..74eef63fa 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/HelmSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftRotation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; @@ -19,12 +18,6 @@ import org.jetbrains.annotations.NotNull; public final class HelmSign implements Listener { - private final @NotNull CraftManager craftManager; - - @Inject - public HelmSign(@NotNull CraftManager craftManager){ - this.craftManager = craftManager; - } @EventHandler public void onSignChange(SignChangeEvent event){ @@ -57,7 +50,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - Craft craft = craftManager.getCraftByPlayer(event.getPlayer()); + Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); if (craft == null) { return; } @@ -85,20 +78,20 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; if (craft.getType().getBoolProperty(CraftType.ROTATE_AT_MIDPOINT)) { - craftManager.getCraftByPlayer(event.getPlayer()).rotate(rotation, craft.getHitBox().getMidPoint()); + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).rotate(rotation, craft.getHitBox().getMidPoint()); } else { - craftManager.getCraftByPlayer(event.getPlayer()).rotate(rotation, MathUtils.bukkit2MovecraftLoc(sign.getLocation())); + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).rotate(rotation, MathUtils.bukkit2MovecraftLoc(sign.getLocation())); } //timeMap.put(event.getPlayer(), System.currentTimeMillis()); //TODO: Lower speed while turning - /*int curTickCooldown = craftManager.getCraftByPlayer(event.getPlayer()).getCurTickCooldown(); - int baseTickCooldown = craftManager.getCraftByPlayer(event.getPlayer()).getType().getCruiseTickCooldown(); + /*int curTickCooldown = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getCurTickCooldown(); + int baseTickCooldown = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getCruiseTickCooldown(); if (curTickCooldown * 2 > baseTickCooldown) curTickCooldown = baseTickCooldown; else curTickCooldown = curTickCooldown * 2;*/ - //craftManager.getCraftByPlayer(event.getPlayer()).setCurTickCooldown(curTickCooldown); // lose half your speed when turning + //CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).setCurTickCooldown(curTickCooldown); // lose half your speed when turning } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java index 08bb393e5..37c0f00d4 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/MoveSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.localisation.I18nSupport; @@ -16,12 +15,6 @@ public final class MoveSign implements Listener{ private static final String HEADER = "Move:"; - private final @NotNull CraftManager craftManager; - - @Inject - public MoveSign(@NotNull CraftManager craftManager){ - this.craftManager = craftManager; - } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { @@ -37,19 +30,19 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - if (craftManager.getCraftByPlayer(event.getPlayer()) == null) { + if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) { return; } /*Long time = timeMap.get(event.getPlayer()); if (time != null) { long ticksElapsed = (System.currentTimeMillis() - time) / 50; - Craft craft = craftManager.getCraftByPlayer(event.getPlayer()); + Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); // if the craft should go slower underwater, make time pass // more slowly there if (craft.getType().getHalfSpeedUnderwater() && craft.getMinY() < craft.getW().getSeaLevel()) { ticksElapsed = ticksElapsed >> 1; } - if (Math.abs(ticksElapsed) < craftManager.getCraftByPlayer(event.getPlayer()).getType().getTickCooldown()) { + if (Math.abs(ticksElapsed) < CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getTickCooldown()) { event.setCancelled(true); return; } @@ -58,7 +51,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { int dx = Integer.parseInt(numbers[0]); int dy = Integer.parseInt(numbers[1]); int dz = Integer.parseInt(numbers[2]); - int maxMove = craftManager.getCraftByPlayer(event.getPlayer()).getType().getIntProperty(CraftType.MAX_STATIC_MOVE); + int maxMove = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getIntProperty(CraftType.MAX_STATIC_MOVE); if (dx > maxMove) dx = maxMove; @@ -73,15 +66,15 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { if (dz < 0 - maxMove) dz = 0 - maxMove; - if (!event.getPlayer().hasPermission("movecraft." + craftManager.getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { + if (!event.getPlayer().hasPermission("movecraft." + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { event.getPlayer().sendMessage( I18nSupport.getInternationalisedString("Insufficient Permissions")); return; } - if (craftManager.getCraftByPlayer(event.getPlayer()).getType().getBoolProperty(CraftType.CAN_STATIC_MOVE)) { - craftManager.getCraftByPlayer(event.getPlayer()).translate(dx, dy, dz); + if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getBoolProperty(CraftType.CAN_STATIC_MOVE)) { + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).translate(dx, dy, dz); //timeMap.put(event.getPlayer(), System.currentTimeMillis()); - craftManager.getCraftByPlayer(event.getPlayer()).setLastCruiseUpdate(System.currentTimeMillis()); + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).setLastCruiseUpdate(System.currentTimeMillis()); } } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/NameSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/NameSign.java index 25c21d417..787f3711c 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/NameSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/NameSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.Craft; @@ -26,10 +25,6 @@ public final class NameSign implements Listener { private static final String HEADER = "Name:"; - - @Inject - public NameSign(){} - @EventHandler public void onCraftDetect(@NotNull CraftDetectEvent event) { Craft c = event.getCraft(); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/PilotSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/PilotSign.java index bb124f1fb..9ed6cd230 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/PilotSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/PilotSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import org.bukkit.ChatColor; import org.bukkit.block.Block; import org.bukkit.block.Sign; @@ -14,10 +13,6 @@ public final class PilotSign implements Listener { private static final String HEADER = "Pilot:"; - - @Inject - public PilotSign(){} - @EventHandler public final void onSignChange(SignChangeEvent event){ if (event.getLine(0).equalsIgnoreCase(HEADER)) { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java index f96d174d1..2e187d3fb 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RelativeMoveSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.localisation.I18nSupport; @@ -16,12 +15,6 @@ public final class RelativeMoveSign implements Listener{ private static final String HEADER = "RMove:"; - private final @NotNull CraftManager craftManager; - - @Inject - public RelativeMoveSign(@NotNull CraftManager craftManager){ - this.craftManager = craftManager; - } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { @@ -37,20 +30,20 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - if (craftManager.getCraftByPlayer(event.getPlayer()) == null) { + if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) { return; } /*Long time = timeMap.get(event.getPlayer()); if (time != null) { long ticksElapsed = (System.currentTimeMillis() - time) / 50; - Craft craft = craftManager.getCraftByPlayer(event.getPlayer()); + Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); // if the craft should go slower underwater, make time pass // more slowly there if (craft.getType().getHalfSpeedUnderwater() && craft.getMinY() < craft.getW().getSeaLevel()) { ticksElapsed = ticksElapsed >> 1; } - if (Math.abs(ticksElapsed) < craftManager.getCraftByPlayer(event.getPlayer()).getType().getTickCooldown()) { + if (Math.abs(ticksElapsed) < CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getTickCooldown()) { event.setCancelled(true); return; } @@ -67,7 +60,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { // positive // = // forwards - int maxMove = craftManager.getCraftByPlayer(event.getPlayer()).getType().getIntProperty(CraftType.MAX_STATIC_MOVE); + int maxMove = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getIntProperty(CraftType.MAX_STATIC_MOVE); if (dLeftRight > maxMove) dLeftRight = maxMove; @@ -106,15 +99,15 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { break; } - if (!event.getPlayer().hasPermission("movecraft." + craftManager.getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { + if (!event.getPlayer().hasPermission("movecraft." + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { event.getPlayer().sendMessage( I18nSupport.getInternationalisedString("Insufficient Permissions")); return; } - if (craftManager.getCraftByPlayer(event.getPlayer()).getType().getBoolProperty(CraftType.CAN_STATIC_MOVE)) { - craftManager.getCraftByPlayer(event.getPlayer()).translate(dx, dy, dz); + if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getBoolProperty(CraftType.CAN_STATIC_MOVE)) { + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).translate(dx, dy, dz); //timeMap.put(event.getPlayer(), System.currentTimeMillis()); - craftManager.getCraftByPlayer(event.getPlayer()).setLastCruiseUpdate(System.currentTimeMillis()); + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).setLastCruiseUpdate(System.currentTimeMillis()); } } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java index 5ebc07d9d..91e9aa544 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ReleaseSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.events.CraftReleaseEvent; @@ -16,12 +15,6 @@ public final class ReleaseSign implements Listener{ private static final String HEADER = "Release"; - private final @NotNull CraftManager craftManager; - - @Inject - public ReleaseSign(@NotNull CraftManager craftManager){ - this.craftManager = craftManager; - } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { @@ -37,10 +30,10 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - Craft craft = craftManager.getCraftByPlayer(event.getPlayer()); + Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); if (craft == null) { return; } - craftManager.release(craft, CraftReleaseEvent.Reason.PLAYER, false); + CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.PLAYER, false); } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java index ae2c4644d..f9bad8835 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/RemoteSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.Craft; @@ -28,12 +27,6 @@ public final class RemoteSign implements Listener{ private static final String HEADER = "Remote Sign"; - private final @NotNull CraftManager craftManager; - - @Inject - public RemoteSign(@NotNull CraftManager craftManager){ - this.craftManager = craftManager; - } @EventHandler public final void onSignChange(SignChangeEvent event) { @@ -64,7 +57,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { } event.setCancelled(true); Craft foundCraft = null; - for (PlayerCraft tcraft : craftManager.getPlayerCraftsInWorld(event.getClickedBlock().getWorld())) { + for (PlayerCraft tcraft : CraftManager.getInstance().getPlayerCraftsInWorld(event.getClickedBlock().getWorld())) { if (MathUtils.locationInHitBox(tcraft.getHitBox(), event.getClickedBlock().getLocation())) { // don't use a craft with a null player. This is // mostly to avoid trying to use subcrafts diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java index ccb05bf28..39f9fd215 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/ScuttleSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.SinkingCraft; @@ -24,13 +23,8 @@ import static net.countercraft.movecraft.util.ChatUtils.MOVECRAFT_COMMAND_PREFIX; public class ScuttleSign implements Listener { - private static final String HEADER = "Scuttle"; - private final @NotNull CraftManager craftManager; - @Inject - public ScuttleSign(@NotNull CraftManager craftManager){ - this.craftManager = craftManager; - } + private static final String HEADER = "Scuttle"; @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { @@ -48,14 +42,14 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - Craft craft = craftManager.getCraftByPlayer(event.getPlayer()); + Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); if (craft == null) { if (!event.getPlayer().hasPermission("movecraft.commands.scuttle.others")) { event.getPlayer().sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft")); return; } - craft = MathUtils.fastNearestCraftToLoc(craftManager.getCrafts(), + craft = MathUtils.fastNearestCraftToLoc(CraftManager.getInstance().getCrafts(), event.getClickedBlock().getLocation()); if (craft == null) return; @@ -82,7 +76,7 @@ private void scuttle(Craft craft, CommandSender commandSender){ return; craft.setCruising(false); - craftManager.sink(craft); + CraftManager.getInstance().sink(craft); commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Scuttle - Scuttle Activated")); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java index 9269e4669..1d77fee88 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SpeedSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; @@ -21,16 +20,8 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -import org.jetbrains.annotations.NotNull; - -public final class SpeedSign implements Listener { - private final @NotNull CraftManager craftManager; - - @Inject - public SpeedSign(@NotNull CraftManager craftManager){ - this.craftManager = craftManager; - } +public final class SpeedSign implements Listener{ @EventHandler public void onCraftDetect(CraftDetectEvent event){ World world = event.getCraft().getWorld(); @@ -83,7 +74,7 @@ public void onSignClick(PlayerInteractEvent event) { event.setCancelled(true); Player player = event.getPlayer(); - Craft craft = craftManager.getCraftByPlayer(player); + Craft craft = CraftManager.getInstance().getCraftByPlayer(player); if (craft == null) return; diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java index 1b42a7cfc..c8469fa38 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/SubcraftRotateSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.MovecraftRotation; @@ -31,14 +30,7 @@ public final class SubcraftRotateSign implements Listener { private static final String HEADER = "Subcraft Rotate"; - private final Set rotating; - private final @NotNull CraftManager craftManager; - - @Inject - public SubcraftRotateSign(@NotNull CraftManager craftManager) { - this.craftManager = craftManager; - rotating = new HashSet<>(); - } + private final Set rotating = new HashSet<>(); @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { @@ -68,7 +60,7 @@ else if (event.getAction() == Action.LEFT_CLICK_BLOCK) // rotate subcraft String craftTypeStr = ChatColor.stripColor(sign.getLine(1)); - CraftType craftType = craftManager.getCraftTypeFromString(craftTypeStr); + CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(craftTypeStr); if (craftType == null) return; if (ChatColor.stripColor(sign.getLine(2)).equals("") @@ -83,7 +75,7 @@ else if (event.getAction() == Action.LEFT_CLICK_BLOCK) return; } - Craft playerCraft = craftManager.getCraftByPlayer(event.getPlayer()); + Craft playerCraft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); if (playerCraft != null) { if (!playerCraft.isNotProcessing()) { event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Detection - Parent Craft is busy")); @@ -102,7 +94,7 @@ public void run() { Player player = event.getPlayer(); World world = event.getClickedBlock().getWorld(); - craftManager.detect( + CraftManager.getInstance().detect( startPoint, craftType, (type, w, p, parents) -> { if (parents.size() > 1) @@ -132,7 +124,7 @@ public void run() { var newHitbox = parent.getHitBox().union(craft.getHitBox()); parent.setHitBox(newHitbox); } - craftManager.release(craft, CraftReleaseEvent.Reason.SUB_CRAFT, false); + CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.SUB_CRAFT, false); } }.runTaskLater(Movecraft.getInstance(), 3); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java index 7d2b4fafc..4f41f6db5 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/sign/TeleportSign.java @@ -1,6 +1,5 @@ package net.countercraft.movecraft.sign; -import jakarta.inject.Inject; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.type.CraftType; @@ -19,12 +18,6 @@ public final class TeleportSign implements Listener { private static final String HEADER = "Teleport:"; - private final @NotNull CraftManager craftManager; - - @Inject - public TeleportSign(@NotNull CraftManager craftManager){ - this.craftManager = craftManager; - } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSignClick(@NotNull PlayerInteractEvent event) { @@ -40,7 +33,7 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { return; } event.setCancelled(true); - if (craftManager.getCraftByPlayer(event.getPlayer()) == null) { + if (CraftManager.getInstance().getCraftByPlayer(event.getPlayer()) == null) { return; } @@ -63,11 +56,11 @@ public void onSignClick(@NotNull PlayerInteractEvent event) { World world = Bukkit.getWorld(w); if (world == null) world = sign.getWorld(); - if (!event.getPlayer().hasPermission("movecraft." + craftManager.getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { + if (!event.getPlayer().hasPermission("movecraft." + CraftManager.getInstance().getCraftByPlayer(event.getPlayer()).getType().getStringProperty(CraftType.NAME) + ".move")) { event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions")); return; } - final Craft c = craftManager.getCraftByPlayer(event.getPlayer()); + final Craft c = CraftManager.getInstance().getCraftByPlayer(event.getPlayer()); if (c == null || !c.getType().getBoolProperty(CraftType.CAN_TELEPORT)) { return; } From f7fd831b09a1e613ce0c61e82802bb8b6bfdceee Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sun, 1 Sep 2024 07:08:03 -0700 Subject: [PATCH 09/18] unroll wildcard imports --- .../net/countercraft/movecraft/Movecraft.java | 40 +++++++++++++++---- .../movecraft/async/AsyncManager.java | 10 +++-- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index b4cd085e4..a3405d6e5 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -18,7 +18,16 @@ package net.countercraft.movecraft; import net.countercraft.movecraft.async.AsyncManager; -import net.countercraft.movecraft.commands.*; +import net.countercraft.movecraft.commands.CraftInfoCommand; +import net.countercraft.movecraft.commands.CraftReportCommand; +import net.countercraft.movecraft.commands.CraftTypeCommand; +import net.countercraft.movecraft.commands.CruiseCommand; +import net.countercraft.movecraft.commands.ManOverboardCommand; +import net.countercraft.movecraft.commands.MovecraftCommand; +import net.countercraft.movecraft.commands.PilotCommand; +import net.countercraft.movecraft.commands.ReleaseCommand; +import net.countercraft.movecraft.commands.RotateCommand; +import net.countercraft.movecraft.commands.ScuttleCommand; import net.countercraft.movecraft.config.DataPackService; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.config.SettingsService; @@ -33,11 +42,29 @@ import net.countercraft.movecraft.lifecycle.ListenerLifecycleService; import net.countercraft.movecraft.lifecycle.ServiceHost; import net.countercraft.movecraft.lifecycle.WorkerServiceHost; -import net.countercraft.movecraft.listener.*; +import net.countercraft.movecraft.listener.BlockListener; +import net.countercraft.movecraft.listener.CraftPilotListener; +import net.countercraft.movecraft.listener.CraftReleaseListener; +import net.countercraft.movecraft.listener.InteractListener; +import net.countercraft.movecraft.listener.PlayerListener; import net.countercraft.movecraft.localisation.I18nSupport; import net.countercraft.movecraft.mapUpdater.MapUpdateManager; import net.countercraft.movecraft.processing.WorldManager; -import net.countercraft.movecraft.sign.*; +import net.countercraft.movecraft.sign.AscendSign; +import net.countercraft.movecraft.sign.CraftSign; +import net.countercraft.movecraft.sign.CruiseSign; +import net.countercraft.movecraft.sign.DescendSign; +import net.countercraft.movecraft.sign.HelmSign; +import net.countercraft.movecraft.sign.MoveSign; +import net.countercraft.movecraft.sign.NameSign; +import net.countercraft.movecraft.sign.PilotSign; +import net.countercraft.movecraft.sign.RelativeMoveSign; +import net.countercraft.movecraft.sign.ReleaseSign; +import net.countercraft.movecraft.sign.RemoteSign; +import net.countercraft.movecraft.sign.ScuttleSign; +import net.countercraft.movecraft.sign.SpeedSign; +import net.countercraft.movecraft.sign.SubcraftRotateSign; +import net.countercraft.movecraft.sign.TeleportSign; import net.countercraft.movecraft.support.SmoothTeleportFactory; import net.countercraft.movecraft.support.WorldHandlerFactory; import org.bukkit.plugin.java.JavaPlugin; @@ -49,8 +76,6 @@ public class Movecraft extends JavaPlugin { private static Movecraft instance; - - private Logger logger; private boolean shuttingDown; private Injector injector; @@ -67,8 +92,9 @@ public void onDisable() { @Override public void onEnable() { + Logger logger = getLogger(); injector = Injectors.manual(); - injector.registerInstance(getLogger()); + injector.registerInstance(logger); injector.registerInstance(this); injector.register(AsyncManager.class); injector.register(MapUpdateManager.class); @@ -108,6 +134,7 @@ public void onEnable() { injector.register(PlayerListener.class); injector.register(ChunkManager.class); + //TODO: Sign rework getServer().getPluginManager().registerEvents(new AscendSign(), this); getServer().getPluginManager().registerEvents(new CraftSign(), this); getServer().getPluginManager().registerEvents(new CruiseSign(), this); @@ -147,7 +174,6 @@ public void onEnable() { public void onLoad() { super.onLoad(); instance = this; - logger = getLogger(); saveDefaultConfig(); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java index 9e5f81e44..3edbe9cf4 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java @@ -22,7 +22,6 @@ import net.countercraft.movecraft.CruiseDirection; import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; -import net.countercraft.movecraft.lifecycle.Service; import net.countercraft.movecraft.async.rotation.RotationTask; import net.countercraft.movecraft.async.translation.TranslationTask; import net.countercraft.movecraft.craft.Craft; @@ -38,10 +37,15 @@ import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; -import java.util.*; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.WeakHashMap; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; From ac4f358f8547d08fd9dfcc5a13d9cd922a1d07eb Mon Sep 17 00:00:00 2001 From: ohnoey Date: Mon, 2 Sep 2024 20:59:59 -0700 Subject: [PATCH 10/18] Add builder --- .../net/countercraft/movecraft/Movecraft.java | 35 +++++----- .../movecraft/lifecycle/PluginBuilder.java | 67 +++++++++++++++++++ 2 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index a3405d6e5..d93dd7652 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -39,9 +39,8 @@ import net.countercraft.movecraft.features.fading.WreckManager; import net.countercraft.movecraft.features.status.StatusManager; import net.countercraft.movecraft.features.status.StatusSign; -import net.countercraft.movecraft.lifecycle.ListenerLifecycleService; +import net.countercraft.movecraft.lifecycle.PluginBuilder; import net.countercraft.movecraft.lifecycle.ServiceHost; -import net.countercraft.movecraft.lifecycle.WorkerServiceHost; import net.countercraft.movecraft.listener.BlockListener; import net.countercraft.movecraft.listener.CraftPilotListener; import net.countercraft.movecraft.listener.CraftReleaseListener; @@ -68,8 +67,6 @@ import net.countercraft.movecraft.support.SmoothTeleportFactory; import net.countercraft.movecraft.support.WorldHandlerFactory; import org.bukkit.plugin.java.JavaPlugin; -import org.int4.dirk.api.Injector; -import org.int4.dirk.di.Injectors; import org.jetbrains.annotations.NotNull; import java.util.logging.Logger; @@ -77,7 +74,7 @@ public class Movecraft extends JavaPlugin { private static Movecraft instance; private boolean shuttingDown; - private Injector injector; + private PluginBuilder.Application application; public static synchronized Movecraft getInstance() { return instance; @@ -86,14 +83,15 @@ public static synchronized Movecraft getInstance() { @Override public void onDisable() { shuttingDown = true; - injector.getInstance(ServiceHost.class).stopAll(); - injector = null; + application.host().stopAll(); + application = null; } @Override public void onEnable() { + var injector = PluginBuilder.create(); Logger logger = getLogger(); - injector = Injectors.manual(); + injector.registerInstance(logger); injector.registerInstance(this); injector.register(AsyncManager.class); @@ -161,12 +159,9 @@ public void onEnable() { injector.register(StatusManager.class); injector.register(StatusSign.class); - // Lifecycle management - injector.register(WorkerServiceHost.class); - injector.register(ListenerLifecycleService.class); - // Startup - injector.getInstance(ServiceHost.class).startAll(); + application = injector.build(); + application.host().startAll(); logger.info("[V %s] has been enabled.".formatted(getDescription().getVersion())); } @@ -177,19 +172,25 @@ public void onLoad() { saveDefaultConfig(); } + private record MovecraftAPI( + @NotNull WorldHandler worldHandler, + @NotNull SmoothTeleport smoothTeleport, + @NotNull AsyncManager asyncManager, + @NotNull WreckManager wreckManager) {} + public WorldHandler getWorldHandler(){ - return injector.getInstance(WorldHandler.class); + return application.container().getService(WorldHandler.class); } public SmoothTeleport getSmoothTeleport() { - return injector.getInstance(SmoothTeleport.class); + return application.container().getService(SmoothTeleport.class); } public AsyncManager getAsyncManager() { - return injector.getInstance(AsyncManager.class); + return application.container().getService(AsyncManager.class); } public @NotNull WreckManager getWreckManager(){ - return injector.getInstance(WreckManager.class); + return application.container().getService(WreckManager.class); } } diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java new file mode 100644 index 000000000..66afb3261 --- /dev/null +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java @@ -0,0 +1,67 @@ +package net.countercraft.movecraft.lifecycle; + +import org.int4.dirk.api.Injector; +import org.int4.dirk.di.Injectors; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Collection; + +public abstract class PluginBuilder { + private final Injector injector; + + protected PluginBuilder() { + injector = Injectors.manual(); + } + + @Contract("->new") + public static @NotNull PluginBuilder create(){ + return null; + } + + @Contract("_->this") + public @NotNull PluginBuilder register(Type type){ + injector.register(type); + + return this; + } + + @Contract("_->this") + public @NotNull PluginBuilder register(Collection types){ + injector.register(types); + + return this; + } + + @Contract("_,_->this") + public @NotNull PluginBuilder registerInstance(Object instance, Annotation... qualifiers){ + injector.registerInstance(instance, qualifiers); + + return this; + } + + @Contract("->new") + public @NotNull Application build(){ + // Lifecycle management + injector.register(WorkerServiceHost.class); + injector.register(ListenerLifecycleService.class); + + return new Application(injector.getInstance(ServiceHost.class), new ServiceProvider(injector)); + } + + public record Application(ServiceHost host, ServiceProvider container){} + + public static class ServiceProvider { + private final @NotNull Injector injector; + + private ServiceProvider(@NotNull Injector injector) { + this.injector = injector; + } + + public T getService(@NotNull Class cls){ + return injector.getInstance(cls); + } + } +} From 119650fde13cdba4f8ed76955ecf299915fdf736 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Mon, 2 Sep 2024 21:02:37 -0700 Subject: [PATCH 11/18] Rename service --- .../java/net/countercraft/movecraft/Movecraft.java | 9 ++++----- .../net/countercraft/movecraft/craft/CraftManager.java | 9 ++++----- .../movecraft/localisation/I18nSupport.java | 4 ++-- ...DataPackService.java => DataPackHostedService.java} | 6 +++--- ...SettingsService.java => SettingsHostedService.java} | 9 +++------ .../lifecycle/{Service.java => HostedService.java} | 2 +- ...ervice.java => ListenerLifecycleHostedService.java} | 4 ++-- .../movecraft/lifecycle/PluginBuilder.java | 4 ++-- .../countercraft/movecraft/lifecycle/ServiceHost.java | 10 +++++----- ...erServiceHost.java => WorkerHostedServiceHost.java} | 4 ++-- .../movecraft/processing/WorldManager.java | 4 ---- 11 files changed, 28 insertions(+), 37 deletions(-) rename api/src/main/java/net/countercraft/movecraft/config/{DataPackService.java => DataPackHostedService.java} (94%) rename api/src/main/java/net/countercraft/movecraft/config/{SettingsService.java => SettingsHostedService.java} (92%) rename api/src/main/java/net/countercraft/movecraft/lifecycle/{Service.java => HostedService.java} (75%) rename api/src/main/java/net/countercraft/movecraft/lifecycle/{ListenerLifecycleService.java => ListenerLifecycleHostedService.java} (76%) rename api/src/main/java/net/countercraft/movecraft/lifecycle/{WorkerServiceHost.java => WorkerHostedServiceHost.java} (87%) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index d93dd7652..e1c1b8b3d 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -28,9 +28,9 @@ import net.countercraft.movecraft.commands.ReleaseCommand; import net.countercraft.movecraft.commands.RotateCommand; import net.countercraft.movecraft.commands.ScuttleCommand; -import net.countercraft.movecraft.config.DataPackService; +import net.countercraft.movecraft.config.DataPackHostedService; import net.countercraft.movecraft.config.Settings; -import net.countercraft.movecraft.config.SettingsService; +import net.countercraft.movecraft.config.SettingsHostedService; import net.countercraft.movecraft.craft.ChunkManager; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.features.contacts.ContactsCommand; @@ -40,7 +40,6 @@ import net.countercraft.movecraft.features.status.StatusManager; import net.countercraft.movecraft.features.status.StatusSign; import net.countercraft.movecraft.lifecycle.PluginBuilder; -import net.countercraft.movecraft.lifecycle.ServiceHost; import net.countercraft.movecraft.listener.BlockListener; import net.countercraft.movecraft.listener.CraftPilotListener; import net.countercraft.movecraft.listener.CraftReleaseListener; @@ -101,7 +100,7 @@ public void onEnable() { injector.registerInstance(WorldManager.INSTANCE); injector.register(WreckManager.class); injector.register(I18nSupport.class); - injector.register(SettingsService.class); + injector.register(SettingsHostedService.class); // TODO: make this work somehow if(shuttingDown && Settings.IGNORE_RESET) { @@ -112,7 +111,7 @@ public void onEnable() { return; } - injector.register(DataPackService.class); + injector.register(DataPackHostedService.class); injector.register(CraftManager.class); //TODO: migrate to aikar or brigadier commands, left in place for now diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java index 176171883..6ba6c62b3 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java @@ -19,16 +19,15 @@ import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; -import net.countercraft.movecraft.config.DataPackService; +import net.countercraft.movecraft.config.DataPackHostedService; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.events.CraftReleaseEvent; import net.countercraft.movecraft.events.CraftSinkEvent; import net.countercraft.movecraft.events.TypesReloadedEvent; import net.countercraft.movecraft.exception.NonCancellableReleaseException; -import net.countercraft.movecraft.lifecycle.Service; +import net.countercraft.movecraft.lifecycle.HostedService; import net.countercraft.movecraft.localisation.I18nSupport; import net.countercraft.movecraft.processing.CachedMovecraftWorld; -import net.countercraft.movecraft.processing.MovecraftWorld; import net.countercraft.movecraft.processing.WorldManager; import net.countercraft.movecraft.processing.effects.Effect; import net.countercraft.movecraft.processing.functions.CraftSupplier; @@ -61,7 +60,7 @@ import static net.countercraft.movecraft.util.ChatUtils.ERROR_PREFIX; -public class CraftManager implements Iterable, Service { +public class CraftManager implements Iterable, HostedService { private static CraftManager instance; /** @@ -91,7 +90,7 @@ public static CraftManager getInstance() { @NotNull private Set craftTypes; - private CraftManager(@NotNull DataPackService dataPackService) { + private CraftManager(@NotNull DataPackHostedService dataPackService) { if(dataPackService.isDatapackInitialized()) craftTypes = loadCraftTypes(); else diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java b/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java index 4d4a601f9..04ff53adf 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java @@ -19,7 +19,7 @@ import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.config.Settings; -import net.countercraft.movecraft.lifecycle.Service; +import net.countercraft.movecraft.lifecycle.HostedService; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import org.bukkit.plugin.Plugin; @@ -36,7 +36,7 @@ import java.util.Properties; import java.util.logging.Level; -public class I18nSupport implements Service { +public class I18nSupport implements HostedService { private static Properties languageFile; private final @NotNull Plugin plugin; diff --git a/api/src/main/java/net/countercraft/movecraft/config/DataPackService.java b/api/src/main/java/net/countercraft/movecraft/config/DataPackHostedService.java similarity index 94% rename from api/src/main/java/net/countercraft/movecraft/config/DataPackService.java rename to api/src/main/java/net/countercraft/movecraft/config/DataPackHostedService.java index 979d4b041..f1bae5f66 100644 --- a/api/src/main/java/net/countercraft/movecraft/config/DataPackService.java +++ b/api/src/main/java/net/countercraft/movecraft/config/DataPackHostedService.java @@ -1,7 +1,7 @@ package net.countercraft.movecraft.config; import io.papermc.paper.datapack.Datapack; -import net.countercraft.movecraft.lifecycle.Service; +import net.countercraft.movecraft.lifecycle.HostedService; import org.bukkit.Server; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; @@ -11,12 +11,12 @@ import java.io.IOException; import java.util.logging.Logger; -public class DataPackService implements Service { +public class DataPackHostedService implements HostedService { private final @NotNull Plugin plugin; private final @NotNull Logger logger; private boolean isInitialized; - public DataPackService(@NotNull Plugin plugin, @NotNull Logger logger) { + public DataPackHostedService(@NotNull Plugin plugin, @NotNull Logger logger) { this.plugin = plugin; this.logger = logger; } diff --git a/api/src/main/java/net/countercraft/movecraft/config/SettingsService.java b/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java similarity index 92% rename from api/src/main/java/net/countercraft/movecraft/config/SettingsService.java rename to api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java index c1f9a6470..4e211d4c0 100644 --- a/api/src/main/java/net/countercraft/movecraft/config/SettingsService.java +++ b/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java @@ -1,13 +1,10 @@ package net.countercraft.movecraft.config; import jakarta.inject.Inject; -import jakarta.inject.Provider; -import net.countercraft.movecraft.lifecycle.Service; +import net.countercraft.movecraft.lifecycle.HostedService; import net.countercraft.movecraft.util.Tags; import org.bukkit.Material; import org.bukkit.configuration.Configuration; -import org.int4.dirk.api.Injector; -import org.int4.dirk.di.Injectors; import org.jetbrains.annotations.NotNull; import java.util.HashSet; @@ -15,12 +12,12 @@ import java.util.Set; import java.util.logging.Logger; -public class SettingsService implements Service { +public class SettingsHostedService implements HostedService { private final @NotNull Configuration configuration; private final @NotNull Logger logger; @Inject - public SettingsService(@NotNull Configuration configuration, @NotNull Logger logger) { + public SettingsHostedService(@NotNull Configuration configuration, @NotNull Logger logger) { this.configuration = configuration; this.logger = logger; } diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/Service.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/HostedService.java similarity index 75% rename from api/src/main/java/net/countercraft/movecraft/lifecycle/Service.java rename to api/src/main/java/net/countercraft/movecraft/lifecycle/HostedService.java index 543678977..1bc22730e 100644 --- a/api/src/main/java/net/countercraft/movecraft/lifecycle/Service.java +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/HostedService.java @@ -1,6 +1,6 @@ package net.countercraft.movecraft.lifecycle; -public interface Service { +public interface HostedService { default void start(){} default void stop(){} } diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleService.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleHostedService.java similarity index 76% rename from api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleService.java rename to api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleHostedService.java index 9d53931ec..18738bd78 100644 --- a/api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleService.java +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleHostedService.java @@ -7,12 +7,12 @@ import java.util.List; -public class ListenerLifecycleService implements Service { +public class ListenerLifecycleHostedService implements HostedService { private final @NotNull List listeners; private final @NotNull Plugin plugin; @Inject - public ListenerLifecycleService(@NotNull List listeners, @NotNull Plugin plugin){ + public ListenerLifecycleHostedService(@NotNull List listeners, @NotNull Plugin plugin){ this.listeners = listeners; this.plugin = plugin; } diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java index 66afb3261..ae0e145c5 100644 --- a/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java @@ -45,8 +45,8 @@ protected PluginBuilder() { @Contract("->new") public @NotNull Application build(){ // Lifecycle management - injector.register(WorkerServiceHost.class); - injector.register(ListenerLifecycleService.class); + injector.register(WorkerHostedServiceHost.class); + injector.register(ListenerLifecycleHostedService.class); return new Application(injector.getInstance(ServiceHost.class), new ServiceProvider(injector)); } diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/ServiceHost.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/ServiceHost.java index 03973f01b..e9c8e2c53 100644 --- a/api/src/main/java/net/countercraft/movecraft/lifecycle/ServiceHost.java +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/ServiceHost.java @@ -6,18 +6,18 @@ import java.util.List; public class ServiceHost { - private final @NotNull List services; + private final @NotNull List hostedServices; @Inject - public ServiceHost(@NotNull List services) { - this.services = services; + public ServiceHost(@NotNull List hostedServices) { + this.hostedServices = hostedServices; } public void startAll(){ - services.forEach(Service::start); + hostedServices.forEach(HostedService::start); } public void stopAll(){ - services.forEach(Service::stop); + hostedServices.forEach(HostedService::stop); } } diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerServiceHost.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHostedServiceHost.java similarity index 87% rename from api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerServiceHost.java rename to api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHostedServiceHost.java index b8c1ee5d1..6488c0459 100644 --- a/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerServiceHost.java +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHostedServiceHost.java @@ -7,12 +7,12 @@ import java.util.List; import java.util.stream.Collectors; -public class WorkerServiceHost implements Service { +public class WorkerHostedServiceHost implements HostedService { private final @NotNull Plugin plugin; private final @NotNull List workers; private @NotNull List tasks; - public WorkerServiceHost(@NotNull Plugin plugin, @NotNull List workers) { + public WorkerHostedServiceHost(@NotNull Plugin plugin, @NotNull List workers) { this.plugin = plugin; this.workers = workers; tasks = List.of(); diff --git a/api/src/main/java/net/countercraft/movecraft/processing/WorldManager.java b/api/src/main/java/net/countercraft/movecraft/processing/WorldManager.java index 894b20171..c48fc7fa4 100644 --- a/api/src/main/java/net/countercraft/movecraft/processing/WorldManager.java +++ b/api/src/main/java/net/countercraft/movecraft/processing/WorldManager.java @@ -1,13 +1,9 @@ package net.countercraft.movecraft.processing; -import jakarta.inject.Inject; -import net.countercraft.movecraft.lifecycle.Service; import net.countercraft.movecraft.lifecycle.Worker; import net.countercraft.movecraft.processing.effects.Effect; import net.countercraft.movecraft.util.CompletableFutureTask; import org.bukkit.Bukkit; -import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; From 614140c95f610c249f347afe21447f58f84dcfbc Mon Sep 17 00:00:00 2001 From: ohnoey Date: Mon, 2 Sep 2024 21:03:31 -0700 Subject: [PATCH 12/18] Simplify names --- ...LifecycleHostedService.java => ListenerHostedService.java} | 4 ++-- .../net/countercraft/movecraft/lifecycle/PluginBuilder.java | 4 ++-- .../{WorkerHostedServiceHost.java => WorkerHost.java} | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) rename api/src/main/java/net/countercraft/movecraft/lifecycle/{ListenerLifecycleHostedService.java => ListenerHostedService.java} (76%) rename api/src/main/java/net/countercraft/movecraft/lifecycle/{WorkerHostedServiceHost.java => WorkerHost.java} (87%) diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleHostedService.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerHostedService.java similarity index 76% rename from api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleHostedService.java rename to api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerHostedService.java index 18738bd78..db17198cb 100644 --- a/api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerLifecycleHostedService.java +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/ListenerHostedService.java @@ -7,12 +7,12 @@ import java.util.List; -public class ListenerLifecycleHostedService implements HostedService { +public class ListenerHostedService implements HostedService { private final @NotNull List listeners; private final @NotNull Plugin plugin; @Inject - public ListenerLifecycleHostedService(@NotNull List listeners, @NotNull Plugin plugin){ + public ListenerHostedService(@NotNull List listeners, @NotNull Plugin plugin){ this.listeners = listeners; this.plugin = plugin; } diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java index ae0e145c5..4904e0ee3 100644 --- a/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java @@ -45,8 +45,8 @@ protected PluginBuilder() { @Contract("->new") public @NotNull Application build(){ // Lifecycle management - injector.register(WorkerHostedServiceHost.class); - injector.register(ListenerLifecycleHostedService.class); + injector.register(WorkerHost.class); + injector.register(ListenerHostedService.class); return new Application(injector.getInstance(ServiceHost.class), new ServiceProvider(injector)); } diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHostedServiceHost.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHost.java similarity index 87% rename from api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHostedServiceHost.java rename to api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHost.java index 6488c0459..5be403896 100644 --- a/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHostedServiceHost.java +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHost.java @@ -7,12 +7,12 @@ import java.util.List; import java.util.stream.Collectors; -public class WorkerHostedServiceHost implements HostedService { +public class WorkerHost implements HostedService { private final @NotNull Plugin plugin; private final @NotNull List workers; private @NotNull List tasks; - public WorkerHostedServiceHost(@NotNull Plugin plugin, @NotNull List workers) { + public WorkerHost(@NotNull Plugin plugin, @NotNull List workers) { this.plugin = plugin; this.workers = workers; tasks = List.of(); From e5f8ba720fe9efa4bcc552cf73695e5ffff908ab Mon Sep 17 00:00:00 2001 From: ohnoey Date: Mon, 2 Sep 2024 21:12:33 -0700 Subject: [PATCH 13/18] split injection to seperate method --- .../net/countercraft/movecraft/Movecraft.java | 59 ++++++++++--------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index e1c1b8b3d..6acdd61c4 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -89,31 +89,17 @@ public void onDisable() { @Override public void onEnable() { var injector = PluginBuilder.create(); - Logger logger = getLogger(); - - injector.registerInstance(logger); - injector.registerInstance(this); - injector.register(AsyncManager.class); - injector.register(MapUpdateManager.class); - injector.register(SmoothTeleportFactory.class); - injector.register(WorldHandlerFactory.class); - injector.registerInstance(WorldManager.INSTANCE); - injector.register(WreckManager.class); - injector.register(I18nSupport.class); - injector.register(SettingsHostedService.class); + registerServices(injector); // TODO: make this work somehow if(shuttingDown && Settings.IGNORE_RESET) { - logger.severe("Movecraft is incompatible with the reload command. Movecraft has shut down and will restart when the server is restarted."); - logger.severe("If you wish to use the reload command and Movecraft, you may disable this check inside the config.yml by setting 'safeReload: false'"); + this.getLogger().severe("Movecraft is incompatible with the reload command. Movecraft has shut down and will restart when the server is restarted."); + this.getLogger().severe("If you wish to use the reload command and Movecraft, you may disable this check inside the config.yml by setting 'safeReload: false'"); getPluginLoader().disablePlugin(this); return; } - injector.register(DataPackHostedService.class); - injector.register(CraftManager.class); - //TODO: migrate to aikar or brigadier commands, left in place for now getCommand("movecraft").setExecutor(new MovecraftCommand()); getCommand("release").setExecutor(new ReleaseCommand()); @@ -126,11 +112,6 @@ public void onEnable() { getCommand("crafttype").setExecutor(new CraftTypeCommand()); getCommand("craftinfo").setExecutor(new CraftInfoCommand()); - injector.register(InteractListener.class); - injector.register(BlockListener.class); - injector.register(PlayerListener.class); - injector.register(ChunkManager.class); - //TODO: Sign rework getServer().getPluginManager().registerEvents(new AscendSign(), this); getServer().getPluginManager().registerEvents(new CraftSign(), this); @@ -148,16 +129,9 @@ public void onEnable() { getServer().getPluginManager().registerEvents(new TeleportSign(), this); getServer().getPluginManager().registerEvents(new ScuttleSign(), this); - injector.register(CraftPilotListener.class); - injector.register(CraftReleaseListener.class); - injector.register(ContactsManager.class); - injector.register(ContactsSign.class); getCommand("contacts").setExecutor(new ContactsCommand()); - injector.register(StatusManager.class); - injector.register(StatusSign.class); - // Startup application = injector.build(); application.host().startAll(); @@ -171,6 +145,33 @@ public void onLoad() { saveDefaultConfig(); } + private void registerServices(PluginBuilder injector){ + injector.registerInstance(getLogger()); + injector.registerInstance(this); + injector.register(AsyncManager.class); + injector.register(MapUpdateManager.class); + injector.register(SmoothTeleportFactory.class); + injector.register(WorldHandlerFactory.class); + injector.registerInstance(WorldManager.INSTANCE); + injector.register(WreckManager.class); + injector.register(I18nSupport.class); + injector.register(SettingsHostedService.class); + injector.register(DataPackHostedService.class); + injector.register(CraftManager.class); + injector.register(InteractListener.class); + injector.register(BlockListener.class); + injector.register(PlayerListener.class); + injector.register(ChunkManager.class); + injector.register(CraftPilotListener.class); + injector.register(CraftReleaseListener.class); + injector.register(ContactsManager.class); + injector.register(StatusManager.class); + + injector.register(StatusSign.class); + injector.register(ContactsSign.class); + + } + private record MovecraftAPI( @NotNull WorldHandler worldHandler, @NotNull SmoothTeleport smoothTeleport, From 4406d442ffcef6b46d2f1331b81ccf6d890417b0 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Mon, 2 Sep 2024 21:16:20 -0700 Subject: [PATCH 14/18] Simplify --- .../net/countercraft/movecraft/Movecraft.java | 28 ++----------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 6acdd61c4..6db7b0949 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -29,7 +29,6 @@ import net.countercraft.movecraft.commands.RotateCommand; import net.countercraft.movecraft.commands.ScuttleCommand; import net.countercraft.movecraft.config.DataPackHostedService; -import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.config.SettingsHostedService; import net.countercraft.movecraft.craft.ChunkManager; import net.countercraft.movecraft.craft.CraftManager; @@ -68,11 +67,8 @@ import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; -import java.util.logging.Logger; - public class Movecraft extends JavaPlugin { private static Movecraft instance; - private boolean shuttingDown; private PluginBuilder.Application application; public static synchronized Movecraft getInstance() { @@ -81,7 +77,6 @@ public static synchronized Movecraft getInstance() { @Override public void onDisable() { - shuttingDown = true; application.host().stopAll(); application = null; } @@ -91,15 +86,6 @@ public void onEnable() { var injector = PluginBuilder.create(); registerServices(injector); - // TODO: make this work somehow - if(shuttingDown && Settings.IGNORE_RESET) { - this.getLogger().severe("Movecraft is incompatible with the reload command. Movecraft has shut down and will restart when the server is restarted."); - this.getLogger().severe("If you wish to use the reload command and Movecraft, you may disable this check inside the config.yml by setting 'safeReload: false'"); - getPluginLoader().disablePlugin(this); - - return; - } - //TODO: migrate to aikar or brigadier commands, left in place for now getCommand("movecraft").setExecutor(new MovecraftCommand()); getCommand("release").setExecutor(new ReleaseCommand()); @@ -111,6 +97,7 @@ public void onEnable() { getCommand("scuttle").setExecutor(new ScuttleCommand()); getCommand("crafttype").setExecutor(new CraftTypeCommand()); getCommand("craftinfo").setExecutor(new CraftInfoCommand()); + getCommand("contacts").setExecutor(new ContactsCommand()); //TODO: Sign rework getServer().getPluginManager().registerEvents(new AscendSign(), this); @@ -129,13 +116,10 @@ public void onEnable() { getServer().getPluginManager().registerEvents(new TeleportSign(), this); getServer().getPluginManager().registerEvents(new ScuttleSign(), this); - - getCommand("contacts").setExecutor(new ContactsCommand()); - // Startup application = injector.build(); application.host().startAll(); - logger.info("[V %s] has been enabled.".formatted(getDescription().getVersion())); + getLogger().info("[V %s] has been enabled.".formatted(getDescription().getVersion())); } @Override @@ -167,17 +151,11 @@ private void registerServices(PluginBuilder injector){ injector.register(ContactsManager.class); injector.register(StatusManager.class); + // Signs injector.register(StatusSign.class); injector.register(ContactsSign.class); - } - private record MovecraftAPI( - @NotNull WorldHandler worldHandler, - @NotNull SmoothTeleport smoothTeleport, - @NotNull AsyncManager asyncManager, - @NotNull WreckManager wreckManager) {} - public WorldHandler getWorldHandler(){ return application.container().getService(WorldHandler.class); } From 49439f5f94649d10f42c70f80c05761b31551111 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Mon, 2 Sep 2024 21:26:32 -0700 Subject: [PATCH 15/18] implement builders --- .../net/countercraft/movecraft/Movecraft.java | 12 ++++------ .../movecraft/lifecycle/PluginBuilder.java | 24 +++++++++++++++---- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 6db7b0949..42bee7c1c 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -83,7 +83,7 @@ public void onDisable() { @Override public void onEnable() { - var injector = PluginBuilder.create(); + var injector = PluginBuilder.createFor(this); registerServices(injector); //TODO: migrate to aikar or brigadier commands, left in place for now @@ -129,9 +129,7 @@ public void onLoad() { saveDefaultConfig(); } - private void registerServices(PluginBuilder injector){ - injector.registerInstance(getLogger()); - injector.registerInstance(this); + private static void registerServices(PluginBuilder injector){ injector.register(AsyncManager.class); injector.register(MapUpdateManager.class); injector.register(SmoothTeleportFactory.class); @@ -156,15 +154,15 @@ private void registerServices(PluginBuilder injector){ injector.register(ContactsSign.class); } - public WorldHandler getWorldHandler(){ + public @NotNull WorldHandler getWorldHandler(){ return application.container().getService(WorldHandler.class); } - public SmoothTeleport getSmoothTeleport() { + public @NotNull SmoothTeleport getSmoothTeleport() { return application.container().getService(SmoothTeleport.class); } - public AsyncManager getAsyncManager() { + public @NotNull AsyncManager getAsyncManager() { return application.container().getService(AsyncManager.class); } diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java index 4904e0ee3..4841bddd8 100644 --- a/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.lifecycle; +import org.bukkit.plugin.Plugin; import org.int4.dirk.api.Injector; import org.int4.dirk.di.Injectors; import org.jetbrains.annotations.Contract; @@ -8,19 +9,33 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.Collection; +import java.util.logging.Logger; -public abstract class PluginBuilder { +public final class PluginBuilder { private final Injector injector; - protected PluginBuilder() { + private PluginBuilder() { injector = Injectors.manual(); } + @Contract("_->new") + public static @NotNull PluginBuilder createFor(@NotNull Plugin plugin){ + var builder = new PluginBuilder(); + builder.injector.registerInstance(plugin.getLogger()); + builder.injector.registerInstance(plugin); + + return builder; + } + @Contract("->new") - public static @NotNull PluginBuilder create(){ - return null; + public static @NotNull PluginBuilder createForTest(){ + var builder = new PluginBuilder(); + builder.injector.registerInstance(Logger.getLogger("movecraft-unit-test")); + + return builder; } + @SuppressWarnings("UnusedReturnValue") @Contract("_->this") public @NotNull PluginBuilder register(Type type){ injector.register(type); @@ -35,6 +50,7 @@ protected PluginBuilder() { return this; } + @SuppressWarnings("UnusedReturnValue") @Contract("_,_->this") public @NotNull PluginBuilder registerInstance(Object instance, Annotation... qualifiers){ injector.registerInstance(instance, qualifiers); From 1a02327722ebdce775a9c390bbfcaac6646343e4 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Mon, 2 Sep 2024 21:30:53 -0700 Subject: [PATCH 16/18] Seperate startup --- .../net/countercraft/movecraft/Movecraft.java | 45 +--------------- .../net/countercraft/movecraft/Startup.java | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 44 deletions(-) create mode 100644 Movecraft/src/main/java/net/countercraft/movecraft/Startup.java diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index 42bee7c1c..b882d8b9a 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -28,25 +28,9 @@ import net.countercraft.movecraft.commands.ReleaseCommand; import net.countercraft.movecraft.commands.RotateCommand; import net.countercraft.movecraft.commands.ScuttleCommand; -import net.countercraft.movecraft.config.DataPackHostedService; -import net.countercraft.movecraft.config.SettingsHostedService; -import net.countercraft.movecraft.craft.ChunkManager; -import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.features.contacts.ContactsCommand; -import net.countercraft.movecraft.features.contacts.ContactsManager; -import net.countercraft.movecraft.features.contacts.ContactsSign; import net.countercraft.movecraft.features.fading.WreckManager; -import net.countercraft.movecraft.features.status.StatusManager; -import net.countercraft.movecraft.features.status.StatusSign; import net.countercraft.movecraft.lifecycle.PluginBuilder; -import net.countercraft.movecraft.listener.BlockListener; -import net.countercraft.movecraft.listener.CraftPilotListener; -import net.countercraft.movecraft.listener.CraftReleaseListener; -import net.countercraft.movecraft.listener.InteractListener; -import net.countercraft.movecraft.listener.PlayerListener; -import net.countercraft.movecraft.localisation.I18nSupport; -import net.countercraft.movecraft.mapUpdater.MapUpdateManager; -import net.countercraft.movecraft.processing.WorldManager; import net.countercraft.movecraft.sign.AscendSign; import net.countercraft.movecraft.sign.CraftSign; import net.countercraft.movecraft.sign.CruiseSign; @@ -62,8 +46,6 @@ import net.countercraft.movecraft.sign.SpeedSign; import net.countercraft.movecraft.sign.SubcraftRotateSign; import net.countercraft.movecraft.sign.TeleportSign; -import net.countercraft.movecraft.support.SmoothTeleportFactory; -import net.countercraft.movecraft.support.WorldHandlerFactory; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; @@ -84,7 +66,7 @@ public void onDisable() { @Override public void onEnable() { var injector = PluginBuilder.createFor(this); - registerServices(injector); + Startup.registerServices(injector); //TODO: migrate to aikar or brigadier commands, left in place for now getCommand("movecraft").setExecutor(new MovecraftCommand()); @@ -129,31 +111,6 @@ public void onLoad() { saveDefaultConfig(); } - private static void registerServices(PluginBuilder injector){ - injector.register(AsyncManager.class); - injector.register(MapUpdateManager.class); - injector.register(SmoothTeleportFactory.class); - injector.register(WorldHandlerFactory.class); - injector.registerInstance(WorldManager.INSTANCE); - injector.register(WreckManager.class); - injector.register(I18nSupport.class); - injector.register(SettingsHostedService.class); - injector.register(DataPackHostedService.class); - injector.register(CraftManager.class); - injector.register(InteractListener.class); - injector.register(BlockListener.class); - injector.register(PlayerListener.class); - injector.register(ChunkManager.class); - injector.register(CraftPilotListener.class); - injector.register(CraftReleaseListener.class); - injector.register(ContactsManager.class); - injector.register(StatusManager.class); - - // Signs - injector.register(StatusSign.class); - injector.register(ContactsSign.class); - } - public @NotNull WorldHandler getWorldHandler(){ return application.container().getService(WorldHandler.class); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java b/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java new file mode 100644 index 000000000..f232dbad5 --- /dev/null +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java @@ -0,0 +1,52 @@ +package net.countercraft.movecraft; + +import net.countercraft.movecraft.async.AsyncManager; +import net.countercraft.movecraft.config.DataPackHostedService; +import net.countercraft.movecraft.config.SettingsHostedService; +import net.countercraft.movecraft.craft.ChunkManager; +import net.countercraft.movecraft.craft.CraftManager; +import net.countercraft.movecraft.features.contacts.ContactsManager; +import net.countercraft.movecraft.features.contacts.ContactsSign; +import net.countercraft.movecraft.features.fading.WreckManager; +import net.countercraft.movecraft.features.status.StatusManager; +import net.countercraft.movecraft.features.status.StatusSign; +import net.countercraft.movecraft.lifecycle.PluginBuilder; +import net.countercraft.movecraft.listener.BlockListener; +import net.countercraft.movecraft.listener.CraftPilotListener; +import net.countercraft.movecraft.listener.CraftReleaseListener; +import net.countercraft.movecraft.listener.InteractListener; +import net.countercraft.movecraft.listener.PlayerListener; +import net.countercraft.movecraft.localisation.I18nSupport; +import net.countercraft.movecraft.mapUpdater.MapUpdateManager; +import net.countercraft.movecraft.processing.WorldManager; +import net.countercraft.movecraft.support.SmoothTeleportFactory; +import net.countercraft.movecraft.support.WorldHandlerFactory; + +public class Startup { + public static void registerServices(PluginBuilder injector){ + injector + .register(AsyncManager.class) + .register(MapUpdateManager.class) + .register(SmoothTeleportFactory.class) + .register(WorldHandlerFactory.class) + .registerInstance(WorldManager.INSTANCE) + .register(WreckManager.class) + .register(I18nSupport.class) + .register(SettingsHostedService.class) + .register(DataPackHostedService.class) + .register(CraftManager.class) + .register(InteractListener.class) + .register(BlockListener.class) + .register(PlayerListener.class) + .register(ChunkManager.class) + .register(CraftPilotListener.class) + .register(CraftReleaseListener.class) + .register(ContactsManager.class) + .register(StatusManager.class); + + // Signs + injector + .register(StatusSign.class) + .register(ContactsSign.class); + } +} From 7c7a5f63ae8946a3c297c3cccbbbb4985d3f54f7 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Mon, 2 Sep 2024 22:42:50 -0700 Subject: [PATCH 17/18] Add tests, and fix some failures --- Movecraft/build.gradle.kts | 4 ++ .../net/countercraft/movecraft/Startup.java | 11 ++++- .../movecraft/async/AsyncManager.java | 6 +-- .../movecraft/craft/CraftManager.java | 5 +- .../features/fading/WreckManager.java | 2 + .../movecraft/localisation/I18nSupport.java | 23 +++++---- .../support/SmoothTeleportFactory.java | 2 + .../support/WorldHandlerFactory.java | 2 + .../countercraft/movecraft/StartupTest.java | 49 +++++++++++++++++++ .../config/DataPackHostedService.java | 2 + .../config/SettingsHostedService.java | 14 ++++-- .../movecraft/lifecycle/PluginBuilder.java | 9 +--- .../movecraft/lifecycle/WorkerHost.java | 2 + .../movecraft/support/VersionProvider.java | 2 + 14 files changed, 101 insertions(+), 32 deletions(-) create mode 100644 Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java diff --git a/Movecraft/build.gradle.kts b/Movecraft/build.gradle.kts index 683722e23..bc6b3b9e6 100644 --- a/Movecraft/build.gradle.kts +++ b/Movecraft/build.gradle.kts @@ -13,6 +13,10 @@ dependencies { runtimeOnly(project(":movecraft-v1_21", "reobf")) implementation(project(":movecraft-api")) compileOnly("org.yaml:snakeyaml:2.0") + testImplementation(libs.org.junit.jupiter.junit.jupiter.api) + testImplementation(libs.junit.junit) + testImplementation(libs.org.hamcrest.hamcrest.library) + testImplementation("org.mockito:mockito-core:5.13.0") } tasks.shadowJar { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java b/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java index f232dbad5..d59f3c6a0 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java @@ -20,13 +20,18 @@ import net.countercraft.movecraft.mapUpdater.MapUpdateManager; import net.countercraft.movecraft.processing.WorldManager; import net.countercraft.movecraft.support.SmoothTeleportFactory; +import net.countercraft.movecraft.support.VersionProvider; import net.countercraft.movecraft.support.WorldHandlerFactory; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; public class Startup { - public static void registerServices(PluginBuilder injector){ + @Contract("_->param1") + public static @NotNull PluginBuilder registerServices(@NotNull PluginBuilder injector){ injector - .register(AsyncManager.class) .register(MapUpdateManager.class) + .register(AsyncManager.class) + .register(VersionProvider.class) .register(SmoothTeleportFactory.class) .register(WorldHandlerFactory.class) .registerInstance(WorldManager.INSTANCE) @@ -48,5 +53,7 @@ public static void registerServices(PluginBuilder injector){ injector .register(StatusSign.class) .register(ContactsSign.class); + + return injector; } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java index 3edbe9cf4..fe995b768 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/async/AsyncManager.java @@ -36,14 +36,12 @@ import net.kyori.adventure.text.Component; import org.bukkit.World; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Set; import java.util.WeakHashMap; import java.util.concurrent.BlockingQueue; @@ -55,12 +53,10 @@ public class AsyncManager implements Worker { private final BlockingQueue finishedAlgorithms; private final Set clearanceSet; private final Map cooldownCache; - private final @NotNull Plugin plugin; private final @NotNull MapUpdateManager mapUpdateManager; @Inject - public AsyncManager(@NotNull Plugin plugin, @NotNull MapUpdateManager mapUpdateManager) { - this.plugin = Objects.requireNonNull(plugin); + public AsyncManager(@NotNull MapUpdateManager mapUpdateManager) { this.mapUpdateManager = mapUpdateManager; ownershipMap = new HashMap<>(); finishedAlgorithms = new LinkedBlockingQueue<>(); diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java index 6ba6c62b3..37477791e 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/craft/CraftManager.java @@ -17,6 +17,7 @@ package net.countercraft.movecraft.craft; +import jakarta.inject.Inject; import net.countercraft.movecraft.Movecraft; import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.config.DataPackHostedService; @@ -89,8 +90,8 @@ public static CraftManager getInstance() { */ @NotNull private Set craftTypes; - - private CraftManager(@NotNull DataPackHostedService dataPackService) { + @Inject + public CraftManager(@NotNull DataPackHostedService dataPackService) { if(dataPackService.isDatapackInitialized()) craftTypes = loadCraftTypes(); else diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/features/fading/WreckManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/features/fading/WreckManager.java index ee12262df..fd8f487ea 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/features/fading/WreckManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/features/fading/WreckManager.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.features.fading; +import jakarta.inject.Inject; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.processing.WorldManager; @@ -16,6 +17,7 @@ public class WreckManager { private final @NotNull WorldManager worldManager; + @Inject public WreckManager(@NotNull WorldManager worldManager){ this.worldManager = Objects.requireNonNull(worldManager); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java b/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java index 04ff53adf..a7931721d 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java @@ -17,7 +17,7 @@ package net.countercraft.movecraft.localisation; -import net.countercraft.movecraft.Movecraft; +import jakarta.inject.Inject; import net.countercraft.movecraft.config.Settings; import net.countercraft.movecraft.lifecycle.HostedService; import net.kyori.adventure.text.Component; @@ -34,16 +34,21 @@ import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.Properties; -import java.util.logging.Level; +import java.util.logging.Logger; public class I18nSupport implements HostedService { private static Properties languageFile; private final @NotNull Plugin plugin; + private final @NotNull Logger logger; - public I18nSupport(@NotNull Plugin plugin){ + @Inject + public I18nSupport(@NotNull Plugin plugin, @NotNull Logger logger){ this.plugin = plugin; + this.logger = logger; } + + @Override public void start() { String[] localisations = {"en", "cz", "nl", "fr"}; @@ -57,10 +62,10 @@ public void start() { init(); } - private static void init() { + private void init() { languageFile = new Properties(); - File localisationDirectory = new File(Movecraft.getInstance().getDataFolder().getAbsolutePath() + "/localisation"); + File localisationDirectory = new File(plugin.getDataFolder().getAbsolutePath() + "/localisation"); if (!localisationDirectory.exists()) { localisationDirectory.mkdirs(); @@ -70,13 +75,7 @@ private static void init() { try { inputStream = new FileInputStream(localisationDirectory.getAbsolutePath() + "/movecraftlang" + "_" + Settings.LOCALE + ".properties"); } catch (FileNotFoundException e) { - e.printStackTrace(); - } - - if (inputStream == null) { - Movecraft.getInstance().getLogger().log(Level.SEVERE, "Critical Error in Localisation System"); - Movecraft.getInstance().getServer().shutdown(); - return; + throw new IllegalStateException("Critical Error in Localisation System", e); } try { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/support/SmoothTeleportFactory.java b/Movecraft/src/main/java/net/countercraft/movecraft/support/SmoothTeleportFactory.java index 4ece5b3f2..c810566f5 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/support/SmoothTeleportFactory.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/support/SmoothTeleportFactory.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.support; +import jakarta.inject.Inject; import jakarta.inject.Provider; import net.countercraft.movecraft.SmoothTeleport; import net.countercraft.movecraft.config.Settings; @@ -12,6 +13,7 @@ public class SmoothTeleportFactory implements Provider { private final @NotNull Logger logger; private final @NotNull VersionInfo versionInfo; + @Inject public SmoothTeleportFactory(@NotNull Logger logger, @NotNull VersionInfo versionInfo) { this.logger = logger; this.versionInfo = versionInfo; diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/support/WorldHandlerFactory.java b/Movecraft/src/main/java/net/countercraft/movecraft/support/WorldHandlerFactory.java index 606bcf6ec..aaf9c4308 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/support/WorldHandlerFactory.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/support/WorldHandlerFactory.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.support; +import jakarta.inject.Inject; import jakarta.inject.Provider; import net.countercraft.movecraft.WorldHandler; import net.countercraft.movecraft.config.Settings; @@ -11,6 +12,7 @@ public class WorldHandlerFactory implements Provider { private final @NotNull Logger logger; private final @NotNull VersionInfo versionInfo; + @Inject public WorldHandlerFactory(@NotNull Logger logger, @NotNull VersionInfo versionInfo) { this.logger = logger; this.versionInfo = versionInfo; diff --git a/Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java b/Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java new file mode 100644 index 000000000..145a42f10 --- /dev/null +++ b/Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java @@ -0,0 +1,49 @@ +package net.countercraft.movecraft; + +import net.countercraft.movecraft.lifecycle.PluginBuilder; +import org.bukkit.Server; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginManager; +import org.bukkit.scheduler.BukkitScheduler; +import org.junit.Test; + +import java.util.logging.Logger; + +import static org.mockito.Mockito.*; + +public class StartupTest { + @Test + public void testInjection(){ + var host = buildTestApplication(); + } + + @Test + public void testStartStop(){ + var host = buildTestApplication(); + + host.host().startAll(); + host.host().stopAll(); + } + + private static PluginBuilder.Application buildTestApplication(){ + var pluginManager = mock(PluginManager.class); + var scheduler = mock(BukkitScheduler.class); + + var server = mock(Server.class); + when(server.getScheduler()).thenReturn(scheduler); + when(server.getPluginManager()).thenReturn(pluginManager); + + var plugin = mock(Plugin.class); + when(plugin.getLogger()).thenReturn(Logger.getLogger("movecraft-unit-test")); + when(plugin.getServer()).thenReturn(server); + + // Create builder + var builder = PluginBuilder.createFor(plugin); + + // Register plugin services + Startup.registerServices(builder); + + // Build + return builder.build(); + } +} diff --git a/api/src/main/java/net/countercraft/movecraft/config/DataPackHostedService.java b/api/src/main/java/net/countercraft/movecraft/config/DataPackHostedService.java index f1bae5f66..800c6be8b 100644 --- a/api/src/main/java/net/countercraft/movecraft/config/DataPackHostedService.java +++ b/api/src/main/java/net/countercraft/movecraft/config/DataPackHostedService.java @@ -1,6 +1,7 @@ package net.countercraft.movecraft.config; import io.papermc.paper.datapack.Datapack; +import jakarta.inject.Inject; import net.countercraft.movecraft.lifecycle.HostedService; import org.bukkit.Server; import org.bukkit.plugin.Plugin; @@ -16,6 +17,7 @@ public class DataPackHostedService implements HostedService { private final @NotNull Logger logger; private boolean isInitialized; + @Inject public DataPackHostedService(@NotNull Plugin plugin, @NotNull Logger logger) { this.plugin = plugin; this.logger = logger; diff --git a/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java b/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java index 4e211d4c0..20d8a9413 100644 --- a/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java +++ b/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java @@ -5,7 +5,10 @@ import net.countercraft.movecraft.util.Tags; import org.bukkit.Material; import org.bukkit.configuration.Configuration; +import org.bukkit.plugin.Plugin; +import org.int4.dirk.annotations.Opt; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashSet; import java.util.Map; @@ -13,17 +16,22 @@ import java.util.logging.Logger; public class SettingsHostedService implements HostedService { - private final @NotNull Configuration configuration; + private final @Nullable Configuration configuration; private final @NotNull Logger logger; @Inject - public SettingsHostedService(@NotNull Configuration configuration, @NotNull Logger logger) { - this.configuration = configuration; + public SettingsHostedService(@Nullable @Opt Plugin plugin, @NotNull Logger logger) { + this.configuration = plugin == null ? null : plugin.getConfig(); this.logger = logger; } @Override public void start() { + // TODO: Abstract away configuration to not need plugin + if(configuration == null){ + return; + } + Settings.LOCALE = configuration.getString("Locale"); Settings.Debug = configuration.getBoolean("Debug", false); Settings.DisableNMSCompatibilityCheck = configuration.getBoolean("IReallyKnowWhatIAmDoing", false); diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java index 4841bddd8..1084b09dd 100644 --- a/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/PluginBuilder.java @@ -27,14 +27,6 @@ private PluginBuilder() { return builder; } - @Contract("->new") - public static @NotNull PluginBuilder createForTest(){ - var builder = new PluginBuilder(); - builder.injector.registerInstance(Logger.getLogger("movecraft-unit-test")); - - return builder; - } - @SuppressWarnings("UnusedReturnValue") @Contract("_->this") public @NotNull PluginBuilder register(Type type){ @@ -63,6 +55,7 @@ private PluginBuilder() { // Lifecycle management injector.register(WorkerHost.class); injector.register(ListenerHostedService.class); + injector.register(ServiceHost.class); return new Application(injector.getInstance(ServiceHost.class), new ServiceProvider(injector)); } diff --git a/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHost.java b/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHost.java index 5be403896..48c62f625 100644 --- a/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHost.java +++ b/api/src/main/java/net/countercraft/movecraft/lifecycle/WorkerHost.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.lifecycle; +import jakarta.inject.Inject; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; @@ -12,6 +13,7 @@ public class WorkerHost implements HostedService { private final @NotNull List workers; private @NotNull List tasks; + @Inject public WorkerHost(@NotNull Plugin plugin, @NotNull List workers) { this.plugin = plugin; this.workers = workers; diff --git a/api/src/main/java/net/countercraft/movecraft/support/VersionProvider.java b/api/src/main/java/net/countercraft/movecraft/support/VersionProvider.java index 6a820773c..e1246ace6 100644 --- a/api/src/main/java/net/countercraft/movecraft/support/VersionProvider.java +++ b/api/src/main/java/net/countercraft/movecraft/support/VersionProvider.java @@ -1,5 +1,6 @@ package net.countercraft.movecraft.support; +import jakarta.inject.Inject; import jakarta.inject.Provider; import net.countercraft.movecraft.WorldHandler; import net.countercraft.movecraft.config.Settings; @@ -11,6 +12,7 @@ public class VersionProvider implements Provider { private final @NotNull Plugin plugin; + @Inject public VersionProvider(@NotNull Plugin plugin) { this.plugin = plugin; } From 3f4d01420b68964ca181f89b31a4ad0e1f7be1f6 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sat, 14 Sep 2024 20:04:58 -0700 Subject: [PATCH 18/18] Add tests, and fix some failures using them --- Movecraft/build.gradle.kts | 1 + .../net/countercraft/movecraft/Startup.java | 2 +- .../movecraft/localisation/I18nSupport.java | 14 +++++++---- .../countercraft/movecraft/StartupTest.java | 23 +++++++++++++++++-- .../config/SettingsHostedService.java | 11 +++------ 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/Movecraft/build.gradle.kts b/Movecraft/build.gradle.kts index bc6b3b9e6..14950e7b7 100644 --- a/Movecraft/build.gradle.kts +++ b/Movecraft/build.gradle.kts @@ -17,6 +17,7 @@ dependencies { testImplementation(libs.junit.junit) testImplementation(libs.org.hamcrest.hamcrest.library) testImplementation("org.mockito:mockito-core:5.13.0") + testImplementation("com.github.seeseemelk:MockBukkit-v1.18:2.85.2") } tasks.shadowJar { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java b/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java index d59f3c6a0..d1f058b41 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java @@ -32,12 +32,12 @@ public class Startup { .register(MapUpdateManager.class) .register(AsyncManager.class) .register(VersionProvider.class) + .register(SettingsHostedService.class) .register(SmoothTeleportFactory.class) .register(WorldHandlerFactory.class) .registerInstance(WorldManager.INSTANCE) .register(WreckManager.class) .register(I18nSupport.class) - .register(SettingsHostedService.class) .register(DataPackHostedService.class) .register(CraftManager.class) .register(InteractListener.class) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java b/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java index a7931721d..2167bb794 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java @@ -33,6 +33,8 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Properties; import java.util.logging.Logger; @@ -65,15 +67,17 @@ public void start() { private void init() { languageFile = new Properties(); - File localisationDirectory = new File(plugin.getDataFolder().getAbsolutePath() + "/localisation"); + Path localisationDirectory = plugin.getDataFolder().toPath().resolve("localisation"); - if (!localisationDirectory.exists()) { - localisationDirectory.mkdirs(); + try { + Files.createDirectories(localisationDirectory); + } catch (IOException e) { + throw new IllegalStateException("Critical Error in Localisation System", e); } - InputStream inputStream = null; + InputStream inputStream; try { - inputStream = new FileInputStream(localisationDirectory.getAbsolutePath() + "/movecraftlang" + "_" + Settings.LOCALE + ".properties"); + inputStream = new FileInputStream(localisationDirectory.resolve("movecraftlang_" + Settings.LOCALE + ".properties").toFile()); } catch (FileNotFoundException e) { throw new IllegalStateException("Critical Error in Localisation System", e); } diff --git a/Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java b/Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java index 145a42f10..9c5cc7349 100644 --- a/Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java +++ b/Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java @@ -1,12 +1,18 @@ package net.countercraft.movecraft; +import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock; +import io.papermc.paper.datapack.DatapackManager; import net.countercraft.movecraft.lifecycle.PluginBuilder; import org.bukkit.Server; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; -import org.bukkit.scheduler.BukkitScheduler; import org.junit.Test; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.UUID; import java.util.logging.Logger; import static org.mockito.Mockito.*; @@ -27,15 +33,28 @@ public void testStartStop(){ private static PluginBuilder.Application buildTestApplication(){ var pluginManager = mock(PluginManager.class); - var scheduler = mock(BukkitScheduler.class); + var scheduler = new BukkitSchedulerMock(); + var dataPackManager = mock(DatapackManager.class); var server = mock(Server.class); when(server.getScheduler()).thenReturn(scheduler); when(server.getPluginManager()).thenReturn(pluginManager); + when(server.getDatapackManager()).thenReturn(dataPackManager); + + Path directory; + try { + directory = Files.createTempDirectory(UUID.randomUUID().toString()); + directory.resolve("localisation").toFile().mkdir(); + directory.resolve("localisation").resolve("movecraftlang_null.properties").toFile().createNewFile(); + } catch (IOException e) { + throw new RuntimeException(e); + } var plugin = mock(Plugin.class); when(plugin.getLogger()).thenReturn(Logger.getLogger("movecraft-unit-test")); when(plugin.getServer()).thenReturn(server); + when(plugin.getDataFolder()).thenReturn(directory.toFile()); + when(plugin.getConfig()).thenReturn(mock(FileConfiguration.class)); // Create builder var builder = PluginBuilder.createFor(plugin); diff --git a/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java b/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java index 20d8a9413..6ea4fbb8a 100644 --- a/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java +++ b/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java @@ -16,22 +16,17 @@ import java.util.logging.Logger; public class SettingsHostedService implements HostedService { - private final @Nullable Configuration configuration; + private final @NotNull Configuration configuration; private final @NotNull Logger logger; @Inject - public SettingsHostedService(@Nullable @Opt Plugin plugin, @NotNull Logger logger) { - this.configuration = plugin == null ? null : plugin.getConfig(); + public SettingsHostedService(@NotNull Plugin plugin, @NotNull Logger logger) { + this.configuration = plugin.getConfig(); this.logger = logger; } @Override public void start() { - // TODO: Abstract away configuration to not need plugin - if(configuration == null){ - return; - } - Settings.LOCALE = configuration.getString("Locale"); Settings.Debug = configuration.getBoolean("Debug", false); Settings.DisableNMSCompatibilityCheck = configuration.getBoolean("IReallyKnowWhatIAmDoing", false);