Expand Instrument API

This commit is contained in:
Jake Potrebic 2022-11-19 12:38:50 -08:00
parent 8f498dc19a
commit 6f8d88a351
No known key found for this signature in database
GPG Key ID: 27CC63F7CBC866C7
2 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,62 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sat, 19 Nov 2022 12:38:15 -0800
Subject: [PATCH] Expand Instrument API
diff --git a/src/main/java/org/bukkit/Instrument.java b/src/main/java/org/bukkit/Instrument.java
index 92194803bcdfbcfdb99567835906ce9219eabd04..6fbdc2a177e7361e2e3e55d47714deec5944f907 100644
--- a/src/main/java/org/bukkit/Instrument.java
+++ b/src/main/java/org/bukkit/Instrument.java
@@ -78,6 +78,7 @@ public enum Instrument {
private final byte type;
private static final Map<Byte, Instrument> BY_DATA = Maps.newHashMap();
+ private static final java.util.Optional<org.bukkit.Instrument.InstrumentUtil> UTIL = net.kyori.adventure.util.Services.service(InstrumentUtil.class); // Paper
private Instrument(final int type) {
this.type = (byte) type;
@@ -110,4 +111,43 @@ public enum Instrument {
BY_DATA.put(instrument.getType(), instrument);
}
}
+
+ // Paper start
+ /**
+ * Gets the sound for this instrument.
+ *
+ * @return the sound
+ */
+ public @org.jetbrains.annotations.NotNull Sound getSound() {
+ return UTIL.orElseThrow().getSound(this);
+ }
+
+ /**
+ * Gets the instrument for the default block data of the provided material.
+ *
+ * @param material the material
+ * @return the instrument
+ */
+ public static @org.jetbrains.annotations.NotNull Instrument getInstrumentFor(@org.jetbrains.annotations.NotNull Material material) {
+ return getInstrumentFor(material.createBlockData());
+ }
+
+ /**
+ * Gets the instrument that plays for this block data
+ *
+ * @param blockData the block data
+ * @return the instrument
+ */
+ public static @org.jetbrains.annotations.NotNull Instrument getInstrumentFor(org.bukkit.block.data.@org.jetbrains.annotations.NotNull BlockData blockData) {
+ return UTIL.orElseThrow().getSoundFor(blockData);
+ }
+
+ @org.jetbrains.annotations.ApiStatus.Internal
+ public interface InstrumentUtil {
+
+ @org.jetbrains.annotations.NotNull Sound getSound(@org.jetbrains.annotations.NotNull Instrument instrument);
+
+ @org.jetbrains.annotations.NotNull Instrument getSoundFor(org.bukkit.block.data.@org.jetbrains.annotations.NotNull BlockData blockData);
+ }
+ // Paper end
}

View File

@ -0,0 +1,55 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sat, 19 Nov 2022 12:38:28 -0800
Subject: [PATCH] Expand Instrument API
diff --git a/src/main/java/io/papermc/paper/world/block/state/PaperInstrument.java b/src/main/java/io/papermc/paper/world/block/state/PaperInstrument.java
new file mode 100644
index 0000000000000000000000000000000000000000..c7552a4d55ce6e8a67633a4b24a2eb85059b3895
--- /dev/null
+++ b/src/main/java/io/papermc/paper/world/block/state/PaperInstrument.java
@@ -0,0 +1,36 @@
+package io.papermc.paper.world.block.state;
+
+import java.util.Objects;
+import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
+import org.bukkit.Instrument;
+import org.bukkit.Sound;
+import org.bukkit.block.data.BlockData;
+import org.bukkit.craftbukkit.CraftSound;
+import org.bukkit.craftbukkit.block.data.CraftBlockData;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.framework.qual.DefaultQualifier;
+
+@DefaultQualifier(NonNull.class)
+public final class PaperInstrument {
+
+ @SuppressWarnings("deprecation")
+ public static Instrument toBukkit(NoteBlockInstrument nms) {
+ return Objects.requireNonNull(Instrument.getByType((byte) nms.ordinal()));
+ }
+
+ public static NoteBlockInstrument toNMS(Instrument bukkit) {
+ return CraftBlockData.toNMS(bukkit, NoteBlockInstrument.class);
+ }
+
+ public static final class InstrumentUtilImpl implements org.bukkit.Instrument.InstrumentUtil {
+ @Override
+ public Sound getSound(Instrument instrument) {
+ return CraftSound.getBukkit(toNMS(instrument).getSoundEvent());
+ }
+
+ @Override
+ public Instrument getSoundFor(BlockData blockData) {
+ return toBukkit(NoteBlockInstrument.byState(((CraftBlockData) blockData).getState()));
+ }
+ }
+}
diff --git a/src/main/resources/META-INF/services/org.bukkit.Instrument$InstrumentUtil b/src/main/resources/META-INF/services/org.bukkit.Instrument$InstrumentUtil
new file mode 100644
index 0000000000000000000000000000000000000000..475a9e8344f7d43f686f43db95ec861c147595c1
--- /dev/null
+++ b/src/main/resources/META-INF/services/org.bukkit.Instrument$InstrumentUtil
@@ -0,0 +1 @@
+io.papermc.paper.world.block.state.PaperInstrument$InstrumentUtilImpl