Skip to content

Commit

Permalink
Add more scroll options, change quick place behaviour for ANBs and Re…
Browse files Browse the repository at this point in the history
…peater
  • Loading branch information
IONayrus committed Feb 5, 2025
1 parent c6b9bc9 commit 8d37f32
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ else if (items.getB().is(NBMTags.Items.TUNERS))
if (items.contains(Registry.COMPOSER.get())) {
ItemStack composer = items.getFirst(Registry.COMPOSER.get());
ComposeData cData = ComposeData.getComposeData(composer);
player.displayClientMessage(Component.literal("Repeater delay: " + cData.preDelay()).withColor(Color.RED.darker().getRGB()), true);
player.displayClientMessage(Component.literal("Next repeater delay: " + cData.postDelay()).withColor(Color.RED.darker().getRGB()), true);
}
}
CoreRender.renderCoresInRange(e, level);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static void useItemOnBlockEvent(UseItemOnBlockEvent event){
if(state.getBlock() instanceof RepeaterBlock && items.contains(Registry.COMPOSER.get())){
ItemStack composer = items.getFirst(Registry.COMPOSER.get());
ComposeData cData = ComposeData.getComposeData(composer);
int target = cData.preDelay();
int target = cData.postDelay();
int set = Math.min(target, 4);
if(target>0){
target -= set;
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/net/nayrus/noteblockmaster/item/ComposersNote.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public InteractionResult useOn(UseOnContext context) {
private static InteractionResult placeRepeater(UseOnContext context, Player player, Level level, BlockPos pos, Inventory inv) {
ItemStack composer = context.getItemInHand();
ComposeData cData = ComposeData.getComposeData(composer);
int target = cData.preDelay();
int target = cData.postDelay();
int set = Math.min(target, 4);
if(target>0) {
target -= set;
Expand All @@ -80,21 +80,22 @@ private static InteractionResult placeRepeater(UseOnContext context, Player play
public static Tuple<Integer, Integer> subtickAndPauseOnBeat(int beat, float bpm){
float tPB = 60000 / bpm;
int current_subtick = 0;
int pre_delay = 0;
int post_delay = 0;
int lastTime = 0;
for(int i = (beat - 1); i <= beat; i++){
for(int i = beat; i <= beat + 1; i++){
int noteTimeMs = (int) (i * tPB);
int subTickTime = noteTimeMs % 100;
int redClockTime = noteTimeMs - subTickTime;
int subtick = (subTickTime - (subTickTime % AdvancedNoteBlock.SUBTICK_LENGTH)) / AdvancedNoteBlock.SUBTICK_LENGTH;

if(i == beat){
current_subtick = subtick;
pre_delay = ((redClockTime - lastTime) / 100);
}else{
post_delay = ((redClockTime - lastTime) / 100);
}
lastTime = redClockTime;
}
return new Tuple<>(current_subtick, pre_delay);
return new Tuple<>(current_subtick, post_delay);
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/net/nayrus/noteblockmaster/item/TunerItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ private static void placeAdvancedNoteBlock(Level level, ItemStack tuner, BlockPo
else{
if(composer.is(Registry.COMPOSER)){
ComposeData cData = ComposeData.getComposeData(composer);
level.setBlockAndUpdate(pos.above(), block.setInstrument(level, pos.above(), block.defaultBlockState())
.setValue(AdvancedNoteBlock.SUBTICK, cData.subtick()));
if(!player.isShiftKeyDown()){
Tuple<Integer, Integer> next = ComposersNote.subtickAndPauseOnBeat(cData.beat() + 1, cData.bpm());
composer.set(Registry.COMPOSE_DATA, new ComposeData(cData.beat() + 1, next.getA(), next.getB(), cData.bpm()));
ComposeData new_ = new ComposeData(cData.beat() + 1, next.getA(), next.getB(), cData.bpm());
composer.set(Registry.COMPOSE_DATA, new_);
cData = new_;
}
level.setBlockAndUpdate(pos.above(), block.setInstrument(level, pos.above(), block.defaultBlockState())
.setValue(AdvancedNoteBlock.SUBTICK, cData.subtick()));
}
else{
BlockState state = block.defaultBlockState().setValue(AdvancedNoteBlock.SUBTICK, data.value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import net.nayrus.noteblockmaster.NoteBlockMaster;
import net.nayrus.noteblockmaster.setup.Registry;

public record ComposeData(int beat, int subtick, int preDelay, float bpm) implements CustomPacketPayload {
public record ComposeData(int beat, int subtick, int postDelay, float bpm) implements CustomPacketPayload {

public static final Type<ComposeData> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(NoteBlockMaster.MOD_ID, "composedata"));

public static final Codec<ComposeData> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
Codec.INT.fieldOf("beat").forGetter(ComposeData::beat),
Codec.INT.fieldOf("subtick").forGetter(ComposeData::subtick),
Codec.INT.fieldOf("delay").forGetter(ComposeData::preDelay),
Codec.INT.fieldOf("delay").forGetter(ComposeData::postDelay),
Codec.FLOAT.fieldOf("bpm").forGetter(ComposeData::bpm)
).apply(instance, ComposeData::new)

Expand All @@ -28,7 +28,7 @@ public record ComposeData(int beat, int subtick, int preDelay, float bpm) implem
public static final StreamCodec<FriendlyByteBuf, ComposeData> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT, ComposeData::beat,
ByteBufCodecs.INT, ComposeData::subtick,
ByteBufCodecs.INT, ComposeData::preDelay,
ByteBufCodecs.INT, ComposeData::postDelay,
ByteBufCodecs.FLOAT, ComposeData::bpm,
ComposeData::new
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
|| op.keyDown.matches(keyCode, scanCode)
|| op.keyRight.matches(keyCode, scanCode)
|| op.keyLeft.matches(keyCode, scanCode)
|| op.keyShift.matches(keyCode, scanCode)
|| op.keyJump.matches(keyCode, scanCode)
|| keyCode == 257)
this.onClose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double scrollX, double scrollY) {
if(this.slider.isMouseOver(mouseX, mouseY) || this.input.isMouseOver(mouseX, mouseY))
changeValue((int)(this.value + scrollY * (hasShiftDown() ? 2 : 1)));
changeValue((int)(this.value + scrollY * getScrollFactor(false).floatValue()));
return super.mouseScrolled(mouseX, mouseY, scrollX, scrollY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void onPress(Button button) {
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double scrollX, double scrollY) {
if(this.slider.isMouseOver(mouseX, mouseY) || this.input.isMouseOver(mouseX, mouseY))
changeValue((int)(this.value + scrollY * (hasShiftDown() ? 2 : 1)));
changeValue((int)(this.value + scrollY * getScrollFactor(false).floatValue()));
return super.mouseScrolled(mouseX, mouseY, scrollX, scrollY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import net.nayrus.noteblockmaster.setup.Registry;
import net.neoforged.neoforge.network.PacketDistributor;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BaseComposerScreen extends BaseScreen implements Button.OnPress{

protected IntegerEditBox beat;
Expand Down Expand Up @@ -51,11 +54,13 @@ public void onClose() {
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double scrollX, double scrollY) {
if(this.bpm.isMouseOver(mouseX, mouseY))
changeBPMVal(this.bpm_val + (float) scrollY * (hasShiftDown() ? 2 : 1 ));
changeBPMVal(BigDecimal.valueOf(this.bpm_val)
.add(getScrollFactor().multiply(BigDecimal.valueOf(scrollY)))
.setScale(4, RoundingMode.HALF_UP).floatValue());
if(this.beat.isMouseOver(mouseX, mouseY))
changeBeatVal((int)(this.beat_val + scrollY * (hasShiftDown() ? 2 : 1 )));
changeBeatVal((int)(this.beat_val + scrollY * getScrollFactor().floatValue()));
else if(this.decrease.isMouseOver(mouseX, mouseY)) {
changeBeatVal((int)(this.beat_val + scrollY * (hasShiftDown() ? 2 : 1 )));
changeBeatVal((int)(this.beat_val + scrollY * getScrollFactor().floatValue()));
this.decrease.playDownSound(Minecraft.getInstance().getSoundManager());
}
return super.mouseScrolled(mouseX, mouseY, scrollX, scrollY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import net.minecraft.resources.ResourceLocation;
import net.nayrus.noteblockmaster.utils.Utils;

import java.math.BigDecimal;

public class BaseScreen extends Screen {

protected final ResourceLocation GUI;
Expand Down Expand Up @@ -49,4 +51,16 @@ public void mouseMoved(double mouseX, double mouseY) {
|| !Utils.isIntInRange((int)mouseY, getRelY(), getRelY() + this.guiHeight))
onClose();
}

public BigDecimal getScrollFactor(boolean reduceOnAlt){
BigDecimal factor = BigDecimal.ONE;
if (hasShiftDown()) factor = factor.multiply(BigDecimal.valueOf(2));
if (hasControlDown()) factor = factor.multiply(BigDecimal.valueOf(5));
if (hasAltDown()) factor = factor.multiply(BigDecimal.valueOf(reduceOnAlt ? 0.1 : 3));
return factor;
}

public BigDecimal getScrollFactor(){
return getScrollFactor(true);
}
}

0 comments on commit 8d37f32

Please sign in to comment.