избавляемся от рефлексии: скины для голов
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package ghast;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.authlib.properties.Property;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import net.minecraft.server.v1_12_R1.BlockPosition;
|
||||
import net.minecraft.server.v1_12_R1.TileEntitySkull;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.SkullType;
|
||||
@@ -8,8 +12,7 @@ import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.block.Skull;
|
||||
import ru.dmitriymx.reflection.ReflectionClass;
|
||||
import ru.dmitriymx.reflection.ReflectionObject;
|
||||
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
@@ -19,9 +22,6 @@ import java.util.UUID;
|
||||
@SuppressWarnings("unused")
|
||||
public class BuildHelper {
|
||||
|
||||
private final Class<?> CLASS_BLOCKPOSITION = getClassForName("net.minecraft.server.v1_12_R1.BlockPosition");
|
||||
private final Class<?> CLASS_GAMEPROFILE = getClassForName("com.mojang.authlib.GameProfile");
|
||||
|
||||
/**
|
||||
* Установка черепа.
|
||||
* <p>
|
||||
@@ -95,17 +95,10 @@ public class BuildHelper {
|
||||
* @param skinUrl URL на текстуру
|
||||
*/
|
||||
public static void setPlayerHeadSkin(Skull skull, String skinUrl) {
|
||||
//TODO заменить рефлексию на "фантомные" классы
|
||||
ReflectionObject refobjBlockPosition = new ReflectionClass(CLASS_BLOCKPOSITION)
|
||||
.constructor(double.class, double.class, double.class)
|
||||
.newInstance(skull.getX(), skull.getY(), skull.getZ());
|
||||
|
||||
new ReflectionObject(skull.getWorld())
|
||||
.method("getHandle").invoke()
|
||||
.method("getTileEntity", CLASS_BLOCKPOSITION)
|
||||
.invoke(refobjBlockPosition.getOriginalObject())
|
||||
.method("setGameProfile", CLASS_GAMEPROFILE)
|
||||
.invoke(getRefObjPlayerProfile(skinUrl).getOriginalObject());
|
||||
((TileEntitySkull) (((CraftWorld) skull.getWorld())
|
||||
.getHandle()
|
||||
.getTileEntity(new BlockPosition(skull.getX(), skull.getY(), skull.getZ()))))
|
||||
.setGameProfile(generateTexturedPlayerProfile(skinUrl));
|
||||
}
|
||||
|
||||
public Sign placeSignWall(Location location, BlockFace face) {
|
||||
@@ -119,29 +112,12 @@ public class BuildHelper {
|
||||
return sign;
|
||||
}
|
||||
|
||||
private ReflectionObject getRefObjPlayerProfile(String url){
|
||||
ReflectionObject refobjProperty = new ReflectionClass(
|
||||
getClassForName("com.mojang.authlib.properties.Property"))
|
||||
.constructor(String.class, String.class)
|
||||
.newInstance("textures", Base64.getEncoder()
|
||||
.encodeToString(("{textures:{SKIN:{url:\"" + url + "\"}}}").getBytes(StandardCharsets.UTF_8)));
|
||||
private GameProfile generateTexturedPlayerProfile(String skinUrl) {
|
||||
GameProfile gameProfile = new GameProfile(UUID.randomUUID(), null);
|
||||
|
||||
ReflectionObject refobjGameProfile = new ReflectionClass(CLASS_GAMEPROFILE)
|
||||
.constructor(UUID.class, String.class)
|
||||
.newInstance(UUID.randomUUID(), null);
|
||||
refobjGameProfile
|
||||
.method("getProperties").invoke()
|
||||
.method("put", Object.class, Object.class)
|
||||
.invoke("textures", refobjProperty.getOriginalObject());
|
||||
|
||||
return refobjGameProfile;
|
||||
}
|
||||
|
||||
private Class<?> getClassForName(String className) {
|
||||
try {
|
||||
return Class.forName(className);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
gameProfile.getProperties().put("textures",
|
||||
new Property("textures", Base64.getEncoder().encodeToString(
|
||||
("{textures:{SKIN:{url:\"" + skinUrl + "\"}}}").getBytes(StandardCharsets.UTF_8))));
|
||||
return gameProfile;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user