Skip to content

Commit

Permalink
Use dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
oh-noey committed Sep 1, 2024
1 parent 61c79dd commit cc6fdce
Show file tree
Hide file tree
Showing 41 changed files with 666 additions and 269 deletions.
294 changes: 62 additions & 232 deletions Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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 class AsyncManager extends BukkitRunnable implements Service {
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 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()) {
Expand Down Expand Up @@ -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());
Expand All @@ -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());
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,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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,6 +11,8 @@
import org.jetbrains.annotations.NotNull;

public class CraftReleaseListener implements Listener {
@Inject
public CraftReleaseListener(){}

@EventHandler
public void onDisassembly(@NotNull CraftReleaseEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,7 +38,12 @@
import java.util.WeakHashMap;

public final class InteractListener implements Listener {
private final Map<Player, Long> timeMap = new WeakHashMap<>();
private final Map<Player, Long> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,7 +43,12 @@
import java.util.WeakHashMap;

public class PlayerListener implements Listener {
private final Map<Craft, Long> timeToReleaseAfter = new WeakHashMap<>();
private final Map<Craft, Long> timeToReleaseAfter;

@Inject
public PlayerListener() {
timeToReleaseAfter = new WeakHashMap<>();
}

private Set<Location> checkCraftBorders(Craft craft) {
Set<Location> mergePoints = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

Expand All @@ -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<UpdateCommand> updates;
private final @NotNull Plugin plugin;

private final Queue<UpdateCommand> updates = new ConcurrentLinkedQueue<>();
// private final Queue<UpdateCommand> updates = new LinkedBlockingQueue<>();
//private PriorityQueue<UpdateCommand> updateQueue = new PriorityQueue<>();

//@Deprecated
//public HashMap<Craft, Integer> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,6 +21,8 @@
import org.jetbrains.annotations.NotNull;

public class AscendSign implements Listener {
@Inject
public AscendSign(){}

@EventHandler
public void onCraftDetect(CraftDetectEvent event){
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,7 +38,12 @@
import java.util.Set;

public final class CraftSign implements Listener {
private final Set<MovecraftLocation> piloting = new HashSet<>();
private final Set<MovecraftLocation> piloting;

@Inject
public CraftSign() {
piloting = new HashSet<>();
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSignChange(@NotNull SignChangeEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,6 +20,8 @@
import org.jetbrains.annotations.NotNull;

public final class DescendSign implements Listener{
@Inject
public DescendSign(){}

@EventHandler
public void onCraftDetect(CraftDetectEvent event){
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,6 +19,8 @@
import org.jetbrains.annotations.NotNull;

public final class HelmSign implements Listener {
@Inject
public HelmSign(){}

@EventHandler
public void onSignChange(SignChangeEvent event){
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down
Loading

0 comments on commit cc6fdce

Please sign in to comment.