-
-
Notifications
You must be signed in to change notification settings - Fork 64
RealisticSound Try #551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
RealisticSound Try #551
Changes from all commits
b8942a2
5db987a
76fa5c7
f8601f5
f61b4e0
094e896
98c8d38
7e4de1d
b9a507a
72d3aa7
b3eac1b
42e3f20
e950e25
904d277
1b5994b
1a4ce5e
4cafc53
1dd2c45
4d38923
a5803bb
a0babf9
84cd5e9
c41ee0c
e427716
bb5fa53
c8842f0
46a925b
3c905e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.paneedah.mwc.network.handlers; | ||
|
|
||
| import com.paneedah.mwc.network.messages.RealisticSoundClientMessage; | ||
| import com.paneedah.weaponlib.sound.ClientSoundPlayer; | ||
| import net.minecraft.client.Minecraft; | ||
| import net.minecraft.util.SoundCategory; | ||
| import net.minecraftforge.fml.common.network.simpleimpl.IMessage; | ||
| import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; | ||
| import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; | ||
|
|
||
| public class RealisticSoundClientHandler implements IMessageHandler<RealisticSoundClientMessage, IMessage> { | ||
|
|
||
| @Override | ||
| public IMessage onMessage(final RealisticSoundClientMessage message, final MessageContext ctx) { | ||
| Minecraft.getMinecraft().addScheduledTask(() -> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| Minecraft minecraft = Minecraft.getMinecraft(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the constant |
||
| if (minecraft.world != null) { | ||
| ClientSoundPlayer csp = new ClientSoundPlayer(); | ||
| csp.PlaySoundClient(message.getDistance(), message.getSound(), SoundCategory.PLAYERS, message.getVolume(), message.getPitch(), (float)message.getPos().getX(), (float)message.getPos().getY(), (float)message.getPos().getZ()); | ||
|
Comment on lines
+18
to
+19
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case using a local variable is useless as you aren't using it twice, simply do Although for better performance get rid of |
||
| } | ||
|
Desoroxxx marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.paneedah.mwc.network.handlers; | ||
|
|
||
| import com.paneedah.mwc.network.messages.RealisticSoundClientMessage; | ||
| import com.paneedah.mwc.network.messages.RealisticSoundMessage; | ||
| import com.paneedah.weaponlib.sound.RealisticSound; | ||
| import net.minecraft.entity.player.EntityPlayer; | ||
| import net.minecraft.entity.player.EntityPlayerMP; | ||
| import net.minecraft.world.World; | ||
| import net.minecraftforge.fml.common.network.simpleimpl.IMessage; | ||
| import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; | ||
| import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import static com.paneedah.mwc.MWC.CHANNEL; | ||
|
|
||
| public class RealisticSoundHandler implements IMessageHandler<RealisticSoundMessage, IMessage> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
|
|
||
| @Override | ||
| public IMessage onMessage(final RealisticSoundMessage message, final MessageContext ctx) { | ||
| ctx.getServerHandler().player.getServerWorld().addScheduledTask(() -> { | ||
|
strubium marked this conversation as resolved.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| final EntityPlayer player = ctx.getServerHandler().player; | ||
| final World world = player.getEntityWorld(); | ||
| for (EntityPlayer playerInWorld : world.playerEntities) { | ||
| final double distance = Math.sqrt(message.getPos().distanceSq(playerInWorld.getPosition())); | ||
| final RealisticSound sound = RealisticSound.createSound(message.getSilencer(), message.getSound(), message.getPos(), playerInWorld.getPosition()); | ||
| if (sound.getVolumeIn() > 0) { | ||
| if (playerInWorld instanceof EntityPlayerMP) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use an early return |
||
| EntityPlayerMP playerMP = (EntityPlayerMP) playerInWorld; | ||
This comment was marked as off-topic.
Sorry, something went wrong. |
||
| playerMP.getServerWorld().addScheduledTask(() -> { | ||
| CHANNEL.sendTo(new RealisticSoundClientMessage(sound.getSound(), message.getPos(), sound.getVolumeIn(), sound.getPitchIn(), distance), playerMP); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package com.paneedah.mwc.network.messages; | ||
|
|
||
| import io.netty.buffer.ByteBuf; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import net.minecraft.util.ResourceLocation; | ||
| import net.minecraft.util.SoundEvent; | ||
| import net.minecraft.util.math.BlockPos; | ||
| import net.minecraftforge.fml.common.network.simpleimpl.IMessage; | ||
|
|
||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public final class RealisticSoundClientMessage implements IMessage { | ||
|
|
||
| private float volume; | ||
| private float pitch; | ||
|
|
||
| private double distance; | ||
|
|
||
| private SoundEvent sound; | ||
| private BlockPos pos; | ||
|
|
||
| @Override | ||
| public void fromBytes(final ByteBuf buf) { | ||
| int length = buf.readInt(); | ||
| byte[] soundBytes = new byte[length]; | ||
| buf.readBytes(soundBytes); | ||
| String soundName = new String(soundBytes); | ||
| sound = SoundEvent.REGISTRY.getObject(new ResourceLocation(soundName)); | ||
|
Comment on lines
+28
to
+32
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use sound event ID instead |
||
|
|
||
| final int x = buf.readInt(); | ||
| final int y = buf.readInt(); | ||
| final int z = buf.readInt(); | ||
| pos = new BlockPos(x, y, z); | ||
|
|
||
| volume = buf.readFloat(); | ||
| pitch = buf.readFloat(); | ||
| distance = buf.readDouble(); | ||
| } | ||
|
|
||
| @Override | ||
| public void toBytes(ByteBuf buf) { | ||
| String soundName = sound.getRegistryName().toString(); | ||
| byte[] soundBytes = soundName.getBytes(); | ||
| buf.writeInt(soundBytes.length); | ||
| buf.writeBytes(soundBytes); | ||
|
Comment on lines
+46
to
+49
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use sound event ID instead |
||
|
|
||
| buf.writeInt(pos.getX()); | ||
| buf.writeInt(pos.getY()); | ||
| buf.writeInt(pos.getZ()); | ||
|
|
||
| buf.writeFloat(volume); | ||
| buf.writeFloat(pitch); | ||
| buf.writeDouble(distance); | ||
| } | ||
|
Desoroxxx marked this conversation as resolved.
|
||
|
|
||
| public SoundEvent getSound() {return this.sound;} | ||
| public BlockPos getPos() { return this.pos;} | ||
| public float getVolume() {return this.volume;} | ||
| public float getPitch() {return this.pitch;} | ||
| public double getDistance() {return this.distance;} | ||
|
Desoroxxx marked this conversation as resolved.
Comment on lines
+60
to
+64
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Useless because of the |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package com.paneedah.mwc.network.messages; | ||
|
|
||
| import io.netty.buffer.ByteBuf; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import net.minecraft.util.ResourceLocation; | ||
| import net.minecraft.util.SoundEvent; | ||
| import net.minecraft.util.math.BlockPos; | ||
| import net.minecraftforge.fml.common.network.simpleimpl.IMessage; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public final class RealisticSoundMessage implements IMessage { | ||
| private boolean silencer; | ||
|
|
||
| private int dimensionId; | ||
|
|
||
| private SoundEvent sound; | ||
| private BlockPos pos; | ||
|
|
||
| @Override | ||
| public void fromBytes(final ByteBuf buf) { | ||
| int length = buf.readInt(); | ||
| byte[] soundBytes = new byte[length]; | ||
| buf.readBytes(soundBytes); | ||
| String soundName = new String(soundBytes); | ||
| sound = SoundEvent.REGISTRY.getObject(new ResourceLocation(soundName)); | ||
|
Desoroxxx marked this conversation as resolved.
Comment on lines
+25
to
+29
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use sound event ID instead |
||
| final int x = buf.readInt(); | ||
| final int y = buf.readInt(); | ||
| final int z = buf.readInt(); | ||
| pos = new BlockPos(x, y, z); | ||
|
|
||
| dimensionId = buf.readInt(); | ||
| silencer = buf.readBoolean(); | ||
| } | ||
|
|
||
| @Override | ||
| public void toBytes(final ByteBuf buf) { | ||
| String soundName = sound.getRegistryName().toString(); | ||
| byte[] soundBytes = soundName.getBytes(); | ||
|
Desoroxxx marked this conversation as resolved.
|
||
| buf.writeInt(soundBytes.length); | ||
| buf.writeBytes(soundBytes); | ||
|
Comment on lines
+41
to
+44
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use sound event ID instead |
||
|
|
||
| buf.writeInt(pos.getX()); | ||
| buf.writeInt(pos.getY()); | ||
| buf.writeInt(pos.getZ()); | ||
|
Desoroxxx marked this conversation as resolved.
|
||
|
|
||
| buf.writeInt(dimensionId); | ||
| buf.writeBoolean(silencer); | ||
| } | ||
| public SoundEvent getSound() {return this.sound;} | ||
| public BlockPos getPos() { return this.pos;} | ||
| public int getWorldId() {return this.dimensionId;} | ||
| public boolean getSilencer() {return this.silencer;} | ||
|
Comment on lines
+53
to
+56
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Useless because of the |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.paneedah.weaponlib.sound; | ||
|
|
||
| import net.minecraft.client.Minecraft; | ||
| import net.minecraft.client.audio.ISound; | ||
| import net.minecraft.client.audio.PositionedSoundRecord; | ||
| import net.minecraft.util.SoundCategory; | ||
| import net.minecraft.util.SoundEvent; | ||
| import net.minecraftforge.fml.relauncher.Side; | ||
| import net.minecraftforge.fml.relauncher.SideOnly; | ||
|
|
||
| public final class ClientSoundPlayer { | ||
|
|
||
| @SideOnly(Side.CLIENT) | ||
| public void PlaySoundClient(double distance, SoundEvent soundIn, SoundCategory categoryIn, float volumeIn, float pitchIn, float xIn, float yIn, float zIn) { | ||
|
Comment on lines
+13
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be camelCase
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final, too much to do in GitHub |
||
| Minecraft mc = Minecraft.getMinecraft(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the constant |
||
| PositionedSoundRecord positionedsoundrecord = new PositionedSoundRecord(soundIn.getSoundName(), categoryIn, volumeIn, pitchIn, false, 0, ISound.AttenuationType.NONE, xIn,yIn, zIn); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final |
||
| if (distance > 100.0D) | ||
| { | ||
| final double d1 = Math.sqrt(distance) / 40D; | ||
| mc.getSoundHandler().playDelayedSound(positionedsoundrecord, (int)(d1 * 20.0D)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double mathed, you first diveded it by 40 and then multiplied by 20, justice for poor CPU |
||
| } else { | ||
| mc.getSoundHandler().playSound(positionedsoundrecord); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.paneedah.weaponlib.sound; | ||
|
|
||
| import net.minecraft.util.SoundCategory; | ||
| import net.minecraft.util.SoundEvent; | ||
| import net.minecraft.util.math.BlockPos; | ||
|
|
||
| public final class RealisticSound { | ||
|
|
||
| private static final double MAX_DISTANCE = 400D; | ||
| private static final double MAX_DISTANCE_SILENCER = 100D; | ||
|
|
||
| private final float volumeIn; | ||
| private final float pitchIn; | ||
|
|
||
| private final SoundEvent sound; | ||
|
|
||
| public RealisticSound(SoundEvent sound, float volumeIn, float pitchIn) { | ||
| this.sound = sound; | ||
| this.volumeIn = volumeIn; | ||
| this.pitchIn = pitchIn; | ||
| } | ||
|
Comment on lines
+17
to
+21
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use an |
||
|
|
||
| public static RealisticSound createSound(Boolean silencer, SoundEvent soundIn, BlockPos origin, BlockPos playercoord) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final (too much for github) |
||
| double distance = Math.sqrt(playercoord.distanceSq(origin)); | ||
| float volume = (float) adjustVolumeForDistance(silencer, distance); | ||
| return new RealisticSound(soundIn, volume, 1.0F); | ||
|
Comment on lines
+24
to
+26
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. local variables are unecessary here |
||
| } | ||
|
|
||
| public static double adjustVolumeForDistance(final boolean silencer, final double distance) { | ||
| final double maxDistance = silencer ? MAX_DISTANCE_SILENCER : MAX_DISTANCE; | ||
| if (distance >= maxDistance) | ||
| return 0; | ||
|
|
||
| if (distance <= 0) | ||
| return 1; | ||
|
|
||
| return 1 - (distance / maxDistance); | ||
| } | ||
|
|
||
| public SoundEvent getSound() { | ||
| return sound; | ||
| } | ||
|
|
||
| public float getVolumeIn() { | ||
| return volumeIn; | ||
| } | ||
|
|
||
| public float getPitchIn() { | ||
| return pitchIn; | ||
| } | ||
|
Comment on lines
+40
to
+50
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
RealisticSoundClientMessageHandler, also should befinal