-
-
Save DreamTexX/7220ec0253fb9f54bac720bc7ad9885b to your computer and use it in GitHub Desktop.
// These two lines should be outside the npc spawn function for caching | |
HashMap<String, String> skins = new HashMap<>(); | |
HashMap<String, String> signatures = new HashMap<>(); | |
// This code was written inside an command, thats why (Player) commandSender | |
Player player = (Player) commandSender; | |
MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer(); | |
WorldServer world = ((CraftWorld) player.getWorld()).getHandle(); | |
Location loc = player.getLocation(); | |
for (int x = loc.getBlockX() - 16; x < loc.getBlockX() + 16; x++) { | |
for (int z = loc.getBlockZ() - 16; z < loc.getBlockZ() + 16; z++) { | |
GameProfile profile = new GameProfile(UUID.randomUUID(), player.getName()); | |
if (skins.containsKey(player.getName())) { | |
profile.getProperties().put("textures", new Property("textures", skins.get(player.getName()), signatures.get(player.getName()))); | |
} else { | |
try { | |
HttpsURLConnection connection = (HttpsURLConnection) new URL(String.format("https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false", UUIDTypeAdapter.fromUUID(player.getUniqueId()))).openConnection(); | |
if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) { | |
String reply = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine(); | |
String skin = reply.split("\"value\":\"")[1].split("\"")[0]; | |
String signature = reply.split("\"signature\":\"")[1].split("\"")[0]; | |
skins.put(player.getName(), skin); | |
signatures.put(player.getName(), signature); | |
profile.getProperties().put("textures", new Property("textures", skin, signature)); | |
} else { | |
System.out.println("Connection could not be opened (Response code " + connection.getResponseCode() + ", " + connection.getResponseMessage() + ")"); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
EntityPlayer npc = new EntityPlayer(server, world, profile, new PlayerInteractManager(world)); | |
Location spawn = player.getLocation(); | |
npc.setLocation(x, loc.getBlockY(), z, 0, 0); | |
for (Player onlinePlayer : Bukkit.getServer().getOnlinePlayers()) { | |
PlayerConnection connection = ((CraftPlayer) onlinePlayer).getHandle().playerConnection; | |
connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc)); | |
connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc)); | |
} | |
} | |
} |
What ist
skins
andsignatures
in your code ?
That's a really good question. I used this code one time and can't remember what this variable where. Sorry
But I will have a look later, I may find it out
This would be nice
Your code looks like a good solution to spawn a fake player, that's why I asked.
It has some mistakes, like it will not spawn for players that connect after the npc is spawned but that could be easily fixed when you store the npcs and send it when the player spawns.
Both, skin and signature should be an ArrayList<String, String> if i read my code correctly. I will add it
Edit: I meant HashMap not ArrayList
ArrayList has a method called put
?
I thought more this is a HashMap, because the HashMap Class has a put
method, but I just asked to be sure.
Of course its an hashmap, i'm just lost xD
Ok, that makes more sense
Anyways thanks xD
What ist
skins
andsignatures
in your code ?