aboutsummaryrefslogtreecommitdiff
path: root/spark-forge/src/main/java/me/lucko/spark
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2019-04-16 21:37:59 +0100
committerLuck <git@lucko.me>2019-04-16 21:37:59 +0100
commitecd4cec8545460a4fc4ca65b911c2503a00cd8e7 (patch)
tree62067383a1044abc3a09724e89c6e7c619e87ec0 /spark-forge/src/main/java/me/lucko/spark
parent8a61b404848ed8e3c27f06eb73239d37d4273240 (diff)
downloadspark-ecd4cec8545460a4fc4ca65b911c2503a00cd8e7.tar.gz
spark-ecd4cec8545460a4fc4ca65b911c2503a00cd8e7.tar.bz2
spark-ecd4cec8545460a4fc4ca65b911c2503a00cd8e7.zip
Lots of refactoring, add tps command
Diffstat (limited to 'spark-forge/src/main/java/me/lucko/spark')
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlugin.java (renamed from spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlatform.java)18
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlugin.java (renamed from spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlatform.java)28
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlugin.java (renamed from spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlatform.java)52
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java13
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/SparkForgeMod.java4
5 files changed, 49 insertions, 66 deletions
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlatform.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlugin.java
index c383636..2c6c2fb 100644
--- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlatform.java
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeClientSparkPlugin.java
@@ -20,35 +20,35 @@
package me.lucko.spark.forge;
-import me.lucko.spark.sampler.TickCounter;
-
+import me.lucko.spark.common.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.HashSet;
import java.util.List;
+import java.util.Set;
-public class ForgeClientSparkPlatform extends ForgeSparkPlatform {
+public class ForgeClientSparkPlugin extends ForgeSparkPlugin {
public static void register(SparkForgeMod mod) {
- ClientCommandHandler.instance.registerCommand(new ForgeClientSparkPlatform(mod));
+ ClientCommandHandler.instance.registerCommand(new ForgeClientSparkPlugin(mod));
}
- public ForgeClientSparkPlatform(SparkForgeMod mod) {
+ public ForgeClientSparkPlugin(SparkForgeMod mod) {
super(mod);
}
@Override
- protected void broadcast(ITextComponent msg) {
- Minecraft.getMinecraft().player.sendMessage(msg);
+ public Set<ICommandSender> getSenders() {
+ return new HashSet<>(Collections.singleton(Minecraft.getMinecraft().player));
}
@Override
- public TickCounter newTickCounter() {
+ public TickCounter createTickCounter() {
return new ForgeTickCounter(TickEvent.Type.CLIENT);
}
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlatform.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlugin.java
index a5c6c01..98740c0 100644
--- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlatform.java
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeServerSparkPlugin.java
@@ -20,38 +20,32 @@
package me.lucko.spark.forge;
-import me.lucko.spark.sampler.TickCounter;
-
+import me.lucko.spark.common.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.HashSet;
import java.util.List;
+import java.util.Set;
-public class ForgeServerSparkPlatform extends ForgeSparkPlatform {
-
- public ForgeServerSparkPlatform(SparkForgeMod mod) {
+public class ForgeServerSparkPlugin extends ForgeSparkPlugin {
+ public ForgeServerSparkPlugin(SparkForgeMod mod) {
super(mod);
}
@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")) {
- player.sendMessage(msg);
- }
- }
+ public Set<ICommandSender> getSenders() {
+ MinecraftServer mcServer = FMLCommonHandler.instance().getMinecraftServerInstance();
+ Set<ICommandSender> senders = new HashSet<>(mcServer.getPlayerList().getPlayers());
+ senders.add(mcServer);
+ return senders;
}
@Override
- public TickCounter newTickCounter() {
+ public TickCounter createTickCounter() {
return new ForgeTickCounter(TickEvent.Type.SERVER);
}
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlatform.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlugin.java
index 1f4c173..609894d 100644
--- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlatform.java
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeSparkPlugin.java
@@ -21,10 +21,9 @@
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 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;
@@ -38,25 +37,27 @@ 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.ExecutorService;
import java.util.concurrent.Executors;
-
-import javax.annotation.Nullable;
+import java.util.concurrent.ScheduledExecutorService;
@SuppressWarnings("NullableProblems")
-public abstract class ForgeSparkPlatform extends SparkPlatform<ICommandSender> implements ICommand {
+public abstract class ForgeSparkPlugin implements SparkPlugin<ICommandSender>, ICommand {
private final SparkForgeMod mod;
+ private final ScheduledExecutorService scheduler;
+ private final SparkPlatform<ICommandSender> platform;
- private final ExecutorService worker = Executors.newSingleThreadExecutor(
- new ThreadFactoryBuilder().setNameFormat("spark-forge-async-worker").build()
- );
-
- protected ForgeSparkPlatform(SparkForgeMod mod) {
+ 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
@@ -70,38 +71,27 @@ public abstract class ForgeSparkPlatform extends SparkPlatform<ICommandSender> i
}
@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);
+ TextComponent component = ComponentSerializers.LEGACY.deserialize(message, '&');
+ ITextComponent mcComponent = ITextComponent.Serializer.jsonToComponent(ComponentSerializers.JSON.serialize(component));
+ sender.sendMessage(mcComponent);
}
@Override
- public void sendLink(String url) {
+ 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);
- broadcast(msg);
+ sender.sendMessage(msg);
}
@Override
public void runAsync(Runnable r) {
- this.worker.execute(r);
+ this.scheduler.execute(r);
}
@Override
@@ -128,7 +118,7 @@ public abstract class ForgeSparkPlatform extends SparkPlatform<ICommandSender> i
return;
}
- executeCommand(sender, args);
+ this.platform.executeCommand(sender, args);
}
@Override
@@ -136,7 +126,7 @@ public abstract class ForgeSparkPlatform extends SparkPlatform<ICommandSender> i
if (!checkPermission(server, sender)) {
return Collections.emptyList();
}
- return tabCompleteCommand(sender, args);
+ return this.platform.tabCompleteCommand(sender, args);
}
@Override
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<Runnable> tasks = new HashSet<>();
+ private final Set<TickTask> 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() {