Eigene NPCs in der 1.8 erstellen! [UPDATE: NPCInteractEvent]

Hier könnt ihr anderen Leuten helfen, indem ihr Anleitungen oder praktische Codesegmente zur Verfügung stellt.

Eigene NPCs in der 1.8 erstellen! [UPDATE: NPCInteractEvent]

Beitragvon Summerfeeling » Mo 29. Dez 2014, 08:20

Hey,
heute möchte ich meine Klasse mit euch teilen, die mir/euch ermöglicht, NPCs in der 1.8 zu spawnen!
Ich werde hier nicht näher auf die Klasse eingehen, da ich dieses bereits in meinem Tutorial für die NPCs in der 1.7 getan habe. Wer also was erfahren möchte, guckt dort vorbei.

Diese Klasse hier ist außerdem um einiges komplexer! Sie enthält mehrere Methoden um zum Beispiel den NPC zu porten, oder ihm Rüstung zu geben!

Für diese Klasse benötigt ihr außerdem die Spigot 1.8 - welche ihr hier immer up-to-date ohne BuildTool downloaden könnt! :D

Ich habe euch ebenfalls diese Klasse nochmal auf GitLab geteilt, da man diese dort besser ansehen kann.

Aber hier gibt's natürlich auch nochmal den Code! ;)
Code: Alles auswählen
  1. package de.Summerfeeling.Tutorial.NPC;
  2. import java.lang.reflect.Field;
  3. import java.util.List;
  4. import java.util.Random;
  5. import java.util.UUID;
  6. import net.minecraft.server.v1_8_R1.DataWatcher;
  7. import net.minecraft.server.v1_8_R1.EnumGamemode;
  8. import net.minecraft.server.v1_8_R1.EnumPlayerInfoAction;
  9. import net.minecraft.server.v1_8_R1.PacketPlayOutEntityDestroy;
  10. import net.minecraft.server.v1_8_R1.PacketPlayOutEntityEquipment;
  11. import net.minecraft.server.v1_8_R1.PacketPlayOutEntityTeleport;
  12. import net.minecraft.server.v1_8_R1.PacketPlayOutNamedEntitySpawn;
  13. import net.minecraft.server.v1_8_R1.PacketPlayOutPlayerInfo;
  14. import net.minecraft.server.v1_8_R1.PlayerInfoData;
  15. import org.bukkit.Bukkit;
  16. import org.bukkit.Location;
  17. import org.bukkit.Material;
  18. import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
  19. import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack;
  20. import org.bukkit.craftbukkit.v1_8_R1.util.CraftChatMessage;
  21. import org.bukkit.entity.Player;
  22. import org.bukkit.inventory.ItemStack;
  23. import com.mojang.authlib.GameProfile;
  24. public class NPC {
  25.    
  26.    private DataWatcher watcher;
  27.    private Material chestplate;
  28.    private Material leggings;
  29.    private Location location;
  30.    private Material inHand;
  31.    private Material helmet;
  32.    private Material boots;
  33.    private String tablist;
  34.    private int entityID;
  35.    private String name;
  36.    private UUID uuid;
  37.    
  38.    /*
  39.     * NPC, a class for spawning fake players in the 1.8
  40.    Copyright (C) [Summerfeeling]
  41.    Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation veröffentlicht, weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
  42.    Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
  43.    Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>.
  44.     */
  45.    
  46.    public NPC(String name, String tablist, UUID uuid, int entityID, Location location, Material inHand) {
  47.       this.location = location;
  48.       this.tablist = tablist;
  49.       this.name = name;
  50.       this.uuid = uuid;
  51.       this.entityID = entityID;
  52.       this.inHand = inHand;
  53.       this.watcher = new DataWatcher(null);
  54.       watcher.a(6, (float) 20);
  55.    }
  56.    
  57.    public NPC(String name, Location location) {
  58.       this(name, name, UUID.randomUUID(), new Random().nextInt(10000), location, Material.AIR);
  59.    }
  60.    
  61.    public NPC(String name, Location location, Material inHand) {
  62.       this(name, name, UUID.randomUUID(), new Random().nextInt(10000), location, inHand);
  63.    }
  64.    
  65.    public NPC(String name, String tablist, Location location) {
  66.       this(name, tablist, UUID.randomUUID(), new Random().nextInt(10000), location, Material.AIR);
  67.    }
  68.    
  69.    public NPC(String name, String tablist, Location location, Material inHand) {
  70.       this(name, tablist, UUID.randomUUID(), new Random().nextInt(10000), location, inHand);
  71.    }
  72.    
  73.    @SuppressWarnings("deprecation")
  74.    public void spawn() {
  75.       try{
  76.          PacketPlayOutNamedEntitySpawn packet = new PacketPlayOutNamedEntitySpawn();
  77.          this.addToTablist();
  78.          
  79.          this.setValue(packet, "a", this.entityID);
  80.          this.setValue(packet, "b", this.uuid);
  81.          this.setValue(packet, "c", this.toFixedPoint(this.location.getX()));
  82.          this.setValue(packet, "d", this.toFixedPoint(this.location.getY()));
  83.          this.setValue(packet, "e", this.toFixedPoint(this.location.getZ()));
  84.          this.setValue(packet, "f", this.toPackedByte(this.location.getYaw()));
  85.          this.setValue(packet, "g", this.toPackedByte(this.location.getPitch()));
  86.          this.setValue(packet, "h", this.inHand == null ? 0 : this.inHand.getId());
  87.          this.setValue(packet, "i", this.watcher);
  88.          
  89.          for(Player online : Bukkit.getOnlinePlayers()) {
  90.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  91.          }
  92.       }catch(Exception e) {
  93.          e.printStackTrace();
  94.       }
  95.    }
  96.    
  97.    @SuppressWarnings("deprecation")
  98.    public void despawn() {
  99.       PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(new int[]{this.entityID});
  100.       this.removeFromTablist();
  101.       for(Player online : Bukkit.getOnlinePlayers()) {
  102.          ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  103.       }
  104.    }
  105.    
  106.    @SuppressWarnings("deprecation")
  107.    public void changePlayerlistName(String name) {
  108.       try{
  109.          PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
  110.          PlayerInfoData data = new PlayerInfoData(packet, new GameProfile(this.uuid, this.name), 0, EnumGamemode.NOT_SET, CraftChatMessage.fromString(name)[0]);
  111.          @SuppressWarnings("unchecked") List<PlayerInfoData> players = (List<PlayerInfoData>) this.getValue(packet, "b");
  112.          players.add(data);
  113.          
  114.          this.setValue(packet, "a", EnumPlayerInfoAction.UPDATE_DISPLAY_NAME);
  115.          this.setValue(packet, "b", players);
  116.          this.tablist = name;
  117.          
  118.          for(Player online : Bukkit.getOnlinePlayers()) {
  119.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  120.          }
  121.       }catch(Exception e) {
  122.          e.printStackTrace();
  123.       }
  124.    }
  125.    
  126.    @SuppressWarnings("deprecation")
  127.    private void addToTablist() {
  128.       try {
  129.          PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
  130.          PlayerInfoData data = new PlayerInfoData(packet, new GameProfile(this.uuid, this.name), 0, EnumGamemode.NOT_SET, CraftChatMessage.fromString(this.tablist)[0]);
  131.          @SuppressWarnings("unchecked") List<PlayerInfoData> players = (List<PlayerInfoData>) this.getValue(packet, "b");
  132.          players.add(data);
  133.          
  134.          this.setValue(packet, "a", EnumPlayerInfoAction.ADD_PLAYER);
  135.          this.setValue(packet, "b", players);
  136.          
  137.          for(Player online : Bukkit.getOnlinePlayers()) {
  138.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  139.          }
  140.       } catch (Exception e) {
  141.          e.printStackTrace();
  142.       }
  143.    }
  144.    
  145.    @SuppressWarnings("deprecation")
  146.    private void removeFromTablist() {
  147.       try{
  148.          PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
  149.          PlayerInfoData data = new PlayerInfoData(packet, new GameProfile(this.uuid, this.name), 0, EnumGamemode.NOT_SET, CraftChatMessage.fromString(this.tablist)[0]);
  150.          @SuppressWarnings("unchecked") List<PlayerInfoData> players = (List<PlayerInfoData>) this.getValue(packet, "b");
  151.          players.add(data);
  152.          
  153.          this.setValue(packet, "a", EnumPlayerInfoAction.REMOVE_PLAYER);
  154.          this.setValue(packet, "b", players);
  155.          
  156.          for(Player online : Bukkit.getOnlinePlayers()) {
  157.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  158.          }
  159.       }catch(Exception e) {
  160.          e.printStackTrace();
  161.       }
  162.    }
  163.    
  164.    @SuppressWarnings("deprecation")
  165.    public void teleport(Location location) {
  166.       try{
  167.          PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport();
  168.          
  169.          this.setValue(packet, "a", this.entityID);
  170.          this.setValue(packet, "b", this.toFixedPoint(location.getX()));
  171.          this.setValue(packet, "c", this.toFixedPoint(location.getY()));
  172.          this.setValue(packet, "d", this.toFixedPoint(location.getZ()));
  173.          this.setValue(packet, "e", this.toPackedByte(location.getYaw()));
  174.          this.setValue(packet, "f", this.toPackedByte(location.getPitch()));
  175.          this.setValue(packet, "g", this.location.getBlock().getType() == Material.AIR ? false : true);
  176.          this.location = location;
  177.          
  178.          for(Player online : Bukkit.getOnlinePlayers()) {
  179.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  180.          }
  181.       }catch(Exception e) {
  182.          e.printStackTrace();
  183.       }
  184.    }
  185.    
  186.    @SuppressWarnings("deprecation")
  187.    public void setItemInHand(Material material) {
  188.       try{
  189.          PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  190.          
  191.          this.setValue(packet, "a", this.entityID);
  192.          this.setValue(packet, "b", 0);
  193.          this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  194.          this.inHand = material;
  195.          
  196.          for(Player online : Bukkit.getOnlinePlayers()) {
  197.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  198.          }
  199.       }catch(Exception e) {
  200.          e.printStackTrace();
  201.       }
  202.    }
  203.    
  204.    public Material getItemInHand() {
  205.       return this.inHand;
  206.    }
  207.    
  208.    @SuppressWarnings("deprecation")
  209.    public void setHelmet(Material material) {
  210.       try{
  211.          PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  212.          
  213.          this.setValue(packet, "a", this.entityID);
  214.          this.setValue(packet, "b", 4);
  215.          this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  216.          this.helmet = material;
  217.          
  218.          for(Player online : Bukkit.getOnlinePlayers()) {
  219.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  220.          }
  221.       }catch(Exception e) {
  222.          e.printStackTrace();
  223.       }
  224.    }
  225.    
  226.    public Material getHelmet() {
  227.       return this.helmet;
  228.    }
  229.    
  230.    @SuppressWarnings("deprecation")
  231.    public void setChestplate(Material material) {
  232.       try{
  233.          PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  234.          
  235.          this.setValue(packet, "a", this.entityID);
  236.          this.setValue(packet, "b", 3);
  237.          this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  238.          this.chestplate = material;
  239.          
  240.          for(Player online : Bukkit.getOnlinePlayers()) {
  241.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  242.          }
  243.       }catch(Exception e) {
  244.          e.printStackTrace();
  245.       }
  246.    }
  247.    
  248.    public Material getChestplate() {
  249.       return this.chestplate;
  250.    }
  251.    
  252.    @SuppressWarnings("deprecation")
  253.    public void setLeggings(Material material) {
  254.       try{
  255.          PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  256.          
  257.          this.setValue(packet, "a", this.entityID);
  258.          this.setValue(packet, "b", 2);
  259.          this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  260.          this.leggings = material;
  261.          
  262.          for(Player online : Bukkit.getOnlinePlayers()) {
  263.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  264.          }
  265.       }catch(Exception e) {
  266.          e.printStackTrace();
  267.       }
  268.    }
  269.    
  270.    public Material getLeggings() {
  271.       return this.leggings;
  272.    }
  273.    
  274.    @SuppressWarnings("deprecation")
  275.    public void setBoots(Material material) {
  276.       try{
  277.          PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  278.          
  279.          this.setValue(packet, "a", this.entityID);
  280.          this.setValue(packet, "b", 1);
  281.          this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  282.          this.boots = material;
  283.          
  284.          for(Player online : Bukkit.getOnlinePlayers()) {
  285.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  286.          }
  287.       }catch(Exception e) {
  288.          e.printStackTrace();
  289.       }
  290.    }
  291.    
  292.    public Material getBoots() {
  293.       return this.boots;
  294.    }
  295.    
  296.    public int getEntityID() {
  297.       return this.entityID;
  298.    }
  299.    
  300.    public UUID getUUID() {
  301.       return this.uuid;
  302.    }
  303.    
  304.    public Location getLocation() {
  305.       return this.location;
  306.    }
  307.    
  308.    public String getName() {
  309.       return this.name;
  310.    }
  311.    
  312.    public String getPlayerlistName() {
  313.       return this.tablist;
  314.    }
  315.    
  316.    private void setValue(Object instance, String field, Object value) throws Exception {
  317.       Field f = instance.getClass().getDeclaredField(field);
  318.       f.setAccessible(true);
  319.       f.set(instance, value);
  320.    }
  321.    
  322.    private Object getValue(Object instance, String field) throws Exception {
  323.       Field f = instance.getClass().getDeclaredField(field);
  324.       f.setAccessible(true);
  325.       return f.get(instance);
  326.    }
  327.    
  328.    private int toFixedPoint(double d) {
  329.       return (int) (d * 32.0);
  330.    }
  331.    
  332.    private byte toPackedByte(float f) {
  333.       return (byte) ((int) (f * 256.0F / 360.0F));
  334.    }
  335.    
  336. }


