Skip to content

Commit

Permalink
Merge pull request #78 from teakivy/Config-Rewrite
Browse files Browse the repository at this point in the history
Created new Config util class
  • Loading branch information
teakivy committed Nov 6, 2023
2 parents 3ca24e2 + 93947eb commit 81696d5
Show file tree
Hide file tree
Showing 26 changed files with 155 additions and 113 deletions.
56 changes: 4 additions & 52 deletions src/main/java/me/teakivy/teakstweaks/TeaksTweaks.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.google.gson.Gson;
import me.teakivy.teakstweaks.craftingtweaks.CraftingRegister;
import me.teakivy.teakstweaks.packs.hermitcraft.tag.Tag;
import me.teakivy.teakstweaks.utils.*;
import me.teakivy.teakstweaks.utils.config.Config;
import me.teakivy.teakstweaks.utils.gui.GUIListener;
import me.teakivy.teakstweaks.utils.lang.Translatable;
import me.teakivy.teakstweaks.utils.metrics.Metrics;
Expand All @@ -20,8 +20,6 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;

import static me.teakivy.teakstweaks.utils.metrics.CustomMetrics.registerCustomMetrics;

Expand All @@ -31,26 +29,20 @@ public final class TeaksTweaks extends JavaPlugin implements Listener {

private Register register;

public Tag tagListener;
private static boolean devMode;

/**
* Called when the plugin is enabled
*/
@Override
public void onEnable() {
devMode = getConfig().getBoolean("config.dev-mode");

// Credits
createCredits();

// Metrics
Metrics metrics = new Metrics(this, 12001);
registerCustomMetrics(metrics);

// Update Config.yml
updateConfig();

// Initialize & Update Config
Config.init();

// Language
Translatable.init(getConfig().getString("settings.language"));
Expand All @@ -61,18 +53,12 @@ public void onEnable() {

Bukkit.getScheduler().runTaskLater(this, UpdateChecker::sendUpdateMessage, 20L * 3);


// Crafting Tweaks
CraftingRegister.registerAll();

// Commands
Register.registerCommands();

// Config
this.saveDefaultConfig();

tagListener = new Tag();

// Plugin startup logic
Logger.info(newText(" "));
Logger.info(Translatable.get("startup.plugin.started", Placeholder.parsed("version", this.getDescription().getVersion())));
Expand All @@ -82,6 +68,7 @@ public void onEnable() {
register = new Register();
register.registerAll();

// Remove legacy data.yml file
removeDataFile();
}

Expand Down Expand Up @@ -149,15 +136,6 @@ public static TeaksTweaks getInstance() {
return getPlugin(TeaksTweaks.class);
}

/**
* Get the config section for a pack
* @param pack Pack name
* @return config: packs.[pack]
*/
public static ConfigurationSection getPackConfig(String pack) {
return getInstance().getConfig().getConfigurationSection("packs." + pack);
}

/**
* Create the credits file
*/
Expand All @@ -168,24 +146,6 @@ private void createCredits() {
}
}

/**
* Update the config.yml file
*/
private void updateConfig() {
String configVersion = this.getConfig().getString("config.version");
String pluginConfigVersion = Objects.requireNonNull(this.getConfig().getDefaults()).getString("config.version");

if (!devMode && !configVersion.equalsIgnoreCase(pluginConfigVersion)) return;

try {
ConfigUpdater.update(this, "config.yml", new File(this.getDataFolder(), "config.yml"), Collections.emptyList(), true);
} catch (IOException e) {
e.printStackTrace();
}

Logger.info(newText("Updated Plugin Config"));
}

/**
* Get the Gson instance from JsonManager
* @return Gson instance
Expand All @@ -204,14 +164,6 @@ private void removeDataFile() {
}
}

/**
* Check if the plugin is in dev mode
* @return Boolean
*/
public static boolean isDevMode() {
return devMode;
}

public static Component newText(String text) {
return MiniMessage.miniMessage().deserialize(text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.teakivy.teakstweaks.utils.command.Arg;
import me.teakivy.teakstweaks.utils.command.CommandEvent;
import me.teakivy.teakstweaks.utils.command.CommandType;
import me.teakivy.teakstweaks.utils.config.Config;

import java.util.Arrays;

Expand Down Expand Up @@ -40,9 +41,9 @@ public void sendInfoMessage() {
sendMessage("info.title", insert("version", TeaksTweaks.getInstance().getDescription().getVersion()));
sendText("");
sendMessage("info.author", insert("author", get("plugin.author")));
sendMessage("info.config_version", insert("config_version", getConfig().getString("config.version")));
sendMessage("info.config_generated", insert("config_generated", getConfig().getString("config.created-version")));
if (TeaksTweaks.getInstance().getConfig().getBoolean("config.dev-mode")) {
sendMessage("info.config_version", insert("config_version", Config.getVersion()));
sendMessage("info.config_generated", insert("config_generated", Config.getCreatedVersion()));
if (Config.isDevMode()) {
sendMessage("info.dev_mode_enabled");
}
sendMessage("info.support", insert("discord", get("plugin.discord")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.teakivy.teakstweaks.TeaksTweaks;
import me.teakivy.teakstweaks.packs.PackType;
import me.teakivy.teakstweaks.utils.Logger;
import me.teakivy.teakstweaks.utils.config.Config;
import me.teakivy.teakstweaks.utils.lang.Translatable;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void init() {
* Register the pack
*/
public void register() {
if (TeaksTweaks.getInstance().getConfig().getBoolean("crafting-tweaks." + path + ".enabled")) init();
if (Config.isCraftingTweakEnabled(path)) init();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/teakivy/teakstweaks/packs/BasePack.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.teakivy.teakstweaks.TeaksTweaks;
import me.teakivy.teakstweaks.utils.Logger;
import me.teakivy.teakstweaks.utils.config.Config;
import me.teakivy.teakstweaks.utils.lang.Translatable;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;
Expand Down Expand Up @@ -45,7 +46,7 @@ public BasePack(String path, PackType packType, Material material) {
this.name = Translatable.getString(this.translatableKey + ".name");
this.path = path;
this.packType = packType;
this.config = teaksTweaks.getConfig().getConfigurationSection("packs." + path);
this.config = Config.getPackConfig(path);
this.permission = "teakstweaks." + path;

String[] description = Translatable.getString(this.translatableKey + ".description").split("<newline>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.teakivy.teakstweaks.packs.BasePack;
import me.teakivy.teakstweaks.packs.PackType;
import me.teakivy.teakstweaks.utils.config.Config;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.Particle;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void run() {
marker.setRadius(.001F);
marker.addScoreboardTag("loaded");

if (teaksTweaks.getConfig().getBoolean("packs.chunk-loaders.show-particles")) {
if (Config.getBoolean("packs.chunk-loaders.show-particles")) {
marker.getWorld().spawnParticle(Particle.FLAME, item.getLocation(), 100, 0, 0, 0, .5);
}
item.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.teakivy.teakstweaks.packs.BasePack;
import me.teakivy.teakstweaks.packs.PackType;
import me.teakivy.teakstweaks.utils.Key;
import me.teakivy.teakstweaks.utils.config.Config;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand Down Expand Up @@ -39,7 +40,7 @@ public void init() {
public void registerRecipe() {
if (getConfig().getBoolean("allow-smelting")) {
FurnaceRecipe recipe = new FurnaceRecipe(new ItemStack(Material.GLASS_BOTTLE), Material.EXPERIENCE_BOTTLE);
recipe.setExperience(teaksTweaks.getConfig().getInt("packs.xp-management.take-xp-amount"));
recipe.setExperience(Config.getInt("packs.xp-management.take-xp-amount"));
Bukkit.addRecipe(recipe);
}
}
Expand Down Expand Up @@ -126,10 +127,10 @@ public void bottleXP(PlayerInteractEvent event) {
}

PersistentDataContainer data = xpMeta.getPersistentDataContainer();
data.set(Key.get("xp_amount"), PersistentDataType.INTEGER, teaksTweaks.getConfig().getInt("packs.xp-management.return-xp-amount"));
data.set(Key.get("xp_amount"), PersistentDataType.INTEGER, Config.getInt("packs.xp-management.return-xp-amount"));
xpBottle.setItemMeta(xpMeta);

data.set(Key.get("xp_smelt_amount"), PersistentDataType.INTEGER, teaksTweaks.getConfig().getInt("packs.xp-management.take-xp-amount"));
data.set(Key.get("xp_smelt_amount"), PersistentDataType.INTEGER, Config.getInt("packs.xp-management.take-xp-amount"));
xpBottle.setItemMeta(xpMeta);

if (getConfig().getBoolean("sneak-to-bottle-all") && player.isSneaking()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.teakivy.teakstweaks.packs.BasePack;
import me.teakivy.teakstweaks.packs.PackType;
import me.teakivy.teakstweaks.utils.Key;
import me.teakivy.teakstweaks.utils.config.Config;
import me.teakivy.teakstweaks.utils.lang.Translatable;
import org.bukkit.*;
import org.bukkit.configuration.ConfigurationSection;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void run() {
}

public static void startThunder(Player player, Location loc) {
ConfigurationSection config = TeaksTweaks.getInstance().getConfig().getConfigurationSection("packs.thunder-shrine");
ConfigurationSection config = Config.get().getConfigurationSection("packs.thunder-shrine");
World world = player.getWorld();
if (config.getBoolean("summoning.strike-lightning")) {
world.strikeLightning(loc.add(0, +1, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import me.teakivy.teakstweaks.TeaksTweaks;
import me.teakivy.teakstweaks.utils.config.Config;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Material;
Expand All @@ -25,7 +26,7 @@ public MiniBlockTrade(String name, String texture, Material material) {
}

public MerchantRecipe getTrade() {
MerchantRecipe recipe = new MerchantRecipe(getSkull(), TeaksTweaks.getInstance().getConfig().getInt("packs.wandering-trades.mini-blocks.per-trade"));
MerchantRecipe recipe = new MerchantRecipe(getSkull(), Config.getInt("packs.wandering-trades.mini-blocks.per-trade"));

recipe.addIngredient(new ItemStack(Material.EMERALD, 1));
recipe.addIngredient(new ItemStack(material));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.internal.LinkedTreeMap;
import me.teakivy.teakstweaks.TeaksTweaks;
import me.teakivy.teakstweaks.utils.JsonManager;
import me.teakivy.teakstweaks.utils.config.Config;
import org.bukkit.Material;
import org.bukkit.inventory.MerchantRecipe;

Expand Down Expand Up @@ -49,12 +50,12 @@ public static void load() {

public static List<MerchantRecipe> getBlockTrades() {
List<MerchantRecipe> recipes = new ArrayList<>();
int amount = TeaksTweaks.getInstance().getConfig().getInt("packs.wandering-trades.mini-blocks.amount-of-trades");
int amount = Config.getInt("packs.wandering-trades.mini-blocks.amount-of-trades");
List<Integer> numbers = new ArrayList<>();

if (!TeaksTweaks.getInstance().getConfig().getBoolean("packs.wandering-trades.mini-blocks.has-mini-blocks")) return recipes;
if (!Config.getBoolean("packs.wandering-trades.mini-blocks.has-mini-blocks")) return recipes;

if (TeaksTweaks.getInstance().getConfig().getBoolean("config.dev-mode")) {
if (Config.isDevMode()) {
for (MiniBlockTrade miniBlock : miniBlocks) {
recipes.add(miniBlock.getTrade());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.teakivy.teakstweaks.packs.BasePack;
import me.teakivy.teakstweaks.packs.PackType;
import me.teakivy.teakstweaks.utils.config.Config;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -56,7 +57,7 @@ private List<MerchantRecipe> getHeadTrades() {
}
}

if (teaksTweaks.getConfig().getBoolean("config.dev-mode")) {
if (Config.isDevMode()) {
for (String player : players) {
trades.add(newHeadRecipe(player));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.teakivy.teakstweaks.packs.BasePack;
import me.teakivy.teakstweaks.packs.PackType;
import me.teakivy.teakstweaks.utils.config.Config;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Statistic;
Expand All @@ -24,7 +25,7 @@ public Phantoms() {
@EventHandler
public void onSpawn(CreatureSpawnEvent event) {
Entity entity = event.getEntity();
if (entity.getType() != EntityType.PHANTOM || (event.getSpawnReason() != CreatureSpawnEvent.SpawnReason.NATURAL && !teaksTweaks.getConfig().getBoolean("config.dev-mode"))) return;
if (entity.getType() != EntityType.PHANTOM || (event.getSpawnReason() != CreatureSpawnEvent.SpawnReason.NATURAL && !Config.isDevMode())) return;
Phantom phantom = (Phantom) entity;
Player player = null;
double distance = Integer.MAX_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.authlib.properties.PropertyMap;
import me.teakivy.teakstweaks.TeaksTweaks;
import me.teakivy.teakstweaks.utils.ReflectionUtils;
import me.teakivy.teakstweaks.utils.config.Config;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.apache.commons.lang3.text.WordUtils;
Expand Down Expand Up @@ -62,7 +63,7 @@ public void onDeath(EntityDeathEvent event) {
}

public boolean dropHead(EntityDeathEvent event) {
if (TeaksTweaks.getInstance().getConfig().getBoolean("config.dev-mode")) return true;
if (Config.isDevMode()) return true;
return shouldDrop(event.getEntity().getKiller());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.teakivy.teakstweaks.packs.BasePack;
import me.teakivy.teakstweaks.packs.PackType;
import me.teakivy.teakstweaks.utils.Logger;
import me.teakivy.teakstweaks.utils.config.Config;
import me.teakivy.teakstweaks.utils.lang.Translatable;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
Expand Down Expand Up @@ -133,31 +134,31 @@ public static void unAFK(Player player) {

public static void displayAFKMessage(Player player, Boolean isAFK) {
if (isAFK) {
if (teaksTweaks.getConfig().getBoolean("packs.afk-display.message.display-to-self")) {
if (Config.getBoolean("packs.afk-display.message.display-to-self")) {
player.sendMessage(Translatable.get("afk_display.self_now_afk"));
}
if (teaksTweaks.getConfig().getBoolean("packs.afk-display.message.display-to-everyone")) {
if (Config.getBoolean("packs.afk-display.message.display-to-everyone")) {
for (Player player1 : Bukkit.getOnlinePlayers()) {
if (player1.getUniqueId() != player.getUniqueId()) {
player1.sendMessage(Translatable.get("afk_display.other_now_afk", insert("player", player.getName())));
}
}
}
if (teaksTweaks.getConfig().getBoolean("packs.afk-display.message.display-to-console")) {
if (Config.getBoolean("packs.afk-display.message.display-to-console")) {
Logger.info(Translatable.get("afk_display.other_now_afk", insert("player", player.getName())));
}
} else {
if (teaksTweaks.getConfig().getBoolean("packs.afk-display.message.display-to-self")) {
if (Config.getBoolean("packs.afk-display.message.display-to-self")) {
player.sendMessage(Translatable.get("afk_display.self_not_afk"));
}
if (teaksTweaks.getConfig().getBoolean("packs.afk-display.message.display-to-everyone")) {
if (Config.getBoolean("packs.afk-display.message.display-to-everyone")) {
for (Player player1 : Bukkit.getOnlinePlayers()) {
if (player1.getUniqueId() != player.getUniqueId()) {
player1.sendMessage(Translatable.get("afk_display.other_not_afk", insert("player", player.getName())));
}
}
}
if (teaksTweaks.getConfig().getBoolean("packs.afk-display.message.display-to-console")) {
if (Config.getBoolean("packs.afk-display.message.display-to-console")) {
Logger.info(Translatable.get("afk_display.other_not_afk", insert("player", player.getName())));
}
}
Expand Down
Loading

0 comments on commit 81696d5

Please sign in to comment.