From ecd4cec8545460a4fc4ca65b911c2503a00cd8e7 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 16 Apr 2019 21:37:59 +0100 Subject: Lots of refactoring, add tps command --- .../spark/forge/ForgeClientSparkPlatform.java | 74 ---------- .../lucko/spark/forge/ForgeClientSparkPlugin.java | 74 ++++++++++ .../spark/forge/ForgeServerSparkPlatform.java | 77 ----------- .../lucko/spark/forge/ForgeServerSparkPlugin.java | 71 ++++++++++ .../me/lucko/spark/forge/ForgeSparkPlatform.java | 151 --------------------- .../me/lucko/spark/forge/ForgeSparkPlugin.java | 141 +++++++++++++++++++ .../me/lucko/spark/forge/ForgeTickCounter.java | 13 +- .../java/me/lucko/spark/forge/SparkForgeMod.java | 4 +- 8 files changed, 294 insertions(+), 311 deletions(-) delete mode 100644 spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlatform.java create mode 100644 spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlugin.java delete mode 100644 spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlatform.java create mode 100644 spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlugin.java delete mode 100644 spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlatform.java create mode 100644 spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlugin.java (limited to 'spark-forge/src/main/java/me/lucko/spark') diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlatform.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlatform.java deleted file mode 100644 index c383636..0000000 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlatform.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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.sampler.TickCounter; - -import net.minecraft.client.Minecraft; -import net.minecraft.command.ICommandSender; -import net.minecraft.server.MinecraftServer; -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 ForgeClientSparkPlatform extends ForgeSparkPlatform { - - public static void register(SparkForgeMod mod) { - ClientCommandHandler.instance.registerCommand(new ForgeClientSparkPlatform(mod)); - } - - public ForgeClientSparkPlatform(SparkForgeMod mod) { - super(mod); - } - - @Override - protected void broadcast(ITextComponent msg) { - Minecraft.getMinecraft().player.sendMessage(msg); - } - - @Override - public TickCounter newTickCounter() { - return new ForgeTickCounter(TickEvent.Type.CLIENT); - } - - @Override - public String getLabel() { - return "sparkc"; - } - - @Override - public String getName() { - return "sparkc"; - } - - @Override - public List getAliases() { - return Collections.singletonList("sparkclient"); - } - - @Override - public boolean checkPermission(MinecraftServer server, ICommandSender sender) { - return true; - } -} 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 new file mode 100644 index 0000000..2c6c2fb --- /dev/null +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlugin.java @@ -0,0 +1,74 @@ +/* + * 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.TickCounter; +import net.minecraft.client.Minecraft; +import net.minecraft.command.ICommandSender; +import net.minecraft.server.MinecraftServer; +import net.minecraftforge.client.ClientCommandHandler; +import net.minecraftforge.fml.common.gameevent.TickEvent; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class ForgeClientSparkPlugin extends ForgeSparkPlugin { + + public static void register(SparkForgeMod mod) { + ClientCommandHandler.instance.registerCommand(new ForgeClientSparkPlugin(mod)); + } + + public ForgeClientSparkPlugin(SparkForgeMod mod) { + super(mod); + } + + @Override + public Set getSenders() { + return new HashSet<>(Collections.singleton(Minecraft.getMinecraft().player)); + } + + @Override + public TickCounter createTickCounter() { + return new ForgeTickCounter(TickEvent.Type.CLIENT); + } + + @Override + public String getLabel() { + return "sparkc"; + } + + @Override + public String getName() { + return "sparkc"; + } + + @Override + public List getAliases() { + return Collections.singletonList("sparkclient"); + } + + @Override + public boolean checkPermission(MinecraftServer server, ICommandSender sender) { + return true; + } +} diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlatform.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlatform.java deleted file mode 100644 index a5c6c01..0000000 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlatform.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.sampler.TickCounter; - -import net.minecraft.command.ICommandSender; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.server.MinecraftServer; -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 ForgeServerSparkPlatform extends ForgeSparkPlatform { - - public ForgeServerSparkPlatform(SparkForgeMod mod) { - super(mod); - } - - @Override - protected void broadcast(ITextComponent msg) { - FMLCommonHandler.instance().getMinecraftServerInstance().sendMessage(msg); - - List players = FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers(); - for (EntityPlayerMP player : players) { - if (player.canUseCommand(4, "spark")) { - player.sendMessage(msg); - } - } - } - - @Override - public TickCounter newTickCounter() { - return new ForgeTickCounter(TickEvent.Type.SERVER); - } - - @Override - public String getLabel() { - return "spark"; - } - - @Override - public String getName() { - return "spark"; - } - - @Override - public List getAliases() { - return Collections.emptyList(); - } - - @Override - public boolean checkPermission(MinecraftServer server, ICommandSender sender) { - return sender.canUseCommand(4, "spark"); - } -} 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 new file mode 100644 index 0000000..98740c0 --- /dev/null +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlugin.java @@ -0,0 +1,71 @@ +/* + * 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.TickCounter; +import net.minecraft.command.ICommandSender; +import net.minecraft.server.MinecraftServer; +import net.minecraftforge.fml.common.FMLCommonHandler; +import net.minecraftforge.fml.common.gameevent.TickEvent; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class ForgeServerSparkPlugin extends ForgeSparkPlugin { + public ForgeServerSparkPlugin(SparkForgeMod mod) { + super(mod); + } + + @Override + public Set getSenders() { + MinecraftServer mcServer = FMLCommonHandler.instance().getMinecraftServerInstance(); + Set senders = new HashSet<>(mcServer.getPlayerList().getPlayers()); + senders.add(mcServer); + return senders; + } + + @Override + public TickCounter createTickCounter() { + return new ForgeTickCounter(TickEvent.Type.SERVER); + } + + @Override + public String getLabel() { + return "spark"; + } + + @Override + public String getName() { + return "spark"; + } + + @Override + public List getAliases() { + return Collections.emptyList(); + } + + @Override + public boolean checkPermission(MinecraftServer server, ICommandSender sender) { + return sender.canUseCommand(4, "spark"); + } +} diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlatform.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlatform.java deleted file mode 100644 index 1f4c173..0000000 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlatform.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * 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 com.google.common.util.concurrent.ThreadFactoryBuilder; - -import me.lucko.spark.common.SparkPlatform; -import me.lucko.spark.sampler.ThreadDumper; - -import net.kyori.text.TextComponent; -import net.kyori.text.serializer.ComponentSerializers; -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 net.minecraftforge.fml.common.Mod; - -import java.nio.file.Path; -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 ForgeSparkPlatform extends SparkPlatform implements ICommand { - - private final SparkForgeMod mod; - - private final ExecutorService worker = Executors.newSingleThreadExecutor( - new ThreadFactoryBuilder().setNameFormat("spark-forge-async-worker").build() - ); - - protected ForgeSparkPlatform(SparkForgeMod mod) { - this.mod = mod; - } - - @Override - public String getVersion() { - return SparkForgeMod.class.getAnnotation(Mod.class).version(); - } - - @Override - public Path getPluginFolder() { - return this.mod.getConfigDirectory(); - } - - @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 - public void sendMessage(ICommandSender sender, String message) { - sender.sendMessage(colorize(message)); - } - - @Override - public void sendMessage(String message) { - ITextComponent msg = colorize(message); - broadcast(msg); - } - - @Override - public 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 - public void runAsync(Runnable r) { - this.worker.execute(r); - } - - @Override - public 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) { - 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; - } - - executeCommand(sender, args); - } - - @Override - public List getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos blockPos) { - if (!checkPermission(server, sender)) { - return Collections.emptyList(); - } - return tabCompleteCommand(sender, args); - } - - @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/ForgeSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlugin.java new file mode 100644 index 0000000..609894d --- /dev/null +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlugin.java @@ -0,0 +1,141 @@ +/* + * 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 com.google.common.util.concurrent.ThreadFactoryBuilder; +import me.lucko.spark.common.SparkPlatform; +import me.lucko.spark.common.SparkPlugin; +import me.lucko.spark.common.sampler.ThreadDumper; +import net.kyori.text.TextComponent; +import net.kyori.text.serializer.ComponentSerializers; +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 net.minecraftforge.fml.common.Mod; + +import javax.annotation.Nullable; +import java.nio.file.Path; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; + +@SuppressWarnings("NullableProblems") +public abstract class ForgeSparkPlugin implements SparkPlugin, ICommand { + + private final SparkForgeMod mod; + private final ScheduledExecutorService scheduler; + private final SparkPlatform platform; + + protected ForgeSparkPlugin(SparkForgeMod mod) { + this.mod = mod; + this.scheduler = Executors.newSingleThreadScheduledExecutor( + new ThreadFactoryBuilder().setNameFormat("spark-forge-async-worker").build() + ); + this.platform = new SparkPlatform<>(this); + this.platform.enable(); + } + + @Override + public String getVersion() { + return SparkForgeMod.class.getAnnotation(Mod.class).version(); + } + + @Override + public Path getPluginFolder() { + return this.mod.getConfigDirectory(); + } + + @SuppressWarnings("deprecation") + @Override + public void sendMessage(ICommandSender sender, String message) { + TextComponent component = ComponentSerializers.LEGACY.deserialize(message, '&'); + ITextComponent mcComponent = ITextComponent.Serializer.jsonToComponent(ComponentSerializers.JSON.serialize(component)); + sender.sendMessage(mcComponent); + } + + @Override + public void sendLink(ICommandSender sender, 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); + + sender.sendMessage(msg); + } + + @Override + public void runAsync(Runnable r) { + this.scheduler.execute(r); + } + + @Override + public 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) { + 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; + } + + this.platform.executeCommand(sender, args); + } + + @Override + public List getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos blockPos) { + if (!checkPermission(server, sender)) { + return Collections.emptyList(); + } + return this.platform.tabCompleteCommand(sender, args); + } + + @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/ForgeTickCounter.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java index 17f595b..de7d846 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 @@ -20,8 +20,7 @@ package me.lucko.spark.forge; -import me.lucko.spark.sampler.TickCounter; - +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; @@ -32,7 +31,7 @@ import java.util.Set; public class ForgeTickCounter implements TickCounter { private final TickEvent.Type type; - private final Set tasks = new HashSet<>(); + private final Set tasks = new HashSet<>(); private int tick = 0; public ForgeTickCounter(TickEvent.Type type) { @@ -49,8 +48,8 @@ public class ForgeTickCounter implements TickCounter { return; } - for (Runnable r : this.tasks){ - r.run(); + for (TickTask r : this.tasks){ + r.onTick(this); } this.tick++; } @@ -71,12 +70,12 @@ public class ForgeTickCounter implements TickCounter { } @Override - public void addTickTask(Runnable runnable) { + public void addTickTask(TickTask runnable) { this.tasks.add(runnable); } @Override - public void removeTickTask(Runnable runnable) { + public void removeTickTask(TickTask 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 index aa4f379..32dfd77 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/SparkForgeMod.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/SparkForgeMod.java @@ -48,13 +48,13 @@ public class SparkForgeMod { @EventHandler public void init(FMLInitializationEvent e) { if (FMLCommonHandler.instance().getSide() == Side.CLIENT) { - ForgeClientSparkPlatform.register(this); + ForgeClientSparkPlugin.register(this); } } @EventHandler public void serverInit(FMLServerStartingEvent e) { - e.registerServerCommand(new ForgeServerSparkPlatform(this)); + e.registerServerCommand(new ForgeServerSparkPlugin(this)); } public Path getConfigDirectory() { -- cgit