Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Unmodifiable;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* Holds the properties for this item for when it is consumed.
Expand All @@ -31,6 +32,14 @@ static Consumable.Builder consumable() {
@Contract(pure = true)
ItemUseAnimation animation();

/**
* Gets the fixed range of the sound played when consuming this item, if present.
*
* @return the fixed sound range, or {@code null} if the sound uses a variable range
*/
@Contract(pure = true)
@Nullable Float soundRange();

@Contract(pure = true)
Key sound();

Expand Down Expand Up @@ -73,6 +82,16 @@ interface Builder extends DataComponentBuilder<Consumable> {
@Contract(value = "_ -> this", mutates = "this")
Builder sound(Key sound);

/**
* Sets the sound played when consuming the item with an optional fixed range.
*
* @param sound the {@link Key} representing the sound to be used
* @param range the fixed range of the sound, or {@code null} to use a variable range
* @return the builder for chaining
*/
@Contract(value = "_, _ -> this", mutates = "this")
Builder sound(Key sound, @Nullable Float range);

/**
* Sets whether consuming the item results in particle effects.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import io.papermc.paper.util.MCUtil;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.List;
import java.util.Optional;
import net.kyori.adventure.key.Key;
import net.minecraft.core.Holder;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import org.bukkit.craftbukkit.util.Handleable;
import org.checkerframework.checker.index.qual.NonNegative;
import org.jetbrains.annotations.Unmodifiable;
import org.jspecify.annotations.Nullable;

import static io.papermc.paper.util.BoundChecker.requireNonNegative;

Expand Down Expand Up @@ -43,6 +45,11 @@ public Key sound() {
return PaperAdventure.asAdventure(this.impl.sound().value().location());
}

@Override
public @Nullable Float soundRange() {
return this.impl.sound().value().fixedRange().orElse(null);
}

@Override
public boolean hasConsumeParticles() {
return this.impl.hasConsumeParticles();
Expand All @@ -58,7 +65,7 @@ public Consumable.Builder toBuilder() {
return new BuilderImpl()
.consumeSeconds(this.consumeSeconds())
.animation(this.animation())
.sound(this.sound())
.sound(this.sound(), this.soundRange())
.addEffects(this.consumeEffects());
}

Expand Down Expand Up @@ -90,6 +97,12 @@ public Builder sound(final Key sound) {
return this;
}

@Override
public Builder sound(final Key sound, final @Nullable Float range) {
this.eatSound = Holder.direct(new SoundEvent(PaperAdventure.asVanilla(sound), Optional.ofNullable(range)));
return this;
}

@Override
public Builder hasConsumeParticles(final boolean hasConsumeParticles) {
this.hasConsumeParticles = hasConsumeParticles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.papermc.paper.datacomponent.DataComponentType;
import io.papermc.paper.datacomponent.DataComponentTypes;
import io.papermc.paper.datacomponent.item.ChargedProjectiles;
import io.papermc.paper.datacomponent.item.Consumable;
import io.papermc.paper.datacomponent.item.CustomModelData;
import io.papermc.paper.datacomponent.item.DyedItemColor;
import io.papermc.paper.datacomponent.item.Fireworks;
Expand Down Expand Up @@ -252,6 +253,21 @@ void testTool() {
Assertions.assertFalse(meta.hasTool());
}

@Test
void testConsumableFixedSoundRange() {
final Key sound = Key.key("minecraft:item.apple.eat");
final Consumable consumable = Consumable.consumable()
.sound(sound, 12.0F)
.build();

Assertions.assertEquals(sound, consumable.sound());
Assertions.assertEquals(12.0F, consumable.soundRange());

final Consumable rebuilt = consumable.toBuilder().build();
Assertions.assertEquals(sound, rebuilt.sound());
Assertions.assertEquals(12.0F, rebuilt.soundRange());
}

@Test
void testJukeboxPlayable() {
JukeboxPlayable properties = JukeboxPlayable.jukeboxPlayable(JukeboxSong.MALL).build();
Expand Down