diff options
author | Luck <git@lucko.me> | 2019-08-25 22:04:03 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2019-08-25 22:04:03 +0100 |
commit | f8398d61af0bbfc63c36f706010b0dfea54a8b6b (patch) | |
tree | 2a8ab5a15eee1097cb9b2307c04424d17cc27162 /spark-forge | |
parent | c8d631f65ca40b65e392297523d43884a8260797 (diff) | |
download | spark-f8398d61af0bbfc63c36f706010b0dfea54a8b6b.tar.gz spark-f8398d61af0bbfc63c36f706010b0dfea54a8b6b.tar.bz2 spark-f8398d61af0bbfc63c36f706010b0dfea54a8b6b.zip |
Update spark-forge to 1.14.4
Diffstat (limited to 'spark-forge')
10 files changed, 233 insertions, 137 deletions
diff --git a/spark-forge/build.gradle b/spark-forge/build.gradle index 4334b87..6b55901 100644 --- a/spark-forge/build.gradle +++ b/spark-forge/build.gradle @@ -3,7 +3,7 @@ buildscript { maven { url = "http://files.minecraftforge.net/maven" } } dependencies { - classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT' + classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true } } @@ -11,15 +11,10 @@ plugins { id 'com.github.johnrengelman.shadow' version '4.0.1' } -apply plugin: 'net.minecraftforge.gradle.forge' +apply plugin: 'net.minecraftforge.gradle' minecraft { - version = '1.12.2-14.23.4.2705' - runDir = 'run' - mappings = 'snapshot_20171003' - - replaceIn 'me/lucko/spark/forge/ForgeSparkMod.java' - replace '@version@', project.pluginVersion + mappings channel: 'snapshot', version: '20190801-1.14.3' } processResources { @@ -28,16 +23,27 @@ processResources { 'pluginVersion': project.pluginVersion, 'pluginDescription': project.pluginDescription ) - include 'mcmod.info' + include 'META-INF/mods.toml' + } + + from(sourceSets.main.resources.srcDirs) { + exclude 'META-INF/mods.toml' } } +configurations { + shade + compile.extendsFrom shade +} + dependencies { - compile project(':spark-common') + minecraft 'net.minecraftforge:forge:1.14.4-28.0.45' + shade project(':spark-common') } shadowJar { archiveName = 'spark-forge.jar' + configurations = [project.configurations.shade] relocate 'okio', 'me.lucko.spark.lib.okio' relocate 'okhttp3', 'me.lucko.spark.lib.okhttp3' @@ -53,6 +59,7 @@ artifacts { reobf { shadowJar { - mappingType = 'SEARGE' + dependsOn createMcpToSrg + mappings = createMcpToSrg.outputs.files.singleFile } } diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlugin.java index e4212ab..0d38ea2 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlugin.java @@ -20,34 +20,95 @@ package me.lucko.spark.forge; +import com.mojang.brigadier.CommandDispatcher; import me.lucko.spark.common.sampler.TickCounter; import net.minecraft.client.Minecraft; -import net.minecraft.command.ICommandSender; -import net.minecraftforge.client.ClientCommandHandler; -import net.minecraftforge.fml.common.gameevent.TickEvent; +import net.minecraft.client.entity.player.ClientPlayerEntity; +import net.minecraft.client.network.play.ClientPlayNetHandler; +import net.minecraft.command.ICommandSource; +import net.minecraft.command.ISuggestionProvider; +import net.minecraftforge.client.event.ClientChatEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; -import java.util.Collections; -import java.util.List; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.concurrent.TimeUnit; import java.util.stream.Stream; public class ForgeClientSparkPlugin extends ForgeSparkPlugin { - public static void register(ForgeSparkMod mod) { - ClientCommandHandler.instance.registerCommand(new ForgeClientSparkPlugin(mod)); + private static final Field COMMAND_DISPATCHER_FIELD; + static { + COMMAND_DISPATCHER_FIELD = Arrays.stream(ClientPlayNetHandler.class.getDeclaredFields()) + .filter(f -> f.getType() == CommandDispatcher.class) + .findFirst().orElseThrow(() -> new RuntimeException("No field with CommandDispatcher type")); + COMMAND_DISPATCHER_FIELD.setAccessible(true); } - public ForgeClientSparkPlugin(ForgeSparkMod mod) { + public static void register(ForgeSparkMod mod, FMLClientSetupEvent event) { + Minecraft minecraft = event.getMinecraftSupplier().get(); + + ForgeClientSparkPlugin plugin = new ForgeClientSparkPlugin(mod, minecraft); + MinecraftForge.EVENT_BUS.register(plugin); + + plugin.scheduler.scheduleWithFixedDelay(plugin::checkCommandRegistered, 10, 10, TimeUnit.SECONDS); + } + + private final Minecraft minecraft; + private CommandDispatcher<ISuggestionProvider> dispatcher; + + public ForgeClientSparkPlugin(ForgeSparkMod mod, Minecraft minecraft) { super(mod); + this.minecraft = minecraft; + } + + private void checkCommandRegistered() { + ClientPlayerEntity player = this.minecraft.player; + if (player == null) { + return; + } + + ClientPlayNetHandler connection = player.connection; + if (connection == null) { + return; + } + + try { + CommandDispatcher<ISuggestionProvider> dispatcher = (CommandDispatcher) COMMAND_DISPATCHER_FIELD.get(connection); + if (dispatcher != this.dispatcher) { + this.dispatcher = dispatcher; + registerCommands(this.dispatcher, context -> 1, "sparkc", "sparkclient"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + @SubscribeEvent + public void onClientChat(ClientChatEvent event) { + String chat = event.getMessage(); + String[] split = chat.split(" "); + if (split.length == 0 || (!split[0].equals("/sparkc") && !split[0].equals("/sparkclient"))) { + return; + } + + String[] args = Arrays.copyOfRange(split, 1, split.length); + this.platform.executeCommand(new ForgeCommandSender(this.minecraft.player, this), args); + this.minecraft.ingameGUI.getChatGUI().addToSentMessages(chat); + event.setCanceled(true); } @Override - boolean hasPermission(ICommandSender sender, String permission) { + boolean hasPermission(ICommandSource sender, String permission) { return true; } @Override public Stream<ForgeCommandSender> getSendersWithPermission(String permission) { - return Stream.of(new ForgeCommandSender(Minecraft.getMinecraft().player, this)); + return Stream.of(new ForgeCommandSender(this.minecraft.player, this)); } @Override @@ -60,13 +121,4 @@ public class ForgeClientSparkPlugin extends ForgeSparkPlugin { return "sparkc"; } - @Override - public String getName() { - return "sparkc"; - } - - @Override - public List<String> getAliases() { - return Collections.singletonList("sparkclient"); - } } 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 f7b2122..418cf2d 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 @@ -23,37 +23,44 @@ package me.lucko.spark.forge; import me.lucko.spark.common.CommandSender; import net.kyori.text.Component; import net.kyori.text.serializer.gson.GsonComponentSerializer; -import net.minecraft.command.ICommandSender; -import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.command.ICommandSource; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.network.rcon.IServer; import net.minecraft.util.text.ITextComponent; import java.util.UUID; public class ForgeCommandSender implements CommandSender { - private final ICommandSender sender; + private final ICommandSource sender; private final ForgeSparkPlugin plugin; - public ForgeCommandSender(ICommandSender sender, ForgeSparkPlugin plugin) { + public ForgeCommandSender(ICommandSource sender, ForgeSparkPlugin plugin) { this.sender = sender; this.plugin = plugin; } @Override public String getName() { - return this.sender.getName(); + if (this.sender instanceof PlayerEntity) { + return ((PlayerEntity) this.sender).getGameProfile().getName(); + } else if (this.sender instanceof IServer) { + return "Console"; + } else { + return "unknown:" + this.sender.getClass().getSimpleName(); + } } @Override public UUID getUniqueId() { - if (this.sender instanceof EntityPlayer) { - return ((EntityPlayer) this.sender).getUniqueID(); + if (this.sender instanceof PlayerEntity) { + return ((PlayerEntity) this.sender).getUniqueID(); } return null; } @Override public void sendMessage(Component message) { - ITextComponent component = ITextComponent.Serializer.jsonToComponent(GsonComponentSerializer.INSTANCE.serialize(message)); + ITextComponent component = ITextComponent.Serializer.fromJson(GsonComponentSerializer.INSTANCE.serialize(message)); this.sender.sendMessage(component); } diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlugin.java index 22b6adc..13ccb59 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlugin.java @@ -20,32 +20,68 @@ package me.lucko.spark.forge; +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 net.minecraft.command.ICommandSender; +import net.minecraft.command.CommandSource; +import net.minecraft.command.ICommandSource; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.MinecraftServer; -import net.minecraftforge.fml.common.FMLCommonHandler; -import net.minecraftforge.fml.common.gameevent.TickEvent; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.fml.event.server.FMLServerStartingEvent; +import net.minecraftforge.server.permission.DefaultPermissionLevel; +import net.minecraftforge.server.permission.PermissionAPI; -import java.util.Collections; -import java.util.List; +import java.util.Arrays; import java.util.stream.Stream; -public class ForgeServerSparkPlugin extends ForgeSparkPlugin { - public ForgeServerSparkPlugin(ForgeSparkMod mod) { +public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command<CommandSource> { + + public static void register(ForgeSparkMod mod, FMLServerStartingEvent event) { + MinecraftServer server = event.getServer(); + CommandDispatcher<CommandSource> dispatcher = event.getCommandDispatcher(); + + ForgeServerSparkPlugin plugin = new ForgeServerSparkPlugin(mod, server); + registerCommands(dispatcher, plugin, "spark"); + PermissionAPI.registerNode("spark", DefaultPermissionLevel.OP, "Access to the spark command"); + } + + private final MinecraftServer server; + + public ForgeServerSparkPlugin(ForgeSparkMod mod, MinecraftServer server) { super(mod); + this.server = server; } @Override - boolean hasPermission(ICommandSender sender, String permission) { - return sender.canUseCommand(4, permission); + public int run(CommandContext<CommandSource> context) throws CommandSyntaxException { + String[] split = context.getInput().split(" "); + if (split.length == 0 || !split[0].equals("/spark")) { + return 0; + } + + String[] args = Arrays.copyOfRange(split, 1, split.length); + + this.platform.executeCommand(new ForgeCommandSender(context.getSource().asPlayer(), this), args); + return 1; + } + + @Override + boolean hasPermission(ICommandSource sender, String permission) { + if (sender instanceof PlayerEntity) { + return PermissionAPI.hasPermission((PlayerEntity) sender, permission); + } else { + return true; + } } @Override public Stream<ForgeCommandSender> getSendersWithPermission(String permission) { - MinecraftServer mcServer = FMLCommonHandler.instance().getMinecraftServerInstance(); return Stream.concat( - mcServer.getPlayerList().getPlayers().stream().filter(player -> player.canUseCommand(4, permission)), - Stream.of(mcServer) + this.server.getPlayerList().getPlayers().stream().filter(player -> PermissionAPI.hasPermission(player, permission)), + Stream.of(this.server) ).map(sender -> new ForgeCommandSender(sender, this)); } @@ -58,14 +94,4 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin { public String getCommandName() { return "spark"; } - - @Override - public String getName() { - return "spark"; - } - - @Override - public List<String> getAliases() { - return Collections.emptyList(); - } } 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 1659d6d..7a3cafe 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,41 +20,52 @@ package me.lucko.spark.forge; -import net.minecraftforge.fml.common.FMLCommonHandler; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModContainer; +import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.common.Mod; -import net.minecraftforge.fml.common.Mod.EventHandler; -import net.minecraftforge.fml.common.event.FMLInitializationEvent; -import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; -import net.minecraftforge.fml.common.event.FMLServerStartingEvent; -import net.minecraftforge.fml.relauncher.Side; +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 org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.nio.file.Path; -@Mod( - modid = "spark", - name = "spark", - version = "@version@", - acceptableRemoteVersions = "*" -) +@Mod("spark") public class ForgeSparkMod { - private Path configDirectory = null; + private static final Logger LOGGER = LogManager.getLogger(); - @EventHandler - public void preInit(FMLPreInitializationEvent e) { - this.configDirectory = e.getModConfigurationDirectory().toPath(); + private ModContainer container; + private Path configDirectory; + + public ForgeSparkMod() { + FMLJavaModLoadingContext.get().getModEventBus().register(this); + MinecraftForge.EVENT_BUS.register(this); } - @EventHandler - public void init(FMLInitializationEvent e) { - if (FMLCommonHandler.instance().getSide() == Side.CLIENT) { - ForgeClientSparkPlugin.register(this); - } + public String getVersion() { + return this.container.getModInfo().getVersion().toString(); + } + + @SubscribeEvent + public void setup(FMLCommonSetupEvent e) { + this.container = ModLoadingContext.get().getActiveContainer(); + this.configDirectory = FMLPaths.CONFIGDIR.get().resolve(this.container.getModId()); + } + + @SubscribeEvent + public void clientInit(FMLClientSetupEvent e) { + ForgeClientSparkPlugin.register(this, e); } - @EventHandler + @SubscribeEvent public void serverInit(FMLServerStartingEvent e) { - e.registerServerCommand(new ForgeServerSparkPlugin(this)); + ForgeServerSparkPlugin.register(this, e); } public Path getConfigDirectory() { diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlugin.java index b2f2332..7141e8d 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlugin.java @@ -21,27 +21,37 @@ package me.lucko.spark.forge; import com.google.common.util.concurrent.ThreadFactoryBuilder; +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.SparkPlugin; import me.lucko.spark.common.sampler.ThreadDumper; -import net.minecraft.command.ICommand; -import net.minecraft.command.ICommandSender; -import net.minecraft.server.MinecraftServer; -import net.minecraft.util.math.BlockPos; -import net.minecraftforge.fml.common.Mod; +import net.minecraft.command.ICommandSource; -import javax.annotation.Nullable; import java.nio.file.Path; -import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; -@SuppressWarnings("NullableProblems") -public abstract class ForgeSparkPlugin implements SparkPlugin, ICommand { +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) + ); + + dispatcher.register(command); + } + } private final ForgeSparkMod mod; - private final ScheduledExecutorService scheduler; - private final SparkPlatform platform; + protected final ScheduledExecutorService scheduler; + protected final SparkPlatform platform; protected ForgeSparkPlugin(ForgeSparkMod mod) { this.mod = mod; @@ -52,11 +62,11 @@ public abstract class ForgeSparkPlugin implements SparkPlugin, ICommand { this.platform.enable(); } - abstract boolean hasPermission(ICommandSender sender, String permission); + abstract boolean hasPermission(ICommandSource sender, String permission); @Override public String getVersion() { - return ForgeSparkMod.class.getAnnotation(Mod.class).version(); + return this.mod.getVersion(); } @Override @@ -73,36 +83,4 @@ public abstract class ForgeSparkPlugin implements SparkPlugin, ICommand { public ThreadDumper getDefaultThreadDumper() { return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()}); } - - // implement ICommand - - @Override - public String getUsage(ICommandSender iCommandSender) { - return "/" + getCommandName(); - } - - @Override - public void execute(MinecraftServer server, ICommandSender sender, String[] args) { - this.platform.executeCommand(new ForgeCommandSender(sender, this), args); - } - - @Override - public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos blockPos) { - return this.platform.tabCompleteCommand(new ForgeCommandSender(sender, this), args); - } - - @Override - public boolean checkPermission(MinecraftServer minecraftServer, ICommandSender sender) { - return hasPermission(sender, "spark"); - } - - @Override - public boolean isUsernameIndex(String[] strings, int i) { - return false; - } - - @Override - public int compareTo(ICommand o) { - return getCommandName().compareTo(o.getName()); - } } diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java index de7d846..2ff3f1d 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java @@ -22,8 +22,8 @@ package me.lucko.spark.forge; import me.lucko.spark.common.sampler.TickCounter; import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import net.minecraftforge.fml.common.gameevent.TickEvent; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; import java.util.HashSet; import java.util.Set; diff --git a/spark-forge/src/main/resources/META-INF/mods.toml b/spark-forge/src/main/resources/META-INF/mods.toml new file mode 100644 index 0000000..3c57015 --- /dev/null +++ b/spark-forge/src/main/resources/META-INF/mods.toml @@ -0,0 +1,16 @@ +modLoader="javafml" +loaderVersion="[28,)" +authors="Luck, sk89q" + +[[mods]] +modId="spark" +displayName="spark" +version="${pluginVersion}" +description="${pluginDescription}" + +[[dependencies.spark]] + modId="forge" + mandatory=true + versionRange="[28,)" + ordering="NONE" + side="BOTH"
\ No newline at end of file diff --git a/spark-forge/src/main/resources/mcmod.info b/spark-forge/src/main/resources/mcmod.info deleted file mode 100644 index 6b3be4c..0000000 --- a/spark-forge/src/main/resources/mcmod.info +++ /dev/null @@ -1,7 +0,0 @@ -[{ - "modid": "spark", - "name": "spark", - "description": "${pluginDescription}", - "version": "${pluginVersion}", - "authors": ["Luck", "sk89q"] -}]
\ No newline at end of file diff --git a/spark-forge/src/main/resources/pack.mcmeta b/spark-forge/src/main/resources/pack.mcmeta new file mode 100644 index 0000000..6dd0930 --- /dev/null +++ b/spark-forge/src/main/resources/pack.mcmeta @@ -0,0 +1,6 @@ +{ + "pack": { + "description": "spark resources", + "pack_format": 4 + } +}
\ No newline at end of file |