#1340: Centralize the conversion from and to Minecraft / Bukkit registry items even more and add a test case for them

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2024-01-22 20:36:36 +11:00
parent e6b4a5f109
commit d7095f8578
20 changed files with 623 additions and 110 deletions

View File

@@ -1,31 +1,20 @@
package org.bukkit.craftbukkit;
import com.google.common.base.Preconditions;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import org.bukkit.GameEvent;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.jetbrains.annotations.NotNull;
public class CraftGameEvent extends GameEvent {
public class CraftGameEvent extends GameEvent implements Handleable<net.minecraft.world.level.gameevent.GameEvent> {
public static GameEvent minecraftToBukkit(net.minecraft.world.level.gameevent.GameEvent minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<net.minecraft.world.level.gameevent.GameEvent> registry = CraftRegistry.getMinecraftRegistry(Registries.GAME_EVENT);
GameEvent bukkit = Registry.GAME_EVENT.get(CraftNamespacedKey.fromMinecraft(registry.getKey(minecraft)));
Preconditions.checkArgument(bukkit != null);
return bukkit;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.GAME_EVENT, Registry.GAME_EVENT);
}
public static net.minecraft.world.level.gameevent.GameEvent bukkitToMinecraft(GameEvent bukkit) {
Preconditions.checkArgument(bukkit != null);
return ((CraftGameEvent) bukkit).getHandle();
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
@@ -36,6 +25,7 @@ public class CraftGameEvent extends GameEvent {
this.handle = handle;
}
@Override
public net.minecraft.world.level.gameevent.GameEvent getHandle() {
return handle;
}

View File

@@ -1,32 +1,21 @@
package org.bukkit.craftbukkit;
import com.google.common.base.Preconditions;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.Instrument;
import org.bukkit.MusicInstrument;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.jetbrains.annotations.NotNull;
public class CraftMusicInstrument extends MusicInstrument {
public class CraftMusicInstrument extends MusicInstrument implements Handleable<Instrument> {
public static MusicInstrument minecraftToBukkit(Instrument minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<Instrument> registry = CraftRegistry.getMinecraftRegistry(Registries.INSTRUMENT);
MusicInstrument bukkit = Registry.INSTRUMENT.get(CraftNamespacedKey.fromMinecraft(registry.getKey(minecraft)));
Preconditions.checkArgument(bukkit != null);
return bukkit;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.INSTRUMENT, Registry.INSTRUMENT);
}
public static Instrument bukkitToMinecraft(MusicInstrument bukkit) {
Preconditions.checkArgument(bukkit != null);
return ((CraftMusicInstrument) bukkit).getHandle();
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
@@ -37,6 +26,7 @@ public class CraftMusicInstrument extends MusicInstrument {
this.handle = handle;
}
@Override
public Instrument getHandle() {
return handle;
}

View File

@@ -23,6 +23,7 @@ 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.craftbukkit.util.Handleable;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.generator.structure.Structure;
import org.bukkit.generator.structure.StructureType;
@@ -48,6 +49,47 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
return getMinecraftRegistry().registryOrThrow(key);
}
/**
* Usage note: Only use this method to delegate the conversion methods from the individual Craft classes to here.
* Do not use it in other parts of CraftBukkit, use the methods in the respective Craft classes instead.
*
* @param minecraft the minecraft representation
* @param registryKey the registry key of the minecraft registry to use
* @param bukkitRegistry the bukkit registry to use
* @return the bukkit representation of the minecraft value
*/
public static <B extends Keyed, M> B minecraftToBukkit(M minecraft, ResourceKey<IRegistry<M>> registryKey, Registry<B> bukkitRegistry) {
Preconditions.checkArgument(minecraft != null);
IRegistry<M> registry = CraftRegistry.getMinecraftRegistry(registryKey);
B bukkit = bukkitRegistry.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft)
.orElseThrow(() -> new IllegalStateException(String.format("Cannot convert '%s' to bukkit representation, since it is not registered.", minecraft))).location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
}
/**
* Usage note: Only use this method to delegate the conversion methods from the individual Craft classes to here.
* Do not use it in other parts of CraftBukkit, use the methods in the respective Craft classes instead.
*
* @param bukkit the bukkit representation
* @return the minecraft representation of the bukkit value
*/
public static <B extends Keyed, M> M bukkitToMinecraft(B bukkit) {
Preconditions.checkArgument(bukkit != null);
return ((Handleable<M>) bukkit).getHandle();
}
/**
* Note: Newly added registries should also be added to RegistriesArgumentProvider in the test package
*
* @param bukkitClass the bukkit class of the registry
* @param registryHolder the minecraft registry holder
* @return the bukkit registry of the provided class
*/
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);

View File

@@ -1,7 +1,5 @@
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;
@@ -10,29 +8,20 @@ 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.craftbukkit.util.Handleable;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentTarget;
import org.bukkit.enchantments.EnchantmentWrapper;
import org.bukkit.inventory.ItemStack;
public class CraftEnchantment extends Enchantment {
public class CraftEnchantment extends Enchantment implements Handleable<net.minecraft.world.item.enchantment.Enchantment> {
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;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.ENCHANTMENT, Registry.ENCHANTMENT);
}
public static net.minecraft.world.item.enchantment.Enchantment bukkitToMinecraft(Enchantment bukkit) {
Preconditions.checkArgument(bukkit != null);
return ((CraftEnchantment) bukkit).getHandle();
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
@@ -45,6 +34,7 @@ public class CraftEnchantment extends Enchantment {
this.id = BuiltInRegistries.ENCHANTMENT.getId(handle);
}
@Override
public net.minecraft.world.item.enchantment.Enchantment getHandle() {
return handle;
}

View File

@@ -1,32 +1,21 @@
package org.bukkit.craftbukkit.generator.structure;
import com.google.common.base.Preconditions;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.generator.structure.Structure;
import org.bukkit.generator.structure.StructureType;
public class CraftStructure extends Structure {
public class CraftStructure extends Structure implements Handleable<net.minecraft.world.level.levelgen.structure.Structure> {
public static Structure minecraftToBukkit(net.minecraft.world.level.levelgen.structure.Structure minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<net.minecraft.world.level.levelgen.structure.Structure> registry = CraftRegistry.getMinecraftRegistry(Registries.STRUCTURE);
Structure bukkit = Registry.STRUCTURE.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.STRUCTURE, Registry.STRUCTURE);
}
public static net.minecraft.world.level.levelgen.structure.Structure bukkitToMinecraft(Structure bukkit) {
Preconditions.checkArgument(bukkit != null);
return ((CraftStructure) bukkit).getHandle();
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
@@ -39,6 +28,7 @@ public class CraftStructure extends Structure {
this.structureType = CraftStructureType.minecraftToBukkit(structure.type());
}
@Override
public net.minecraft.world.level.levelgen.structure.Structure getHandle() {
return structure;
}

View File

@@ -1,31 +1,20 @@
package org.bukkit.craftbukkit.generator.structure;
import com.google.common.base.Preconditions;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.generator.structure.StructureType;
public class CraftStructureType extends StructureType {
public class CraftStructureType extends StructureType implements Handleable<net.minecraft.world.level.levelgen.structure.StructureType<?>> {
public static StructureType minecraftToBukkit(net.minecraft.world.level.levelgen.structure.StructureType<?> minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<net.minecraft.world.level.levelgen.structure.StructureType<?>> registry = CraftRegistry.getMinecraftRegistry(Registries.STRUCTURE_TYPE);
StructureType bukkit = Registry.STRUCTURE_TYPE.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.STRUCTURE_TYPE, Registry.STRUCTURE_TYPE);
}
public static net.minecraft.world.level.levelgen.structure.StructureType<?> bukkitToMinecraft(StructureType bukkit) {
Preconditions.checkArgument(bukkit != null);
return ((CraftStructureType) bukkit).getHandle();
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
@@ -36,6 +25,7 @@ public class CraftStructureType extends StructureType {
this.structureType = structureType;
}
@Override
public net.minecraft.world.level.levelgen.structure.StructureType<?> getHandle() {
return structureType;
}

View File

@@ -1,10 +1,22 @@
package org.bukkit.craftbukkit.inventory.trim;
import net.minecraft.core.registries.Registries;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.inventory.meta.trim.TrimMaterial;
import org.jetbrains.annotations.NotNull;
public class CraftTrimMaterial implements TrimMaterial {
public class CraftTrimMaterial implements TrimMaterial, Handleable<net.minecraft.world.item.armortrim.TrimMaterial> {
public static TrimMaterial minecraftToBukkit(net.minecraft.world.item.armortrim.TrimMaterial minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.TRIM_MATERIAL, Registry.TRIM_MATERIAL);
}
public static net.minecraft.world.item.armortrim.TrimMaterial bukkitToMinecraft(TrimMaterial bukkit) {
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
private final net.minecraft.world.item.armortrim.TrimMaterial handle;
@@ -14,13 +26,14 @@ public class CraftTrimMaterial implements TrimMaterial {
this.handle = handle;
}
@Override
public net.minecraft.world.item.armortrim.TrimMaterial getHandle() {
return handle;
}
@Override
@NotNull
public NamespacedKey getKey() {
return key;
}
public net.minecraft.world.item.armortrim.TrimMaterial getHandle() {
return handle;
}
}

View File

@@ -1,10 +1,22 @@
package org.bukkit.craftbukkit.inventory.trim;
import net.minecraft.core.registries.Registries;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.inventory.meta.trim.TrimPattern;
import org.jetbrains.annotations.NotNull;
public class CraftTrimPattern implements TrimPattern {
public class CraftTrimPattern implements TrimPattern, Handleable<net.minecraft.world.item.armortrim.TrimPattern> {
public static TrimPattern minecraftToBukkit(net.minecraft.world.item.armortrim.TrimPattern minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.TRIM_PATTERN, Registry.TRIM_PATTERN);
}
public static net.minecraft.world.item.armortrim.TrimPattern bukkitToMinecraft(TrimPattern bukkit) {
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
private final net.minecraft.world.item.armortrim.TrimPattern handle;
@@ -14,13 +26,14 @@ public class CraftTrimPattern implements TrimPattern {
this.handle = handle;
}
@Override
public net.minecraft.world.item.armortrim.TrimPattern getHandle() {
return handle;
}
@Override
@NotNull
public NamespacedKey getKey() {
return key;
}
public net.minecraft.world.item.armortrim.TrimPattern getHandle() {
return handle;
}
}

View File

@@ -1,19 +1,25 @@
package org.bukkit.craftbukkit.potion;
import com.google.common.base.Preconditions;
import net.minecraft.core.IRegistry;
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.craftbukkit.util.Handleable;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;
public class CraftPotionEffectType extends PotionEffectType {
public class CraftPotionEffectType extends PotionEffectType implements Handleable<MobEffectList> {
public static PotionEffectType minecraftToBukkit(MobEffectList minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.MOB_EFFECT, Registry.EFFECT);
}
public static MobEffectList bukkitToMinecraft(PotionEffectType bukkit) {
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private final NamespacedKey key;
private final MobEffectList handle;
@@ -25,6 +31,7 @@ public class CraftPotionEffectType extends PotionEffectType {
this.id = CraftRegistry.getMinecraftRegistry(Registries.MOB_EFFECT).getId(handle) + 1;
}
@Override
public MobEffectList getHandle() {
return handle;
}
@@ -123,21 +130,4 @@ public class CraftPotionEffectType extends PotionEffectType {
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 = Registry.EFFECT.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
}
public static MobEffectList bukkitToMinecraft(PotionEffectType bukkit) {
Preconditions.checkArgument(bukkit != null);
return ((CraftPotionEffectType) bukkit).getHandle();
}
}

View File

@@ -0,0 +1,6 @@
package org.bukkit.craftbukkit.util;
public interface Handleable<M> {
M getHandle();
}