Skip to content

Commit

Permalink
Merge branch 'master' into 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Treetrain1 committed Aug 10, 2023
2 parents 60b2184 + 0cddaef commit 1a4b5bb
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 192 deletions.
115 changes: 8 additions & 107 deletions src/main/java/net/frozenblock/lib/FrozenClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
import net.frozenblock.lib.sound.api.instances.RestrictedStartingSound;
import net.frozenblock.lib.sound.api.instances.distance_based.FadingDistanceSwitchingSound;
import net.frozenblock.lib.sound.api.instances.distance_based.RestrictedMovingFadingDistanceSwitchingSoundLoop;
import net.frozenblock.lib.sound.api.networking.FlyBySoundPacket;
import net.frozenblock.lib.sound.api.networking.LocalPlayerSoundPacket;
import net.frozenblock.lib.sound.api.networking.LocalSoundPacket;
import net.frozenblock.lib.sound.api.networking.MovingRestrictionSoundPacket;
import net.frozenblock.lib.sound.api.predicate.SoundPredicate;
import net.frozenblock.lib.sound.impl.block_sound_group.BlockSoundGroupManager;
import net.frozenblock.lib.spotting_icons.impl.EntitySpottingIconInterface;
Expand Down Expand Up @@ -64,14 +68,13 @@ public void onInitializeClient() {
ClientFreezer.onInitializeClient();
registerClientEvents();

receiveLocalSoundPacket();
receiveMovingRestrictionSoundPacket();
receiveRestrictedMovingSoundLoopPacket();
ClientPlayNetworking.registerGlobalReceiver(LocalSoundPacket.PACKET_TYPE, LocalSoundPacket::receive);
ClientPlayNetworking.registerGlobalReceiver(MovingRestrictionSoundPacket.PACKET_TYPE, MovingRestrictionSoundPacket::receive);
receiveStartingRestrictedMovingSoundLoopPacket();
receiveMovingRestrictionLoopingFadingDistanceSoundPacket();
receiveMovingFadingDistanceSoundPacket();
receiveFadingDistanceSoundPacket();
receiveFlybySoundPacket();
ClientPlayNetworking.registerGlobalReceiver(FlyBySoundPacket.PACKET_TYPE, FlyBySoundPacket::receive);
receiveCooldownChangePacket();
receiveForcedCooldownPacket();
receiveCooldownTickCountPacket();
Expand All @@ -82,7 +85,7 @@ public void onInitializeClient() {
receiveIconPacket();
receiveIconRemovePacket();
receiveWindSyncPacket();
receiveLocalPlayerSoundPacket();
ClientPlayNetworking.registerGlobalReceiver(LocalPlayerSoundPacket.PACKET_TYPE, LocalPlayerSoundPacket::receive);

Panoramas.addPanorama(new ResourceLocation("textures/gui/title/background/panorama"));

Expand All @@ -102,88 +105,6 @@ public void onInitializeClient() {
});
}

private static void receiveLocalSoundPacket() {
ClientPlayNetworking.registerGlobalReceiver(FrozenMain.LOCAL_SOUND_PACKET, (client, handler, buf, responseSender) -> {
double x = buf.readDouble();
double y = buf.readDouble();
double z = buf.readDouble();
SoundEvent sound = buf.readById(BuiltInRegistries.SOUND_EVENT);
SoundSource source = buf.readEnum(SoundSource.class);
float volume = buf.readFloat();
float pitch = buf.readFloat();
boolean distanceDelay = buf.readBoolean();
client.execute(() -> {
ClientLevel level = client.level;
if (level != null) {
level.playLocalSound(x, y, z, sound, source, volume, pitch, distanceDelay);
}
});
});
}

