How do I create a new Instance of a DataWatcher on a Packet?

Discussion in 'Plugin Development' started by Agentleader1, May 28, 2015.

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

    Agentleader1

    Code:
    Code:
    package com.Agentleader1.DarkThunder;
    
    import java.lang.reflect.Field;
    
    import net.minecraft.server.v1_7_R4.DataWatcher;
    import net.minecraft.server.v1_7_R4.PacketPlayOutEntityDestroy;
    import net.minecraft.server.v1_7_R4.PacketPlayOutSpawnEntity;
    import net.minecraft.server.v1_7_R4.PacketPlayOutSpawnEntityLiving;
    import net.minecraft.server.v1_7_R4.PlayerConnection;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    
    @SuppressWarnings({"deprecation", "unused"})
    public class BossBar {
        private int efjnggermkrkfk30rfv;
        public static void start(){
            Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.instance, new Runnable(){
                public void run(){
                    for(Player p : Bukkit.getOnlinePlayers()){
                        CraftPlayer player = (CraftPlayer) p;
                        PlayerConnection connection = player.getHandle().playerConnection;
                        try {
                            connection.sendPacket(spawnMob(player.getTargetBlock(null, 6).getState().getLocation()));
                        } catch (SecurityException|NoSuchFieldException|IllegalArgumentException|IllegalAccessException e) {}
                        Bukkit.getScheduler().scheduleSyncDelayedTask(Main.instance, new Runnable(){
                            public void run(){
                                connection.sendPacket(destroyEntity(123));
                            }
                        }, 1);
                    }
                }
            }, 20, 5);
        }
        private static PacketPlayOutEntityDestroy destroyEntity(int id)
        {
            return new PacketPlayOutEntityDestroy(id);
        }
        private static PacketPlayOutSpawnEntityLiving spawnMob(Location loc) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
            PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving();
           
             Field a = packet.getClass().getDeclaredField("a");
                Field b = packet.getClass().getDeclaredField("b");
                Field c = packet.getClass().getDeclaredField("c");
                Field d = packet.getClass().getDeclaredField("d");
                Field e = packet.getClass().getDeclaredField("e");
                Field f = packet.getClass().getDeclaredField("f");
                Field g = packet.getClass().getDeclaredField("g");
                Field h = packet.getClass().getDeclaredField("h");
                Field i = packet.getClass().getDeclaredField("i");
                Field j = packet.getClass().getDeclaredField("j");
                Field k = packet.getClass().getDeclaredField("k");
        
                a.setAccessible(true);
                b.setAccessible(true);
                c.setAccessible(true);
                d.setAccessible(true);
                e.setAccessible(true);
                f.setAccessible(true);
                g.setAccessible(true);
                h.setAccessible(true);
                i.setAccessible(true);
                j.setAccessible(true);
                k.setAccessible(true);
        
                a.set(packet, (int) 123); //Entity ID
                b.set(packet, (byte) 64); //Mob type (ID: 64)
                c.set(packet, (int) Math.floor(loc.getBlockX() * 32.0D)); //X positions
                d.set(packet, (int) Math.floor(loc.getBlockY() * 32.0D)); //Y position
                e.set(packet, (int) Math.floor(loc.getBlockZ() * 32.0D)); //Z position
                f.set(packet, (byte) 0); //Pitch
                g.set(packet, (byte) 0); //Head Pitch
                h.set(packet, (byte) 0); //Yaw
        
               
                DataWatcher watcher = new DataWatcher(); //here's the issue, I can't use null for the parameter.
                //watcher.a(0, 0x20); //Flags, 0x20 = invisible
                watcher.a(5, ChatColor.AQUA + "Test"); //Entity name
                watcher.a(6, 0); //Snow name, 1 = show, 0 = don't show
                watcher.a(8, 0); //Firework effects, 0 = none
                watcher.a(16, 1); //Wither health, 300 = full health
        
                try{
                    Field t = PacketPlayOutSpawnEntity.class.getDeclaredField("t");
                    t.setAccessible(true);
                    t.set(packet, watcher);
                    t.setAccessible(false);
                } catch(Exception ex){
                }
            return packet;
        }
    }
    
    How do I create a DataWatcher for a Packet? The only constructor has a parameter for entity, and I can not use null.

    @Comphenix Do you know how to do this without ProtocolLib?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  2. Offline

    I Al Istannen

    @Agentleader1 I haven't really looked in your code, nor I have done sth with packets (without ProtocolLib) before, but maybe spawn an Entity, grab the watcher and remove it? I think I read that somewhere in an ProtocolLib tutorial, but maybe it will work for you too.
     
  3. Offline

    Agentleader1

    @I Al Istannen I'll almost confident it won't work, but I guess its worth a try.
     
  4. Offline

    Agentleader1

    Bump. Looking for alternate solutions.
     
  5. Offline

    I Al Istannen

Thread Status:
Not open for further replies.

Share This Page