aboutsummaryrefslogtreecommitdiff
path: root/spark-forge/src/main/java/me/lucko
diff options
context:
space:
mode:
Diffstat (limited to 'spark-forge/src/main/java/me/lucko')
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientCommandHandler.java43
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandHandler.java116
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerCommandHandler.java46
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java63
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/SparkForgeMod.java30
5 files changed, 298 insertions, 0 deletions
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientCommandHandler.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientCommandHandler.java
new file mode 100644
index 0000000..5fbf524
--- /dev/null
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientCommandHandler.java
@@ -0,0 +1,43 @@
+package me.lucko.spark.forge;
+
+import me.lucko.spark.profiler.TickCounter;
+
+import net.minecraft.client.Minecraft;
+import net.minecraft.util.text.ITextComponent;
+import net.minecraftforge.client.ClientCommandHandler;
+import net.minecraftforge.fml.common.gameevent.TickEvent;
+
+import java.util.Collections;
+import java.util.List;
+
+public class ForgeClientCommandHandler extends ForgeCommandHandler {
+
+ public static void register() {
+ ClientCommandHandler.instance.registerCommand(new ForgeClientCommandHandler());
+ }
+
+ @Override
+ protected void broadcast(ITextComponent msg) {
+ Minecraft.getMinecraft().player.sendMessage(msg);
+ }
+
+ @Override
+ protected TickCounter newTickCounter() {
+ return new ForgeTickCounter(TickEvent.Type.CLIENT);
+ }
+
+ @Override
+ public String getLabel() {
+ return "sparkclient";
+ }
+
+ @Override
+ public String getName() {
+ return "sparkclient";
+ }
+
+ @Override
+ public List<String> getAliases() {
+ return Collections.singletonList("cprofiler");
+ }
+}
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandHandler.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandHandler.java
new file mode 100644
index 0000000..63ec201
--- /dev/null
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandHandler.java
@@ -0,0 +1,116 @@
+package me.lucko.spark.forge;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+
+import me.lucko.spark.common.CommandHandler;
+import me.lucko.spark.profiler.ThreadDumper;
+
+import net.kyori.text.TextComponent;
+import net.kyori.text.serializer.ComponentSerializers;
+import net.minecraft.command.CommandException;
+import net.minecraft.command.ICommand;
+import net.minecraft.command.ICommandSender;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.util.math.BlockPos;
+import net.minecraft.util.text.ITextComponent;
+import net.minecraft.util.text.Style;
+import net.minecraft.util.text.TextComponentString;
+import net.minecraft.util.text.TextFormatting;
+import net.minecraft.util.text.event.ClickEvent;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import javax.annotation.Nullable;
+
+@SuppressWarnings("NullableProblems")
+public abstract class ForgeCommandHandler extends CommandHandler<ICommandSender> implements ICommand {
+
+ private final ExecutorService worker = Executors.newSingleThreadExecutor(
+ new ThreadFactoryBuilder().setNameFormat("spark-forge-async-worker").build()
+ );
+
+ @SuppressWarnings("deprecation")
+ protected ITextComponent colorize(String message) {
+ TextComponent component = ComponentSerializers.LEGACY.deserialize(message, '&');
+ return ITextComponent.Serializer.jsonToComponent(ComponentSerializers.JSON.serialize(component));
+ }
+
+ protected abstract void broadcast(ITextComponent msg);
+
+ @Override
+ protected void sendMessage(ICommandSender sender, String message) {
+ sender.sendMessage(colorize(message));
+ }
+
+ @Override
+ protected void sendMessage(String message) {
+ ITextComponent msg = colorize(message);
+ broadcast(msg);
+ }
+
+ @Override
+ protected void sendLink(String url) {
+ TextComponentString msg = new TextComponentString(url);
+ Style style = msg.getStyle();
+ style.setColor(TextFormatting.GRAY);
+ style.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
+ msg.setStyle(style);
+
+ broadcast(msg);
+ }
+
+ @Override
+ protected void runAsync(Runnable r) {
+ worker.execute(r);
+ }
+
+ @Override
+ protected ThreadDumper getDefaultThreadDumper() {
+ return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()});
+ }
+
+ // implement ICommand
+
+ @Override
+ public String getUsage(ICommandSender iCommandSender) {
+ return "/" + getLabel();
+ }
+
+ @Override
+ public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
+ if (!checkPermission(server, sender)) {
+ TextComponentString msg = new TextComponentString("You do not have permission to use this command.");
+ Style style = msg.getStyle();
+ style.setColor(TextFormatting.GRAY);
+ msg.setStyle(style);
+
+ sender.sendMessage(msg);
+ return;
+ }
+
+ handleCommand(sender, args);
+ }
+
+ @Override
+ public boolean checkPermission(MinecraftServer server, ICommandSender sender) {
+ return sender.canUseCommand(4, "spark.profiler");
+ }
+
+ @Override
+ public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos blockPos) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public boolean isUsernameIndex(String[] strings, int i) {
+ return false;
+ }
+
+ @Override
+ public int compareTo(ICommand o) {
+ return getLabel().compareTo(o.getName());
+ }
+}
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerCommandHandler.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerCommandHandler.java
new file mode 100644
index 0000000..40c4989
--- /dev/null
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerCommandHandler.java
@@ -0,0 +1,46 @@
+package me.lucko.spark.forge;
+
+import me.lucko.spark.profiler.TickCounter;
+
+import net.minecraft.entity.player.EntityPlayerMP;
+import net.minecraft.util.text.ITextComponent;
+import net.minecraftforge.fml.common.FMLCommonHandler;
+import net.minecraftforge.fml.common.gameevent.TickEvent;
+
+import java.util.Collections;
+import java.util.List;
+
+public class ForgeServerCommandHandler extends ForgeCommandHandler {
+
+ @Override
+ protected void broadcast(ITextComponent msg) {
+ FMLCommonHandler.instance().getMinecraftServerInstance().sendMessage(msg);
+
+ List<EntityPlayerMP> players = FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers();
+ for (EntityPlayerMP player : players) {
+ if (player.canUseCommand(4, "spark.profiler")) {
+ player.sendMessage(msg);
+ }
+ }
+ }
+
+ @Override
+ protected TickCounter newTickCounter() {
+ return new ForgeTickCounter(TickEvent.Type.SERVER);
+ }
+
+ @Override
+ public String getLabel() {
+ return "spark";
+ }
+
+ @Override
+ public String getName() {
+ return "spark";
+ }
+
+ @Override
+ public List<String> getAliases() {
+ return Collections.singletonList("profiler");
+ }
+}
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
new file mode 100644
index 0000000..b113bcf
--- /dev/null
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java
@@ -0,0 +1,63 @@
+package me.lucko.spark.forge;
+
+import me.lucko.spark.profiler.TickCounter;
+
+import net.minecraftforge.common.MinecraftForge;
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
+import net.minecraftforge.fml.common.gameevent.TickEvent;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.atomic.LongAdder;
+
+public class ForgeTickCounter implements TickCounter {
+ private final TickEvent.Type type;
+
+ private final Set<Runnable> tasks = new HashSet<>();
+ private final LongAdder tick = new LongAdder();
+
+ public ForgeTickCounter(TickEvent.Type type) {
+ this.type = type;
+ }
+
+ @SubscribeEvent
+ public void onTick(TickEvent e) {
+ if (e.phase != TickEvent.Phase.END) {
+ return;
+ }
+
+ if (e.type != this.type) {
+ return;
+ }
+
+ this.tick.increment();
+ for (Runnable r : this.tasks){
+ r.run();
+ }
+ }
+
+ @Override
+ public void start() {
+ MinecraftForge.EVENT_BUS.register(this);
+ }
+
+ @Override
+ public void close() {
+ MinecraftForge.EVENT_BUS.unregister(this);
+ }
+
+ @Override
+ public long getCurrentTick() {
+ return this.tick.longValue();
+ }
+
+ @Override
+ public void addTickTask(Runnable runnable) {
+ this.tasks.add(runnable);
+ }
+
+ @Override
+ public void removeTickTask(Runnable runnable) {
+ this.tasks.remove(runnable);
+ }
+}
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/SparkForgeMod.java b/spark-forge/src/main/java/me/lucko/spark/forge/SparkForgeMod.java
new file mode 100644
index 0000000..39c1f6f
--- /dev/null
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/SparkForgeMod.java
@@ -0,0 +1,30 @@
+package me.lucko.spark.forge;
+
+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.FMLServerStartingEvent;
+import net.minecraftforge.fml.relauncher.Side;
+
+@Mod(
+ modid = "spark",
+ name = "spark",
+ version = "@version@",
+ acceptableRemoteVersions = "*"
+)
+public class SparkForgeMod {
+
+ @EventHandler
+ public void init(FMLInitializationEvent e) {
+ if (FMLCommonHandler.instance().getSide() == Side.CLIENT) {
+ ForgeClientCommandHandler.register();
+ }
+ }
+
+ @EventHandler
+ public void serverInit(FMLServerStartingEvent e) {
+ e.registerServerCommand(new ForgeServerCommandHandler());
+ }
+
+}