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.

Re: Eigene NPCs in der 1.8 erstellen! [UPDATE FÜR 1.8.3]

Beitragvon xMeWinatorx » So 9. Aug 2015, 09:16

@naturboy

Echt? Noch ein Grund dafür die 1.8 zu hassen! Hat jemand ein Workaround dafür? Also das man das Bett vor faked oder so?
Wie genau muss das mit dem Bett denn sein? Muss das Bett dann an der BlockPosition liegen?
Benutzeravatar
xMeWinatorx
 
Beiträge: 3
Registriert: Sa 8. Aug 2015, 21:43

Re: Eigene NPCs in der 1.8 erstellen! [UPDATE FÜR 1.8.3]

Beitragvon naturboy » Di 11. Aug 2015, 18:34

xMeWinatorx hat geschrieben:@naturboy

Echt? Noch ein Grund dafür die 1.8 zu hassen! Hat jemand ein Workaround dafür? Also das man das Bett vor faked oder so?
Wie genau muss das mit dem Bett denn sein? Muss das Bett dann an der BlockPosition liegen?

Gerade eben ausprobiert das Bett lässt sich per Blockchange auch auf einer anderen y höhe positionieren also nur x und z cordinate müssen übereinstimmen.
Meine Klasse:
Code: Alles auswählen
  1. package main;
  2. import java.lang.reflect.Field;
  3. import net.minecraft.server.v1_8_R1.BlockPosition;
  4. import net.minecraft.server.v1_8_R1.DataWatcher;
  5. import net.minecraft.server.v1_8_R1.EntityHuman;
  6. import net.minecraft.server.v1_8_R1.IChatBaseComponent;
  7. import net.minecraft.server.v1_8_R1.MathHelper;
  8. import net.minecraft.server.v1_8_R1.PacketPlayOutBed;
  9. import net.minecraft.server.v1_8_R1.PacketPlayOutNamedEntitySpawn;
  10. import net.minecraft.server.v1_8_R1.PacketPlayOutRelEntityMove;
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.Location;
  13. import org.bukkit.Material;
  14. import org.bukkit.command.Command;
  15. import org.bukkit.command.CommandSender;
  16. import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
  17. import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
  18. import org.bukkit.entity.Player;
  19. import org.bukkit.plugin.java.JavaPlugin;
  20. public class Bed extends JavaPlugin{
  21.    private static float yaw;
  22.    private static float pitch;
  23.    private static int currentEntId = 1337;
  24.    @Override
  25.    public void onEnable() {
  26.       this.yaw = 145.0F;
  27.       this.pitch = 0.0F;
  28.    }
  29.    @Override
  30.    public void onDisable() {
  31.       
  32.    }
  33.    @Override
  34.    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  35.       if(sender instanceof Player){
  36.          if(command.getName().equalsIgnoreCase("bed")){
  37.             Player p = (Player) sender;
  38.             spawnCorpse(p);
  39.          }
  40.       }
  41.       return true;
  42.    }
  43.    public static Location getNonClippableBlockUnderPlayer(Player p, int addToYPos)
  44.      {
  45.        Location loc = p.getLocation();
  46.        if (loc.getBlockY() <= 1) {
  47.          return null;
  48.        }
  49.        for (int y = loc.getBlockY(); y >= 1; y--) {
  50.          Material m = loc.getWorld().getBlockAt(loc.getBlockX(), y, loc.getBlockZ()).getType();
  51.          if (m.isSolid()) {
  52.            return new Location(loc.getWorld(), loc.getX(), y + addToYPos, loc.getZ());
  53.          }
  54.        }
  55.        return null;
  56.      }
  57.    public static void spawnCorpse(Player player) {
  58.       final int id = currentEntId;
  59.        PacketPlayOutNamedEntitySpawn npc = new PacketPlayOutNamedEntitySpawn(
  60.          ((CraftPlayer)player).getHandle());
  61.        PacketPlayOutBed sleep = new PacketPlayOutBed();
  62.        PacketPlayOutRelEntityMove move = new PacketPlayOutRelEntityMove(id, (byte)0, (byte)(int)((player.getLocation().getY() - 1.9D - player.getLocation().getY()) * 32.0D), (byte)0, false);
  63.        Location locUnder = getNonClippableBlockUnderPlayer(player, 1);
  64.        Location yUsed = locUnder != null ? locUnder : player.getLocation();
  65.        Location bed = yUsed.clone().subtract(0.0D, 5.0D, 0.0D);
  66.        Field npcd;
  67.        try { Field npca = npc.getClass().getDeclaredField("a");
  68.          npca.setAccessible(true);
  69.          npca.setInt(npc, id);
  70.          npca.setAccessible(!npca.isAccessible());
  71.          npcd = npc.getClass().getDeclaredField("d");
  72.          npcd.setAccessible(true);
  73.          npcd.setInt(npc, MathHelper.floor((yUsed.getY() + 2.0D) * 32.0D));
  74.          npcd.setAccessible(!npcd.isAccessible());
  75.          Field npch = npc.getClass().getDeclaredField("h");
  76.          npch.setAccessible(true);
  77.          npch.setInt(npc, 0);
  78.          Field sleepa = sleep.getClass().getDeclaredField("a");
  79.          sleepa.setAccessible(true);
  80.          sleepa.setInt(sleep, id);
  81.          sleepa.setAccessible(!sleepa.isAccessible());
  82.          Field sleepb = sleep.getClass().getDeclaredField("b");
  83.          sleepb.setAccessible(true);
  84.          sleepb.set(
  85.            sleep,
  86.            new BlockPosition(bed.getBlockX(), bed.getBlockY(), bed
  87.            .getBlockZ()));
  88.          sleepb.setAccessible(!sleepb.isAccessible());
  89.          Field npcf = npc.getClass().getDeclaredField("f");
  90.          npcf.setAccessible(true);
  91.          npcf.setByte(npc, (byte)(int)(yaw * 256.0F / 360.0F));
  92.          npcf.setAccessible(!npcf.isAccessible());
  93.          Field npcg = npc.getClass().getDeclaredField("g");
  94.          npcg.setAccessible(true);
  95.          npcg.setByte(npc, (byte)(int)(pitch * 256.0F / 360.0F));
  96.          npcg.setAccessible(!npcg.isAccessible());
  97.          DataWatcher w = emptyPlayerDataWatcher(player);
  98.          w.watch(10,
  99.            Byte.valueOf(((CraftPlayer)player).getHandle().getDataWatcher()
  100.            .getByte(10)));
  101.          w.watch(6, Float.valueOf(20.0F));
  102.          Field npci = npc.getClass().getDeclaredField("i");
  103.          npci.setAccessible(true);
  104.          npci.set(npc, w);
  105.          npci.setAccessible(!npci.isAccessible());
  106.        } catch (Exception x) {
  107.          x.printStackTrace();
  108.        }
  109.        for (Player p : player.getWorld().getPlayers()) {
  110.          p.sendBlockChange(bed, Material.BED_BLOCK, (byte)0);
  111.          ((CraftPlayer)p).getHandle().playerConnection.sendPacket(npc);
  112.          ((CraftPlayer)p).getHandle().playerConnection.sendPacket(sleep);
  113.          ((CraftPlayer)p).getHandle().playerConnection.sendPacket(move);
  114.        }
  115.        currentEntId += 1;
  116.      }
  117.    private static DataWatcher emptyPlayerDataWatcher(Player player){
  118.        EntityHuman h = new EntityHuman(((CraftWorld)player.getWorld()).getHandle(), ((CraftPlayer)player).getProfile())
  119.        {
  120.          public void sendMessage(IChatBaseComponent arg0) {
  121.          }
  122.          public boolean a(int arg0, String arg1) {
  123.            return false;
  124.          }
  125.          public BlockPosition getChunkCoordinates() {
  126.            return null;
  127.          }
  128.          public boolean v() {
  129.            return false;
  130.          }
  131.        };
  132.        h.d(currentEntId);
  133.        return h.getDataWatcher();
  134.      }
  135. }