Verwendung:
Diese Klasse lässt sich ganz einfach verwenden.
Ihr müsst nur ein neues NPC Objekt instanzieren, mit eurem gewünschten Constructor! :)
Code: Alles auswählen
  1.       NPC npc = new NPC("Mein NPC!", Player.getLocation());


In Kürze werde ich diese Klasse noch um eine Methode erweitern, die NPCs umzubennen, meine aktuelle (nicht im Code enthalten) buggt. Werde mich aber schnell drum kümmern! ;) Ebenso wird in Kürze auch eine updatesichere Klasse folgen! :)

Nun noch einen schönen Tag,
Summerfeeling!
Zuletzt geändert von Summerfeeling am So 16. Aug 2015, 14:47, insgesamt 4-mal geändert.
Grüße
Summerfeeling | Timo
Benutzeravatar
Summerfeeling
 
Beiträge: 1300
Registriert: Sa 15. Jun 2013, 18:43
Wohnort: Viersen

Re: Eigene NPCs in der 1.8 erstellen!

Beitragvon Summerfeeling » Mo 29. Dez 2014, 08:22

UPDATE FÜR DIE 1.8.3+ (1.8_R2)

Hier nun die Klasse für die 1.8.3+. Bitte lasst euch nicht von den 660 Zeilen Code verschrecken. Es liegt daran, dass ich zwei Klasse von @Jofkos in dieser Klasse haben. Den GameProfileBuilder und den UUIDFetcher. Solltet ihr einer der beiden Klassen bereits in irgendeiner Weiße in eurem Projekt haben, könnt ihr die Klasse einfach auf der NPC Klasse löschen, und eure verwenden.

Nun, hier erstmal der Code: (alternativ Link)

