Skip to content

Commit

Permalink
Fix Settings.yml config reload & Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Nov 12, 2023
1 parent e01ff68 commit 67ae6d0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/main/java/dev/mrshawn/deathmessages/files/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;
import java.util.Map;

public enum Config implements ConfigEnum {
public enum Config {

DISABLE_DEFAULT_MESSAGES("Disable-Default-Messages", true),
ADD_PREFIX_TO_ALL_MESSAGES("Add-Prefix-To-All-Messages", true),
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/dev/mrshawn/deathmessages/files/ConfigEnum.java

This file was deleted.

20 changes: 9 additions & 11 deletions src/main/java/dev/mrshawn/deathmessages/files/FileSettings.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.mrshawn.deathmessages.files;

import dev.mrshawn.deathmessages.DeathMessages;
import org.apache.logging.log4j.LogManager;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
Expand All @@ -13,22 +13,20 @@
import java.util.Map;

public class FileSettings<C extends Enum<C>> {
private final JavaPlugin plugin;
private final String fileName;
private final File file;
private YamlConfiguration yamlConfig;
private final Map<Enum<C>, Object> values = new HashMap<>();

public FileSettings(JavaPlugin plugin, String fileName) {
this.plugin = plugin;
public FileSettings(String fileName) {
this.fileName = fileName;
this.file = new File(plugin.getDataFolder(), fileName);
this.file = new File(DeathMessages.getInstance().getDataFolder(), fileName);
loadFile();
}

private void loadFile() {
if (!file.exists()) {
plugin.saveResource(fileName, false);
DeathMessages.getInstance().saveResource(fileName, false);
}
}

Expand All @@ -45,16 +43,16 @@ public FileSettings<C> loadSettings(Class<C> enumClass) {

EnumSet<C> enumSet = EnumSet.allOf(enumClass);
for (C value : enumSet) {
if (!(value instanceof ConfigEnum configEnum)) {
if (!(value instanceof Config config)) {
throw new IllegalArgumentException("Enum " + enumClass.getName() + " must implement ConfigEnum");
}

String configPath = configEnum.getPath();
String configPath = config.getPath();
if (yamlConfig.contains(configPath)) {
// Dreeam TODO - will not reach here when reload configs
values.put(value, yamlConfig.get(configPath));
} else {
Object defaultValue = configEnum.getDefault();
Object defaultValue = config.getDefault();
if (defaultValue != null) {
yamlConfig.set(configPath, defaultValue);
values.put(value, defaultValue);
Expand Down Expand Up @@ -93,8 +91,8 @@ public <T> T get(Enum<?> value, Class<T> clazz) {
return clazz.cast(values.get(value));
}

public void set(Enum<C> enumValue, ConfigEnum configEnum, Object setValue) {
public void set(Enum<C> enumValue, Config config, Object setValue) {
values.put(enumValue, setValue);
yamlConfig.set(configEnum.getPath(), setValue);
yamlConfig.set(config.getPath(), setValue);
}
}
79 changes: 41 additions & 38 deletions src/main/java/dev/mrshawn/deathmessages/utils/Assets.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import dev.mrshawn.deathmessages.config.EntityDeathMessages;
import dev.mrshawn.deathmessages.config.Messages;
import dev.mrshawn.deathmessages.config.PlayerDeathMessages;
import dev.mrshawn.deathmessages.config.Settings;
import dev.mrshawn.deathmessages.enums.DeathAffiliation;
import dev.mrshawn.deathmessages.enums.DeathModes;
import dev.mrshawn.deathmessages.enums.MobType;
import dev.mrshawn.deathmessages.files.Config;
import dev.mrshawn.deathmessages.files.FileSettings;
import dev.mrshawn.deathmessages.kotlin.files.FileStore;
import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.key.Key;
Expand Down Expand Up @@ -61,8 +61,9 @@

public class Assets {

private static final FileSettings<Config> config = FileStore.INSTANCE.getCONFIG();
private static final boolean addPrefix = config.getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES);
// Dreeam TODO - to figure out why the value defined in private static field will not change with the change of the config value
//private static final CommentedConfiguration config = Settings.getInstance().getConfig();
//private static final boolean addPrefix = config.getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES.getPath());
public static final HashMap<String, String> addingMessage = new HashMap<>();

public static boolean isNumeric(String s) {
Expand Down Expand Up @@ -109,7 +110,7 @@ public static boolean itemNameIsWeapon(ItemStack itemStack) {

String displayName = itemStack.getItemMeta().getDisplayName();

for (String s : config.getStringList(Config.CUSTOM_ITEM_DISPLAY_NAMES_IS_WEAPON)) {
for (String s : Settings.getInstance().getConfig().getStringList(Config.CUSTOM_ITEM_DISPLAY_NAMES_IS_WEAPON.getPath())) {
Pattern pattern = Pattern.compile(s);
Matcher matcher = pattern.matcher(displayName);
if (matcher.find()) {
Expand All @@ -120,7 +121,7 @@ public static boolean itemNameIsWeapon(ItemStack itemStack) {
}

public static boolean itemMaterialIsWeapon(ItemStack itemStack) {
for (String s : config.getStringList(Config.CUSTOM_ITEM_MATERIAL_IS_WEAPON)) {
for (String s : Settings.getInstance().getConfig().getStringList(Config.CUSTOM_ITEM_MATERIAL_IS_WEAPON.getPath())) {
Material material = Material.getMaterial(s);
if (itemStack.getType().equals(material)) {
return true;
Expand Down Expand Up @@ -266,14 +267,16 @@ public static TextComponent entityDeathMessage(EntityManager em, MobType mobType
public static TextComponent getNaturalDeath(PlayerManager pm, String damageCause) {
List<String> msgs = sortList(getPlayerDeathMessages().getStringList("Natural-Cause." + damageCause), pm.getPlayer(), pm.getPlayer());

if (config.getBoolean(Config.DEBUG)) System.out.println("Natural-Cause." + damageCause); // Dreeam - debug
if (Settings.getInstance().getConfig().getBoolean(Config.DEBUG.getPath())) System.out.println("Natural-Cause." + damageCause); // Dreeam - debug

String msg = msgs.get(ThreadLocalRandom.current().nextInt(msgs.size()));
msg = playerDeathPlaceholders(msg, pm, null);

TextComponent.Builder base = Component.text();

if (addPrefix) {
// Dreeam - For debug
//System.out.println(Settings.getInstance().getConfig().getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES.getPath()));
//System.out.println(FileStore.INSTANCE.getCONFIG().getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES));
if (Settings.getInstance().getConfig().getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES.getPath())) {
TextComponent prefix = Assets.convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix"));
base.append(prefix);
}
Expand Down Expand Up @@ -315,8 +318,8 @@ public static TextComponent getNaturalDeath(PlayerManager pm, String damageCause

String displayName;
if (i.getItemMeta() == null || !i.getItemMeta().hasDisplayName() || i.getItemMeta().getDisplayName().isEmpty()) {
if (config.getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED)) {
if (!config.getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_IGNORE_ENCHANTMENTS)) {
if (Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED.getPath())) {
if (!Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_IGNORE_ENCHANTMENTS.getPath())) {
if (i.getEnchantments().isEmpty()) {
return getNaturalDeath(pm, "Projectile-Unknown");
}
Expand Down Expand Up @@ -379,14 +382,14 @@ public static TextComponent getWeapon(boolean gang, PlayerManager pm, LivingEnti
msgs = sortList(getPlayerDeathMessages().getStringList("Custom-Mobs.Mythic-Mobs." + internalMobType + "." + affiliation + ".Weapon"), pm.getPlayer(), mob);
}

if (config.getBoolean(Config.DEBUG)) System.out.println(mode + "." + affiliation + ".Weapon"); // Dreeam - debug
if (Settings.getInstance().getConfig().getBoolean(Config.DEBUG.getPath())) System.out.println(mode + "." + affiliation + ".Weapon"); // Dreeam - debug

String msg = msgs.get(ThreadLocalRandom.current().nextInt(msgs.size()));
msg = playerDeathPlaceholders(msg, pm, mob);

TextComponent.Builder base = Component.text();

if (addPrefix) {
if (Settings.getInstance().getConfig().getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES.getPath())) {
TextComponent prefix = Assets.convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix"));
base.append(prefix);
}
Expand Down Expand Up @@ -464,14 +467,14 @@ public static TextComponent getEntityDeathWeapon(Player p, Entity e, MobType mob
if (tameable.getOwner() != null) hasOwner = true;
}

if (config.getBoolean(Config.DEBUG)) System.out.println("Entities." + entityName + ".Weapon"); // Dreeam - debug
if (Settings.getInstance().getConfig().getBoolean(Config.DEBUG.getPath())) System.out.println("Entities." + entityName + ".Weapon"); // Dreeam - debug

String msg = msgs.get(ThreadLocalRandom.current().nextInt(msgs.size()));
msg = entityDeathPlaceholders(msg, p, e, hasOwner);

TextComponent.Builder base = Component.text();

if (addPrefix) {
if (Settings.getInstance().getConfig().getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES.getPath())) {
TextComponent prefix = Assets.convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix"));
base.append(prefix);
}
Expand All @@ -480,15 +483,15 @@ public static TextComponent getEntityDeathWeapon(Player p, Entity e, MobType mob
String displayName;
ItemStack i = p.getEquipment().getItemInMainHand();
if (i.getItemMeta() == null || !i.getItemMeta().hasDisplayName() || i.getItemMeta().getDisplayName().isEmpty()) {
if (config.getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED)) {
if (!config.getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_IGNORE_ENCHANTMENTS)) {
if (Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED.getPath())) {
if (!Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_IGNORE_ENCHANTMENTS.getPath())) {
if (i.getEnchantments().isEmpty()) {
return getEntityDeath(p, e,
config.getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_WEAPON_DEFAULT_TO), mobType);
Settings.getInstance().getConfig().getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_WEAPON_DEFAULT_TO.getPath()), mobType);
}
} else {
return getEntityDeath(p, e,
config.getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_WEAPON_DEFAULT_TO), mobType);
Settings.getInstance().getConfig().getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_WEAPON_DEFAULT_TO.getPath()), mobType);
}
}
displayName = Assets.convertString(i.getType().name());
Expand Down Expand Up @@ -543,12 +546,12 @@ public static TextComponent get(boolean gang, PlayerManager pm, LivingEntity mob
msgs = sortList(getPlayerDeathMessages().getStringList("Custom-Mobs.Mythic-Mobs." + internalMobType + "." + affiliation + "." + damageCause), pm.getPlayer(), mob);
}

if (config.getBoolean(Config.DEBUG)) System.out.println(mode + "." + affiliation + "." + damageCause); // Dreeam - debug
if (Settings.getInstance().getConfig().getBoolean(Config.DEBUG.getPath())) System.out.println(mode + "." + affiliation + "." + damageCause); // Dreeam - debug

if (msgs.isEmpty()) {
if (config.getBoolean(Config.DEFAULT_NATURAL_DEATH_NOT_DEFINED))
if (Settings.getInstance().getConfig().getBoolean(Config.DEFAULT_NATURAL_DEATH_NOT_DEFINED.getPath()))
return getNaturalDeath(pm, damageCause);
if (config.getBoolean(Config.DEFAULT_MELEE_LAST_DAMAGE_NOT_DEFINED))
if (Settings.getInstance().getConfig().getBoolean(Config.DEFAULT_MELEE_LAST_DAMAGE_NOT_DEFINED.getPath()))
return get(gang, pm, mob, getSimpleCause(EntityDamageEvent.DamageCause.ENTITY_ATTACK));
return null;
}
Expand All @@ -557,7 +560,7 @@ public static TextComponent get(boolean gang, PlayerManager pm, LivingEntity mob

TextComponent.Builder base = Component.text();

if (addPrefix) {
if (Settings.getInstance().getConfig().getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES.getPath())) {
TextComponent prefix = Assets.convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix"));
base.append(prefix);
}
Expand Down Expand Up @@ -591,14 +594,14 @@ public static TextComponent getProjectile(boolean gang, PlayerManager pm, Living
msgs = sortList(getPlayerDeathMessages().getStringList("Custom-Mobs.Mythic-Mobs." + internalMobType + "." + affiliation + "." + projectileDamage), pm.getPlayer(), mob);
}

if (config.getBoolean(Config.DEBUG)) System.out.println(mode + "." + affiliation + "." + projectileDamage); // Dreeam - debug
if (Settings.getInstance().getConfig().getBoolean(Config.DEBUG.getPath())) System.out.println(mode + "." + affiliation + "." + projectileDamage); // Dreeam - debug

String msg = msgs.get(ThreadLocalRandom.current().nextInt(msgs.size()));
msg = playerDeathPlaceholders(msg, pm, mob);

TextComponent.Builder base = Component.text();

if (addPrefix) {
if (Settings.getInstance().getConfig().getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES.getPath())) {
TextComponent prefix = Assets.convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix"));
base.append(prefix);
}
Expand All @@ -608,10 +611,10 @@ public static TextComponent getProjectile(boolean gang, PlayerManager pm, Living
String displayName;
HoverEvent<HoverEvent.ShowItem> showItem;
if (i.getItemMeta() == null || !i.getItemMeta().hasDisplayName() || i.getItemMeta().getDisplayName().isEmpty()) {
if (config.getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED)) {
if (!config.getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO)
if (Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED.getPath())) {
if (!Settings.getInstance().getConfig().getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO.getPath())
.equals(projectileDamage)) {
return getProjectile(gang, pm, mob, config.getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO));
return getProjectile(gang, pm, mob, Settings.getInstance().getConfig().getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO.getPath()));
}
}
displayName = Assets.convertString(i.getType().name());
Expand Down Expand Up @@ -666,8 +669,8 @@ public static TextComponent getEntityDeathProjectile(Player p, EntityManager em,
}

if (msgs.isEmpty()) {
if (config.getBoolean(Config.DEBUG)) System.out.println("Entities." + entityName + "." + projectileDamage); // Dreeam - debug
if (config.getBoolean(Config.DEFAULT_MELEE_LAST_DAMAGE_NOT_DEFINED)) {
if (Settings.getInstance().getConfig().getBoolean(Config.DEBUG.getPath())) System.out.println("Entities." + entityName + "." + projectileDamage); // Dreeam - debug
if (Settings.getInstance().getConfig().getBoolean(Config.DEFAULT_MELEE_LAST_DAMAGE_NOT_DEFINED.getPath())) {
return getEntityDeath(p, em.getEntity(), getSimpleCause(EntityDamageEvent.DamageCause.ENTITY_ATTACK), mobType);
}
return null;
Expand All @@ -683,7 +686,7 @@ public static TextComponent getEntityDeathProjectile(Player p, EntityManager em,

TextComponent.Builder base = Component.text();

if (addPrefix) {
if (Settings.getInstance().getConfig().getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES.getPath())) {
TextComponent prefix = Assets.convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix"));
base.append(prefix);
}
Expand All @@ -692,11 +695,11 @@ public static TextComponent getEntityDeathProjectile(Player p, EntityManager em,
ItemStack i = p.getEquipment().getItemInMainHand();
String displayName;
if (i.getItemMeta() == null || !i.getItemMeta().hasDisplayName() || i.getItemMeta().getDisplayName().isEmpty()) {
if (config.getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED)) {
if (!config.getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO)
if (Settings.getInstance().getConfig().getBoolean(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_ENABLED.getPath())) {
if (!Settings.getInstance().getConfig().getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO.getPath())
.equals(projectileDamage)) {
return getEntityDeathProjectile(p, em,
config.getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO), mobType);
Settings.getInstance().getConfig().getString(Config.DISABLE_WEAPON_KILL_WITH_NO_CUSTOM_NAME_SOURCE_PROJECTILE_DEFAULT_TO.getPath()), mobType);
}
}
displayName = Assets.convertString(i.getType().name());
Expand Down Expand Up @@ -762,13 +765,13 @@ public static TextComponent getEntityDeath(Player player, Entity e, String damag
msgs = sortList(getEntityDeathMessages().getStringList("Mythic-Mobs-Entities." + internalMobType + "." + damageCause), player, e);
}

if (config.getBoolean(Config.DEBUG)) System.out.println("Entities." + entityName + "." + damageCause); // Dreeam - debug
if (Settings.getInstance().getConfig().getBoolean(Config.DEBUG.getPath())) System.out.println("Entities." + entityName + "." + damageCause); // Dreeam - debug

String msg = msgs.get(ThreadLocalRandom.current().nextInt(msgs.size()));

TextComponent.Builder base = Component.text();

if (addPrefix) {
if (Settings.getInstance().getConfig().getBoolean(Config.ADD_PREFIX_TO_ALL_MESSAGES.getPath())) {
TextComponent prefix = Assets.convertFromLegacy(Messages.getInstance().getConfig().getString("Prefix"));
base.append(prefix);
}
Expand Down Expand Up @@ -885,16 +888,16 @@ public static String playerDeathPlaceholders(String msg, PlayerManager pm, Livin
}
} else {
String mobName = mob.getName();
if (config.getBoolean(Config.RENAME_MOBS_ENABLED)) {
String[] chars = config.getString(Config.RENAME_MOBS_IF_CONTAINS).split("(?!^)");
if (Settings.getInstance().getConfig().getBoolean(Config.RENAME_MOBS_ENABLED.getPath())) {
String[] chars = Settings.getInstance().getConfig().getString(Config.RENAME_MOBS_IF_CONTAINS.getPath()).split("(?!^)");
for (String ch : chars) {
if (mobName.contains(ch)) {
mobName = Messages.getInstance().getConfig().getString("Mobs." + mob.getType().toString().toLowerCase());
break;
}
}
}
if (!(mob instanceof Player) && config.getBoolean(Config.DISABLE_NAMED_MOBS)) {
if (!(mob instanceof Player) && Settings.getInstance().getConfig().getBoolean(Config.DISABLE_NAMED_MOBS.getPath())) {
mobName = Messages.getInstance().getConfig().getString("Mobs." + mob.getType().toString().toLowerCase());
}
msg = msg
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package dev.mrshawn.deathmessages.kotlin.files

import dev.mrshawn.deathmessages.DeathMessages
import dev.mrshawn.deathmessages.files.Config
import dev.mrshawn.deathmessages.files.FileSettings

object FileStore {
val CONFIG: FileSettings<Config> =
FileSettings<Config>(DeathMessages.getInstance(), "Settings.yml").loadSettings(Config::class.java)
FileSettings<Config>("Settings.yml").loadSettings(Config::class.java)
}

0 comments on commit 67ae6d0

Please sign in to comment.