#1250: Standardize and centralize Bukkit / Minecraft registry conversion

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2023-09-22 03:02:16 +10:00
parent 0b9699cc2e
commit 4248b8a4d8
47 changed files with 468 additions and 252 deletions

View File

@@ -1,8 +1,9 @@
package org.bukkit.craftbukkit;
import com.google.common.base.Preconditions;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.MinecraftKey;
import net.minecraft.core.Holder;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.sounds.SoundEffect;
import org.bukkit.Registry;
import org.bukkit.Sound;
@@ -10,21 +11,34 @@ import org.bukkit.craftbukkit.util.CraftNamespacedKey;
public class CraftSound {
public static SoundEffect getSoundEffect(String s) {
SoundEffect effect = BuiltInRegistries.SOUND_EVENT.get(new MinecraftKey(s));
Preconditions.checkArgument(effect != null, "Sound effect %s does not exist", s);
public static Sound minecraftToBukkit(SoundEffect minecraft) {
Preconditions.checkArgument(minecraft != null);
return effect;
IRegistry<SoundEffect> registry = CraftRegistry.getMinecraftRegistry(Registries.SOUND_EVENT);
Sound bukkit = Registry.SOUNDS.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
}
public static SoundEffect getSoundEffect(Sound s) {
SoundEffect effect = BuiltInRegistries.SOUND_EVENT.get(CraftNamespacedKey.toMinecraft(s.getKey()));
Preconditions.checkArgument(effect != null, "Sound effect %s does not exist", s);
public static SoundEffect bukkitToMinecraft(Sound bukkit) {
Preconditions.checkArgument(bukkit != null);
return effect;
return CraftRegistry.getMinecraftRegistry(Registries.SOUND_EVENT)
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
}
public static Sound getBukkit(SoundEffect soundEffect) {
return Registry.SOUNDS.get(CraftNamespacedKey.fromMinecraft(BuiltInRegistries.SOUND_EVENT.getKey(soundEffect)));
public static Holder<SoundEffect> bukkitToMinecraftHolder(Sound bukkit) {
Preconditions.checkArgument(bukkit != null);
IRegistry<SoundEffect> registry = CraftRegistry.getMinecraftRegistry(Registries.SOUND_EVENT);
if (registry.wrapAsHolder(bukkitToMinecraft(bukkit)) instanceof Holder.c<SoundEffect> holder) {
return holder;
}
throw new IllegalArgumentException("No Reference holder found for " + bukkit
+ ", this can happen if a plugin creates its own sound effect with out properly registering it.");
}
}