PacketPlayOutNamedSoundEffect no sound

Discussion in 'Plugin Development' started by jlin13, Dec 5, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    jlin13

    All the code executes fine, packet gets sent, but no sound.

    PHP:
    public static void sendNamedSoundEffect(Player p, Sound soundEffect, float volume, float pitch) {

            try {
        
                Class<?namedSoundPacketClass getNMSClass("PacketPlayOutNamedSoundEffect");
                
    Constructor<?> namedSoundConstructor = namedSoundPacketClass.getConstructor(String.class, double.class, double.class,
                        double.class, float.class, float.class);
                // Volume 0 - 1.0, can be more?, Pitch 0.5 - 2.0
                Object namedSoundPacket = namedSoundConstructor.newInstance(soundEffect.toString().toLowerCase(), p.getLocation().getX(),
                        p.getLocation().getY(), p.getLocation().getZ(), volume, pitch);
                Method sendPacket = getNMSClass("PlayerConnection").getMethod("sendPacket", getNMSClass("Packet"));
        
                sendPacket.invoke(getPlayerConnection(p), namedSoundPacket);
        
            } catch (ClassNotFoundException | NoSuchMethodException | SecurityException |
                    InstantiationException | IllegalAccessException | IllegalArgumentException |
                    InvocationTargetException e) {
                e.printStackTrace();
            }

        }
    getPlayerConnection()
    PHP:
    private static Object getPlayerConnection(Player pthrows SecurityExceptionNoSuchMethodException {
        
            try {
            
                
    Object nmsPlayer getNMSPlayer(p);
                
    Field connectionField nmsPlayer.getClass().getField("playerConnection");
                
    Object playerConnection connectionField.get(nmsPlayer);
            
                return 
    playerConnection;
            
            } catch (
    IllegalAccessException IllegalArgumentException NoSuchFieldException e) {
                
    e.printStackTrace();
            }
        
            return 
    null;
        
        }
    getNMSClass()
    PHP:
    private static Class<?getNMSClass(String nmsClassStringthrows ClassNotFoundException {
         
            
    String version Bukkit.getServer().getClass().getPackage().getName().replace("."",").split(",")[3] + ".";
            
    String name "net.minecraft.server." version nmsClassString;
            Class<
    ?> nmsClass = Class.forName(name);
         
            return nmsClass;
         
        }
    Here's a link to the full code.

    I have a command set up so I can choose packets to send via command /proto <PacketType> <params>.

    When I go in-game and type /proto playoutnamedsound arrow_hit(?) 1.0 1.0, nothing plays! (I've tried other sounds too). Output is perfectly fine

    This is on 1.8
     
    Last edited: Dec 5, 2016
  2. Offline

    Lordloss

    Why dont you use player.playSound?
     
  3. Offline

    jlin13

    The purpose isn't to play a sound - I'm not building a plugin for the sound itself, I'm building it to play with reflection and packets and I couldn't get this one to work
     
  4. Offline

    Zombie_Striker

    @jlin13
    Yes. This values can be higher than 1.0, but it cannot be lower than 0 (from what I can tell, it just re-sets it to 0).

    http://wiki.vg/Protocol#Named_Sound_Effect
    If you look at the XYZ variables, they all need to be multiplied by 8.
     
    jlin13 likes this.
  5. Offline

    jlin13

    I tried multiplying them by 8 when they are passed in to namedSoundPacket and I still don't get sound
     
  6. Offline

    Zombie_Striker

    @jlin13
    Try going to XYZ (0,0,0). At that location, the actual XYZ values should not matter. After that, You should then try changing the soundname, volume, and pitch.

    [Edit] I see in your link you have another packet creator for an animation packet. Does that work? If it does not, there may be an issue with the playerconnection or with the way you are sending the packet.
     
  7. Offline

    jlin13

    Yeah the other one works - I'll try that first and report back
     
Thread Status:
Not open for further replies.

Share This Page