diff options
author | Luck <git@lucko.me> | 2019-08-26 09:02:55 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2019-08-26 09:02:55 +0100 |
commit | bf2262c392c7e234c8662aa7be5d2970dcacee65 (patch) | |
tree | eedcfee731264ea5ba9d687a5dfd70eaf36a835e | |
parent | 8a7ca8ef4a29bd1d684a29f925f3a9db1f69fe0b (diff) | |
download | spark-bf2262c392c7e234c8662aa7be5d2970dcacee65.tar.gz spark-bf2262c392c7e234c8662aa7be5d2970dcacee65.tar.bz2 spark-bf2262c392c7e234c8662aa7be5d2970dcacee65.zip |
Various misc changes to spark-forge and spark-fabric
18 files changed, 231 insertions, 162 deletions
diff --git a/spark-fabric/build.gradle b/spark-fabric/build.gradle index ca0ce24..7640be2 100644 --- a/spark-fabric/build.gradle +++ b/spark-fabric/build.gradle @@ -1,3 +1,5 @@ +import net.fabricmc.loom.task.RemapJarTask + plugins { id 'fabric-loom' version '0.2.5-SNAPSHOT' id 'com.github.johnrengelman.shadow' version '4.0.1' @@ -40,7 +42,7 @@ shadowJar { relocate 'com.google.protobuf', 'me.lucko.spark.lib.protobuf' } -task remappedShadowJar(type: net.fabricmc.loom.task.RemapJarTask) { +task remappedShadowJar(type: RemapJarTask) { dependsOn tasks.shadowJar input = tasks.shadowJar.archiveFile addNestedDependencies = true 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 3a8d128..fea877c 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 @@ -21,6 +21,7 @@ package me.lucko.spark.fabric; import me.lucko.spark.common.CommandSender; +import me.lucko.spark.fabric.plugin.FabricSparkPlugin; import net.kyori.text.Component; import net.kyori.text.serializer.gson.GsonComponentSerializer; import net.minecraft.entity.player.PlayerEntity; @@ -31,13 +32,12 @@ import net.minecraft.text.Text; import java.util.UUID; public class FabricCommandSender implements CommandSender { - - private final VanillaPermission permission; private final CommandOutput sender; + private final FabricSparkPlugin plugin; - public FabricCommandSender(VanillaPermission permission, CommandOutput sender) { - this.permission = permission; + public FabricCommandSender(CommandOutput sender, FabricSparkPlugin plugin) { this.sender = sender; + this.plugin = plugin; } @Override @@ -67,7 +67,7 @@ public class FabricCommandSender implements CommandSender { @Override public boolean hasPermission(String permission) { - return this.permission.hasPermissionLevel(4); // Require /stop access, reasonable + return this.plugin.hasPermission(this.sender, permission); } @Override @@ -86,9 +86,4 @@ public class FabricCommandSender implements CommandSender { public int hashCode() { return this.sender.hashCode(); } - - public interface VanillaPermission { - - boolean hasPermissionLevel(int level); - } } 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 new file mode 100644 index 0000000..d046ab8 --- /dev/null +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkGameHooks.java @@ -0,0 +1,73 @@ +/* + * 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; + +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; + + public void setChatSendCallback(Predicate<String> callback) { + this.chatSendCallback = callback; + } + + public boolean tryProcessChat(String message) { + 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 4e4468b..0f6fb1f 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 @@ -22,85 +22,38 @@ package me.lucko.spark.fabric; import net.fabricmc.api.ModInitializer; import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.ModContainer; import java.nio.file.Path; -import java.util.HashSet; -import java.util.Set; -import java.util.function.Predicate; public class FabricSparkMod implements ModInitializer { + private static FabricSparkMod mod; - private static FabricSparkMod instance; - private final Set<FabricTickCounter> clientCounters = new HashSet<>(); - private final Set<FabricTickCounter> serverCounters = new HashSet<>(); - private String version; - private Path configDir; - // Use events from Fabric API later - // Return true to abort sending to server - private Predicate<String> chatSendCallback = s -> false; - - public FabricSparkMod() { + public static FabricSparkMod getMod() { + return mod; } - public static FabricSparkMod getInstance() { - return instance; - } + private ModContainer container; + private Path configDirectory; @Override public void onInitialize() { - FabricSparkMod.instance = this; - FabricLoader loader = FabricLoader.getInstance(); - this.version = loader.getModContainer("spark") - .orElseThrow(() -> new IllegalStateException("Spark loaded incorrectly!")) - .getMetadata() - .getVersion() - .getFriendlyString(); - this.configDir = loader.getConfigDirectory().toPath().resolve("spark"); + FabricSparkMod.mod = this; - // When Fabric API is available, we will register event listeners here + FabricLoader loader = FabricLoader.getInstance(); + this.container = loader.getModContainer("spark") + .orElseThrow(() -> new IllegalStateException("Unable to get container for spark")); + this.configDirectory = loader.getConfigDirectory().toPath().resolve("spark"); } public String getVersion() { - return version; + return this.container.getMetadata().getVersion().getFriendlyString(); } public Path getConfigDirectory() { - return configDir; - } - - public void setChatSendCallback(Predicate<String> callback) { - this.chatSendCallback = callback; - } - - public boolean tryProcessChat(String message) { - return 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 each : clientCounters) { - each.onTick(); - } - } - - public void tickServerCounters() { - for (FabricTickCounter each : serverCounters) { - each.onTick(); + if (this.configDirectory == null) { + throw new IllegalStateException("Config directory not set"); } + return this.configDirectory; } } 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 03d00b2..b189e3f 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 @@ -24,20 +24,11 @@ import me.lucko.spark.common.sampler.TickCounter; import java.util.HashSet; import java.util.Set; -import java.util.function.Consumer; - -public class FabricTickCounter implements TickCounter { +public abstract class FabricTickCounter implements TickCounter { private final Set<TickTask> tasks = new HashSet<>(); - private final Consumer<FabricTickCounter> adder; - private final Consumer<FabricTickCounter> remover; private int tick = 0; - public FabricTickCounter(Consumer<FabricTickCounter> adder, Consumer<FabricTickCounter> remover) { - this.adder = adder; - this.remover = remover; - } - public void onTick() { for (TickTask r : this.tasks) { r.onTick(this); @@ -46,16 +37,6 @@ public class FabricTickCounter implements TickCounter { } @Override - public void start() { - this.adder.accept(this); - } - - @Override - public void close() { - this.remover.accept(this); - } - - @Override public int getCurrentTick() { return this.tick; } @@ -69,4 +50,28 @@ public class FabricTickCounter implements TickCounter { public void removeTickTask(TickTask runnable) { this.tasks.remove(runnable); } + + public static final class Server extends FabricTickCounter { + @Override + public void start() { + FabricSparkGameHooks.INSTANCE.addServerCounter(this); + } + + @Override + public void close() { + FabricSparkGameHooks.INSTANCE.removeServerCounter(this); + } + } + + public static final class Client extends FabricTickCounter { + @Override + public void start() { + FabricSparkGameHooks.INSTANCE.addClientCounter(this); + } + + @Override + public void close() { + FabricSparkGameHooks.INSTANCE.removeClientCounter(this); + } + } } 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 index 3ce57b1..8085567 100644 --- 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 @@ -21,7 +21,7 @@ package me.lucko.spark.fabric.mixin; import com.mojang.authlib.GameProfile; -import me.lucko.spark.fabric.FabricSparkMod; +import me.lucko.spark.fabric.FabricSparkGameHooks; import net.minecraft.client.network.AbstractClientPlayerEntity; import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.world.ClientWorld; @@ -42,7 +42,7 @@ public abstract class ClientPlayerEntityMixin extends AbstractClientPlayerEntity locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true) public void onSendChatMessage(String message, CallbackInfo ci) { - if (FabricSparkMod.getInstance().tryProcessChat(message)) { + if (FabricSparkGameHooks.INSTANCE.tryProcessChat(message)) { ci.cancel(); } } 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 index b987ca6..ae7ef5f 100644 --- 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 @@ -20,8 +20,9 @@ package me.lucko.spark.fabric.mixin; -import me.lucko.spark.fabric.FabricClientSparkPlugin; +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; @@ -40,12 +41,12 @@ public abstract class MinecraftClientMixin extends NonBlockingThreadExecutor<Run @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.getInstance(), (MinecraftClient) (Object) this); + FabricClientSparkPlugin.register(FabricSparkMod.getMod(), (MinecraftClient) (Object) this); } @Inject(method = "tick()V", at = @At("RETURN")) public void onTick(CallbackInfo ci) { - FabricSparkMod.getInstance().tickClientCounters(); + 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 index bb14b64..4e3a0ab 100644 --- 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 @@ -20,8 +20,9 @@ package me.lucko.spark.fabric.mixin; -import me.lucko.spark.fabric.FabricServerSparkPlugin; +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; @@ -41,12 +42,12 @@ public abstract class MinecraftServerMixin extends NonBlockingThreadExecutor<Ser @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.getInstance(), (MinecraftServer) (Object) this); + FabricServerSparkPlugin.register(FabricSparkMod.getMod(), (MinecraftServer) (Object) this); } @Inject(method = "tick(Ljava/util/function/BooleanSupplier;)V", at = @At("RETURN")) public void onTick(CallbackInfo ci) { - FabricSparkMod.getInstance().tickServerCounters(); + FabricSparkGameHooks.INSTANCE.tickServerCounters(); } } diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClientSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java index 1421779..d87d5be 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClientSparkPlugin.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java @@ -18,13 +18,18 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package me.lucko.spark.fabric; +package me.lucko.spark.fabric.plugin; import com.mojang.brigadier.CommandDispatcher; import me.lucko.spark.common.sampler.TickCounter; +import me.lucko.spark.fabric.FabricCommandSender; +import me.lucko.spark.fabric.FabricSparkGameHooks; +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; @@ -33,6 +38,12 @@ import java.util.stream.Stream; public class FabricClientSparkPlugin extends FabricSparkPlugin { + public static void register(FabricSparkMod mod, MinecraftClient client) { + FabricClientSparkPlugin plugin = new FabricClientSparkPlugin(mod, client); + + plugin.scheduler.scheduleWithFixedDelay(plugin::checkCommandRegistered, 10, 10, TimeUnit.SECONDS); + } + private final MinecraftClient minecraft; private CommandDispatcher<CommandSource> dispatcher; @@ -41,12 +52,6 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin { this.minecraft = minecraft; } - public static void register(FabricSparkMod mod, MinecraftClient client) { - FabricClientSparkPlugin plugin = new FabricClientSparkPlugin(mod, client); - - plugin.scheduler.scheduleWithFixedDelay(plugin::checkCommandRegistered, 10, 10, TimeUnit.SECONDS); - } - private void checkCommandRegistered() { ClientPlayerEntity player = this.minecraft.player; if (player == null) { @@ -63,17 +68,13 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin { if (dispatcher != this.dispatcher) { this.dispatcher = dispatcher; registerCommands(this.dispatcher, c -> 0, "sparkc", "sparkclient"); - this.mod.setChatSendCallback(this::onClientChat); + FabricSparkGameHooks.INSTANCE.setChatSendCallback(this::onClientChat); } } catch (Exception e) { e.printStackTrace(); } } - private FabricCommandSender createClientSender() { - return new FabricCommandSender(this.minecraft.player.networkHandler.getCommandSource()::hasPermissionLevel, this.minecraft.player); - } - public boolean onClientChat(String chat) { String[] split = chat.split(" "); if (split.length == 0 || (!split[0].equals("/sparkc") && !split[0].equals("/sparkclient"))) { @@ -81,19 +82,24 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin { } String[] args = Arrays.copyOfRange(split, 1, split.length); - this.platform.executeCommand(createClientSender(), args); + this.platform.executeCommand(new FabricCommandSender(this.minecraft.player, this), args); this.minecraft.inGameHud.getChatHud().addToMessageHistory(chat); return true; } @Override + public boolean hasPermission(CommandOutput sender, String permission) { + return true; + } + + @Override public Stream<FabricCommandSender> getSendersWithPermission(String permission) { - return Stream.of(createClientSender()); + return Stream.of(new FabricCommandSender(this.minecraft.player, this)); } @Override public TickCounter createTickCounter() { - return new FabricTickCounter(FabricSparkMod.getInstance()::addClientCounter, FabricSparkMod.getInstance()::removeClientCounter); + return new FabricTickCounter.Client(); } @Override diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricServerSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java index 01bc442..9729e55 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricServerSparkPlugin.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java @@ -18,63 +18,74 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package me.lucko.spark.fabric; +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 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.minecraft.entity.player.PlayerEntity; import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.CommandOutput; import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.text.LiteralText; import java.util.Arrays; import java.util.stream.Stream; public class FabricServerSparkPlugin extends FabricSparkPlugin implements Command<ServerCommandSource> { - private final MinecraftServer server; - - public FabricServerSparkPlugin(FabricSparkMod mod, MinecraftServer server) { - super(mod); - this.server = server; - } - public static void register(FabricSparkMod mod, MinecraftServer server) { CommandDispatcher<ServerCommandSource> dispatcher = server.getCommandManager().getDispatcher(); FabricServerSparkPlugin plugin = new FabricServerSparkPlugin(mod, server); registerCommands(dispatcher, plugin, "spark"); - // PermissionAPI.registerNode("spark", DefaultPermissionLevel.OP, "Access to the spark command"); + } + + private final MinecraftServer server; + + public FabricServerSparkPlugin(FabricSparkMod mod, MinecraftServer server) { + super(mod); + this.server = server; } @Override public int run(CommandContext<ServerCommandSource> context) throws CommandSyntaxException { String[] split = context.getInput().split(" "); if (split.length == 0 || !split[0].equals("/spark")) { - context.getSource().sendError(new LiteralText("Wrong split started with " + (split.length == 0 ? "nothing" : split[0]))); return 0; } String[] args = Arrays.copyOfRange(split, 1, split.length); - this.platform.executeCommand(new FabricCommandSender(context.getSource()::hasPermissionLevel, context.getSource().getPlayer()), args); + this.platform.executeCommand(new FabricCommandSender(context.getSource().getPlayer(), this), args); return 1; } @Override + public boolean hasPermission(CommandOutput sender, String permission) { + if (sender instanceof PlayerEntity) { + return this.server.getPermissionLevel(((PlayerEntity) sender).getGameProfile()) >= 4; + } else { + return true; + } + } + + @Override public Stream<FabricCommandSender> getSendersWithPermission(String permission) { return Stream.concat( this.server.getPlayerManager().getPlayerList().stream() - .filter(player -> this.server.getPermissionLevel(player.getGameProfile()) == 4), + .filter(player -> hasPermission(player, permission)), Stream.of(this.server) - ).map(sender -> new FabricCommandSender(i -> true, sender)); + ).map(sender -> new FabricCommandSender(sender, this)); } @Override public TickCounter createTickCounter() { - return new FabricTickCounter(FabricSparkMod.getInstance()::addServerCounter, FabricSparkMod.getInstance()::removeServerCounter); + return new FabricTickCounter.Server(); } @Override diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java index a0f6cf0..6672358 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkPlugin.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java @@ -18,7 +18,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package me.lucko.spark.fabric; +package me.lucko.spark.fabric.plugin; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.mojang.brigadier.Command; @@ -30,6 +30,8 @@ import com.mojang.brigadier.tree.LiteralCommandNode; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.SparkPlugin; import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.fabric.FabricSparkMod; +import net.minecraft.server.command.CommandOutput; import java.nio.file.Path; import java.util.concurrent.Executors; @@ -37,23 +39,11 @@ import java.util.concurrent.ScheduledExecutorService; public abstract class FabricSparkPlugin implements SparkPlugin { - protected final ScheduledExecutorService scheduler; - protected final SparkPlatform platform; - protected final FabricSparkMod mod; - - protected FabricSparkPlugin(FabricSparkMod mod) { - this.mod = mod; - this.scheduler = Executors.newSingleThreadScheduledExecutor( - new ThreadFactoryBuilder().setNameFormat("spark-fabric-async-worker").build() - ); - this.platform = new SparkPlatform(this); - this.platform.enable(); - } - public static <T> void registerCommands(CommandDispatcher<T> dispatcher, Command<T> executor, String... aliases) { if (aliases.length == 0) { return; } + String mainName = aliases[0]; LiteralArgumentBuilder<T> command = LiteralArgumentBuilder.<T>literal(mainName) .executes(executor) @@ -67,6 +57,21 @@ public abstract class FabricSparkPlugin implements SparkPlugin { } } + private final FabricSparkMod mod; + protected final ScheduledExecutorService scheduler; + protected final SparkPlatform platform; + + protected FabricSparkPlugin(FabricSparkMod mod) { + this.mod = mod; + this.scheduler = Executors.newSingleThreadScheduledExecutor( + new ThreadFactoryBuilder().setNameFormat("spark-fabric-async-worker").build() + ); + this.platform = new SparkPlatform(this); + this.platform.enable(); + } + + public abstract boolean hasPermission(CommandOutput sender, String permission); + @Override public String getVersion() { return this.mod.getVersion(); diff --git a/spark-fabric/src/main/resources/assets/spark/icon.png b/spark-fabric/src/main/resources/assets/spark/icon.png Binary files differindex fef8f70..bb6bd0e 100644 --- a/spark-fabric/src/main/resources/assets/spark/icon.png +++ b/spark-fabric/src/main/resources/assets/spark/icon.png diff --git a/spark-fabric/src/main/resources/fabric.mod.json b/spark-fabric/src/main/resources/fabric.mod.json index 13a352d..7b9b0fc 100644 --- a/spark-fabric/src/main/resources/fabric.mod.json +++ b/spark-fabric/src/main/resources/fabric.mod.json @@ -1,13 +1,13 @@ { "schemaVersion": 1, "id": "spark", + "name": "spark", "version": "${pluginVersion}", - "name": "Spark", - "icon": "assets/spark/icon.png", "description": "${pluginDescription}", "authors": [ - "Lucko" + "Luck", "sk89q" ], + "icon": "assets/spark/icon.png", "contact": { "homepage": "https://sparkprofiler.github.io/", "sources": "https://github.com/lucko/spark", diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java index 418cf2d..ff988f0 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java @@ -21,6 +21,7 @@ package me.lucko.spark.forge; import me.lucko.spark.common.CommandSender; +import me.lucko.spark.forge.plugin.ForgeSparkPlugin; import net.kyori.text.Component; import net.kyori.text.serializer.gson.GsonComponentSerializer; import net.minecraft.command.ICommandSource; 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 f3bf5c3..1f7199d 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 @@ -20,6 +20,8 @@ package me.lucko.spark.forge; +import me.lucko.spark.forge.plugin.ForgeClientSparkPlugin; +import me.lucko.spark.forge.plugin.ForgeServerSparkPlugin; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.ModContainer; diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java index 0d38ea2..7de0038 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java @@ -18,10 +18,13 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package me.lucko.spark.forge; +package me.lucko.spark.forge.plugin; import com.mojang.brigadier.CommandDispatcher; import me.lucko.spark.common.sampler.TickCounter; +import me.lucko.spark.forge.ForgeCommandSender; +import me.lucko.spark.forge.ForgeSparkMod; +import me.lucko.spark.forge.ForgeTickCounter; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.player.ClientPlayerEntity; import net.minecraft.client.network.play.ClientPlayNetHandler; @@ -102,7 +105,7 @@ public class ForgeClientSparkPlugin extends ForgeSparkPlugin { } @Override - boolean hasPermission(ICommandSource sender, String permission) { + public boolean hasPermission(ICommandSource sender, String permission) { return true; } diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java index 13ccb59..5eb61f8 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java @@ -18,13 +18,16 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package me.lucko.spark.forge; +package me.lucko.spark.forge.plugin; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import me.lucko.spark.common.sampler.TickCounter; +import me.lucko.spark.forge.ForgeCommandSender; +import me.lucko.spark.forge.ForgeSparkMod; +import me.lucko.spark.forge.ForgeTickCounter; import net.minecraft.command.CommandSource; import net.minecraft.command.ICommandSource; import net.minecraft.entity.player.PlayerEntity; @@ -69,7 +72,7 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command< } @Override - boolean hasPermission(ICommandSource sender, String permission) { + public boolean hasPermission(ICommandSource sender, String permission) { if (sender instanceof PlayerEntity) { return PermissionAPI.hasPermission((PlayerEntity) sender, permission); } else { @@ -80,7 +83,7 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command< @Override public Stream<ForgeCommandSender> getSendersWithPermission(String permission) { return Stream.concat( - this.server.getPlayerList().getPlayers().stream().filter(player -> PermissionAPI.hasPermission(player, permission)), + 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/ForgeSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java index 7141e8d..915ca6d 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java @@ -18,7 +18,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package me.lucko.spark.forge; +package me.lucko.spark.forge.plugin; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.mojang.brigadier.Command; @@ -26,9 +26,11 @@ 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.tree.LiteralCommandNode; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.SparkPlugin; import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.forge.ForgeSparkMod; import net.minecraft.command.ICommandSource; import java.nio.file.Path; @@ -38,14 +40,20 @@ import java.util.concurrent.ScheduledExecutorService; public abstract class ForgeSparkPlugin implements SparkPlugin { public static <T> void registerCommands(CommandDispatcher<T> dispatcher, Command<T> executor, String... aliases) { - for (String alias : aliases) { - LiteralArgumentBuilder<T> command = LiteralArgumentBuilder.<T>literal(alias) - .executes(executor) - .then(RequiredArgumentBuilder.<T, String>argument("args", StringArgumentType.greedyString()) - .executes(executor) - ); + 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()) + .executes(executor) + ); - dispatcher.register(command); + LiteralCommandNode<T> node = dispatcher.register(command); + for (int i = 1; i < aliases.length; i++) { + dispatcher.register(LiteralArgumentBuilder.<T>literal(aliases[i]).redirect(node)); } } @@ -62,7 +70,7 @@ public abstract class ForgeSparkPlugin implements SparkPlugin { this.platform.enable(); } - abstract boolean hasPermission(ICommandSource sender, String permission); + public abstract boolean hasPermission(ICommandSource sender, String permission); @Override public String getVersion() { |