Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ permissions:
contents: read

env:
COMMON_BRANCH: main
COMMON_BRANCH: common
COMMON_DIR: Dissolver Enhanced (Common)
TARGET_DIR: Dissolver Enhanced (Target)

jobs:
build-common:
name: Build Common
if: ${{ github.ref_name == 'main' }}
if: ${{ github.ref_name == 'common' }}
runs-on: ubuntu-22.04
steps:
- name: Checkout Common
Expand Down Expand Up @@ -59,7 +59,7 @@ jobs:

discover-version-branches:
name: Discover Version Branches
if: ${{ github.ref_name == 'main' }}
if: ${{ github.ref_name == 'common' }}
runs-on: ubuntu-22.04
outputs:
branches: ${{ steps.branches.outputs.branches }}
Expand All @@ -81,7 +81,7 @@ jobs:

build-all-version-branches:
name: Build ${{ matrix.branch }}
if: ${{ github.ref_name == 'main' && needs.discover-version-branches.outputs.branches != '[]' }}
if: ${{ github.ref_name == 'common' && needs.discover-version-branches.outputs.branches != '[]' }}
needs: discover-version-branches
runs-on: ubuntu-22.04
strategy:
Expand Down Expand Up @@ -186,7 +186,7 @@ jobs:

package-all-builds:
name: Package All Builds
if: ${{ github.ref_name == 'main' && needs.build-common.result == 'success' && needs.build-all-version-branches.result == 'success' }}
if: ${{ github.ref_name == 'common' && needs.build-common.result == 'success' && needs.build-all-version-branches.result == 'success' }}
needs:
- build-common
- build-all-version-branches
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.exohayvan.dissolver_enhanced.data;

public final class RecipeLoadCoordinator {
public static final RecipeLoadCoordinator GLOBAL = new RecipeLoadCoordinator();

private final Object monitor = new Object();

public void runExclusive(Runnable load) {
synchronized (monitor) {
load.run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.core.Holder;
import net.exohayvan.dissolver_enhanced.DissolverEnhanced;
import net.exohayvan.dissolver_enhanced.data.EMCValues;
import net.exohayvan.dissolver_enhanced.data.RecipeLoadCoordinator;
import net.exohayvan.dissolver_enhanced.helpers.ItemHelper;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -38,6 +39,10 @@ public static void onServerStarted(ServerStartedEvent event) {
}

private static void loadRecipes(MinecraftServer server) {
RecipeLoadCoordinator.GLOBAL.runExclusive(() -> loadRecipesLocked(server));
}

private static void loadRecipesLocked(MinecraftServer server) {
RECIPES.clear();
RECIPE_SOURCES.clear();
RECIPE_JSON.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.gson.JsonParseException;
import net.exohayvan.dissolver_enhanced.DissolverEnhanced;
import net.exohayvan.dissolver_enhanced.data.EMCValues;
import net.exohayvan.dissolver_enhanced.data.RecipeLoadCoordinator;
import net.exohayvan.dissolver_enhanced.helpers.ItemHelper;
import net.exohayvan.dissolver_enhanced.helpers.RecipeGenerator;
import net.minecraft.core.HolderLookup;
Expand Down Expand Up @@ -58,30 +59,28 @@ private void applyMixin(Object preparedRecipes, ResourceManager resourceManager,
if (map == null) {
return;
}

EMCValues.beginStartup(map.size());
RECIPES.clear();
RECIPE_SOURCES.clear();
RECIPE_JSON.clear();
STONE_CUTTER_LIST.clear();
// let tag items load before looking through recipes
new Thread(() -> {
Map<ResourceLocation, JsonElement> recipeSnapshot = new HashMap<>(map);

new Thread(() -> RecipeLoadCoordinator.GLOBAL.runExclusive(() -> {
EMCValues.beginStartup(recipeSnapshot.size());
RECIPES.clear();
RECIPE_SOURCES.clear();
RECIPE_JSON.clear();
STONE_CUTTER_LIST.clear();
wait(800);

Iterator<Map.Entry<ResourceLocation, JsonElement>> recipeIterator = map.entrySet().iterator();
while (recipeIterator.hasNext()) {
Map.Entry<ResourceLocation, JsonElement> entry = recipeIterator.next();
for (Map.Entry<ResourceLocation, JsonElement> entry : recipeSnapshot.entrySet()) {
try {
getRecipe(entry);
}catch (Exception e) {
} catch (Exception e) {
if (!getJsonRecipe(entry)) {
EMCValues.incrementRecipesNotUnderstood();
}
}
}

EMCValues.recipesLoaded(RECIPES, RECIPE_SOURCES, RECIPE_JSON, STONE_CUTTER_LIST);
}).start();
})).start();
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.exohayvan.dissolver_enhanced.data;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;

class RecipeLoadCoordinatorTest {
@Test
void runsOverlappingLoadsExclusively() throws Exception {
RecipeLoadCoordinator coordinator = new RecipeLoadCoordinator();
CountDownLatch firstStarted = new CountDownLatch(1);
CountDownLatch releaseFirst = new CountDownLatch(1);
CountDownLatch completed = new CountDownLatch(2);
AtomicInteger active = new AtomicInteger();
AtomicInteger peak = new AtomicInteger();
Thread first = new Thread(() -> coordinator.runExclusive(() -> { peak.accumulateAndGet(active.incrementAndGet(), Math::max); firstStarted.countDown(); try { releaseFirst.await(5, TimeUnit.SECONDS); } catch (InterruptedException exception) { Thread.currentThread().interrupt(); } active.decrementAndGet(); completed.countDown(); }));
Thread second = new Thread(() -> coordinator.runExclusive(() -> { peak.accumulateAndGet(active.incrementAndGet(), Math::max); active.decrementAndGet(); completed.countDown(); }));
first.start(); assertThat(firstStarted.await(5, TimeUnit.SECONDS)).isTrue(); second.start(); Thread.sleep(100); assertThat(peak.get()).isEqualTo(1); releaseFirst.countDown(); assertThat(completed.await(5, TimeUnit.SECONDS)).isTrue(); assertThat(peak.get()).isEqualTo(1);

Check warning on line 21 in src/test/java/net/exohayvan/dissolver_enhanced/data/RecipeLoadCoordinatorTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this use of "Thread.sleep()".

See more on https://sonarcloud.io/project/issues?id=Exohayvan_Dissolver-Enhanced&issues=AZ96_2TP5v-f9-AA3EHF&open=AZ96_2TP5v-f9-AA3EHF&pullRequest=12
}
}
Loading