Benutzeravatar
naturboy
 
Beiträge: 279
Registriert: Mi 17. Jul 2013, 17:45

Re: Eigene NPCs in der 1.8 erstellen! [UPDATE FÜR 1.8.3]

Beitragvon TVXtrem » Di 11. Aug 2015, 19:20

Hey :),
hat jemand von euch zufällig eine auf Packets basierende walk() Methode für die 1.8 ?
Komme mit dem "PacketPlayOutRelEntityMove" nicht wirklich klar. :/

Danke im Vorraus!
TVXtrem
Benutzeravatar
TVXtrem
 
Beiträge: 8
Registriert: Di 6. Mai 2014, 15:30

Re: Eigene NPCs in der 1.8 erstellen! [UPDATE FÜR 1.8.3]

Beitragvon Summerfeeling » Mi 12. Aug 2015, 14:49

TVXtrem hat geschrieben:Hey :),
hat jemand von euch zufällig eine auf Packets basierende walk() Methode für die 1.8 ?
Komme mit dem "PacketPlayOutRelEntityMove" nicht wirklich klar. :/

Danke im Vorraus!
TVXtrem

Mit dem Packet kannst du deine Entity nur 4 Blöcke bewegen, danach greift das Packet nicht mehr, und du musst die Entity teleportieren. Wenn du also einen relativ gut aussehenden Movement haben willst, musst du die Strecke so splitten, dass sie immer auf 4 Blöcken besteht! ;)