private static void receiveLocalPlayerSoundPacket() {
ClientPlayNetworking.registerGlobalReceiver(FrozenMain.LOCAL_PLAYER_SOUND_PACKET, (ctx, handler, byteBuf, responseSender) -> {
SoundEvent sound = byteBuf.readById(BuiltInRegistries.SOUND_EVENT);
float volume = byteBuf.readFloat();
float pitch = byteBuf.readFloat();
ctx.execute(() -> {
if (ctx.level != null) {
LocalPlayer player = ctx.player;
if (player != null) {
assert sound != null;
ctx.getSoundManager().play(new EntityBoundSoundInstance(sound, SoundSource.PLAYERS, volume, pitch, player, ctx.level.random.nextLong()));
}
}
});
});
}

@SuppressWarnings("unchecked")
private static <T extends Entity> void receiveMovingRestrictionSoundPacket() {
ClientPlayNetworking.registerGlobalReceiver(FrozenMain.MOVING_RESTRICTION_SOUND_PACKET, (ctx, handler, byteBuf, responseSender) -> {
int id = byteBuf.readVarInt();
SoundEvent sound = byteBuf.readById(BuiltInRegistries.SOUND_EVENT);
SoundSource category = byteBuf.readEnum(SoundSource.class);
float volume = byteBuf.readFloat();
float pitch = byteBuf.readFloat();
ResourceLocation predicateId = byteBuf.readResourceLocation();
boolean stopOnDeath = byteBuf.readBoolean();
ctx.execute(() -> {
ClientLevel level = ctx.level;
if (level != null) {
T entity = (T) level.getEntity(id);
if (entity != null) {
SoundPredicate.LoopPredicate<T> predicate = SoundPredicate.getPredicate(predicateId);
ctx.getSoundManager().play(new RestrictedMovingSound<>(entity, sound, category, volume, pitch, predicate, stopOnDeath));
}
}
});
});
}

@SuppressWarnings("unchecked")
private static <T extends Entity> void receiveRestrictedMovingSoundLoopPacket() {
ClientPlayNetworking.registerGlobalReceiver(FrozenMain.MOVING_RESTRICTION_LOOPING_SOUND_PACKET, (ctx, handler, byteBuf, responseSender) -> {
int id = byteBuf.readVarInt();
SoundEvent sound = byteBuf.readById(BuiltInRegistries.SOUND_EVENT);
SoundSource category = byteBuf.readEnum(SoundSource.class);
float volume = byteBuf.readFloat();
float pitch = byteBuf.readFloat();
ResourceLocation predicateId = byteBuf.readResourceLocation();
boolean stopOnDeath = byteBuf.readBoolean();
ctx.execute(() -> {
ClientLevel level = ctx.level;
if (level != null) {
T entity = (T) level.getEntity(id);
if (entity != null) {
SoundPredicate.LoopPredicate<T> predicate = SoundPredicate.getPredicate(predicateId);
ctx.getSoundManager().play(new RestrictedMovingSoundLoop<>(entity, sound, category, volume, pitch, predicate, stopOnDeath));
}
}
});
});
}

