Cleaned EcoEnchant

This commit is contained in:
Auxilor 2020-11-28 14:53:43 +00:00
parent 0a0d2415a7
commit b6f699142b
4 changed files with 67 additions and 101 deletions

View File

@ -3,16 +3,14 @@ package com.willfp.ecoenchants.enchantments;
import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.config.ConfigManager; import com.willfp.ecoenchants.config.ConfigManager;
import com.willfp.ecoenchants.config.configs.EnchantmentConfig; import com.willfp.ecoenchants.config.configs.EnchantmentConfig;
import com.willfp.ecoenchants.enchantments.itemtypes.Spell;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity; import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import com.willfp.ecoenchants.enchantments.util.Watcher; import com.willfp.ecoenchants.enchantments.util.Watcher;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderEntry;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager;
import com.willfp.ecoenchants.util.NumberUtils;
import com.willfp.ecoenchants.util.StringUtils; import com.willfp.ecoenchants.util.StringUtils;
import com.willfp.ecoenchants.util.interfaces.ObjectCallable; import com.willfp.ecoenchants.util.interfaces.ObjectCallable;
import com.willfp.ecoenchants.util.interfaces.Registerable; import com.willfp.ecoenchants.util.interfaces.Registerable;
import com.willfp.ecoenchants.util.optional.Prerequisite; import com.willfp.ecoenchants.util.optional.Prerequisite;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang.WordUtils; import org.apache.commons.lang.WordUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
@ -28,7 +26,6 @@ import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.*; import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
@SuppressWarnings({"unchecked", "deprecation"}) @SuppressWarnings({"unchecked", "deprecation"})
public abstract class EcoEnchant extends Enchantment implements Listener, Registerable, Watcher { public abstract class EcoEnchant extends Enchantment implements Listener, Registerable, Watcher {
@ -58,10 +55,10 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
protected EcoEnchant(String key, EcoEnchant.EnchantmentType type, Class<?> plugin, Prerequisite... prerequisites) { protected EcoEnchant(String key, EcoEnchant.EnchantmentType type, Class<?> plugin, Prerequisite... prerequisites) {
super(NamespacedKey.minecraft(key)); super(NamespacedKey.minecraft(key));
if(Pattern.matches("[a-z_]", key)) throw new InvalidEnchantmentException("Key must only contain lowercase letters and underscores"); Validate.isTrue(Pattern.matches("[a-z_]", key), "Key must only contain lowercase letters and underscores");
this.type = type; this.type = type;
this.permissionName = key.replace("_", ""); this.permissionName = key.replaceAll("_", "");
ConfigManager.addEnchantmentConfig(new EnchantmentConfig(this.permissionName, plugin, this.type)); ConfigManager.addEnchantmentConfig(new EnchantmentConfig(this.permissionName, plugin, this.type));
this.config = ConfigManager.getEnchantmentConfig(this.permissionName); this.config = ConfigManager.getEnchantmentConfig(this.permissionName);
@ -79,7 +76,7 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
return; return;
this.update(); this.update();
this.add(); EcoEnchants.addNewEcoEnchant(this);
} }
/** /**
@ -101,7 +98,7 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
target.addAll(config.getTargets()); target.addAll(config.getTargets());
target.forEach(enchantmentTarget -> targetMaterials.addAll(enchantmentTarget.getMaterials())); target.forEach(enchantmentTarget -> targetMaterials.addAll(enchantmentTarget.getMaterials()));
enabled = config.getBool("enabled", true); enabled = config.getBool("enabled", true);
this.updatePlaceholders(); EnchantmentUtils.registerPlaceholders(this);
this.register(); this.register();
} }
@ -138,88 +135,6 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
} catch (NoSuchFieldException | IllegalAccessException ignored) {} } catch (NoSuchFieldException | IllegalAccessException ignored) {}
} }
private void add() {
EcoEnchants.addNewEcoEnchant(this);
}
private void remove() {
EcoEnchants.removeEcoEnchant(this);
}
private void updatePlaceholders() {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "enabled", (player) -> {
return String.valueOf(this.isEnabled());
})
);
this.getConfig().config.getKeys(true).forEach(string -> {
String key = string.replaceAll("\\.", "_").replaceAll("-", "_");
Object object = this.getConfig().config.get(string);
if (object instanceof Integer) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + key, (player) -> {
return ((Integer) object).toString();
})
);
} else if(object instanceof String) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + key, (player) -> {
return (String) object;
})
);
} else if(object instanceof Double) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + key, (player) -> {
return NumberUtils.format((Double) object);
})
);
} else if(object instanceof Collection<?>) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + key, (player) -> {
Collection<?> c = (Collection<?>) object;
return c.stream().map(String::valueOf).collect(Collectors.joining(", "));
})
);
}
});
if(this.getConfig().config.get(EcoEnchants.CONFIG_LOCATION + "chance-per-level") != null) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "chance_per_level", (player) -> {
return NumberUtils.format(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "chance-per-level"));
})
);
}
if(this.getConfig().config.get(EcoEnchants.CONFIG_LOCATION + "multiplier") != null) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "multiplier", (player) -> {
return NumberUtils.format(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "multiplier"));
})
);
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "multiplier_percentage", (player) -> {
return NumberUtils.format(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "multiplier") * 100);
})
);
}
if(this instanceof Spell) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "cooldown", (player) -> {
return NumberUtils.format(Spell.getCooldown((Spell) this, player));
}, true)
);
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "cooldown_total", (player) -> {
return NumberUtils.format(((Spell) this).getCooldownTime());
})
);
}
}
/** /**
* Get if enchantment can be removed in grindstone * Get if enchantment can be removed in grindstone
* @return Whether the enchantment can be removed * @return Whether the enchantment can be removed

View File

@ -1,10 +0,0 @@
package com.willfp.ecoenchants.enchantments;
/**
* Triggered if enchantment is invalid (for extensions)
*/
public class InvalidEnchantmentException extends RuntimeException {
public InvalidEnchantmentException(String errorMessage) {
super(errorMessage);
}
}

View File

@ -2,10 +2,54 @@ package com.willfp.ecoenchants.enchantments.util;
import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.itemtypes.Spell;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderEntry;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager;
import com.willfp.ecoenchants.util.NumberUtils; import com.willfp.ecoenchants.util.NumberUtils;
import com.willfp.ecoenchants.util.StringUtils;
public class EnchantmentUtils { public class EnchantmentUtils {
public static boolean passedChance(EcoEnchant enchantment, int level) { public static boolean passedChance(EcoEnchant enchantment, int level) {
return NumberUtils.randFloat(0, 1) < ((enchantment.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "chance-per-level") * level) / 100); return NumberUtils.randFloat(0, 1) < ((enchantment.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "chance-per-level") * level) / 100);
} }
public static void registerPlaceholders(EcoEnchant enchantment) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(enchantment.getPermissionName() + "_" + "enabled", (player) -> String.valueOf(enchantment.isEnabled()))
);
enchantment.getConfig().config.getKeys(true).forEach(string -> {
String key = string.replaceAll("\\.", "_").replaceAll("-", "_");
Object object = enchantment.getConfig().config.get(string);
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(enchantment.getPermissionName() + "_" + key, (player) -> StringUtils.internalToString(object))
);
});
if(enchantment.getConfig().config.get(EcoEnchants.CONFIG_LOCATION + "chance-per-level") != null) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(enchantment.getPermissionName() + "_" + "chance_per_level", (player) -> NumberUtils.format(enchantment.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "chance-per-level")))
);
}
if(enchantment.getConfig().config.get(EcoEnchants.CONFIG_LOCATION + "multiplier") != null) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(enchantment.getPermissionName() + "_" + "multiplier", (player) -> NumberUtils.format(enchantment.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "multiplier")))
);
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(enchantment.getPermissionName() + "_" + "multiplier_percentage", (player) -> NumberUtils.format(enchantment.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "multiplier") * 100))
);
}
if(enchantment instanceof Spell) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(enchantment.getPermissionName() + "_" + "cooldown", (player) -> NumberUtils.format(Spell.getCooldown((Spell) enchantment, player)), true)
);
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(enchantment.getPermissionName() + "_" + "cooldown_total", (player) -> NumberUtils.format(((Spell) enchantment).getCooldownTime()))
);
}
}
} }

View File

@ -4,8 +4,10 @@ import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Collection;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static net.md_5.bungee.api.ChatColor.COLOR_CHAR; import static net.md_5.bungee.api.ChatColor.COLOR_CHAR;
@ -38,4 +40,19 @@ public class StringUtils {
return matcher.appendTail(buffer).toString(); return matcher.appendTail(buffer).toString();
} }
public static String internalToString(Object object) {
if(object == null) return "null";
if (object instanceof Integer) {
return ((Integer) object).toString();
} else if(object instanceof String) {
return (String) object;
} else if(object instanceof Double) {
return NumberUtils.format((Double) object);
} else if(object instanceof Collection<?>) {
Collection<?> c = (Collection<?>) object;
return c.stream().map(String::valueOf).collect(Collectors.joining(", "));
} else return String.valueOf(object);
}
} }