Guest User

C00PacketSendSound.class

a guest
Oct 2nd, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. /*
  2. ** All credits go to CoolAlias for this simple Packet Handling implemention! Minecraft 1.10, works like a charm!
  3. ** http://www.minecraftforum.net/forums/mapping-and-modding/mapping-and-modding-tutorials/2137055-1-7-x-1-8-customizing-packet-handling-with
  4. */
  5. public class C00PacketSendSound implements IMessage{
  6.  
  7.     private String name;
  8.     private SoundCategory category;
  9.     private double
  10.     x,y,z;
  11.     private float
  12.     volume,pitch;
  13.    
  14.     public C00PacketSendSound() {}
  15.    
  16.     public C00PacketSendSound(ImplSound sound) {
  17.         this.name=sound.getName();
  18.         this.category=sound.getCategory();
  19.         this.x=sound.getX();
  20.         this.y=sound.getY();
  21.         this.z=sound.getZ();
  22.         this.volume=sound.getVolume();
  23.         this.pitch=sound.getPitch();
  24.     }
  25.    
  26.     @Override
  27.     public void fromBytes(ByteBuf buf) {
  28.         this.name=ByteBufUtils.readUTF8String(buf);
  29.         this.category=SoundCategory.getByName(ByteBufUtils.readUTF8String(buf));
  30.         this.x=buf.readDouble();
  31.         this.y=buf.readDouble();
  32.         this.z=buf.readDouble();
  33.         this.volume=buf.readFloat();
  34.         this.pitch=buf.readFloat();
  35.     }
  36.  
  37.     @Override
  38.     public void toBytes(ByteBuf buf) {
  39.         ByteBufUtils.writeUTF8String(buf, name);
  40.         ByteBufUtils.writeUTF8String(buf, category.getName());
  41.         buf.writeDouble(x);
  42.         buf.writeDouble(y);
  43.         buf.writeDouble(z);
  44.         buf.writeFloat(volume);
  45.         buf.writeFloat(pitch);
  46.     }
  47.  
  48.     public static class Handler extends ClientMessageHandler<C00PacketSendSound>{
  49.  
  50.         @Override
  51.         public IMessage handleClientMessage(EntityPlayer player, C00PacketSendSound message, MessageContext ctx) {
  52.             Minecraft.getMinecraft().addScheduledTask(new Runnable() {
  53.                
  54.                 //Just added this line to make sure the method gets called in the Minecraft thread!
  55.                 //Don't know if this is the problem solver or not, but it works now!
  56.                 @Override
  57.                 public void run() {
  58.                     SoundManager sndManager=new SoundManager();
  59.                     ImplSound sound=new ImplSound(message.name, message.category, message.volume, message.pitch, message.x, message.y, message.z);
  60.                     sndManager.playSound(sound);
  61.                 }
  62.             });
  63.             return null;
  64.         }
  65.  
  66.     }
  67.    
  68. }
Advertisement
Add Comment
Please, Sign In to add comment