Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1.  
  2. Save New Duplicate & Edit Just Text Twitter
  3. >
  4. package de.bitcraft.pathfinding;
  5.  
  6. import com.google.common.reflect.Reflection;
  7. import com.mojang.authlib.GameProfile;
  8. import com.mojang.authlib.properties.Property;
  9. import net.minecraft.server.v1_8_R3.*;
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.Chunk;
  12. import org.bukkit.Location;
  13. import org.bukkit.World;
  14. import org.bukkit.craftbukkit.v1_8_R3.CraftServer;
  15. import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
  16. import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
  17. import org.bukkit.craftbukkit.v1_8_R3.util.CraftChatMessage;
  18. import org.bukkit.entity.Entity;
  19. import org.bukkit.entity.Player;
  20.  
  21. import java.util.*;
  22.  
  23. /**
  24. * Created by Robin on 25.11.2017.
  25. */
  26. public class NPC extends Reflections {
  27.  
  28. MinecraftServer minecraftServer;
  29.  
  30. WorldServer worldServer;
  31.  
  32. EntityPlayer entityPlayer;
  33. GameProfile gameProfile;
  34.  
  35. public NPC(String name, Location location) {
  36. minecraftServer = ((CraftServer) Bukkit.getServer()).getServer();
  37. worldServer = ((CraftWorld) location.getWorld()).getHandle();
  38. gameProfile = new GameProfile(UUID.randomUUID(), name);
  39.  
  40. entityPlayer = new EntityPlayer(minecraftServer, worldServer, gameProfile, new PlayerInteractManager(worldServer));
  41. Player player = entityPlayer.getBukkitEntity().getPlayer();
  42. player.setPlayerListName("");
  43. entityPlayer.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
  44.  
  45. }
  46.  
  47. public void changeSkin(String value, String signature) {
  48. gameProfile.getProperties().put("textures", new Property("textures", value, signature));
  49. }
  50. public void move(Location loc) {
  51. entityPlayer.move(loc.getX(), loc.getY(), loc.getZ());
  52. /* PacketPlayOutEntity.PacketPlayOutRelEntityMoveLook packet = new PacketPlayOutEntity.PacketPlayOutRelEntityMoveLook(this.getEntityID(),
  53. (byte)(int)Math.floor((loc.getX()-this.getLocation().getX())*32.0D),
  54. (byte)(int)Math.floor((loc.getY()-this.getLocation().getY())*32.0D),
  55. (byte)(int)Math.floor((loc.getZ()-this.getLocation().getZ())*32.0D),
  56. (byte)loc.getYaw(), (byte)loc.getPitch(), false);
  57. for(Player player : Bukkit.getOnlinePlayers()) {
  58. sendPacket(packet, player);
  59. }*/
  60. }
  61.  
  62. public Player getPlayer() {
  63. return entityPlayer.getBukkitEntity().getPlayer();
  64. }
  65.  
  66. public void spawn(Player player) {
  67. PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn = new PacketPlayOutNamedEntitySpawn();
  68. setValue(packetPlayOutNamedEntitySpawn, "a", entityPlayer.getBukkitEntity().getEntityId());
  69. setValue(packetPlayOutNamedEntitySpawn, "b", gameProfile.getId());
  70. setValue(packetPlayOutNamedEntitySpawn, "c", getFixLocation(getLocation().getX()));
  71. setValue(packetPlayOutNamedEntitySpawn, "d", getFixLocation(getLocation().getY()));
  72. setValue(packetPlayOutNamedEntitySpawn, "e", getFixLocation(getLocation().getZ()));
  73. setValue(packetPlayOutNamedEntitySpawn, "f", getFixRotation(getLocation().getYaw()));
  74. setValue(packetPlayOutNamedEntitySpawn, "g", getFixRotation(getLocation().getPitch()));
  75. setValue(packetPlayOutNamedEntitySpawn, "h", 0);
  76. DataWatcher dataWatcher = new DataWatcher(entityPlayer);
  77. dataWatcher.a(10, (byte)127);
  78. dataWatcher.a(6, (float) 20);
  79. setValue(packetPlayOutNamedEntitySpawn, "i", dataWatcher);
  80. addToTablist(player);
  81. sendPacket(packetPlayOutNamedEntitySpawn, player);
  82.  
  83. headRotation(getFixRotation(getLocation().getYaw()), getFixRotation(getLocation().getPitch()), player);
  84. }
  85.  
  86. public void despawn(Player player) {
  87. PacketPlayOutEntityDestroy packetPlayOutEntityDestroy = new PacketPlayOutEntityDestroy(new int[] {entityPlayer.getBukkitEntity().getEntityId()});
  88. removeToTabList(player);
  89. sendPacket(packetPlayOutEntityDestroy, player);
  90. }
  91.  
  92. public void addToTablist(Player player) {
  93. PacketPlayOutPlayerInfo packetPlayOutPlayerInfo = new PacketPlayOutPlayerInfo();
  94. PacketPlayOutPlayerInfo.PlayerInfoData playerInfoData = packetPlayOutPlayerInfo.new PlayerInfoData(gameProfile, 1, WorldSettings.EnumGamemode.NOT_SET, CraftChatMessage.fromString(gameProfile.getName())[0]);
  95. List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List <PacketPlayOutPlayerInfo.PlayerInfoData>) getValue(packetPlayOutPlayerInfo, "b");
  96. players.add(playerInfoData);
  97.  
  98. setValue(packetPlayOutPlayerInfo, "a", PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER);
  99. setValue(packetPlayOutPlayerInfo, "b", players);
  100.  
  101. sendPacket(packetPlayOutPlayerInfo, player);
  102. }
  103.  
  104. public void removeToTabList(Player player) {
  105. PacketPlayOutPlayerInfo packetPlayOutPlayerInfo = new PacketPlayOutPlayerInfo();
  106. PacketPlayOutPlayerInfo.PlayerInfoData playerInfoData = packetPlayOutPlayerInfo.new PlayerInfoData(gameProfile, 1, WorldSettings.EnumGamemode.NOT_SET, CraftChatMessage.fromString(gameProfile.getName())[0]);
  107. List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List <PacketPlayOutPlayerInfo.PlayerInfoData>) getValue(packetPlayOutPlayerInfo, "b");
  108. players.add(playerInfoData);
  109.  
  110. setValue(packetPlayOutPlayerInfo, "a", PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER);
  111. setValue(packetPlayOutPlayerInfo, "b", players);
  112.  
  113. sendPacket(packetPlayOutPlayerInfo, player);
  114. }
  115.  
  116. public void teleport(Location location, Player player){
  117. PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport();
  118. setValue(packet, "a", entityPlayer.getBukkitEntity().getEntityId());
  119. setValue(packet, "b", getFixLocation(location.getX()));
  120. setValue(packet, "c", getFixLocation(location.getY()));
  121. setValue(packet, "d", getFixLocation(location.getZ()));
  122. setValue(packet, "e", getFixRotation(location.getYaw()));
  123. setValue(packet, "f", getFixRotation(location.getPitch()));
  124. setValue(packet, "g", !this.getLocation().getBlock().isEmpty());
  125. sendPacket(packet, player);
  126. headRotation(location.getYaw(), location.getPitch(), player);
  127. }
  128.  
  129. private Entity getEntityByUniqueId(UUID uuid) {
  130. for(World world : Bukkit.getWorlds()) {
  131. for(Chunk chunk : world.getLoadedChunks()) {
  132. for(Entity entity : chunk.getEntities()) {
  133. if(entity.getUniqueId().equals(uuid)) {
  134. return entity;
  135. }
  136. }
  137. }
  138. }
  139. return null;
  140. }
  141.  
  142. public void headRotation(float yaw,float pitch, Player player){
  143. PacketPlayOutEntity.PacketPlayOutEntityLook packet = new PacketPlayOutEntity.PacketPlayOutEntityLook(entityPlayer.getBukkitEntity().getEntityId(), getFixRotation(yaw),getFixRotation(pitch) , true);
  144. PacketPlayOutEntityHeadRotation packetHead = new PacketPlayOutEntityHeadRotation();
  145. setValue(packetHead, "a", entityPlayer.getBukkitEntity().getEntityId());
  146. setValue(packetHead, "b", getFixRotation(yaw));
  147.  
  148.  
  149. sendPacket(packet, player);
  150. sendPacket(packetHead, player);
  151. }
  152.  
  153. public void setLocation(Location location) {
  154. }
  155.  
  156. public int getFixLocation(double pos){
  157. return (int)MathHelper.floor(pos * 32.0D);
  158. }
  159.  
  160. public byte getFixRotation(float yawpitch){
  161. return (byte) ((int) (yawpitch * 256.0F / 360.0F));
  162. }
  163.  
  164. public int getEntityID() {
  165. return entityPlayer.getBukkitEntity().getEntityId();
  166. }
  167.  
  168.  
  169.  
  170. public Location getLocation() {
  171. return getPlayer().getLocation();
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement