/* * 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.fabric; 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 com.mojang.brigadier.tree.LiteralCommandNode; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.SparkPlugin; import me.lucko.spark.common.sampler.ThreadDumper; import java.nio.file.Path; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; public abstract class FabricSparkPlugin implements SparkPlugin { protected final ScheduledExecutorService scheduler; protected final SparkPlatform platform; protected final FabricSparkMod mod; protected FabricSparkPlugin(FabricSparkMod mod) { this.mod = mod; this.scheduler = Executors.newSingleThreadScheduledExecutor( new ThreadFactoryBuilder().setNameFormat("spark-fabric-async-worker").build() ); this.platform = new SparkPlatform(this); this.platform.enable(); } public static void registerCommands(CommandDispatcher dispatcher, Command executor, String... aliases) { if (aliases.length == 0) { return; } String mainName = aliases[0]; LiteralArgumentBuilder command = LiteralArgumentBuilder.literal(mainName) .executes(executor) .then(RequiredArgumentBuilder.argument("args", StringArgumentType.greedyString()) .executes(executor) ); LiteralCommandNode node = dispatcher.register(command); for (int i = 1; i < aliases.length; i++) { dispatcher.register(LiteralArgumentBuilder.literal(aliases[i]).redirect(node)); } } @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()}); } }