#EDIT: Was mir noch einfallen würde, wäre ein Tier die KI zu nehmen, und dann den NPC auf das Tier zu setzen, und das Tier durch die Navigation oder Pathfinder zu deiner gewünschten Locations schicken. Am Ziel dann einfach das Tier wieder löschen.
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! [UPDATE FÜR 1.8.3]

Beitragvon xMeWinatorx » Do 13. Aug 2015, 13:46

@naturboy

Hast du das plugin Corpses geschrieben? (1.8) Da der Code da genau so aussieht...
Benutzeravatar
xMeWinatorx
 
Beiträge: 3
Registriert: Sa 8. Aug 2015, 21:43

Re: Eigene NPCs in der 1.8 erstellen! [UPDATE FÜR 1.8.3]

Beitragvon naturboy » Fr 14. Aug 2015, 09:00

xMeWinatorx hat geschrieben:@naturboy

Hast du das plugin Corpses geschrieben? (1.8) Da der Code da genau so aussieht...

Ähm decompilen Ähm
Benutzeravatar
naturboy
 
Beiträge: 279
Registriert: Mi 17. Jul 2013, 17:45

Re: Eigene NPCs in der 1.8 erstellen! [UPDATE FÜR 1.8.3]

Beitragvon Summerfeeling » Fr 14. Aug 2015, 18:55

naturboy hat geschrieben:
xMeWinatorx hat geschrieben:@naturboy

Hast du das plugin Corpses geschrieben? (1.8) Da der Code da genau so aussieht...

Ähm decompilen Ähm

*hust* Das ist 'n Open-Source Projekt :p
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! [UPDATE FÜR 1.8.3]

Beitragvon naturboy » Sa 15. Aug 2015, 18:24

Summerfeeling hat geschrieben:
naturboy hat geschrieben:
xMeWinatorx hat geschrieben:@naturboy

Hast du das plugin Corpses geschrieben? (1.8) Da der Code da genau so aussieht...

Ähm decompilen Ähm

*hust* Das ist 'n Open-Source Projekt :p

Ja gut hab ich gesagt
Benutzeravatar
naturboy
 
Beiträge: 279
Registriert: Mi 17. Jul 2013, 17:45

Re: Eigene NPCs in der 1.8 erstellen! [UPDATE: NPCInteractEv

Beitragvon Summerfeeling » So 16. Aug 2015, 14:50

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

Mit diesem Update folgt nun ein sehr viel gefragtes PlayerInteractNPCEvent. Dieses Event wird mit Netty arbeiten, und erfordert Implementierungen in euer Plugin. Euch ist es freigestellt, dieses Event zu benutzen. Wollt ihr es jedoch benutzen, so müssen die Methoden NPC#injectNetty(Player) im PlayeRJoinEvent und die Methode NPC#ejectNetty(Player) im PlayerQuitEvent aufgerufen werden. Weiterhin gilt, NPCs müssen asynchron gespawned werden!
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! [UPDATE: NPCInteractEv

Beitragvon PRiiSM » Sa 10. Dez 2016, 13:18

Gibts da auch ne Version die in der 1.10 funktioniert? :D
Join auf UnicaCity! https://unicacity.de
Benutzeravatar
PRiiSM
 
Beiträge: 75
Registriert: Mo 9. Feb 2015, 22:08

Vorherige

Zurück zu Anleitungen

Wer ist online?

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

cron