13
0
geforkt von Mirrors/Paper

SPIGOT-6026: Pull PotionEffectType and Enchantment from registry

By: DerFrZocker <derrieple@gmail.com>
Dieser Commit ist enthalten in:
CraftBukkit/Spigot 2023-12-06 03:40:00 +11:00
Ursprung 8398e12b34
Commit dd8ca5c2dc
9 geänderte Dateien mit 226 neuen und 265 gelöschten Zeilen

Datei anzeigen

@ -1,14 +0,0 @@
--- a/net/minecraft/world/effect/MobEffects.java
+++ b/net/minecraft/world/effect/MobEffects.java
@@ -47,6 +47,10 @@
public MobEffects() {}
private static MobEffectList register(String s, MobEffectList mobeffectlist) {
- return (MobEffectList) IRegistry.register(BuiltInRegistries.MOB_EFFECT, s, mobeffectlist);
+ // CraftBukkit start
+ mobeffectlist = (MobEffectList) IRegistry.register(BuiltInRegistries.MOB_EFFECT, s, mobeffectlist);
+ org.bukkit.potion.PotionEffectType.registerPotionEffectType(new org.bukkit.craftbukkit.potion.CraftPotionEffectType(mobeffectlist));
+ return mobeffectlist;
+ // CraftBukkit end
}
}

Datei anzeigen

@ -1,14 +0,0 @@
--- a/net/minecraft/world/item/enchantment/Enchantments.java
+++ b/net/minecraft/world/item/enchantment/Enchantments.java
@@ -50,6 +50,10 @@
public Enchantments() {}
private static Enchantment register(String s, Enchantment enchantment) {
- return (Enchantment) IRegistry.register(BuiltInRegistries.ENCHANTMENT, s, enchantment);
+ // CraftBukkit start
+ enchantment = (Enchantment) IRegistry.register(BuiltInRegistries.ENCHANTMENT, s, enchantment);
+ org.bukkit.enchantments.Enchantment.registerEnchantment(new org.bukkit.craftbukkit.enchantments.CraftEnchantment(enchantment));
+ return enchantment;
+ // CraftBukkit end
}
}

Datei anzeigen

