-
-
Notifications
You must be signed in to change notification settings - Fork 10
Show a toast when a requested crafting job is completed #214
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rubensworks
merged 10 commits into
master-1.21-lts
from
feature/crafting-job-finished-toast
Sep 4, 2026
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6f75bb1
Show a toast when a requested crafting job is completed
rubensworks 9ebe94e
Fix generic inference when multiplying the completed job's outputs
rubensworks 19e3e4f
Label the crafting job notify checkbox
claude ac2c501
Keep the crafting plan buttons within the gui
claude 2318b42
Use the crafting job's total amount for the completion toast
claude 00996d4
Bump IntegratedCrafting to 1.5.0-681
claude adc5d71
Restyle the crafting job toast in the mod's colours
claude bc836f9
Show any crafted ingredient in the toast, and default the notify opti…
claude 07ffc23
Sum the quantities of crafting jobs that complete as one toast
claude a136074
Bump IntegratedCrafting to 1.5.0-689
claude 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
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
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
93 changes: 93 additions & 0 deletions
93
src/main/java/org/cyclops/integratedterminals/client/gui/toast/CraftingJobToast.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,93 @@ | ||
| package org.cyclops.integratedterminals.client.gui.toast; | ||
|
|
||
| import net.minecraft.client.Minecraft; | ||
| import net.minecraft.client.gui.GuiGraphics; | ||
| import net.minecraft.client.gui.components.toasts.Toast; | ||
| import net.minecraft.client.gui.components.toasts.ToastComponent; | ||
| import net.minecraft.network.chat.Component; | ||
| import net.minecraft.resources.ResourceLocation; | ||
| import net.minecraft.util.FormattedCharSequence; | ||
| import net.minecraft.world.item.ItemStack; | ||
| import org.cyclops.integratedterminals.Reference; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A toast that shows an item icon alongside a title and a wrapping subtitle. | ||
| * | ||
| * Toasts with an equal token replace each other instead of being queued, | ||
| * so the token determines how toasts are grouped. | ||
| * | ||
| * @author rubensworks | ||
| */ | ||
| public class CraftingJobToast implements Toast { | ||
|
|
||
| private static final ResourceLocation BACKGROUND_SPRITE = ResourceLocation.fromNamespaceAndPath(Reference.MOD_ID, "toast/crafting_job"); | ||
| private static final int DISPLAY_MILLIS = 5000; | ||
| private static final int MARGIN = 7; | ||
| private static final int ICON_LEFT = 7; | ||
| private static final int ICON_SIZE = 16; | ||
| private static final int TEXT_LEFT = ICON_LEFT + ICON_SIZE + 5; | ||
| private static final int LINE_SPACING = 12; | ||
|
|
||
| private final Object token; | ||
| private final ItemStack icon; | ||
| private Component title; | ||
| private List<FormattedCharSequence> subtitleLines; | ||
| private long lastChangedAt = Long.MIN_VALUE; | ||
| private boolean changed = true; | ||
|
|
||
| public CraftingJobToast(Object token, ItemStack icon, Component title, Component subtitle) { | ||
| this.token = token; | ||
| this.icon = icon; | ||
| this.title = title; | ||
| this.subtitleLines = splitSubtitle(subtitle); | ||
| } | ||
|
|
||
| /** | ||
| * Update the contents of this toast in-place, without queueing a new one. | ||
| * @param newTitle The new title. | ||
| * @param newSubtitle The new subtitle. | ||
| */ | ||
| public void reset(Component newTitle, Component newSubtitle) { | ||
| this.title = newTitle; | ||
| this.subtitleLines = splitSubtitle(newSubtitle); | ||
| this.changed = true; | ||
| } | ||
|
|
||
| private List<FormattedCharSequence> splitSubtitle(Component text) { | ||
| return Minecraft.getInstance().font.split(text, width() - TEXT_LEFT - MARGIN); | ||
| } | ||
|
|
||
| @Override | ||
| public int height() { | ||
| return 20 + Math.max(1, subtitleLines.size()) * LINE_SPACING; | ||
| } | ||
|
|
||
| @Override | ||
| public Visibility render(GuiGraphics graphics, ToastComponent toastComponent, long timeSinceLastVisible) { | ||
| if (changed) { | ||
| lastChangedAt = timeSinceLastVisible; | ||
| changed = false; | ||
| } | ||
|
|
||
| graphics.blitSprite(BACKGROUND_SPRITE, 0, 0, width(), height()); | ||
| graphics.renderItem(icon, ICON_LEFT, 8); | ||
|
|
||
| var font = toastComponent.getMinecraft().font; | ||
| graphics.drawString(font, title, TEXT_LEFT, 7, 0xFFFFFF, false); | ||
| for (int i = 0; i < subtitleLines.size(); i++) { | ||
| graphics.drawString(font, subtitleLines.get(i), TEXT_LEFT, 18 + i * LINE_SPACING, 0xAAAAAA, false); | ||
| } | ||
|
|
||
| return timeSinceLastVisible - lastChangedAt < (long) (DISPLAY_MILLIS * toastComponent.getNotificationDisplayTimeMultiplier()) | ||
| ? Visibility.SHOW | ||
| : Visibility.HIDE; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getToken() { | ||
| return token; | ||
| } | ||
|
|
||
| } |
131 changes: 131 additions & 0 deletions
131
src/main/java/org/cyclops/integratedterminals/gametest/GameTestCraftingJobNotify.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,131 @@ | ||
| package org.cyclops.integratedterminals.gametest; | ||
|
|
||
| import com.mojang.authlib.GameProfile; | ||
| 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.server.level.ClientInformation; | ||
| import net.minecraft.server.level.ServerPlayer; | ||
| 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.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.commoncapabilities.api.capability.recipehandler.IRecipeDefinition; | ||
| import org.cyclops.integratedcrafting.api.crafting.CraftingJob; | ||
| import org.cyclops.integratedcrafting.api.network.ICraftingNetwork; | ||
| import org.cyclops.integratedcrafting.core.CraftingHelpers; | ||
| import org.cyclops.integratedcrafting.gametest.GameTestHelpersIntegratedCrafting; | ||
| 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 org.cyclops.integratedterminals.Reference; | ||
| import org.cyclops.integratedterminals.api.terminalstorage.crafting.ITerminalCraftingPlan; | ||
| import org.cyclops.integratedterminals.modcompat.integratedcrafting.TerminalCraftingOptionRecipeDefinition; | ||
| import org.cyclops.integratedterminals.modcompat.integratedcrafting.TerminalStorageTabIngredientCraftingHandlerCraftingNetwork; | ||
|
|
||
| import java.util.Iterator; | ||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * Game tests for requesting a notification when a crafting job started from a terminal is completed. | ||
| * @author rubensworks | ||
| */ | ||
| @GameTestHolder(Reference.MOD_ID) | ||
| @PrefixGameTestTemplate(false) | ||
| public class GameTestCraftingJobNotify { | ||
|
|
||
| public static final BlockPos POS = BlockPos.ZERO.offset(2, 0, 2); | ||
|
|
||
| /** | ||
| * A job started with the notify option enabled carries the initiator and the notify flag. | ||
| */ | ||
| @GameTest(template = "empty10", templateNamespace = Reference.MOD_ID, timeoutTicks = 2000) | ||
| public void testStartCraftingJobWithNotify(GameTestHelper helper) { | ||
| testStartCraftingJob(helper, true); | ||
| } | ||
|
|
||
| /** | ||
| * A job started with the notify option disabled carries the initiator, but not the notify flag. | ||
| */ | ||
| @GameTest(template = "empty10", templateNamespace = Reference.MOD_ID, timeoutTicks = 2000) | ||
| public void testStartCraftingJobWithoutNotify(GameTestHelper helper) { | ||
| testStartCraftingJob(helper, false); | ||
| } | ||
|
|
||
| private void testStartCraftingJob(GameTestHelper helper, boolean notifyOnCompletion) { | ||
| prepareNetwork(helper); | ||
|
|
||
| // This player is deliberately not added to the player list, | ||
| // so that no notification packet is sent for the completed job. | ||
| ServerPlayer player = new ServerPlayer(helper.getLevel().getServer(), helper.getLevel(), | ||
| new GameProfile(UUID.randomUUID(), "test-mock-player"), ClientInformation.createDefault()); | ||
|
|
||
| helper.startSequence() | ||
| .thenIdle(20) | ||
| .thenExecute(() -> { | ||
| INetwork network = getNetwork(helper); | ||
| int channel = IPositionedAddonsNetworkIngredients.DEFAULT_CHANNEL; | ||
| TerminalStorageTabIngredientCraftingHandlerCraftingNetwork handler = | ||
| new TerminalStorageTabIngredientCraftingHandlerCraftingNetwork(); | ||
|
|
||
| ITerminalCraftingPlan<Integer> craftingPlan = handler.calculateCraftingPlan(network, channel, | ||
| new TerminalCraftingOptionRecipeDefinition<>(IngredientComponents.ITEMSTACK, | ||
| getChestRecipe(helper, network, channel)), 1); | ||
| try { | ||
| handler.startCraftingJob(network, channel, craftingPlan, player, notifyOnCompletion); | ||
| } catch (Exception e) { | ||
| helper.fail("The crafting job could not be started: " + e.getMessage()); | ||
| } | ||
|
|
||
| CraftingJob craftingJob = getSingleCraftingJob(helper, network, channel); | ||
| helper.assertTrue(player.getUUID().toString().equals(craftingJob.getInitiatorUuid()), | ||
| "The started job did not carry the initiator"); | ||
| helper.assertTrue(craftingJob.isNotifyInitiator() == notifyOnCompletion, | ||
| "The started job did not carry the expected notify flag"); | ||
| }) | ||
| .thenSucceed(); | ||
| } | ||
|
|
||
| private static void prepareNetwork(GameTestHelper helper) { | ||
| GameTestHelpersIntegratedCrafting.INetworkPositions<PartTypeInterfaceCrafting.State> positions = | ||
| GameTestHelpersIntegratedCrafting.createBasicNetwork(helper, POS); | ||
|
|
||
| ChestBlockEntity chest = helper.getBlockEntity(POS.east()); | ||
| chest.setItem(0, new ItemStack(Items.OAK_PLANKS, 64)); | ||
|
|
||
| positions.interfaceRecipeAdders().get(0).accept(Triple.of(0, RecipeType.CRAFTING, | ||
| ResourceLocation.fromNamespaceAndPath("minecraft", "chest"))); | ||
| } | ||
|
|
||
| private static IRecipeDefinition getChestRecipe(GameTestHelper helper, INetwork network, int channel) { | ||
| Iterator<IRecipeDefinition> recipes = CraftingHelpers.getCraftingNetworkChecked(network) | ||
| .getRecipeIndex(channel) | ||
| .getRecipes(IngredientComponents.ITEMSTACK, new ItemStack(Items.CHEST), ItemMatch.ITEM); | ||
| if (!recipes.hasNext()) { | ||
| helper.fail("No chest recipe was available in the network"); | ||
| } | ||
| return recipes.next(); | ||
| } | ||
|
|
||
| private static CraftingJob getSingleCraftingJob(GameTestHelper helper, INetwork network, int channel) { | ||
| ICraftingNetwork craftingNetwork = CraftingHelpers.getCraftingNetworkChecked(network); | ||
| Iterator<CraftingJob> craftingJobs = craftingNetwork.getCraftingJobs(channel); | ||
| if (!craftingJobs.hasNext()) { | ||
| helper.fail("No crafting job was scheduled"); | ||
| } | ||
| return craftingJobs.next(); | ||
| } | ||
|
|
||
| private static INetwork getNetwork(GameTestHelper helper) { | ||
| return NetworkHelpers.getNetwork(helper.getLevel(), helper.absolutePos(POS), null) | ||
| .orElseThrow(() -> new IllegalStateException("Could not find a network")); | ||
| } | ||
|
|
||
| } |
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
Oops, something went wrong.
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.