Fabric fixes (#2089)

* Fix SonarCloud bugs

* Remove color codes from console messages

* Replace version string in mod.json

* Advance dependency initialization to pre-enable hook

* Resolve config folder using correct method
This commit is contained in:
Antti Koponen 2021-09-18 22:38:12 +03:00 committed by GitHub
parent 37a6ad86e9
commit d6aa07d93f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 41 deletions

View File

@ -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'

View File

@ -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();

View File

@ -40,7 +40,6 @@ public class FabricSensor implements ServerSensor<ServerWorld> {
@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<ServerWorld> {
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

View File

@ -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

View File

@ -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;
}

View File

@ -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();
}
}

View File

@ -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;
}
}