Code: Alles auswählen
  1. package de.summerfeeling.postcrafter.entity;
  2. import com.google.gson.*;
  3. import com.mojang.authlib.GameProfile;
  4. import com.mojang.authlib.properties.Property;
  5. import com.mojang.authlib.properties.PropertyMap;
  6. import com.mojang.util.UUIDTypeAdapter;
  7. import net.md_5.bungee.api.ChatColor;
  8. import net.minecraft.server.v1_8_R2.*;
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.Location;
  11. import org.bukkit.Material;
  12. import org.bukkit.craftbukkit.v1_8_R2.entity.CraftPlayer;
  13. import org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack;
  14. import org.bukkit.craftbukkit.v1_8_R2.util.CraftChatMessage;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.inventory.ItemStack;
  17. import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.lang.reflect.Field;
  22. import java.lang.reflect.Type;
  23. import java.net.HttpURLConnection;
  24. import java.net.URL;
  25. import java.util.*;
  26. import java.util.concurrent.ExecutorService;
  27. import java.util.concurrent.Executors;
  28. public class NPC {
  29.     private DataWatcher watcher;
  30.     private GameProfile profile;
  31.     private Material chestplate;
  32.     private Material leggings;
  33.     private Location location;
  34.     private Material inHand;
  35.     private Material helmet;
  36.     private Material boots;
  37.     private String tablist;
  38.     private int entityID;
  39.     private String name;
  40.    /*
  41.     * NPC, a class for spawning fake players in the 1.8
  42.    Copyright (C) [Summerfeeling]
  43.    Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation veröffentlicht, weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
  44.    Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
  45.    Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>.
  46.     */
  47.     public NPC(String name, String tablist, int entityID, Location location, Material inHand) {
  48.         this.location = location;
  49.         this.tablist = ChatColor.translateAlternateColorCodes('&', tablist);
  50.         this.name = ChatColor.translateAlternateColorCodes('&', name);
  51.         this.entityID = entityID;
  52.         this.inHand = inHand;
  53.         this.watcher = new DataWatcher(null);
  54.         watcher.a(6, (float) 20);
  55.     }
  56.     public NPC(String name, Location location) {
  57.         this(name, name, new Random().nextInt(10000), location, Material.AIR);
  58.     }
  59.     public NPC(String name, Location location, Material inHand) {
  60.         this(name, name, new Random().nextInt(10000), location, inHand);
  61.     }
  62.     public NPC(String name, String tablist, Location location) {
  63.         this(name, tablist, new Random().nextInt(10000), location, Material.AIR);
  64.     }
  65.     public NPC(String name, String tablist, Location location, Material inHand) {
  66.         this(name, tablist, new Random().nextInt(10000), location, inHand);
  67.     }
  68.     @SuppressWarnings("deprecation")
  69.     public void spawn() {
  70.         try{
  71.             PacketPlayOutNamedEntitySpawn packet = new PacketPlayOutNamedEntitySpawn();
  72.             addToTablist();
  73.             setValue(packet, "a", entityID);
  74.             setValue(packet, "b", this.profile.getId());
  75.             setValue(packet, "c", toFixedPoint(location.getX()));
  76.             setValue(packet, "d", toFixedPoint(location.getY()));
  77.             setValue(packet, "e", toFixedPoint(location.getZ()));
  78.             setValue(packet, "f", toPackedByte(location.getYaw()));
  79.             setValue(packet, "g", toPackedByte(location.getPitch()));
  80.             setValue(packet, "h", inHand == null ? 0 : inHand.getId());
  81.             setValue(packet, "i", watcher);
  82.             for(Player online : Bukkit.getOnlinePlayers()) {
  83.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  84.             }
  85.         }catch(Exception e) {
  86.             e.printStackTrace();
  87.         }
  88.     }
  89.     @SuppressWarnings("deprecation")
  90.     public void despawn() {
  91.         PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(new int[]{this.entityID});
  92.         this.removeFromTablist();
  93.         for(Player online : Bukkit.getOnlinePlayers()) {
  94.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  95.         }
  96.     }
  97.     @SuppressWarnings("deprecation")
  98.     public void changePlayerlistName(String name) {
  99.         try{
  100.             PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.UPDATE_DISPLAY_NAME);
  101.             PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(this.profile, 0, WorldSettings.EnumGamemode.NOT_SET, CraftChatMessage.fromString(name)[0]);
  102.             @SuppressWarnings("unchecked") List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) this.getValue(packet, "b");
  103.             players.add(data);
  104.             this.setValue(packet, "b", players);
  105.             this.tablist = name;
  106.             for(Player online : Bukkit.getOnlinePlayers()) {
  107.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  108.             }
  109.         }catch(Exception e) {
  110.             e.printStackTrace();
  111.         }
  112.     }
  113.     @SuppressWarnings("deprecation")
  114.     private void addToTablist() {
  115.         try {
  116.             PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
  117.             GameProfile profile = this.profile = this.getProfile();
  118.             PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(profile, 1, WorldSettings.EnumGamemode.NOT_SET, CraftChatMessage.fromString(tablist)[0]);
  119.             @SuppressWarnings("unchecked") List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) getValue(packet, "b");
  120.             players.add(data);
  121.             setValue(packet, "a", PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER);
  122.             setValue(packet, "b", players);
  123.             for(Player online : Bukkit.getOnlinePlayers()) {
  124.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  125.             }
  126.         } catch (Exception e) {
  127.             e.printStackTrace();
  128.         }
  129.     }
  130.     @SuppressWarnings("deprecation")
  131.     private void removeFromTablist() {
  132.         try{
  133.             PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER);
  134.             PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(this.profile, -1, null, null);
  135.             @SuppressWarnings("unchecked") List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) this.getValue(packet, "b");
  136.             players.add(data);
  137.             this.setValue(packet, "b", players);
  138.             for(Player online : Bukkit.getOnlinePlayers()) {
  139.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  140.             }
  141.         }catch(Exception e) {
  142.             e.printStackTrace();
  143.         }
  144.     }
  145.     @SuppressWarnings("deprecation")
  146.     public void teleport(Location location) {
  147.         try{
  148.             PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport();
  149.             this.setValue(packet, "a", this.entityID);
  150.             this.setValue(packet, "b", this.toFixedPoint(location.getX()));
  151.             this.setValue(packet, "c", this.toFixedPoint(location.getY()));
  152.             this.setValue(packet, "d", this.toFixedPoint(location.getZ()));
  153.             this.setValue(packet, "e", this.toPackedByte(location.getYaw()));
  154.             this.setValue(packet, "f", this.toPackedByte(location.getPitch()));
  155.             this.setValue(packet, "g", this.location.getBlock().getType() != Material.AIR);
  156.             this.location = location;
  157.             for(Player online : Bukkit.getOnlinePlayers()) {
  158.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  159.             }
  160.         }catch(Exception e) {
  161.             e.printStackTrace();
  162.         }
  163.     }
  164.     @SuppressWarnings("deprecation")
  165.     public void setItemInHand(Material material) {
  166.         try{
  167.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  168.             this.setValue(packet, "a", this.entityID);
  169.             this.setValue(packet, "b", 0);
  170.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  171.             this.inHand = material;
  172.             for(Player online : Bukkit.getOnlinePlayers()) {
  173.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  174.             }
  175.         }catch(Exception e) {
  176.             e.printStackTrace();
  177.         }
  178.     }
  179.     public Material getItemInHand() {
  180.         return this.inHand;
  181.     }
  182.     @SuppressWarnings("deprecation")
  183.     public void setHelmet(Material material) {
  184.         try{
  185.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  186.             this.setValue(packet, "a", this.entityID);
  187.             this.setValue(packet, "b", 4);
  188.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  189.             this.helmet = material;
  190.             for(Player online : Bukkit.getOnlinePlayers()) {
  191.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  192.             }
  193.         }catch(Exception e) {
  194.             e.printStackTrace();
  195.         }
  196.     }
  197.     public Material getHelmet() {
  198.         return this.helmet;
  199.     }
  200.     @SuppressWarnings("deprecation")
  201.     public void setChestplate(Material material) {
  202.         try{
  203.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  204.             this.setValue(packet, "a", this.entityID);
  205.             this.setValue(packet, "b", 3);
  206.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  207.             this.chestplate = material;
  208.             for(Player online : Bukkit.getOnlinePlayers()) {
  209.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  210.             }
  211.         }catch(Exception e) {
  212.             e.printStackTrace();
  213.         }
  214.     }
  215.     public Material getChestplate() {
  216.         return this.chestplate;
  217.     }
  218.     @SuppressWarnings("deprecation")
  219.     public void setLeggings(Material material) {
  220.         try{
  221.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  222.             this.setValue(packet, "a", this.entityID);
  223.             this.setValue(packet, "b", 2);
  224.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  225.             this.leggings = material;
  226.             for(Player online : Bukkit.getOnlinePlayers()) {
  227.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  228.             }
  229.         }catch(Exception e) {
  230.             e.printStackTrace();
  231.         }
  232.     }
  233.     public Material getLeggings() {
  234.         return this.leggings;
  235.     }
  236.     @SuppressWarnings("deprecation")
  237.     public void setBoots(Material material) {
  238.         try{
  239.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  240.             this.setValue(packet, "a", this.entityID);
  241.             this.setValue(packet, "b", 1);
  242.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  243.             this.boots = material;
  244.             for(Player online : Bukkit.getOnlinePlayers()) {
  245.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  246.             }
  247.         }catch(Exception e) {
  248.             e.printStackTrace();
  249.         }
  250.     }
  251.     public Material getBoots() {
  252.         return this.boots;
  253.     }
  254.     public int getEntityID() {
  255.         return this.entityID;
  256.     }
  257.     public UUID getUUID() {
  258.         return this.profile.getId();
  259.     }
  260.     public Location getLocation() {
  261.         return this.location;
  262.     }
  263.     public String getName() {
  264.         return this.name;
  265.     }
  266.     public String getPlayerlistName() {
  267.         return this.tablist;
  268.     }
  269.     private void setValue(Object instance, String field, Object value) throws Exception {
  270.         Field f = instance.getClass().getDeclaredField(field);
  271.         f.setAccessible(true);
  272.         f.set(instance, value);
  273.     }
  274.     private Object getValue(Object instance, String field) throws Exception {
  275.         Field f = instance.getClass().getDeclaredField(field);
  276.         f.setAccessible(true);
  277.         return f.get(instance);
  278.     }
  279.     private int toFixedPoint(double d) {
  280.         return (int) (d * 32.0);
  281.     }
  282.     private byte toPackedByte(float f) {
  283.         return (byte) ((int) (f * 256.0F / 360.0F));
  284.     }
  285.     private GameProfile getProfile() {
  286.         try {
  287.             GameProfile profile = GameProfileBuilder.fetch(UUIDFetcher.getUUID(ChatColor.stripColor(this.name)));
  288.             Field name = profile.getClass().getDeclaredField("name");
  289.             name.setAccessible(true);
  290.             name.set(profile, this.name);
  291.             return profile;
  292.         } catch (Exception e) {
  293.             return new GameProfile(UUID.randomUUID(), this.name);
  294.         }
  295.     }
  296.     public static class GameProfileBuilder {
  297.         private static final String SERVICE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false";
  298.         private static final String JSON_SKIN = "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"}}}";
  299.         private static final String JSON_CAPE = "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"},\"CAPE\":{\"url\":\"%s\"}}}";
  300.         private static Gson gson = new GsonBuilder().disableHtmlEscaping().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(GameProfile.class, new GameProfileSerializer()).registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create();
  301.         private static HashMap<UUID, CachedProfile> cache = new HashMap<UUID, CachedProfile>();
  302.         private static long cacheTime = -1;
  303.         /**
  304.          * Don't run in main thread!
  305.          *
  306.          * Fetches the GameProfile from the Mojang servers
  307.          *
  308.          * @param uuid The player uuid
  309.          * @return The GameProfile
  310.          * @throws IOException If something wents wrong while fetching
  311.          * @see GameProfile
  312.          */
  313.         public static GameProfile fetch(UUID uuid) throws IOException {
  314.             return fetch(uuid, false);
  315.         }
  316.         /**
  317.          * Don't run in main thread!
  318.          *
  319.          * Fetches the GameProfile from the Mojang servers
  320.          * @param uuid The player uuid
  321.          * @param forceNew If true the cache is ignored
  322.          * @return The GameProfile
  323.          * @throws IOException If something wents wrong while fetching
  324.          * @see GameProfile
  325.          */
  326.         public static GameProfile fetch(UUID uuid, boolean forceNew) throws IOException {
  327.             if (!forceNew && cache.containsKey(uuid) && cache.get(uuid).isValid()) {
  328.                 return cache.get(uuid).profile;
  329.             } else {
  330.                 HttpURLConnection connection = (HttpURLConnection) new URL(String.format(SERVICE_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection();
  331.                 connection.setReadTimeout(5000);
  332.                 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
  333.                     String json = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();
  334.                     GameProfile result = gson.fromJson(json, GameProfile.class);
  335.                     cache.put(uuid, new CachedProfile(result));
  336.                     return result;
  337.                 } else {
  338.                     if (!forceNew && cache.containsKey(uuid)) {
  339.                         return cache.get(uuid).profile;
  340.                     }
  341.                     JsonObject error = (JsonObject) new JsonParser().parse(new BufferedReader(new InputStreamReader(connection.getErrorStream())).readLine());
  342.                     throw new IOException(error.get("error").getAsString() + ": " + error.get("errorMessage").getAsString());
  343.                 }
  344.             }
  345.         }
  346.         /**
  347.          * Builds a GameProfile for the specified args
  348.          *
  349.          * @param uuid The uuid
  350.          * @param name The name
  351.          * @param skin The url from the skin image
  352.          * @return A GameProfile built from the arguments
  353.          * @see GameProfile
  354.          */
  355.         public static GameProfile getProfile(UUID uuid, String name, String skin) {
  356.             return getProfile(uuid, name, skin, null);
  357.         }
  358.         /**
  359.          * Builds a GameProfile for the specified args
  360.          *
  361.          * @param uuid The uuid
  362.          * @param name The name
  363.          * @param skinUrl Url from the skin image
  364.          * @param capeUrl Url from the cape image
  365.          * @return A GameProfile built from the arguments
  366.          * @see GameProfile
  367.          */
  368.         public static GameProfile getProfile(UUID uuid, String name, String skinUrl, String capeUrl) {
  369.             GameProfile profile = new GameProfile(uuid, name);
  370.             boolean cape = capeUrl != null && !capeUrl.isEmpty();
  371.             List<Object> args = new ArrayList<Object>();
  372.             args.add(System.currentTimeMillis());
  373.             args.add(UUIDTypeAdapter.fromUUID(uuid));
  374.             args.add(name);
  375.             args.add(skinUrl);
  376.             if (cape) args.add(capeUrl);
  377.             profile.getProperties().put("textures", new Property("textures", Base64Coder.encodeString(String.format(cape ? JSON_CAPE : JSON_SKIN, args.toArray(new Object[args.size()])))));
  378.             return profile;
  379.         }
  380.         /**
  381.          * Sets the time as long as you want to keep the gameprofiles in cache (-1 = never remove it)
  382.          * @param time cache time (default = -1)
  383.          */
  384.         public static void setCacheTime(long time) {
  385.             cacheTime = time;
  386.         }
  387.         private static class GameProfileSerializer implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile> {
  388.             public GameProfile deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
  389.                 JsonObject object = (JsonObject) json;
  390.                 UUID id = object.has("id") ? (UUID) context.deserialize(object.get("id"), UUID.class) : null;
  391.                 String name = object.has("name") ? object.getAsJsonPrimitive("name").getAsString() : null;
  392.                 GameProfile profile = new GameProfile(id, name);
  393.                 if (object.has("properties")) {
  394.                     for (Map.Entry<String, Property> prop : ((PropertyMap) context.deserialize(object.get("properties"), PropertyMap.class)).entries()) {
  395.                         profile.getProperties().put(prop.getKey(), prop.getValue());
  396.                     }
  397.                 }
  398.                 return profile;
  399.             }
  400.             public JsonElement serialize(GameProfile profile, Type type, JsonSerializationContext context) {
  401.                 JsonObject result = new JsonObject();
  402.                 if (profile.getId() != null)
  403.                     result.add("id", context.serialize(profile.getId()));
  404.                 if (profile.getName() != null)
  405.                     result.addProperty("name", profile.getName());
  406.                 if (!profile.getProperties().isEmpty())
  407.                     result.add("properties", context.serialize(profile.getProperties()));
  408.                 return result;
  409.             }
  410.         }
  411.         private static class CachedProfile {
  412.             private long timestamp = System.currentTimeMillis();
  413.             private GameProfile profile;
  414.             public CachedProfile(GameProfile profile) {
  415.                 this.profile = profile;
  416.             }
  417.             public boolean isValid() {
  418.                 return cacheTime < 0 || (System.currentTimeMillis() - timestamp) < cacheTime;
  419.             }
  420.         }
  421.     }
  422.     public static class UUIDFetcher {
  423.         /**
  424.          * Date when name changes were introduced
  425.          * @see UUIDFetcher#getUUIDAt(String, long)
  426.          */
  427.         public static final long FEBRUARY_2015 = 1422748800000L;
  428.         private static Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
  429.         private static final String UUID_URL = "https://api.mojang.com/users/profiles/minecraft/%s?at=%d";
  430.         private static final String NAME_URL = "https://api.mojang.com/user/profiles/%s/names";
  431.         private static Map<String, UUID> uuidCache = new HashMap<String, UUID>();
  432.         private static Map<UUID, String> nameCache = new HashMap<UUID, String>();
  433.         private static ExecutorService pool = Executors.newCachedThreadPool();
  434.         private String name;
  435.         private UUID id;
  436.         /**
  437.          * Fetches the uuid asynchronously and passes it to the consumer
  438.          *
  439.          * @param name The name
  440.          * @param action Do what you want to do with the uuid her
  441.          */
  442.         public static void getUUID(final String name, Consumer<UUID> action) {
  443.             pool.execute(new Acceptor<UUID>(action) {
  444.                 @Override
  445.                 public UUID getValue() {
  446.                     return getUUID(name);
  447.                 }
  448.             });
  449.         }
  450.         /**
  451.          * Fetches the uuid synchronously and returns it
  452.          *
  453.          * @param name The name
  454.          * @return The uuid
  455.          */
  456.         public static UUID getUUID(String name) {
  457.             return getUUIDAt(name, System.currentTimeMillis());
  458.         }
  459.         /**
  460.          * Fetches the uuid synchronously for a specified name and time and passes the result to the consumer
  461.          *
  462.          * @param name The name
  463.          * @param timestamp Time when the player had this name in milliseconds
  464.          * @param action Do what you want to do with the uuid her
  465.          */
  466.         public static void getUUIDAt(final String name, final long timestamp, Consumer<UUID> action) {
  467.             pool.execute(new Acceptor<UUID>(action) {
  468.                 @Override
  469.                 public UUID getValue() {
  470.                     return getUUIDAt(name, timestamp);
  471.                 }
  472.             });
  473.         }
  474.         /**
  475.          * Fetches the uuid synchronously for a specified name and time
  476.          *
  477.          * @param name The name
  478.          * @param timestamp Time when the player had this name in milliseconds
  479.          * @see UUIDFetcher#FEBRUARY_2015
  480.          */
  481.         public static UUID getUUIDAt(String name, long timestamp) {
  482.             name = name.toLowerCase();
  483.             if (uuidCache.containsKey(name)) {
  484.                 return uuidCache.get(name);
  485.             }
  486.             try {
  487.                 HttpURLConnection connection = (HttpURLConnection) new URL(String.format(UUID_URL, name, timestamp/1000)).openConnection();
  488.                 connection.setReadTimeout(5000);
  489.                 UUIDFetcher data = gson.fromJson(new BufferedReader(new InputStreamReader(connection.getInputStream())), UUIDFetcher.class);
  490.                 uuidCache.put(name, data.id);
  491.                 nameCache.put(data.id, data.name);
  492.                 return data.id;
  493.             } catch (Exception e) {
  494.                 e.printStackTrace();
  495.             }
  496.             return null;
  497.         }
  498.         /**
  499.          * Fetches the name asynchronously and passes it to the consumer
  500.          *
  501.          * @param uuid The uuid
  502.          * @param action Do what you want to do with the name her
  503.          */
  504.         public static void getName(final UUID uuid, Consumer<String> action) {
  505.             pool.execute(new Acceptor<String>(action) {
  506.                 @Override
  507.                 public String getValue() {
  508.                     return getName(uuid);
  509.                 }
  510.             });
  511.         }
  512.         /**
  513.          * Fetches the name synchronously and returns it
  514.          *
  515.          * @param uuid The uuid
  516.          * @return The name
  517.          */
  518.         public static String getName(UUID uuid) {
  519.             if (nameCache.containsKey(uuid)) {
  520.                 return nameCache.get(uuid);
  521.             }
  522.             try {
  523.                 HttpURLConnection connection = (HttpURLConnection) new URL(String.format(NAME_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection();
  524.                 connection.setReadTimeout(5000);
  525.                 UUIDFetcher[] nameHistory = gson.fromJson(new BufferedReader(new InputStreamReader(connection.getInputStream())), UUIDFetcher[].class);
  526.                 UUIDFetcher currentNameData = nameHistory[nameHistory.length - 1];
  527.                 uuidCache.put(currentNameData.name.toLowerCase(), uuid);
  528.                 nameCache.put(uuid, currentNameData.name);
  529.                 return currentNameData.name;
  530.             } catch (Exception e) {
  531.                 e.printStackTrace();
  532.             }
  533.             return null;
  534.         }
  535.         public static interface Consumer<T> {
  536.             void accept(T t);
  537.         }
  538.         public static abstract class Acceptor<T> implements Runnable {
  539.             private Consumer<T> consumer;
  540.             public Acceptor(Consumer<T> consumer) {
  541.                 this.consumer = consumer;
  542.             }
  543.             public abstract T getValue();
  544.             @Override
  545.             public void run() {
  546.                 consumer.accept(getValue());
  547.             }
  548.         }
  549.     }
  550. }


Verwendung ist weiterhin wie oben, jedoch muss ab sofort das spawnen asynchron vorgenommen werden!

Dafür einfach folgendes machen:
Code: Alles auswählen
  1. new Thread(new Runnable() { public void run() { new NPC("Summerfeeling", new Location(Bukkit.getWorld("world"), 0, 0 ,0).spawn() } }).start();


Mir ist noch eine Sache aufgefallen, die interessant sein könnte. Solltet ihr einen NPC mit den Namen eines Onlinespielers erstellen, so könnt ihr diesen nicht löschen, solange der Spieler online ist!

Hoffe euch gefällt die Klasse weiterhin, und wenn ihr Bugs findet, ab in die Kommentare damit ;)

Neues Update! Fake Skins
Gleiche Dinge wie oben, sowie im letzen Post von mir (Seite 3) beachten.

Code: Alles auswählen
  1. package de.summerfeeling.postcrafter.entity;
  2. import com.google.gson.*;
  3. import com.mojang.authlib.GameProfile;
  4. import com.mojang.authlib.properties.Property;
  5. import com.mojang.authlib.properties.PropertyMap;
  6. import com.mojang.util.UUIDTypeAdapter;
  7. import net.md_5.bungee.api.ChatColor;
  8. import net.minecraft.server.v1_8_R3.*;
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.Location;
  11. import org.bukkit.Material;
  12. import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
  13. import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
  14. import org.bukkit.craftbukkit.v1_8_R3.util.CraftChatMessage;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.inventory.ItemStack;
  17. import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.lang.reflect.Field;
  22. import java.lang.reflect.Type;
  23. import java.net.HttpURLConnection;
  24. import java.net.URL;
  25. import java.util.*;
  26. import java.util.concurrent.ExecutorService;
  27. import java.util.concurrent.Executors;
  28. public class NPC {
  29.     private DataWatcher watcher;
  30.     private GameProfile profile;
  31.     private Material chestplate;
  32.     private boolean hideTablist;
  33.     private Material leggings;
  34.     private Location location;
  35.     private String skinName;
  36.     private Material inHand;
  37.     private Material helmet;
  38.     private Material boots;
  39.     private String tablist;
  40.     private int entityID;
  41.     private String name;
  42.    /*
  43.     * NPC, a class for spawning fake players in the 1.8
  44.    Copyright (C) [Summerfeeling]
  45.    Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation veröffentlicht, weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
  46.    Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÃœR EINEN BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
  47.    Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>.
  48.     */
  49.     public NPC(String skinName, String name, String tablist, int entityID, Location location, Material inHand, boolean hideTablist) {
  50.         this.location = location;
  51.         this.tablist = ChatColor.translateAlternateColorCodes('&', tablist);
  52.         this.name = ChatColor.translateAlternateColorCodes('&', name);
  53.         this.entityID = entityID;
  54.         this.inHand = inHand;
  55.         this.skinName = skinName;
  56.         this.watcher = new DataWatcher(null);
  57.         this.hideTablist = hideTablist;
  58.         watcher.a(6, (float) 20);
  59.     }
  60.     public NPC(String name, Location location, boolean hideTablist) {
  61.         this(null, name, name, new Random().nextInt(10000), location, Material.AIR, hideTablist);
  62.     }
  63.     public NPC(String skinName, String name, Location location, boolean hideTablist) {
  64.         this(skinName, name, name, new Random().nextInt(10000), location, Material.AIR, hideTablist);
  65.     }
  66.     public NPC(String name, Location location, Material inHand, boolean hideTablist) {
  67.         this(null, name, name, new Random().nextInt(10000), location, inHand, hideTablist);
  68.     }
  69.     public NPC(String name, String tablist, Location location, Material inHand, boolean hideTablist) {
  70.         this(null, name, tablist, new Random().nextInt(10000), location, inHand, hideTablist);
  71.     }
  72.     @SuppressWarnings("deprecation")
  73.     public void spawn() {
  74.         try{
  75.             PacketPlayOutNamedEntitySpawn packet = new PacketPlayOutNamedEntitySpawn();
  76.             addToTablist();
  77.             setValue(packet, "a", entityID);
  78.             setValue(packet, "b", this.profile.getId());
  79.             setValue(packet, "c", toFixedPoint(location.getX()));
  80.             setValue(packet, "d", toFixedPoint(location.getY()));
  81.             setValue(packet, "e", toFixedPoint(location.getZ()));
  82.             setValue(packet, "f", toPackedByte(location.getYaw()));
  83.             setValue(packet, "g", toPackedByte(location.getPitch()));
  84.             setValue(packet, "h", inHand == null ? 0 : inHand.getId());
  85.             setValue(packet, "i", watcher);
  86.             for(Player online : Bukkit.getOnlinePlayers()) {
  87.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  88.             }
  89.             if (hideTablist) removeFromTablist();
  90.         }catch(Exception e) {
  91.             e.printStackTrace();
  92.         }
  93.     }
  94.     @SuppressWarnings("deprecation")
  95.     public void despawn() {
  96.         PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(new int[]{this.entityID});
  97.         this.removeFromTablist();
  98.         for(Player online : Bukkit.getOnlinePlayers()) {
  99.             ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  100.         }
  101.     }
  102.     @SuppressWarnings("deprecation")
  103.     public void changePlayerlistName(String name) {
  104.         try{
  105.             PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.UPDATE_DISPLAY_NAME);
  106.             PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(this.profile, 0, WorldSettings.EnumGamemode.NOT_SET, CraftChatMessage.fromString(name)[0]);
  107.             @SuppressWarnings("unchecked") List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) this.getValue(packet, "b");
  108.             players.add(data);
  109.             this.setValue(packet, "b", players);
  110.             this.tablist = name;
  111.             for(Player online : Bukkit.getOnlinePlayers()) {
  112.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  113.             }
  114.         }catch(Exception e) {
  115.             e.printStackTrace();
  116.         }
  117.     }
  118.     @SuppressWarnings("deprecation")
  119.     private void addToTablist() {
  120.         try {
  121.             PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
  122.             GameProfile profile = this.profile = this.getProfile();
  123.             PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(profile, 1, WorldSettings.EnumGamemode.NOT_SET, CraftChatMessage.fromString(tablist)[0]);
  124.             @SuppressWarnings("unchecked") List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) getValue(packet, "b");
  125.             players.add(data);
  126.             setValue(packet, "a", PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER);
  127.             setValue(packet, "b", players);
  128.             for(Player online : Bukkit.getOnlinePlayers()) {
  129.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  130.             }
  131.         } catch (Exception e) {
  132.             e.printStackTrace();
  133.         }
  134.     }
  135.     @SuppressWarnings("deprecation")
  136.     private void removeFromTablist() {
  137.         try{
  138.             PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER);
  139.             PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(this.profile, -1, null, null);
  140.             @SuppressWarnings("unchecked") List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) this.getValue(packet, "b");
  141.             players.add(data);
  142.             this.setValue(packet, "b", players);
  143.             for(Player online : Bukkit.getOnlinePlayers()) {
  144.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  145.             }
  146.         }catch(Exception e) {
  147.             e.printStackTrace();
  148.         }
  149.     }
  150.     @SuppressWarnings("deprecation")
  151.     public void teleport(Location location) {
  152.         try{
  153.             PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport();
  154.             this.setValue(packet, "a", this.entityID);
  155.             this.setValue(packet, "b", this.toFixedPoint(location.getX()));
  156.             this.setValue(packet, "c", this.toFixedPoint(location.getY()));
  157.             this.setValue(packet, "d", this.toFixedPoint(location.getZ()));
  158.             this.setValue(packet, "e", this.toPackedByte(location.getYaw()));
  159.             this.setValue(packet, "f", this.toPackedByte(location.getPitch()));
  160.             this.setValue(packet, "g", this.location.getBlock().getType() != Material.AIR);
  161.             this.location = location;
  162.             for(Player online : Bukkit.getOnlinePlayers()) {
  163.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  164.             }
  165.         }catch(Exception e) {
  166.             e.printStackTrace();
  167.         }
  168.     }
  169.     @SuppressWarnings("deprecation")
  170.     public void setItemInHand(Material material) {
  171.         try{
  172.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  173.             this.setValue(packet, "a", this.entityID);
  174.             this.setValue(packet, "b", 0);
  175.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  176.             this.inHand = material;
  177.             for(Player online : Bukkit.getOnlinePlayers()) {
  178.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  179.             }
  180.         }catch(Exception e) {
  181.             e.printStackTrace();
  182.         }
  183.     }
  184.     public Material getItemInHand() {
  185.         return this.inHand;
  186.     }
  187.     @SuppressWarnings("deprecation")
  188.     public void setHelmet(Material material) {
  189.         try{
  190.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  191.             this.setValue(packet, "a", this.entityID);
  192.             this.setValue(packet, "b", 4);
  193.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  194.             this.helmet = material;
  195.             for(Player online : Bukkit.getOnlinePlayers()) {
  196.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  197.             }
  198.         }catch(Exception e) {
  199.             e.printStackTrace();
  200.         }
  201.     }
  202.     public Material getHelmet() {
  203.         return this.helmet;
  204.     }
  205.     @SuppressWarnings("deprecation")
  206.     public void setChestplate(Material material) {
  207.         try{
  208.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  209.             this.setValue(packet, "a", this.entityID);
  210.             this.setValue(packet, "b", 3);
  211.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  212.             this.chestplate = material;
  213.             for(Player online : Bukkit.getOnlinePlayers()) {
  214.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  215.             }
  216.         }catch(Exception e) {
  217.             e.printStackTrace();
  218.         }
  219.     }
  220.     public Material getChestplate() {
  221.         return this.chestplate;
  222.     }
  223.     @SuppressWarnings("deprecation")
  224.     public void setLeggings(Material material) {
  225.         try{
  226.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  227.             this.setValue(packet, "a", this.entityID);
  228.             this.setValue(packet, "b", 2);
  229.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  230.             this.leggings = material;
  231.             for(Player online : Bukkit.getOnlinePlayers()) {
  232.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  233.             }
  234.         }catch(Exception e) {
  235.             e.printStackTrace();
  236.         }
  237.     }
  238.     public Material getLeggings() {
  239.         return this.leggings;
  240.     }
  241.     @SuppressWarnings("deprecation")
  242.     public void setBoots(Material material) {
  243.         try{
  244.             PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  245.             this.setValue(packet, "a", this.entityID);
  246.             this.setValue(packet, "b", 1);
  247.             this.setValue(packet, "c", material == Material.AIR || material == null ? CraftItemStack.asNMSCopy(new ItemStack(Material.AIR)) : CraftItemStack.asNMSCopy(new ItemStack(material)));
  248.             this.boots = material;
  249.             for(Player online : Bukkit.getOnlinePlayers()) {
  250.                 ((CraftPlayer) online).getHandle().playerConnection.sendPacket(packet);
  251.             }
  252.         }catch(Exception e) {
  253.             e.printStackTrace();
  254.         }
  255.     }
  256.     public Material getBoots() {
  257.         return this.boots;
  258.     }
  259.     public int getEntityID() {
  260.         return this.entityID;
  261.     }
  262.     public UUID getUUID() {
  263.         return this.profile.getId();
  264.     }
  265.     public Location getLocation() {
  266.         return this.location;
  267.     }
  268.     public String getName() {
  269.         return this.name;
  270.     }
  271.     public String getPlayerlistName() {
  272.         return this.tablist;
  273.     }
  274.     private void setValue(Object instance, String field, Object value) throws Exception {
  275.         Field f = instance.getClass().getDeclaredField(field);
  276.         f.setAccessible(true);
  277.         f.set(instance, value);
  278.     }
  279.     private Object getValue(Object instance, String field) throws Exception {
  280.         Field f = instance.getClass().getDeclaredField(field);
  281.         f.setAccessible(true);
  282.         return f.get(instance);
  283.     }
  284.     private int toFixedPoint(double d) {
  285.         return (int) (d * 32.0);
  286.     }
  287.     private byte toPackedByte(float f) {
  288.         return (byte) ((int) (f * 256.0F / 360.0F));
  289.     }
  290.     private GameProfile getProfile() {
  291.         try {
  292.             GameProfile profile = GameProfileBuilder.fetch(UUIDFetcher.getUUID(ChatColor.stripColor(this.name)));
  293.             Field name = profile.getClass().getDeclaredField("name");
  294.             name.setAccessible(true);
  295.             name.set(profile, this.name);
  296.             return profile;
  297.         } catch (Exception e) {
  298.             return getFakeProfile();
  299.         }
  300.     }
  301.     private GameProfile getFakeProfile() {
  302.         try {
  303.             GameProfile profile = GameProfileBuilder.fetch(UUIDFetcher.getUUID(ChatColor.stripColor(this.skinName)));
  304.             Field name = profile.getClass().getDeclaredField("name");
  305.             name.setAccessible(true);
  306.             name.set(profile, this.name);
  307.             return profile;
  308.         } catch (Exception e) {
  309.             return new GameProfile(UUID.randomUUID(), this.name);
  310.         }
  311.     }
  312.     public static class GameProfileBuilder {
  313.         private static final String SERVICE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false";
  314.         private static final String JSON_SKIN = "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"}}}";
  315.         private static final String JSON_CAPE = "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"},\"CAPE\":{\"url\":\"%s\"}}}";
  316.         private static Gson gson = new GsonBuilder().disableHtmlEscaping().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(GameProfile.class, new GameProfileSerializer()).registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create();
  317.         private static HashMap<UUID, CachedProfile> cache = new HashMap<UUID, CachedProfile>();
  318.         private static long cacheTime = -1;
  319.         /**
  320.          * Don't run in main thread!
  321.          *
  322.          * Fetches the GameProfile from the Mojang servers
  323.          *
  324.          * @param uuid The player uuid
  325.          * @return The GameProfile
  326.          * @throws IOException If something wents wrong while fetching
  327.          * @see GameProfile
  328.          */
  329.         public static GameProfile fetch(UUID uuid) throws IOException {
  330.             return fetch(uuid, false);
  331.         }
  332.         /**
  333.          * Don't run in main thread!
  334.          *
  335.          * Fetches the GameProfile from the Mojang servers
  336.          * @param uuid The player uuid
  337.          * @param forceNew If true the cache is ignored
  338.          * @return The GameProfile
  339.          * @throws IOException If something wents wrong while fetching
  340.          * @see GameProfile
  341.          */
  342.         public static GameProfile fetch(UUID uuid, boolean forceNew) throws IOException {
  343.             if (!forceNew && cache.containsKey(uuid) && cache.get(uuid).isValid()) {
  344.                 return cache.get(uuid).profile;
  345.             } else {
  346.                 HttpURLConnection connection = (HttpURLConnection) new URL(String.format(SERVICE_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection();
  347.                 connection.setReadTimeout(5000);
  348.                 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
  349.                     String json = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();
  350.                     GameProfile result = gson.fromJson(json, GameProfile.class);
  351.                     cache.put(uuid, new CachedProfile(result));
  352.                     return result;
  353.                 } else {
  354.                     if (!forceNew && cache.containsKey(uuid)) {
  355.                         return cache.get(uuid).profile;
  356.                     }
  357.                     JsonObject error = (JsonObject) new JsonParser().parse(new BufferedReader(new InputStreamReader(connection.getErrorStream())).readLine());
  358.                     throw new IOException(error.get("error").getAsString() + ": " + error.get("errorMessage").getAsString());
  359.                 }
  360.             }
  361.         }
  362.         /**
  363.          * Builds a GameProfile for the specified args
  364.          *
  365.          * @param uuid The uuid
  366.          * @param name The name
  367.          * @param skin The url from the skin image
  368.          * @return A GameProfile built from the arguments
  369.          * @see GameProfile
  370.          */
  371.         public static GameProfile getProfile(UUID uuid, String name, String skin) {
  372.             return getProfile(uuid, name, skin, null);
  373.         }
  374.         /**
  375.          * Builds a GameProfile for the specified args
  376.          *
  377.          * @param uuid The uuid
  378.          * @param name The name
  379.          * @param skinUrl Url from the skin image
  380.          * @param capeUrl Url from the cape image
  381.          * @return A GameProfile built from the arguments
  382.          * @see GameProfile
  383.          */
  384.         public static GameProfile getProfile(UUID uuid, String name, String skinUrl, String capeUrl) {
  385.             GameProfile profile = new GameProfile(uuid, name);
  386.             boolean cape = capeUrl != null && !capeUrl.isEmpty();
  387.             List<Object> args = new ArrayList<>();
  388.             args.add(System.currentTimeMillis());
  389.             args.add(UUIDTypeAdapter.fromUUID(uuid));
  390.             args.add(name);
  391.             args.add(skinUrl);
  392.             if (cape) args.add(capeUrl);
  393.             profile.getProperties().put("textures", new Property("textures", Base64Coder.encodeString(String.format(cape ? JSON_CAPE : JSON_SKIN, args.toArray(new Object[args.size()])))));
  394.             return profile;
  395.         }
  396.         /**
  397.          * Sets the time as long as you want to keep the gameprofiles in cache (-1 = never remove it)
  398.          * @param time cache time (default = -1)
  399.          */
  400.         public static void setCacheTime(long time) {
  401.             cacheTime = time;
  402.         }
  403.         private static class GameProfileSerializer implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile> {
  404.             public GameProfile deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
  405.                 JsonObject object = (JsonObject) json;
  406.                 UUID id = object.has("id") ? (UUID) context.deserialize(object.get("id"), UUID.class) : null;
  407.                 String name = object.has("name") ? object.getAsJsonPrimitive("name").getAsString() : null;
  408.                 GameProfile profile = new GameProfile(id, name);
  409.                 if (object.has("properties")) {
  410.                     for (Map.Entry<String, Property> prop : ((PropertyMap) context.deserialize(object.get("properties"), PropertyMap.class)).entries()) {
  411.                         profile.getProperties().put(prop.getKey(), prop.getValue());
  412.                     }
  413.                 }
  414.                 return profile;
  415.             }
  416.             public JsonElement serialize(GameProfile profile, Type type, JsonSerializationContext context) {
  417.                 JsonObject result = new JsonObject();
  418.                 if (profile.getId() != null)
  419.                     result.add("id", context.serialize(profile.getId()));
  420.                 if (profile.getName() != null)
  421.                     result.addProperty("name", profile.getName());
  422.                 if (!profile.getProperties().isEmpty())
  423.                     result.add("properties", context.serialize(profile.getProperties()));
  424.                 return result;
  425.             }
  426.         }
  427.         private static class CachedProfile {
  428.             private long timestamp = System.currentTimeMillis();
  429.             private GameProfile profile;
  430.             public CachedProfile(GameProfile profile) {
  431.                 this.profile = profile;
  432.             }
  433.             public boolean isValid() {
  434.                 return cacheTime < 0 || (System.currentTimeMillis() - timestamp) < cacheTime;
  435.             }
  436.         }
  437.     }
  438.     public static class UUIDFetcher {
  439.         /**
  440.          * Date when name changes were introduced
  441.          * @see UUIDFetcher#getUUIDAt(String, long)
  442.          */
  443.         public static final long FEBRUARY_2015 = 1422748800000L;
  444.         private static Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
  445.         private static final String UUID_URL = "https://api.mojang.com/users/profiles/minecraft/%s?at=%d";
  446.         private static final String NAME_URL = "https://api.mojang.com/user/profiles/%s/names";
  447.         private static Map<String, UUID> uuidCache = new HashMap<String, UUID>();
  448.         private static Map<UUID, String> nameCache = new HashMap<UUID, String>();
  449.         private static ExecutorService pool = Executors.newCachedThreadPool();
  450.         private String name;
  451.         private UUID id;
  452.         /**
  453.          * Fetches the uuid asynchronously and passes it to the consumer
  454.          *
  455.          * @param name The name
  456.          * @param action Do what you want to do with the uuid her
  457.          */
  458.         public static void getUUID(final String name, Consumer<UUID> action) {
  459.             pool.execute(new Acceptor<UUID>(action) {
  460.                 @Override
  461.                 public UUID getValue() {
  462.                     return getUUID(name);
  463.                 }
  464.             });
  465.         }
  466.         /**
  467.          * Fetches the uuid synchronously and returns it
  468.          *
  469.          * @param name The name
  470.          * @return The uuid
  471.          */
  472.         public static UUID getUUID(String name) {
  473.             return getUUIDAt(name, System.currentTimeMillis());
  474.         }
  475.         /**
  476.          * Fetches the uuid synchronously for a specified name and time and passes the result to the consumer
  477.          *
  478.          * @param name The name
  479.          * @param timestamp Time when the player had this name in milliseconds
  480.          * @param action Do what you want to do with the uuid her
  481.          */
  482.         public static void getUUIDAt(final String name, final long timestamp, Consumer<UUID> action) {
  483.             pool.execute(new Acceptor<UUID>(action) {
  484.                 @Override
  485.                 public UUID getValue() {
  486.                     return getUUIDAt(name, timestamp);
  487.                 }
  488.             });
  489.         }
  490.         /**
  491.          * Fetches the uuid synchronously for a specified name and time
  492.          *
  493.          * @param name The name
  494.          * @param timestamp Time when the player had this name in milliseconds
  495.          * @see UUIDFetcher#FEBRUARY_2015
  496.          */
  497.         public static UUID getUUIDAt(String name, long timestamp) {
  498.             name = name.toLowerCase();
  499.             if (uuidCache.containsKey(name)) {
  500.                 return uuidCache.get(name);
  501.             }
  502.             try {
  503.                 HttpURLConnection connection = (HttpURLConnection) new URL(String.format(UUID_URL, name, timestamp/1000)).openConnection();
  504.                 connection.setReadTimeout(5000);
  505.                 UUIDFetcher data = gson.fromJson(new BufferedReader(new InputStreamReader(connection.getInputStream())), UUIDFetcher.class);
  506.                 uuidCache.put(name, data.id);
  507.                 nameCache.put(data.id, data.name);
  508.                 return data.id;
  509.             } catch (Exception e) { }
  510.             return null;
  511.         }
  512.         /**
  513.          * Fetches the name asynchronously and passes it to the consumer
  514.          *
  515.          * @param uuid The uuid
  516.          * @param action Do what you want to do with the name her
  517.          */
  518.         public static void getName(final UUID uuid, Consumer<String> action) {
  519.             pool.execute(new Acceptor<String>(action) {
  520.                 @Override
  521.                 public String getValue() {
  522.                     return getName(uuid);
  523.                 }
  524.             });
  525.         }
  526.         /**
  527.          * Fetches the name synchronously and returns it
  528.          *
  529.          * @param uuid The uuid
  530.          * @return The name
  531.          */
  532.         public static String getName(UUID uuid) {
  533.             if (nameCache.containsKey(uuid)) {
  534.                 return nameCache.get(uuid);
  535.             }
  536.             try {
  537.                 HttpURLConnection connection = (HttpURLConnection) new URL(String.format(NAME_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection();
  538.                 connection.setReadTimeout(5000);
  539.                 UUIDFetcher[] nameHistory = gson.fromJson(new BufferedReader(new InputStreamReader(connection.getInputStream())), UUIDFetcher[].class);
  540.                 UUIDFetcher currentNameData = nameHistory[nameHistory.length - 1];
  541.                 uuidCache.put(currentNameData.name.toLowerCase(), uuid);
  542.                 nameCache.put(uuid, currentNameData.name);
  543.                 return currentNameData.name;
  544.             } catch (Exception e) {
  545.                 e.printStackTrace();
  546.             }
  547.             return null;
  548.         }
  549.         public static interface Consumer<T> {
  550.             void accept(T t);
  551.         }
  552.         public static abstract class Acceptor<T> implements Runnable {
  553.             private Consumer<T> consumer;
  554.             public Acceptor(Consumer<T> consumer) {
  555.                 this.consumer = consumer;
  556.             }
  557.             public abstract T getValue();
  558.             @Override
  559.             public void run() {
  560.                 consumer.accept(getValue());
  561.             }
  562.         }
  563.     }
  564. }


hideTablist NICHT getestet. Wenn nicht funktionstüchtig, bitte melden und Funktion löschen.

NEUES UPDATE! PlayerInteractNPCEvent. Siehe Seite 4
Zuletzt geändert von Summerfeeling am So 16. Aug 2015, 14:46, insgesamt 4-mal geändert.
Grüße
Summerfeeling | Timo
Benutzeravatar
Summerfeeling
 
Beiträge: 1300
Registriert: Sa 15. Jun 2013, 18:43
Wohnort: Viersen

Re: Eigene NPCs in der 1.8 erstellen!

Beitragvon Admiral_Zott » Di 30. Dez 2014, 12:35

Müssen wir das CopyRight übernehmen :D??
Sonst ist es ne schöne Class, werde ich auf jeden Fall mal verwenden. Darf ich diese Klasse auch in einem Video benutzen und erklären? Würde auch den Thread hier verlinken.
MFG Admiral_Zott
Benutzeravatar
Admiral_Zott
 
Beiträge: 220
Registriert: Do 10. Apr 2014, 11:56
Wohnort: Zu Hause

Re: Eigene NPCs in der 1.8 erstellen!

Beitragvon Sep2703 » Di 30. Dez 2014, 12:55

Ist es so schlimm, das Copyright zu übernehmen?
Zumal es unter der GNU-Lizenz 3 veröffentlicht ist, darfst du es doch im Prinzip frei verwenden.
Du möchtest programmieren lernen oder dein Bukkit-/Spigot-Wissen erweitern?
Hier habe ich für dich kostenlose Tutorials: https://youtube.com/janhektor
Benutzeravatar
Sep2703
 
Beiträge: 677
Registriert: Mi 8. Jan 2014, 15:13
Wohnort: 127.0.0.1

Re: Eigene NPCs in der 1.8 erstellen!

Beitragvon Summerfeeling » Di 30. Dez 2014, 13:15

Admiral_Zott hat geschrieben:Müssen wir das CopyRight übernehmen :D??
Sonst ist es ne schöne Class, werde ich auf jeden Fall mal verwenden. Darf ich diese Klasse auch in einem Video benutzen und erklären? Würde auch den Thread hier verlinken.
MFG Admiral_Zott

Nein, müsst ihr natürlich nicht.
Klar darfst du n Video über die Klasse machen, wenn du mir versprichst, dass dein Tutorial über Pets nichts mit AnimalTamern und Targets (von aggresiven Monstern) zu tun hat ;)
Grüße
Summerfeeling | Timo
Benutzeravatar
Summerfeeling
 
