From ef38e23040d03ff2c73a9a68fd3f829876aca26c Mon Sep 17 00:00:00 2001 From: XiaoMoMi <972454774@qq.com> Date: Mon, 29 Sep 2025 01:36:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=97=E4=BD=93=E5=88=86?= =?UTF-8?q?=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/block/AbstractBlockManager.java | 22 +++++++---- .../core/font/AbstractFontManager.java | 37 +++++++++++-------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/net/momirealms/craftengine/core/block/AbstractBlockManager.java b/core/src/main/java/net/momirealms/craftengine/core/block/AbstractBlockManager.java index 898a99a9e..8f2f9f035 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/block/AbstractBlockManager.java +++ b/core/src/main/java/net/momirealms/craftengine/core/block/AbstractBlockManager.java @@ -46,6 +46,7 @@ import java.io.IOException; import java.nio.file.Path; import java.util.*; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutionException; public abstract class AbstractBlockManager extends AbstractModelGenerator implements BlockManager { @@ -428,13 +429,9 @@ public abstract class AbstractBlockManager extends AbstractModelGenerator implem } } - CompletableFutures.allOf(internalIdAllocators).thenRun(() -> ResourceConfigUtils.runCatching(path, node, () -> { - for (int i = 0; i < internalIdAllocators.size(); i++) { - CompletableFuture future = internalIdAllocators.get(i); - try { - int internalId = future.get(); - states.get(i).setCustomBlockState(BlockRegistryMirror.byId(internalId + AbstractBlockManager.this.vanillaBlockStateCount)); - } catch (ExecutionException e) { + CompletableFutures.allOf(internalIdAllocators).whenComplete((v, t) -> ResourceConfigUtils.runCatching(path, node, () -> { + if (t != null) { + if (t instanceof CompletionException e) { Throwable cause = e.getCause(); // 这里不会有conflict了,因为之前已经判断过了 if (cause instanceof IdAllocator.IdExhaustedException) { @@ -443,7 +440,16 @@ public abstract class AbstractBlockManager extends AbstractModelGenerator implem Debugger.BLOCK.warn(() -> "Unknown error while allocating internal block state id.", cause); return; } - } catch (InterruptedException e) { + } + throw new RuntimeException("Unknown error occurred", t); + } + + for (int i = 0; i < internalIdAllocators.size(); i++) { + CompletableFuture future = internalIdAllocators.get(i); + try { + int internalId = future.get(); + states.get(i).setCustomBlockState(BlockRegistryMirror.byId(internalId + AbstractBlockManager.this.vanillaBlockStateCount)); + } catch (InterruptedException | ExecutionException e) { AbstractBlockManager.this.plugin.logger().warn("Interrupted while allocating internal block state for block " + id.asString(), e); return; } diff --git a/core/src/main/java/net/momirealms/craftengine/core/font/AbstractFontManager.java b/core/src/main/java/net/momirealms/craftengine/core/font/AbstractFontManager.java index 37b33b51b..999dcc6da 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/font/AbstractFontManager.java +++ b/core/src/main/java/net/momirealms/craftengine/core/font/AbstractFontManager.java @@ -28,6 +28,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.*; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutionException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -572,7 +573,7 @@ public abstract class AbstractFontManager implements FontManager { codepoints = CharacterUtils.charsToCodePoints(charString.toCharArray()); } for (int j = 0; j < codepoints.length; j++) { - futureCodepoints.add(allocator.assignFixedId(id.asString() + ":" + i + ":" + j, codepoints[i])); + futureCodepoints.add(allocator.assignFixedId(id.asString() + ":" + i + ":" + j, codepoints[j])); } if (tempColumns == -1) { tempColumns = codepoints.length; @@ -607,26 +608,32 @@ public abstract class AbstractFontManager implements FontManager { } } - CompletableFutures.allOf(futureCodepoints).thenRun(() -> ResourceConfigUtils.runCatching(path, node, () -> { + CompletableFutures.allOf(futureCodepoints).whenComplete((v, t) -> ResourceConfigUtils.runCatching(path, node, () -> { + if (t != null) { + if (t instanceof CompletionException e) { + Throwable cause = e.getCause(); + if (cause instanceof IdAllocator.IdConflictException conflict) { + throw new LocalizedResourceConfigException("warning.config.image.codepoint.conflict", + fontId.toString(), + CharacterUtils.encodeCharsToUnicode(Character.toChars(conflict.id())), + new String(Character.toChars(conflict.id())), + conflict.previousOwner() + ); + } else if (cause instanceof IdAllocator.IdExhaustedException) { + throw new LocalizedResourceConfigException("warning.config.image.codepoint.exhausted", fontId.asString()); + } + } + throw new RuntimeException("Unknown error occurred", t); + } + int[][] codepointGrid = new int[rows][columns]; + for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { try { int codepoint = futureCodepoints.get(i * columns + j).get(); codepointGrid[i][j] = codepoint; - } catch (ExecutionException e) { - Throwable cause = e.getCause(); - if (cause instanceof IdAllocator.IdConflictException conflict) { - throw new LocalizedResourceConfigException("warning.config.image.codepoint.conflict", - fontId.toString(), - CharacterUtils.encodeCharsToUnicode(Character.toChars(conflict.id())), - new String(Character.toChars(conflict.id())), - conflict.previousOwner() - ); - } else if (cause instanceof IdAllocator.IdExhaustedException) { - throw new LocalizedResourceConfigException("warning.config.image.codepoint.exhausted", fontId.asString()); - } - } catch (InterruptedException e) { + } catch (InterruptedException | ExecutionException e) { AbstractFontManager.this.plugin.logger().warn("Interrupted while allocating codepoint for image " + id.asString(), e); return; }