-
-
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 23 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,24 @@ | ||
| 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> { | ||
|
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(RealisticSoundClientMessage message, MessageContext ctx) { | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
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. naming of args should be |
||
| 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; | ||
|
Desoroxxx marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||
| 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(RealisticSoundMessage message, MessageContext ctx) { | ||||||||||||
|
Desoroxxx marked this conversation as resolved.
Outdated
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. naming of args should be |
||||||||||||
| 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 |
||||||||||||
| EntityPlayer player = ctx.getServerHandler().player; | ||||||||||||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||||||||||||
| World world = player.getEntityWorld(); | ||||||||||||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||||||||||||
| for (EntityPlayer playerInWorld : world.playerEntities) { | ||||||||||||
|
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. Could use a space
Suggested change
|
||||||||||||
| double distance = Math.sqrt(message.getPos().distanceSq(playerInWorld.getPosition())); | ||||||||||||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||||||||||||
| RealisticSound sound = RealisticSound.createSound(message.getSilencer(), message.getSound(), message.getPos(), playerInWorld.getPosition()); | ||||||||||||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||||||||||||
| if (sound.getVolumeIn() > 0.0F) { | ||||||||||||
|
Desoroxxx marked this conversation as resolved.
Outdated
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 |
||||||||||||
| 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 | ||||||||||||
| ); | ||||||||||||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||||||||||||
| }); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| }); | ||||||||||||
| return null; | ||||||||||||
|
Desoroxxx marked this conversation as resolved.
|
||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| 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 class RealisticSoundClientMessage implements IMessage { | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||
|
|
||
| private SoundEvent sound; | ||
| private BlockPos pos; | ||
| private float volume; | ||
| private float pitch; | ||
| private double distance; | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||
|
|
||
| @Override | ||
| public void fromBytes(ByteBuf buf) { | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
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. naming of arg should be |
||
| 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 |
||
|
|
||
| int x = buf.readInt(); | ||
| int y = buf.readInt(); | ||
| int z = buf.readInt(); | ||
| pos = new BlockPos(x, y, z); | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||
| volume = buf.readFloat(); | ||
| pitch = buf.readFloat(); | ||
| distance = buf.readDouble(); | ||
| } | ||
|
Desoroxxx marked this conversation as resolved.
|
||
|
|
||
| @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 |
||
| } | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| 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 class RealisticSoundMessage implements IMessage { | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
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 named |
||
| boolean silencer; | ||
| private SoundEvent sound; | ||
| private BlockPos pos; | ||
| private int dimensionId; | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||
|
|
||
| @Override | ||
| public void fromBytes(ByteBuf buf) { | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
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. arg should be named |
||
| 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 |
||
| int x = buf.readInt(); | ||
| int y = buf.readInt(); | ||
| int z = buf.readInt(); | ||
| pos = new BlockPos(x, y, z); | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||
| dimensionId = buf.readInt(); | ||
| silencer = buf.readBoolean(); | ||
| } | ||
|
|
||
| @Override | ||
| public void toBytes(ByteBuf buf) { | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
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. arg should be named |
||
| 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,24 @@ | ||
| 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 class ClientSoundPlayer { | ||
| @SideOnly(Side.CLIENT) | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
|
||
| 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) | ||
| { | ||
| double d1 = Math.sqrt(distance) / 40.0D; | ||
|
Desoroxxx marked this conversation as resolved.
Outdated
Desoroxxx marked this conversation as resolved.
Outdated
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. Also |
||
| 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); | ||
| } | ||
| } | ||
|
Desoroxxx marked this conversation as resolved.
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.