Skip to content

Commit

Permalink
Added STOP_ALL_SOUNDS, RECEIVE_SOUND (event) and several client value…
Browse files Browse the repository at this point in the history
…s regarding the event
  • Loading branch information
XTerPL committed Sep 27, 2022
1 parent aac8d5d commit caab6b8
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
package io.github.techstreet.dfscript.event;

import io.github.techstreet.dfscript.event.system.CancellableEvent;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.network.packet.s2c.play.PlaySoundFromEntityS2CPacket;
import net.minecraft.network.packet.s2c.play.PlaySoundIdS2CPacket;
import net.minecraft.network.packet.s2c.play.PlaySoundS2CPacket;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;

public class RecieveSoundEvent implements CancellableEvent {
private boolean cancelled = false;
private final PlaySoundS2CPacket packet;

public RecieveSoundEvent(PlaySoundS2CPacket packet) {
this.packet = packet;
private final SoundInstance sound;

public RecieveSoundEvent(SoundInstance sound) {
this.sound = sound;
}

public Identifier getSoundId() {
return sound.getId();
}
public float getVolume() {
return sound.getVolume();
}

public PlaySoundS2CPacket getPacket() {
return packet;
public float getPitch() {
return sound.getPitch();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
import java.net.InetSocketAddress;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.s2c.play.DisconnectS2CPacket;
import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket;
import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket;
import net.minecraft.network.packet.s2c.play.PlaySoundS2CPacket;
import net.minecraft.network.packet.s2c.play.TeamS2CPacket;
import net.minecraft.network.packet.s2c.play.*;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand Down Expand Up @@ -94,7 +90,7 @@ private void onDisconnect(DisconnectS2CPacket packet, CallbackInfo ci) {
EventManager.getInstance().dispatch(event);
}

@Inject(method = "onPlaySound", at = @At("HEAD"), cancellable = true)
/*@Inject(method = "onPlaySound", at = @At("HEAD"), cancellable = true)
private void onPlaySound(PlaySoundS2CPacket packet, CallbackInfo ci) {
RecieveSoundEvent event = new RecieveSoundEvent(packet);
EventManager.getInstance().dispatch(event);
Expand All @@ -103,4 +99,24 @@ private void onPlaySound(PlaySoundS2CPacket packet, CallbackInfo ci) {
ci.cancel();
}
}
@Inject(method = "onPlaySoundFromEntity", at = @At("HEAD"), cancellable = true)
private void onPlaySoundFromEntity(PlaySoundFromEntityS2CPacket packet, CallbackInfo ci) {
RecieveSoundEvent event = new RecieveSoundEvent(packet);
EventManager.getInstance().dispatch(event);
if (event.isCancelled()) {
ci.cancel();
}
}
@Inject(method = "onPlaySoundId", at = @At("HEAD"), cancellable = true)
private void onPlaySoundId(PlaySoundIdS2CPacket packet, CallbackInfo ci) {
RecieveSoundEvent event = new RecieveSoundEvent(packet);
EventManager.getInstance().dispatch(event);
if (event.isCancelled()) {
ci.cancel();
}
}*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.techstreet.dfscript.mixin.sound;

import io.github.techstreet.dfscript.event.RecieveSoundEvent;
import io.github.techstreet.dfscript.event.system.EventManager;
import io.github.techstreet.dfscript.util.chat.ChatUtil;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.client.sound.SoundSystem;
import net.minecraft.network.packet.s2c.play.PlaySoundIdS2CPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(SoundSystem.class)
public class MSoundSystem {
@Inject(method = "play", at = @At("HEAD"), cancellable = true)
public void play(SoundInstance sound, CallbackInfo ci) {
RecieveSoundEvent event = new RecieveSoundEvent(sound);
EventManager.getInstance().dispatch(event);

if (event.isCancelled()) {
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.github.techstreet.dfscript.DFScript;
import io.github.techstreet.dfscript.event.BuildModeEvent;
import io.github.techstreet.dfscript.event.DevModeEvent;
import io.github.techstreet.dfscript.event.HudRenderEvent;
import io.github.techstreet.dfscript.event.KeyPressEvent;
import io.github.techstreet.dfscript.event.PlayModeEvent;
import io.github.techstreet.dfscript.event.ReceiveChatEvent;
import io.github.techstreet.dfscript.event.SendChatEvent;
import io.github.techstreet.dfscript.event.TickEvent;
import io.github.techstreet.dfscript.event.*;
import io.github.techstreet.dfscript.event.system.Event;
import io.github.techstreet.dfscript.event.system.EventManager;
import io.github.techstreet.dfscript.loader.Loadable;
Expand Down Expand Up @@ -202,6 +195,7 @@ private void loadEvents() {
manager.register(BuildModeEvent.class, this::handleEvent);
manager.register(DevModeEvent.class, this::handleEvent);
manager.register(HudRenderEvent.class, this::handleEvent);
manager.register(RecieveSoundEvent.class, this::handleEvent);
}

public void handleEvent(Event event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,12 @@ public enum ScriptActionType {
}
})),

STOP_ALL_SOUNDS(builder -> builder.name("Stop All Sounds")
.description("Stops all sounds.")
.icon(Items.COAL)
.category(ScriptActionCategory.VISUALS)
.action(ctx -> DFScript.MC.getSoundManager().stopAll())),

DISPLAY_TITLE(builder -> builder.name("Display Title")
.description("Displays a title.")
.icon(Items.WARPED_SIGN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.github.techstreet.dfscript.DFScript;
import io.github.techstreet.dfscript.event.KeyPressEvent;
import io.github.techstreet.dfscript.event.ReceiveChatEvent;
import io.github.techstreet.dfscript.event.RecieveSoundEvent;
import io.github.techstreet.dfscript.event.SendChatEvent;
import io.github.techstreet.dfscript.event.system.Event;
import io.github.techstreet.dfscript.script.action.ScriptActionArgument.ScriptActionArgumentType;
Expand Down Expand Up @@ -137,7 +138,31 @@ public enum ScriptClientValueArgument implements ScriptArgument {
(event, context) -> new ScriptTextValue(DFScript.PLAYER_UUID)),

PLAYER_NAME("Player Name", "The name of the player.", Items.PLAYER_HEAD, ScriptActionArgumentType.TEXT,
(event, context) -> new ScriptTextValue(DFScript.PLAYER_NAME));
(event, context) -> new ScriptTextValue(DFScript.PLAYER_NAME)),

EVENT_SOUND("ReceivedSound", "The ID of the sound. (OnReceiveSound)", Items.NAUTILUS_SHELL, ScriptActionArgumentType.TEXT, (event, context) -> {
if(event instanceof RecieveSoundEvent e) {
return new ScriptTextValue(e.getSoundId().toString().replaceAll("^minecraft:", ""));
} else {
throw new IllegalStateException("The event is not a receive sound event.");
}
}),

EVENT_VOLUME("ReceivedSoundVolume", "The volume of the sound received. (OnReceiveSound)", Items.NOTE_BLOCK, ScriptActionArgumentType.NUMBER, (event, context) -> {
if(event instanceof RecieveSoundEvent e) {
return new ScriptNumberValue(e.getVolume());
} else {
throw new IllegalStateException("The event is not a receive sound event.");
}
}),

EVENT_PITCH("ReceivedSoundPitch", "The pitch of the sound received. (OnReceiveSound)", Items.JUKEBOX, ScriptActionArgumentType.NUMBER, (event, context) -> {
if(event instanceof RecieveSoundEvent e) {
return new ScriptNumberValue(e.getPitch());
} else {
throw new IllegalStateException("The event is not a receive sound event.");
}
});

private final String name;
private final ItemStack icon;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package io.github.techstreet.dfscript.script.event;

import io.github.techstreet.dfscript.event.BuildModeEvent;
import io.github.techstreet.dfscript.event.DevModeEvent;
import io.github.techstreet.dfscript.event.HudRenderEvent;
import io.github.techstreet.dfscript.event.KeyPressEvent;
import io.github.techstreet.dfscript.event.PlayModeEvent;
import io.github.techstreet.dfscript.event.ReceiveChatEvent;
import io.github.techstreet.dfscript.event.SendChatEvent;
import io.github.techstreet.dfscript.event.TickEvent;
import io.github.techstreet.dfscript.event.*;
import io.github.techstreet.dfscript.event.system.Event;
import io.github.techstreet.dfscript.script.menu.ScriptMenuClickButtonEvent;
import net.minecraft.item.Item;
Expand Down Expand Up @@ -39,7 +32,9 @@ public enum ScriptEventType {

OVERLAY_EVENT(HudRenderEvent.class, "OnOverlay", "Executed when the overlay is being rendered.", Items.GREEN_STAINED_GLASS_PANE),

MENU_BUTTON_EVENT(ScriptMenuClickButtonEvent.class, "OnMenuButtonClick", "Executed when a player clicks a button inside a custom menu.", Items.CHISELED_STONE_BRICKS);
MENU_BUTTON_EVENT(ScriptMenuClickButtonEvent.class, "OnMenuButtonClick", "Executed when a player clicks a button inside a custom menu.", Items.CHISELED_STONE_BRICKS),

RECEIVE_SOUND(RecieveSoundEvent.class, "OnReceiveSound", "Executed when a player receives a sound", Items.NAUTILUS_SHELL);

private final String name;
private final ItemStack icon;
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/dfscript.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"player.MLocalPlayer",
"render.MInGameHUD",
"render.MMultiplayerScreen",
"render.MOptionsScreen"
"render.MOptionsScreen",
"sound.MSoundSystem"
],
"client": [
],
Expand Down

0 comments on commit caab6b8

Please sign in to comment.