Improve bukkit logging

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
This commit is contained in:
solonovamax 2021-08-30 20:35:18 -04:00
parent a776ecfc2b
commit 7b9c88f8a6
No known key found for this signature in database
GPG Key ID: ED0FC2D44CD76482
3 changed files with 322 additions and 82 deletions

View File

@ -1,6 +1,8 @@
package com.dfsek.terra.addons.terrascript.script.functions; package com.dfsek.terra.addons.terrascript.script.functions;
import net.jafama.FastMath; import net.jafama.FastMath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -22,6 +24,8 @@ import com.dfsek.terra.api.vector.Vector3;
public class StructureFunction implements Function<Boolean> { public class StructureFunction implements Function<Boolean> {
private static final Logger logger = LoggerFactory.getLogger(StructureFunction.class);
private final Registry<Structure> registry; private final Registry<Structure> registry;
private final Returnable<String> id; private final Returnable<String> id;
private final Returnable<Number> x, y, z; private final Returnable<Number> x, y, z;
@ -61,7 +65,7 @@ public class StructureFunction implements Function<Boolean> {
String app = id.apply(implementationArguments, variableMap); String app = id.apply(implementationArguments, variableMap);
Structure script = registry.get(app); Structure script = registry.get(app);
if(script == null) { if(script == null) {
main.logger().severe("No such structure " + app); logger.error("No such structure {}", app);
return null; return null;
} }
@ -70,7 +74,7 @@ public class StructureFunction implements Function<Boolean> {
try { try {
rotation1 = Rotation.valueOf(rotString); rotation1 = Rotation.valueOf(rotString);
} catch(IllegalArgumentException e) { } catch(IllegalArgumentException e) {
main.logger().severe("Invalid rotation " + rotString); logger.error("Invalid rotation {}", rotString);
return null; return null;
} }

View File

@ -1,6 +1,5 @@
package com.dfsek.terra.bukkit; package com.dfsek.terra.bukkit;
import io.papermc.lib.PaperLib;
import org.bstats.bukkit.Metrics; import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
@ -27,24 +26,14 @@ import com.dfsek.terra.bukkit.listeners.CommonListener;
import com.dfsek.terra.bukkit.listeners.PaperListener; import com.dfsek.terra.bukkit.listeners.PaperListener;
import com.dfsek.terra.bukkit.listeners.SpigotListener; import com.dfsek.terra.bukkit.listeners.SpigotListener;
import com.dfsek.terra.bukkit.util.PaperUtil; import com.dfsek.terra.bukkit.util.PaperUtil;
import com.dfsek.terra.bukkit.util.VersionUtil;
import com.dfsek.terra.commands.CommandUtil; import com.dfsek.terra.commands.CommandUtil;
import com.dfsek.terra.commands.TerraCommandManager; import com.dfsek.terra.commands.TerraCommandManager;
public class TerraBukkitPlugin extends JavaPlugin { public class TerraBukkitPlugin extends JavaPlugin {
public static final BukkitVersion BUKKIT_VERSION;
private static final Logger logger = LoggerFactory.getLogger(TerraBukkitPlugin.class); private static final Logger logger = LoggerFactory.getLogger(TerraBukkitPlugin.class);
static {
String ver = Bukkit.getServer().getClass().getPackage().getName();
if(ver.contains("1_17")) BUKKIT_VERSION = BukkitVersion.V1_17;
else if(ver.contains("1_16")) BUKKIT_VERSION = BukkitVersion.V1_16;
else if(ver.contains("1_15")) BUKKIT_VERSION = BukkitVersion.V1_15;
else if(ver.contains("1_14")) BUKKIT_VERSION = BukkitVersion.V1_14;
else if(ver.contains("1_13")) BUKKIT_VERSION = BukkitVersion.V1_13;
else BUKKIT_VERSION = BukkitVersion.UNKNOWN;
}
private final TerraPluginImpl terraPlugin = new TerraPluginImpl(this); private final TerraPluginImpl terraPlugin = new TerraPluginImpl(this);
private final Map<String, com.dfsek.terra.api.world.generator.ChunkGenerator> generatorMap = new HashMap<>(); private final Map<String, com.dfsek.terra.api.world.generator.ChunkGenerator> generatorMap = new HashMap<>();
private final Map<String, ConfigPack> worlds = new HashMap<>(); private final Map<String, ConfigPack> worlds = new HashMap<>();
@ -56,16 +45,11 @@ public class TerraBukkitPlugin extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
logger.info("Running on version {}", BUKKIT_VERSION);
if(BUKKIT_VERSION == BukkitVersion.UNKNOWN) {
logger.warn("Terra is running on an unknown Bukkit version. Proceed with caution.");
}
terraPlugin.getEventManager().callEvent(new PlatformInitializationEvent()); terraPlugin.getEventManager().callEvent(new PlatformInitializationEvent());
new Metrics(this, 9017); // Set up bStats. new Metrics(this, 9017); // Set up bStats.
PluginCommand c = Objects.requireNonNull(getCommand("terra")); PluginCommand cmd = Objects.requireNonNull(getCommand("terra"));
CommandManager manager = new TerraCommandManager(terraPlugin); CommandManager manager = new TerraCommandManager(terraPlugin);
@ -87,8 +71,8 @@ public class TerraBukkitPlugin extends JavaPlugin {
BukkitCommandAdapter command = new BukkitCommandAdapter(manager); BukkitCommandAdapter command = new BukkitCommandAdapter(manager);
c.setExecutor(command); cmd.setExecutor(command);
c.setTabCompleter(command); cmd.setTabCompleter(command);
long save = terraPlugin.getTerraConfig().getDataSaveInterval(); long save = terraPlugin.getTerraConfig().getDataSaveInterval();
@ -97,20 +81,55 @@ public class TerraBukkitPlugin extends JavaPlugin {
Bukkit.getPluginManager().registerEvents(new CommonListener(terraPlugin), this); // Register master event listener Bukkit.getPluginManager().registerEvents(new CommonListener(terraPlugin), this); // Register master event listener
PaperUtil.checkPaper(this); PaperUtil.checkPaper(this);
if(PaperLib.isPaper()) {
try { try {
Class.forName("io.papermc.paper.event.world.StructureLocateEvent"); // Check if user is on Paper version with event. Class.forName("io.papermc.paper.event.world.StructureLocateEvent"); // Check if user is on Paper version with event.
Bukkit.getPluginManager().registerEvents(new PaperListener(terraPlugin), this); // Register Paper events. Bukkit.getPluginManager().registerEvents(new PaperListener(terraPlugin), this); // Register Paper events.
} catch(ClassNotFoundException e) { } catch(ClassNotFoundException e) {
registerSpigotEvents(true); // Outdated Paper version. /*
} The command
fmt -w 72 -g 72 -u text | \
boxes -a cmd -p a1h3 -t 4e -d jstone -s82 | \
sed -Ee 's/\+-+\*\//|------------------------------------------------------------------------------|/g' \
-e 's/^\s*(.*)$/"\1\\n"/g' -e 's/\///g' -e 's/\*|\+/./g' -e 's/$/ +/g' -e '/^"\| {3}-{72} {3}\|\\n" \+$/d'
was used to create these boxes. Leaving this here for if we want to create more/modify them.
*/
if(VersionUtil.getSpigotVersionInfo().isPaper()) { // logging with stack trace to be annoying.
logger.warn("""
.------------------------------------------------------------------------------.
| |
| You are using an outdated version of Paper. This version does not |
| contain StructureLocateEvent. Terra will now fall back to Spigot |
| events. This will prevent cartographer villagers from spawning, |
| and cause structure location to not function. If you want these |
| functionalities, update to the latest build of Paper. If you use a |
| fork, update to the latest version, then if you still receive this |
| message, ask the fork developer to update upstream. |
| |
|------------------------------------------------------------------------------|
""", e);
} else { } else {
registerSpigotEvents(false); logger.warn("""
.------------------------------------------------------------------------------.
| |
| Paper is not in use. Falling back to Spigot events. This will prevent |
| cartographer villagers from spawning, and cause structure location to |
| not function. If you want these functionalities (and all the other |
| benefits that Paper offers), upgrade your server to Paper. Find out |
| more at https://papermc.io/ |
| |
|------------------------------------------------------------------------------|
""", e);
Bukkit.getPluginManager().registerEvents(new SpigotListener(terraPlugin), this); // Register Spigot event listener
}
} }
} }
@Override @Override
public @Nullable ChunkGenerator getDefaultWorldGenerator(@NotNull String worldName, @Nullable String id) { public @Nullable
ChunkGenerator getDefaultWorldGenerator(@NotNull String worldName, @Nullable String id) {
return new BukkitChunkGeneratorWrapper(generatorMap.computeIfAbsent(worldName, name -> { return new BukkitChunkGeneratorWrapper(generatorMap.computeIfAbsent(worldName, name -> {
if(!terraPlugin.getConfigRegistry().contains(id)) throw new IllegalArgumentException("No such config pack \"" + id + "\""); if(!terraPlugin.getConfigRegistry().contains(id)) throw new IllegalArgumentException("No such config pack \"" + id + "\"");
ConfigPack pack = terraPlugin.getConfigRegistry().get(id); ConfigPack pack = terraPlugin.getConfigRegistry().get(id);
@ -119,57 +138,77 @@ public class TerraBukkitPlugin extends JavaPlugin {
})); }));
} }
private void registerSpigotEvents(boolean outdated) { private boolean doVersionCheck() {
if(outdated) { logger.info("Running on version {} with {}.", VersionUtil.getMinecraftVersionInfo(), VersionUtil.getSpigotVersionInfo());
logger.error("You are using an outdated version of Paper.");
logger.error("This version does not contain StructureLocateEvent."); if(VersionUtil.getMinecraftVersionInfo().getMinor() < 17)
logger.error("Terra will now fall back to Spigot events."); logger.error("Terra does not work on version 1.16.5 at the moment.");
logger.error("This will prevent cartographer villagers from spawning,");
logger.error("and cause structure location to not function."); if(!VersionUtil.getSpigotVersionInfo().isSpigot())
logger.error("If you want these functionalities, update to the latest build of Paper."); logger.error("YOU ARE RUNNING A CRAFTBUKKIT OR BUKKIT SERVER JAR. PLEASE UPGRADE TO PAPER SPIGOT.");
logger.error("If you use a fork, update to the latest version, then if you still");
logger.error("receive this message, ask the fork developer to update upstream."); if(VersionUtil.getSpigotVersionInfo().isYatopia())
logger.warn("Yatopia is a highly unstable fork of spigot. You may experience various issues with it.");
if(VersionUtil.getSpigotVersionInfo().isMohist()) {
if(System.getProperty("IKnowMohistCausesLotsOfIssuesButIWillUseItAnyways") == null) {
Runnable runnable = () -> { // scary big block of text
logger.error("""
.----------------------------------------------------------------------------------.
| |
| !! Warning !! |
| |
| You are currently using Mohist. |
| |
| Do not use Mohist. |
| |
| The concept of combining the rigid Bukkit platform, which assumes a 100% |
| Vanilla server, with the flexible Forge platform, which allows changing |
| core components of the game, simply does not work. These platforms are |
| incompatible at a conceptual level, the only way to combine them would |
| be to make incompatible changes to both. As a result, Mohist's Bukkit |
| API implementation is not compliant. This will cause many plugins to |
| break. Rather than fix their platform, Mohist has chosen to distribute |
| unofficial builds of plugins they deem to be "fixed". These builds are not |
| "fixed", they are simply hacked together to work with Mohist's half-baked |
| Bukkit implementation. To distribute these as "fixed" versions implies that: |
| - These builds are endorsed by the original developers. (They are not) |
| - The issue is on the plugin's end, not Mohist's. (It is not. The issue |
| is that Mohist chooses to not create a compliant Bukkit implementation) |
| Please, do not use Mohist. It causes issues with most plugins, and rather |
| than fixing their platform, Mohist has chosen to distribute unofficial |
| hacked-together builds of plugins, calling them "fixed". If you want |
| to use a server API with Forge mods, look at the Sponge project, an |
| API that is designed to be implementation-agnostic, with first-party |
| support for the Forge mod loader. You are bound to encounter issues if |
| you use Terra with Mohist. We will provide NO SUPPORT for servers running |
| Mohist. If you wish to proceed anyways, you can add the JVM System Property |
| "IKnowMohistCausesLotsOfIssuesButIWillUseItAnyways" to enable the plugin. No |
| support will be provided for servers running Mohist. |
| |
| Because of this **TERRA HAS BEEN DISABLED**. |
| Do not come ask us why it is not working. |
| |
|----------------------------------------------------------------------------------|
""");
};
runnable.run();
//noinspection deprecation
Bukkit.getScheduler().scheduleAsyncDelayedTask(this, runnable, 200L);
// Bukkit.shutdown(); // we're not *that* evil
setEnabled(false);
return false;
} else { } else {
logger.error("Paper is not in use. Falling back to Spigot events."); logger.warn("""
logger.error("This will prevent cartographer villagers from spawning,"); You are using Mohist, so we will not give you any support for issues that may arise.
logger.error("and cause structure location to not function."); Since you enabled the "IKnowMohistCausesLotsOfIssuesButIWillUseItAnyways" flag, we won't disable Terra. But be warned.
logger.error("If you want these functionalities (and all the other");
logger.error("benefits that Paper offers), upgrade your server to Paper.");
logger.error("Find out more at https://papermc.io/");
}
Bukkit.getPluginManager().registerEvents(new SpigotListener(terraPlugin), this); // Register Spigot event listener > I felt a great disturbance in the JVM,as if millions of plugins suddenly cried out in stack traces and were suddenly silenced.
} > I fear something terrible has happened.
> - Astrash
public enum BukkitVersion { """);
V1_13(13),
V1_14(14),
V1_15(15),
V1_16(16),
V1_17(17),
UNKNOWN(Integer.MAX_VALUE); // Assume unknown version is latest.
private final int index;
BukkitVersion(int index) {
this.index = index;
}
/**
* Gets if this version is above or equal to another.
*
* @param other Other version
*
* @return Whether this version is equal to or later than other.
*/
public boolean above(BukkitVersion other) {
return this.index >= other.index;
} }
} }
return true;
}
} }

View File

@ -0,0 +1,197 @@
package com.dfsek.terra.bukkit.util;
import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class VersionUtil {
public static final SpigotVersionInfo SPIGOT_VERSION_INFO;
public static final MinecraftVersionInfo MINECRAFT_VERSION_INFO;
private static final Logger logger = LoggerFactory.getLogger(VersionUtil.class);
static {
SPIGOT_VERSION_INFO = new SpigotVersionInfo();
MinecraftVersionInfo mcVersionInfo;
try {
mcVersionInfo = new MinecraftVersionInfo();
} catch(Throwable t) {
logger.error("Error while parsing minecraft version info. Continuing launch, but setting all versions to -1.");
mcVersionInfo = new MinecraftVersionInfo(-1, -1, -1);
}
MINECRAFT_VERSION_INFO = mcVersionInfo;
}
public static MinecraftVersionInfo getMinecraftVersionInfo() {
return MINECRAFT_VERSION_INFO;
}
public static SpigotVersionInfo getSpigotVersionInfo() {
return SPIGOT_VERSION_INFO;
}
public static final class SpigotVersionInfo {
private final boolean spigot;
private final boolean paper;
private final boolean mohist;
private final boolean airplane;
private final boolean tuinity;
private final boolean purpur;
private final boolean yatopia;
public SpigotVersionInfo() {
logger.debug("Parsing spigot version info...");
paper = PaperLib.isPaper();
spigot = PaperLib.isSpigot();
boolean isTuinity = false;
try {
Class.forName("com.tuinity.tuinity.config.TuinityConfig");
isTuinity = true;
} catch(ClassNotFoundException ignored) { }
this.tuinity = isTuinity;
boolean isAirplane = false;
try {
Class.forName("gg.airplane.AirplaneConfig");
isAirplane = true;
} catch(ClassNotFoundException ignored) { }
this.airplane = isAirplane;
boolean isPurpur = false;
try {
Class.forName("net.pl3x.purpur.PurpurConfig");
isPurpur = true;
} catch(ClassNotFoundException ignored) { }
this.purpur = isPurpur;
boolean isYatopia = false;
try {
Class.forName("org.yatopiamc.yatopia.server.YatopiaConfig");
isYatopia = true;
} catch(ClassNotFoundException ignored) { }
this.yatopia = isYatopia;
boolean isMohist = false;
try {
Class.forName("com.mohistmc.MohistMC");
// it's mohist
isMohist = true;
} catch(ClassNotFoundException ignore) { }
this.mohist = isMohist;
logger.debug("Spigot version info parsed successfully.");
}
@Override
public String toString() {
if(mohist)
return "Mohist...";
else if(yatopia)
return "Yaptopia";
else if(purpur)
return "Purpur";
else if(tuinity)
return "Tuinity";
else if(airplane)
return "Airplane";
else if(paper)
return "Paper";
else if(spigot)
return "Spigot";
else
return "Craftbukkit";
}
public boolean isAirplane() {
return airplane;
}
public boolean isPaper() {
return paper;
}
public boolean isMohist() {
return mohist;
}
public boolean isPurpur() {
return purpur;
}
public boolean isSpigot() {
return spigot;
}
public boolean isTuinity() {
return tuinity;
}
public boolean isYatopia() {
return yatopia;
}
}
public static final class MinecraftVersionInfo {
private static final Logger logger = LoggerFactory.getLogger(MinecraftVersionInfo.class);
private static final Pattern VERSION_PATTERN = Pattern.compile("v?(\\d+)_(\\d+)_R(\\d+)");
private final int major;
private final int minor;
private final int patch;
private MinecraftVersionInfo() {
this(Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]);
}
private MinecraftVersionInfo(int major, int minor, int patch) {
this.major = major;
this.minor = minor;
this.patch = patch;
}
private MinecraftVersionInfo(String versionString) {
Matcher versionMatcher = VERSION_PATTERN.matcher(versionString);
if(versionMatcher.find()) {
major = Integer.parseInt(versionMatcher.group(1));
minor = Integer.parseInt(versionMatcher.group(2));
patch = Integer.parseInt(versionMatcher.group(3));
} else {
logger.warn("Error while parsing minecraft version info. Continuing launch, but setting all versions to -1.");
major = -1;
minor = -1;
patch = -1;
}
}
@Override
public String toString() {
if(major == -1 && minor == -1 && patch == -1)
return "Unknown";
return String.format("v%d.%d.%d", major, minor, patch);
}
public int getMajor() {
return major;
}
public int getMinor() {
return minor;
}
public int getPatch() {
return patch;
}
}
}