Skip to content

Commit f9141cf

Browse files
committed
Moved sound play logic to using network packets
1 parent 4467ec3 commit f9141cf

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod_id=ae2addonlib
1616
mod_name=AE2AddonLib
1717
mod_license=LGPL-3.0
1818
mod_license_url=https://github.com/pedroksl/AE2AddonLib/blob/main/LICENSE
19-
mod_version=1.0.3-1.20.1
19+
mod_version=1.0.4-1.20.1
2020
mod_url=https://github.com/pedroksl/AE2AddonLib
2121
version_type=release
2222
mod_group_id=net.pedroksl.ae2addonlib

src/main/java/net/pedroksl/ae2addonlib/api/IFluidTankHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import net.minecraftforge.common.capabilities.ForgeCapabilities;
1010
import net.minecraftforge.fluids.FluidStack;
1111
import net.minecraftforge.fluids.capability.IFluidHandler;
12-
import net.pedroksl.ae2addonlib.client.widgets.FluidTankSlot;
12+
import net.pedroksl.ae2addonlib.network.LibNetworkHandler;
13+
import net.pedroksl.ae2addonlib.network.clientPacket.FluidTankClientAudioPacket;
1314

1415
import appeng.api.config.Actionable;
1516
import appeng.api.stacks.AEFluidKey;
@@ -95,7 +96,7 @@ default void onItemUse(int index, int button) {
9596
setCarriedItem(cap.getContainer());
9697

9798
if (inserted > 0) {
98-
FluidTankSlot.playDownSound(true);
99+
LibNetworkHandler.INSTANCE.sendTo(new FluidTankClientAudioPacket(true), getServerPlayer());
99100
}
100101
}
101102
} else {
@@ -121,7 +122,7 @@ default void onItemUse(int index, int button) {
121122

122123
setCarriedItem(cap.getContainer());
123124

124-
FluidTankSlot.playDownSound(false);
125+
LibNetworkHandler.INSTANCE.sendTo(new FluidTankClientAudioPacket(false), getServerPlayer());
125126
}
126127
}
127128
}

src/main/java/net/pedroksl/ae2addonlib/api/IFluidTankScreen.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.pedroksl.ae2addonlib.api;
22

33
import net.minecraftforge.fluids.FluidStack;
4+
import net.pedroksl.ae2addonlib.client.widgets.FluidTankSlot;
45

56
/**
67
* Marks a screen as compatible with {@link net.pedroksl.ae2addonlib.client.widgets.FluidTankSlot}.
@@ -13,4 +14,12 @@ public interface IFluidTankScreen {
1314
* @param stack The tank's updated stack.
1415
*/
1516
void updateFluidTankContents(int index, FluidStack stack);
17+
18+
/**
19+
* Method called when a successful interaction wants to play a sound on the client.
20+
* @param isInsert Decides if the sound is of an insertion or extraction.
21+
*/
22+
default void playDownSound(boolean isInsert) {
23+
FluidTankSlot.playDownSound(isInsert);
24+
}
1625
}

src/main/java/net/pedroksl/ae2addonlib/network/LibNetworkHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.pedroksl.ae2addonlib.network;
22

33
import net.pedroksl.ae2addonlib.AE2AddonLib;
4+
import net.pedroksl.ae2addonlib.network.clientPacket.FluidTankClientAudioPacket;
45
import net.pedroksl.ae2addonlib.network.clientPacket.FluidTankStackUpdatePacket;
56
import net.pedroksl.ae2addonlib.network.clientPacket.OutputDirectionUpdatePacket;
67
import net.pedroksl.ae2addonlib.network.serverPacket.AddonConfigButtonPacket;
@@ -23,7 +24,7 @@ public class LibNetworkHandler extends NetworkHandler {
2324

2425
@Override
2526
public void init() {
26-
// registerPacket(FluidTankClientAudioPacket.class, FluidTankClientAudioPacket::new);
27+
registerPacket(FluidTankClientAudioPacket.class, FluidTankClientAudioPacket::new);
2728
registerPacket(FluidTankStackUpdatePacket.class, FluidTankStackUpdatePacket::new);
2829
registerPacket(OutputDirectionUpdatePacket.class, OutputDirectionUpdatePacket::new);
2930

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
package net.pedroksl.ae2addonlib.network.clientPacket;
22

3+
import net.minecraft.client.Minecraft;
4+
import net.minecraft.network.FriendlyByteBuf;
5+
import net.minecraft.world.entity.player.Player;
6+
import net.pedroksl.ae2addonlib.api.IFluidTankScreen;
7+
import net.pedroksl.ae2addonlib.network.AddonPacket;
8+
39
/**
4-
* Record used to define the packet used to tell the client to trigger sound from a {@link net.pedroksl.ae2addonlib.client.widgets.FluidTankSlot} interaction.
5-
* @param isInsert Defines if the sound to be played is of an insertion or extraction.
10+
* Class used to define the packet used to tell the client to trigger sound from a {@link net.pedroksl.ae2addonlib.client.widgets.FluidTankSlot} interaction.
611
*/
7-
public record FluidTankClientAudioPacket(boolean isInsert) { // implements ClientboundPacket {
8-
9-
// @Override
10-
// public void handleOnClient(IPayloadContext context) {
11-
// if (Minecraft.getInstance().screen instanceof IFluidTankScreen screen) {
12-
// FluidTankSlot.playDownSound(isInsert);
13-
// }
14-
// }
12+
public class FluidTankClientAudioPacket extends AddonPacket {
13+
14+
private final boolean isInsert;
15+
16+
/**
17+
* Constructs the packet from data in the stream.
18+
* @param stream The data stream.
19+
*/
20+
public FluidTankClientAudioPacket(FriendlyByteBuf stream) {
21+
this.isInsert = stream.readBoolean();
22+
}
23+
24+
/**
25+
* Constructs the packet to send to the stream.
26+
* @param isInsert Defines if the sound to be played is of an insertion or extraction.
27+
*/
28+
public FluidTankClientAudioPacket(boolean isInsert) {
29+
this.isInsert = isInsert;
30+
}
31+
32+
@Override
33+
protected void write(FriendlyByteBuf stream) {
34+
stream.writeBoolean(isInsert);
35+
}
36+
37+
@Override
38+
public void clientPacketData(Player player) {
39+
if (Minecraft.getInstance().screen instanceof IFluidTankScreen screen) {
40+
screen.playDownSound(isInsert);
41+
}
42+
}
1543
}

0 commit comments

Comments
 (0)