@ -16,15 +16,19 @@ import org.bukkit.Keyed;
import org.bukkit.MusicInstrument;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.enchantments.CraftEnchantment;
import org.bukkit.craftbukkit.generator.structure.CraftStructure;
import org.bukkit.craftbukkit.generator.structure.CraftStructureType;
import org.bukkit.craftbukkit.inventory.trim.CraftTrimMaterial;
import org.bukkit.craftbukkit.inventory.trim.CraftTrimPattern;
import org.bukkit.craftbukkit.potion.CraftPotionEffectType;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.generator.structure.Structure;
import org.bukkit.generator.structure.StructureType;
import org.bukkit.inventory.meta.trim.TrimMaterial;
import org.bukkit.inventory.meta.trim.TrimPattern;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;
public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
@ -45,12 +49,18 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
}
public static <B extends Keyed> Registry<?> createRegistry(Class<B> bukkitClass, IRegistryCustom registryHolder) {
if (bukkitClass == Enchantment.class) {
return new CraftRegistry<>(Enchantment.class, registryHolder.registryOrThrow(Registries.ENCHANTMENT), CraftEnchantment::new);
}
if (bukkitClass == GameEvent.class) {
return new CraftRegistry<>(GameEvent.class, registryHolder.registryOrThrow(Registries.GAME_EVENT), CraftGameEvent::new);
}
if (bukkitClass == MusicInstrument.class) {
return new CraftRegistry<>(MusicInstrument.class, registryHolder.registryOrThrow(Registries.INSTRUMENT), CraftMusicInstrument::new);
}
if (bukkitClass == PotionEffectType.class) {
return new CraftRegistry<>(PotionEffectType.class, registryHolder.registryOrThrow(Registries.MOB_EFFECT), CraftPotionEffectType::new);
}
if (bukkitClass == Structure.class) {
return new CraftRegistry<>(Structure.class, registryHolder.registryOrThrow(Registries.STRUCTURE), CraftStructure::new);
}

Datei anzeigen

@ -329,13 +329,7 @@ public final class CraftServer implements Server {
CraftRegistry.setMinecraftRegistry(console.registryAccess());
// Register all the Enchantments and PotionTypes now so we can stop new registration immediately after
Enchantments.SHARPNESS.getClass();
org.bukkit.enchantments.Enchantment.stopAcceptingRegistrations();
Potion.setPotionBrewer(new CraftPotionBrewer());
MobEffects.BLINDNESS.getClass();
PotionEffectType.stopAcceptingRegistrations();
// Ugly hack :(
if (!Main.useConsole) {

Datei anzeigen

@ -1,9 +1,14 @@
package org.bukkit.craftbukkit.enchantments;
import com.google.common.base.Preconditions;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.enchantment.EnchantmentBinding;
import net.minecraft.world.item.enchantment.EnchantmentVanishing;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
@ -12,171 +17,133 @@ import org.bukkit.enchantments.EnchantmentWrapper;
import org.bukkit.inventory.ItemStack;
public class CraftEnchantment extends Enchantment {
private final net.minecraft.world.item.enchantment.Enchantment target;
public CraftEnchantment(net.minecraft.world.item.enchantment.Enchantment target) {
super(CraftNamespacedKey.fromMinecraft(BuiltInRegistries.ENCHANTMENT.getKey(target)));
this.target = target;
public static Enchantment minecraftToBukkit(net.minecraft.world.item.enchantment.Enchantment minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<net.minecraft.world.item.enchantment.Enchantment> registry = CraftRegistry.getMinecraftRegistry(Registries.ENCHANTMENT);
Enchantment bukkit = Registry.ENCHANTMENT.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
}
public static net.minecraft.world.item.enchantment.Enchantment bukkitToMinecraft(Enchantment bukkit) {
Preconditions.checkArgument(bukkit != null);
return ((CraftEnchantment) bukkit).getHandle();
}
private final NamespacedKey key;
private final net.minecraft.world.item.enchantment.Enchantment handle;
private final int id;
public CraftEnchantment(NamespacedKey key, net.minecraft.world.item.enchantment.Enchantment handle) {
this.key = key;
this.handle = handle;
this.id = BuiltInRegistries.ENCHANTMENT.getId(handle);
}
public net.minecraft.world.item.enchantment.Enchantment getHandle() {
return handle;
}
@Override
public NamespacedKey getKey() {
return key;
}
@Override
public int getMaxLevel() {
return target.getMaxLevel();
return handle.getMaxLevel();
}
@Override
public int getStartLevel() {
return target.getMinLevel();
return handle.getMinLevel();
}
@Override
public EnchantmentTarget getItemTarget() {
switch (target.category) {
case ARMOR:
return EnchantmentTarget.ARMOR;
case ARMOR_FEET:
return EnchantmentTarget.ARMOR_FEET;
case ARMOR_HEAD:
return EnchantmentTarget.ARMOR_HEAD;
case ARMOR_LEGS:
return EnchantmentTarget.ARMOR_LEGS;
case ARMOR_CHEST:
return EnchantmentTarget.ARMOR_TORSO;
case DIGGER:
return EnchantmentTarget.TOOL;
case WEAPON:
return EnchantmentTarget.WEAPON;
case BOW:
return EnchantmentTarget.BOW;
case FISHING_ROD:
return EnchantmentTarget.FISHING_ROD;
case BREAKABLE:
return EnchantmentTarget.BREAKABLE;
case WEARABLE:
return EnchantmentTarget.WEARABLE;
case TRIDENT:
return EnchantmentTarget.TRIDENT;
case CROSSBOW:
return EnchantmentTarget.CROSSBOW;
case VANISHABLE:
return EnchantmentTarget.VANISHABLE;
default:
return null;
}
return switch (handle.category) {
case ARMOR -> EnchantmentTarget.ARMOR;
case ARMOR_FEET -> EnchantmentTarget.ARMOR_FEET;
case ARMOR_HEAD -> EnchantmentTarget.ARMOR_HEAD;
case ARMOR_LEGS -> EnchantmentTarget.ARMOR_LEGS;
case ARMOR_CHEST -> EnchantmentTarget.ARMOR_TORSO;
case DIGGER -> EnchantmentTarget.TOOL;
case WEAPON -> EnchantmentTarget.WEAPON;
case BOW -> EnchantmentTarget.BOW;
case FISHING_ROD -> EnchantmentTarget.FISHING_ROD;
case BREAKABLE -> EnchantmentTarget.BREAKABLE;
case WEARABLE -> EnchantmentTarget.WEARABLE;
case TRIDENT -> EnchantmentTarget.TRIDENT;
case CROSSBOW -> EnchantmentTarget.CROSSBOW;
case VANISHABLE -> EnchantmentTarget.VANISHABLE;
};
}
@Override
public boolean isTreasure() {
return target.isTreasureOnly();
return handle.isTreasureOnly();
}
@Override
public boolean isCursed() {
return target instanceof EnchantmentBinding || target instanceof EnchantmentVanishing;
return handle instanceof EnchantmentBinding || handle instanceof EnchantmentVanishing;
}
@Override
public boolean canEnchantItem(ItemStack item) {
return target.canEnchant(CraftItemStack.asNMSCopy(item));
return handle.canEnchant(CraftItemStack.asNMSCopy(item));
}
@Override
public String getName() {
// PAIL: migration paths
switch (BuiltInRegistries.ENCHANTMENT.getId(target)) {
case 0:
return "PROTECTION_ENVIRONMENTAL";
case 1:
return "PROTECTION_FIRE";
case 2:
return "PROTECTION_FALL";
case 3:
return "PROTECTION_EXPLOSIONS";
case 4:
return "PROTECTION_PROJECTILE";
case 5:
return "OXYGEN";
case 6:
return "WATER_WORKER";
case 7:
return "THORNS";
case 8:
return "DEPTH_STRIDER";
case 9:
return "FROST_WALKER";
case 10:
return "BINDING_CURSE";
case 11:
return "SOUL_SPEED";
case 12:
return "SWIFT_SNEAK";
case 13:
return "DAMAGE_ALL";
case 14:
return "DAMAGE_UNDEAD";
case 15:
return "DAMAGE_ARTHROPODS";
case 16:
return "KNOCKBACK";
case 17:
return "FIRE_ASPECT";
case 18:
return "LOOT_BONUS_MOBS";
case 19:
return "SWEEPING_EDGE";
case 20:
return "DIG_SPEED";
case 21:
return "SILK_TOUCH";
case 22:
return "DURABILITY";
case 23:
return "LOOT_BONUS_BLOCKS";
case 24:
return "ARROW_DAMAGE";
case 25:
return "ARROW_KNOCKBACK";
case 26:
return "ARROW_FIRE";
case 27:
return "ARROW_INFINITE";
case 28:
return "LUCK";
case 29:
return "LURE";
case 30:
return "LOYALTY";
case 31:
return "IMPALING";
case 32:
return "RIPTIDE";
case 33:
return "CHANNELING";
case 34:
return "MULTISHOT";
case 35:
return "QUICK_CHARGE";
case 36:
return "PIERCING";
case 37:
return "MENDING";
case 38:
return "VANISHING_CURSE";
default:
return "UNKNOWN_ENCHANT_" + BuiltInRegistries.ENCHANTMENT.getId(target);
}
}
public static net.minecraft.world.item.enchantment.Enchantment getRaw(Enchantment enchantment) {
if (enchantment instanceof EnchantmentWrapper) {
enchantment = ((EnchantmentWrapper) enchantment).getEnchantment();
}
if (enchantment instanceof CraftEnchantment) {
return ((CraftEnchantment) enchantment).target;
}
return null;
return switch (id) {
case 0 -> "PROTECTION_ENVIRONMENTAL";
case 1 -> "PROTECTION_FIRE";
case 2 -> "PROTECTION_FALL";
case 3 -> "PROTECTION_EXPLOSIONS";
case 4 -> "PROTECTION_PROJECTILE";
case 5 -> "OXYGEN";
case 6 -> "WATER_WORKER";
case 7 -> "THORNS";
case 8 -> "DEPTH_STRIDER";
case 9 -> "FROST_WALKER";
case 10 -> "BINDING_CURSE";
case 11 -> "SOUL_SPEED";
case 12 -> "SWIFT_SNEAK";
case 13 -> "DAMAGE_ALL";
case 14 -> "DAMAGE_UNDEAD";
case 15 -> "DAMAGE_ARTHROPODS";
case 16 -> "KNOCKBACK";
case 17 -> "FIRE_ASPECT";
case 18 -> "LOOT_BONUS_MOBS";
case 19 -> "SWEEPING_EDGE";
case 20 -> "DIG_SPEED";
case 21 -> "SILK_TOUCH";
case 22 -> "DURABILITY";
case 23 -> "LOOT_BONUS_BLOCKS";
case 24 -> "ARROW_DAMAGE";
case 25 -> "ARROW_KNOCKBACK";
case 26 -> "ARROW_FIRE";
case 27 -> "ARROW_INFINITE";
case 28 -> "LUCK";
case 29 -> "LURE";
case 30 -> "LOYALTY";
case 31 -> "IMPALING";
case 32 -> "RIPTIDE";
case 33 -> "CHANNELING";
case 34 -> "MULTISHOT";
case 35 -> "QUICK_CHARGE";
case 36 -> "PIERCING";
case 37 -> "MENDING";
case 38 -> "VANISHING_CURSE";
default -> getKey().toString();
};
}
@Override
@ -188,10 +155,29 @@ public class CraftEnchantment extends Enchantment {
return false;
}
CraftEnchantment ench = (CraftEnchantment) other;
return !target.isCompatibleWith(ench.target);
return !handle.isCompatibleWith(ench.getHandle());
}
public net.minecraft.world.item.enchantment.Enchantment getHandle() {
return target;
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftEnchantment)) {
return false;
}
return getKey().equals(((Enchantment) other).getKey());
}
@Override
public int hashCode() {
return getKey().hashCode();
}
@Override
public String toString() {
return "CraftEnchantment[" + getKey() + "]";
}
}

Datei anzeigen

@ -225,7 +225,7 @@ public final class CraftItemStack extends ItemStack {
if (handle == null) {
return 0;
}
return EnchantmentManager.getItemEnchantmentLevel(CraftEnchantment.getRaw(ench), handle);
return EnchantmentManager.getItemEnchantmentLevel(CraftEnchantment.bukkitToMinecraft(ench), handle);
}
@Override

Datei anzeigen

@ -2,20 +2,37 @@ package org.bukkit.craftbukkit.potion;
import com.google.common.base.Preconditions;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.effect.MobEffectList;
import org.bukkit.Color;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;
public class CraftPotionEffectType extends PotionEffectType {
private final MobEffectList handle;
public CraftPotionEffectType(MobEffectList handle) {
super(BuiltInRegistries.MOB_EFFECT.getId(handle) + 1, CraftNamespacedKey.fromMinecraft(BuiltInRegistries.MOB_EFFECT.getKey(handle)));
private final NamespacedKey key;
private final MobEffectList handle;
private final int id;
public CraftPotionEffectType(NamespacedKey key, MobEffectList handle) {
this.key = key;
this.handle = handle;
this.id = CraftRegistry.getMinecraftRegistry(Registries.MOB_EFFECT).getId(handle) + 1;
}
public MobEffectList getHandle() {
return handle;
}
@NotNull
@Override
public NamespacedKey getKey() {
return key;
}
@Override
@ -23,82 +40,55 @@ public class CraftPotionEffectType extends PotionEffectType {
return 1.0D;
}
public MobEffectList getHandle() {
return handle;
@Override
public int getId() {
return id;
}
@Override
public String getName() {
switch (getId()) {
case 1:
return "SPEED";
case 2:
return "SLOW";
case 3:
return "FAST_DIGGING";
case 4:
return "SLOW_DIGGING";
case 5:
return "INCREASE_DAMAGE";
case 6:
return "HEAL";
case 7:
return "HARM";
case 8:
return "JUMP";
case 9:
return "CONFUSION";
case 10:
return "REGENERATION";
case 11:
return "DAMAGE_RESISTANCE";
case 12:
return "FIRE_RESISTANCE";
case 13:
return "WATER_BREATHING";
case 14:
return "INVISIBILITY";
case 15:
return "BLINDNESS";
case 16:
return "NIGHT_VISION";
case 17:
return "HUNGER";
case 18:
return "WEAKNESS";
case 19:
return "POISON";
case 20:
return "WITHER";
case 21:
return "HEALTH_BOOST";
case 22:
return "ABSORPTION";
case 23:
return "SATURATION";
case 24:
return "GLOWING";
case 25:
return "LEVITATION";
case 26:
return "LUCK";
case 27:
return "UNLUCK";
case 28:
return "SLOW_FALLING";
case 29:
return "CONDUIT_POWER";
case 30:
return "DOLPHINS_GRACE";
case 31:
return "BAD_OMEN";
case 32:
return "HERO_OF_THE_VILLAGE";
case 33:
return "DARKNESS";
default:
return "UNKNOWN_EFFECT_TYPE_" + getId();
return switch (getId()) {
case 1 -> "SPEED";
case 2 -> "SLOW";
case 3 -> "FAST_DIGGING";
case 4 -> "SLOW_DIGGING";
case 5 -> "INCREASE_DAMAGE";
case 6 -> "HEAL";
case 7 -> "HARM";
case 8 -> "JUMP";
case 9 -> "CONFUSION";
case 10 -> "REGENERATION";
case 11 -> "DAMAGE_RESISTANCE";
case 12 -> "FIRE_RESISTANCE";
case 13 -> "WATER_BREATHING";
case 14 -> "INVISIBILITY";
case 15 -> "BLINDNESS";
case 16 -> "NIGHT_VISION";
case 17 -> "HUNGER";
case 18 -> "WEAKNESS";
case 19 -> "POISON";
case 20 -> "WITHER";
case 21 -> "HEALTH_BOOST";
case 22 -> "ABSORPTION";
case 23 -> "SATURATION";
case 24 -> "GLOWING";
case 25 -> "LEVITATION";
case 26 -> "LUCK";
case 27 -> "UNLUCK";
case 28 -> "SLOW_FALLING";
case 29 -> "CONDUIT_POWER";
case 30 -> "DOLPHINS_GRACE";
case 31 -> "BAD_OMEN";
case 32 -> "HERO_OF_THE_VILLAGE";
case 33 -> "DARKNESS";
default -> getKey().toString();
};
}
@NotNull
@Override
public PotionEffect createEffect(int duration, int amplifier) {
return new PotionEffect(this, isInstant() ? 1 : (int) (duration * getDurationModifier()), amplifier);
}
@Override
@ -111,11 +101,34 @@ public class CraftPotionEffectType extends PotionEffectType {
return Color.fromRGB(handle.getColor());
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof PotionEffectType)) {
return false;
}
return getKey().equals(((PotionEffectType) other).getKey());
}
@Override
public int hashCode() {
return getKey().hashCode();
}
@Override
public String toString() {
return "CraftPotionEffectType[" + getKey() + "]";
}
public static PotionEffectType minecraftToBukkit(MobEffectList minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<MobEffectList> registry = CraftRegistry.getMinecraftRegistry(Registries.MOB_EFFECT);
PotionEffectType bukkit = PotionEffectType.getByKey(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
PotionEffectType bukkit = Registry.EFFECT.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
@ -125,7 +138,6 @@ public class CraftPotionEffectType extends PotionEffectType {
public static MobEffectList bukkitToMinecraft(PotionEffectType bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.getMinecraftRegistry(Registries.MOB_EFFECT)
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
return ((CraftPotionEffectType) bukkit).getHandle();
}
}

Datei anzeigen

@ -62,7 +62,6 @@ public abstract class AbstractTestingBase {
BIOMES = REGISTRY_CUSTOM.registryOrThrow(Registries.BIOME);
DummyServer.setup();
DummyEnchantments.setup();
CraftRegistry.setMinecraftRegistry(REGISTRY_CUSTOM);

Datei anzeigen

@ -1,12 +0,0 @@
package org.bukkit.support;
import net.minecraft.world.item.enchantment.Enchantments;
public class DummyEnchantments {
static {
Enchantments.SHARPNESS.getClass();
org.bukkit.enchantments.Enchantment.stopAcceptingRegistrations();
}
public static void setup() {}
}