Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.31 KB | None | 0 0
  1. package de.phil.setup;
  2.  
  3. /* created by Semetre edited by fphil*/
  4.  
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.Serializable;
  8. import java.lang.reflect.Field;
  9. import java.net.URL;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Scanner;
  13. import java.util.UUID;
  14.  
  15. import net.minecraft.server.v1_8_R3.*;
  16. import org.bukkit.Bukkit;
  17. import org.bukkit.Location;
  18. import org.bukkit.Material;
  19. import org.bukkit.World;
  20. import org.bukkit.configuration.file.YamlConfiguration;
  21. import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
  22. import org.bukkit.craftbukkit.v1_8_R3.util.CraftChatMessage;
  23. import org.bukkit.entity.Player;
  24.  
  25. import com.google.gson.Gson;
  26. import com.google.gson.JsonObject;
  27. import com.mojang.authlib.GameProfile;
  28. import com.mojang.authlib.properties.Property;
  29.  
  30. import net.minecraft.server.v1_8_R3.PacketPlayOutEntity.PacketPlayOutEntityLook;
  31. import net.minecraft.server.v1_8_R3.WorldSettings.EnumGamemode;
  32.  
  33. public class NPC implements Serializable {
  34.  
  35. public enum Slot {
  36. HAND ( 0 ), HELMET ( 4 ), CHESTPLATE ( 3 ), LEGGINGS ( 2 ), BOOTS ( 1 );
  37.  
  38. private Integer slot;
  39.  
  40. private Slot ( Integer slot ) {
  41. this.slot = slot;
  42. }
  43.  
  44. public Integer getSlot ( ) {
  45. return slot;
  46. }
  47. }
  48.  
  49. private static final long serialVersionUID = - 672963264444438574L;
  50. private int entityID;
  51. private Location location;
  52. private GameProfile gameprofile;
  53. private Float health = 20F;
  54.  
  55. public NPC ( String name , Location location ) {
  56. entityID = ( int ) Math.ceil ( Math.random ( ) * 1000 ) + 2000;
  57. gameprofile = new GameProfile ( UUID.randomUUID ( ) , name );
  58. this.location = location.clone ( );
  59. }
  60.  
  61. public NPC ( String name , Integer entityID , UUID uuid , Location location ) {
  62. this.entityID = entityID;
  63. gameprofile = new GameProfile ( uuid , name );
  64. this.location = location.clone ( );
  65. }
  66.  
  67. public int getEntityID ( ) {
  68. return entityID;
  69. }
  70.  
  71. public String getName ( ) {
  72. return gameprofile.getName ( );
  73. }
  74.  
  75. public Float getHealth ( ) {
  76. return health;
  77. }
  78.  
  79. public void setHealth ( Float health ) {
  80. this.health = health;
  81. }
  82.  
  83. public Location getLocation ( ) {
  84. return location;
  85. }
  86.  
  87. public void setSkin ( String name ) {
  88.  
  89. try {
  90.  
  91. String value = "";
  92. String signatur = "";
  93.  
  94. File file = new File ( "plugins//NPCS//" + name + ".yml" );
  95. YamlConfiguration cfg = YamlConfiguration.loadConfiguration ( file );
  96.  
  97. if ( ! file.exists ( ) ) {
  98.  
  99. Gson gson = new Gson ( );
  100. String url = "https://api.mojang.com/users/profiles/minecraft/" + name;
  101. String json = getStringFromURL ( url );
  102. String uuid = gson.fromJson ( json , JsonObject.class ).get ( "id" ).getAsString ( );
  103.  
  104. url = "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false";
  105. json = getStringFromURL ( url );
  106. JsonObject mainObject = gson.fromJson ( json , JsonObject.class );
  107. JsonObject jObject = mainObject.get ( "properties" ).getAsJsonArray ( ).get ( 0 ).getAsJsonObject ( );
  108. value = jObject.get ( "value" ).getAsString ( );
  109. signatur = jObject.get ( "signature" ).getAsString ( );
  110.  
  111. cfg.set ( "value" , value );
  112. cfg.set ( "signature" , signatur );
  113.  
  114. cfg.save ( file );
  115.  
  116. } else {
  117.  
  118. value = cfg.getString ( "value" );
  119. signatur = cfg.getString ( "signature" );
  120.  
  121. }
  122.  
  123. if ( value == "" ) {
  124.  
  125. value = "eyJ0aW1lc3RhbXAiOjE1MTgwMjM5Nzk0NjUsInByb2ZpbGVJZCI6IjcxMjdjYTdiNmZhZDQwNDc4ODdjZjgyMjE2NzFjMDhhIiwicHJvZmlsZU5hbWUiOiJmcGhpbCIsInNpZ25hdHVyZVJlcXVpcmVkIjp0cnVlLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMzYyMTI0ZTg2NDZjZGQxZmVlNTYwMzY5YWM4MmQzYmE4NGIyMTE5NGU3OTFlMjgxNjk5M2M2OGU4Y2E3YzkxIiwibWV0YWRhdGEiOnsibW9kZWwiOiJzbGltIn19fX0=";
  126. signatur = "yX9ZaOAKtQoFxgQJXUB3k6od1aXkah7/1CjUZ5zyC9d1HAPge9BCsn0BpU+LkixDMbNGzxRaHO9E5lNW9vr4o4JfbLPrt27/5b6WYnumTJPImSxWdA88lmmr0rZlmeyvPn3eBvsCw6JVBFG7slyv0fqvDRHufgBmx9ppULFfuNUH53b9rR1vm1pm4zfzSM/bW3k/1BuSElTDorP0It98RIGU19ajHfJNWfHIO42q/bRP9Ay5o7SSiIWAeAhRkWyjimznJhgxT4ghqW2E/OhfnvDxkWs2ihXkAVqPgL/9gBQREys3JAzFbO7/MOD8FGtwIsD7Xj481hsPJ10lj6FAEmCaQN9kaIE5hYWXepOztSCe9R1FCYApSpuHSRWtsTh65Od5QF/cWqk2clWEe+GFM39Y1N+kvNszRYHo34UHn+jvJb8VYIMzNYMHf90uMDmEu1RlXSHfHtMgNu82POvAese/GEmnWnwExguIyKYCrzRUZ7caGsmLXouWQV3NWUETBXqBio5R/nmWAvBa4ksn981mEZ2NUg8rBb+9RsTHFW97dvfcJkkmZ00YgXRJs39A7nv7p2FGrnAlUq4XMAcUxEMXCNb8CE+aT12kMQA5j3VQiwHoeZ/dWAc+0uU5W/hEND48JAxuMktBerTmN55Vvi6LRAn2ySnGcutKuENHfes=";
  127.  
  128. }
  129.  
  130. gameprofile.getProperties ( ).put ( "textures" , new Property ( "textures" , value , signatur ) );
  131.  
  132. } catch ( IOException e ) {
  133. Bukkit.getConsoleSender ( ).sendMessage ( "§cFILE FEHLER" );
  134. } catch ( Exception ignore ) {
  135.  
  136. }
  137. }
  138.  
  139. public void animation ( int animation ) {
  140. PacketPlayOutAnimation packet = new PacketPlayOutAnimation ( );
  141. setValue ( packet , "a" , entityID );
  142. setValue ( packet , "b" , ( byte ) animation );
  143. sendPacket ( packet );
  144. }
  145.  
  146. public void status ( int status ) {
  147. PacketPlayOutEntityStatus packet = new PacketPlayOutEntityStatus ( );
  148. setValue ( packet , "a" , entityID );
  149. setValue ( packet , "b" , ( byte ) status );
  150. sendPacket ( packet );
  151. }
  152.  
  153.  
  154. public void equip ( Slot slot , ItemStack itemstack ) {
  155. PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment ( );
  156. setValue ( packet , "a" , entityID );
  157. setValue ( packet , "b" , slot.getSlot ( ) );
  158. setValue ( packet , "c" , itemstack );
  159. sendPacket ( packet );
  160. }
  161.  
  162. @SuppressWarnings ( "deprecation" )
  163. public void sleep ( boolean state ) {
  164. if ( state ) {
  165. Location bedLocation = new Location ( location.getWorld ( ) , 1 , 1 , 1 );
  166. PacketPlayOutBed packet = new PacketPlayOutBed ( );
  167. setValue ( packet , "a" , entityID );
  168. setValue ( packet , "b" , new BlockPosition ( bedLocation.getX ( ) , bedLocation.getY ( ) , bedLocation.getZ ( ) ) );
  169.  
  170. for ( Player pl : Bukkit.getOnlinePlayers ( ) ) {
  171. pl.sendBlockChange ( bedLocation , Material.BED_BLOCK , ( byte ) 0 );
  172. }
  173.  
  174. sendPacket ( packet );
  175. teleport ( location.clone ( ).add ( 0 , 0.3 , 0 ) );
  176. } else {
  177. animation ( 2 );
  178. teleport ( location.clone ( ).subtract ( 0 , 0.3 , 0 ) );
  179. }
  180. }
  181.  
  182. public void spawn ( ) {
  183. PacketPlayOutNamedEntitySpawn packet = new PacketPlayOutNamedEntitySpawn ( );
  184. setValue ( packet , "a" , entityID );
  185. setValue ( packet , "b" , gameprofile.getId ( ) );
  186. setValue ( packet , "c" , getFixLocation ( location.getX ( ) ) );
  187. setValue ( packet , "d" , getFixLocation ( location.getY ( ) ) );
  188. setValue ( packet , "e" , getFixLocation ( location.getZ ( ) ) );
  189. setValue ( packet , "f" , getFixRotation ( location.getYaw ( ) ) );
  190. setValue ( packet , "g" , getFixRotation ( location.getPitch ( ) ) );
  191. setValue ( packet , "h" , 0 );
  192. DataWatcher w = new DataWatcher ( null );
  193. w.a ( 6 , health );
  194. addToTablist ( );
  195. w.a ( 10 , ( byte ) 127 );
  196. setValue ( packet , "i" , w );
  197. sendPacket ( packet );
  198. headRotation ( location.getYaw ( ) , location.getPitch ( ) );
  199. }
  200.  
  201. public void spawn ( Player player ) {
  202. PacketPlayOutNamedEntitySpawn packet = new PacketPlayOutNamedEntitySpawn ( );
  203. setValue ( packet , "a" , entityID );
  204. setValue ( packet , "b" , gameprofile.getId ( ) );
  205. setValue ( packet , "c" , getFixLocation ( location.getX ( ) ) );
  206. setValue ( packet , "d" , getFixLocation ( location.getY ( ) ) );
  207. setValue ( packet , "e" , getFixLocation ( location.getZ ( ) ) );
  208. setValue ( packet , "f" , getFixRotation ( location.getYaw ( ) ) );
  209. setValue ( packet , "g" , getFixRotation ( location.getPitch ( ) ) );
  210. setValue ( packet , "h" , 0 );
  211. DataWatcher w = new DataWatcher ( null );
  212. w.a ( 6 , health );
  213. addToTablist ( );
  214. w.a ( 10 , ( byte ) 127 );
  215. setValue ( packet , "i" , w );
  216. sendPacket ( packet , player );
  217. headRotation ( location.getYaw ( ) , location.getPitch ( ) );
  218. }
  219.  
  220. public void teleport ( Location location ) {
  221. PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport ( );
  222. setValue ( packet , "a" , entityID );
  223. setValue ( packet , "b" , getFixLocation ( location.getX ( ) ) );
  224. setValue ( packet , "c" , getFixLocation ( location.getY ( ) ) );
  225. setValue ( packet , "d" , getFixLocation ( location.getZ ( ) ) );
  226. setValue ( packet , "e" , getFixRotation ( location.getYaw ( ) ) );
  227. setValue ( packet , "f" , getFixRotation ( location.getPitch ( ) ) );
  228.  
  229. sendPacket ( packet );
  230. headRotation ( location.getYaw ( ) , location.getPitch ( ) );
  231. this.location = location.clone ( );
  232. }
  233.  
  234. public void headRotation ( float yaw , float pitch ) {
  235. PacketPlayOutEntityLook packet = new PacketPlayOutEntityLook ( entityID , getFixRotation ( yaw ) ,
  236. getFixRotation ( pitch ) , true );
  237. PacketPlayOutEntityHeadRotation packetHead = new PacketPlayOutEntityHeadRotation ( );
  238. setValue ( packetHead , "a" , entityID );
  239. setValue ( packetHead , "b" , getFixRotation ( yaw ) );
  240.  
  241. sendPacket ( packet );
  242. sendPacket ( packetHead );
  243.  
  244. this.location.setYaw ( yaw );
  245. this.location.setPitch ( pitch );
  246. }
  247.  
  248. public void headRotation ( Location location ) {
  249. headRotation ( location.getYaw ( ) , location.getPitch ( ) );
  250. }
  251.  
  252. public void destroy ( ) {
  253. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy ( new int[] { entityID } );
  254. removeFromTablist ( );
  255. sendPacket ( packet );
  256. }
  257.  
  258. public void destroy ( Player player ) {
  259. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy ( new int[] { entityID } );
  260. removeFromTablist ( );
  261. sendPacket ( packet , player );
  262. }
  263.  
  264. public void addToTablist ( ) {
  265. PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo ( );
  266. PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData ( gameprofile , 1 , EnumGamemode.NOT_SET ,
  267. CraftChatMessage.fromString ( gameprofile.getName ( ) )[ 0 ] );
  268. @SuppressWarnings ( "unchecked" )
  269. List< PacketPlayOutPlayerInfo.PlayerInfoData > players = ( List< PacketPlayOutPlayerInfo.PlayerInfoData > ) getValue (
  270. packet , "b" );
  271. players.add ( data );
  272.  
  273. setValue ( packet , "a" , PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER );
  274. setValue ( packet , "b" , players );
  275. sendPacket ( packet );
  276. }
  277.  
  278. public void removeFromTablist ( ) {
  279. PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo ( );
  280. PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData ( gameprofile , 1 , EnumGamemode.NOT_SET ,
  281. CraftChatMessage.fromString ( gameprofile.getName ( ) )[ 0 ] );
  282. @SuppressWarnings ( "unchecked" )
  283. List< PacketPlayOutPlayerInfo.PlayerInfoData > players = ( List< PacketPlayOutPlayerInfo.PlayerInfoData > ) getValue (
  284. packet , "b" );
  285. players.add ( data );
  286. setValue ( packet , "a" , PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER );
  287. setValue ( packet , "b" , players );
  288.  
  289. sendPacket ( packet );
  290. }
  291.  
  292. public void removeFromTablist ( Player player ) {
  293. PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo ( );
  294. PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData ( gameprofile , 1 , EnumGamemode.NOT_SET ,
  295. CraftChatMessage.fromString ( gameprofile.getName ( ) )[ 0 ] );
  296. @SuppressWarnings ( "unchecked" )
  297. List< PacketPlayOutPlayerInfo.PlayerInfoData > players = ( List< PacketPlayOutPlayerInfo.PlayerInfoData > ) getValue (
  298. packet , "b" );
  299. players.add ( data );
  300. setValue ( packet , "a" , PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER );
  301. setValue ( packet , "b" , players );
  302.  
  303. sendPacket ( packet , player );
  304. }
  305.  
  306. private int getFixLocation ( double pos ) {
  307. return ( int ) MathHelper.floor ( pos * 32.0D );
  308. }
  309.  
  310. private byte getFixRotation ( float yawpitch ) {
  311. return ( byte ) ( ( int ) ( yawpitch * 256.0F / 360.0F ) );
  312. }
  313.  
  314. private void setValue ( Object obj , String name , Object value ) {
  315. try {
  316. Field field = obj.getClass ( ).getDeclaredField ( name );
  317. field.setAccessible ( true );
  318. field.set ( obj , value );
  319. } catch ( Exception e ) {
  320. }
  321. }
  322.  
  323. private Object getValue ( Object obj , String name ) {
  324. try {
  325. Field field = obj.getClass ( ).getDeclaredField ( name );
  326. field.setAccessible ( true );
  327. return field.get ( obj );
  328. } catch ( Exception e ) {
  329. }
  330. return null;
  331. }
  332.  
  333. private void sendPacket ( Packet< ? > packet , Player player ) {
  334. ( ( CraftPlayer ) player ).getHandle ( ).playerConnection.sendPacket ( packet );
  335. }
  336.  
  337. private void sendPacket ( Packet< ? > packet ) {
  338. for ( Player player : Bukkit.getOnlinePlayers ( ) ) {
  339. sendPacket ( packet , player );
  340. }
  341. }
  342.  
  343. private String getStringFromURL ( String url ) {
  344. String text = "";
  345. try {
  346. Scanner scanner = new Scanner ( new URL ( url ).openStream ( ) );
  347. while ( scanner.hasNext ( ) ) {
  348. String line = scanner.nextLine ( );
  349. while ( line.startsWith ( " " ) ) {
  350. line = line.substring ( 1 );
  351. }
  352. text = text + line;
  353. }
  354. scanner.close ( );
  355. } catch ( IOException e ) {
  356. e.printStackTrace ( );
  357. }
  358. return text;
  359. }
  360.  
  361. public HashMap< String, Object > encode ( ) {
  362. HashMap< String, Object > map = new HashMap<> ( );
  363.  
  364. // LOC
  365. map.put ( "X" , location.getX ( ) );
  366. map.put ( "Y" , location.getY ( ) );
  367. map.put ( "Z" , location.getZ ( ) );
  368. map.put ( "Pitch" , location.getPitch ( ) );
  369. map.put ( "Yaw" , location.getYaw ( ) );
  370. map.put ( "World" , location.getWorld ( ).getName ( ) );
  371.  
  372. // DATA
  373. map.put ( "name" , gameprofile.getName ( ) );
  374. map.put ( "entityID" , entityID );
  375. map.put ( "UUID" , gameprofile.getId ( ) );
  376. map.put ( "health" , health );
  377.  
  378. // GAMEPROFILE
  379. String value = "";
  380. String signatur = "";
  381. for ( Property property : gameprofile.getProperties ( ).get ( "textures" ) ) {
  382. value = property.getValue ( );
  383. signatur = property.getSignature ( );
  384. }
  385. map.put ( "value" , value );
  386. map.put ( "signatur" , signatur );
  387.  
  388. return map;
  389. }
  390.  
  391. public static NPC decode ( HashMap< String, Object > map ) {
  392. String name = ( String ) map.get ( "name" );
  393. Integer entityID = ( Integer ) map.get ( "entityID" );
  394. UUID uuid = ( UUID ) map.get ( "UUID" );
  395.  
  396. World world = Bukkit.getWorld ( ( String ) map.get ( "World" ) );
  397. Double x = ( Double ) map.get ( "X" );
  398. Double y = ( Double ) map.get ( "Y" );
  399. Double z = ( Double ) map.get ( "Z" );
  400. Float pitch = ( Float ) map.get ( "Pitch" );
  401. Float yaw = ( Float ) map.get ( "Yaw" );
  402. Location location = new Location ( world , x , y , z , yaw , pitch );
  403.  
  404. NPC npc = new NPC ( name , entityID , uuid , location );
  405. npc.health = ( Float ) map.get ( "health" );
  406. String value = ( String ) map.get ( "value" );
  407. String signatur = ( String ) map.get ( "signatur" );
  408. npc.gameprofile.getProperties ( ).put ( "textures" , new Property ( "textures" , value , signatur ) );
  409. return npc;
  410. }
  411.  
  412. public String toString ( ) {
  413. return encode ( ).toString ( );
  414. }
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement