Search in sources :

Example 1 with MusicDisc

use of org.spongepowered.api.effect.sound.music.MusicDisc in project SpongeCommon by SpongePowered.

the class ServerLevelMixin method impl$throwBroadcastEvent.

@Inject(method = "levelEvent", at = @At("HEAD"), cancellable = true)
private void impl$throwBroadcastEvent(final Player player, final int eventID, final BlockPos pos, final int dataID, CallbackInfo ci) {
    if (eventID == Constants.WorldEvents.PLAY_RECORD_EVENT && ShouldFire.PLAY_SOUND_EVENT_RECORD) {
        try (final CauseStackManager.StackFrame frame = Sponge.server().causeStackManager().pushCauseFrame()) {
            final BlockEntity tileEntity = this.shadow$getBlockEntity(pos);
            if (tileEntity instanceof JukeboxBlockEntity) {
                final JukeboxBlockEntity jukebox = (JukeboxBlockEntity) tileEntity;
                final ItemStack record = jukebox.getRecord();
                frame.pushCause(jukebox);
                frame.addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(record));
                if (!record.isEmpty()) {
                    final Optional<MusicDisc> recordProperty = ((org.spongepowered.api.item.inventory.ItemStack) (Object) record).get(Keys.MUSIC_DISC);
                    if (!recordProperty.isPresent()) {
                        // Safeguard for https://github.com/SpongePowered/SpongeCommon/issues/2337
                        return;
                    }
                    final MusicDisc recordType = recordProperty.get();
                    final PlaySoundEvent.Record event = SpongeCommonEventFactory.callPlaySoundRecordEvent(frame.currentCause(), jukebox, recordType, dataID);
                    if (event.isCancelled()) {
                        ci.cancel();
                    }
                }
            }
        }
    }
}
Also used : MusicDisc(org.spongepowered.api.effect.sound.music.MusicDisc) CauseStackManager(org.spongepowered.api.event.CauseStackManager) PlaySoundEvent(org.spongepowered.api.event.sound.PlaySoundEvent) JukeboxBlockEntity(net.minecraft.world.level.block.entity.JukeboxBlockEntity) ItemStack(net.minecraft.world.item.ItemStack) JukeboxBlockEntity(net.minecraft.world.level.block.entity.JukeboxBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

ItemStack (net.minecraft.world.item.ItemStack)1 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)1 JukeboxBlockEntity (net.minecraft.world.level.block.entity.JukeboxBlockEntity)1 MusicDisc (org.spongepowered.api.effect.sound.music.MusicDisc)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 PlaySoundEvent (org.spongepowered.api.event.sound.PlaySoundEvent)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1