Skip to content

Instantly share code, notes, and snippets.

@BGMP
Last active December 15, 2020 17:51
Show Gist options
  • Save BGMP/8c4e218128f08a8af648f83d646f9ed0 to your computer and use it in GitHub Desktop.
Save BGMP/8c4e218128f08a8af648f83d646f9ed0 to your computer and use it in GitHub Desktop.
boop
package net.braehmc.bgnpc.NPC;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
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_8_R3.*;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.craftbukkit.v1_8_R3.CraftServer;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.UUID;
public class NPC {
private static void setNPCSkin(Player player, GameProfile gameProfile, String skinHolder) {
try {
lookUpNPCSkin(player, gameProfile, skinHolder);
} catch (IOException exception) {
exception.printStackTrace();
}
}
@SuppressWarnings("deprecation")
private static void lookUpNPCSkin(Player player, GameProfile gameProfile, String skinHolder) throws IOException {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(skinHolder);
if (!offlinePlayer.isOnline()) {
player.sendMessage(ChatColor.RED + "Could not retrieve skin metadata for " + ChatColor.DARK_AQUA + skinHolder + ChatColor.RED + "." + ChatColor.RESET);
return;
}
String offlinePlayerUUID = offlinePlayer.getUniqueId().toString();
String offlinePlayerTrimmedUUID = offlinePlayerUUID.replaceAll("-", "");
String sessionServerInstance = "https://sessionserver.mojang.com/session/minecraft/profile/" + offlinePlayerTrimmedUUID + "?unsigned=false";
URL sessionServer = new URL(sessionServerInstance);
URLConnection sessionServerConnection = sessionServer.openConnection();
InputStream sessionServerContent = sessionServerConnection.getInputStream();
InputStreamReader sessionServerContentReader = new InputStreamReader(sessionServerContent);
BufferedReader bufferedReader = new BufferedReader(sessionServerContentReader);
String rawJsonContent = bufferedReader.readLine();
JsonParser jsonParser = new JsonParser();
JsonObject mainJsonObject = (JsonObject) jsonParser.parse(rawJsonContent);
JsonArray propertiesArray = mainJsonObject.getAsJsonArray("properties");
JsonObject mainJsonsPropertiesSubObject = (JsonObject) jsonParser.parse(propertiesArray.get(0).toString());
JsonElement texture = mainJsonsPropertiesSubObject.get("value"); // Player's skin, named under "value"
JsonElement signature = mainJsonsPropertiesSubObject.get("signature"); // Skin's signature
gameProfile.getProperties().put("textures", new Property("textures", texture.getAsString(), signature.getAsString()));
}
public static void createNPC(Player player, String name, String skinHolder, int x, int y, int z, float yaw, float pitch) {
MinecraftServer netMinecraftServer = ((CraftServer) Bukkit.getServer()).getServer();
WorldServer netMinecraftServerWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle();
PlayerInteractManager playerInteractManager = new PlayerInteractManager(netMinecraftServerWorld);
GameProfile gameProfile = new GameProfile(UUID.randomUUID(), name);
setNPCSkin(player, gameProfile, skinHolder);
EntityPlayer npc = new EntityPlayer(netMinecraftServer, netMinecraftServerWorld, gameProfile, playerInteractManager);
npc.setLocation(x, y, z, yaw, pitch);
DataWatcher dataWatcher = npc.getDataWatcher();
dataWatcher.watch(10, (byte) 127);
PacketPlayOutPlayerInfo packet1 = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc);
PacketPlayOutNamedEntitySpawn packet2 = new PacketPlayOutNamedEntitySpawn(npc);
PacketPlayOutEntityMetadata packet3 = new PacketPlayOutEntityMetadata(npc.getId(), dataWatcher, true);
Collection<? extends Player> onlinePlayers = Bukkit.getOnlinePlayers();
onlinePlayers.forEach(onlinePlayer -> {
PlayerConnection playerConnection = ((CraftPlayer) onlinePlayer).getHandle().playerConnection;
playerConnection.sendPacket(packet1);
playerConnection.sendPacket(packet2);
playerConnection.sendPacket(packet3);
});
}
}
@BGMP
Copy link
Author

BGMP commented Jul 3, 2019

Reminder to fix the NullPointerException caused by the OfflinePlayer Object

@BGMP
Copy link
Author

BGMP commented Jul 3, 2019

Reminder to fix the NullPointerException caused by the OfflinePlayer Object

Fixed.

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