Add registry builder for Instrument (#12682)
Co-authored-by: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com>
This commit is contained in:
@@ -12,6 +12,7 @@ import io.papermc.paper.registry.data.PaperDamageTypeRegistryEntry;
|
||||
import io.papermc.paper.registry.data.PaperEnchantmentRegistryEntry;
|
||||
import io.papermc.paper.registry.data.PaperFrogVariantRegistryEntry;
|
||||
import io.papermc.paper.registry.data.PaperGameEventRegistryEntry;
|
||||
import io.papermc.paper.registry.data.PaperInstrumentRegistryEntry;
|
||||
import io.papermc.paper.registry.data.PaperJukeboxSongRegistryEntry;
|
||||
import io.papermc.paper.registry.data.PaperPaintingVariantRegistryEntry;
|
||||
import io.papermc.paper.registry.data.PaperPigVariantRegistryEntry;
|
||||
@@ -125,7 +126,7 @@ public final class PaperRegistries {
|
||||
start(Registries.JUKEBOX_SONG, RegistryKey.JUKEBOX_SONG).craft(JukeboxSong.class, CraftJukeboxSong::new).writable(PaperJukeboxSongRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.BANNER_PATTERN, RegistryKey.BANNER_PATTERN).craft(PatternType.class, CraftPatternType::new, true).writable(PaperBannerPatternRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.PAINTING_VARIANT, RegistryKey.PAINTING_VARIANT).craft(Art.class, CraftArt::new).writable(PaperPaintingVariantRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.INSTRUMENT, RegistryKey.INSTRUMENT).craft(MusicInstrument.class, CraftMusicInstrument::new, true).build().delayed(),
|
||||
start(Registries.INSTRUMENT, RegistryKey.INSTRUMENT).craft(MusicInstrument.class, CraftMusicInstrument::new, true).writable(PaperInstrumentRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.CAT_VARIANT, RegistryKey.CAT_VARIANT).craft(Cat.Type.class, CraftCat.CraftType::new).writable(PaperCatTypeRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.FROG_VARIANT, RegistryKey.FROG_VARIANT).craft(Frog.Variant.class, CraftFrog.CraftVariant::new).writable(PaperFrogVariantRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.CHICKEN_VARIANT, RegistryKey.CHICKEN_VARIANT).craft(Chicken.Variant.class, CraftChicken.CraftVariant::new).writable(PaperChickenVariantRegistryEntry.PaperBuilder::new),
|
||||
|
||||
+11
@@ -1,4 +1,15 @@
|
||||
package io.papermc.paper.registry.data;
|
||||
|
||||
import io.papermc.paper.registry.RegistryBuilderFactory;
|
||||
import io.papermc.paper.registry.data.util.Conversions;
|
||||
import java.util.function.Consumer;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import org.bukkit.MusicInstrument;
|
||||
|
||||
public final class InlinedRegistryBuilderProviderImpl implements InlinedRegistryBuilderProvider {
|
||||
|
||||
@Override
|
||||
public MusicInstrument createInstrument(final Consumer<RegistryBuilderFactory<MusicInstrument, ? extends InstrumentRegistryEntry.Builder>> value) {
|
||||
return Conversions.global().createApiInstanceFromBuilder(Registries.INSTRUMENT, value);
|
||||
}
|
||||
}
|
||||
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package io.papermc.paper.registry.data;
|
||||
|
||||
import io.papermc.paper.registry.PaperRegistries;
|
||||
import io.papermc.paper.registry.PaperRegistryBuilder;
|
||||
import io.papermc.paper.registry.RegistryBuilderFactory;
|
||||
import io.papermc.paper.registry.TypedKey;
|
||||
import io.papermc.paper.registry.data.util.Conversions;
|
||||
import io.papermc.paper.registry.holder.PaperRegistryHolders;
|
||||
import io.papermc.paper.registry.holder.RegistryHolder;
|
||||
import java.util.function.Consumer;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.world.item.Instrument;
|
||||
import org.bukkit.MusicInstrument;
|
||||
import org.bukkit.Sound;
|
||||
import org.checkerframework.checker.index.qual.Positive;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import static io.papermc.paper.registry.data.util.Checks.asArgument;
|
||||
import static io.papermc.paper.registry.data.util.Checks.asArgumentMinExclusive;
|
||||
import static io.papermc.paper.registry.data.util.Checks.asConfigured;
|
||||
|
||||
public class PaperInstrumentRegistryEntry implements InstrumentRegistryEntry {
|
||||
|
||||
protected final Conversions conversions;
|
||||
protected @Nullable Holder<SoundEvent> soundEvent;
|
||||
protected @Nullable Float useDuration;
|
||||
protected @Nullable Float range;
|
||||
protected @Nullable Component description;
|
||||
|
||||
public PaperInstrumentRegistryEntry(final Conversions conversions, final @Nullable Instrument internal) {
|
||||
this.conversions = conversions;
|
||||
|
||||
if (internal == null) {
|
||||
return;
|
||||
}
|
||||
this.soundEvent = internal.soundEvent();
|
||||
this.useDuration = internal.useDuration();
|
||||
this.range = internal.range();
|
||||
this.description = internal.description();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegistryHolder<Sound, SoundEventRegistryEntry> soundEvent() {
|
||||
final Holder<SoundEvent> current = asConfigured(this.soundEvent, "soundEvent");
|
||||
return PaperRegistryHolders.create(current, e -> new PaperSoundEventRegistryEntry(this.conversions, e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Positive float duration() {
|
||||
return asConfigured(this.useDuration, "useDuration");
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Positive float range() {
|
||||
return asConfigured(this.range, "range");
|
||||
}
|
||||
|
||||
@Override
|
||||
public net.kyori.adventure.text.Component description() {
|
||||
return this.conversions.asAdventure(asConfigured(this.description, "description"));
|
||||
}
|
||||
|
||||
public static final class PaperBuilder extends PaperInstrumentRegistryEntry implements Builder, PaperRegistryBuilder<Instrument, MusicInstrument> {
|
||||
|
||||
public PaperBuilder(final Conversions conversions, final @Nullable Instrument internal) {
|
||||
super(conversions, internal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder soundEvent(final TypedKey<Sound> soundEvent) {
|
||||
this.soundEvent = this.conversions.getReferenceHolder(PaperRegistries.toNms(asArgument(soundEvent, "soundEvent")));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder soundEvent(final Consumer<RegistryBuilderFactory<Sound, ? extends SoundEventRegistryEntry.Builder>> soundEvent) {
|
||||
this.soundEvent = this.conversions.createHolderFromBuilder(Registries.SOUND_EVENT, asArgument(soundEvent, "soundEvent"));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder soundEvent(final RegistryHolder<Sound, SoundEventRegistryEntry> soundEvent) {
|
||||
this.soundEvent = PaperRegistryHolders.convert(soundEvent, this.conversions);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder duration(final @Positive float duration) {
|
||||
this.useDuration = asArgumentMinExclusive(duration, "useDuration", 0);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder range(final @Positive float range) {
|
||||
this.range = asArgumentMinExclusive(range, "range", 0);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder description(final net.kyori.adventure.text.Component description) {
|
||||
this.description = this.conversions.asVanilla(asArgument(description, "description"));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instrument build() {
|
||||
return new Instrument(
|
||||
asConfigured(this.soundEvent, "soundEvent"),
|
||||
this.duration(),
|
||||
this.range(),
|
||||
asConfigured(this.description, "description")
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user