From fd9940e6f05c663e357eb36a468cc761516d1e8d Mon Sep 17 00:00:00 2001 From: games647 Date: Sun, 10 Jul 2016 13:34:08 +0200 Subject: [PATCH] Fix setting skin on Cauldron (Fixes #36) --- .../protocollib/LoginSkinApplyListener.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/LoginSkinApplyListener.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/LoginSkinApplyListener.java index 2f2015a5..f8ca60fb 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/LoginSkinApplyListener.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/LoginSkinApplyListener.java @@ -1,11 +1,17 @@ package com.github.games647.fastlogin.bukkit.listener.protocollib; +import com.comphenix.protocol.reflect.accessors.Accessors; +import com.comphenix.protocol.reflect.accessors.MethodAccessor; +import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedSignedProperty; import com.github.games647.fastlogin.bukkit.BukkitLoginSession; import com.github.games647.fastlogin.bukkit.FastLoginBukkit; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Collection; +import java.util.logging.Level; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -15,6 +21,11 @@ import org.bukkit.event.player.PlayerLoginEvent; public class LoginSkinApplyListener implements Listener { + private static final Class GAME_PROFILE = MinecraftReflection.getGameProfileClass(); + + private static final MethodAccessor GET_PROPERTIES = Accessors.getMethodAcccessorOrNull( + GAME_PROFILE, "getProperties"); + private final FastLoginBukkit plugin; public LoginSkinApplyListener(FastLoginBukkit plugin) { @@ -45,7 +56,17 @@ public class LoginSkinApplyListener implements Listener { String signature = session.getSkinSignature(); if (skinData != null && signature != null) { WrappedSignedProperty skin = WrappedSignedProperty.fromValues("textures", skinData, signature); - gameProfile.getProperties().put("textures", skin); + try { + gameProfile.getProperties().put("textures", skin); + } catch (ClassCastException castException) { + Object map = GET_PROPERTIES.invoke(gameProfile.getHandle()); + try { + Method putMethod = map.getClass().getMethod("put", Object.class, Object.class); + putMethod.invoke(map, "textures", skin.getHandle()); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { + plugin.getLogger().log(Level.SEVERE, "Error setting premium skin", ex); + } + } } } }