From d310f6399515b94b75a770fe53648a5d617673b7 Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Sun, 8 Jan 2023 20:36:10 +1100 Subject: [PATCH] SPIGOT-7034: Add methods for set/get instrument in Goat Horn By: byquanton <32410361+byquanton@users.noreply.github.com> --- .../inventory/CraftItemFactory.java | 2 + .../craftbukkit/inventory/CraftItemStack.java | 2 + .../craftbukkit/inventory/CraftMetaItem.java | 4 +- .../inventory/CraftMetaMusicInstrument.java | 133 ++++++++++++++++++ .../craftbukkit/inventory/ItemMetaTest.java | 9 ++ 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java index a1cbd96be3..c973b45a5b 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java @@ -322,6 +322,8 @@ public final class CraftItemFactory implements ItemFactory { return meta instanceof CraftMetaCompass ? meta : new CraftMetaCompass(meta); case BUNDLE: return meta instanceof CraftMetaBundle ? meta : new CraftMetaBundle(meta); + case GOAT_HORN: + return meta instanceof CraftMetaMusicInstrument ? meta : new CraftMetaMusicInstrument(meta); default: return new CraftMetaItem(meta); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java index ffa7e9505f..977065a412 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -585,6 +585,8 @@ public final class CraftItemStack extends ItemStack { return new CraftMetaCompass(item.getTag()); case BUNDLE: return new CraftMetaBundle(item.getTag()); + case GOAT_HORN: + return new CraftMetaMusicInstrument(item.getTag()); default: return new CraftMetaItem(item.getTag()); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index b4389c1a0e..6ba71fb623 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -158,6 +158,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { .put(CraftMetaEntityTag.class, "ENTITY_TAG") .put(CraftMetaCompass.class, "COMPASS") .put(CraftMetaBundle.class, "BUNDLE") + .put(CraftMetaMusicInstrument.class, "MUSIC_INSTRUMENT") .put(CraftMetaItem.class, "UNSPECIFIC") .build(); @@ -1418,7 +1419,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { CraftMetaCompass.LODESTONE_DIMENSION.NBT, CraftMetaCompass.LODESTONE_POS.NBT, CraftMetaCompass.LODESTONE_TRACKED.NBT, - CraftMetaBundle.ITEMS.NBT + CraftMetaBundle.ITEMS.NBT, + CraftMetaMusicInstrument.GOAT_HORN_INSTRUMENT.NBT )); } return HANDLED_TAGS; diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java new file mode 100644 index 0000000000..b4e105e3fe --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java @@ -0,0 +1,133 @@ +package org.bukkit.craftbukkit.inventory; + +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import net.minecraft.nbt.NBTTagCompound; +import org.bukkit.Material; +import org.bukkit.MusicInstrument; +import org.bukkit.NamespacedKey; +import org.bukkit.configuration.serialization.DelegateDeserialization; +import org.bukkit.inventory.meta.MusicInstrumentMeta; + +@DelegateDeserialization(CraftMetaItem.SerializableMeta.class) +public class CraftMetaMusicInstrument extends CraftMetaItem implements MusicInstrumentMeta { + + static final ItemMetaKey GOAT_HORN_INSTRUMENT = new ItemMetaKey("instrument"); + private MusicInstrument instrument; + + CraftMetaMusicInstrument(CraftMetaItem meta) { + super(meta); + + if (meta instanceof CraftMetaMusicInstrument) { + CraftMetaMusicInstrument craftMetaMusicInstrument = (CraftMetaMusicInstrument) meta; + this.instrument = craftMetaMusicInstrument.instrument; + } + } + + CraftMetaMusicInstrument(NBTTagCompound tag) { + super(tag); + + if (tag.contains(GOAT_HORN_INSTRUMENT.NBT)) { + String string = tag.getString(GOAT_HORN_INSTRUMENT.NBT); + this.instrument = MusicInstrument.getByKey(NamespacedKey.fromString(string)); + } + } + + CraftMetaMusicInstrument(Map map) { + super(map); + + String instrumentString = SerializableMeta.getString(map, GOAT_HORN_INSTRUMENT.BUKKIT, true); + if (instrumentString != null) { + this.instrument = MusicInstrument.getByKey(NamespacedKey.fromString(instrumentString)); + } + } + + @Override + void applyToItem(NBTTagCompound tag) { + super.applyToItem(tag); + + if (instrument != null) { + tag.putString(GOAT_HORN_INSTRUMENT.NBT, instrument.getKey().toString()); + } + } + + @Override + boolean applicableTo(Material type) { + switch (type) { + case GOAT_HORN: + return true; + default: + return false; + } + } + + @Override + boolean equalsCommon(CraftMetaItem meta) { + if (!super.equalsCommon(meta)) { + return false; + } + if (meta instanceof CraftMetaMusicInstrument) { + CraftMetaMusicInstrument that = (CraftMetaMusicInstrument) meta; + return this.instrument == that.instrument; + } + return true; + } + + @Override + boolean notUncommon(CraftMetaItem meta) { + return super.notUncommon(meta) && (meta instanceof CraftMetaMusicInstrument || isInstrumentEmpty()); + } + + @Override + boolean isEmpty() { + return super.isEmpty() && isInstrumentEmpty(); + } + + boolean isInstrumentEmpty() { + return instrument == null; + } + + @Override + int applyHash() { + final int orginal; + int hash = orginal = super.applyHash(); + + if (hasInstrument()) { + hash = 61 * hash + instrument.hashCode(); + } + + return orginal != hash ? CraftMetaMusicInstrument.class.hashCode() ^ hash : hash; + } + + @Override + public CraftMetaMusicInstrument clone() { + CraftMetaMusicInstrument meta = (CraftMetaMusicInstrument) super.clone(); + meta.instrument = this.instrument; + return meta; + } + + @Override + ImmutableMap.Builder serialize(ImmutableMap.Builder builder) { + super.serialize(builder); + + if (hasInstrument()) { + builder.put(GOAT_HORN_INSTRUMENT.BUKKIT, instrument.getKey().toString()); + } + + return builder; + } + + @Override + public MusicInstrument getInstrument() { + return instrument; + } + + public boolean hasInstrument() { + return instrument != null; + } + + @Override + public void setInstrument(MusicInstrument instrument) { + this.instrument = instrument; + } +} diff --git a/paper-server/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java b/paper-server/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java index 7f10e43382..5f188f2a48 100644 --- a/paper-server/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java +++ b/paper-server/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java @@ -19,6 +19,7 @@ import org.bukkit.DyeColor; import org.bukkit.FireworkEffect; import org.bukkit.FireworkEffect.Type; import org.bukkit.Material; +import org.bukkit.MusicInstrument; import org.bukkit.NamespacedKey; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeModifier; @@ -377,6 +378,14 @@ public class ItemMetaTest extends AbstractTestingBase { cleanStack.setItemMeta(meta); return cleanStack; } + }, + new StackProvider(Material.GOAT_HORN) { + @Override ItemStack operate(ItemStack cleanStack) { + final CraftMetaMusicInstrument meta = (CraftMetaMusicInstrument) cleanStack.getItemMeta(); + meta.setInstrument(MusicInstrument.ADMIRE); + cleanStack.setItemMeta(meta); + return cleanStack; + } } );