tweaks724/src/main/java/eu/m724/tweaks/TweaksPlugin.java
2025-01-20 11:16:20 +01:00

177 lines
5.6 KiB
Java

/*
* Copyright (C) 2025 Minecon724
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text.
*/
package eu.m724.tweaks;
import eu.m724.mstats.MStatsPlugin;
import eu.m724.tweaks.alert.AlertModule;
import eu.m724.tweaks.auth.AuthModule;
import eu.m724.tweaks.chat.ChatModule;
import eu.m724.tweaks.door.DoorKnockModule;
import eu.m724.tweaks.door.DoorOpenModule;
import eu.m724.tweaks.full.FullModule;
import eu.m724.tweaks.hardcore.HardcoreModule;
import eu.m724.tweaks.killswitch.KillswitchModule;
import eu.m724.tweaks.knockback.KnockbackModule;
import eu.m724.tweaks.motd.MotdModule;
import eu.m724.tweaks.ping.F3NameListener;
import eu.m724.tweaks.ping.PingChecker;
import eu.m724.tweaks.pomodoro.PomodoroModule;
import eu.m724.tweaks.redstone.RedstoneModule;
import eu.m724.tweaks.sleep.SleepModule;
import eu.m724.tweaks.swing.SwingModule;
import eu.m724.tweaks.updater.UpdaterModule;
import eu.m724.tweaks.worldborder.WorldBorderExpandModule;
import eu.m724.tweaks.worldborder.WorldBorderHideModule;
import java.util.Locale;
import java.util.logging.Level;
public class TweaksPlugin extends MStatsPlugin {
private static TweaksPlugin INSTANCE;
@Override
public void onEnable() {
long start = System.nanoTime();
INSTANCE = this;
if (getServer().getPluginManager().getPlugin("ProtocolLib") == null) {
getLogger().severe("ProtocolLib is required for this plugin.");
getLogger().severe("https://www.spigotmc.org/resources/protocollib.1997/");
getServer().getPluginManager().disablePlugin(this);
return;
}
TweaksConfig config = TweaksConfig.load(this);
getLogger().setLevel(config.debug() ? Level.FINEST : Level.INFO);
DebugLogger.logger = getLogger();
if (config.debug()) {
DebugLogger.warning("Debug harms performance");
}
DebugLogger.fine("Language");
new Language(Locale.of(config.locale())); // TODO
DebugLogger.fine(Language.getString("languageNotice", Language.getString("language"), Language.getString("languageEnglish")));
var runningVersion = getServer().getBukkitVersion();
var targetVersion = getTargetVersion();
if (!runningVersion.equals(targetVersion)) {
// the incompatibility can be between 1.21.4-R0.1-SNAPSHOT and 1.21.4-R1-SNAPSHOT
var runningMc = runningVersion.split("-")[0];
var targetMc = targetVersion.split("-")[0];
if (!runningMc.equals(targetMc)) {
targetVersion = targetMc;
runningVersion = runningMc;
}
getLogger().warning("This plugin was built for %s. This server is running %s.".formatted(targetVersion, runningVersion));
getLogger().warning("Some modules will not work. Disable those modules, or make a compatible build yourself:");
getLogger().warning("https://git.m724.eu/Minecon724/tweaks724/src/branch/master/docs/BUILDING.md");
}
/* start modules */
if (config.worldborderHide()) {
TweaksModule.init(WorldBorderHideModule.class);
}
if (config.worldborderExpand()) {
TweaksModule.init(WorldBorderExpandModule.class);
}
if (config.chatEnabled()) {
TweaksModule.init(ChatModule.class);
}
if (config.doorKnocking()) {
TweaksModule.init(DoorKnockModule.class);
}
if (config.doorDoubleOpen()) {
TweaksModule.init(DoorOpenModule.class);
}
if (config.brandEnabled()) {
DebugLogger.fine("Enabling Brand");
new F3NameListener(this).init();
}
DebugLogger.fine("Enabling Ping");
new PingChecker(this).init(getCommand("ping"));
if (config.motdEnabled()) {
TweaksModule.init(MotdModule.class);
}
if (config.pomodoroEnabled()) {
TweaksModule.init(PomodoroModule.class);
}
if (config.updaterEnabled()) {
TweaksModule.init(UpdaterModule.class);
}
if (config.hardcoreEnabled()) {
TweaksModule.init(HardcoreModule.class);
}
if (config.sleepEnabled()) {
TweaksModule.init(SleepModule.class);
}
if (config.authEnabled()) {
TweaksModule.init(AuthModule.class);
}
TweaksModule.init(AlertModule.class);
TweaksModule.init(FullModule.class);
if (config.redstoneEnabled()) {
TweaksModule.init(RedstoneModule.class);
}
TweaksModule.init(KnockbackModule.class);
if (config.killswitchEnabled()) {
TweaksModule.init(KillswitchModule.class);
}
if (config.swingEnabled()) {
TweaksModule.init(SwingModule.class);
}
/* end modules */
if (config.metrics()) {
DebugLogger.fine("Enabling Metrics");
mStats(1);
}
DebugLogger.fine("Took %.3f milliseconds".formatted((System.nanoTime() - start) / 1000000.0));
}
private String getTargetVersion() {
var permission = getServer().getPluginManager().getPermission("7weaks724.ignore.this");
var desc = permission.getDescription().substring("Internal, not for use. ".length()).split(",");
var version = desc[0];
return version;
}
public boolean hasResource(String resource) {
return this.getClassLoader().getResource(resource) != null;
}
public static TweaksPlugin getInstance() {
return INSTANCE;
}
}