From ab55d07ffddbae559544f6e6cfb77069cd8ebf50 Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Wed, 21 Sep 2022 16:17:41 +0200 Subject: [PATCH] Fix #saturate-Pattern (#1944) * fix: block type access + resources in #saturate pattern * chore: no need to download jar file in loadModTextures --- .../function/pattern/SaturatePattern.java | 2 +- .../core/util/TextureUtil.java | 62 +++++++++---------- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/function/pattern/SaturatePattern.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/function/pattern/SaturatePattern.java index 48c411750..0c42c814a 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/function/pattern/SaturatePattern.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/function/pattern/SaturatePattern.java @@ -66,7 +66,7 @@ public class SaturatePattern extends AbstractPattern { } int newColor = TextureUtil.multiplyColor(currentColor, color); BlockType newBlock = util.getNearestBlock(newColor); - if (newBlock.equals(type)) { + if (newBlock == null || newBlock.equals(type)) { return false; } return set.setBlock(extent, newBlock.getDefaultState()); diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/TextureUtil.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/TextureUtil.java index 6261249fc..078b0dc38 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/TextureUtil.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/TextureUtil.java @@ -30,7 +30,6 @@ import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -427,7 +426,7 @@ public class TextureUtil implements TextureHolder { HashSet blocks = new HashSet<>(); for (int typeId = 0; typeId < ids.length; typeId++) { if (ids[typeId]) { - blocks.add(BlockTypes.get(typeId)); + blocks.add(BlockTypesCache.values[typeId]); } } return fromBlocks(blocks); @@ -445,7 +444,7 @@ public class TextureUtil implements TextureHolder { TextureUtil tu = Fawe.instance().getTextureUtil(); for (int typeId : tu.getValidBlockIds()) { - BlockType block = BlockTypes.get(typeId); + BlockType block = BlockTypesCache.values[typeId]; extent.init(0, 0, 0, block.getDefaultState().toBaseBlock()); if (mask.test(extent)) { blocks.add(block); @@ -685,7 +684,7 @@ public class TextureUtil implements TextureHolder { if (min == Long.MAX_VALUE) { return null; } - return BlockTypes.get(closest); + return BlockTypesCache.values[closest]; } /** @@ -714,7 +713,7 @@ public class TextureUtil implements TextureHolder { if (min == Long.MAX_VALUE) { return null; } - return BlockTypes.get(closest); + return BlockTypesCache.values[closest]; } /** @@ -737,8 +736,8 @@ public class TextureUtil implements TextureHolder { } } } - layerBuffer[0] = BlockTypes.get(closest[0]); - layerBuffer[1] = BlockTypes.get(closest[1]); + layerBuffer[0] = BlockTypesCache.values[closest[0]]; + layerBuffer[1] = BlockTypesCache.values[closest[1]]; return layerBuffer; } @@ -905,36 +904,25 @@ public class TextureUtil implements TextureHolder { if (folder.exists()) { // Get all the jar files File[] files = folder.listFiles((dir, name) -> name.endsWith(".jar")); - if (files.length == 0) { - new File(Fawe.platform().getDirectory() + "/" + Settings.settings().PATHS.TEXTURES + "/") - .mkdirs(); - try (BufferedInputStream in = new BufferedInputStream( - new URL("https://piston-data.mojang.com/v1/objects/c0898ec7c6a5a2eaa317770203a1554260699994/client.jar") - .openStream()); - FileOutputStream fileOutputStream = new FileOutputStream( - Fawe.platform().getDirectory() + "/" + Settings.settings().PATHS.TEXTURES + "/1.19.2.jar")) { - byte[] dataBuffer = new byte[1024]; - int bytesRead; - while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { - fileOutputStream.write(dataBuffer, 0, bytesRead); - } - fileOutputStream.close(); - files = folder.listFiles((dir, name) -> name.endsWith(".jar")); - } catch (IOException e) { - LOGGER.error( - "Could not download version jar. Please do so manually by creating a `FastAsyncWorldEdit/textures` " + - "folder with a `.minecraft/versions` jar or mods in it."); - LOGGER.error("If the file exists, please make sure the server has read access to the directory."); - } + // We expect the latest version to be already there, due to the download in TextureUtil# + if (files == null || files.length == 0) { + LOGGER.error("No version jar found in {}. Delete the named folder and restart your server to download the " + + "missing assets.", folder.getPath()); + LOGGER.error( + "If no asset jar is created, please do so manually by creating a `FastAsyncWorldEdit/textures` " + + "folder with a `.minecraft/versions` jar or mods in it."); } - if ((files.length > 0)) { + if (files != null && (files.length > 0)) { for (File file : files) { ZipFile zipFile = new ZipFile(file); // Get all the groups in the current jar // The vanilla textures are in `assets/minecraft` // A jar may contain textures for multiple mods - String modelsDir = "assets/%1$s/models/block/%2$s.json"; + String[] modelsDir = { + "assets/%1$s/models/block/%2$s.json", + "assets/%1$s/models/item/%2$s.json" + }; String texturesDir = "assets/%1$s/textures/%2$s.png"; Type typeToken = new TypeToken>() { @@ -958,10 +946,15 @@ public class TextureUtil implements TextureHolder { String nameSpace = split.length == 1 ? "" : split[0]; // Read models - String modelFileName = String.format(modelsDir, nameSpace, name); - ZipEntry entry = getEntry(zipFile, modelFileName); + ZipEntry entry = null; + for (final String dir : modelsDir) { + String modelFileName = String.format(dir, nameSpace, name); + if ((entry = getEntry(zipFile, modelFileName)) != null) { + break; + } + } if (entry == null) { - LOGGER.error("Cannot find {} in {}", modelFileName, file); + LOGGER.error("Cannot find {} in {}", modelsDir, file); continue; } @@ -1179,7 +1172,8 @@ public class TextureUtil implements TextureHolder { if (min == Long.MAX_VALUE) { return null; } - return BlockTypes.get(closest); + + return BlockTypesCache.values[closest]; } private String getFileName(String path) {