Skip to content

Commit b9d6ba2

Browse files
authored
Expose more data for MusicInstrument (#12415)
1 parent 84ee424 commit b9d6ba2

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

paper-api/src/main/java/org/bukkit/MusicInstrument.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import io.papermc.paper.registry.RegistryKey;
66
import java.util.Collection;
77
import java.util.Collections;
8-
import org.jetbrains.annotations.NotNull;
9-
import org.jetbrains.annotations.Nullable;
8+
import net.kyori.adventure.text.Component;
9+
import org.jspecify.annotations.NullMarked;
10+
import org.jspecify.annotations.Nullable;
1011

11-
public abstract class MusicInstrument implements Keyed, net.kyori.adventure.translation.Translatable { // Paper - translation keys
12+
@NullMarked
13+
public abstract class MusicInstrument implements Keyed, net.kyori.adventure.translation.Translatable {
1214

1315
// Start generate - MusicInstrument
1416
// @GeneratedFrom 1.21.5
@@ -38,7 +40,7 @@ public abstract class MusicInstrument implements Keyed, net.kyori.adventure.tran
3840
*/
3941
@Nullable
4042
@Deprecated(since = "1.20.1")
41-
public static MusicInstrument getByKey(@NotNull NamespacedKey namespacedKey) {
43+
public static MusicInstrument getByKey(final NamespacedKey namespacedKey) {
4244
return Registry.INSTRUMENT.get(namespacedKey);
4345
}
4446

@@ -48,25 +50,50 @@ public static MusicInstrument getByKey(@NotNull NamespacedKey namespacedKey) {
4850
* @return the memoryKeys
4951
* @deprecated use {@link Registry#iterator()}.
5052
*/
51-
@NotNull
5253
@Deprecated(since = "1.20.1")
5354
public static Collection<MusicInstrument> values() {
5455
return Collections.unmodifiableCollection(Lists.newArrayList(Registry.INSTRUMENT));
5556
}
5657

57-
@NotNull
58-
private static MusicInstrument getInstrument(@NotNull String key) {
58+
private static MusicInstrument getInstrument(final String key) {
5959
return RegistryAccess.registryAccess().getRegistry(RegistryKey.INSTRUMENT).getOrThrow(NamespacedKey.minecraft(key));
6060
}
6161

62-
// Paper start - deprecate getKey
62+
/**
63+
* Gets the use duration of this music instrument.
64+
*
65+
* @return the duration expressed in seconds.
66+
*/
67+
public abstract float getDuration();
68+
69+
/**
70+
* Gets the range of the sound.
71+
*
72+
* @return the range of the sound.
73+
*/
74+
public abstract float getRange();
75+
76+
/**
77+
* Provides the description of this instrument as displayed to the client.
78+
*
79+
* @return the description component.
80+
*/
81+
public abstract Component description();
82+
83+
/**
84+
* Gets the sound for this instrument.
85+
*
86+
* @return the sound
87+
*/
88+
public abstract Sound getSound();
89+
6390
/**
6491
* @deprecated use {@link Registry#getKey(Keyed)}, {@link io.papermc.paper.registry.RegistryAccess#getRegistry(io.papermc.paper.registry.RegistryKey)},
6592
* and {@link io.papermc.paper.registry.RegistryKey#INSTRUMENT}. MusicInstruments can exist without a key.
6693
*/
6794
@Deprecated(forRemoval = true, since = "1.20.5")
6895
@Override
69-
public abstract @NotNull NamespacedKey getKey();
96+
public abstract NamespacedKey getKey();
7097

7198
/**
7299
* @deprecated use {@link Registry#getKey(Keyed)}, {@link io.papermc.paper.registry.RegistryAccess#getRegistry(io.papermc.paper.registry.RegistryKey)},
@@ -78,15 +105,11 @@ private static MusicInstrument getInstrument(@NotNull String key) {
78105
return Keyed.super.key();
79106
}
80107

81-
// Paper end - deprecate getKey
82-
83-
// Paper start - mark translation key as deprecated
84108
/**
85109
* @deprecated this method assumes that the instrument description
86110
* always be a translatable component which is not guaranteed.
87111
*/
88112
@Override
89113
@Deprecated(forRemoval = true)
90-
public abstract @NotNull String translationKey();
91-
// Paper end - mark translation key as deprecated
114+
public abstract String translationKey();
92115
}

paper-server/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package org.bukkit.craftbukkit;
22

33
import com.google.common.base.Preconditions;
4+
import io.papermc.paper.adventure.PaperAdventure;
45
import io.papermc.paper.registry.RegistryKey;
56
import io.papermc.paper.util.Holderable;
7+
import net.kyori.adventure.text.Component;
68
import net.minecraft.core.Holder;
79
import net.minecraft.core.registries.Registries;
810
import net.minecraft.world.item.Instrument;
911
import org.bukkit.MusicInstrument;
1012
import org.bukkit.NamespacedKey;
11-
import org.bukkit.Registry;
13+
import org.bukkit.Sound;
1214
import org.jetbrains.annotations.NotNull;
1315

1416
public class CraftMusicInstrument extends MusicInstrument implements io.papermc.paper.util.Holderable<Instrument> {
@@ -26,19 +28,19 @@ public static Instrument bukkitToMinecraft(MusicInstrument bukkit) {
2628
}
2729

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

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

35-
return ((CraftMusicInstrument) bukkit).toBukkitSerializationObject(Instrument.DIRECT_CODEC); // Paper - switch to Holder
37+
return ((CraftMusicInstrument) bukkit).toBukkitSerializationObject(Instrument.DIRECT_CODEC);
3638
}
3739

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

41-
return io.papermc.paper.util.Holderable.fromBukkitSerializationObject(string, Instrument.CODEC, RegistryKey.INSTRUMENT); // Paper - switch to Holder
43+
return io.papermc.paper.util.Holderable.fromBukkitSerializationObject(string, Instrument.CODEC, RegistryKey.INSTRUMENT);
4244
}
4345

4446
@Override
@@ -63,8 +65,28 @@ public CraftMusicInstrument(Holder<Instrument> holder) {
6365
}
6466

6567
@Override
66-
public Holder<Instrument> getHolder() { // Paper - switch to Holder
67-
return this.holder; // Paper - switch to Holder
68+
public Holder<Instrument> getHolder() {
69+
return this.holder;
70+
}
71+
72+
@Override
73+
public float getDuration() {
74+
return this.getHandle().useDuration();
75+
}
76+
77+
@Override
78+
public float getRange() {
79+
return this.getHandle().range();
80+
}
81+
82+
@Override
83+
public Component description() {
84+
return PaperAdventure.asAdventure(this.getHandle().description());
85+
}
86+
87+
@Override
88+
public Sound getSound() {
89+
return CraftSound.minecraftHolderToBukkit(this.getHandle().soundEvent());
6890
}
6991

7092
@NotNull
@@ -76,7 +98,7 @@ public NamespacedKey getKey() {
7698
@Override
7799
public @NotNull String translationKey() {
78100
if (!(this.getHandle().description().getContents() instanceof final net.minecraft.network.chat.contents.TranslatableContents translatableContents)) {
79-
throw new UnsupportedOperationException("Description isn't translatable!"); // Paper
101+
throw new UnsupportedOperationException("Description isn't translatable!");
80102
}
81103
return translatableContents.getKey();
82104
}

paper-server/src/main/java/org/bukkit/craftbukkit/CraftSound.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public static Sound minecraftToBukkit(SoundEvent minecraft) {
1414
return CraftRegistry.minecraftToBukkit(minecraft, Registries.SOUND_EVENT);
1515
}
1616

17+
public static Sound minecraftHolderToBukkit(Holder<SoundEvent> minecraft) {
18+
return CraftRegistry.minecraftHolderToBukkit(minecraft, Registries.SOUND_EVENT);
19+
}
20+
1721
public static SoundEvent bukkitToMinecraft(Sound bukkit) {
1822
return CraftRegistry.bukkitToMinecraft(bukkit);
1923
}

0 commit comments

Comments
 (0)