Beiträge: 1300
Registriert: Sa 15. Jun 2013, 18:43
Wohnort: Viersen

Re: Eigene NPCs in der 1.8 erstellen!

Beitragvon Gerredtor » Fr 2. Jan 2015, 02:22

ich möchte diese klasse gerne für ein AntiPvPLogger Plugin verwenden wie bekomme ich es nun hin das die npcs schaden nehmen und auch sterben können ?
Benutzeravatar
Gerredtor
 
Beiträge: 2
Registriert: Fr 2. Jan 2015, 02:21

Re: Eigene NPCs in der 1.8 erstellen!

Beitragvon Summerfeeling » Fr 2. Jan 2015, 14:15

Gerredtor hat geschrieben:ich möchte diese klasse gerne für ein AntiPvPLogger Plugin verwenden wie bekomme ich es nun hin das die npcs schaden nehmen und auch sterben können ?

Das dürfte mit der Klasse gar nicht gehen, soweit ich weiß. ;)
Grüße
Summerfeeling | Timo
Benutzeravatar
Summerfeeling
 
Beiträge: 1300
Registriert: Sa 15. Jun 2013, 18:43
Wohnort: Viersen

Re: Eigene NPCs in der 1.8 erstellen!

Beitragvon ImGameboy » Sa 3. Jan 2015, 11:49

