Search in sources :

Example 1 with InstrumentType

use of org.spongepowered.api.data.type.InstrumentType in project SpongeCommon by SpongePowered.

the class InstrumentTestPlugin method onUseItem.

@Listener
public void onUseItem(InteractBlockEvent.Secondary.MainHand event, @First Player player) {
    if (!enabled || !player.get(Keys.IS_SNEAKING).get()) {
        return;
    }
    final BlockSnapshot snapshot = event.getTargetBlock();
    if (snapshot.getState().getType().equals(BlockTypes.NOTEBLOCK)) {
        final InstrumentProperty instrumentProperty = player.getWorld().getBlock(snapshot.getPosition().sub(0, 1, 0)).getProperty(InstrumentProperty.class).orElse(null);
        if (instrumentProperty != null) {
            final InstrumentType instrument = instrumentProperty.getValue();
            player.sendMessage(Text.of(TextColors.DARK_GREEN, "Clicked on a note block with instrument: ", TextColors.GREEN, instrument.getName()));
        } else {
            player.sendMessage(Text.of(TextColors.DARK_GREEN, "Clicked on a note block that strangely did not have any instrument type."));
        }
    } else {
        final InstrumentProperty instrumentProperty = snapshot.getProperty(InstrumentProperty.class).orElse(null);
        if (instrumentProperty != null) {
            final InstrumentType instrument = instrumentProperty.getValue();
            player.sendMessage(Text.of(TextColors.DARK_GREEN, "Clicked on a block with instrument type: ", TextColors.GREEN, instrument.getName()));
        } else {
            player.sendMessage(Text.of(TextColors.DARK_GREEN, "Clicked on a block which had no instrument type."));
        }
    }
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) InstrumentType(org.spongepowered.api.data.type.InstrumentType) InstrumentProperty(org.spongepowered.api.data.property.block.InstrumentProperty) Listener(org.spongepowered.api.event.Listener)

Example 2 with InstrumentType

use of org.spongepowered.api.data.type.InstrumentType in project LanternServer by LanternPowered.

the class LanternNote method playNote.

@Override
public void playNote() {
    final Location<World> location = getLocation();
    final Location<World> downLocation = location.add(0, -1, 0);
    // Get the instrument type based on the underlying block
    final InstrumentType instrumentType = downLocation.getProperty(InstrumentProperty.class).map(InstrumentProperty::getValue).orElse(InstrumentTypes.HARP);
    final NotePitch notePitch = get(Keys.NOTE_PITCH).get();
    // Trigger the note play effect
    ((LanternWorld) location.getExtent()).addBlockAction(location.getBlockPosition(), getBlock().getType(), new NoteAction(instrumentType, notePitch));
    // Calculate the pitch value based on the note pitch
    double pitch = (double) ((LanternNotePitch) notePitch).getInternalId();
    pitch = Math.pow(2.0, (pitch - 12.0) / 12.0);
    location.getExtent().playSound(instrumentType.getSound(), SoundCategories.BLOCK, location.getPosition().add(0.5, 0.5, 0.5), 3.0, pitch);
}
Also used : NoteAction(org.lanternpowered.server.block.action.vanilla.NoteAction) InstrumentType(org.spongepowered.api.data.type.InstrumentType) LanternWorld(org.lanternpowered.server.world.LanternWorld) World(org.spongepowered.api.world.World) LanternWorld(org.lanternpowered.server.world.LanternWorld) NotePitch(org.spongepowered.api.data.type.NotePitch) LanternNotePitch(org.lanternpowered.server.data.type.LanternNotePitch)

Aggregations

InstrumentType (org.spongepowered.api.data.type.InstrumentType)2 NoteAction (org.lanternpowered.server.block.action.vanilla.NoteAction)1 LanternNotePitch (org.lanternpowered.server.data.type.LanternNotePitch)1 LanternWorld (org.lanternpowered.server.world.LanternWorld)1 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)1 InstrumentProperty (org.spongepowered.api.data.property.block.InstrumentProperty)1 NotePitch (org.spongepowered.api.data.type.NotePitch)1 Listener (org.spongepowered.api.event.Listener)1 World (org.spongepowered.api.world.World)1