Skip to content

Commit 7462b8f

Browse files
committed
Add blockhit sound
1 parent eb0a505 commit 7462b8f

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Better Combat weapon attribute range is now only applied when a weapon has no modifier for vanilla range
55
- Added function to reformat `Entity Interaction Range` to `Attack Range` on weapon tooltips
66
- Knockback reduction for fast attacks is now stronger (configurable)
7+
- Added block hit sound upon swinging at blocks
78

89
# 2.1.1
910

common/src/main/java/net/bettercombat/mixin/client/MinecraftClientInject.java

+9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public abstract class MinecraftClientInject implements MinecraftClient_BetterCom
5757

5858
@Shadow @Final public InGameHud inGameHud;
5959

60+
@Shadow @Nullable public HitResult crosshairTarget;
61+
6062
private MinecraftClient thisClient() {
6163
return (MinecraftClient)((Object)this);
6264
}
@@ -345,6 +347,13 @@ private void performAttack() {
345347
updateTargetsInReach(targets);
346348
if(targets.size() == 0) {
347349
PlatformClient.onEmptyLeftClick(player);
350+
351+
if (crosshairTarget.getType() == BLOCK) {
352+
var blockHitResult = (BlockHitResult) crosshairTarget;
353+
var pos = blockHitResult.getBlockPos();
354+
var packet = new Packets.C2S_BlockHit(pos);
355+
Platform.networkC2S_Send(packet);
356+
}
348357
}
349358

350359
// Mimic logic of:

common/src/main/java/net/bettercombat/network/Packets.java

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.minecraft.network.codec.PacketCodec;
1111
import net.minecraft.network.packet.CustomPayload;
1212
import net.minecraft.util.Identifier;
13+
import net.minecraft.util.math.BlockPos;
1314

1415
import java.util.ArrayList;
1516
import java.util.List;
@@ -146,6 +147,17 @@ public Id<? extends CustomPayload> getId() {
146147
}
147148
}
148149

150+
public record C2S_BlockHit(BlockPos pos) implements CustomPayload {
151+
public static Identifier ID = Identifier.of(BetterCombatMod.ID, "block_hit");
152+
public static final CustomPayload.Id<C2S_BlockHit> PACKET_ID = new CustomPayload.Id<>(ID);
153+
public static final PacketCodec<PacketByteBuf, C2S_BlockHit> CODEC = BlockPos.PACKET_CODEC.xmap(C2S_BlockHit::new, C2S_BlockHit::pos).cast();
154+
155+
@Override
156+
public Id<? extends CustomPayload> getId() {
157+
return PACKET_ID;
158+
}
159+
}
160+
149161
public record ConfigSync(String json) implements CustomPayload {
150162
public static Identifier ID = Identifier.of(BetterCombatMod.ID, "config_sync");
151163
public static final CustomPayload.Id<ConfigSync> PACKET_ID = new CustomPayload.Id<>(ID);

common/src/main/java/net/bettercombat/network/ServerNetwork.java

+15
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,19 @@ public static void handleAttackRequest(Packets.C2S_AttackRequest request, Minecr
209209
});
210210
});
211211
}
212+
213+
public static void handleBlockHit(Packets.C2S_BlockHit packet, MinecraftServer server, ServerPlayerEntity player) {
214+
var world = player.getWorld();
215+
if (world == null) {
216+
return;
217+
}
218+
var block = world.getBlockState(packet.pos());
219+
if (block == null || block.isAir()) {
220+
return;
221+
}
222+
var soundGroup = block.getSoundGroup();
223+
if (soundGroup != null) {
224+
world.playSound(null, packet.pos().getX(), packet.pos().getY(), packet.pos().getZ(), soundGroup.getHitSound(), player.getSoundCategory(), 1.0F, 1.0F);
225+
}
226+
}
212227
}

fabric/src/main/java/net/bettercombat/fabric/network/FabricServerNetwork.java

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static void init() {
5858
PayloadTypeRegistry.playS2C().register(Packets.AttackAnimation.PACKET_ID, Packets.AttackAnimation.CODEC);
5959
PayloadTypeRegistry.playC2S().register(Packets.AttackAnimation.PACKET_ID, Packets.AttackAnimation.CODEC);
6060
PayloadTypeRegistry.playC2S().register(Packets.C2S_AttackRequest.PACKET_ID, Packets.C2S_AttackRequest.CODEC);
61+
PayloadTypeRegistry.playC2S().register(Packets.C2S_BlockHit.PACKET_ID, Packets.C2S_BlockHit.CODEC);
6162

6263
ServerPlayNetworking.registerGlobalReceiver(Packets.AttackAnimation.PACKET_ID, (packet, context) -> {
6364
ServerNetwork.handleAttackAnimation(packet, context.server(), context.player());
@@ -66,6 +67,10 @@ public static void init() {
6667
ServerPlayNetworking.registerGlobalReceiver(Packets.C2S_AttackRequest.PACKET_ID, (packet, context) -> {
6768
ServerNetwork.handleAttackRequest(packet, context.server(), context.player(), context.player().networkHandler);
6869
});
70+
71+
ServerPlayNetworking.registerGlobalReceiver(Packets.C2S_BlockHit.PACKET_ID, (packet, context) -> {
72+
ServerNetwork.handleBlockHit(packet, context.server(), context.player());
73+
});
6974
}
7075

7176
public record ConfigurationTask(String configString) implements ServerPlayerConfigurationTask {

neoforge/src/main/java/net/bettercombat/neoforge/network/NetworkEvents.java

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public static void register(final RegisterPayloadHandlersEvent event) {
4848
ServerNetwork.handleAttackRequest(packet, server, player, vanillaHandler);
4949
});
5050

51+
registrar.playToServer(Packets.C2S_BlockHit.PACKET_ID, Packets.C2S_BlockHit.CODEC, (packet, context) -> {
52+
var player = (ServerPlayerEntity)context.player();
53+
var server = player.server;
54+
ServerNetwork.handleBlockHit(packet, server, player);
55+
});
56+
5157
// Shared play stage
5258

5359
registrar.playBidirectional(Packets.AttackAnimation.PACKET_ID, Packets.AttackAnimation.CODEC, (packet, context) -> {

0 commit comments

Comments
 (0)