Skip to content

Commit

Permalink
last round of optimizations before release
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Apr 28, 2024
1 parent 2ab9dbe commit 034a270
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 103 deletions.
83 changes: 46 additions & 37 deletions src/main/java/me/xginko/villageroptimizer/VillagerOptimizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import me.xginko.villageroptimizer.config.Config;
import me.xginko.villageroptimizer.config.LanguageCache;
import me.xginko.villageroptimizer.modules.VillagerOptimizerModule;
import me.xginko.villageroptimizer.utils.GenericUtil;
import me.xginko.villageroptimizer.utils.Util;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
Expand All @@ -18,6 +18,7 @@
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.util.*;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -45,55 +46,56 @@ public void onEnable() {
audiences = BukkitAudiences.create(this);
logger = ComponentLogger.logger(getLogger().getName());
bStats = new Metrics(this, 19954);

try {
getDataFolder().mkdirs();
} catch (Exception e) {
logger.error("Failed to create plugin directory! Cannot enable!", e);
getServer().getPluginManager().disablePlugin(this);
}

logger.info(Component.text("╭────────────────────────────────────────────────────────────╮").style(GenericUtil.STYLE));
logger.info(Component.text("│ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ _ __ _ __ __ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ | | / /(_)/ // /___ _ ___ _ ___ ____ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ | |/ // // // // _ `// _ `// -_)/ __/ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ |___//_//_//_/ \\_,_/ \\_, / \\__//_/ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ ____ __ _ /___/_ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ / __ \\ ___ / /_ (_)__ _ (_)___ ___ ____ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ / /_/ // _ \\/ __// // ' \\ / //_ // -_)/ __/ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ \\____// .__/\\__//_//_/_/_//_/ /__/\\__//_/ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ /_/ by xGinko │").style(GenericUtil.STYLE));
logger.info(Component.text("│ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ │").style(GenericUtil.STYLE));
logger.info(Component.text("╭────────────────────────────────────────────────────────────╮").style(Util.PL_STYLE));
logger.info(Component.text("│ │").style(Util.PL_STYLE));
logger.info(Component.text("│ │").style(Util.PL_STYLE));
logger.info(Component.text("│ _ __ _ __ __ │").style(Util.PL_STYLE));
logger.info(Component.text("│ | | / /(_)/ // /___ _ ___ _ ___ ____ │").style(Util.PL_STYLE));
logger.info(Component.text("│ | |/ // // // // _ `// _ `// -_)/ __/ │").style(Util.PL_STYLE));
logger.info(Component.text("│ |___//_//_//_/ \\_,_/ \\_, / \\__//_/ │").style(Util.PL_STYLE));
logger.info(Component.text("│ ____ __ _ /___/_ │").style(Util.PL_STYLE));
logger.info(Component.text("│ / __ \\ ___ / /_ (_)__ _ (_)___ ___ ____ │").style(Util.PL_STYLE));
logger.info(Component.text("│ / /_/ // _ \\/ __// // ' \\ / //_ // -_)/ __/ │").style(Util.PL_STYLE));
logger.info(Component.text("│ \\____// .__/\\__//_//_/_/_//_/ /__/\\__//_/ │").style(Util.PL_STYLE));
logger.info(Component.text("│ /_/ by xGinko │").style(Util.PL_STYLE));
logger.info(Component.text("│ │").style(Util.PL_STYLE));
logger.info(Component.text("│ │").style(Util.PL_STYLE));
logger.info(Component.text("│ ")
.style(GenericUtil.STYLE).append(Component.text("https://github.com/xGinko/VillagerOptimizer")
.color(NamedTextColor.GRAY)).append(Component.text(" │").style(GenericUtil.STYLE)));
logger.info(Component.text("│ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ │").style(GenericUtil.STYLE));
.style(Util.PL_STYLE).append(Component.text("https://github.com/xGinko/VillagerOptimizer")
.color(NamedTextColor.GRAY)).append(Component.text(" │").style(Util.PL_STYLE)));
logger.info(Component.text("│ │").style(Util.PL_STYLE));
logger.info(Component.text("│ │").style(Util.PL_STYLE));

logger.info(Component.text("│ ")
.style(GenericUtil.STYLE).append(Component.text(" ➤ Loading Translations...").style(GenericUtil.STYLE))
.append(Component.text(" │").style(GenericUtil.STYLE)));
reloadLang(true);
.style(Util.PL_STYLE).append(Component.text(" ➤ Loading Config...").style(Util.PL_STYLE))
.append(Component.text(" │").style(Util.PL_STYLE)));
reloadConfiguration();

logger.info(Component.text("│ ")
.style(GenericUtil.STYLE).append(Component.text(" ➤ Loading Config...").style(GenericUtil.STYLE))
.append(Component.text(" │").style(GenericUtil.STYLE)));
reloadConfiguration();
.style(Util.PL_STYLE).append(Component.text(" ➤ Loading Translations...").style(Util.PL_STYLE))
.append(Component.text(" │").style(Util.PL_STYLE)));
reloadLang(true);

