Skip to content

Commit

Permalink
Removed unnecessary code paths to improve readability.
Browse files Browse the repository at this point in the history
- also prevented duplicate songs and added a music trigger on dimension-change.
  • Loading branch information
0xTas committed Jan 21, 2024
1 parent 09de770 commit ab9bed9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/main/java/dev/stardust/mixin/MinecraftClientMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void mixinGetMusicType(CallbackInfoReturnable<MusicSound> cir) {
MusicTweaks tweaks = Modules.get().get(MusicTweaks.class);

if (tweaks == null || !tweaks.isActive()) return;
MusicSound type = tweaks.getTypes();
MusicSound type = tweaks.getType();

if (type != null) {
cir.setReturnValue(type);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/dev/stardust/mixin/SoundSystemMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private void mixinTick(CallbackInfo ci) {
if (source == null) continue;

playing = true;
tweaks.setCurrentSong(sound.toString());
if (tweaks.isActive() && !tweaks.randomPitch()) {
this.dirtyPitch = true;
source.setPitch(1.0f + tweaks.getPitchAdjustment());
Expand Down Expand Up @@ -79,9 +80,10 @@ private void mixinTick(CallbackInfo ci) {
if (tweaks.isActive() && this.totalTicksPlaying % 30 == 0 && tweaks.shouldDisplayNowPlaying() && songID != null) {
if (this.totalTicksPlaying <= 90 || !tweaks.shouldFadeOut()) {
String songName = tweaks.getSongName(songID);

// See NarratorManagerMixin.java lol
switch (tweaks.getDisplayMode()) {
case Chat -> tweaks.sendNowPlayingMessage(songName);
// See NarratorManagerMixin.java lol
case Record -> tweaks.getClient().inGameHud.setRecordPlayingOverlay(Text.of(songName));
}
}
Expand Down
129 changes: 51 additions & 78 deletions src/main/java/dev/stardust/modules/MusicTweaks.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import meteordevelopment.meteorclient.settings.*;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.mixininterface.IChatHud;
import meteordevelopment.meteorclient.events.packets.PacketEvent;
import meteordevelopment.meteorclient.events.game.GameJoinedEvent;
import net.minecraft.network.packet.s2c.play.PlayerRespawnS2CPacket;


/**
Expand Down Expand Up @@ -675,10 +677,8 @@ public enum DisplayType {
);


// See MusicTrackerMixin.java && SoundSystemMixin.java && WeightedSoundSetMixin.java
// && MinecraftClientMixin.java
@Nullable
public MusicSound getTypes() {
// See MinecraftClientMixin.java
public MusicSound getType() {
if (this.currentType != null) return this.currentType;

int min;
Expand All @@ -693,82 +693,17 @@ public MusicSound getTypes() {
min = minTimeUntilNextSong.get() * 20;
max = maxTimeUntilNextSong.get() * 20;
}

if (max <= min) max = min + 1;
List<MusicSound> types = new ArrayList<>();
if (minecraft.get() || clark.get() || sweden.get() ||subwooferLullaby.get() || livingMice.get() || haggstrom.get()
|| danny.get() || key.get() || oxygene.get() || dryHands.get() || wetHands.get() || miceOnVenus.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_GAME, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (aerie.get() || firebugs.get() || labyrinthine.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_OVERWORLD_SWAMP, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (aFamiliarRoom.get() || anOrdinaryDay.get() || echoInTheWind.get() || floatingDream.get() || leftToBloom.get()
|| oneMoreDay.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_OVERWORLD_LUSH_CAVES, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (wending.get() || standTall.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_OVERWORLD_STONY_PEAKS, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (ancestry.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_OVERWORLD_DEEP_DARK, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (infiniteAmethyst.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_OVERWORLD_GROVE, min, ThreadLocalRandom.current().nextInt(min, max), false));
types.add(new MusicSound(SoundEvents.MUSIC_OVERWORLD_DRIPSTONE_CAVES, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (axolotl.get() || dragonFish.get() || shuniji.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_UNDER_WATER, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (concreteHalls.get() || deadVoxel.get() || warmth.get() || balladOfTheCats.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_NETHER_BASALT_DELTAS, min, ThreadLocalRandom.current().nextInt(min, max), false));
types.add(new MusicSound(SoundEvents.MUSIC_NETHER_CRIMSON_FOREST, min, ThreadLocalRandom.current().nextInt(min, max), false));
types.add(new MusicSound(SoundEvents.MUSIC_NETHER_NETHER_WASTES, min, ThreadLocalRandom.current().nextInt(min, max), false));
types.add(new MusicSound(SoundEvents.MUSIC_NETHER_SOUL_SAND_VALLEY, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (chrysopoeia.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_NETHER_CRIMSON_FOREST, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (rubedo.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_NETHER_NETHER_WASTES, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (soBelow.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_NETHER_BASALT_DELTAS, min, ThreadLocalRandom.current().nextInt(min, max), false));
types.add(new MusicSound(SoundEvents.MUSIC_NETHER_SOUL_SAND_VALLEY, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (bromeliad.get() || crescentDunes.get() || mutation.get() || moogCity2.get() || beginning2.get() || floatingTrees.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_MENU, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (alpha.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_CREDITS, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (theEnd.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_END, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (boss.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_DRAGON, min, ThreadLocalRandom.current().nextInt(min, max), false));
}
if (record5.get() || record11.get() || record13.get() || recordCat.get() || recordChirp.get() || recordBlocks.get()
|| recordFar.get() || recordMall.get() || recordMellohi.get() || recordStal.get() || recordStrad.get()
|| recordWard.get() || recordWait.get() || recordOtherside.get() || recordPigstep.get() || recordRelic.get()) {
types.add(new MusicSound(SoundEvents.MUSIC_GAME, min, ThreadLocalRandom.current().nextInt(min, max), false));
}

MusicSound type;
if (!types.isEmpty()) {
if (types.size() > 1) {
type = types.get(ThreadLocalRandom.current().nextInt(types.size()));
} else {
type = types.get(0);
}
} else {
return null;
}
// It doesn't matter which SoundEvents.MUSIC_??? we return since the WeightedSoundSet is overwritten directly now.
// actually I lied tho don't use the music disc events, or it won't work (see WeightedSoundSetMixin.java)
MusicSound type = new MusicSound(SoundEvents.MUSIC_GAME, min, ThreadLocalRandom.current().nextInt(min, max), false);

this.currentType = type;
return type;
}

// See SoundSystemMixin.java
public String getSongName(String songID) {
String songName;
switch (songID) {
Expand Down Expand Up @@ -845,6 +780,7 @@ public String getSongName(String songID) {
return songName;
}

// See WeightedSoundSetMixin.java
public List<String> getSoundSet() {
List<String> ids = new ArrayList<>();
if (minecraft.get()) ids.add("minecraft:music/game/calm1");
Expand Down Expand Up @@ -915,9 +851,21 @@ public List<String> getSoundSet() {
if (recordPigstep.get()) ids.add("minecraft:records/pigstep");
if (recordRelic.get()) ids.add("minecraft:records/relic");

// Prevent duplicates
if (this.currentSong != null && ids.size() > 1) {
for (int n = 0; n < ids.size(); n++) {
if (this.currentSong.equals("Sound["+ids.get(n)+"]")) {
ids.remove(n);
this.currentSong = null;
break;
}
}
}

return ids;
}

// See SoundSystemMixin.java
public float getNextPitchStep(float currentPitch) {
if (this.lastDirection == null) {
this.lastDirection = PitchDirection.Descending;
Expand Down Expand Up @@ -963,17 +911,21 @@ public void sendNowPlayingMessage(String songName) {
);
}

// See MusicTrackerMixin.java
public void nullifyCurrentType() {
this.currentType = null;
this.rcc = StardustUtil.rCC();
}

// See SoundSystemMixin.java
public MinecraftClient getClient() { return this.mc; }
public boolean shouldFadeOut() { return fadeOut.get(); }
public boolean randomPitch() { return randomPitch.get(); }
public boolean trippyPitch() { return trippyPitchSetting.get(); }
public float getVolumeAdjustment() { return volume.get() / 100f; }
public boolean overrideDelay() { return overrideDelayMode.get(); }
public DisplayType getDisplayMode() { return displayTypeSetting.get(); }
public void setCurrentSong(@Nullable String id) { this.currentSong = id; }
public int getTimeUntilNextSong() { return timeUntilNextSong.get() * 20; }
public float getPitchAdjustment() { return pitchAdjustment.get() / 1000f; }
public boolean shouldDisplayNowPlaying() { return displayNowPlaying.get(); }
Expand All @@ -983,6 +935,10 @@ private enum PitchDirection {
Ascending, Descending
}

@Nullable
private String lastDim = null;
@Nullable
private String currentSong = null;
@Nullable
private MusicSound currentType = null;
@Nullable
Expand All @@ -992,9 +948,9 @@ private enum PitchDirection {

@Override
public void onActivate() {
MusicSound type = this.getTypes();
if (!startOnEnable.get()) return;

if (type == null || !startOnEnable.get()) return;
MusicSound type = this.getType();
if (((MusicTrackerAccessor) mc.getMusicTracker()).getCurrent() == null) mc.getMusicTracker().play(type);
}

Expand All @@ -1006,15 +962,32 @@ public void onDeactivate() {

@EventHandler
private void onGameJoin(GameJoinedEvent event) {
MusicSound type = this.getTypes();

if (type == null) return;
SoundInstance instance = ((MusicTrackerAccessor) mc.getMusicTracker()).getCurrent();
if (instance != null) {
MusicSound type = this.getType();
if (type != mc.getMusicType()) {
mc.getMusicTracker().stop();
mc.getMusicTracker().play(type);
}
}

if (mc.world != null) {
this.lastDim = mc.world.getDimensionKey().toString();
}
}

@EventHandler
private void onDimensionChange(PacketEvent.Receive event) {
if (!(event.packet instanceof PlayerRespawnS2CPacket packet)) return;

if (this.lastDim != null) {
if (!packet.getDimensionType().toString().equals(this.lastDim)) {
MusicSound type = this.getType();

mc.getMusicTracker().stop();
mc.getMusicTracker().play(type);
this.lastDim = packet.getDimensionType().toString();
}
}
}
}

0 comments on commit ab9bed9

Please sign in to comment.