Das Problem dabei ist ja, das es den Spieler für den Server gar nicht gibt.
Das heißt, du musst das mit dem Schaden selbst machen. Das kann man gut mit ProtocolLib machen.
Lückenstopfer ^^
Benutzeravatar
ImGameboy
 
Beiträge: 210
Registriert: Mi 17. Sep 2014, 15:25

Re: Eigene NPCs in der 1.8 erstellen!

Beitragvon Yekllurt » Sa 7. Feb 2015, 22:21

Hi,
das ist eine sehr schöne Klasse, ich hätte allerdings noch einen Vorschlag, der wäre du könntest machen, dass es noch eine moveToLocation(Location loc) Methode oder so etwas gibt.

Lg Yekllurt
Oh, schön, dass du die Signatur liest!
Benutzeravatar
Yekllurt
 
Beiträge: 23
Registriert: Sa 7. Feb 2015, 17:31

Re: Eigene NPCs in der 1.8 erstellen!

Beitragvon BlackMesaLab » Fr 27. Feb 2015, 18:16

Kann man mit dieser Klasse auch farbige Rüstung setzen ? Wenn ja wie? :D :D
Benutzeravatar
BlackMesaLab
 
Beiträge: 12
Registriert: So 3. Aug 2014, 21:09

Nächste

Zurück zu Anleitungen

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 2 Gäste