diff options
author | liach <liach@users.noreply.github.com> | 2020-01-12 14:19:18 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2020-01-12 14:19:44 +0000 |
commit | fb53db0025a883bd9b798ddef13196a0b3d05e75 (patch) | |
tree | c76f7f7f441aa5e0d0cd49a363549b8bd284ab86 /spark-fabric/src/main/java | |
parent | 2fe79f4140011f93df1567afcc9b75fababa187e (diff) | |
download | spark-fb53db0025a883bd9b798ddef13196a0b3d05e75.tar.gz spark-fb53db0025a883bd9b798ddef13196a0b3d05e75.tar.bz2 spark-fb53db0025a883bd9b798ddef13196a0b3d05e75.zip |
Update fabric mod to 1.15.1
Diffstat (limited to 'spark-fabric/src/main/java')
8 files changed, 66 insertions, 187 deletions
diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkGameHooks.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkGameHooks.java index d046ab8..5eec53d 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkGameHooks.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkGameHooks.java @@ -20,16 +20,11 @@ package me.lucko.spark.fabric; -import java.util.HashSet; -import java.util.Set; import java.util.function.Predicate; public enum FabricSparkGameHooks { INSTANCE; - private final Set<FabricTickCounter> clientCounters = new HashSet<>(); - private final Set<FabricTickCounter> serverCounters = new HashSet<>(); - // Use events from Fabric API later // Return true to abort sending to server private Predicate<String> chatSendCallback = s -> false; @@ -42,32 +37,4 @@ public enum FabricSparkGameHooks { return this.chatSendCallback.test(message); } - public void addClientCounter(FabricTickCounter counter) { - this.clientCounters.add(counter); - } - - public void removeClientCounter(FabricTickCounter counter) { - this.clientCounters.remove(counter); - } - - public void addServerCounter(FabricTickCounter counter) { - this.serverCounters.add(counter); - } - - public void removeServerCounter(FabricTickCounter counter) { - this.serverCounters.remove(counter); - } - - public void tickClientCounters() { - for (FabricTickCounter counter : this.clientCounters) { - counter.onTick(); - } - } - - public void tickServerCounters() { - for (FabricTickCounter counter : this.serverCounters) { - counter.onTick(); - } - } - } diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java index 0f6fb1f..780cedd 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java @@ -20,19 +20,19 @@ package me.lucko.spark.fabric; +import me.lucko.spark.fabric.plugin.FabricClientSparkPlugin; +import me.lucko.spark.fabric.plugin.FabricServerSparkPlugin; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.event.server.ServerStartCallback; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; +import net.minecraft.client.MinecraftClient; import java.nio.file.Path; public class FabricSparkMod implements ModInitializer { private static FabricSparkMod mod; - public static FabricSparkMod getMod() { - return mod; - } - private ModContainer container; private Path configDirectory; @@ -44,6 +44,14 @@ public class FabricSparkMod implements ModInitializer { this.container = loader.getModContainer("spark") .orElseThrow(() -> new IllegalStateException("Unable to get container for spark")); this.configDirectory = loader.getConfigDirectory().toPath().resolve("spark"); + + // load hooks + ServerStartCallback.EVENT.register(server -> FabricServerSparkPlugin.register(this, server)); + } + + // called be entrypoint defined in fabric.mod.json + public static void initializeClient() { + FabricClientSparkPlugin.register(FabricSparkMod.mod, MinecraftClient.getInstance()); } public String getVersion() { diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickCounter.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickCounter.java index ce58dcf..7781e03 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickCounter.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickCounter.java @@ -22,35 +22,45 @@ package me.lucko.spark.fabric; import me.lucko.spark.common.sampler.AbstractTickCounter; import me.lucko.spark.common.sampler.TickCounter; +import net.fabricmc.fabric.api.event.client.ClientTickCallback; +import net.fabricmc.fabric.api.event.server.ServerTickCallback; +import net.minecraft.client.MinecraftClient; +import net.minecraft.server.MinecraftServer; public abstract class FabricTickCounter extends AbstractTickCounter implements TickCounter { + protected boolean closed = false; + @Override - public void onTick() { - super.onTick(); + public void close() { + this.closed = true; } - public static final class Server extends FabricTickCounter { + public static final class Server extends FabricTickCounter implements ServerTickCallback { @Override - public void start() { - FabricSparkGameHooks.INSTANCE.addServerCounter(this); + public void tick(MinecraftServer minecraftServer) { + if (!this.closed) { + onTick(); + } } @Override - public void close() { - FabricSparkGameHooks.INSTANCE.removeServerCounter(this); + public void start() { + ServerTickCallback.EVENT.register(this); } } - public static final class Client extends FabricTickCounter { + public static final class Client extends FabricTickCounter implements ClientTickCallback { @Override - public void start() { - FabricSparkGameHooks.INSTANCE.addClientCounter(this); + public void tick(MinecraftClient minecraftClient) { + if (!this.closed) { + onTick(); + } } @Override - public void close() { - FabricSparkGameHooks.INSTANCE.removeClientCounter(this); + public void start() { + ClientTickCallback.EVENT.register(this); } } } diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/MinecraftClientMixin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/MinecraftClientMixin.java deleted file mode 100644 index ae7ef5f..0000000 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/MinecraftClientMixin.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) <luck@lucko.me> - * Copyright (c) contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -package me.lucko.spark.fabric.mixin; - -import me.lucko.spark.fabric.FabricSparkGameHooks; -import me.lucko.spark.fabric.FabricSparkMod; -import me.lucko.spark.fabric.plugin.FabricClientSparkPlugin; -import net.minecraft.client.MinecraftClient; -import net.minecraft.util.NonBlockingThreadExecutor; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(MinecraftClient.class) -public abstract class MinecraftClientMixin extends NonBlockingThreadExecutor<Runnable> { - - public MinecraftClientMixin(String string_1) { - super(string_1); - } - - // Inject at when menu pops up - @Inject(method = "init()V", at = @At(value = "INVOKE", - target = "Lnet/minecraft/client/gui/screen/SplashScreen;method_18819(Lnet/minecraft/client/MinecraftClient;)V")) - public void onInit(CallbackInfo ci) { - FabricClientSparkPlugin.register(FabricSparkMod.getMod(), (MinecraftClient) (Object) this); - } - - @Inject(method = "tick()V", at = @At("RETURN")) - public void onTick(CallbackInfo ci) { - FabricSparkGameHooks.INSTANCE.tickClientCounters(); - } - -} diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/MinecraftServerMixin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/MinecraftServerMixin.java deleted file mode 100644 index 4e3a0ab..0000000 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/MinecraftServerMixin.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) <luck@lucko.me> - * Copyright (c) contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -package me.lucko.spark.fabric.mixin; - -import me.lucko.spark.fabric.FabricSparkGameHooks; -import me.lucko.spark.fabric.FabricSparkMod; -import me.lucko.spark.fabric.plugin.FabricServerSparkPlugin; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.ServerTask; -import net.minecraft.util.NonBlockingThreadExecutor; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(MinecraftServer.class) -public abstract class MinecraftServerMixin extends NonBlockingThreadExecutor<ServerTask> { - - public MinecraftServerMixin(String string_1) { - super(string_1); - } - - // Inject before set favicon call - @Inject(method = "run()V", - at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;setFavicon(Lnet/minecraft/server/ServerMetadata;)V")) - public void onRun(CallbackInfo ci) { - FabricServerSparkPlugin.register(FabricSparkMod.getMod(), (MinecraftServer) (Object) this); - } - - @Inject(method = "tick(Ljava/util/function/BooleanSupplier;)V", at = @At("RETURN")) - public void onTick(CallbackInfo ci) { - FabricSparkGameHooks.INSTANCE.tickServerCounters(); - } - -} diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java index 2541f7d..8cacc62 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java @@ -34,11 +34,11 @@ import me.lucko.spark.fabric.FabricSparkMod; import me.lucko.spark.fabric.FabricTickCounter; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; -import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.server.command.CommandOutput; import net.minecraft.server.command.CommandSource; import java.util.Arrays; +import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.stream.Stream; @@ -47,7 +47,6 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin implements Sugges public static void register(FabricSparkMod mod, MinecraftClient client) { FabricClientSparkPlugin plugin = new FabricClientSparkPlugin(mod, client); - plugin.scheduler.scheduleWithFixedDelay(plugin::checkCommandRegistered, 10, 10, TimeUnit.SECONDS); } @@ -59,19 +58,20 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin implements Sugges this.minecraft = minecraft; } - private void checkCommandRegistered() { - ClientPlayerEntity player = this.minecraft.player; - if (player == null) { - return; - } + private CommandDispatcher<CommandSource> getPlayerCommandDispatcher() { + return Optional.ofNullable(this.minecraft.player) + .map(player -> player.networkHandler) + .map(ClientPlayNetworkHandler::getCommandDispatcher) + .orElse(null); + } - ClientPlayNetworkHandler connection = player.networkHandler; - if (connection == null) { + private void checkCommandRegistered() { + CommandDispatcher<CommandSource> dispatcher = getPlayerCommandDispatcher(); + if (dispatcher == null) { return; } try { - CommandDispatcher<CommandSource> dispatcher = connection.getCommandDispatcher(); if (dispatcher != this.dispatcher) { this.dispatcher = dispatcher; registerCommands(this.dispatcher, c -> Command.SINGLE_SUCCESS, this, "sparkc", "sparkclient"); diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java index 76568c3..a75b2aa 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java @@ -21,7 +21,6 @@ package me.lucko.spark.fabric.plugin; import com.mojang.brigadier.Command; -import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.SuggestionProvider; @@ -31,6 +30,7 @@ import me.lucko.spark.common.sampler.TickCounter; import me.lucko.spark.fabric.FabricCommandSender; import me.lucko.spark.fabric.FabricSparkMod; import me.lucko.spark.fabric.FabricTickCounter; +import net.fabricmc.fabric.api.registry.CommandRegistry; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.CommandOutput; @@ -44,10 +44,8 @@ import java.util.stream.Stream; public class FabricServerSparkPlugin extends FabricSparkPlugin implements Command<ServerCommandSource>, SuggestionProvider<ServerCommandSource> { public static void register(FabricSparkMod mod, MinecraftServer server) { - CommandDispatcher<ServerCommandSource> dispatcher = server.getCommandManager().getDispatcher(); - FabricServerSparkPlugin plugin = new FabricServerSparkPlugin(mod, server); - registerCommands(dispatcher, plugin, plugin, "spark"); + CommandRegistry.INSTANCE.register(false, dispatcher -> registerCommands(dispatcher, plugin, plugin, "spark")); } private final MinecraftServer server; diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java index 1f6f098..3cc125e 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java @@ -40,25 +40,6 @@ import java.util.concurrent.ScheduledExecutorService; public abstract class FabricSparkPlugin implements SparkPlugin { - public static <T> void registerCommands(CommandDispatcher<T> dispatcher, Command<T> executor, SuggestionProvider<T> suggestor, String... aliases) { - if (aliases.length == 0) { - return; - } - - String mainName = aliases[0]; - LiteralArgumentBuilder<T> command = LiteralArgumentBuilder.<T>literal(mainName) - .executes(executor) - .then(RequiredArgumentBuilder.<T, String>argument("args", StringArgumentType.greedyString()) - .suggests(suggestor) - .executes(executor) - ); - - LiteralCommandNode<T> node = dispatcher.register(command); - for (int i = 1; i < aliases.length; i++) { - dispatcher.register(LiteralArgumentBuilder.<T>literal(aliases[i]).redirect(node)); - } - } - private final FabricSparkMod mod; protected final ScheduledExecutorService scheduler; protected final SparkPlatform platform; @@ -93,4 +74,24 @@ public abstract class FabricSparkPlugin implements SparkPlugin { public ThreadDumper getDefaultThreadDumper() { return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()}); } + + public static <T> void registerCommands(CommandDispatcher<T> dispatcher, Command<T> executor, SuggestionProvider<T> suggestor, String... aliases) { + if (aliases.length == 0) { + return; + } + + String mainName = aliases[0]; + LiteralArgumentBuilder<T> command = LiteralArgumentBuilder.<T>literal(mainName) + .executes(executor) + .then(RequiredArgumentBuilder.<T, String>argument("args", StringArgumentType.greedyString()) + .suggests(suggestor) + .executes(executor) + ); + + LiteralCommandNode<T> node = dispatcher.register(command); + for (int i = 1; i < aliases.length; i++) { + dispatcher.register(LiteralArgumentBuilder.<T>literal(aliases[i]).redirect(node)); + } + } + } |