Reworked EnchantmentType into instantiatable system

This commit is contained in:
Auxilor 2020-11-28 09:40:53 +00:00
parent b143eb279c
commit 7c0e17f1f3
7 changed files with 54 additions and 50 deletions

View File

@ -55,12 +55,7 @@ public class EnchantDisplay {
public static final String PREFIX = "§w"; public static final String PREFIX = "§w";
static String normalColor;
static String curseColor;
static String specialColor;
static String artifactColor;
static String descriptionColor; static String descriptionColor;
static String spellColor;
static int numbersThreshold; static int numbersThreshold;
static boolean useNumerals; static boolean useNumerals;
@ -78,11 +73,6 @@ public class EnchantDisplay {
*/ */
public static void update() { public static void update() {
descriptionColor = StringUtils.translate(ConfigManager.getLang().getString("description-color")); descriptionColor = StringUtils.translate(ConfigManager.getLang().getString("description-color"));
curseColor = StringUtils.translate(ConfigManager.getLang().getString("curse-color"));
specialColor = StringUtils.translate(ConfigManager.getLang().getString("special-color"));
artifactColor = StringUtils.translate(ConfigManager.getLang().getString("artifact-color"));
spellColor = StringUtils.translate(ConfigManager.getLang().getString("spell-color"));
normalColor = StringUtils.translate(ConfigManager.getLang().getString("not-curse-color"));
useNumerals = ConfigManager.getConfig().getBool("lore.use-numerals"); useNumerals = ConfigManager.getConfig().getBool("lore.use-numerals");
numbersThreshold = ConfigManager.getConfig().getInt("lore.use-numbers-above-threshold"); numbersThreshold = ConfigManager.getConfig().getInt("lore.use-numbers-above-threshold");

View File

@ -52,23 +52,7 @@ public class EnchantmentCache {
type = enchantment.isCursed() ? EcoEnchant.EnchantmentType.CURSE : EcoEnchant.EnchantmentType.NORMAL; type = enchantment.isCursed() ? EcoEnchant.EnchantmentType.CURSE : EcoEnchant.EnchantmentType.NORMAL;
} }
switch(type) { color = type.getColor();
case ARTIFACT:
color = EnchantDisplay.artifactColor;
break;
case SPECIAL:
color = EnchantDisplay.specialColor;
break;
case CURSE:
color = EnchantDisplay.curseColor;
break;
case SPELL:
color = EnchantDisplay.spellColor;
break;
default:
color = EnchantDisplay.normalColor;
break;
}
if(EcoEnchants.getFromEnchantment(enchantment) != null) { if(EcoEnchants.getFromEnchantment(enchantment) != null) {
EnchantmentRarity rarity = EcoEnchants.getFromEnchantment(enchantment).getRarity(); EnchantmentRarity rarity = EcoEnchants.getFromEnchantment(enchantment).getRarity();

View File

@ -10,8 +10,10 @@ import com.willfp.ecoenchants.integrations.placeholder.PlaceholderEntry;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager; 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; import com.willfp.ecoenchants.util.StringUtils;
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 jdk.nashorn.internal.codegen.ObjectClassGenerator;
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;
@ -433,24 +435,43 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
return targetMaterials.contains(itemStack.getType()) || itemStack.getType().equals(Material.BOOK) || itemStack.getType().equals(Material.ENCHANTED_BOOK); return targetMaterials.contains(itemStack.getType()) || itemStack.getType().equals(Material.BOOK) || itemStack.getType().equals(Material.ENCHANTED_BOOK);
} }
/** public static class EnchantmentType {
* The types of {@link EcoEnchant} public static final EnchantmentType NORMAL = new EnchantmentType(false, () -> ConfigManager.getLang().getString("not-curse-color"));
*/ public static final EnchantmentType CURSE = new EnchantmentType(false, () -> ConfigManager.getLang().getString("curse-color"));
public enum EnchantmentType { public static final EnchantmentType SPECIAL = new EnchantmentType(() -> !ConfigManager.getConfig().getBool("types.special.allow-multiple"), () -> ConfigManager.getLang().getString("special-color"));
NORMAL(false), public static final EnchantmentType ARTIFACT = new EnchantmentType(() -> !ConfigManager.getConfig().getBool("types.artifact.allow-multiple"), () -> ConfigManager.getLang().getString("artifact-color"));
CURSE(false), public static final EnchantmentType SPELL = new EnchantmentType(true, () -> ConfigManager.getLang().getString("spell-color"));
SPECIAL(true),
ARTIFACT(true),
SPELL(true);
static { private static final Set<EnchantmentType> values = new HashSet<>();
update();
private boolean singular;
private String color;
private final ObjectCallable<String> colorCallable;
private final ObjectCallable<Boolean> singularCallable;
public EnchantmentType(boolean singular, String color) {
this(() -> singular, () -> color);
} }
boolean singular; public EnchantmentType(boolean singular, ObjectCallable<String> colorCallable) {
this(() -> singular, colorCallable);
}
EnchantmentType(boolean singular) { public EnchantmentType(ObjectCallable<Boolean> singularCallable, ObjectCallable<String> colorCallable) {
this.singular = singular; this.singularCallable = singularCallable;
this.colorCallable = colorCallable;
color = colorCallable.call();
singular = singularCallable.call();
values.add(this);
}
private void refresh() {
this.color = colorCallable.call();
this.singular = singularCallable.call();
}
public String getColor() {
return color;
} }
public boolean isSingular() { public boolean isSingular() {
@ -458,8 +479,11 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
} }
public static void update() { public static void update() {
SPECIAL.singular = !ConfigManager.getConfig().getBool("types.special.allow-multiple"); values.forEach(EnchantmentType::refresh);
ARTIFACT.singular = !ConfigManager.getConfig().getBool("types.artifact.allow-multiple"); }
public static Set<EnchantmentType> getValues() {
return values;
} }
} }
} }

