GameProfile wird nicht angewendet

Wenn ihr bei eurem Plugin nicht weiter kommt, könnt ihr hier euer Problem beschreiben und um Hilfe bitten.

GameProfile wird nicht angewendet

Beitragvon FilipZocktanYT » So 1. Jan 2017, 17:08

Hey,
ich erstelle momentan ein Plugin für eine AdventureMap und dafür "verwandeln" wir uns zu anderen Menschen. Jetzt wollte ich den Skin und den Namen eines Spielers senden, allerdings geht das leider nicht.

Die Klasse des Commands:
Code: Alles auswählen
  1. package ga.zcktn.kololok.commands;
  2. import org.bukkit.command.Command;
  3. import org.bukkit.command.CommandExecutor;
  4. import org.bukkit.command.CommandSender;
  5. import org.bukkit.entity.Player;
  6. import ga.zcktn.kololok.skinchanger.ToBergmannHandler;
  7. public class ToBergmannCommand implements CommandExecutor {
  8. @Override
  9. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  10. if(sender instanceof Player){
  11. ToBergmannHandler tbh = new ToBergmannHandler((Player)sender);
  12. Boolean geschafft = tbh.setSkin();
  13. System.out.println(geschafft);
  14. sender.sendMessage(geschafft.toString());
  15. sender.sendMessage("§3Du bist nun Herr Bergmann!");
  16. }
  17. return false;
  18. }
  19. }


Der "ToBergmannHandler"
Code: Alles auswählen
  1. package ga.zcktn.kololok.skinchanger;
  2. import org.bukkit.Bukkit;
  3. import org.bukkit.craftbukkit.v1_8_R3.CraftServer;
  4. import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
  5. import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
  6. import org.bukkit.entity.Player;
  7. import com.mojang.authlib.GameProfile;
  8. import net.minecraft.server.v1_8_R3.EntityPlayer;
  9. import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerInfo;
  10. import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerInfo.EnumPlayerInfoAction;
  11. import net.minecraft.server.v1_8_R3.PlayerInteractManager;
  12. import org.bukkit.World;
  13. import net.minecraft.server.v1_8_R3.WorldServer;
  14. public class ToBergmannHandler {
  15. private CraftPlayer p;
  16. protected static WorldServer getWorldServer(World world) {
  17. if (world instanceof CraftWorld) {
  18. return ((CraftWorld) world).getHandle();
  19. }
  20. return null;
  21. }
  22. public ToBergmannHandler(Player p) {
  23. this.p = (CraftPlayer)p;
  24. }
  25. @SuppressWarnings("deprecation")
  26. public boolean setSkin() {
  27. try {
  28. System.out.println(Bukkit.getOfflinePlayer("HerrBergmann").getUniqueId().toString());
  29. GameProfile profile = GameProfileBuilder.getProfile(Bukkit.getOfflinePlayer("HerrBergmann").getUniqueId(), "HerrBergmann",
  30. "http://textures.minecraft.net/texture/dae79c413914ee8679b31955f0ba294a494a7e2ef61b6c55d96864ec6c0334e");
  31. GameProfileBuilder.setCacheTime(-1);
  32. PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.UPDATE_DISPLAY_NAME, new EntityPlayer(((CraftServer)Bukkit.getServer()).getServer(),getWorldServer(p.getWorld()) , profile,new PlayerInteractManager(getWorldServer(p.getWorld()))));
  33. for (Player p2 : Bukkit.getServer().getOnlinePlayers()) {
  34. CraftPlayer cp = (CraftPlayer) p2;
  35. cp.getHandle().playerConnection.sendPacket(packet);
  36. }
  37. return true;
  38. } catch (Exception e) {
  39. return false;
  40. }
  41. }
  42. }


