Skip to content

Instantly share code, notes, and snippets.

@DreamTexX
Last active September 12, 2022 20:59
Show Gist options
  • Save DreamTexX/7220ec0253fb9f54bac720bc7ad9885b to your computer and use it in GitHub Desktop.
Save DreamTexX/7220ec0253fb9f54bac720bc7ad9885b to your computer and use it in GitHub Desktop.
[Create Fake Player] #Spigot
// 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));
}
}
}
@adrian-lxm
Copy link

What ist skins and signatures in your code ?

@DreamTexX
Copy link
Author

What ist skins and signatures in your code ?

That's a really good question. I used this code one time and can't remember what this variable where. Sorry

@DreamTexX
Copy link
Author

But I will have a look later, I may find it out

@adrian-lxm
Copy link

This would be nice
Your code looks like a good solution to spawn a fake player, that's why I asked.

@DreamTexX
Copy link
Author

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.

@DreamTexX
Copy link
Author

DreamTexX commented Mar 14, 2021

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

@adrian-lxm
Copy link

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.

@DreamTexX
Copy link
Author

Of course its an hashmap, i'm just lost xD

@adrian-lxm
Copy link

Ok, that makes more sense
Anyways thanks xD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment