diff --git a/core/src/main/java/org/geysermc/geyser/configuration/GeyserConfiguration.java b/core/src/main/java/org/geysermc/geyser/configuration/GeyserConfiguration.java index d74d804df..914499e7c 100644 --- a/core/src/main/java/org/geysermc/geyser/configuration/GeyserConfiguration.java +++ b/core/src/main/java/org/geysermc/geyser/configuration/GeyserConfiguration.java @@ -91,6 +91,13 @@ public interface GeyserConfiguration { boolean isAddNonBedrockItems(); + boolean isAddCustomBlocks(); + + boolean isAddCustomSkullBlocks(); + + // TODO this should probably go in a different file? + List getCustomSkullProfiles(); + boolean isAboveBedrockNetherBuilding(); boolean isForceResourcePacks(); diff --git a/core/src/main/java/org/geysermc/geyser/configuration/GeyserJacksonConfiguration.java b/core/src/main/java/org/geysermc/geyser/configuration/GeyserJacksonConfiguration.java index 77b351518..788f519ea 100644 --- a/core/src/main/java/org/geysermc/geyser/configuration/GeyserJacksonConfiguration.java +++ b/core/src/main/java/org/geysermc/geyser/configuration/GeyserJacksonConfiguration.java @@ -142,6 +142,15 @@ public abstract class GeyserJacksonConfiguration implements GeyserConfiguration @JsonProperty("above-bedrock-nether-building") private boolean aboveBedrockNetherBuilding = false; + @JsonProperty("add-custom-blocks") + boolean addCustomBlocks = true; + + @JsonProperty("add-custom-skull-blocks") + boolean addCustomSkullBlocks = false; + + @JsonProperty("custom-skull-profiles") + List customSkullProfiles = Collections.emptyList(); + @JsonProperty("force-resource-packs") private boolean forceResourcePacks = true; @@ -278,6 +287,10 @@ public abstract class GeyserJacksonConfiguration implements GeyserConfiguration @JsonProperty("config-version") private int configVersion = 0; + public boolean isAddCustomSkullBlocks() { + return addCustomBlocks && addCustomSkullBlocks; + } + /** * Ensure that the port deserializes in the config as a number no matter what. */ diff --git a/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java b/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java index 33eca67a9..b0de24225 100644 --- a/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java +++ b/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java @@ -108,7 +108,7 @@ public class UpstreamPacketHandler extends LoggingPacketHandler { header.getUuid().toString(), header.getVersionString(), resourcePack.getFile().length(), "", "", "", false, false)); } - resourcePacksInfo.setForcedToAccept(GeyserImpl.getInstance().getConfig().isForceResourcePacks()); + resourcePacksInfo.setForcedToAccept(GeyserImpl.getInstance().getConfig().isForceResourcePacks() || GeyserImpl.getInstance().getConfig().isAddCustomSkullBlocks()); session.sendUpstreamPacket(resourcePacksInfo); GeyserLocale.loadGeyserLocale(session.locale()); diff --git a/core/src/main/java/org/geysermc/geyser/pack/ResourcePack.java b/core/src/main/java/org/geysermc/geyser/pack/ResourcePack.java index 55a95b53c..4b63095a9 100644 --- a/core/src/main/java/org/geysermc/geyser/pack/ResourcePack.java +++ b/core/src/main/java/org/geysermc/geyser/pack/ResourcePack.java @@ -85,7 +85,7 @@ public class ResourcePack { return; } - Path skullResourcePack = SkullResourcePackManager.createResourcePack(BlockRegistries.CUSTOM_SKULLS.get().keySet()); + Path skullResourcePack = SkullResourcePackManager.createResourcePack(); if (skullResourcePack != null) { resourcePacks.add(skullResourcePack); } diff --git a/core/src/main/java/org/geysermc/geyser/pack/SkullResourcePackManager.java b/core/src/main/java/org/geysermc/geyser/pack/SkullResourcePackManager.java index 819811c9b..434dfaec6 100644 --- a/core/src/main/java/org/geysermc/geyser/pack/SkullResourcePackManager.java +++ b/core/src/main/java/org/geysermc/geyser/pack/SkullResourcePackManager.java @@ -26,11 +26,14 @@ package org.geysermc.geyser.pack; import it.unimi.dsi.fastutil.Pair; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import org.geysermc.geyser.GeyserImpl; +import org.geysermc.geyser.registry.BlockRegistries; import org.geysermc.geyser.skin.SkinProvider; import org.geysermc.geyser.util.FileUtils; import javax.imageio.ImageIO; +import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.nio.ByteBuffer; @@ -40,26 +43,40 @@ import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.List; import java.util.*; import java.util.function.UnaryOperator; +import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class SkullResourcePackManager { - private static final long RESOURCE_PACK_VERSION = 4; + private static final long RESOURCE_PACK_VERSION = 5; + + private static final Path SKULL_SKIN_CACHE_PATH = GeyserImpl.getInstance().getBootstrap().getConfigFolder().resolve("cache").resolve("player_skulls"); + + public static final Map SKULL_SKINS = new Object2ObjectOpenHashMap<>(); @SuppressWarnings("ResultOfMethodCallIgnored") - public static Path createResourcePack(Set skins) { - Path packPath = GeyserImpl.getInstance().getBootstrap().getConfigFolder().resolve("cache").resolve("player_skulls.mcpack"); - File packFile = packPath.toFile(); - if (skins.isEmpty()) { - packFile.delete(); // No need to keep resource pack - GeyserImpl.getInstance().getLogger().debug("No skins to create player skull resource pack."); + public static Path createResourcePack() { + Path cachePath = GeyserImpl.getInstance().getBootstrap().getConfigFolder().resolve("cache"); + try { + Files.createDirectories(cachePath); + } catch (IOException e) { + GeyserImpl.getInstance().getLogger().severe("Unable to create directories for player skull resource pack!", e); return null; } - if (packFile.exists() && canReusePack(skins, packFile)) { + cleanSkullSkinCache(); + + Path packPath = cachePath.resolve("player_skulls.mcpack"); + File packFile = packPath.toFile(); + if (BlockRegistries.CUSTOM_SKULLS.get().isEmpty() || !GeyserImpl.getInstance().getConfig().isAddCustomSkullBlocks()) { + packFile.delete(); // No need to keep resource pack + return null; + } + if (packFile.exists() && canReusePack(packFile)) { GeyserImpl.getInstance().getLogger().info("Reusing cached player skull resource pack."); return packPath; } @@ -68,19 +85,74 @@ public class SkullResourcePackManager { GeyserImpl.getInstance().getLogger().info("Creating skull resource pack."); packFile.delete(); try (ZipOutputStream zipOS = new ZipOutputStream(Files.newOutputStream(packPath, StandardOpenOption.WRITE, StandardOpenOption.CREATE))) { - addBaseResources(zipOS, skins); - addSkinTextures(zipOS, skins); - addAttachables(zipOS, skins); + addBaseResources(zipOS); + addSkinTextures(zipOS); + addAttachables(zipOS); GeyserImpl.getInstance().getLogger().info("Finished creating skull resource pack."); return packPath; } catch (IOException e) { - GeyserImpl.getInstance().getLogger().error("Unable to create player skull resource pack!", e); + GeyserImpl.getInstance().getLogger().severe("Unable to create player skull resource pack!", e); + GeyserImpl.getInstance().getLogger().severe("Bedrock players will see dirt blocks instead of custom skull blocks."); packFile.delete(); } return null; } - private static void addBaseResources(ZipOutputStream zipOS, Set skins) throws IOException { + public static void cacheSkullSkin(String skinUrl, String skinHash) throws IOException { + Path skinPath = SKULL_SKINS.get(skinHash); + if (skinPath != null) { + return; + } + + Files.createDirectories(SKULL_SKIN_CACHE_PATH); + skinPath = SKULL_SKIN_CACHE_PATH.resolve(skinHash + ".png"); + if (Files.exists(skinPath)) { + SKULL_SKINS.put(skinHash, skinPath); + return; + } + + BufferedImage image = SkinProvider.requestImage(skinUrl, null); + if (image.getHeight() != 64) { + // We have to resize legacy skins to 64x64 for them to be displayed properly + BufferedImage modernSkin = new BufferedImage(64, 64, image.getType()); + + Graphics g = modernSkin.createGraphics(); + g.drawImage(image, 0, 0, null); + g.setColor(new Color(0, 0, 0, 0)); + g.fillRect(0, 32, 64, 32); + g.dispose(); + + image.flush(); + image = modernSkin; + } + + ImageIO.write(image, "png", skinPath.toFile()); + SKULL_SKINS.put(skinHash, skinPath); + GeyserImpl.getInstance().getLogger().debug("Cached player skull to " + skinPath + " for " + skinHash); + } + + public static void cleanSkullSkinCache() { + try (Stream stream = Files.list(SKULL_SKIN_CACHE_PATH)) { + int removeCount = 0; + for (Path path : stream.toList()) { + String skinHash = path.getFileName().toString(); + skinHash = skinHash.substring(0, skinHash.length() - ".png".length()); + if (!SKULL_SKINS.containsKey(skinHash) && path.toFile().delete()) { + removeCount++; + } + } + if (removeCount != 0) { + GeyserImpl.getInstance().getLogger().debug("Removed " + removeCount + " unnecessary skull skins."); + } + } catch (IOException e) { + GeyserImpl.getInstance().getLogger().debug("Unable to clean up skull skin cache."); + if (GeyserImpl.getInstance().getConfig().isDebugMode()) { + e.printStackTrace(); + } + } + } + + private static void addBaseResources(ZipOutputStream zipOS) throws IOException { try (BufferedReader reader = new BufferedReader(new InputStreamReader(GeyserImpl.getInstance().getBootstrap().getResource("bedrock/skull_resource_pack_files.txt")))) { List lines = reader.lines().toList(); for (String path : lines) { @@ -90,9 +162,9 @@ public class SkullResourcePackManager { String resourcePath = "bedrock/" + path; switch (path) { case "skull_resource_pack/manifest.json" -> - fillTemplate(zipOS, resourcePath, template -> fillManifestJson(template, skins)); + fillTemplate(zipOS, resourcePath, SkullResourcePackManager::fillManifestJson); case "skull_resource_pack/textures/terrain_texture.json" -> - fillTemplate(zipOS, resourcePath, template -> fillTerrainTextureJson(template, skins)); + fillTemplate(zipOS, resourcePath, SkullResourcePackManager::fillTerrainTextureJson); default -> zipOS.write(FileUtils.readAllBytes(resourcePath)); } zipOS.closeEntry(); @@ -100,9 +172,9 @@ public class SkullResourcePackManager { } } - private static void addAttachables(ZipOutputStream zipOS, Set skins) throws IOException { + private static void addAttachables(ZipOutputStream zipOS) throws IOException { String template = new String(FileUtils.readAllBytes("bedrock/skull_resource_pack/attachables/template_attachable.json"), StandardCharsets.UTF_8); - for (String skinHash : skins) { + for (String skinHash : SKULL_SKINS.keySet()) { ZipEntry entry = new ZipEntry("skull_resource_pack/attachables/" + skinHash + ".json"); zipOS.putNextEntry(entry); zipOS.write(fillAttachableJson(template, skinHash).getBytes(StandardCharsets.UTF_8)); @@ -110,22 +182,13 @@ public class SkullResourcePackManager { } } - private static void addSkinTextures(ZipOutputStream zipOS, Set skins) throws IOException { - Path skullSkinCache = GeyserImpl.getInstance().getBootstrap().getConfigFolder().resolve("cache").resolve("player_skulls"); - Files.createDirectories(skullSkinCache); - for (String skin : skins) { - Path skinPath = skullSkinCache.resolve(skin + ".png"); - if (!Files.exists(skinPath)) { - // TODO this should go somewhere else and be async somehow - BufferedImage image = SkinProvider.requestImage("http://textures.minecraft.net/texture/" + skin, null); - ImageIO.write(image, "png", skinPath.toFile()); - GeyserImpl.getInstance().getLogger().debug("Cached player skull to file " + skinPath + " for " + skin); - } - - ZipEntry entry = new ZipEntry("skull_resource_pack/textures/blocks/" + skin + ".png"); - + private static void addSkinTextures(ZipOutputStream zipOS) throws IOException { + for (Path skinPath : SKULL_SKINS.values()) { + ZipEntry entry = new ZipEntry("skull_resource_pack/textures/blocks/" + skinPath.getFileName()); zipOS.putNextEntry(entry); - zipOS.write(FileUtils.readAllBytes(skinPath.toFile())); + try (InputStream stream = Files.newInputStream(skinPath)) { + stream.transferTo(zipOS); + } zipOS.closeEntry(); } } @@ -141,26 +204,26 @@ public class SkullResourcePackManager { .replace("${texture}", skinHash); } - private static String fillManifestJson(String template, Set skins) { - Pair uuids = generatePackUUIDs(skins); + private static String fillManifestJson(String template) { + Pair uuids = generatePackUUIDs(); return template.replace("${uuid1}", uuids.first().toString()) .replace("${uuid2}", uuids.second().toString()); } - private static String fillTerrainTextureJson(String template, Set skins) { + private static String fillTerrainTextureJson(String template) { StringBuilder textures = new StringBuilder(); - for (String skinHash : skins) { - String texture = String.format("\"geyser.%s_player_skin\":{\"textures\":\"textures/blocks/%s\"},", skinHash, skinHash); + for (String skinHash : SKULL_SKINS.keySet()) { + String texture = String.format("\"geyser.%s_player_skin\":{\"textures\":\"textures/blocks/%s\"},\n", skinHash, skinHash); textures.append(texture); } if (textures.length() != 0) { // Remove trailing comma - textures.deleteCharAt(textures.length() - 1); + textures.delete(textures.length() - 2, textures.length()); } return template.replace("${texture_data}", textures); } - private static Pair generatePackUUIDs(Set skins) { + private static Pair generatePackUUIDs() { UUID uuid1 = UUID.randomUUID(); UUID uuid2 = UUID.randomUUID(); try { @@ -168,7 +231,7 @@ public class SkullResourcePackManager { for (int i = 0; i < 8; i++) { md.update((byte) ((RESOURCE_PACK_VERSION >> (i * 8)) & 0xFF)); } - skins.stream() + SKULL_SKINS.keySet().stream() .sorted() .map(hash -> hash.getBytes(StandardCharsets.UTF_8)) .forEach(md::update); @@ -183,8 +246,8 @@ public class SkullResourcePackManager { return Pair.of(uuid1, uuid2); } - private static boolean canReusePack(Set skins, File packFile) { - Pair uuids = generatePackUUIDs(skins); + private static boolean canReusePack(File packFile) { + Pair uuids = generatePackUUIDs(); try (ZipFile zipFile = new ZipFile(packFile)) { Optional manifestEntry = zipFile.stream() .filter(entry -> entry.getName().contains("manifest.json")) diff --git a/core/src/main/java/org/geysermc/geyser/registry/BlockRegistries.java b/core/src/main/java/org/geysermc/geyser/registry/BlockRegistries.java index 1d13e1acf..7e4c0f5c7 100644 --- a/core/src/main/java/org/geysermc/geyser/registry/BlockRegistries.java +++ b/core/src/main/java/org/geysermc/geyser/registry/BlockRegistries.java @@ -32,6 +32,7 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import org.geysermc.geyser.api.block.custom.CustomBlockData; import org.geysermc.geyser.registry.loader.RegistryLoaders; import org.geysermc.geyser.registry.populator.BlockRegistryPopulator; +import org.geysermc.geyser.registry.populator.CustomSkullRegistryPopulator; import org.geysermc.geyser.registry.type.BlockMapping; import org.geysermc.geyser.registry.type.BlockMappings; import org.geysermc.geyser.registry.type.CustomSkull; @@ -81,11 +82,11 @@ public class BlockRegistries { /** * A registry which stores skin texture hashes to custom skull blocks. - * TODO add loader/populator */ public static final SimpleMappedRegistry CUSTOM_SKULLS = SimpleMappedRegistry.create(RegistryLoaders.empty(Object2ObjectOpenHashMap::new)); static { + CustomSkullRegistryPopulator.populate(); BlockRegistryPopulator.populate(); } diff --git a/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java b/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java index e6a210f18..2fbefe03d 100644 --- a/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java +++ b/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java @@ -86,7 +86,11 @@ public class BlockRegistryPopulator { BLOCKS_JSON = null; } + private static void registerCustomBedrockBlocks() { + if (!GeyserImpl.getInstance().getConfig().isAddCustomBlocks()) { + return; + } List customBlocks = new ArrayList<>(); GeyserImpl.getInstance().getEventBus().fire(new GeyserDefineCustomBlocksEvent() { @Override diff --git a/core/src/main/java/org/geysermc/geyser/registry/populator/CustomSkullRegistryPopulator.java b/core/src/main/java/org/geysermc/geyser/registry/populator/CustomSkullRegistryPopulator.java new file mode 100644 index 000000000..c2719ae2d --- /dev/null +++ b/core/src/main/java/org/geysermc/geyser/registry/populator/CustomSkullRegistryPopulator.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.geyser.registry.populator; + +import it.unimi.dsi.fastutil.objects.Object2ObjectMaps; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import org.geysermc.geyser.GeyserImpl; +import org.geysermc.geyser.pack.SkullResourcePackManager; +import org.geysermc.geyser.registry.BlockRegistries; +import org.geysermc.geyser.registry.type.CustomSkull; +import org.geysermc.geyser.skin.SkinManager; + +import java.io.IOException; +import java.util.Map; + +public class CustomSkullRegistryPopulator { + + public static void populate() { + SkullResourcePackManager.SKULL_SKINS.clear(); // Remove skins after reloading + if (!GeyserImpl.getInstance().getConfig().isAddCustomSkullBlocks()) { + BlockRegistries.CUSTOM_SKULLS.set(Object2ObjectMaps.emptyMap()); + return; + } + + Map customSkulls = new Object2ObjectOpenHashMap<>(); + for (String skullProfile : GeyserImpl.getInstance().getConfig().getCustomSkullProfiles()) { + try { + SkinManager.GameProfileData profileData = SkinManager.GameProfileData.loadFromJson(skullProfile); + if (profileData == null) { + GeyserImpl.getInstance().getLogger().warning("Skull profile " + skullProfile + " contained no skins and will not be added as a custom block."); + continue; + } + try { + String skinUrl = profileData.skinUrl(); + String skinHash = skinUrl.substring(skinUrl.lastIndexOf("/") + 1); + SkullResourcePackManager.cacheSkullSkin(skinUrl, skinHash); + customSkulls.put(skinHash, new CustomSkull(skinHash)); + } catch (IOException e) { + GeyserImpl.getInstance().getLogger().error("Failed to cache skin for skull profile " + skullProfile + " This skull will not be added as a custom block.", e); + } + } catch (IOException e) { + GeyserImpl.getInstance().getLogger().error("Skull profile " + skullProfile + " is invalid and will not be added as a custom block.", e); + } + } + GeyserImpl.getInstance().getLogger().debug("Registered " + customSkulls.size() + " custom skulls as custom blocks."); + BlockRegistries.CUSTOM_SKULLS.set(customSkulls); + } +} diff --git a/core/src/main/resources/config.yml b/core/src/main/resources/config.yml index d1a956187..4d8e6e0fc 100644 --- a/core/src/main/resources/config.yml +++ b/core/src/main/resources/config.yml @@ -161,6 +161,19 @@ custom-skull-render-distance: 32 # This option requires a restart of Geyser in order to change its setting. add-non-bedrock-items: true +# Whether to allow custom blocks to be added. +# This should only need to be disabled if using a proxy that does not use the "transfer packet" style of server switching. +add-custom-blocks: true + +# Whether to allow some custom skulls to be translated as custom blocks. This requires `add-custom-blocks` and `allow-custom-skulls` to be +# enabled. This will generate a resource pack for Bedrock players and enables `force-resource-packs` +add-custom-skull-blocks: true + +# List of custom skull profiles to translate as custom blocks. This requires `add-custom-skull-blocks` to be enabled. +# This is the Value stored in the SkullOwner of the custom skull +custom-skull-profiles: + - ewogICJ0aW1lc3RhbXAiIDogMTY1NzMyMjIzOTgzMywKICAicHJvZmlsZUlkIiA6ICJjZGRiZTUyMGQwNDM0YThiYTFjYzlmYzkyZmRlMmJjZiIsCiAgInByb2ZpbGVOYW1lIiA6ICJkYXZjaG9vIiwKICAidGV4dHVyZXMiIDogewogICAgIlNLSU4iIDogewogICAgICAidXJsIiA6ICJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2E5MDc5MGM1N2UxODFlZDEzYWRlZDE0YzQ3ZWUyZjdjOGRlMzUzM2UwMTdiYTk1N2FmN2JkZjlkZjFiZGU5NGYiLAogICAgICAibWV0YWRhdGEiIDogewogICAgICAgICJtb2RlbCIgOiAic2xpbSIKICAgICAgfQogICAgfQogIH0KfQ + # Bedrock prevents building and displaying blocks above Y127 in the Nether. # This config option works around that by changing the Nether dimension ID to the End ID. # The main downside to this is that the entire Nether will have the same red fog rather than having different fog for each biome.