Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Dependency Injection for service initialization #707

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
5 changes: 5 additions & 0 deletions Movecraft/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ 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")
testImplementation("com.github.seeseemelk:MockBukkit-v1.18:2.85.2")
}

tasks.shadowJar {
Expand Down
304 changes: 45 additions & 259 deletions Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions Movecraft/src/main/java/net/countercraft/movecraft/Startup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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.VersionProvider;
import net.countercraft.movecraft.support.WorldHandlerFactory;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public class Startup {
@Contract("_->param1")
public static @NotNull PluginBuilder registerServices(@NotNull PluginBuilder injector){
injector
.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(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);

return injector;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,11 +31,11 @@
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;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
Expand All @@ -47,13 +48,21 @@
import java.util.concurrent.LinkedBlockingQueue;

@Deprecated
public class AsyncManager extends BukkitRunnable {
private final Map<AsyncTask, Craft> ownershipMap = new HashMap<>();
private final BlockingQueue<AsyncTask> finishedAlgorithms = new LinkedBlockingQueue<>();
private final Set<Craft> clearanceSet = new HashSet<>();
private final Map<Craft, Integer> cooldownCache = new WeakHashMap<>();

public AsyncManager() {}
public class AsyncManager implements Worker {
private final Map<AsyncTask, Craft> ownershipMap;
private final BlockingQueue<AsyncTask> finishedAlgorithms;
private final Set<Craft> clearanceSet;
private final Map<Craft, Integer> cooldownCache;
private final @NotNull MapUpdateManager mapUpdateManager;

@Inject
public AsyncManager(@NotNull MapUpdateManager mapUpdateManager) {
this.mapUpdateManager = mapUpdateManager;
ownershipMap = new HashMap<>();
finishedAlgorithms = new LinkedBlockingQueue<>();
clearanceSet = new HashSet<>();
cooldownCache = new WeakHashMap<>();
}

public void submitTask(AsyncTask task, Craft c) {
if (c.isNotProcessing()) {
Expand Down Expand Up @@ -120,14 +129,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());
Expand All @@ -153,7 +162,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());
Expand Down Expand Up @@ -312,6 +321,16 @@ private void processSinking() {
}
}

@Override
public boolean isAsync() {
return false;
}

@Override
public int getPeriod() {
return 1;
}

public void run() {
clearAll();

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +20,10 @@

@Deprecated
public class ChunkManager implements Listener {


@Inject
public ChunkManager(){}

private static final Set<MovecraftChunk> chunks = new HashSet<>();

public static void addChunksToLoad(Iterable<MovecraftChunk> list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@

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;
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.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;
Expand Down Expand Up @@ -59,19 +61,17 @@

import static net.countercraft.movecraft.util.ChatUtils.ERROR_PREFIX;

public class CraftManager implements Iterable<Craft>{
public class CraftManager implements Iterable<Craft>, HostedService {
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.
Expand All @@ -90,14 +90,20 @@ public static void initialize(boolean loadCraftTypes) {
*/
@NotNull private Set<CraftType> craftTypes;


private CraftManager(boolean loadCraftTypes) {
if(loadCraftTypes)
@Inject
public CraftManager(@NotNull DataPackHostedService dataPackService) {
if(dataPackService.isDatapackInitialized())
craftTypes = loadCraftTypes();
else
craftTypes = new HashSet<>();
}


@Override
public void start() {
instance = this;
}

@NotNull
private Set<CraftType> loadCraftTypes() {
File craftsFile = new File(Movecraft.getInstance().getDataFolder().getAbsolutePath() + "/types");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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;
import net.countercraft.movecraft.craft.datatag.CraftDataTagKey;
import net.countercraft.movecraft.craft.datatag.CraftDataTagRegistry;
import net.countercraft.movecraft.craft.type.CraftType;
import net.countercraft.movecraft.events.*;
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;
Expand All @@ -21,14 +22,28 @@
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;

import java.util.*;

public class ContactsManager extends BukkitRunnable implements Listener {
public class ContactsManager implements Listener, Worker {
private static final CraftDataTagKey<Map<Craft, Long>> RECENT_CONTACTS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "recent-contacts"), craft -> new WeakHashMap<>());
private final @NotNull CraftManager craftManager;
@Inject
public ContactsManager(@NotNull CraftManager craftManager){
this.craftManager = craftManager;
}

@Override
public boolean isAsync() {
return true;
}

@Override
public int getPeriod() {
return 20;
}

@Override
public void run() {
Expand All @@ -41,7 +56,7 @@ private void runContacts() {
if (w == null)
continue;

Set<Craft> craftsInWorld = CraftManager.getInstance().getCraftsInWorld(w);
Set<Craft> craftsInWorld = craftManager.getCraftsInWorld(w);
for (Craft base : craftsInWorld) {
if (base instanceof SinkingCraft || base instanceof SubCraft)
continue;
Expand Down Expand Up @@ -122,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;

Expand Down Expand Up @@ -234,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<Craft> contacts = other.getDataTag(Craft.CONTACTS);
if (contacts.contains(base))
continue;
Expand All @@ -243,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<Craft, Long> recentContacts = other.getDataTag(RECENT_CONTACTS);
if (!recentContacts.containsKey(other))
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +17,7 @@
public class WreckManager {
private final @NotNull WorldManager worldManager;

@Inject
public WreckManager(@NotNull WorldManager worldManager){
this.worldManager = Objects.requireNonNull(worldManager);
}
Expand Down
Loading