diff options
author | embeddedt <42941056+embeddedt@users.noreply.github.com> | 2022-06-09 16:34:22 -0400 |
---|---|---|
committer | embeddedt <42941056+embeddedt@users.noreply.github.com> | 2022-06-09 16:34:22 -0400 |
commit | b3cb433ae9992962344f8aaa12024a5c3481590f (patch) | |
tree | 9a30a5cf2d915cd31f9e44d5f2153651aaa92d86 /spark-fabric | |
parent | 0d7f5bb4ec86f39579373eac863b425043590cd1 (diff) | |
parent | 0ac8713eaaefe7336db2e0369bbe547dc6c0da7d (diff) | |
download | spark-b3cb433ae9992962344f8aaa12024a5c3481590f.tar.gz spark-b3cb433ae9992962344f8aaa12024a5c3481590f.tar.bz2 spark-b3cb433ae9992962344f8aaa12024a5c3481590f.zip |
Merge remote-tracking branch 'lucko/master' into forge-1.7.10
Diffstat (limited to 'spark-fabric')
13 files changed, 198 insertions, 208 deletions
diff --git a/spark-fabric/build.gradle b/spark-fabric/build.gradle index d7e87cd..9da8e01 100644 --- a/spark-fabric/build.gradle +++ b/spark-fabric/build.gradle @@ -1,7 +1,7 @@ import net.fabricmc.loom.task.RemapJarTask plugins { - id 'fabric-loom' version '0.10.+' + id 'fabric-loom' version '0.12.+' id 'com.github.johnrengelman.shadow' version '7.0.0' } @@ -28,19 +28,19 @@ configurations { dependencies { // https://modmuss50.me/fabric.html - minecraft 'com.mojang:minecraft:1.18.1' - mappings 'net.fabricmc:yarn:1.18.1+build.9:v2' - modImplementation 'net.fabricmc:fabric-loader:0.12.12' + minecraft 'com.mojang:minecraft:1.19' + mappings 'net.fabricmc:yarn:1.19+build.1:v2' + modImplementation 'net.fabricmc:fabric-loader:0.14.7' Set<String> apiModules = [ "fabric-api-base", - "fabric-command-api-v1", + "fabric-command-api-v2", "fabric-lifecycle-events-v1" ] // Add each module as a dependency apiModules.forEach { - modImplementation(fabricApi.module(it, '0.45.0+1.18')) + modImplementation(fabricApi.module(it, '0.55.3+1.19')) } include(modImplementation('me.lucko:fabric-permissions-api:0.1-SNAPSHOT')) @@ -67,7 +67,7 @@ processResources { } shadowJar { - archiveFileName = 'spark-fabric-dev.jar' + archiveFileName = "spark-fabric-${project.pluginVersion}-dev.jar" configurations = [project.configurations.shade] relocate 'okio', 'me.lucko.spark.lib.okio' @@ -89,7 +89,7 @@ task remappedShadowJar(type: RemapJarTask) { dependsOn tasks.shadowJar input = tasks.shadowJar.archiveFile addNestedDependencies = true - archiveFileName = 'spark-fabric.jar' + archiveFileName = "spark-${project.pluginVersion}-fabric.jar" } tasks.assemble.dependsOn tasks.remappedShadowJar diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java index fa59079..7030680 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java @@ -20,25 +20,47 @@ package me.lucko.spark.fabric; +import com.google.common.collect.ImmutableMap; + import me.lucko.spark.common.util.ClassSourceLookup; import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.ModContainer; import java.nio.file.Path; +import java.util.Collection; +import java.util.Map; public class FabricClassSourceLookup extends ClassSourceLookup.ByCodeSource { private final Path modsDirectory; + private final Map<Path, String> pathToModMap; public FabricClassSourceLookup() { - this.modsDirectory = FabricLoader.getInstance().getGameDir().resolve("mods").toAbsolutePath().normalize(); + FabricLoader loader = FabricLoader.getInstance(); + this.modsDirectory = loader.getGameDir().resolve("mods").toAbsolutePath().normalize(); + this.pathToModMap = constructPathToModIdMap(loader.getAllMods()); } @Override public String identifyFile(Path path) { + String id = this.pathToModMap.get(path); + if (id != null) { + return id; + } + if (!path.startsWith(this.modsDirectory)) { return null; } return super.identifyFileName(this.modsDirectory.relativize(path).toString()); } + + private static Map<Path, String> constructPathToModIdMap(Collection<ModContainer> mods) { + ImmutableMap.Builder<Path, String> builder = ImmutableMap.builder(); + for (ModContainer mod : mods) { + Path path = mod.getRootPath().toAbsolutePath().normalize(); + builder.put(path, mod.getMetadata().getId()); + } + return builder.build(); + } } diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java index 14b3442..2138dbe 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java @@ -34,8 +34,6 @@ import net.minecraft.text.Text; import java.util.UUID; public class FabricCommandSender extends AbstractCommandSender<CommandOutput> { - private static final UUID NIL_UUID = new UUID(0, 0); - private final FabricSparkPlugin plugin; public FabricCommandSender(CommandOutput commandOutput, FabricSparkPlugin plugin) { @@ -67,7 +65,7 @@ public class FabricCommandSender extends AbstractCommandSender<CommandOutput> { @Override public void sendMessage(Component message) { Text component = Text.Serializer.fromJson(GsonComponentSerializer.gson().serialize(message)); - super.delegate.sendSystemMessage(component, NIL_UUID); + super.delegate.sendMessage(component); } @Override diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricPlatformInfo.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricPlatformInfo.java index 93ccda4..e298121 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricPlatformInfo.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricPlatformInfo.java @@ -20,13 +20,13 @@ package me.lucko.spark.fabric; -import me.lucko.spark.common.platform.AbstractPlatformInfo; +import me.lucko.spark.common.platform.PlatformInfo; import net.fabricmc.loader.api.FabricLoader; import java.util.Optional; -public class FabricPlatformInfo extends AbstractPlatformInfo { +public class FabricPlatformInfo implements PlatformInfo { private final Type type; public FabricPlatformInfo(Type type) { @@ -35,7 +35,7 @@ public class FabricPlatformInfo extends AbstractPlatformInfo { @Override public Type getType() { - return type; + return this.type; } @Override diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkGameHooks.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricPlayerPingProvider.java index 5eec53d..bae6c41 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkGameHooks.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricPlayerPingProvider.java @@ -20,21 +20,28 @@ package me.lucko.spark.fabric; -import java.util.function.Predicate; +import com.google.common.collect.ImmutableMap; -public enum FabricSparkGameHooks { - INSTANCE; +import me.lucko.spark.common.monitor.ping.PlayerPingProvider; - // Use events from Fabric API later - // Return true to abort sending to server - private Predicate<String> chatSendCallback = s -> false; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; - public void setChatSendCallback(Predicate<String> callback) { - this.chatSendCallback = callback; - } +import java.util.Map; + +public class FabricPlayerPingProvider implements PlayerPingProvider { + private final MinecraftServer server; - public boolean tryProcessChat(String message) { - return this.chatSendCallback.test(message); + public FabricPlayerPingProvider(MinecraftServer server) { + this.server = server; } + @Override + public Map<String, Integer> poll() { + ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder(); + for (ServerPlayerEntity player : this.server.getPlayerManager().getPlayerList()) { + builder.put(player.getGameProfile().getName(), player.pingMilliseconds); + } + return builder.build(); + } } 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 1dd4fd8..ad419f7 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,14 +20,21 @@ package me.lucko.spark.fabric; +import com.mojang.brigadier.CommandDispatcher; + import me.lucko.spark.fabric.plugin.FabricClientSparkPlugin; import me.lucko.spark.fabric.plugin.FabricServerSparkPlugin; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; import net.minecraft.client.MinecraftClient; +import net.minecraft.command.CommandRegistryAccess; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.CommandManager.RegistrationEnvironment; +import net.minecraft.server.command.ServerCommandSource; import java.nio.file.Path; import java.util.Objects; @@ -38,6 +45,8 @@ public class FabricSparkMod implements ModInitializer { private ModContainer container; private Path configDirectory; + private FabricServerSparkPlugin activeServerPlugin = null; + @Override public void onInitialize() { FabricSparkMod.mod = this; @@ -47,16 +56,36 @@ public class FabricSparkMod implements ModInitializer { .orElseThrow(() -> new IllegalStateException("Unable to get container for spark")); this.configDirectory = loader.getConfigDir().resolve("spark"); - // load hooks - ServerLifecycleEvents.SERVER_STARTING.register(server -> FabricServerSparkPlugin.register(this, server)); + // server event hooks + ServerLifecycleEvents.SERVER_STARTING.register(this::initializeServer); + ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStopping); + CommandRegistrationCallback.EVENT.register(this::onServerCommandRegister); } - // called be entrypoint defined in fabric.mod.json + // client (called be entrypoint defined in fabric.mod.json) public static void initializeClient() { Objects.requireNonNull(FabricSparkMod.mod, "mod"); FabricClientSparkPlugin.register(FabricSparkMod.mod, MinecraftClient.getInstance()); } + // server + public void initializeServer(MinecraftServer server) { + this.activeServerPlugin = FabricServerSparkPlugin.register(this, server); + } + + public void onServerStopping(MinecraftServer stoppingServer) { + if (this.activeServerPlugin != null) { + this.activeServerPlugin.disable(); + this.activeServerPlugin = null; + } + } + + public void onServerCommandRegister(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess access, RegistrationEnvironment env) { + if (this.activeServerPlugin != null) { + this.activeServerPlugin.registerCommands(dispatcher); + } + } + public String getVersion() { return this.container.getMetadata().getVersion().getFriendlyString(); } diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/ClientPlayerEntityMixin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/ClientPlayerEntityMixin.java deleted file mode 100644 index 495b213..0000000 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/ClientPlayerEntityMixin.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 com.mojang.authlib.GameProfile; - -import me.lucko.spark.fabric.FabricSparkGameHooks; - -import net.minecraft.client.network.AbstractClientPlayerEntity; -import net.minecraft.client.network.ClientPlayerEntity; -import net.minecraft.client.world.ClientWorld; - -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; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - -@Mixin(ClientPlayerEntity.class) -public abstract class ClientPlayerEntityMixin extends AbstractClientPlayerEntity { - - public ClientPlayerEntityMixin(ClientWorld clientWorld_1, GameProfile gameProfile_1) { - super(clientWorld_1, gameProfile_1); - } - - @Inject(method = "sendChatMessage(Ljava/lang/String;)V", at = @At("HEAD"), - locals = LocalCapture.CAPTURE_FAILHARD, - cancellable = true) - public void onSendChatMessage(String message, CallbackInfo ci) { - if (FabricSparkGameHooks.INSTANCE.tryProcessChat(message)) { - ci.cancel(); - } - } -} diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java index 359630d..dc2e7d9 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java @@ -1,12 +1,34 @@ +/* + * 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.placeholder; import eu.pb4.placeholders.PlaceholderAPI; import eu.pb4.placeholders.PlaceholderResult; + import me.lucko.spark.common.SparkPlatform; -import me.lucko.spark.common.command.modules.HealthModule; import me.lucko.spark.common.monitor.cpu.CpuMonitor; import me.lucko.spark.common.monitor.tick.TickStatistics; import me.lucko.spark.common.util.RollingAverage; +import me.lucko.spark.common.util.StatisticFormatter; + import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.minecraft.text.Text; @@ -45,16 +67,16 @@ public class SparkFabricPlaceholderApi { if (tps == null) { return PlaceholderResult.invalid("Invalid argument"); } else { - return PlaceholderResult.value(toText(HealthModule.formatTps(tps))); + return PlaceholderResult.value(toText(StatisticFormatter.formatTps(tps))); } } else { return PlaceholderResult.value(toText( Component.text() - .append(HealthModule.formatTps(tickStatistics.tps5Sec())).append(Component.text(", ")) - .append(HealthModule.formatTps(tickStatistics.tps10Sec())).append(Component.text(", ")) - .append(HealthModule.formatTps(tickStatistics.tps1Min())).append(Component.text(", ")) - .append(HealthModule.formatTps(tickStatistics.tps5Min())).append(Component.text(", ")) - .append(HealthModule.formatTps(tickStatistics.tps15Min())) + .append(StatisticFormatter.formatTps(tickStatistics.tps5Sec())).append(Component.text(", ")) + .append(StatisticFormatter.formatTps(tickStatistics.tps10Sec())).append(Component.text(", ")) + .append(StatisticFormatter.formatTps(tickStatistics.tps1Min())).append(Component.text(", ")) + .append(StatisticFormatter.formatTps(tickStatistics.tps5Min())).append(Component.text(", ")) + .append(StatisticFormatter.formatTps(tickStatistics.tps15Min())) .build() )); } @@ -82,13 +104,13 @@ public class SparkFabricPlaceholderApi { if (duration == null) { return PlaceholderResult.invalid("Invalid argument"); } else { - return PlaceholderResult.value(toText(HealthModule.formatTickDurations(duration))); + return PlaceholderResult.value(toText(StatisticFormatter.formatTickDurations(duration))); } } else { return PlaceholderResult.value(toText( Component.text() - .append(HealthModule.formatTickDurations(tickStatistics.duration10Sec())).append(Component.text("; ")) - .append(HealthModule.formatTickDurations(tickStatistics.duration1Min())) + .append(StatisticFormatter.formatTickDurations(tickStatistics.duration10Sec())).append(Component.text("; ")) + .append(StatisticFormatter.formatTickDurations(tickStatistics.duration1Min())) .build() )); } @@ -113,14 +135,14 @@ public class SparkFabricPlaceholderApi { if (usage == null) { return PlaceholderResult.invalid("Invalid argument"); } else { - return PlaceholderResult.value(toText(HealthModule.formatCpuUsage(usage))); + return PlaceholderResult.value(toText(StatisticFormatter.formatCpuUsage(usage))); } } else { return PlaceholderResult.value(toText( Component.text() - .append(HealthModule.formatCpuUsage(CpuMonitor.systemLoad10SecAvg())).append(Component.text(", ")) - .append(HealthModule.formatCpuUsage(CpuMonitor.systemLoad1MinAvg())).append(Component.text(", ")) - .append(HealthModule.formatCpuUsage(CpuMonitor.systemLoad15MinAvg())) + .append(StatisticFormatter.formatCpuUsage(CpuMonitor.systemLoad10SecAvg())).append(Component.text(", ")) + .append(StatisticFormatter.formatCpuUsage(CpuMonitor.systemLoad1MinAvg())).append(Component.text(", ")) + .append(StatisticFormatter.formatCpuUsage(CpuMonitor.systemLoad15MinAvg())) .build() )); } @@ -145,14 +167,14 @@ public class SparkFabricPlaceholderApi { if (usage == null) { return PlaceholderResult.invalid("Invalid argument"); } else { - return PlaceholderResult.value(toText(HealthModule.formatCpuUsage(usage))); + return PlaceholderResult.value(toText(StatisticFormatter.formatCpuUsage(usage))); } } else { return PlaceholderResult.value(toText( Component.text() - .append(HealthModule.formatCpuUsage(CpuMonitor.processLoad10SecAvg())).append(Component.text(", ")) - .append(HealthModule.formatCpuUsage(CpuMonitor.processLoad1MinAvg())).append(Component.text(", ")) - .append(HealthModule.formatCpuUsage(CpuMonitor.processLoad15MinAvg())) + .append(StatisticFormatter.formatCpuUsage(CpuMonitor.processLoad10SecAvg())).append(Component.text(", ")) + .append(StatisticFormatter.formatCpuUsage(CpuMonitor.processLoad1MinAvg())).append(Component.text(", ")) + .append(StatisticFormatter.formatCpuUsage(CpuMonitor.processLoad15MinAvg())) .build() )); } 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 0948225..e94d697 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 @@ -33,101 +33,73 @@ import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.tick.TickReporter; import me.lucko.spark.fabric.FabricCommandSender; import me.lucko.spark.fabric.FabricPlatformInfo; -import me.lucko.spark.fabric.FabricSparkGameHooks; import me.lucko.spark.fabric.FabricSparkMod; import me.lucko.spark.fabric.FabricTickHook; import me.lucko.spark.fabric.FabricTickReporter; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayNetworkHandler; -import net.minecraft.command.CommandSource; +import net.minecraft.command.CommandRegistryAccess; import net.minecraft.server.command.CommandOutput; -import java.util.Arrays; -import java.util.Optional; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; import java.util.stream.Stream; -public class FabricClientSparkPlugin extends FabricSparkPlugin implements SuggestionProvider<CommandSource> { +public class FabricClientSparkPlugin extends FabricSparkPlugin implements Command<FabricClientCommandSource>, SuggestionProvider<FabricClientCommandSource> { public static void register(FabricSparkMod mod, MinecraftClient client) { FabricClientSparkPlugin plugin = new FabricClientSparkPlugin(mod, client); plugin.enable(); - - // ensure commands are registered - plugin.scheduler.scheduleWithFixedDelay(plugin::checkCommandRegistered, 10, 10, TimeUnit.SECONDS); - - // register shutdown hook - ClientLifecycleEvents.CLIENT_STOPPING.register(stoppingClient -> { - if (stoppingClient == plugin.minecraft) { - plugin.disable(); - } - }); } private final MinecraftClient minecraft; - private CommandDispatcher<CommandSource> dispatcher; public FabricClientSparkPlugin(FabricSparkMod mod, MinecraftClient minecraft) { super(mod); this.minecraft = minecraft; } - private CommandDispatcher<CommandSource> getPlayerCommandDispatcher() { - return Optional.ofNullable(this.minecraft.player) - .map(player -> player.networkHandler) - .map(ClientPlayNetworkHandler::getCommandDispatcher) - .orElse(null); + @Override + public void enable() { + super.enable(); + + // events + ClientLifecycleEvents.CLIENT_STOPPING.register(this::onDisable); + ClientCommandRegistrationCallback.EVENT.register(this::onCommandRegister); } - private void checkCommandRegistered() { - CommandDispatcher<CommandSource> dispatcher = getPlayerCommandDispatcher(); - if (dispatcher == null) { - return; + private void onDisable(MinecraftClient stoppingClient) { + if (stoppingClient == this.minecraft) { + disable(); } + } - try { - if (dispatcher != this.dispatcher) { - this.dispatcher = dispatcher; - registerCommands(this.dispatcher, c -> Command.SINGLE_SUCCESS, this, "sparkc", "sparkclient"); - FabricSparkGameHooks.INSTANCE.setChatSendCallback(this::onClientChat); - } - } catch (Exception e) { - e.printStackTrace(); - } + public void onCommandRegister(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) { + registerCommands(dispatcher, this, this, "sparkc", "sparkclient"); } - public boolean onClientChat(String chat) { - String[] args = processArgs(chat, false); + @Override + public int run(CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException { + String[] args = processArgs(context, false, "sparkc", "sparkclient"); if (args == null) { - return false; + return 0; } this.threadDumper.ensureSetup(); - this.platform.executeCommand(new FabricCommandSender(this.minecraft.player, this), args); - this.minecraft.inGameHud.getChatHud().addToMessageHistory(chat); - return true; + this.platform.executeCommand(new FabricCommandSender(context.getSource().getEntity(), this), args); + return Command.SINGLE_SUCCESS; } @Override - public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException { - String[] args = processArgs(context.getInput(), true); + public CompletableFuture<Suggestions> getSuggestions(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException { + String[] args = processArgs(context, true, "/sparkc", "/sparkclient"); if (args == null) { return Suggestions.empty(); } - return generateSuggestions(new FabricCommandSender(this.minecraft.player, this), args, builder); - } - - private static String[] processArgs(String input, boolean tabComplete) { - String[] split = input.split(" ", tabComplete ? -1 : 0); - if (split.length == 0 || !split[0].equals("/sparkc") && !split[0].equals("/sparkclient")) { - return null; - } - - return Arrays.copyOfRange(split, 1, split.length); + return generateSuggestions(new FabricCommandSender(context.getSource().getEntity(), this), args, builder); } @Override 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 8d38b1c..428ac4c 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,6 +21,7 @@ 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; @@ -28,49 +29,33 @@ import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import me.lucko.fabric.api.permissions.v0.Permissions; +import me.lucko.spark.common.monitor.ping.PlayerPingProvider; import me.lucko.spark.common.platform.PlatformInfo; import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.tick.TickReporter; import me.lucko.spark.fabric.FabricCommandSender; import me.lucko.spark.fabric.FabricPlatformInfo; +import me.lucko.spark.fabric.FabricPlayerPingProvider; import me.lucko.spark.fabric.FabricSparkMod; import me.lucko.spark.fabric.FabricTickHook; import me.lucko.spark.fabric.FabricTickReporter; - import me.lucko.spark.fabric.placeholder.SparkFabricPlaceholderApi; -import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; + import net.fabricmc.loader.api.FabricLoader; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.CommandOutput; import net.minecraft.server.command.ServerCommandSource; -import java.util.Arrays; import java.util.concurrent.CompletableFuture; import java.util.stream.Stream; public class FabricServerSparkPlugin extends FabricSparkPlugin implements Command<ServerCommandSource>, SuggestionProvider<ServerCommandSource> { - public static void register(FabricSparkMod mod, MinecraftServer server) { + public static FabricServerSparkPlugin register(FabricSparkMod mod, MinecraftServer server) { FabricServerSparkPlugin plugin = new FabricServerSparkPlugin(mod, server); plugin.enable(); - - // register commands - registerCommands(server.getCommandManager().getDispatcher(), plugin, plugin, "spark"); - CommandRegistrationCallback.EVENT.register((dispatcher, isDedicated) -> registerCommands(dispatcher, plugin, plugin, "spark")); - - - if (FabricLoader.getInstance().isModLoaded("placeholder-api")) { - new SparkFabricPlaceholderApi(plugin.platform); - } - - // register shutdown hook - ServerLifecycleEvents.SERVER_STOPPING.register(stoppingServer -> { - if (stoppingServer == plugin.server) { - plugin.disable(); - } - }); + return plugin; } private final MinecraftServer server; @@ -81,8 +66,25 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman } @Override + public void enable() { + super.enable(); + + // register commands + registerCommands(this.server.getCommandManager().getDispatcher()); + + // placeholders + if (FabricLoader.getInstance().isModLoaded("placeholder-api")) { + new SparkFabricPlaceholderApi(this.platform); + } + } + + public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) { + registerCommands(dispatcher, this, this, "spark"); + } + + @Override public int run(CommandContext<ServerCommandSource> context) throws CommandSyntaxException { - String[] args = processArgs(context, false); + String[] args = processArgs(context, false, "/spark", "spark"); if (args == null) { return 0; } @@ -95,7 +97,7 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman @Override public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException { - String[] args = processArgs(context, true); + String[] args = processArgs(context, true, "/spark", "spark"); if (args == null) { return Suggestions.empty(); } @@ -103,15 +105,6 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman return generateSuggestions(new FabricCommandSender(context.getSource().getPlayer(), this), args, builder); } - private static String[] processArgs(CommandContext<ServerCommandSource> context, boolean tabComplete) { - String[] split = context.getInput().split(" ", tabComplete ? -1 : 0); - 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(CommandOutput sender, String permission) { if (sender instanceof PlayerEntity) { @@ -140,6 +133,11 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman } @Override + public PlayerPingProvider createPlayerPingProvider() { + return new FabricPlayerPingProvider(this.server); + } + + @Override public PlatformInfo getPlatformInfo() { return new FabricPlatformInfo(PlatformInfo.Type.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 4bcfce4..b1392d4 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 @@ -25,6 +25,7 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; @@ -35,6 +36,7 @@ import me.lucko.spark.common.SparkPlugin; import me.lucko.spark.common.command.sender.CommandSender; import me.lucko.spark.common.sampler.ThreadDumper; import me.lucko.spark.common.util.ClassSourceLookup; +import me.lucko.spark.common.util.SparkThreadFactory; import me.lucko.spark.fabric.FabricClassSourceLookup; import me.lucko.spark.fabric.FabricSparkMod; @@ -44,6 +46,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.nio.file.Path; +import java.util.Arrays; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -54,22 +57,18 @@ public abstract class FabricSparkPlugin implements SparkPlugin { private final FabricSparkMod mod; private final Logger logger; protected final ScheduledExecutorService scheduler; - protected final SparkPlatform platform; + + protected SparkPlatform platform; protected final ThreadDumper.GameThread threadDumper = new ThreadDumper.GameThread(); protected FabricSparkPlugin(FabricSparkMod mod) { this.mod = mod; this.logger = LogManager.getLogger("spark"); - this.scheduler = Executors.newSingleThreadScheduledExecutor(r -> { - Thread thread = Executors.defaultThreadFactory().newThread(r); - thread.setName("spark-fabric-async-worker"); - thread.setDaemon(true); - return thread; - }); - this.platform = new SparkPlatform(this); + this.scheduler = Executors.newScheduledThreadPool(4, new SparkThreadFactory()); } public void enable() { + this.platform = new SparkPlatform(this); this.platform.enable(); } @@ -155,4 +154,13 @@ public abstract class FabricSparkPlugin implements SparkPlugin { } } + protected static String[] processArgs(CommandContext<?> context, boolean tabComplete, String... aliases) { + String[] split = context.getInput().split(" ", tabComplete ? -1 : 0); + if (split.length == 0 || !Arrays.asList(aliases).contains(split[0])) { + return null; + } + + return Arrays.copyOfRange(split, 1, split.length); + } + } diff --git a/spark-fabric/src/main/resources/fabric.mod.json b/spark-fabric/src/main/resources/fabric.mod.json index a9e15e0..e2e600d 100644 --- a/spark-fabric/src/main/resources/fabric.mod.json +++ b/spark-fabric/src/main/resources/fabric.mod.json @@ -23,13 +23,10 @@ "me.lucko.spark.fabric.FabricSparkMod::initializeClient" ] }, - "mixins": [ - "spark.mixins.json" - ], "depends": { "fabricloader": ">=0.4.0", "fabric-api-base": "*", - "fabric-command-api-v1": "*", + "fabric-command-api-v2": "*", "fabric-lifecycle-events-v1" : "*" } } diff --git a/spark-fabric/src/main/resources/spark.mixins.json b/spark-fabric/src/main/resources/spark.mixins.json deleted file mode 100644 index 3f495ea..0000000 --- a/spark-fabric/src/main/resources/spark.mixins.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "required": true, - "package": "me.lucko.spark.fabric.mixin", - "compatibilityLevel": "JAVA_8", - "client": [ - "ClientPlayerEntityMixin" - ], - "injectors": { - "defaultRequire": 1 - } -} |