Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.53 KB | None | 0 0
  1. package fr.dorvak.ia.npc;
  2.  
  3. import java.util.List;
  4. import java.util.UUID;
  5.  
  6. import org.bukkit.Location;
  7. import org.bukkit.craftbukkit.v1_8_R3.util.CraftChatMessage;
  8. import org.bukkit.inventory.ItemStack;
  9.  
  10. import com.mojang.authlib.GameProfile;
  11. import com.mojang.authlib.properties.Property;
  12.  
  13. import fr.dorvak.ia.utils.Reflections;
  14. import net.minecraft.server.v1_8_R3.DataWatcher;
  15. import net.minecraft.server.v1_8_R3.MathHelper;
  16. import net.minecraft.server.v1_8_R3.PacketPlayOutAnimation;
  17. import net.minecraft.server.v1_8_R3.PacketPlayOutEntity.PacketPlayOutEntityLook;
  18. import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy;
  19. import net.minecraft.server.v1_8_R3.PacketPlayOutEntityEquipment;
  20. import net.minecraft.server.v1_8_R3.PacketPlayOutEntityHeadRotation;
  21. import net.minecraft.server.v1_8_R3.PacketPlayOutEntityStatus;
  22. import net.minecraft.server.v1_8_R3.PacketPlayOutEntityTeleport;
  23. import net.minecraft.server.v1_8_R3.PacketPlayOutNamedEntitySpawn;
  24. import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerInfo;
  25. import net.minecraft.server.v1_8_R3.WorldSettings.EnumGamemode;
  26.  
  27. public class NPC extends Reflections {
  28.    
  29.     private int entityID;
  30.     private GameProfile gameProfile;
  31.     private Location loc;
  32.    
  33.     public NPC(String name, Location loc) {
  34.         entityID = (int)Math.ceil(Math.random() * 1000) + 2000;
  35.         gameProfile = new GameProfile(UUID.randomUUID(), name);
  36.         this.loc = loc.clone();
  37.     }
  38.    
  39.     public void loadSkin(String value, String signature) {
  40.         gameProfile.getProperties().put("textures", new Property("textures", value, signature));
  41.     }
  42.    
  43.     public void animate(int animation) {
  44.         PacketPlayOutAnimation packet = new PacketPlayOutAnimation();
  45.         setValue(packet, "a", entityID);
  46.         setValue(packet, "b", (byte)animation);
  47.         sendPacket(packet);
  48.     }
  49.    
  50.     public void status(int status) {
  51.         PacketPlayOutEntityStatus packet = new PacketPlayOutEntityStatus();
  52.         setValue(packet, "a", entityID);
  53.         setValue(packet, "b", (byte)status);
  54.         sendPacket(packet);
  55.     }
  56.    
  57.     public void equip(int slot, ItemStack item) {
  58.         PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment();
  59.        
  60.         setValue(packet, "a", entityID);
  61.         setValue(packet, "b", slot);
  62.         setValue(packet, "c", item);
  63.         sendPacket(packet);
  64.     }
  65.    
  66.     public void spawn() {
  67.         PacketPlayOutNamedEntitySpawn packet = new PacketPlayOutNamedEntitySpawn();
  68.        
  69.         setValue(packet, "a", entityID);
  70.         setValue(packet, "b", gameProfile.getId());
  71.         setValue(packet, "c", getFixLocation(loc.getX()));
  72.         setValue(packet, "d", getFixLocation(loc.getY()));
  73.         setValue(packet, "e", getFixLocation(loc.getZ()));
  74.         setValue(packet, "f", getFixRotation(loc.getYaw()));
  75.         setValue(packet, "g", getFixRotation(loc.getPitch()));
  76.         setValue(packet, "h", 0);
  77.         DataWatcher watcher = new DataWatcher(null);
  78.         watcher.a(6, (float)20);
  79.         watcher.a(10, (byte)127);
  80.         setValue(packet, "i", watcher);
  81.         addToTabList();
  82.         sendPacket(packet);
  83.         headRotation(loc.getYaw(), loc.getPitch());
  84.     }
  85.    
  86.     public void teleport(Location loc) {
  87.         PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport();
  88.         setValue(packet, "a", entityID);
  89.         setValue(packet, "b", getFixLocation(loc.getX()));
  90.         setValue(packet, "c", getFixLocation(loc.getY()));
  91.         setValue(packet, "d", getFixLocation(loc.getZ()));
  92.         setValue(packet, "e", getFixRotation(loc.getYaw()));
  93.         setValue(packet,"f", getFixRotation(loc.getPitch()));
  94.        
  95.         sendPacket(packet);
  96.         headRotation(loc.getYaw(), loc.getPitch());
  97.         this.loc = loc.clone();
  98.     }
  99.  
  100.     public void headRotation(float yaw, float pitch) {
  101.         PacketPlayOutEntityLook packet = new PacketPlayOutEntityLook(entityID, getFixRotation(yaw), getFixRotation(pitch), true);
  102.        
  103.         PacketPlayOutEntityHeadRotation headPacket = new PacketPlayOutEntityHeadRotation();
  104.         setValue(headPacket, "a", entityID);
  105.         setValue(headPacket, "b", getFixRotation(yaw));
  106.        
  107.         sendPacket(packet);
  108.         sendPacket(headPacket);
  109.     }
  110.    
  111.     public void kill() {
  112.         PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(new int[] {entityID});
  113.         removeFromTabList();
  114.         sendPacket(packet);
  115.     }
  116.    
  117.     public void addToTabList() {
  118.         PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
  119.         PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(gameProfile, 1, EnumGamemode.NOT_SET, CraftChatMessage.fromString(gameProfile.getName())[0]);
  120.        
  121.         @SuppressWarnings("unchecked")
  122.         List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) getValue(packet, "b");
  123.         players.add(data);
  124.        
  125.         setValue(packet, "a", PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER);
  126.         setValue(packet, "b", players);
  127.         sendPacket(packet);
  128.     }
  129.    
  130.     public void removeFromTabList() {
  131.         PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
  132.         PacketPlayOutPlayerInfo.PlayerInfoData data = packet.new PlayerInfoData(gameProfile, 1, EnumGamemode.NOT_SET, CraftChatMessage.fromString(gameProfile.getName())[0]);
  133.        
  134.         @SuppressWarnings("unchecked")
  135.         List<PacketPlayOutPlayerInfo.PlayerInfoData> players = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) getValue(packet, "b");
  136.         players.add(data);
  137.        
  138.         setValue(packet, "a", PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER);
  139.         setValue(packet, "b", players);
  140.         sendPacket(packet);
  141.     }
  142.    
  143.     public int getFixLocation(double pos) {
  144.         return (int)MathHelper.floor(pos * 32.0D);
  145.     }
  146.    
  147.     public byte getFixRotation(float yawPitch) {
  148.         return (byte)((int) (yawPitch * 256.0F / 360.0F));
  149.     }
  150.    
  151.     public int getEntityID() {
  152.         return entityID;
  153.     }
  154.    
  155.     public Location getLocation() {
  156.         return loc;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement