daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import com.mojang.authlib.GameProfile;
  2. import com.mojang.authlib.properties.Property;
  3. import lombok.Getter;
  4. import net.minecraft.server.v1_8_R3.*;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Location;
  7. import org.bukkit.Material;
  8. import org.bukkit.block.BlockFace;
  9. import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity;
  10. import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
  11. import org.bukkit.craftbukkit.v1_8_R3.util.CraftChatMessage;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.EventHandler;
  14. import org.bukkit.event.Listener;
  15. import org.bukkit.event.player.PlayerQuitEvent;
  16. import org.bukkit.metadata.FixedMetadataValue;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.UUID;
  21.  
  22. /**
  23.  * @author Lukas
  24.  */
  25. public class NPC extends ReflectUtils implements Listener {
  26.  
  27.     private static int currentEntityID = -100;
  28.  
  29.     private static int nextID() {
  30.         currentEntityID--;
  31.         return currentEntityID;
  32.     }
  33.  
  34.     private Location location;
  35.     @Getter
  36.     private int entityID;
  37.     private GameProfile gameProfile;
  38.     private String displayName;
  39.  
  40.     private Look look;
  41.     @Getter
  42.     private Runnable runnable;
  43.  
  44.  
  45.     public NPC(Location location) {
  46.         Bukkit.getPluginManager().registerEvents(this, NPCPlugin.getPlugin());
  47.         String value = "eyJ0aW1lc3RhbXAiOjE1MTgzNDE0MDU4ODAsInByb2ZpbGVJZCI6IjM2M2EyMjQ0MThmMTQxMjU5NGVjMDc4MzdmZjhmNjFmIiwicHJvZmlsZU5hbWUiOiJTaG9ydEJ5dGUiLCJzaWduYXR1cmVSZXF1aXJlZCI6dHJ1ZSwidGV4dHVyZXMiOnsiU0tJTiI6eyJ1cmwiOiJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlLzE2N2FlMWIxNmJmZjRjMmVkNDcxYzkwZDQ3Y2U1OTIwNzZkZTUwYzcwM2YxMmVkOTc1YjM1NWVjNGQ3MCJ9fX0=";
  48.         String signature = "JfG+tPMwrKtN2nS7CXbkxOkgYJQ0IUsGkl4yP9OG5zAND1ODKxXDUMrw0jHS5kOTuQ6pHA7tAqdSZ96DlU1oBxoE/skSdRNIXzplNA/RqdKuFbVFl9ifuRCMfVN2kPBB5UQFsPDvaXY0vG5KNQSW0ORFRp1U7l+twVj9Ynuxc8aeaVJBIPnL97NdSfxFxXZ4xNrfOiF4OpDQvT2coB9ro05WkjOxFGQetP5Ne6UHn2YNMVPrmxVFMcwDIrC8nh1zIIZMr8R9szmaYmfivjtgr0Wcvgl+LS17T5CbmbQxWa77MdUH9gtSg6CbThdxlHlyNFs7qSqHNAPDjY8OrH43jZstDQBvHisRGinveuKvU33N3I0lfaZFBeUx3BeRZiESMx0BK+QKSv1TR+9wI2hdHEoEhfg/63b/DB37dhwKDwT2cpCWl4hvyDIqE4QRotWAQS762XB++k0MC0HLEtq6D2M+8WkXehDNHoELWByWGFRnOEQs4jNPr4GjmS7i2uIcCNaRplbgiouPQIw5crM0wNHZarOjdb5UaC7yqcISnlOUkQgosS234LVPspbSFlVq6nHhIEDEGAKGA6N8Wpuo8iLmcmRjuJsgcYDVR9cK0365x9bdrq6D8CY0La7tPbToo+8VpaWgod9X4qhRNfyYq02HLtXeAQ8OgiYhXHf38F0=";
  49.  
  50.         this.location = location;
  51.         this.entityID = nextID();
  52.         this.gameProfile = new GameProfile(UUID.randomUUID(), "ShortByte ยง8[");
  53.         this.gameProfile.getProperties().put("textures", new Property("textures", value, signature));
  54.  
  55.         this.look = Look.INDIVIDUAL;
  56.  
  57.         this.displayName = "ShortByte ยง8[";
  58.  
  59.         this.runnable = () -> {
  60.             for (Player p : Bukkit.getOnlinePlayers()) {
  61.                 boolean canSee = p.getWorld().equals(location.getWorld()) && p.getLocation().distanceSquared(location) < 7000;
  62.                 if (canSee && !p.hasMetadata("npc_" + this.entityID)) {
  63.                     sendPacket(p, getSpawnPackets());
  64.                     Bukkit.getScheduler().runTaskLater(NPCPlugin.getPlugin(), () -> {
  65.                         sendPacket(p, rideEntity(NPCPlugin.getPlugin().getArmorStand().getEntityId()));
  66.                     }, 2);
  67.                     p.setMetadata("npc_" + this.entityID, new FixedMetadataValue(NPCPlugin.getPlugin(), true));
  68.                 } else if (!canSee && p.hasMetadata("npc_" + this.entityID)) {
  69.                     sendPacket(p, getRemovePackets());
  70.                     p.removeMetadata("npc_" + this.entityID, NPCPlugin.getPlugin());
  71.                 } else if (canSee) {
  72.                     sendPacket(p, getLookPackets(p.getLocation()));
  73.                     // location.add(0, 0, 0.1);
  74.                     //  sendPacket(p, getMovePacket());
  75.                 }
  76.             }
  77.         };
  78.     }
  79.  
  80.     @EventHandler
  81.     public void onQuit(PlayerQuitEvent event) {
  82.         if (event.getPlayer().hasMetadata("npc_" + this.entityID))
  83.             event.getPlayer().removeMetadata("npc_" + this.entityID, NPCPlugin.getPlugin());
  84.     }
  85.  
  86.     public Packet<?>[] getSpawnPackets() {
  87.         PacketPlayOutPlayerInfo tablistPacket = new PacketPlayOutPlayerInfo();
  88.         PacketPlayOutPlayerInfo tablistRemovePacket = new PacketPlayOutPlayerInfo();
  89.  
  90.         PacketPlayOutNamedEntitySpawn spawnPacket = new PacketPlayOutNamedEntitySpawn();
  91.  
  92.         try {
  93.             PacketPlayOutPlayerInfo.PlayerInfoData playerInfoData = tablistPacket.new PlayerInfoData(gameProfile, 1, WorldSettings.EnumGamemode.NOT_SET, CraftChatMessage.fromString(displayName)[0]);
  94.             setField(tablistPacket, "a", PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER);
  95.             setField(tablistPacket, "b", Collections.singletonList(playerInfoData));
  96.  
  97.             PacketPlayOutPlayerInfo.PlayerInfoData playerInfoDataRemove = tablistRemovePacket.new PlayerInfoData(gameProfile, 1, WorldSettings.EnumGamemode.NOT_SET, CraftChatMessage.fromString(displayName)[0]);
  98.             setField(tablistRemovePacket, "a", PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER);
  99.             setField(tablistRemovePacket, "b", Collections.singletonList(playerInfoDataRemove));
  100.  
  101.             DataWatcher watcher = new DataWatcher(null);
  102.             watcher.a(6, (float) 20);
  103.             watcher.a(10, (byte) 127);
  104.  
  105.             setField(spawnPacket, "a", this.entityID);
  106.             setField(spawnPacket, "b", this.gameProfile.getId());
  107.             setField(spawnPacket, "c", MathHelper.floor(location.getX() * 32.0D));
  108.             setField(spawnPacket, "d", MathHelper.floor(location.getY() * 32.0D));
  109.             setField(spawnPacket, "e", MathHelper.floor(location.getZ() * 32.0D));
  110.             setField(spawnPacket, "f", (byte) ((int) (location.getYaw() * 256.0F / 360.0F)));
  111.             setField(spawnPacket, "g", (byte) ((int) (location.getPitch() * 256.0F / 360.0F)));
  112.             setField(spawnPacket, "h", 0);
  113.  
  114.             setField(spawnPacket, "i", watcher);
  115.         } catch (Exception ex) {
  116.             ex.printStackTrace();
  117.         }
  118.         return new Packet<?>[]{tablistPacket, spawnPacket};
  119.     }
  120.  
  121.     public Packet<?>[] getRemovePackets() {
  122.         PacketPlayOutEntityDestroy destroy = new PacketPlayOutEntityDestroy(entityID);
  123.         return new Packet[]{destroy};
  124.     }
  125.  
  126.     public Packet<?>[] getLookPackets(Location point) {
  127.         Location clone = this.location.clone();
  128.         final double xDiff = point.getX() - this.location.getX();
  129.         final double yDiff = point.getY() - this.location.getY();
  130.         final double zDiff = point.getZ() - this.location.getZ();
  131.  
  132.         final double DistanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
  133.         final double DistanceY = Math.sqrt(DistanceXZ * DistanceXZ + yDiff * yDiff);
  134.  
  135.         double newYaw = Math.acos(xDiff / DistanceXZ) * 180 / Math.PI;
  136.         final double newPitch = Math.acos(yDiff / DistanceY) * 180 / Math.PI - 90;
  137.  
  138.         if (zDiff < 0.0)
  139.             newYaw = newYaw + Math.abs(180 - newYaw) * 2;
  140.         float yy = ((float) (newYaw - 90));
  141.         float pp = ((float) newPitch);
  142.         if (yy != this.location.getYaw() || pp != this.location.getPitch()) {
  143.             clone.setYaw(yy);
  144.             clone.setPitch(pp);
  145.         }
  146.  
  147.         PacketPlayOutEntity.PacketPlayOutEntityLook packet = new PacketPlayOutEntity.PacketPlayOutEntityLook(this.entityID, (byte) ((int) (clone.getYaw() * 256.0F / 360.0F)), (byte) ((int) (clone.getPitch() * 256.0F / 360.0F)), true);
  148.         PacketPlayOutEntityHeadRotation headPacket = new PacketPlayOutEntityHeadRotation();
  149.         try {
  150.             setField(headPacket, "a", this.entityID);
  151.             setField(headPacket, "b", (byte) ((int) (clone.getYaw() * 256.0F / 360.0F)));
  152.         } catch (Exception ex) {
  153.             ex.printStackTrace();
  154.         }
  155.         return new Packet[]{packet, headPacket};
  156.     }
  157.  
  158.     public Packet<?>[] rideEntity(int id) {
  159.         PacketPlayOutAttachEntity packetPlayOutAttachEntity = new PacketPlayOutAttachEntity();
  160.  
  161.         try {
  162.             setField(packetPlayOutAttachEntity, "a", 0);
  163.             setField(packetPlayOutAttachEntity, "b", entityID);
  164.             setField(packetPlayOutAttachEntity, "c", id);
  165.         } catch (Exception ex) {
  166.             ex.printStackTrace();
  167.         }
  168.         return new Packet[]{packetPlayOutAttachEntity};
  169.     }
  170.  
  171.     public Packet<?>[] getMovePacket() {
  172.         boolean onGround = location.getBlock().getRelative(BlockFace.DOWN).getType() != Material.AIR;
  173.         PacketPlayOutEntityTeleport packetPlayOutEntityTeleport = new PacketPlayOutEntityTeleport(entityID, MathHelper.floor(location.getX() * 32.0D), MathHelper.floor(location.getY() * 32.0D), MathHelper.floor(location.getZ() * 32.0D),
  174.                 (byte) ((int) (location.getYaw() * 256.0F / 360.0F)), (byte) ((int) (location.getYaw() * 256.0F / 360.0F)), onGround);
  175.  
  176.         return new Packet[]{packetPlayOutEntityTeleport};
  177.     }
  178.  
  179.     public void sendPacket(Player player, Packet<?>[] packets) {
  180.         for (Packet<?> packet : packets)
  181.             ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
  182.     }
  183.  
  184.     public void interact(Player player){}
  185.  
  186.     public enum Look {
  187.         INDIVIDUAL,
  188.         NONE,
  189.         NEAREST;
  190.     }
  191. }
RAW Paste Data
Top