@SuppressWarnings("unchecked")
private static <T extends Entity> void receiveStartingRestrictedMovingSoundLoopPacket() {
ClientPlayNetworking.registerGlobalReceiver(FrozenMain.STARTING_RESTRICTION_LOOPING_SOUND_PACKET, (ctx, handler, byteBuf, responseSender) -> {
Expand Down Expand Up @@ -284,26 +205,6 @@ private static void receiveFadingDistanceSoundPacket() {
});
}

private static void receiveFlybySoundPacket() {
ClientPlayNetworking.registerGlobalReceiver(FrozenMain.FLYBY_SOUND_PACKET, (ctx, handler, byteBuf, responseSender) -> {
int id = byteBuf.readVarInt();
SoundEvent sound = byteBuf.readById(BuiltInRegistries.SOUND_EVENT);
SoundSource category = byteBuf.readEnum(SoundSource.class);
float volume = byteBuf.readFloat();
float pitch = byteBuf.readFloat();
ctx.execute(() -> {
ClientLevel level = ctx.level;
if (level != null) {
Entity entity = level.getEntity(id);
if (entity != null) {
FlyBySoundHub.FlyBySound flyBySound = new FlyBySoundHub.FlyBySound(pitch, volume, category, sound);
FlyBySoundHub.addEntity(entity, flyBySound);
}
}
});
});
}

private static void receiveCooldownChangePacket() {
ClientPlayNetworking.registerGlobalReceiver(FrozenMain.COOLDOWN_CHANGE_PACKET, (ctx, handler, byteBuf, responseSender) -> {
Item item = byteBuf.readById(BuiltInRegistries.ITEM);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/net/frozenblock/lib/FrozenMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public void onInitialize() {
//IDENTIFIERS
public static final ResourceLocation FLYBY_SOUND_PACKET = id("flyby_sound_packet");
public static final ResourceLocation LOCAL_SOUND_PACKET = id("local_sound_packet");
public static final ResourceLocation MOVING_RESTRICTION_LOOPING_SOUND_PACKET = id("moving_restriction_looping_sound_packet");
public static final ResourceLocation STARTING_RESTRICTION_LOOPING_SOUND_PACKET = id("starting_moving_restriction_looping_sound_packet");
public static final ResourceLocation MOVING_RESTRICTION_SOUND_PACKET = id("moving_restriction_sound_packet");
public static final ResourceLocation MOVING_RESTRICTION_LOOPING_FADING_DISTANCE_SOUND_PACKET = id("moving_restriction_looping_fading_distance_sound_packet");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public ClientEntityScreenShake(@NotNull Entity entity, float intensity, int dura
}

@Override
public float getIntensity(Vec3 playerPos) {
public float getIntensity(@NotNull Vec3 playerPos) {
if (this.entity != null && !this.entity.isRemoved()) {
this.pos = this.entity.position();
return super.getIntensity(playerPos);
Expand Down
83 changes: 21 additions & 62 deletions src/main/java/net/frozenblock/lib/sound/api/FrozenSoundPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.frozenblock.lib.FrozenMain;
import net.frozenblock.lib.sound.api.networking.FlyBySoundPacket;
import net.frozenblock.lib.sound.api.networking.LocalPlayerSoundPacket;
import net.frozenblock.lib.sound.api.networking.LocalSoundPacket;
import net.frozenblock.lib.sound.api.networking.MovingRestrictionSoundPacket;
import net.frozenblock.lib.sound.impl.EntityLoopingFadingDistanceSoundInterface;
import net.frozenblock.lib.sound.impl.EntityLoopingSoundInterface;
import net.minecraft.core.BlockPos;
Expand All @@ -43,104 +47,62 @@ private FrozenSoundPackets() {

public static void createLocalSound(Level level, BlockPos pos, SoundEvent sound, SoundSource source, float volume, float pitch, boolean distanceDelay) {
if (!level.isClientSide) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeDouble(pos.getX());
byteBuf.writeDouble(pos.getY());
byteBuf.writeDouble(pos.getZ());
byteBuf.writeId(BuiltInRegistries.SOUND_EVENT, sound);
byteBuf.writeEnum(source);
byteBuf.writeFloat(volume);
byteBuf.writeFloat(pitch);
byteBuf.writeBoolean(distanceDelay);
var packet = new LocalSoundPacket(pos.getX(), pos.getY(), pos.getZ(), sound, source, volume, pitch, distanceDelay);
for (ServerPlayer player : PlayerLookup.tracking((ServerLevel) level, pos)) {
ServerPlayNetworking.send(player, FrozenMain.LOCAL_SOUND_PACKET, byteBuf);
ServerPlayNetworking.send(player, packet);
}
}
}

public static void createLocalSound(Level level, double x, double y, double z, SoundEvent sound, SoundSource source, float volume, float pitch, boolean distanceDelay) {
if (!level.isClientSide) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeDouble(x);
byteBuf.writeDouble(y);
byteBuf.writeDouble(z);
byteBuf.writeId(BuiltInRegistries.SOUND_EVENT, sound);
byteBuf.writeEnum(source);
byteBuf.writeFloat(volume);
byteBuf.writeFloat(pitch);
byteBuf.writeBoolean(distanceDelay);
var packet = new LocalSoundPacket(x, y, z, sound, source, volume, pitch, distanceDelay);
for (ServerPlayer player : PlayerLookup.tracking((ServerLevel) level, BlockPos.containing(x, y, z))) {
ServerPlayNetworking.send(player, FrozenMain.LOCAL_SOUND_PACKET, byteBuf);
ServerPlayNetworking.send(player, packet);
}
}
}

public static void createFlybySound(Level world, Entity entity, SoundEvent sound, SoundSource category, float volume, float pitch) {
if (!world.isClientSide) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeVarInt(entity.getId());
byteBuf.writeId(BuiltInRegistries.SOUND_EVENT, sound);
byteBuf.writeEnum(category);
byteBuf.writeFloat(volume);
byteBuf.writeFloat(pitch);
var packet = new FlyBySoundPacket(entity.getId(), sound, category, volume, pitch);
for (ServerPlayer player : PlayerLookup.tracking(entity)) {
ServerPlayNetworking.send(player, FrozenMain.FLYBY_SOUND_PACKET, byteBuf);
ServerPlayNetworking.send(player, packet);
}
if (entity instanceof ServerPlayer serverPlayer) {
ServerPlayNetworking.send(serverPlayer, FrozenMain.FLYBY_SOUND_PACKET, byteBuf);
ServerPlayNetworking.send(serverPlayer, packet);
}
}
}

public static void createMovingRestrictionSound(Level world, Entity entity, SoundEvent sound, SoundSource category, float volume, float pitch, ResourceLocation predicate, boolean stopOnDeath) {
if (!world.isClientSide) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeVarInt(entity.getId());
byteBuf.writeId(BuiltInRegistries.SOUND_EVENT, sound);
byteBuf.writeEnum(category);
byteBuf.writeFloat(volume);
byteBuf.writeFloat(pitch);
byteBuf.writeResourceLocation(predicate);
byteBuf.writeBoolean(stopOnDeath);
var packet = new MovingRestrictionSoundPacket(entity.getId(), sound, category, volume, pitch, predicate, stopOnDeath, false);
for (ServerPlayer player : PlayerLookup.tracking(entity)) {
ServerPlayNetworking.send(player, FrozenMain.MOVING_RESTRICTION_SOUND_PACKET, byteBuf);
ServerPlayNetworking.send(player, packet);
}
if (entity instanceof ServerPlayer serverPlayer) {
ServerPlayNetworking.send(serverPlayer, FrozenMain.MOVING_RESTRICTION_SOUND_PACKET, byteBuf);
ServerPlayNetworking.send(serverPlayer, packet);
}
}
}

public static void createMovingRestrictionLoopingSound(Level world, Entity entity, SoundEvent sound, SoundSource category, float volume, float pitch, ResourceLocation predicate, boolean stopOnDeath) {
if (!world.isClientSide) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeVarInt(entity.getId());
byteBuf.writeId(BuiltInRegistries.SOUND_EVENT, sound);
byteBuf.writeEnum(category);
byteBuf.writeFloat(volume);
byteBuf.writeFloat(pitch);
byteBuf.writeResourceLocation(predicate);
byteBuf.writeBoolean(stopOnDeath);
var packet = new MovingRestrictionSoundPacket(entity.getId(), sound, category, volume, pitch, predicate, stopOnDeath, true);
for (ServerPlayer player : PlayerLookup.tracking(entity)) {
ServerPlayNetworking.send(player, FrozenMain.MOVING_RESTRICTION_LOOPING_SOUND_PACKET, byteBuf);
ServerPlayNetworking.send(player, packet);
}
if (entity instanceof ServerPlayer serverPlayer) {
ServerPlayNetworking.send(serverPlayer, FrozenMain.MOVING_RESTRICTION_LOOPING_SOUND_PACKET, byteBuf);
ServerPlayNetworking.send(serverPlayer, packet);
}
((EntityLoopingSoundInterface)entity).addSound(BuiltInRegistries.SOUND_EVENT.getKey(sound), category, volume, pitch, predicate, stopOnDeath);
}
}

public static void createMovingRestrictionLoopingSound(ServerPlayer player, Entity entity, SoundEvent sound, SoundSource category, float volume, float pitch, ResourceLocation id, boolean stopOnDeath) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeVarInt(entity.getId());
byteBuf.writeId(BuiltInRegistries.SOUND_EVENT, sound);
byteBuf.writeEnum(category);
byteBuf.writeFloat(volume);
byteBuf.writeFloat(pitch);
byteBuf.writeResourceLocation(id);
byteBuf.writeBoolean(stopOnDeath);
ServerPlayNetworking.send(player, FrozenMain.MOVING_RESTRICTION_LOOPING_SOUND_PACKET, byteBuf);
var packet = new MovingRestrictionSoundPacket(entity.getId(), sound, category, volume, pitch, id, stopOnDeath, true);
ServerPlayNetworking.send(player, packet);
}

public static void createMovingRestrictionLoopingFadingDistanceSound(Level world, Entity entity, SoundEvent sound, SoundEvent sound2, SoundSource category, float volume, float pitch, ResourceLocation predicate, boolean stopOnDeath, float fadeDist, float maxDist) {
Expand Down Expand Up @@ -252,11 +214,8 @@ public static void createStartingMovingRestrictionLoopingSound(ServerPlayer play
}

public static void createLocalPlayerSound(ServerPlayer player, SoundEvent sound, float volume, float pitch) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeId(BuiltInRegistries.SOUND_EVENT, sound);
byteBuf.writeFloat(volume);
byteBuf.writeFloat(pitch);
ServerPlayNetworking.send(player, FrozenMain.LOCAL_PLAYER_SOUND_PACKET, byteBuf);
var packet = new LocalPlayerSoundPacket(sound, volume, pitch);
ServerPlayNetworking.send(player, packet);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.frozenblock.lib.sound.api.networking;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.PacketType;
import net.frozenblock.lib.FrozenMain;
import net.frozenblock.lib.sound.api.FlyBySoundHub;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;

public record FlyBySoundPacket(int id, SoundEvent sound, SoundSource category, float volume, float pitch) implements FabricPacket {
public static final PacketType<FlyBySoundPacket> PACKET_TYPE = PacketType.create(FrozenMain.FLYBY_SOUND_PACKET, FlyBySoundPacket::new);

public FlyBySoundPacket(FriendlyByteBuf buf) {
this(
buf.readVarInt(),
buf.readById(BuiltInRegistries.SOUND_EVENT),
buf.readEnum(SoundSource.class),
buf.readFloat(),
buf.readFloat()
);
}

@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(this.id);
buf.writeId(BuiltInRegistries.SOUND_EVENT, this.sound);
buf.writeEnum(this.category);
buf.writeFloat(this.volume);
buf.writeFloat(this.pitch);
}

@Environment(EnvType.CLIENT)
public static void receive(FlyBySoundPacket packet, LocalPlayer player, PacketSender responseSender) {
ClientLevel level = player.clientLevel;
Entity entity = level.getEntity(packet.id());
if (entity != null) {
FlyBySoundHub.FlyBySound flyBySound = new FlyBySoundHub.FlyBySound(packet.pitch(), packet.volume(), packet.category(), packet.sound());
FlyBySoundHub.addEntity(entity, flyBySound);
}
}

@Override
public PacketType<?> getType() {
return PACKET_TYPE;
}
}
Loading

0 comments on commit 1a4b5bb

Please sign in to comment.