package ghast; import lombok.experimental.UtilityClass; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.SkullType; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.Sign; import org.bukkit.block.Skull; import org.joor.Reflect; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.UUID; import static org.joor.Reflect.on; import static org.joor.Reflect.onClass; @UtilityClass @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"); /** * Установка черепа. *
* После установки, необходимо выполнить skull.update(true);
*
* @param location место установки.
* @param face куда будет повёрнут череп.
* @return Блок типа {@link Skull}
*/
public Skull placeSkull(Location location, BlockFace face) {
Location fixedLocation = GhastTools.copyLocation(location);
fixedLocation.setZ(fixedLocation.getZ() - 1);
Block block = location.getWorld().getBlockAt(location);
block.setType(Material.SKULL);
Skull skull = (Skull) block.getState();
skull.setRotation(face);
org.bukkit.material.Skull skullMaterial = (org.bukkit.material.Skull) skull.getData();
skullMaterial.setFacingDirection(BlockFace.SELF);
return skull;
}
/**
* Установка головы игрока.
*
* После установки, необходимо выполнить skull.update(true);
*
* @param location место установки.
* @param face куда будет повёрнута голова.
* @return Блок типа {@link Skull}
*/
public static Skull placePlayerHead(Location location, BlockFace face) {
Location fixedLocation = GhastTools.copyLocation(location);
fixedLocation.setZ(fixedLocation.getZ() - 1);
Block block = fixedLocation.getWorld().getBlockAt(fixedLocation);
block.setType(Material.SKULL);
Skull skull = (Skull) block.getState();
skull.setSkullType(SkullType.PLAYER);
skull.setRotation(face);
org.bukkit.material.Skull skullMaterial = (org.bukkit.material.Skull) skull.getData();
skullMaterial.setFacingDirection(BlockFace.SELF);
return skull;
}
/**
* Установка текстурированной головы игрока.
*
* @param location место установки.
* @param face куда будет повёрнута голова.
* @param skinUrl URL на текстуру
* @return Блок типа {@link Skull}
*/
public static Skull placePlayerHead(Location location, BlockFace face, String skinUrl) {
Skull playerHead = placePlayerHead(location, face);
playerHead.update(true);
setPlayerHeadSkin(playerHead, skinUrl);
return playerHead;
}
/**
* Установка текстуры для головы игрока.
*
* @param skull блок головы игрока
* @param skinUrl URL на текстуру
*/
public static void setPlayerHeadSkin(Skull skull, String skinUrl) {
//TODO заменить рефлексию на "фантомные" классы
Reflect reflectBlockPosition = onClass(CLASS_BLOCKPOSITION)
.create(skull.getX(), skull.getY(), skull.getZ());
on(skull.getWorld())
.call("getHandle")
.call("getTileEntity", reflectBlockPosition.get())
.call("setGameProfile", getReflectPlayerProfile(skinUrl).get());
}
public Sign placeSignWall(Location location, BlockFace face) {
Block block = location.getWorld().getBlockAt(location);
block.setType(Material.WALL_SIGN);
Sign sign = (Sign) block.getState();
org.bukkit.material.Sign signMaterial = (org.bukkit.material.Sign) sign.getData();
signMaterial.setFacingDirection(face);
return sign;
}
private Reflect getReflectPlayerProfile(String url) {
Reflect reflectProperty = onClass("com.mojang.authlib.properties.Property")
.create("textures", Base64.getEncoder()
.encodeToString(("{textures:{SKIN:{url:\"" + url + "\"}}}").getBytes(StandardCharsets.UTF_8)));
Reflect reflectGameProfile = onClass(CLASS_GAMEPROFILE)
.create(UUID.randomUUID(), null);
reflectGameProfile.call("getProperties")
.call("put", "textures", reflectProperty.get());
return reflectGameProfile;
}
private Class> getClassForName(String className) {
try {
return Class.forName(className);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}