Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Instrument Component fields into MusicInstrument #11925

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ServiceLoader;
import java.util.function.Consumer;
import org.bukkit.Art;
import org.bukkit.MusicInstrument;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
Expand All @@ -19,4 +20,6 @@ class Holder {
}

Art createPaintingVariant(Consumer<RegistryBuilderFactory<Art, ? extends PaintingVariantRegistryEntry.Builder>> value);

MusicInstrument createInstrument(Consumer<RegistryBuilderFactory<MusicInstrument, ? extends InstrumentRegistryEntry.Builder>> value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.papermc.paper.registry.data;

import io.papermc.paper.registry.RegistryBuilder;
import io.papermc.paper.registry.TypedKey;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import org.bukkit.MusicInstrument;
import org.checkerframework.checker.index.qual.NonNegative;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;

/**
* A data-centric version-specific registry entry for the {@link org.bukkit.MusicInstrument} type.
*/
@ApiStatus.Experimental
@ApiStatus.NonExtendable
public interface InstrumentRegistryEntry {

@Contract(pure = true)
@NonNegative
Doc94 marked this conversation as resolved.
Show resolved Hide resolved
float duration();

@Contract(pure = true)
@NonNegative
float range();

Component description();

@Contract(pure = true)
Either<TypedKey<Sound>, SoundEventRegistryEntry> soundEvent();

/**
* A mutable builder for the {@link InstrumentRegistryEntry} plugins may change in applicable registry events.
* <p>
* The following values are required for each builder:
* <ul>
* <li>{@link #duration(float)}</li>
* <li>{@link #range(float)}</li>
* <li>{@link #description(Component)}</li>
* <li>
* {@link #soundEvent(TypedKey)} or {@link #soundEvent(Consumer)}
* </li>
* </ul>
*/
@ApiStatus.Experimental
@ApiStatus.NonExtendable
interface Builder extends InstrumentRegistryEntry, RegistryBuilder<MusicInstrument> {

/**
* @param duration
* @return this builder instance.
* @see InstrumentRegistryEntry#duration()
* @see MusicInstrument#getDuration()
*/
@Contract(value = "_ -> this", mutates = "this")
Builder duration(@NonNegative float duration);

/**
* @param range
* @return this builder instance.
* @see InstrumentRegistryEntry#range()
* @see MusicInstrument#getRange() ()
*/
@Contract(value = "_ -> this", mutates = "this")
Builder range(@NonNegative float range);

/**
* @param description
* @return this builder instance.
* @see InstrumentRegistryEntry#description()
* @see MusicInstrument#description()
*/
@Contract(value = "_ -> this", mutates = "this")
Builder description(Component description);

/**
* @param soundEvent
* @return this builder instance.
* @see InstrumentRegistryEntry#soundEvent()
* @see MusicInstrument#getSoundEvent()
*/
@Contract(value = "_ -> this", mutates = "this")
Builder soundEvent(TypedKey<Sound> soundEvent);

@Contract(value = "_ -> this", mutates = "this")
Builder soundEvent(Consumer<RegistryBuilderFactory<Sound, ? extends SoundEventRegistryEntry.Builder>> soundEvent);
}

}
33 changes: 27 additions & 6 deletions paper-api/src/main/java/org/bukkit/MusicInstrument.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Consumer;
import io.papermc.paper.registry.RegistryBuilderFactory;
import io.papermc.paper.registry.data.InlinedRegistryBuilderProvider;
import io.papermc.paper.registry.data.InstrumentRegistryEntry;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import net.kyori.adventure.translation.Translatable;

public abstract class MusicInstrument implements Keyed, net.kyori.adventure.translation.Translatable { // Paper - translation keys
public abstract class MusicInstrument implements Keyed, Translatable {

public static final MusicInstrument PONDER_GOAT_HORN = getInstrument("ponder_goat_horn");
public static final MusicInstrument SING_GOAT_HORN = getInstrument("sing_goat_horn");
Expand All @@ -17,6 +24,17 @@ public abstract class MusicInstrument implements Keyed, net.kyori.adventure.tran
public static final MusicInstrument YEARN_GOAT_HORN = getInstrument("yearn_goat_horn");
public static final MusicInstrument DREAM_GOAT_HORN = getInstrument("dream_goat_horn");

/**
* Create an inlined music instrument.
*
* @param value a consumer for the builder factory
* @return the created instrument
*/
@ApiStatus.Experimental
static @NotNull MusicInstrument create(final @NotNull Consumer<RegistryBuilderFactory<MusicInstrument, ? extends InstrumentRegistryEntry.Builder>> value) {
return InlinedRegistryBuilderProvider.instance().createInstrument(value);
}

/**
* Returns a {@link MusicInstrument} by a {@link NamespacedKey}.
*
Expand Down Expand Up @@ -47,7 +65,14 @@ private static MusicInstrument getInstrument(@NotNull String key) {
return Registry.INSTRUMENT.getOrThrow(NamespacedKey.minecraft(key));
}

// Paper start - deprecate getKey
public abstract float getDuration();

public abstract float getRange();

public abstract Component description();

public abstract Sound getSoundEvent();

/**
* @deprecated use {@link Registry#getKey(Keyed)}, {@link io.papermc.paper.registry.RegistryAccess#getRegistry(io.papermc.paper.registry.RegistryKey)},
* and {@link io.papermc.paper.registry.RegistryKey#INSTRUMENT}. MusicInstruments can exist without a key.
Expand All @@ -66,15 +91,11 @@ private static MusicInstrument getInstrument(@NotNull String key) {
return Keyed.super.key();
}

// Paper end - deprecate getKey

// Paper start - mark translation key as deprecated
/**
* @deprecated this method assumes that the instrument description
* always be a translatable component which is not guaranteed.
*/
@Override
@Deprecated(forRemoval = true)
public abstract @NotNull String translationKey();
// Paper end - mark translation key as deprecated
}
Original file line number Diff line number Diff line change
@@ -1,71 +1,78 @@
package org.bukkit.craftbukkit;

import com.google.common.base.Preconditions;
import io.papermc.paper.adventure.PaperAdventure;
import io.papermc.paper.util.Holderable;
import net.kyori.adventure.text.Component;
import net.minecraft.core.Holder;
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.Handleable;
import org.bukkit.Sound;
import org.jetbrains.annotations.NotNull;

public class CraftMusicInstrument extends MusicInstrument implements io.papermc.paper.util.Holderable<Instrument> {
public class CraftMusicInstrument extends MusicInstrument implements Holderable<Instrument> {

public static MusicInstrument minecraftToBukkit(Instrument minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.INSTRUMENT, Registry.INSTRUMENT);
}

public static MusicInstrument minecraftHolderToBukkit(Holder<Instrument> minecraft) {
return CraftRegistry.minecraftHolderToBukkit(minecraft, Registry.INSTRUMENT); // Paper - switch to Holder
return CraftRegistry.minecraftHolderToBukkit(minecraft, Registry.INSTRUMENT);
}

public static Instrument bukkitToMinecraft(MusicInstrument bukkit) {
return CraftRegistry.bukkitToMinecraft(bukkit);
}

public static Holder<Instrument> bukkitToMinecraftHolder(MusicInstrument bukkit) {
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.INSTRUMENT); // Paper - switch to Holder
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.INSTRUMENT);
}

public static Object bukkitToString(MusicInstrument bukkit) { // Paper - switch to Holder
public static Object bukkitToString(MusicInstrument bukkit) {
Preconditions.checkArgument(bukkit != null);

return ((CraftMusicInstrument) bukkit).toBukkitSerializationObject(Instrument.CODEC); // Paper - switch to Holder
return ((CraftMusicInstrument) bukkit).toBukkitSerializationObject(Instrument.CODEC);
}

public static MusicInstrument stringToBukkit(Object string) { // Paper - switch to Holder
public static MusicInstrument stringToBukkit(Object string) {
Preconditions.checkArgument(string != null);

return io.papermc.paper.util.Holderable.fromBukkitSerializationObject(string, Instrument.CODEC, Registry.INSTRUMENT); // Paper - switch to Holder
return io.papermc.paper.util.Holderable.fromBukkitSerializationObject(string, Instrument.CODEC, Registry.INSTRUMENT);
}

private final Holder<Instrument> holder;

public CraftMusicInstrument(Holder<Instrument> holder) {
this.holder = holder;
}

// Paper start - switch to Holder
@Override
public boolean equals(final Object o) {
return this.implEquals(o);
public Holder<Instrument> getHolder() {
return this.holder;
}

@Override
public int hashCode() {
return this.implHashCode();
public float getDuration() {
return this.getHandle().useDuration();
}

@Override
public String toString() {
return this.implToString();
public float getRange() {
return this.getHandle().range();
}

private final Holder<Instrument> holder;
public CraftMusicInstrument(Holder<Instrument> holder) {
this.holder = holder;
// Paper end - switch to Holder
@Override
public Sound getSoundEvent() {
return CraftSound.minecraftHolderToBukkit(this.getHandle().soundEvent());
}

@NotNull
@Override
public Holder<Instrument> getHolder() { // Paper - switch to Holder
return this.holder; // Paper - switch to Holder
public Component description() {
return PaperAdventure.asAdventure(this.getHandle().description());
}

@NotNull
Expand All @@ -74,15 +81,26 @@ public NamespacedKey getKey() {
return Holderable.super.getKey();
}

// Paper start - add translationKey methods
@Override
public @NotNull String translationKey() {
if (!(this.getHandle().description().getContents() instanceof final net.minecraft.network.chat.contents.TranslatableContents translatableContents)) {
throw new UnsupportedOperationException("Description isn't translatable!"); // Paper
throw new UnsupportedOperationException("Description isn't translatable!");
}
return translatableContents.getKey();
}
// Paper end - add translationKey methods

// Paper - switch to Holder
@Override
public boolean equals(final Object o) {
return this.implEquals(o);
}

@Override
public int hashCode() {
return this.implHashCode();
}

@Override
public String toString() {
return this.implToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public static Sound minecraftToBukkit(SoundEvent minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.SOUND_EVENT, Registry.SOUNDS);
}

public static Sound minecraftHolderToBukkit(Holder<SoundEvent> minecraft) {
return minecraftToBukkit(minecraft.value());
}

public static SoundEvent bukkitToMinecraft(Sound bukkit) {
return CraftRegistry.bukkitToMinecraft(bukkit);
}
Expand All @@ -27,9 +31,7 @@ public CraftSound(Holder<SoundEvent> soundEffect) {
super(soundEffect, count++);
}

// Paper start
public static String getSound(Sound sound) {
return sound.getKey().getKey();
}
// Paper end
}
Loading