View File

@ -121,7 +121,7 @@ public class AnvilMerge {
rightEnchants.forEach(((enchantment, integer) -> { rightEnchants.forEach(((enchantment, integer) -> {
AtomicBoolean doesConflict = new AtomicBoolean(false); AtomicBoolean doesConflict = new AtomicBoolean(false);
Arrays.stream(EcoEnchant.EnchantmentType.values()).forEach(enchantmentType -> { EcoEnchant.EnchantmentType.getValues().forEach(enchantmentType -> {
EcoEnchant enchant = EcoEnchants.getFromEnchantment(enchantment); EcoEnchant enchant = EcoEnchants.getFromEnchantment(enchantment);
if(enchant == null) return; if(enchant == null) return;
if(enchant.getType().equals(enchantmentType) && EcoEnchants.hasAnyOfType(left, enchantmentType) && enchantmentType.isSingular()) doesConflict.set(true); if(enchant.getType().equals(enchantmentType) && EcoEnchants.hasAnyOfType(left, enchantmentType) && enchantmentType.isSingular()) doesConflict.set(true);

View File

@ -1,18 +1,18 @@
package com.willfp.ecoenchants.integrations.placeholder; package com.willfp.ecoenchants.integrations.placeholder;
import com.willfp.ecoenchants.util.interfaces.ObjectCallable; import com.willfp.ecoenchants.util.interfaces.ObjectBiCallable;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class PlaceholderEntry { public class PlaceholderEntry {
private final String identifier; private final String identifier;
private final ObjectCallable<String, Player> function; private final ObjectBiCallable<String, Player> function;
private final boolean requiresPlayer; private final boolean requiresPlayer;
public PlaceholderEntry(String identifier, ObjectCallable<String, Player> function) { public PlaceholderEntry(String identifier, ObjectBiCallable<String, Player> function) {
this(identifier, function, false); this(identifier, function, false);
} }
public PlaceholderEntry(String identifier, ObjectCallable<String, Player> function, boolean requiresPlayer) { public PlaceholderEntry(String identifier, ObjectBiCallable<String, Player> function, boolean requiresPlayer) {
this.identifier = identifier; this.identifier = identifier;
this.function = function; this.function = function;
this.requiresPlayer = requiresPlayer; this.requiresPlayer = requiresPlayer;

View File

@ -0,0 +1,6 @@
package com.willfp.ecoenchants.util.interfaces;
@FunctionalInterface
public interface ObjectBiCallable<A, B> {
A call(B object);
}

View File

@ -1,6 +1,6 @@
package com.willfp.ecoenchants.util.interfaces; package com.willfp.ecoenchants.util.interfaces;
@FunctionalInterface @FunctionalInterface
public interface ObjectCallable<A, B> { public interface ObjectCallable<A> {
A call(B object); A call();
} }