diff --git a/Plan/fabric/build.gradle b/Plan/fabric/build.gradle index 6edc4e6a5..85311fcad 100644 --- a/Plan/fabric/build.gradle +++ b/Plan/fabric/build.gradle @@ -1,3 +1,5 @@ +import org.apache.tools.ant.filters.ReplaceTokens + plugins { id 'fabric-loom' version '0.9.+' } @@ -36,6 +38,20 @@ compileJava { options.release = 16 } +tasks.register('updateVersion', Copy) { + from('src/main/resources') { + include 'fabric.mod.json' + } + into 'build/sources/resources/' + filter(ReplaceTokens, tokens: [version: '' + project.ext.fullVersion]) +} + +processResources { + dependsOn updateVersion + duplicatesStrategy = DuplicatesStrategy.INCLUDE + from 'build/sources/resources' +} + slimJar { relocate 'com.mysql', 'plan.com.mysql' relocate 'com.google.protobuf', 'plan.com.mysql.cj.x.google.protobuf' diff --git a/Plan/fabric/src/main/java/net/playeranalytics/plan/PlanFabric.java b/Plan/fabric/src/main/java/net/playeranalytics/plan/PlanFabric.java index 9097c2eb4..b889535d3 100644 --- a/Plan/fabric/src/main/java/net/playeranalytics/plan/PlanFabric.java +++ b/Plan/fabric/src/main/java/net/playeranalytics/plan/PlanFabric.java @@ -93,26 +93,6 @@ public class PlanFabric implements PlanPlugin, DedicatedServerModInitializer { @Override public void onEnable() { - abstractionLayer = new FabricPlatformLayer(this); - pluginLogger = (FabricPluginLogger) abstractionLayer.getPluginLogger(); - runnableFactory = abstractionLayer.getRunnableFactory(); - - pluginLogger.info("Loading dependencies, this might take a while..."); - try { - ApplicationBuilder.appending("Plan") - .logger((message, args) -> pluginLogger.info(message, args)) - // Use paper repository for downloading slimjar dependencies - .internalRepositories(Collections.singletonList(new Repository(new URL("https://papermc.io/repo/repository/maven-public/")))) - .downloadDirectoryPath(Paths.get(getDataFolder().getAbsolutePath()).resolve("libraries")) - .build(); - } catch (IOException | ReflectiveOperationException | URISyntaxException | NoSuchAlgorithmException e) { - String version = abstractionLayer.getPluginInformation().getVersion(); - pluginLogger.error(this.getClass().getSimpleName() + "-v" + version, e); - pluginLogger.error("Plan failed to load its dependencies correctly!"); - pluginLogger.error("This error should be reported at https://github.com/plan-player-analytics/Plan/issues"); - onDisable(); - } - PlanFabricComponent component = DaggerPlanFabricComponent.builder() .plan(this) .abstractionLayer(abstractionLayer) @@ -177,11 +157,31 @@ public class PlanFabric implements PlanPlugin, DedicatedServerModInitializer { @Override public File getDataFolder() { - return FabricLoader.getInstance().getGameDir().resolve("mods").resolve("Plan").toFile(); + return FabricLoader.getInstance().getConfigDir().resolve("plan").toFile(); } @Override public void onInitializeServer() { + abstractionLayer = new FabricPlatformLayer(this); + pluginLogger = (FabricPluginLogger) abstractionLayer.getPluginLogger(); + runnableFactory = abstractionLayer.getRunnableFactory(); + + pluginLogger.info("Loading dependencies, this might take a while..."); + try { + ApplicationBuilder.appending("Plan") + .logger((message, args) -> pluginLogger.info(message, args)) + // Use paper repository for downloading slimjar dependencies + .internalRepositories(Collections.singletonList(new Repository(new URL("https://papermc.io/repo/repository/maven-public/")))) + .downloadDirectoryPath(Paths.get(getDataFolder().getAbsolutePath()).resolve("libraries")) + .build(); + } catch (IOException | ReflectiveOperationException | URISyntaxException | NoSuchAlgorithmException e) { + String version = abstractionLayer.getPluginInformation().getVersion(); + pluginLogger.error(this.getClass().getSimpleName() + "-v" + version, e); + pluginLogger.error("Plan failed to load its dependencies correctly!"); + pluginLogger.error("This error should be reported at https://github.com/plan-player-analytics/Plan/issues"); + onDisable(); + } + ServerLifecycleEvents.SERVER_STARTING.register(server -> { this.server = (MinecraftDedicatedServer) server; onEnable(); diff --git a/Plan/fabric/src/main/java/net/playeranalytics/plan/gathering/FabricSensor.java b/Plan/fabric/src/main/java/net/playeranalytics/plan/gathering/FabricSensor.java index f4e2cbc2b..dba5b9020 100644 --- a/Plan/fabric/src/main/java/net/playeranalytics/plan/gathering/FabricSensor.java +++ b/Plan/fabric/src/main/java/net/playeranalytics/plan/gathering/FabricSensor.java @@ -40,7 +40,6 @@ public class FabricSensor implements ServerSensor { @Override public double getTPS() { //Returns the ticks per second of the last 100 ticks - int length = server.lastTickLengths.length; double totalTickLength = 0; int count = 0; for (long tickLength : server.lastTickLengths) { @@ -48,8 +47,11 @@ public class FabricSensor implements ServerSensor { totalTickLength += Math.max(tickLength, TimeUnit.MILLISECONDS.toNanos(50)); count++; } - double averageTickLength = totalTickLength / count; - return count != 0 ? TimeUnit.SECONDS.toNanos(1) / averageTickLength : -1; + if (count == 0) { + return -1; + } else { + return TimeUnit.SECONDS.toNanos(1) / (totalTickLength / count); + } } @Override diff --git a/Plan/fabric/src/main/java/net/playeranalytics/plan/identification/properties/FabricServerProperties.java b/Plan/fabric/src/main/java/net/playeranalytics/plan/identification/properties/FabricServerProperties.java index 378dc5f96..4afcf98b1 100644 --- a/Plan/fabric/src/main/java/net/playeranalytics/plan/identification/properties/FabricServerProperties.java +++ b/Plan/fabric/src/main/java/net/playeranalytics/plan/identification/properties/FabricServerProperties.java @@ -32,9 +32,9 @@ public class FabricServerProperties extends ServerProperties { "Fabric", server.getServerPort(), server.getVersion(), - FabricLoader.getInstance().getModContainer("fabric").get().getMetadata().getVersion().getFriendlyString() + + FabricLoader.getInstance().getModContainer("fabric").orElseThrow().getMetadata().getVersion().getFriendlyString() + " (API), " + - FabricLoader.getInstance().getModContainer("fabricloader").get().getMetadata().getVersion().getFriendlyString() + + FabricLoader.getInstance().getModContainer("fabricloader").orElseThrow().getMetadata().getVersion().getFriendlyString() + " (loader)", () -> (server.getServerIp() == null) ? "" : server.getServerIp(), server.getProperties().maxPlayers diff --git a/Plan/fabric/src/main/java/net/playeranalytics/plugin/FabricPlatformLayer.java b/Plan/fabric/src/main/java/net/playeranalytics/plugin/FabricPlatformLayer.java index a82558445..898a7fd90 100644 --- a/Plan/fabric/src/main/java/net/playeranalytics/plugin/FabricPlatformLayer.java +++ b/Plan/fabric/src/main/java/net/playeranalytics/plugin/FabricPlatformLayer.java @@ -16,7 +16,7 @@ */ package net.playeranalytics.plugin; -import net.fabricmc.api.DedicatedServerModInitializer; +import net.playeranalytics.plan.PlanFabric; import net.playeranalytics.plugin.scheduling.FabricRunnableFactory; import net.playeranalytics.plugin.scheduling.RunnableFactory; import net.playeranalytics.plugin.server.FabricListeners; @@ -27,14 +27,14 @@ import org.apache.logging.log4j.LogManager; public class FabricPlatformLayer implements PlatformAbstractionLayer { - private final DedicatedServerModInitializer plugin; + private final PlanFabric plugin; private PluginLogger pluginLogger; private Listeners listeners; private PluginInformation pluginInformation; private RunnableFactory runnableFactory; - public FabricPlatformLayer(DedicatedServerModInitializer plugin) { + public FabricPlatformLayer(PlanFabric plugin) { this.plugin = plugin; } diff --git a/Plan/fabric/src/main/java/net/playeranalytics/plugin/FabricPluginInformation.java b/Plan/fabric/src/main/java/net/playeranalytics/plugin/FabricPluginInformation.java index 3b3ff602b..3f5e09719 100644 --- a/Plan/fabric/src/main/java/net/playeranalytics/plugin/FabricPluginInformation.java +++ b/Plan/fabric/src/main/java/net/playeranalytics/plugin/FabricPluginInformation.java @@ -16,17 +16,17 @@ */ package net.playeranalytics.plugin; -import net.fabricmc.api.DedicatedServerModInitializer; import net.fabricmc.loader.api.FabricLoader; +import net.playeranalytics.plan.PlanFabric; import java.io.File; import java.io.InputStream; public class FabricPluginInformation implements PluginInformation { - private final DedicatedServerModInitializer plugin; + private final PlanFabric plugin; - public FabricPluginInformation(DedicatedServerModInitializer plugin) { + public FabricPluginInformation(PlanFabric plugin) { this.plugin = plugin; } @@ -37,11 +37,16 @@ public class FabricPluginInformation implements PluginInformation { @Override public File getDataFolder() { - return FabricLoader.getInstance().getGameDir().resolve("config").resolve("Plan").toFile(); + return plugin.getDataFolder(); } @Override public String getVersion() { - return FabricLoader.getInstance().getModContainer("plan").get().getMetadata().getVersion().getFriendlyString(); + return FabricLoader.getInstance() + .getModContainer("plan") + .orElseThrow() + .getMetadata() + .getVersion() + .getFriendlyString(); } } diff --git a/Plan/fabric/src/main/java/net/playeranalytics/plugin/server/FabricPluginLogger.java b/Plan/fabric/src/main/java/net/playeranalytics/plugin/server/FabricPluginLogger.java index 8fae3f424..972a1bf67 100644 --- a/Plan/fabric/src/main/java/net/playeranalytics/plugin/server/FabricPluginLogger.java +++ b/Plan/fabric/src/main/java/net/playeranalytics/plugin/server/FabricPluginLogger.java @@ -28,37 +28,37 @@ public class FabricPluginLogger implements PluginLogger { @Override public PluginLogger info(String message) { - logger.info("[Plan] " + message); + logger.info("[Plan] " + message.replaceAll("§[0-9a-fk-or]", "")); return this; } public void info(String message, Object... args) { - String replacedMsg = message.replaceAll("(?<=\\{).+?(?=\\})", ""); + String replacedMsg = message.replaceAll("(?<=\\{).+?(?=})", ""); String formattedMsg = "[Plan] " + replacedMsg; - logger.info(formattedMsg, args); + logger.info(formattedMsg.replaceAll("§[0-9a-fk-or]", ""), args); } @Override public PluginLogger warn(String message) { - logger.warn("[Plan] " + message); + logger.warn("[Plan] " + message.replaceAll("§[0-9a-fk-or]", "")); return this; } @Override public PluginLogger error(String message) { - logger.error("[Plan] " + message); + logger.error("[Plan] " + message.replaceAll("§[0-9a-fk-or]", "")); return this; } @Override public PluginLogger warn(String message, Throwable throwable) { - logger.warn("[Plan] " + message, throwable); + logger.warn("[Plan] " + message.replaceAll("§[0-9a-fk-or]", ""), throwable); return this; } @Override public PluginLogger error(String message, Throwable throwable) { - logger.error("[Plan] " + message, throwable); + logger.error("[Plan] " + message.replaceAll("§[0-9a-fk-or]", ""), throwable); return this; } }