diff --git a/config/checkstyle/suppression.xml b/config/checkstyle/suppression.xml index cb761250..f463a65e 100644 --- a/config/checkstyle/suppression.xml +++ b/config/checkstyle/suppression.xml @@ -13,6 +13,10 @@ + + + + diff --git a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/EcoCraftEnchantmentManager.java b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/EcoCraftEnchantmentManager.java new file mode 100644 index 00000000..8dbee75f --- /dev/null +++ b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/EcoCraftEnchantmentManager.java @@ -0,0 +1,25 @@ +package com.willfp.ecoenchants.proxy.v1_16_R3; + +import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata; +import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantments; +import com.willfp.ecoenchants.proxy.proxies.EcoCraftEnchantmentManagerProxy; +import com.willfp.ecoenchants.proxy.v1_16_R3.vanilla.EcoCraftEnchantment; +import net.minecraft.server.v1_16_R3.Enchantment; +import net.minecraft.server.v1_16_R3.IRegistry; +import org.bukkit.NamespacedKey; +import org.bukkit.craftbukkit.v1_16_R3.util.CraftNamespacedKey; + +import java.util.Map; + +public final class EcoCraftEnchantmentManager implements EcoCraftEnchantmentManagerProxy { + @Override + public void registerNewCraftEnchantments() { + Map metadataMap = VanillaEnchantments.getMetadataMap(); + + for (Enchantment enchantment : IRegistry.ENCHANTMENT) { + NamespacedKey key = CraftNamespacedKey.fromMinecraft(IRegistry.ENCHANTMENT.getKey(enchantment)); + VanillaEnchantmentMetadata metadata = metadataMap.get(org.bukkit.enchantments.Enchantment.getByKey(key)); + new EcoCraftEnchantment(enchantment, metadata).register(); + } + } +} diff --git a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/vanilla/EcoCraftEnchantment.java b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/vanilla/EcoCraftEnchantment.java new file mode 100644 index 00000000..dbaba3ba --- /dev/null +++ b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/vanilla/EcoCraftEnchantment.java @@ -0,0 +1,56 @@ +package com.willfp.ecoenchants.proxy.v1_16_R3.vanilla; + +import com.willfp.ecoenchants.enchantments.support.vanilla.EcoCraftEnchantmentWrapper; +import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata; +import net.minecraft.server.v1_16_R3.Enchantment; +import org.bukkit.NamespacedKey; +import org.bukkit.craftbukkit.v1_16_R3.enchantments.CraftEnchantment; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +@SuppressWarnings("unchecked") +public class EcoCraftEnchantment extends CraftEnchantment implements EcoCraftEnchantmentWrapper { + private final VanillaEnchantmentMetadata metadata; + + public EcoCraftEnchantment(@NotNull final Enchantment target, + @NotNull final VanillaEnchantmentMetadata metadata) { + super(target); + this.metadata = metadata; + } + + @Override + public int getMaxLevel() { + return metadata.getMaxLevel(); + } + + public void register() { + try { + Field byIdField = org.bukkit.enchantments.Enchantment.class.getDeclaredField("byKey"); + Field byNameField = org.bukkit.enchantments.Enchantment.class.getDeclaredField("byName"); + byIdField.setAccessible(true); + byNameField.setAccessible(true); + Map byKey = (Map) byIdField.get(null); + Map byName = (Map) byNameField.get(null); + byKey.remove(this.getKey()); + byName.remove(this.getName()); + + Map byNameClone = new HashMap<>(byName); + for (Map.Entry entry : byNameClone.entrySet()) { + if (entry.getValue().getKey().equals(this.getKey())) { + byName.remove(entry.getKey()); + } + } + + Field f = org.bukkit.enchantments.Enchantment.class.getDeclaredField("acceptingNew"); + f.setAccessible(true); + f.set(null, true); + f.setAccessible(false); + + org.bukkit.enchantments.Enchantment.registerEnchantment(this); + } catch (NoSuchFieldException | IllegalAccessException ignored) { + } + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java index dbff7815..ebc71783 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java @@ -241,7 +241,7 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Watche Map byKey = (Map) byIdField.get(null); Map byName = (Map) byNameField.get(null); byKey.remove(this.getKey()); - byName.remove(this.getDisplayName()); + byName.remove(this.getName()); Map byNameClone = new HashMap<>(byName); for (Map.Entry entry : byNameClone.entrySet()) { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchants.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchants.java index 42b21dbd..96931814 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchants.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchants.java @@ -236,12 +236,12 @@ import com.willfp.ecoenchants.enchantments.ecoenchants.spell.Dynamite; import com.willfp.ecoenchants.enchantments.ecoenchants.spell.Missile; import com.willfp.ecoenchants.enchantments.ecoenchants.spell.Quake; import com.willfp.ecoenchants.enchantments.ecoenchants.spell.Vitalize; -import com.willfp.ecoenchants.enchantments.itemtypes.VanillaEcoEnchantWrapper; import com.willfp.ecoenchants.enchantments.meta.EnchantmentType; +import com.willfp.ecoenchants.proxy.proxies.EcoCraftEnchantmentManagerProxy; +import com.willfp.ecoenchants.util.ProxyUtils; import lombok.experimental.UtilityClass; import org.bukkit.NamespacedKey; import org.bukkit.enchantments.Enchantment; -import org.bukkit.enchantments.EnchantmentWrapper; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.jetbrains.annotations.NotNull; @@ -579,6 +579,8 @@ public class EcoEnchants { for (EcoEnchant ecoEnchant : new HashSet<>(values())) { ecoEnchant.update(); } + + ProxyUtils.getProxy(EcoCraftEnchantmentManagerProxy.class).registerNewCraftEnchantments(); } /** @@ -604,14 +606,4 @@ public class EcoEnchants { BY_KEY.remove(enchant.getKey()); BY_NAME.inverse().remove(enchant); } - - static { - for (Enchantment value : Enchantment.values()) { - if (value instanceof EcoEnchant || value instanceof EnchantmentWrapper) { - continue; - } - - new VanillaEcoEnchantWrapper(value); - } - } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/itemtypes/VanillaEcoEnchantWrapper.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/itemtypes/VanillaEcoEnchantWrapper.java deleted file mode 100644 index 363412a8..00000000 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/itemtypes/VanillaEcoEnchantWrapper.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.willfp.ecoenchants.enchantments.itemtypes; - -import com.willfp.ecoenchants.enchantments.EcoEnchant; -import com.willfp.ecoenchants.enchantments.meta.EnchantmentType; -import org.bukkit.enchantments.Enchantment; -import org.jetbrains.annotations.NotNull; - -@SuppressWarnings("deprecation") -public class VanillaEcoEnchantWrapper extends EcoEnchant { - /** - * Create a new Vanilla EcoEnchant Wrapper. - * - * @param enchantment The enchantment to wrap. - */ - public VanillaEcoEnchantWrapper(@NotNull final Enchantment enchantment) { - super(enchantment.getKey().getKey(), enchantment.isCursed() ? EnchantmentType.CURSE : EnchantmentType.NORMAL); - } -} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/vanilla/EcoCraftEnchantmentWrapper.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/vanilla/EcoCraftEnchantmentWrapper.java new file mode 100644 index 00000000..7719237b --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/vanilla/EcoCraftEnchantmentWrapper.java @@ -0,0 +1,4 @@ +package com.willfp.ecoenchants.enchantments.support.vanilla; + +public interface EcoCraftEnchantmentWrapper { +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/vanilla/VanillaEnchantmentMetadata.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/vanilla/VanillaEnchantmentMetadata.java new file mode 100644 index 00000000..37674853 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/vanilla/VanillaEnchantmentMetadata.java @@ -0,0 +1,11 @@ +package com.willfp.ecoenchants.enchantments.support.vanilla; + +import lombok.Data; + +@Data +public class VanillaEnchantmentMetadata { + /** + * The maximum level for the enchantment. + */ + private final int maxLevel; +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/vanilla/VanillaEnchantments.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/vanilla/VanillaEnchantments.java new file mode 100644 index 00000000..c3e009f2 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/support/vanilla/VanillaEnchantments.java @@ -0,0 +1,19 @@ +package com.willfp.ecoenchants.enchantments.support.vanilla; + +import lombok.experimental.UtilityClass; +import org.bukkit.enchantments.Enchantment; + +import java.util.HashMap; +import java.util.Map; + +@UtilityClass +public class VanillaEnchantments { + /** + * Get a map of all custom enchantment metadata. + * + * @return The map. + */ + public Map getMetadataMap() { + return new HashMap<>(); + } +} diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index f6a43416..8d2dd20c 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -35,157 +35,4 @@ special-color: "&d" artifact-color: "&e" spell-color: "&9" -description-color: "&8" - -enchantments: - protection: - name: "Protection" - description: Reduces most types of damage. - - fire_protection: - name: "Fire Protection" - description: Reduces fire damage and burn time. - - feather_falling: - name: "Feather Falling" - description: Reduces fall damage. - - blast_protection: - name: "Blast Protection" - description: Reduces explosion damage and knockback. - - projectile_protection: - name: "Projectile Protection" - description: Reduces projectile damage. - - respiration: - name: "Respiration" - description: Extends underwater breathing time. - - aqua_affinity: - name: "Aqua Affinity" - description: Increases underwater mining speed. - - thorns: - name: "Thorns" - description: Reflects some of the damage taken when hit. - - depth_strider: - name: "Depth Strider" - description: Increases underwater movement speed. - - frost_walker: - name: "Frost Walker" - description: Turns water beneath the player into ice. - - binding_curse: - name: "Curse of Binding" - description: Items cannot be removed from armor slots. - - sharpness: - name: "Sharpness" - description: Increases damage. - - smite: - name: "Smite" - description: Increases damage against undead mobs. - - bane_of_arthropods: - name: "Bane of Arthropods" - description: Increases damage and slows arthropod mobs. - - knockback: - name: "Knockback" - description: Increases knockback. - - fire_aspect: - name: "Fire Aspect" - description: Sets target on fire. - - looting: - name: "Looting" - description: Increases mob loot. - - sweeping: - name: "Sweeping Edge" - description: Increases sweeping attack damage. - - efficiency: - name: "Efficiency" - description: Increases mining speed. - - silk_touch: - name: "Silk Touch" - description: Mined blocks drop themselves exactly. - - unbreaking: - name: "Unbreaking" - description: Increases item durability. - - fortune: - name: "Fortune" - description: Increases certain block drops. - - power: - name: "Power" - description: Increases arrow damage. - - punch: - name: "Punch" - description: Increases arrow knockback. - - flame: - name: "Flame" - description: Arrows set target on fire. - - infinity: - name: "Infinity" - description: Shooting consumes no regular arrows. - - luck_of_the_sea: - name: "Luck of the Sea" - description: Increases rate of good loot. - - lure: - name: "Lure" - description: Decreases fishing wait time. - - loyalty: - name: "Loyalty" - description: Trident returns after being thrown. - - impaling: - name: "Impaling" - description: Trident deals additional damage to ocean mobs. - - riptide: - name: "Riptide" - description: Trident launches player when thrown in water or while raining. - - channeling: - name: "Channeling" - description: Strikes lightning where trident lands during thunderstorms. - - multishot: - name: "Multishot" - description: Shoots 3 arrows. - - quick_charge: - name: "Quick Charge" - description: Decreases crossbow charging time. - - piercing: - name: "Piercing" - description: Arrows pass through multiple entities. - - mending: - name: "Mending" - description: Repair the item while gaining XP orbs. - - vanishing_curse: - name: "Curse of Vanishing" - description: Item destroyed on death. - - soul_speed: - name: "Soul Speed" - description: Increases walking speed on soul sand and soul soil. +description-color: "&8" \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/vanillaenchants.yml b/eco-core/core-plugin/src/main/resources/vanillaenchants.yml new file mode 100644 index 00000000..73d8d95e --- /dev/null +++ b/eco-core/core-plugin/src/main/resources/vanillaenchants.yml @@ -0,0 +1,153 @@ +protection: + name: "Protection" + description: Reduces most types of damage. + max-level: 4 + +fire_protection: + name: "Fire Protection" + description: Reduces fire damage and burn time. + max-level: 4 + +feather_falling: + name: "Feather Falling" + description: Reduces fall damage. + +blast_protection: + name: "Blast Protection" + description: Reduces explosion damage and knockback. + +projectile_protection: + name: "Projectile Protection" + description: Reduces projectile damage. + +respiration: + name: "Respiration" + description: Extends underwater breathing time. + +aqua_affinity: + name: "Aqua Affinity" + description: Increases underwater mining speed. + +thorns: + name: "Thorns" + description: Reflects some of the damage taken when hit. + +depth_strider: + name: "Depth Strider" + description: Increases underwater movement speed. + +frost_walker: + name: "Frost Walker" + description: Turns water beneath the player into ice. + +binding_curse: + name: "Curse of Binding" + description: Items cannot be removed from armor slots. + +sharpness: + name: "Sharpness" + description: Increases damage. + +smite: + name: "Smite" + description: Increases damage against undead mobs. + +bane_of_arthropods: + name: "Bane of Arthropods" + description: Increases damage and slows arthropod mobs. + +knockback: + name: "Knockback" + description: Increases knockback. + +fire_aspect: + name: "Fire Aspect" + description: Sets target on fire. + +looting: + name: "Looting" + description: Increases mob loot. + +sweeping: + name: "Sweeping Edge" + description: Increases sweeping attack damage. + +efficiency: + name: "Efficiency" + description: Increases mining speed. + +silk_touch: + name: "Silk Touch" + description: Mined blocks drop themselves exactly. + +unbreaking: + name: "Unbreaking" + description: Increases item durability. + +fortune: + name: "Fortune" + description: Increases certain block drops. + +power: + name: "Power" + description: Increases arrow damage. + +punch: + name: "Punch" + description: Increases arrow knockback. + +flame: + name: "Flame" + description: Arrows set target on fire. + +infinity: + name: "Infinity" + description: Shooting consumes no regular arrows. + +luck_of_the_sea: + name: "Luck of the Sea" + description: Increases rate of good loot. + +lure: + name: "Lure" + description: Decreases fishing wait time. + +loyalty: + name: "Loyalty" + description: Trident returns after being thrown. + +impaling: + name: "Impaling" + description: Trident deals additional damage to ocean mobs. + +riptide: + name: "Riptide" + description: Trident launches player when thrown in water or while raining. + +channeling: + name: "Channeling" + description: Strikes lightning where trident lands during thunderstorms. + +multishot: + name: "Multishot" + description: Shoots 3 arrows. + +quick_charge: + name: "Quick Charge" + description: Decreases crossbow charging time. + +piercing: + name: "Piercing" + description: Arrows pass through multiple entities. + +mending: + name: "Mending" + description: Repair the item while gaining XP orbs. + +vanishing_curse: + name: "Curse of Vanishing" + description: Item destroyed on death. + +soul_speed: + name: "Soul Speed" + description: Increases walking speed on soul sand and soul soil. diff --git a/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/EcoCraftEnchantmentManagerProxy.java b/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/EcoCraftEnchantmentManagerProxy.java new file mode 100644 index 00000000..e0fc45c9 --- /dev/null +++ b/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/EcoCraftEnchantmentManagerProxy.java @@ -0,0 +1,10 @@ +package com.willfp.ecoenchants.proxy.proxies; + +import com.willfp.eco.core.proxy.AbstractProxy; + +public interface EcoCraftEnchantmentManagerProxy extends AbstractProxy { + /** + * Re-Register new CraftEnchantments for all vanilla enchantments in order to modify their max level. + */ + void registerNewCraftEnchantments(); +}