Ejemplo n.º 1
0
 public static TargetedDisguise getDisguise(Player observer, Entity entity) {
   UUID entityId = entity.getUniqueId();
   if (futureDisguises.containsKey(entity.getEntityId())) {
     for (TargetedDisguise disguise : futureDisguises.remove(entity.getEntityId())) {
       addDisguise(entityId, disguise);
     }
   }
   if (getDisguises().containsKey(entityId)) {
     for (TargetedDisguise disguise : getDisguises().get(entityId)) {
       if (disguise.canSee(observer)) {
         return disguise;
       }
     }
   }
   return null;
 }
Ejemplo n.º 2
0
 /** Sends entity removal packets, as this disguise was removed */
 public static void destroyEntity(TargetedDisguise disguise) {
   try {
     Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
     if (entityTrackerEntry != null) {
       HashSet trackedPlayers =
           (HashSet)
               ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers")
                   .get(entityTrackerEntry);
       HashSet cloned = (HashSet) trackedPlayers.clone();
       PacketContainer destroyPacket = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY);
       destroyPacket.getIntegerArrays().write(0, new int[] {disguise.getEntity().getEntityId()});
       for (Object p : cloned) {
         Player player = (Player) ReflectionManager.getBukkitEntity(p);
         if (player == disguise.getEntity() || disguise.canSee(player.getName())) {
           ProtocolLibrary.getProtocolManager().sendServerPacket(player, destroyPacket);
         }
       }
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
 }
Ejemplo n.º 3
0
  /** Sends the self disguise to the player */
  public static void sendSelfDisguise(final Player player, final TargetedDisguise disguise) {
    try {
      if (!disguise.isDisguiseInUse()
          || !player.isValid()
          || !player.isOnline()
          || !disguise.isSelfDisguiseVisible()
          || !disguise.canSee(player)) {
        return;
      }
      Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(player);
      if (entityTrackerEntry == null) {
        // A check incase the tracker is null.
        // If it is, then this method will be run again in one tick. Which is when it should be
        // constructed.
        // Else its going to run in a infinite loop hue hue hue..
        // At least until this disguise is discarded
        Bukkit.getScheduler()
            .runTask(
                libsDisguises,
                new Runnable() {
                  public void run() {
                    if (DisguiseAPI.getDisguise(player, player) == disguise) {
                      sendSelfDisguise(player, disguise);
                    }
                  }
                });
        return;
      }
      // Add himself to his own entity tracker
      ((HashSet<Object>)
              ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers")
                  .get(entityTrackerEntry))
          .add(ReflectionManager.getNmsEntity(player));
      ProtocolManager manager = ProtocolLibrary.getProtocolManager();
      // Send the player a packet with himself being spawned
      manager.sendServerPacket(
          player,
          manager
              .createPacketConstructor(PacketType.Play.Server.NAMED_ENTITY_SPAWN, player)
              .createPacket(player));
      WrappedDataWatcher dataWatcher = WrappedDataWatcher.getEntityWatcher(player);
      sendSelfPacket(
          player,
          manager
              .createPacketConstructor(
                  PacketType.Play.Server.ENTITY_METADATA, player.getEntityId(), dataWatcher, true)
              .createPacket(player.getEntityId(), dataWatcher, true));

      boolean isMoving = false;
      try {
        Field field =
            ReflectionManager.getNmsClass("EntityTrackerEntry").getDeclaredField("isMoving");
        field.setAccessible(true);
        isMoving = field.getBoolean(entityTrackerEntry);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
      // Send the velocity packets
      if (isMoving) {
        Vector velocity = player.getVelocity();
        sendSelfPacket(
            player,
            manager
                .createPacketConstructor(
                    PacketType.Play.Server.ENTITY_VELOCITY,
                    player.getEntityId(),
                    velocity.getX(),
                    velocity.getY(),
                    velocity.getZ())
                .createPacket(
                    player.getEntityId(), velocity.getX(), velocity.getY(), velocity.getZ()));
      }

      // Why the hell would he even need this. Meh.
      if (player.getVehicle() != null && player.getEntityId() > player.getVehicle().getEntityId()) {
        sendSelfPacket(
            player,
            manager
                .createPacketConstructor(
                    PacketType.Play.Server.ATTACH_ENTITY, 0, player, player.getVehicle())
                .createPacket(0, player, player.getVehicle()));
      } else if (player.getPassenger() != null
          && player.getEntityId() > player.getPassenger().getEntityId()) {
        sendSelfPacket(
            player,
            manager
                .createPacketConstructor(
                    PacketType.Play.Server.ATTACH_ENTITY, 0, player.getPassenger(), player)
                .createPacket(0, player.getPassenger(), player));
      }

      // Resend the armor
      for (int i = 0; i < 5; i++) {
        ItemStack item;
        if (i == 0) {
          item = player.getItemInHand();
        } else {
          item = player.getInventory().getArmorContents()[i - 1];
        }

        if (item != null && item.getType() != Material.AIR) {
          sendSelfPacket(
              player,
              manager
                  .createPacketConstructor(
                      PacketType.Play.Server.ENTITY_EQUIPMENT, player.getEntityId(), i, item)
                  .createPacket(player.getEntityId(), i, item));
        }
      }
      Location loc = player.getLocation();
      // If the disguised is sleeping for w/e reason
      if (player.isSleeping()) {
        sendSelfPacket(
            player,
            manager
                .createPacketConstructor(
                    PacketType.Play.Server.BED,
                    player,
                    loc.getBlockX(),
                    loc.getBlockY(),
                    loc.getBlockZ())
                .createPacket(player, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
      }

      // Resend any active potion effects
      for (Object potionEffect : player.getActivePotionEffects()) {
        sendSelfPacket(
            player,
            manager
                .createPacketConstructor(
                    PacketType.Play.Server.ENTITY_EFFECT, player.getEntityId(), potionEffect)
                .createPacket(player.getEntityId(), potionEffect));
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Ejemplo n.º 4
0
 /** Resends the entity to all the watching players, which is where the magic begins */
 public static void refreshTrackers(final TargetedDisguise disguise) {
   if (disguise.getEntity().isValid()) {
     PacketContainer destroyPacket = getDestroyPacket(disguise.getEntity().getEntityId());
     try {
       if (selfDisguised.contains(disguise.getEntity().getUniqueId())
           && disguise.isDisguiseInUse()) {
         removeSelfDisguise((Player) disguise.getEntity());
         ProtocolLibrary.getProtocolManager()
             .sendServerPacket((Player) disguise.getEntity(), destroyPacket);
         Bukkit.getScheduler()
             .scheduleSyncDelayedTask(
                 libsDisguises,
                 new Runnable() {
                   public void run() {
                     try {
                       DisguiseUtilities.sendSelfDisguise((Player) disguise.getEntity(), disguise);
                     } catch (Exception ex) {
                       ex.printStackTrace();
                     }
                   }
                 },
                 2);
       }
       final Object entityTrackerEntry =
           ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
       if (entityTrackerEntry != null) {
         HashSet trackedPlayers =
             (HashSet)
                 ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers")
                     .get(entityTrackerEntry);
         Method clear =
             ReflectionManager.getNmsMethod(
                 "EntityTrackerEntry", "clear", ReflectionManager.getNmsClass("EntityPlayer"));
         final Method updatePlayer =
             ReflectionManager.getNmsMethod(
                 "EntityTrackerEntry",
                 "updatePlayer",
                 ReflectionManager.getNmsClass("EntityPlayer"));
         HashSet cloned = (HashSet) trackedPlayers.clone();
         for (final Object p : cloned) {
           Player player = (Player) ReflectionManager.getBukkitEntity(p);
           if (disguise.getEntity() != player && disguise.canSee(player.getName())) {
             clear.invoke(entityTrackerEntry, p);
             ProtocolLibrary.getProtocolManager().sendServerPacket(player, destroyPacket);
             Bukkit.getScheduler()
                 .scheduleSyncDelayedTask(
                     libsDisguises,
                     new Runnable() {
                       public void run() {
                         try {
                           updatePlayer.invoke(entityTrackerEntry, p);
                         } catch (Exception ex) {
                           ex.printStackTrace();
                         }
                       }
                     },
                     2);
           }
         }
       }
     } catch (Exception ex) {
       ex.printStackTrace();
     }
   }
 }