Skip to content

Commit

Permalink
Implement changes from Forge
Browse files Browse the repository at this point in the history
  • Loading branch information
StavWasPlayZ committed Jul 22, 2023
1 parent 70b2416 commit ee5fcc4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;

import com.cstav.genshinstrument.client.config.ModClientConfigs;
import com.cstav.genshinstrument.client.gui.screens.instrument.GenshinConsentScreen;
Expand All @@ -17,6 +18,7 @@

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
Expand Down Expand Up @@ -145,7 +147,7 @@ public ResourceLocation getResourceFromRoot(final String path) {
}


protected final AbstractInstrumentOptionsScreen optionsScreen = initInstrumentOptionsScreen();
public final AbstractInstrumentOptionsScreen optionsScreen = initInstrumentOptionsScreen();

public final InteractionHand interactionHand;
public AbstractInstrumentScreen(final InteractionHand hand) {
Expand Down Expand Up @@ -241,6 +243,23 @@ public void onClose() {

super.onClose();
}


/**
* @return The current instrument screen, if present
*/
public static Optional<AbstractInstrumentScreen> getCurrentScreen(final Minecraft minecraft) {
if (minecraft.screen instanceof AbstractInstrumentScreen)
return Optional.of((AbstractInstrumentScreen)minecraft.screen);

if (minecraft.screen instanceof AbstractInstrumentOptionsScreen) {
final AbstractInstrumentOptionsScreen instrumentOptionsScreen = (AbstractInstrumentOptionsScreen)minecraft.screen;
if (instrumentOptionsScreen.isOverlay)
return Optional.of(instrumentOptionsScreen.instrumentScreen);
}

return Optional.empty();
}


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ protected int getButtonHeight() {


protected final Screen lastScreen;
protected final boolean isOverlay;
public final boolean isOverlay;

protected final @Nullable INoteLabel[] labels;
protected final @Nullable INoteLabel currLabel;

protected final @Nullable AbstractInstrumentScreen instrumentScreen;
public final @Nullable AbstractInstrumentScreen instrumentScreen;

public AbstractInstrumentOptionsScreen(@Nullable AbstractInstrumentScreen screen) {
super(Component.translatable("button.genshinstrument.instrumentOptions"));
Expand Down Expand Up @@ -349,18 +349,19 @@ public boolean isPauseScreen() {

// Make pressing notes possible with keyboard
@Override
public boolean keyPressed(int p_96552_, int p_96553_, int p_96554_) {
if (isOverlay && p_96552_ != 256)
instrumentScreen.keyPressed(p_96552_, p_96553_, p_96554_);
public boolean keyPressed(int pKeyCode, int pScanCode, int pModifiers) {
// Only pass when it is a note key
if (isOverlay && (instrumentScreen.getNoteByKey(pKeyCode) != null))
instrumentScreen.keyPressed(pKeyCode, pScanCode, pModifiers);

return super.keyPressed(p_96552_, p_96553_, p_96554_);
return super.keyPressed(pKeyCode, pScanCode, pModifiers);
}
@Override
public boolean keyReleased(int p_94715_, int p_94716_, int p_94717_) {
if (isOverlay && p_94715_ != 256)
instrumentScreen.keyReleased(p_94715_, p_94716_, p_94717_);
public boolean keyReleased(int pKeyCode, int pScanCode, int pModifiers) {
if (isOverlay && (instrumentScreen.getNoteByKey(pKeyCode) != null))
instrumentScreen.keyReleased(pKeyCode, pScanCode, pModifiers);

return super.keyReleased(p_94715_, p_94716_, p_94717_);
return super.keyReleased(pKeyCode, pScanCode, pModifiers);
}

// Also resizing
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/cstav/genshinstrument/event/ClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.cstav.genshinstrument.event.InstrumentPlayedEvent.ByPlayer.ByPlayerArgs;
import com.cstav.genshinstrument.event.InstrumentPlayedEvent.InstrumentPlayedEventArgs;
import com.cstav.genshinstrument.item.InstrumentItem;
import com.cstav.genshinstrument.util.ServerUtil;
import com.cstav.genshinstrument.sound.NoteSound;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand Down Expand Up @@ -41,30 +41,30 @@ public static void onWorldTick(Minecraft mc) {
public static void onInstrumentPlayed(final InstrumentPlayedEventArgs args) {
if (!args.isClientSide)
return;
if (!ModClientConfigs.SHARED_INSTRUMENT.get())
return;

// If this sound was produced by a player, and that player is ourselves - omit.
if ((args instanceof ByPlayerArgs) && ((ByPlayerArgs)(args)).player.equals(MINECRAFT.player))
return;

if (!ModClientConfigs.SHARED_INSTRUMENT.get())
// Only show play notes in the local range
if (!args.pos.closerThan(MINECRAFT.player.blockPosition(), NoteSound.LOCAL_RANGE))
return;


if (!(MINECRAFT.screen instanceof AbstractInstrumentScreen))
return;
AbstractInstrumentScreen.getCurrentScreen(MINECRAFT).ifPresent((screen) -> {
// The produced instrument sound has to match the current screen's sounds
if (!screen.getInstrumentId().equals(args.instrumentId))
return;

final AbstractInstrumentScreen screen = (AbstractInstrumentScreen) MINECRAFT.screen;
// The instrument sound produced has to match the current screen's sounds
if (!screen.getInstrumentId().equals(args.instrumentId))
return;
// Only show the played note in a close distance
if (!args.pos.closerThan(MINECRAFT.player.blockPosition(), ServerUtil.PLAY_DISTANCE / 3))
return;
try {

screen.getNoteButton(args.noteIdentifier).playNoteAnimation(true);

try {
screen.getNoteButton(args.noteIdentifier).playNoteAnimation(true);
} catch (Exception e) {}
} catch (Exception e) {}
});
}


}

0 comments on commit ee5fcc4

Please sign in to comment.