-
-
Notifications
You must be signed in to change notification settings - Fork 11
Emit an event when a crafting job is completed #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rubensworks
merged 2 commits into
master-1.21-lts
from
feature/crafting-job-finished-event
Sep 2, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/org/cyclops/integratedcrafting/api/event/CraftingJobFinishedEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package org.cyclops.integratedcrafting.api.event; | ||
|
|
||
| import net.neoforged.bus.api.Event; | ||
| import net.neoforged.neoforge.common.NeoForge; | ||
| import org.cyclops.integratedcrafting.api.crafting.CraftingJob; | ||
| import org.cyclops.integratedcrafting.api.network.ICraftingNetwork; | ||
|
|
||
| /** | ||
| * An event that is emitted on the NeoForge event bus when a crafting job has been completed. | ||
| * | ||
| * This is only emitted for jobs that ran to completion, | ||
| * so not for jobs that were cancelled or that were removed together with their crafting interface. | ||
| * | ||
| * This is emitted for every completed job, including the dependencies of a job. | ||
| * Jobs that were requested directly can be identified via {@link #isRootJob()}. | ||
| * | ||
| * @author rubensworks | ||
| */ | ||
| public class CraftingJobFinishedEvent extends Event { | ||
|
|
||
| private final ICraftingNetwork craftingNetwork; | ||
| private final CraftingJob craftingJob; | ||
| private final boolean rootJob; | ||
|
|
||
| public CraftingJobFinishedEvent(ICraftingNetwork craftingNetwork, CraftingJob craftingJob) { | ||
| this.craftingNetwork = craftingNetwork; | ||
| this.craftingJob = craftingJob; | ||
| this.rootJob = craftingJob.getDependentCraftingJobs().isEmpty(); | ||
| } | ||
|
|
||
| /** | ||
| * @return The crafting network in which the job was running. | ||
| */ | ||
| public ICraftingNetwork getCraftingNetwork() { | ||
| return craftingNetwork; | ||
| } | ||
|
|
||
| /** | ||
| * @return The completed crafting job. | ||
| */ | ||
| public CraftingJob getCraftingJob() { | ||
| return craftingJob; | ||
| } | ||
|
|
||
| /** | ||
| * @return If the job was requested directly, as opposed to being a dependency of another job. | ||
| * This is captured when the event is created, | ||
| * as the job's dependency links are cleared once it is removed from its network. | ||
| */ | ||
| public boolean isRootJob() { | ||
| return rootJob; | ||
| } | ||
|
|
||
| public static void post(ICraftingNetwork craftingNetwork, CraftingJob craftingJob) { | ||
| NeoForge.EVENT_BUS.post(new CraftingJobFinishedEvent(craftingNetwork, craftingJob)); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
src/main/java/org/cyclops/integratedcrafting/gametest/GameTestsCraftingJobFinishedEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| package org.cyclops.integratedcrafting.gametest; | ||
|
|
||
| import com.google.common.collect.Lists; | ||
| import net.minecraft.core.BlockPos; | ||
| import net.minecraft.gametest.framework.GameTest; | ||
| import net.minecraft.gametest.framework.GameTestHelper; | ||
| import net.minecraft.resources.ResourceLocation; | ||
| import net.minecraft.world.item.ItemStack; | ||
| import net.minecraft.world.item.Items; | ||
| import net.minecraft.world.item.crafting.RecipeType; | ||
| import net.minecraft.world.level.block.entity.ChestBlockEntity; | ||
| import net.neoforged.bus.api.SubscribeEvent; | ||
| import net.neoforged.neoforge.common.NeoForge; | ||
| import net.neoforged.neoforge.gametest.GameTestHolder; | ||
| import net.neoforged.neoforge.gametest.PrefixGameTestTemplate; | ||
| import org.apache.commons.lang3.tuple.Triple; | ||
| import org.cyclops.commoncapabilities.IngredientComponents; | ||
| import org.cyclops.commoncapabilities.api.capability.itemhandler.ItemMatch; | ||
| import org.cyclops.integratedcrafting.Reference; | ||
| import org.cyclops.integratedcrafting.api.crafting.CraftingJob; | ||
| import org.cyclops.integratedcrafting.api.crafting.CraftingJobDependencyGraph; | ||
| import org.cyclops.integratedcrafting.api.event.CraftingJobFinishedEvent; | ||
| import org.cyclops.integratedcrafting.core.CraftingHelpers; | ||
| import org.cyclops.integratedcrafting.part.PartTypeInterfaceCrafting; | ||
| import org.cyclops.integrateddynamics.api.network.INetwork; | ||
| import org.cyclops.integrateddynamics.api.network.IPositionedAddonsNetworkIngredients; | ||
| import org.cyclops.integrateddynamics.core.helper.NetworkHelpers; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * Game tests for {@link CraftingJobFinishedEvent}. | ||
| * @author rubensworks | ||
| */ | ||
| @GameTestHolder(Reference.MOD_ID) | ||
| @PrefixGameTestTemplate(false) | ||
| public class GameTestsCraftingJobFinishedEvent { | ||
|
|
||
| public static final String TEMPLATE_EMPTY = "empty10"; | ||
| public static final int TIMEOUT = 2000; | ||
| public static final BlockPos POS = BlockPos.ZERO.offset(2, 0, 2); | ||
|
|
||
| /** | ||
| * A job that runs to completion emits an event for the requested job. | ||
| */ | ||
| @GameTest(template = TEMPLATE_EMPTY, timeoutTicks = TIMEOUT) | ||
| public void testEventOnCompletedJob(GameTestHelper helper) { | ||
| prepareNetwork(helper); | ||
| UUID initiator = UUID.randomUUID(); | ||
| EventCollector collector = EventCollector.start(initiator); | ||
|
|
||
| helper.startSequence() | ||
| .thenIdle(20) | ||
| .thenExecute(() -> scheduleChestJob(helper, initiator, true)) | ||
| .thenWaitUntil(() -> helper.assertTrue(collector.getRootJobs().size() == 1, | ||
| "Expected exactly one completion event for the requested job, but got " | ||
| + collector.getRootJobs().size())) | ||
| .thenExecute(() -> { | ||
| CraftingJob craftingJob = collector.getRootJobs().get(0); | ||
| helper.assertTrue(craftingJob.isNotifyInitiator(), | ||
| "The completed job did not carry the notify flag"); | ||
| helper.assertFalse(craftingJob.isCancelled(), | ||
| "The completed job was marked as cancelled"); | ||
| helper.assertTrue(craftingJob.getAmountTotal() == 1, | ||
| "The completed job did not retain its total amount, but had " | ||
| + craftingJob.getAmountTotal()); | ||
| helper.assertTrue(craftingJob.getAmount() == 0, | ||
| "The completed job still had a remaining amount"); | ||
| collector.stop(); | ||
| }) | ||
| .thenSucceed(); | ||
| } | ||
|
|
||
| /** | ||
| * The notify flag is not set when the initiator did not ask to be notified. | ||
| */ | ||
| @GameTest(template = TEMPLATE_EMPTY, timeoutTicks = TIMEOUT) | ||
| public void testEventWithoutNotifyFlag(GameTestHelper helper) { | ||
| prepareNetwork(helper); | ||
| UUID initiator = UUID.randomUUID(); | ||
| EventCollector collector = EventCollector.start(initiator); | ||
|
|
||
| helper.startSequence() | ||
| .thenIdle(20) | ||
| .thenExecute(() -> scheduleChestJob(helper, initiator, false)) | ||
| .thenWaitUntil(() -> helper.assertTrue(collector.getRootJobs().size() == 1, | ||
| "Expected exactly one completion event for the requested job")) | ||
| .thenExecute(() -> { | ||
| helper.assertFalse(collector.getRootJobs().get(0).isNotifyInitiator(), | ||
| "The completed job carried the notify flag"); | ||
| collector.stop(); | ||
| }) | ||
| .thenSucceed(); | ||
| } | ||
|
|
||
| /** | ||
| * A job that is cancelled does not emit a completion event. | ||
| */ | ||
| @GameTest(template = TEMPLATE_EMPTY, timeoutTicks = TIMEOUT) | ||
| public void testNoEventOnCancelledJob(GameTestHelper helper) { | ||
| prepareNetwork(helper); | ||
| UUID initiator = UUID.randomUUID(); | ||
| EventCollector collector = EventCollector.start(initiator); | ||
|
|
||
| helper.startSequence() | ||
| .thenIdle(20) | ||
| .thenExecute(() -> { | ||
| CraftingJob craftingJob = scheduleChestJob(helper, initiator, true); | ||
| helper.assertTrue(CraftingHelpers.getCraftingNetworkChecked(getNetwork(helper)) | ||
| .cancelCraftingJob(craftingJob.getChannel(), craftingJob.getId()), | ||
| "The crafting job could not be cancelled"); | ||
| }) | ||
| .thenIdle(200) | ||
| .thenExecute(() -> { | ||
| helper.assertTrue(collector.getAllJobs().isEmpty(), | ||
| "A completion event was emitted for a cancelled job"); | ||
| collector.stop(); | ||
| }) | ||
| .thenSucceed(); | ||
| } | ||
|
|
||
| private static void prepareNetwork(GameTestHelper helper) { | ||
| GameTestHelpersIntegratedCrafting.INetworkPositions<PartTypeInterfaceCrafting.State> positions = | ||
| GameTestHelpersIntegratedCrafting.createBasicNetwork(helper, POS); | ||
|
|
||
| // Insert crafting inputs in the interface chest | ||
| ChestBlockEntity chest = helper.getBlockEntity(POS.east()); | ||
| chest.setItem(0, new ItemStack(Items.OAK_PLANKS, 64)); | ||
|
|
||
| // Add the chest recipe to the crafting interface | ||
| positions.interfaceRecipeAdders().get(0).accept(Triple.of(0, RecipeType.CRAFTING, | ||
| ResourceLocation.fromNamespaceAndPath("minecraft", "chest"))); | ||
| } | ||
|
|
||
| private static CraftingJob scheduleChestJob(GameTestHelper helper, UUID initiator, boolean notifyInitiator) { | ||
| INetwork network = getNetwork(helper); | ||
| int channel = IPositionedAddonsNetworkIngredients.DEFAULT_CHANNEL; | ||
| try { | ||
| CraftingJobDependencyGraph dependencyGraph = new CraftingJobDependencyGraph(); | ||
| CraftingJob craftingJob = CraftingHelpers.calculateCraftingJobs(network, channel, | ||
| IngredientComponents.ITEMSTACK, new ItemStack(Items.CHEST), ItemMatch.ITEM, true, | ||
| CraftingHelpers.getGlobalCraftingJobIdentifier(), dependencyGraph, false); | ||
| CraftingHelpers.scheduleCraftingJobs(CraftingHelpers.getCraftingNetworkChecked(network), | ||
| CraftingHelpers.getNetworkStorageGetter(network, channel, false), dependencyGraph, true, | ||
| initiator, notifyInitiator); | ||
| return craftingJob; | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException("Crafting job could not be scheduled", e); | ||
| } | ||
| } | ||
|
|
||
| private static INetwork getNetwork(GameTestHelper helper) { | ||
| return NetworkHelpers.getNetwork(helper.getLevel(), helper.absolutePos(POS), null) | ||
| .orElseThrow(() -> new IllegalStateException("Could not find a network")); | ||
| } | ||
|
|
||
| /** | ||
| * Collects the events of a single initiator, so that concurrently running tests don't interfere. | ||
| */ | ||
| public static class EventCollector { | ||
|
|
||
| private final UUID initiator; | ||
| private final List<CraftingJob> allJobs = Lists.newArrayList(); | ||
| private final List<CraftingJob> rootJobs = Lists.newArrayList(); | ||
|
|
||
| public EventCollector(UUID initiator) { | ||
| this.initiator = initiator; | ||
| } | ||
|
|
||
| public static EventCollector start(UUID initiator) { | ||
| EventCollector collector = new EventCollector(initiator); | ||
| NeoForge.EVENT_BUS.register(collector); | ||
| return collector; | ||
| } | ||
|
|
||
| public void stop() { | ||
| NeoForge.EVENT_BUS.unregister(this); | ||
| } | ||
|
|
||
| public List<CraftingJob> getAllJobs() { | ||
| return allJobs; | ||
| } | ||
|
|
||
| public List<CraftingJob> getRootJobs() { | ||
| return rootJobs; | ||
| } | ||
|
|
||
| @SubscribeEvent | ||
| public void onCraftingJobFinished(CraftingJobFinishedEvent event) { | ||
| CraftingJob craftingJob = event.getCraftingJob(); | ||
| if (!this.initiator.toString().equals(craftingJob.getInitiatorUuid())) { | ||
| return; | ||
| } | ||
| this.allJobs.add(craftingJob); | ||
| if (event.isRootJob()) { | ||
| this.rootJobs.add(craftingJob); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.