From 9bec2b79880bf0a814b6a1c8616f6a0c9d03bfc6 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 9 Jan 2021 23:55:57 +0000 Subject: Re-add module for Forge 1.12.2 #26 --- settings.gradle | 1 + spark-common/build.gradle | 7 +- spark-forge1122/build.gradle | 80 +++++++++++++ .../me/lucko/spark/forge/ForgeCommandSender.java | 75 ++++++++++++ .../me/lucko/spark/forge/ForgePlatformInfo.java | 53 ++++++++ .../java/me/lucko/spark/forge/ForgeSparkMod.java | 73 +++++++++++ .../java/me/lucko/spark/forge/ForgeTickHook.java | 60 ++++++++++ .../me/lucko/spark/forge/ForgeTickReporter.java | 72 +++++++++++ .../spark/forge/plugin/ForgeClientSparkPlugin.java | 90 ++++++++++++++ .../spark/forge/plugin/ForgeServerSparkPlugin.java | 108 +++++++++++++++++ .../lucko/spark/forge/plugin/ForgeSparkPlugin.java | 133 +++++++++++++++++++++ spark-forge1122/src/main/resources/mcmod.info | 7 ++ 12 files changed, 756 insertions(+), 3 deletions(-) create mode 100644 spark-forge1122/build.gradle create mode 100644 spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java create mode 100644 spark-forge1122/src/main/java/me/lucko/spark/forge/ForgePlatformInfo.java create mode 100644 spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java create mode 100644 spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeTickHook.java create mode 100644 spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java create mode 100644 spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java create mode 100644 spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java create mode 100644 spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java create mode 100644 spark-forge1122/src/main/resources/mcmod.info diff --git a/settings.gradle b/settings.gradle index 4457134..8fa5172 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,6 +17,7 @@ include ( 'spark-velocity', 'spark-sponge', 'spark-forge', + 'spark-forge1122', 'spark-fabric', 'spark-universal' ) diff --git a/spark-common/build.gradle b/spark-common/build.gradle index bde2ecf..d251cb4 100644 --- a/spark-common/build.gradle +++ b/spark-common/build.gradle @@ -1,10 +1,10 @@ plugins { - id 'com.google.protobuf' version '0.8.8' + id 'com.google.protobuf' version '0.8.14' } dependencies { compile 'org.ow2.asm:asm:7.1' - compile 'com.google.protobuf:protobuf-java:3.0.0' + compile 'com.google.protobuf:protobuf-java:3.14.0' compile 'com.squareup.okhttp3:okhttp:3.14.1' compile 'com.squareup.okio:okio:1.17.3' compile 'org.tukaani:xz:1.8' @@ -27,6 +27,7 @@ dependencies { protobuf { protoc { - artifact = 'com.google.protobuf:protoc:3.0.0' + artifact = 'com.google.protobuf:protoc:3.14.0' + path = '/opt/homebrew/bin/protoc' // required to support building on osx-aarch64 } } diff --git a/spark-forge1122/build.gradle b/spark-forge1122/build.gradle new file mode 100644 index 0000000..d0bd14c --- /dev/null +++ b/spark-forge1122/build.gradle @@ -0,0 +1,80 @@ +buildscript { + repositories { + maven { url = "http://files.minecraftforge.net/maven" } + } + dependencies { + classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true + } +} + +plugins { + id 'net.kyori.blossom' version '1.1.0' + id 'com.github.johnrengelman.shadow' version '4.0.1' +} + +apply plugin: 'net.minecraftforge.gradle' + +minecraft { + mappings channel: 'snapshot', version: '20171003-1.12' +} + +configurations { + shade + compile.extendsFrom shade +} + +// https://github.com/MinecraftForge/ForgeGradle/issues/627#issuecomment-533927535 +configurations.all { + resolutionStrategy { + force 'org.lwjgl.lwjgl:lwjgl-platform:2.9.4-nightly-20150209' + } +} + +dependencies { + minecraft 'net.minecraftforge:forge:1.12.2-14.23.5.2854' + shade project(':spark-common') +} + +processResources { + from(sourceSets.main.resources.srcDirs) { + include 'mcmod.info' + expand ( + 'pluginVersion': project.pluginVersion, + 'pluginDescription': project.pluginDescription + ) + } +} + +blossom { + replaceTokenIn('src/main/java/me/lucko/spark/forge/ForgeSparkMod.java') + replaceToken '@version@', project.pluginVersion +} + +shadowJar { + archiveName = 'spark-forge1122.jar' + configurations = [project.configurations.shade] + + relocate 'okio', 'me.lucko.spark.lib.okio' + relocate 'okhttp3', 'me.lucko.spark.lib.okhttp3' + relocate 'net.kyori.adventure', 'me.lucko.spark.lib.adventure' + relocate 'net.kyori.examination', 'me.lucko.spark.lib.adventure.examination' + relocate 'org.tukaani.xz', 'me.lucko.spark.lib.xz' + relocate 'com.google.protobuf', 'me.lucko.spark.lib.protobuf' + relocate 'org.objectweb.asm', 'me.lucko.spark.lib.asm' + + exclude 'module-info.class' + exclude 'META-INF/maven/**' + exclude 'META-INF/proguard/**' +} + +artifacts { + archives shadowJar + shadow shadowJar +} + +reobf { + shadowJar { + dependsOn createMcpToSrg + mappings = createMcpToSrg.outputs.files.singleFile + } +} diff --git a/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java b/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java new file mode 100644 index 0000000..3d07a44 --- /dev/null +++ b/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java @@ -0,0 +1,75 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.forge; + +import me.lucko.spark.common.command.sender.AbstractCommandSender; +import me.lucko.spark.forge.plugin.ForgeSparkPlugin; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.network.rcon.RConConsoleSource; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.text.ITextComponent; + +import java.util.UUID; + +public class ForgeCommandSender extends AbstractCommandSender { + private final ForgeSparkPlugin plugin; + + public ForgeCommandSender(ICommandSender source, ForgeSparkPlugin plugin) { + super(source); + this.plugin = plugin; + } + + @Override + public String getName() { + if (super.delegate instanceof EntityPlayer) { + return ((EntityPlayer) super.delegate).getGameProfile().getName(); + } else if (super.delegate instanceof MinecraftServer) { + return "Console"; + } else if (super.delegate instanceof RConConsoleSource) { + return "RCON Console"; + } else { + return "unknown:" + super.delegate.getClass().getSimpleName(); + } + } + + @Override + public UUID getUniqueId() { + if (super.delegate instanceof EntityPlayer) { + return ((EntityPlayer) super.delegate).getUniqueID(); + } + return null; + } + + @Override + public void sendMessage(Component message) { + ITextComponent component = ITextComponent.Serializer.jsonToComponent(GsonComponentSerializer.gson().serialize(message)); + super.delegate.sendMessage(component); + } + + @Override + public boolean hasPermission(String permission) { + return this.plugin.hasPermission(super.delegate, permission); + } +} diff --git a/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgePlatformInfo.java b/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgePlatformInfo.java new file mode 100644 index 0000000..128c314 --- /dev/null +++ b/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgePlatformInfo.java @@ -0,0 +1,53 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.forge; + +import me.lucko.spark.common.platform.AbstractPlatformInfo; + +import net.minecraftforge.common.ForgeVersion; + +public class ForgePlatformInfo extends AbstractPlatformInfo { + private final Type type; + + public ForgePlatformInfo(Type type) { + this.type = type; + } + + @Override + public Type getType() { + return type; + } + + @Override + public String getName() { + return "Forge"; + } + + @Override + public String getVersion() { + return ForgeVersion.getVersion(); + } + + @Override + public String getMinecraftVersion() { + return ForgeVersion.mcVersion; + } +} diff --git a/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java b/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java new file mode 100644 index 0000000..2c19659 --- /dev/null +++ b/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeSparkMod.java @@ -0,0 +1,73 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.forge; + +import me.lucko.spark.forge.plugin.ForgeClientSparkPlugin; +import me.lucko.spark.forge.plugin.ForgeServerSparkPlugin; + +import net.minecraftforge.fml.common.FMLCommonHandler; +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 java.nio.file.Path; + +@Mod( + modid = "spark", + name = "spark", + version = "@version@", + acceptableRemoteVersions = "*" +) +public class ForgeSparkMod { + + private Path configDirectory; + + public String getVersion() { + return ForgeSparkMod.class.getAnnotation(Mod.class).version(); + } + + @EventHandler + public void preInit(FMLPreInitializationEvent e) { + this.configDirectory = e.getModConfigurationDirectory().toPath(); + } + + @EventHandler + public void init(FMLInitializationEvent e) { + if (FMLCommonHandler.instance().getSide() == Side.CLIENT) { + ForgeClientSparkPlugin.register(this); + } + } + + @EventHandler + public void serverInit(FMLServerStartingEvent e) { + ForgeServerSparkPlugin.register(this, e); + } + + public Path getConfigDirectory() { + if (this.configDirectory == null) { + throw new IllegalStateException("Config directory not set"); + } + return this.configDirectory; + } +} diff --git a/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeTickHook.java b/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeTickHook.java new file mode 100644 index 0000000..56acf47 --- /dev/null +++ b/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeTickHook.java @@ -0,0 +1,60 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.forge; + +import me.lucko.spark.common.sampler.tick.AbstractTickHook; +import me.lucko.spark.common.sampler.tick.TickHook; + +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; + +public class ForgeTickHook extends AbstractTickHook implements TickHook { + private final TickEvent.Type type; + + public ForgeTickHook(TickEvent.Type type) { + this.type = type; + } + + @SubscribeEvent + public void onTick(TickEvent e) { + if (e.phase != TickEvent.Phase.START) { + return; + } + + if (e.type != this.type) { + return; + } + + onTick(); + } + + @Override + public void start() { + MinecraftForge.EVENT_BUS.register(this); + } + + @Override + public void close() { + MinecraftForge.EVENT_BUS.unregister(this); + } + +} diff --git a/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java b/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java new file mode 100644 index 0000000..972f5c0 --- /dev/null +++ b/spark-forge1122/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java @@ -0,0 +1,72 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.forge; + +import me.lucko.spark.common.sampler.tick.AbstractTickReporter; +import me.lucko.spark.common.sampler.tick.TickReporter; + +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; + +public class ForgeTickReporter extends AbstractTickReporter implements TickReporter { + private final TickEvent.Type type; + + private long start = 0; + + public ForgeTickReporter(TickEvent.Type type) { + this.type = type; + } + + @SubscribeEvent + public void onTick(TickEvent e) { + if (e.type != this.type) { + return; + } + + switch (e.phase) { + case START: + this.start = System.nanoTime(); + break; + case END: + if (this.start == 0) { + return; + } + + double duration = (System.nanoTime() - this.start) / 1000000d; + onTick(duration); + break; + default: + throw new AssertionError(e.phase); + } + } + + @Override + public void start() { + MinecraftForge.EVENT_BUS.register(this); + } + + @Override + public void close() { + MinecraftForge.EVENT_BUS.unregister(this); + } + +} diff --git a/spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java b/spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java new file mode 100644 index 0000000..2e64df8 --- /dev/null +++ b/spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java @@ -0,0 +1,90 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.forge.plugin; + +import me.lucko.spark.common.platform.PlatformInfo; +import me.lucko.spark.common.sampler.tick.TickHook; +import me.lucko.spark.common.sampler.tick.TickReporter; +import me.lucko.spark.forge.ForgeCommandSender; +import me.lucko.spark.forge.ForgePlatformInfo; +import me.lucko.spark.forge.ForgeSparkMod; +import me.lucko.spark.forge.ForgeTickHook; +import me.lucko.spark.forge.ForgeTickReporter; + +import net.minecraft.client.Minecraft; +import net.minecraft.command.ICommandSender; +import net.minecraftforge.client.ClientCommandHandler; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.gameevent.TickEvent; + +import java.util.stream.Stream; + +public class ForgeClientSparkPlugin extends ForgeSparkPlugin { + + public static void register(ForgeSparkMod mod) { + ForgeClientSparkPlugin plugin = new ForgeClientSparkPlugin(mod, Minecraft.getMinecraft()); + plugin.enable(); + + // register listeners + MinecraftForge.EVENT_BUS.register(plugin); + + // register commands + ClientCommandHandler.instance.registerCommand(plugin); + } + + private final Minecraft minecraft; + + public ForgeClientSparkPlugin(ForgeSparkMod mod, Minecraft minecraft) { + super(mod); + this.minecraft = minecraft; + } + + @Override + public boolean hasPermission(ICommandSender sender, String permission) { + return true; + } + + @Override + public Stream getSendersWithPermission(String permission) { + return Stream.of(new ForgeCommandSender(this.minecraft.player, this)); + } + + @Override + public TickHook createTickHook() { + return new ForgeTickHook(TickEvent.Type.CLIENT); + } + + @Override + public TickReporter createTickReporter() { + return new ForgeTickReporter(TickEvent.Type.CLIENT); + } + + @Override + public PlatformInfo getPlatformInfo() { + return new ForgePlatformInfo(PlatformInfo.Type.CLIENT); + } + + @Override + public String getCommandName() { + return "sparkc"; + } + +} diff --git a/spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java b/spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java new file mode 100644 index 0000000..4cf1711 --- /dev/null +++ b/spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java @@ -0,0 +1,108 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.forge.plugin; + + +import me.lucko.spark.common.platform.PlatformInfo; +import me.lucko.spark.common.sampler.tick.TickHook; +import me.lucko.spark.common.sampler.tick.TickReporter; +import me.lucko.spark.forge.ForgeCommandSender; +import me.lucko.spark.forge.ForgePlatformInfo; +import me.lucko.spark.forge.ForgeSparkMod; +import me.lucko.spark.forge.ForgeTickHook; +import me.lucko.spark.forge.ForgeTickReporter; + +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.server.MinecraftServer; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.event.FMLServerStartingEvent; +import net.minecraftforge.fml.common.event.FMLServerStoppingEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; +import net.minecraftforge.server.permission.DefaultPermissionLevel; +import net.minecraftforge.server.permission.PermissionAPI; + +import java.util.stream.Stream; + +public class ForgeServerSparkPlugin extends ForgeSparkPlugin { + + public static void register(ForgeSparkMod mod, FMLServerStartingEvent event) { + ForgeServerSparkPlugin plugin = new ForgeServerSparkPlugin(mod, event.getServer()); + plugin.enable(); + + // register listeners + MinecraftForge.EVENT_BUS.register(plugin); + + // register commands & permissions + event.registerServerCommand(plugin); + 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; + } + + @SubscribeEvent + public void onDisable(FMLServerStoppingEvent event) { + disable(); + } + + @Override + public boolean hasPermission(ICommandSender sender, String permission) { + if (sender instanceof EntityPlayer) { + return PermissionAPI.hasPermission((EntityPlayer) sender, permission); + } else { + return true; + } + } + + @Override + public Stream getSendersWithPermission(String permission) { + return Stream.concat( + this.server.getPlayerList().getPlayers().stream().filter(player -> hasPermission(player, permission)), + Stream.of(this.server) + ).map(sender -> new ForgeCommandSender(sender, this)); + } + + @Override + public TickHook createTickHook() { + return new ForgeTickHook(TickEvent.Type.SERVER); + } + + @Override + public TickReporter createTickReporter() { + return new ForgeTickReporter(TickEvent.Type.SERVER); + } + + @Override + public PlatformInfo getPlatformInfo() { + return new ForgePlatformInfo(PlatformInfo.Type.SERVER); + } + + @Override + public String getCommandName() { + return "spark"; + } +} diff --git a/spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java b/spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java new file mode 100644 index 0000000..0b641c6 --- /dev/null +++ b/spark-forge1122/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java @@ -0,0 +1,133 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.forge.plugin; + +import me.lucko.spark.common.SparkPlatform; +import me.lucko.spark.common.SparkPlugin; +import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.forge.ForgeCommandSender; +import me.lucko.spark.forge.ForgeSparkMod; + +import net.minecraft.command.ICommand; +import net.minecraft.command.ICommandSender; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.math.BlockPos; + +import java.nio.file.Path; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; + +import javax.annotation.Nullable; + +public abstract class ForgeSparkPlugin implements SparkPlugin, ICommand { + + private final ForgeSparkMod mod; + protected final ScheduledExecutorService scheduler; + protected final SparkPlatform platform; + + protected ForgeSparkPlugin(ForgeSparkMod mod) { + this.mod = mod; + this.scheduler = Executors.newSingleThreadScheduledExecutor(r -> { + Thread thread = Executors.defaultThreadFactory().newThread(r); + thread.setName("spark-forge-async-worker"); + thread.setDaemon(true); + return thread; + }); + this.platform = new SparkPlatform(this); + this.platform.enable(); + } + + public void enable() { + this.platform.enable(); + } + + public void disable() { + this.platform.disable(); + this.scheduler.shutdown(); + } + + public abstract boolean hasPermission(ICommandSender sender, String permission); + + @Override + public String getVersion() { + return this.mod.getVersion(); + } + + @Override + public Path getPluginDirectory() { + return this.mod.getConfigDirectory(); + } + + @Override + public void executeAsync(Runnable task) { + this.scheduler.execute(task); + } + + @Override + public ThreadDumper getDefaultThreadDumper() { + return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()}); + } + + // implement ICommand + + @Override + public String getName() { + return getCommandName(); + } + + @Override + public String getUsage(ICommandSender iCommandSender) { + return "/" + getCommandName(); + } + + @Override + public List getAliases() { + return Collections.singletonList(getCommandName()); + } + + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) { + this.platform.executeCommand(new ForgeCommandSender(sender, this), args); + } + + @Override + public List 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-forge1122/src/main/resources/mcmod.info b/spark-forge1122/src/main/resources/mcmod.info new file mode 100644 index 0000000..ddca320 --- /dev/null +++ b/spark-forge1122/src/main/resources/mcmod.info @@ -0,0 +1,7 @@ +[{ + "modid": "spark", + "name": "spark", + "description": "${pluginDescription}", + "version": "${pluginVersion}", + "authors": ["Luck", "sk89q"] +}] -- cgit