#1502: Make Sound an interface

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2024-11-02 11:00:08 +11:00
parent 7b903b41cb
commit 42ebf1afa4
5 changed files with 102 additions and 25 deletions

View File

@@ -1,32 +1,27 @@
package org.bukkit.craftbukkit;
import com.google.common.base.Preconditions;
import java.util.Locale;
import net.minecraft.core.Holder;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.sounds.SoundEffect;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.jetbrains.annotations.NotNull;
public class CraftSound {
public class CraftSound implements Sound, Handleable<SoundEffect> {
private static int count = 0;
public static Sound minecraftToBukkit(SoundEffect minecraft) {
Preconditions.checkArgument(minecraft != null);
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;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.SOUND_EVENT, Registry.SOUNDS);
}
public static SoundEffect bukkitToMinecraft(Sound bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.getMinecraftRegistry(Registries.SOUND_EVENT)
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
return CraftRegistry.bukkitToMinecraft(bukkit);
}
public static Holder<SoundEffect> bukkitToMinecraftHolder(Sound bukkit) {
@@ -41,4 +36,75 @@ public class CraftSound {
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.");
}
private final NamespacedKey key;
private final SoundEffect soundEffect;
private final String name;
private final int ordinal;
public CraftSound(NamespacedKey key, SoundEffect soundEffect) {
this.key = key;
this.soundEffect = soundEffect;
// For backwards compatibility, minecraft values will stile return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive sound specific values.
// Custom sounds will return the key with namespace. For a plugin this should look than like a new sound
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
this.name = key.getKey().toUpperCase(Locale.ROOT).replace('.', '_');
} else {
this.name = key.toString();
}
this.ordinal = count++;
}
@Override
public SoundEffect getHandle() {
return soundEffect;
}
@NotNull
@Override
public NamespacedKey getKey() {
return key;
}
@Override
public int compareTo(@NotNull Sound sound) {
return ordinal - sound.ordinal();
}
@NotNull
@Override
public String name() {
return name;
}
@Override
public int ordinal() {
return ordinal;
}
@Override
public String toString() {
// For backwards compatibility
return name();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftSound otherSound)) {
return false;
}
return getKey().equals(otherSound.getKey());
}
@Override
public int hashCode() {
return getKey().hashCode();
}
}