logger.info(Component.text("│ ")
.style(GenericUtil.STYLE).append(Component.text(" ✓ Done.").color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD))
.append(Component.text(" │").style(GenericUtil.STYLE)));
logger.info(Component.text("│ │").style(GenericUtil.STYLE));
logger.info(Component.text("│ │").style(GenericUtil.STYLE));
logger.info(Component.text("╰────────────────────────────────────────────────────────────╯").style(GenericUtil.STYLE));
.style(Util.PL_STYLE).append(Component.text(" ✓ Done.").color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD))
.append(Component.text(" │").style(Util.PL_STYLE)));
logger.info(Component.text("│ │").style(Util.PL_STYLE));
logger.info(Component.text("│ │").style(Util.PL_STYLE));
logger.info(Component.text("╰────────────────────────────────────────────────────────────╯").style(Util.PL_STYLE));
}

@Override
public void onDisable() {
VillagerOptimizerModule.modules.forEach(VillagerOptimizerModule::disable);
VillagerOptimizerModule.modules.clear();
VillagerOptimizerModule.MODULES.forEach(VillagerOptimizerModule::disable);
VillagerOptimizerModule.MODULES.clear();
if (foliaLib != null) {
foliaLib.getImpl().cancelAllTasks();
foliaLib = null;
Expand Down Expand Up @@ -166,17 +168,24 @@ private void reloadConfiguration() {
private void reloadLang(boolean logFancy) {
languageCacheMap = new HashMap<>();
try {
for (String localeString : getAvailableTranslations()) {
if (logFancy) logger.info(Component.text("│ ").style(GenericUtil.STYLE)
final SortedSet<String> availableLocales = getAvailableTranslations();
if (!config.auto_lang) {
final String defaultLang = config.default_lang.toString().replace("-", "_").toLowerCase();
if (!availableLocales.contains(defaultLang))
throw new FileNotFoundException("Could not find any translation file for language '" + config.default_lang + "'");
availableLocales.removeIf(localeString -> !localeString.equalsIgnoreCase(defaultLang));
}
for (String localeString : availableLocales) {
if (logFancy) logger.info(Component.text("│ ").style(Util.PL_STYLE)
.append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD))
.append(Component.text(" │").style(GenericUtil.STYLE)));
.append(Component.text(" │").style(Util.PL_STYLE)));
else logger.info(String.format("Found language file for %s", localeString));
languageCacheMap.put(localeString, new LanguageCache(localeString));
}
} catch (Throwable t) {
if (logFancy) logger.error(Component.text("│ ").style(GenericUtil.STYLE)
if (logFancy) logger.error(Component.text("│ ").style(Util.PL_STYLE)
.append(Component.text("LANG ERROR").color(NamedTextColor.RED).decorate(TextDecoration.BOLD))
.append(Component.text(" │").style(GenericUtil.STYLE)), t);
.append(Component.text(" │").style(Util.PL_STYLE)), t);
else logger.error("Error while loading translation files!", t);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import me.xginko.villageroptimizer.commands.villageroptimizer.subcommands.ReloadSubCmd;
import me.xginko.villageroptimizer.commands.villageroptimizer.subcommands.VersionSubCmd;
import me.xginko.villageroptimizer.enums.Permissions;
import me.xginko.villageroptimizer.utils.GenericUtil;
import me.xginko.villageroptimizer.utils.Util;
import me.xginko.villageroptimizer.utils.KyoriUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
Expand Down Expand Up @@ -59,17 +59,17 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
private void sendCommandOverview(CommandSender sender) {
if (!sender.hasPermission(Permissions.Commands.RELOAD.get()) && !sender.hasPermission(Permissions.Commands.VERSION.get())) return;
KyoriUtil.sendMessage(sender, Component.text("-----------------------------------------------------").color(NamedTextColor.GRAY));
KyoriUtil.sendMessage(sender, Component.text("VillagerOptimizer Commands").color(GenericUtil.COLOR));
KyoriUtil.sendMessage(sender, Component.text("VillagerOptimizer Commands").color(Util.PL_COLOR));
KyoriUtil.sendMessage(sender, Component.text("-----------------------------------------------------").color(NamedTextColor.GRAY));
subCommands.forEach(subCommand -> KyoriUtil.sendMessage(sender,
subCommand.getSyntax().append(Component.text(" - ").color(NamedTextColor.DARK_GRAY)).append(subCommand.getDescription())));
KyoriUtil.sendMessage(sender,
Component.text("/optimizevillagers <blockradius>").color(GenericUtil.COLOR)
Component.text("/optimizevillagers <blockradius>").color(Util.PL_COLOR)
.append(Component.text(" - ").color(NamedTextColor.DARK_GRAY))
.append(Component.text("Optimize villagers in a radius").color(NamedTextColor.GRAY))
);
KyoriUtil.sendMessage(sender,
Component.text("/unoptmizevillagers <blockradius>").color(GenericUtil.COLOR)
Component.text("/unoptmizevillagers <blockradius>").color(Util.PL_COLOR)
.append(Component.text(" - ").color(NamedTextColor.DARK_GRAY))
.append(Component.text("Unoptimize villagers in a radius").color(NamedTextColor.GRAY))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.xginko.villageroptimizer.commands.SubCommand;
import me.xginko.villageroptimizer.enums.Permissions;
import me.xginko.villageroptimizer.modules.VillagerOptimizerModule;
import me.xginko.villageroptimizer.utils.GenericUtil;
import me.xginko.villageroptimizer.utils.Util;
import me.xginko.villageroptimizer.utils.KyoriUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
Expand All @@ -25,7 +25,7 @@ public TextComponent getDescription() {

@Override
public TextComponent getSyntax() {
return Component.text("/villageroptimizer disable").color(GenericUtil.COLOR);
return Component.text("/villageroptimizer disable").color(Util.PL_COLOR);
}

@Override
Expand All @@ -36,8 +36,8 @@ public void perform(CommandSender sender, String[] args) {
}

KyoriUtil.sendMessage(sender, Component.text("Disabling VillagerOptimizer...").color(NamedTextColor.RED));
VillagerOptimizerModule.modules.forEach(VillagerOptimizerModule::disable);
VillagerOptimizerModule.modules.clear();
VillagerOptimizerModule.MODULES.forEach(VillagerOptimizerModule::disable);
VillagerOptimizerModule.MODULES.clear();
VillagerOptimizer.getCache().cacheMap().clear();
KyoriUtil.sendMessage(sender, Component.text("Disabled all plugin listeners and tasks.").color(NamedTextColor.GREEN));
KyoriUtil.sendMessage(sender, Component.text("You can enable the plugin again using the reload command.").color(NamedTextColor.YELLOW));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.commands.SubCommand;
import me.xginko.villageroptimizer.enums.Permissions;
import me.xginko.villageroptimizer.utils.GenericUtil;
import me.xginko.villageroptimizer.utils.Util;
import me.xginko.villageroptimizer.utils.KyoriUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
Expand All @@ -24,7 +24,7 @@ public TextComponent getDescription() {

@Override
public TextComponent getSyntax() {
return Component.text("/villageroptimizer reload").color(GenericUtil.COLOR);
return Component.text("/villageroptimizer reload").color(Util.PL_COLOR);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.commands.SubCommand;
import me.xginko.villageroptimizer.enums.Permissions;
import me.xginko.villageroptimizer.utils.GenericUtil;
import me.xginko.villageroptimizer.utils.Util;
import me.xginko.villageroptimizer.utils.KyoriUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
Expand All @@ -27,7 +27,7 @@ public TextComponent getDescription() {

@Override
public TextComponent getSyntax() {
return Component.text("/villageroptimizer version").color(GenericUtil.COLOR);
return Component.text("/villageroptimizer version").color(Util.PL_COLOR);
}

@Override
Expand Down Expand Up @@ -57,7 +57,7 @@ public void perform(CommandSender sender, String[] args) {
KyoriUtil.sendMessage(sender, Component.newline()
.append(
Component.text(name + " " + version)
.style(GenericUtil.STYLE)
.style(Util.PL_STYLE)
.clickEvent(ClickEvent.openUrl(website))
)
.append(Component.text(" by ").color(NamedTextColor.GRAY))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import me.xginko.villageroptimizer.VillagerCache;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.config.Config;
import me.xginko.villageroptimizer.utils.GenericUtil;
import me.xginko.villageroptimizer.utils.Util;
import me.xginko.villageroptimizer.utils.LocationUtil;
import org.bukkit.Chunk;
import org.bukkit.Server;
Expand Down Expand Up @@ -111,7 +111,7 @@ public void enable() {
this.periodic_chunk_check = scheduler.runTimer(() -> {
for (World world : server.getWorlds()) {
for (Chunk chunk : world.getLoadedChunks()) {
if (!skip_unloaded_entity_chunks || GenericUtil.isEntitiesLoaded(chunk)) {
if (!skip_unloaded_entity_chunks || Util.isEntitiesLoaded(chunk)) {
this.manageVillagerCount(chunk);
}
}
Expand Down Expand Up @@ -174,7 +174,7 @@ private void manageVillagerCount(@NotNull Chunk chunk) {
scheduler.runAtEntity(villager, kill -> {
villager.remove();
if (log_enabled) {
info("Removed unoptimized villager with profession '" + GenericUtil.formatEnum(villager.getProfession()) + "' at " +
info("Removed unoptimized villager with profession '" + Util.formatEnum(villager.getProfession()) + "' at " +
LocationUtil.toString(villager.getLocation()));
}
});
Expand All @@ -196,7 +196,7 @@ private void manageVillagerCount(@NotNull Chunk chunk) {
villager.remove();

if (log_enabled) {
info("Removed optimized villager with profession '" + GenericUtil.formatEnum(villager.getProfession()) + "' at " +
info("Removed optimized villager with profession '" + Util.formatEnum(villager.getProfession()) + "' at " +
LocationUtil.toString(villager.getLocation()));
}
});
Expand Down
Loading

0 comments on commit 034a270

Please sign in to comment.