Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.wardensmp.plugins.dev.npc;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- import com.mojang.authlib.GameProfile;
- import com.mojang.authlib.properties.Property;
- import net.minecraft.server.v1_16_R3.*;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.craftbukkit.v1_16_R3.CraftServer;
- import org.bukkit.craftbukkit.v1_16_R3.CraftWorld;
- import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer;
- import org.bukkit.entity.Player;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Objects;
- import java.util.UUID;
- public class NPC {
- private static List<EntityPlayer> NPCs = new ArrayList<>();
- public static void createNPC(Player player, String skin, String name) {
- MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
- WorldServer world = ((CraftWorld) Objects.requireNonNull(Bukkit.getWorld(player.getWorld().getName()))).getHandle();
- GameProfile profile = new GameProfile(UUID.randomUUID(), ChatColor.translateAlternateColorCodes('&', name));
- EntityPlayer npc = new EntityPlayer(server, world, profile, new PlayerInteractManager(world));
- npc.setLocation(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(),
- player.getLocation().getYaw(), player.getLocation().getPitch());
- String[] names = getSkin(player, skin);
- profile.getProperties().put("textures", new Property("textures", names[0], names[1]));
- addNPCPacket(npc);
- NPCs.add(npc);
- }
- public static String[] getSkin(Player player, String name) {
- try {
- URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + name);
- InputStreamReader reader = new InputStreamReader(url.openStream());
- String uuid = new JsonParser().parse(reader).getAsJsonObject().get("id").getAsString();
- URL url1 = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false");
- InputStreamReader reader1 = new InputStreamReader(url1.openStream());
- JsonObject property =
- new JsonParser().parse(reader1).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
- String texture = property.get("value").getAsString();
- String signature = property.get("signature").getAsString();
- return new String[] {texture, signature};
- } catch (Exception e) {
- EntityPlayer p = ((CraftPlayer) player).getHandle();
- GameProfile profile = p.getProfile();
- Property property = profile.getProperties().get("textures").iterator().next();
- String texture = property.getValue();
- String signature = property.getSignature();
- return new String[] {texture, signature};
- }
- }
- public static void addNPCPacket(EntityPlayer npc) {
- for (Player player : Bukkit.getOnlinePlayers()) {
- PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
- connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc));
- connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
- connection.sendPacket(new PacketPlayOutEntityHeadRotation(npc, (byte) (npc.yaw * 256 / 360)));
- }
- }
- public static void addJoinPacket(Player player) {
- for (EntityPlayer npc : NPCs) {
- PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
- connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc));
- connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
- connection.sendPacket(new PacketPlayOutEntityHeadRotation(npc, (byte) (npc.yaw * 256 / 360)));
- }
- }
- public static List<EntityPlayer> getNPCs() {
- return NPCs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement