aboutsummaryrefslogtreecommitdiff
path: root/spark-forge
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2020-08-30 11:09:14 +0100
committerLuck <git@lucko.me>2020-08-30 11:09:14 +0100
commitb796134dcd9459945a967684cb4d851a13e6d88e (patch)
tree7f262d68c8dc35e496fb25cc896b80290f048157 /spark-forge
parent2b1a02941be9ee10c1a6d6b77032c3e2bf256e65 (diff)
downloadspark-b796134dcd9459945a967684cb4d851a13e6d88e.tar.gz
spark-b796134dcd9459945a967684cb4d851a13e6d88e.tar.bz2
spark-b796134dcd9459945a967684cb4d851a13e6d88e.zip
Refactor forge mod construction, remove static singleton field
Diffstat (limited to 'spark-forge')
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java12
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java8
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java51
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java4
4 files changed, 44 insertions, 31 deletions
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java
index b6d166b..4fa0274 100644
--- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java
@@ -30,6 +30,7 @@ import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
+import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.loading.FMLPaths;
import net.minecraftforge.fml.network.FMLNetworkConstants;
@@ -42,12 +43,10 @@ public class ForgeSparkMod {
private ModContainer container;
private Path configDirectory;
- public static ForgeSparkMod mod = null;
public ForgeSparkMod() {
- mod = this;
FMLJavaModLoadingContext.get().getModEventBus().register(this);
- MinecraftForge.EVENT_BUS.register(ForgeServerSparkPlugin.class);
+ MinecraftForge.EVENT_BUS.register(this);
ModLoadingContext.get().registerExtensionPoint(ExtensionPoint.DISPLAYTEST, () -> Pair.of(() -> FMLNetworkConstants.IGNORESERVERONLY, (a, b) -> true));
}
@@ -62,8 +61,13 @@ public class ForgeSparkMod {
}
@SubscribeEvent
+ public void serverInit(FMLServerStartingEvent e) {
+ ForgeServerSparkPlugin.register(this, e);
+ }
+
+ @SubscribeEvent
public void clientInit(FMLClientSetupEvent e) {
- ForgeClientSparkPlugin.register(e);
+ ForgeClientSparkPlugin.register(this, e);
}
public Path getConfigDirectory() {
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java
index 83c448a..66fd3ab 100644
--- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java
@@ -53,10 +53,10 @@ import java.util.stream.Stream;
public class ForgeClientSparkPlugin extends ForgeSparkPlugin implements SuggestionProvider<ISuggestionProvider> {
- public static void register(FMLClientSetupEvent event) {
+ public static void register(ForgeSparkMod mod, FMLClientSetupEvent event) {
Minecraft minecraft = event.getMinecraftSupplier().get();
- ForgeClientSparkPlugin plugin = new ForgeClientSparkPlugin(minecraft);
+ ForgeClientSparkPlugin plugin = new ForgeClientSparkPlugin(mod, minecraft);
MinecraftForge.EVENT_BUS.register(plugin);
plugin.scheduler.scheduleWithFixedDelay(plugin::checkCommandRegistered, 10, 10, TimeUnit.SECONDS);
@@ -65,8 +65,8 @@ public class ForgeClientSparkPlugin extends ForgeSparkPlugin implements Suggesti
private final Minecraft minecraft;
private CommandDispatcher<ISuggestionProvider> dispatcher;
- public ForgeClientSparkPlugin(Minecraft minecraft) {
- super();
+ public ForgeClientSparkPlugin(ForgeSparkMod mod, Minecraft minecraft) {
+ super(mod);
this.minecraft = minecraft;
}
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java
index b7285b4..c7a4475 100644
--- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java
@@ -32,6 +32,7 @@ import me.lucko.spark.common.sampler.tick.TickHook;
import me.lucko.spark.common.sampler.tick.TickReporter;
import me.lucko.spark.forge.ForgeCommandSender;
import me.lucko.spark.forge.ForgePlatformInfo;
+import me.lucko.spark.forge.ForgeSparkMod;
import me.lucko.spark.forge.ForgeTickHook;
import me.lucko.spark.forge.ForgeTickReporter;
import net.minecraft.command.CommandSource;
@@ -39,50 +40,49 @@ import net.minecraft.command.ICommandSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.server.MinecraftServer;
+import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
-import net.minecraftforge.fml.server.ServerLifecycleHooks;
+import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.server.permission.DefaultPermissionLevel;
import net.minecraftforge.server.permission.PermissionAPI;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
-import java.util.function.Supplier;
import java.util.stream.Stream;
public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command<CommandSource>, SuggestionProvider<CommandSource> {
- @SubscribeEvent
- public static void register(RegisterCommandsEvent event) {
- ForgeServerSparkPlugin plugin = new ForgeServerSparkPlugin(ServerLifecycleHooks::getCurrentServer);
- CommandDispatcher<CommandSource> dispatcher = event.getDispatcher();
- registerCommands(dispatcher, plugin, plugin, "spark");
+ public static void register(ForgeSparkMod mod, FMLServerStartingEvent event) {
+ final MinecraftServer minecraftServer = event.getServer();
+
+ ForgeServerSparkPlugin plugin = new ForgeServerSparkPlugin(mod, minecraftServer);
+ MinecraftForge.EVENT_BUS.register(plugin);
+
PermissionAPI.registerNode("spark", DefaultPermissionLevel.OP, "Access to the spark command");
}
+ private final MinecraftServer server;
- private final Supplier<MinecraftServer> server;
-
- public ForgeServerSparkPlugin(Supplier<MinecraftServer> server) {
- super();
+ public ForgeServerSparkPlugin(ForgeSparkMod mod, MinecraftServer server) {
+ super(mod);
this.server = server;
}
- private static String /* Nullable */[] processArgs(CommandContext<CommandSource> context) {
- String[] split = context.getInput().split(" ");
- if (split.length == 0 || !split[0].equals("/spark") && !split[0].equals("spark")) {
- return null;
- }
-
- return Arrays.copyOfRange(split, 1, split.length);
+ @SubscribeEvent
+ public void onRegisterCommands(RegisterCommandsEvent e) {
+ CommandDispatcher<CommandSource> dispatcher = e.getDispatcher();
+ registerCommands(dispatcher, this, this, "spark");
}
@Override
public int run(CommandContext<CommandSource> context) throws CommandSyntaxException {
String[] args = processArgs(context);
- if (args == null)
+ if (args == null) {
return 0;
+ }
+
this.platform.executeCommand(new ForgeCommandSender(context.getSource().source, this), args);
return Command.SINGLE_SUCCESS;
}
@@ -103,6 +103,15 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command<
});
}
+ private static String /* Nullable */[] processArgs(CommandContext<CommandSource> context) {
+ String[] split = context.getInput().split(" ");
+ if (split.length == 0 || !split[0].equals("/spark") && !split[0].equals("spark")) {
+ return null;
+ }
+
+ return Arrays.copyOfRange(split, 1, split.length);
+ }
+
@Override
public boolean hasPermission(ICommandSource sender, String permission) {
if (sender instanceof PlayerEntity) {
@@ -115,8 +124,8 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command<
@Override
public Stream<ForgeCommandSender> getSendersWithPermission(String permission) {
return Stream.concat(
- this.server.get().getPlayerList().getPlayers().stream().filter(player -> hasPermission(player, permission)),
- Stream.of(this.server.get())
+ this.server.getPlayerList().getPlayers().stream().filter(player -> hasPermission(player, permission)),
+ Stream.of(this.server)
).map(sender -> new ForgeCommandSender(sender, this));
}
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java
index d7ef386..920ca24 100644
--- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java
@@ -63,8 +63,8 @@ public abstract class ForgeSparkPlugin implements SparkPlugin {
protected final ScheduledExecutorService scheduler;
protected final SparkPlatform platform;
- protected ForgeSparkPlugin() {
- this.mod = ForgeSparkMod.mod;
+ protected ForgeSparkPlugin(ForgeSparkMod mod) {
+ this.mod = mod;
this.scheduler = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder().setNameFormat("spark-forge-async-worker").build()
);