Und der GameProfileBuilder: (Von hier: http://postcrafter.de/viewtopic.php?f=15&t=2116)
Code: Alles auswählen
  1. package ga.zcktn.kololok.skinchanger;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.lang.reflect.Type;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map.Entry;
  12. import java.util.UUID;
  13. import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
  14. import com.google.gson.Gson;
  15. import com.google.gson.GsonBuilder;
  16. import com.google.gson.JsonDeserializationContext;
  17. import com.google.gson.JsonDeserializer;
  18. import com.google.gson.JsonElement;
  19. import com.google.gson.JsonObject;
  20. import com.google.gson.JsonParseException;
  21. import com.google.gson.JsonParser;
  22. import com.google.gson.JsonSerializationContext;
  23. import com.google.gson.JsonSerializer;
  24. import com.mojang.authlib.GameProfile;
  25. import com.mojang.authlib.properties.Property;
  26. import com.mojang.authlib.properties.PropertyMap;
  27. import com.mojang.util.UUIDTypeAdapter;
  28. public class GameProfileBuilder {
  29. private static final String SERVICE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false";
  30. private static final String JSON_SKIN = "{"timestamp":%d,"profileId":"%s","profileName":"%s","isPublic":true,"textures":{"SKIN":{"url":"%s"}}}";
  31. private static final String JSON_CAPE = "{"timestamp":%d,"profileId":"%s","profileName":"%s","isPublic":true,"textures":{"SKIN":{"url":"%s"},"CAPE":{"url":"%s"}}}";
  32. private static Gson gson = new GsonBuilder().disableHtmlEscaping().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(GameProfile.class, new GameProfileSerializer()).registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create();
  33. private static HashMap<UUID, CachedProfile> cache = new HashMap<UUID, CachedProfile>();
  34. private static long cacheTime = -1;
  35. /**
  36. * Don't run in main thread!
  37. *
  38. * Fetches the GameProfile from the Mojang servers
  39. *
  40. * @param uuid The player uuid
  41. * @return The GameProfile
  42. * @throws IOException If something wents wrong while fetching
  43. * @see GameProfile
  44. */
  45. public static GameProfile fetch(UUID uuid) throws IOException {
  46. return fetch(uuid, false);
  47. }
  48. /**
  49. * Don't run in main thread!
  50. *
  51. * Fetches the GameProfile from the Mojang servers
  52. * @param uuid The player uuid
  53. * @param forceNew If true the cache is ignored
  54. * @return The GameProfile
  55. * @throws IOException If something wents wrong while fetching
  56. * @see GameProfile
  57. */
  58. public static GameProfile fetch(UUID uuid, boolean forceNew) throws IOException {
  59. if (!forceNew && cache.containsKey(uuid) && cache.get(uuid).isValid()) {
  60. return cache.get(uuid).profile;
  61. } else {
  62. HttpURLConnection connection = (HttpURLConnection) new URL(String.format(SERVICE_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection();
  63. connection.setReadTimeout(5000);
  64. if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
  65. String json = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();
  66. GameProfile result = gson.fromJson(json, GameProfile.class);
  67. cache.put(uuid, new CachedProfile(result));
  68. return result;
  69. } else {
  70. if (!forceNew && cache.containsKey(uuid)) {
  71. return cache.get(uuid).profile;
  72. }
  73. JsonObject error = (JsonObject) new JsonParser().parse(new BufferedReader(new InputStreamReader(connection.getErrorStream())).readLine());
  74. throw new IOException(error.get("error").getAsString() + ": " + error.get("errorMessage").getAsString());
  75. }
  76. }
  77. }
  78. /**
  79. * Builds a GameProfile for the specified args
  80. *
  81. * @param uuid The uuid
  82. * @param name The name
  83. * @param skin The url from the skin image
  84. * @return A GameProfile built from the arguments
  85. * @see GameProfile
  86. */
  87. public static GameProfile getProfile(UUID uuid, String name, String skin) {
  88. return getProfile(uuid, name, skin, null);
  89. }
  90. /**
  91. * Builds a GameProfile for the specified args
  92. *
  93. * @param uuid The uuid
  94. * @param name The name
  95. * @param skinUrl Url from the skin image
  96. * @param capeUrl Url from the cape image
  97. * @return A GameProfile built from the arguments
  98. * @see GameProfile
  99. */
  100. public static GameProfile getProfile(UUID uuid, String name, String skinUrl, String capeUrl) {
  101. GameProfile profile = new GameProfile(uuid, name);
  102. boolean cape = capeUrl != null && !capeUrl.isEmpty();
  103. List<Object> args = new ArrayList<Object>();
  104. args.add(System.currentTimeMillis());
  105. args.add(UUIDTypeAdapter.fromUUID(uuid));
  106. args.add(name);
  107. args.add(skinUrl);
  108. if (cape) args.add(capeUrl);
  109. profile.getProperties().put("textures", new Property("textures", Base64Coder.encodeString(String.format(cape ? JSON_CAPE : JSON_SKIN, args.toArray(new Object[args.size()])))));
  110. return profile;
  111. }
  112. /**
  113. * Sets the time as long as you want to keep the gameprofiles in cache (-1 = never remove it)
  114. * @param time cache time (default = -1)
  115. */
  116. public static void setCacheTime(long time) {
  117. cacheTime = time;
  118. }
  119. private static class GameProfileSerializer implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile> {
  120. public GameProfile deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
  121. JsonObject object = (JsonObject) json;
  122. UUID id = object.has("id") ? (UUID) context.deserialize(object.get("id"), UUID.class) : null;
  123. String name = object.has("name") ? object.getAsJsonPrimitive("name").getAsString() : null;
  124. GameProfile profile = new GameProfile(id, name);
  125. if (object.has("properties")) {
  126. for (Entry<String, Property> prop : ((PropertyMap) context.deserialize(object.get("properties"), PropertyMap.class)).entries()) {
  127. profile.getProperties().put(prop.getKey(), prop.getValue());
  128. }
  129. }
  130. return profile;
  131. }
  132. public JsonElement serialize(GameProfile profile, Type type, JsonSerializationContext context) {
  133. JsonObject result = new JsonObject();
  134. if (profile.getId() != null)
  135. result.add("id", context.serialize(profile.getId()));
  136. if (profile.getName() != null)
  137. result.addProperty("name", profile.getName());
  138. if (!profile.getProperties().isEmpty())
  139. result.add("properties", context.serialize(profile.getProperties()));
  140. return result;
  141. }
  142. }
  143. private static class CachedProfile {
  144. private long timestamp = System.currentTimeMillis();
  145. private GameProfile profile;
  146. public CachedProfile(GameProfile profile) {
  147. this.profile = profile;
  148. }
  149. public boolean isValid() {
  150. return cacheTime < 0 ? true : (System.currentTimeMillis() - timestamp) < cacheTime;
  151. }
  152. }
  153. }


Die Nachricht am Ende des Commands wird ausgegeben und ich bekomme ein true, das bedeutet es ist auch keine versteckte Exception.

Ich tippe dass es an dem Packet im ToBergmannHandler liegt, weiß aber nicht wie ich es anders tun sollte.

Danke für eure Antworten

LG
FIlip Zocktan
Benutzeravatar
FilipZocktanYT
 
Beiträge: 3
Registriert: Fr 15. Jul 2016, 01:05

Zurück zu Hilfe und Support

Wer ist online?

Mitglieder in diesem Forum: Google [Bot] und 2 Gäste

cron