Added vanilla enchantment support for 1.16.0-3

This commit is contained in:
Auxilor 2021-04-27 13:02:04 +01:00
parent d288032b38
commit 00ae789a38
4 changed files with 162 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package com.willfp.ecoenchants.proxy.v1_16_R1;
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_R1.vanilla.EcoCraftEnchantment;
import net.minecraft.server.v1_16_R1.Enchantment;
import net.minecraft.server.v1_16_R1.IRegistry;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.v1_16_R1.util.CraftNamespacedKey;
import java.util.Map;
public final class EcoCraftEnchantmentManager implements EcoCraftEnchantmentManagerProxy {
@Override
public void registerNewCraftEnchantments() {
Map<org.bukkit.enchantments.Enchantment, VanillaEnchantmentMetadata> 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();
}
}
}

View File

@ -0,0 +1,56 @@
package com.willfp.ecoenchants.proxy.v1_16_R1.vanilla;
import com.willfp.ecoenchants.enchantments.support.vanilla.EcoCraftEnchantmentWrapper;
import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata;
import net.minecraft.server.v1_16_R1.Enchantment;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.v1_16_R1.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() == null ? this.getHandle().getMaxLevel() : 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<NamespacedKey, org.bukkit.enchantments.Enchantment> byKey = (Map<NamespacedKey, org.bukkit.enchantments.Enchantment>) byIdField.get(null);
Map<String, org.bukkit.enchantments.Enchantment> byName = (Map<String, org.bukkit.enchantments.Enchantment>) byNameField.get(null);
byKey.remove(this.getKey());
byName.remove(this.getName());
Map<String, org.bukkit.enchantments.Enchantment> byNameClone = new HashMap<>(byName);
for (Map.Entry<String, org.bukkit.enchantments.Enchantment> 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) {
}
}
}

View File

@ -0,0 +1,25 @@
package com.willfp.ecoenchants.proxy.v1_16_R2;
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_R2.vanilla.EcoCraftEnchantment;
import net.minecraft.server.v1_16_R2.Enchantment;
import net.minecraft.server.v1_16_R2.IRegistry;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.v1_16_R2.util.CraftNamespacedKey;
import java.util.Map;
public final class EcoCraftEnchantmentManager implements EcoCraftEnchantmentManagerProxy {
@Override
public void registerNewCraftEnchantments() {
Map<org.bukkit.enchantments.Enchantment, VanillaEnchantmentMetadata> 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();
}
}
}

View File

@ -0,0 +1,56 @@
package com.willfp.ecoenchants.proxy.v1_16_R2.vanilla;
import com.willfp.ecoenchants.enchantments.support.vanilla.EcoCraftEnchantmentWrapper;
import com.willfp.ecoenchants.enchantments.support.vanilla.VanillaEnchantmentMetadata;
import net.minecraft.server.v1_16_R2.Enchantment;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.v1_16_R2.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() == null ? this.getHandle().getMaxLevel() : 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<NamespacedKey, org.bukkit.enchantments.Enchantment> byKey = (Map<NamespacedKey, org.bukkit.enchantments.Enchantment>) byIdField.get(null);
Map<String, org.bukkit.enchantments.Enchantment> byName = (Map<String, org.bukkit.enchantments.Enchantment>) byNameField.get(null);
byKey.remove(this.getKey());
byName.remove(this.getName());
Map<String, org.bukkit.enchantments.Enchantment> byNameClone = new HashMap<>(byName);
for (Map.Entry<String, org.bukkit.enchantments.Enchantment> 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) {
}
}
}