Skip to content

Commit

Permalink
Add Key Bindings, add Bass + Flute sustain, sounds okay
Browse files Browse the repository at this point in the history
  • Loading branch information
IONayrus committed Jan 19, 2025
1 parent 3a1e3e7 commit 21c2dbf
Show file tree
Hide file tree
Showing 28 changed files with 89 additions and 36 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod_name=Note Block Master
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GNU GPLv3
# The mod version. See https://semver.org/
mod_version=0.6.3
mod_version=0.6.9
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/nayrus/noteblockmaster/NoteBlockMaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.nayrus.noteblockmaster.network.PacketHandler;
import net.nayrus.noteblockmaster.setup.Config;
import net.nayrus.noteblockmaster.setup.Registry;
import net.nayrus.noteblockmaster.utils.KeyBindings;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
Expand All @@ -27,13 +28,10 @@
import net.neoforged.neoforge.server.command.ConfigCommand;
import org.slf4j.Logger;

// The value here should match an entry in the META-INF/neoforge.mods.toml file
@Mod(NoteBlockMaster.MOD_ID)
public class NoteBlockMaster
{

public static final String MOD_ID = "noteblockmaster";

@SuppressWarnings("unused")
public static final Logger LOGGER = LogUtils.getLogger();

Expand All @@ -51,6 +49,7 @@ public NoteBlockMaster(IEventBus modEventBus, ModContainer modContainer)
RECIPE_SERIALIZERS.register(modEventBus);

modEventBus.addListener(PacketHandler::registerNetwork);
modEventBus.addListener(KeyBindings::registerBindings);

if(FMLEnvironment.dist == Dist.CLIENT){
NeoForge.EVENT_BUS.register(ClientEvents.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public void registerSounds() {
.subtitle("sounds.noteblockmaster.noteblock_smithing")
);
addBasicLocalSounds(SoundRegistry.SUSTAINED_HARP_SOUND, "sustained_harp");
addBasicLocalSound(SoundRegistry.SUSTAINED_BASS_SOUND, "sustained_bass");
addBasicLocalSounds(SoundRegistry.SUSTAINED_BASS_SOUND, "sustained_bass");
addBasicLocalSound(SoundRegistry.SUSTAINED_BASEDRUM_SOUND, "sustained_basedrum");
addBasicLocalSound(SoundRegistry.SUSTAINED_BANJO_SOUND, "sustained_banjo");
addBasicLocalSound(SoundRegistry.SUSTAINED_FLUTE_SOUND, "sustained_flute");
addBasicLocalSounds(SoundRegistry.SUSTAINED_FLUTE_SOUND, "sustained_flute");
addBasicLocalSound(SoundRegistry.SUSTAINED_SNARE_SOUND, "sustained_snare");
addBasicLocalSound(SoundRegistry.SUSTAINED_HAT_SOUND, "sustained_hat");
addBasicLocalSound(SoundRegistry.SUSTAINED_CHIME_SOUND, "sustained_chime");
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/net/nayrus/noteblockmaster/event/ClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,40 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.nayrus.noteblockmaster.item.TunerItem;
import net.nayrus.noteblockmaster.network.data.ComposeData;
import net.nayrus.noteblockmaster.render.ANBInfoRender;
import net.nayrus.noteblockmaster.render.CoreRender;
import net.nayrus.noteblockmaster.render.RenderUtils;
import net.nayrus.noteblockmaster.setup.NBMTags;
import net.nayrus.noteblockmaster.setup.Registry;
import net.nayrus.noteblockmaster.sound.SubTickScheduler;
import net.nayrus.noteblockmaster.sound.SustainingSound;
import net.nayrus.noteblockmaster.utils.FinalTuple;
import net.nayrus.noteblockmaster.utils.KeyBindings;
import net.nayrus.noteblockmaster.utils.Utils;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.client.event.RenderLevelStageEvent;
import net.neoforged.neoforge.client.event.sound.PlaySoundSourceEvent;

import java.awt.*;

public class ClientEvents {

@SubscribeEvent
public static void onClientTick(ClientTickEvent.Post event) {
while (KeyBindings.OPEN_GUI.get().consumeClick()) {
if(!(Minecraft.getInstance().player instanceof Player player)) return;
FinalTuple.ItemStackTuple items = FinalTuple.getHeldItems(player);
if(items.contains(TunerItem.class)){
ItemStack A = items.getA();
boolean isTuner = A.is(NBMTags.Items.TUNERS);
TunerItem.openTunerGUI(isTuner ? A : items.getB(), isTuner ? items.getB() : A);
}
}
}


@SubscribeEvent
public static void renderBlockOverlays(RenderLevelStageEvent e){
RenderUtils.CURRENT_CAM_POS = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition();
Expand Down Expand Up @@ -51,7 +68,7 @@ else if (items.contains(Registry.TEMPOTUNER.get()))
public static void playSoundSourceEvent(PlaySoundSourceEvent e){
if(!(e.getSound() instanceof SustainingSound sound)) return;
sound.addNoteParticle();
SubTickScheduler.scheduleStop(sound);
sound.setChannel(e.getChannel());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public InteractionResult useOn(UseOnContext context) {
if(state.getBlock() instanceof AdvancedNoteBlock) return item.useOn(context, true);
ItemStack tuner = player.getOffhandItem();
Inventory inv = player.getInventory();
if(player.isShiftKeyDown()) return InteractionResult.PASS;
if(tuner.is(Registry.TEMPOTUNER) && inv.contains(stack -> stack.is(Items.REPEATER))){
return placeRepeater(context, player, level, pos, inv);
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/net/nayrus/noteblockmaster/item/TunerItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ public InteractionResult useOn(UseOnContext context) {
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand usedHand) {
ItemStack itemstack = player.getItemInHand(usedHand);
if(level.isClientSide()){
if(itemstack.is(Registry.TEMPOTUNER))
Minecraft.getInstance().setScreen(new TempoTunerScreen(itemstack, player.getItemInHand(InteractionHand.values()[(usedHand.ordinal() + 1) % 2])));
if(itemstack.is(Registry.NOTETUNER))
Minecraft.getInstance().setScreen(new NoteTunerScreen(itemstack));
}
if(level.isClientSide()) openTunerGUI(itemstack, player.getItemInHand(InteractionHand.values()[(usedHand.ordinal() + 1) % 2]));
return new InteractionResultHolder<>(InteractionResult.SUCCESS, itemstack);
}

public static void openTunerGUI(ItemStack tuner, ItemStack second){
if(tuner.is(Registry.TEMPOTUNER))
Minecraft.getInstance().setScreen(new TempoTunerScreen(tuner, second));
if(tuner.is(Registry.NOTETUNER))
Minecraft.getInstance().setScreen(new NoteTunerScreen(tuner));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ public class SoundRegistry {
SoundEvent.createVariableRangeEvent(ResourceLocation.fromNamespaceAndPath(MOD_ID, "sustained_snare")));
public static final Supplier<SoundEvent> SUSTAINED_HAT_SOUND = SOUND_EVENTS.register("sustained_hat", () ->
SoundEvent.createVariableRangeEvent(ResourceLocation.fromNamespaceAndPath(MOD_ID, "sustained_hat")));
public static final Supplier<SoundEvent> SUSTAINED_BASS_SOUND = SOUND_EVENTS.register("sustained_bass", () ->
SoundEvent.createVariableRangeEvent(ResourceLocation.fromNamespaceAndPath(MOD_ID, "sustained_bass")));
public static final Supplier<SoundEvent> SUSTAINED_FLUTE_SOUND = SOUND_EVENTS.register("sustained_flute", () ->
SoundEvent.createVariableRangeEvent(ResourceLocation.fromNamespaceAndPath(MOD_ID, "sustained_flute")));
public static final List<Supplier<SoundEvent>> SUSTAINED_BASS_SOUND = createSoundEvents("sustained_bass");
public static final List<Supplier<SoundEvent>> SUSTAINED_FLUTE_SOUND = createSoundEvents("sustained_flute");
public static final Supplier<SoundEvent> SUSTAINED_BELL_SOUND = SOUND_EVENTS.register("sustained_bell", () ->
SoundEvent.createVariableRangeEvent(ResourceLocation.fromNamespaceAndPath(MOD_ID, "sustained_bell")));
public static final Supplier<SoundEvent> SUSTAINED_GUITAR_SOUND = SOUND_EVENTS.register("sustained_guitar", () ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@
import net.minecraft.world.level.block.state.BlockState;
import net.nayrus.noteblockmaster.block.AdvancedNoteBlock;
import net.nayrus.noteblockmaster.block.TuningCore;
import net.nayrus.noteblockmaster.utils.FinalTuple;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.concurrent.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class SubTickScheduler {

public static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new SubtickThread());
public static HashMap<BlockPos, FinalTuple<SustainingSound, ScheduledFuture<?>>> SUSTAINED_SOUNDS = new HashMap<>();
public static HashMap<BlockPos, SustainingSound> SUSTAINED_SOUNDS = new HashMap<>();

public static void delayedNoteBlockEvent(BlockState state, Level level, BlockPos pos, AdvancedInstrument instrument, float volume){
executor.schedule(() -> {
Expand All @@ -43,7 +45,7 @@ public static void delayedNoteBlockEvent(BlockState state, Level level, BlockPos
public static void delayedSustainedNoteBlockEvent(BlockState anb, BlockState core, Level level, BlockPos pos, AdvancedInstrument instrument){
if(level.isClientSide()) {
SustainingSound instance = new SustainingSound(
instrument.getSustainedEvent(TuningCore.getSustain(core)), SoundSource.RECORDS, 3.0F * (TuningCore.getVolume(core) / 20.0F), AdvancedNoteBlock.getPitchFromNote(AdvancedNoteBlock.getNoteValue(anb)),
instrument.getSustainedEvent(TuningCore.getSustain(core)), SoundSource.RECORDS, (TuningCore.getVolume(core) / 20.0F), AdvancedNoteBlock.getPitchFromNote(AdvancedNoteBlock.getNoteValue(anb)),
RandomSource.create(level.getRandom().nextLong()), pos, level, TuningCore.getSustain(core) * 1000);

executor.schedule(()-> playSustainingSound(instance),
Expand All @@ -56,16 +58,10 @@ public static void playSustainingSound(SustainingSound sound) {
Minecraft.getInstance().getSoundManager().play(sound);
}

public static void scheduleStop(SustainingSound sound){
SUSTAINED_SOUNDS.put(sound.getImmutablePos(), new FinalTuple<>(sound, executor.schedule(() ->
playbackStop(sound.getImmutablePos()), sound.getSustain(), TimeUnit.MILLISECONDS)));
}

@OnlyIn(Dist.CLIENT)
public static void playbackStop(BlockPos pos){
SUSTAINED_SOUNDS.computeIfPresent(pos.immutable(), (p, tuple) -> {
Minecraft.getInstance().getSoundManager().stop(tuple.getA());
tuple.getB().cancel(true);
SUSTAINED_SOUNDS.computeIfPresent(pos.immutable(), (p, sound) -> {
if(!sound.getChannel().stopped()) Minecraft.getInstance().getSoundManager().stop(sound);
return null;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package net.nayrus.noteblockmaster.sound;

import com.mojang.blaze3d.audio.Channel;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.Level;
import net.nayrus.noteblockmaster.block.TuningCore;

public class SustainingSound extends SimpleSoundInstance {
public class SustainingSound extends SimpleSoundInstance{

private final BlockPos immutablePos;
private final int sustain;
private final Level level;
private Channel channel;
private final int sustain;

public SustainingSound(SoundEvent soundEvent, SoundSource source, float volume, float pitch, RandomSource random, BlockPos pos, Level level, int sustain) {
super(soundEvent, source, volume, pitch, random, pos);
Expand All @@ -22,14 +25,23 @@ public SustainingSound(SoundEvent soundEvent, SoundSource source, float volume,
}

public void addNoteParticle(){
level.addParticle(ParticleTypes.NOTE, this.getX(), this.getY() + 0.7, this.getZ(), this.pitch + this.sustain/200.0, 0.0, 0.0);
level.addParticle(ParticleTypes.NOTE, this.getX(), this.getY() + 0.7, this.getZ(), this.pitch + this.sustain/ (double)TuningCore.SUSTAIN_MAXVAL, 0.0, 0.0);
}

public BlockPos getImmutablePos() {
return immutablePos;
}

public int getSustain() {
return sustain;
@Override
public Attenuation getAttenuation() {
return Attenuation.NONE;
}

public void setChannel(Channel channel) {
this.channel = channel;
}

public Channel getChannel() {
return this.channel;
}
}
26 changes: 26 additions & 0 deletions src/main/java/net/nayrus/noteblockmaster/utils/KeyBindings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.nayrus.noteblockmaster.utils;

import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;
import net.neoforged.neoforge.client.settings.KeyConflictContext;
import net.neoforged.neoforge.common.util.Lazy;
import org.lwjgl.glfw.GLFW;

public class KeyBindings {

public static final Lazy<KeyMapping> OPEN_GUI = Lazy.of(() -> new KeyMapping(
"key.noteblockmaster.opengui",
KeyConflictContext.IN_GAME,
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_R,
"key.categories.noteblockmaster.nbm" // Mapping will be in the new example category
));

@SubscribeEvent
public static void registerBindings(RegisterKeyMappingsEvent event) {
event.register(OPEN_GUI.get());
}

}
4 changes: 3 additions & 1 deletion src/main/resources/assets/noteblockmaster/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"item.noteblockmaster.core": "Composers Core",
"item.noteblockmaster.sustain": "Sustaining Core",
"item.noteblockmaster.volume": "Muffling Core",
"sounds.noteblockmaster.noteblock_smithing": "Note block upgraded"
"sounds.noteblockmaster.noteblock_smithing": "Note block upgraded",
"key.categories.noteblockmaster.nbm": "Note Block Master",
"key.noteblockmaster.opengui": "Open GUI"
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 21c2dbf

Please sign in to comment.