Skip to content

Commit

Permalink
Fix #saturate-Pattern (#1944)
Browse files Browse the repository at this point in the history
* fix: block type access + resources in #saturate pattern

* chore: no need to download jar file in loadModTextures
  • Loading branch information
PierreSchwang authored Sep 21, 2022
1 parent 1706d64 commit ab55d07
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws W
}
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
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;
Expand Down Expand Up @@ -427,7 +426,7 @@ public static TextureUtil fromClipboard(Clipboard clipboard) throws FileNotFound
HashSet<BlockType> 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);
Expand All @@ -445,7 +444,7 @@ public static TextureUtil fromMask(Mask mask) throws FileNotFoundException {

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);
Expand Down Expand Up @@ -685,7 +684,7 @@ public BlockType getNearestBlock(int color) {
if (min == Long.MAX_VALUE) {
return null;
}
return BlockTypes.get(closest);
return BlockTypesCache.values[closest];
}

/**
Expand Down Expand Up @@ -714,7 +713,7 @@ public BlockType getNextNearestBlock(int color) {
if (min == Long.MAX_VALUE) {
return null;
}
return BlockTypes.get(closest);
return BlockTypesCache.values[closest];
}

/**
Expand All @@ -737,8 +736,8 @@ public BlockType[] getNearestLayer(int color) {
}
}
}
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;
}

Expand Down Expand Up @@ -905,36 +904,25 @@ public void loadModTextures() throws IOException {
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#<init>
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<Map<String, Object>>() {
Expand All @@ -958,10 +946,15 @@ public void loadModTextures() throws IOException {
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;
}

Expand Down Expand Up @@ -1179,7 +1172,8 @@ protected BlockType getNearestBlock(int color, boolean darker) {
if (min == Long.MAX_VALUE) {
return null;
}
return BlockTypes.get(closest);

return BlockTypesCache.values[closest];
}

private String getFileName(String path) {
Expand Down

0 comments on commit ab55d07

Please sign in to comment.