Skip to content

Commit c1cccdf

Browse files
committed
Fix: modify craft result when assembling, supports quick crafting
1 parent f5620f4 commit c1cccdf

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

src/main/java/com/lovetropics/minigames/common/content/crafting_bee/CraftingBeeBehavior.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import net.minecraft.world.entity.player.Player;
4646
import net.minecraft.world.item.ItemStack;
4747
import net.minecraft.world.item.component.ItemContainerContents;
48+
import net.minecraft.world.item.crafting.CraftingInput;
49+
import net.minecraft.world.item.crafting.CraftingRecipe;
4850
import net.minecraft.world.item.crafting.Ingredient;
4951
import net.minecraft.world.level.block.Blocks;
5052
import net.minecraft.world.phys.BlockHitResult;
@@ -125,6 +127,7 @@ public void register(IGamePhase game, EventRegistrar events) throws GameExceptio
125127
events.listen(GamePhaseEvents.DESTROY, () -> timerBars.values().forEach(b -> b.bar().close()));
126128
events.listen(GamePhaseEvents.STOP, reason -> timerBars.values().forEach(b -> b.bar().close()));
127129

130+
events.listen(GamePlayerEvents.CRAFT_RESULT, this::modifyCraftResult);
128131
events.listen(GamePlayerEvents.CRAFT, this::onCraft);
129132
events.listen(GamePlayerEvents.USE_BLOCK, this::useBlock);
130133

@@ -197,25 +200,27 @@ private Stream<ItemStack> singleDecomposition(Ingredient ingredient) {
197200
return Stream.of(ingredient.getItems()[0]);
198201
}
199202

203+
private ItemStack modifyCraftResult(ServerPlayer player, ItemStack result, CraftingInput craftingInput, CraftingRecipe recipe) {
204+
ItemContainerContents usedItems = ItemContainerContents.fromItems(craftingInput.items().stream()
205+
.filter(stack -> !stack.isEmpty())
206+
.map(stack -> stack.copyWithCount(1))
207+
// .peek(s -> s.remove(CraftingBee.CRAFTED_USING)) // TODO - should we keep this?
208+
.sorted(Comparator.comparing(s -> s.getItemHolder().getRegisteredName()))
209+
.toList());
210+
result.set(CraftingBee.CRAFTED_USING, new CraftedUsing(
211+
result.getCount(),
212+
usedItems
213+
));
214+
return result;
215+
}
216+
200217
private void onCraft(Player player, ItemStack crafted, Container container) {
201218
var team = teams.getTeamForPlayer(player);
202219
if (team == null || done) return;
203220

204221
var teamTasks = tasks.get(team);
205222
var task = teamTasks.stream().filter(c -> ItemStack.isSameItemSameComponents(crafted, c.output)).findFirst().orElse(null);
206223

207-
ItemContainerContents usedItems = ItemContainerContents.fromItems(IntStream.range(0, container.getContainerSize())
208-
.mapToObj(container::getItem)
209-
.filter(Predicate.not(ItemStack::isEmpty))
210-
.map(s -> s.copyWithCount(1))
211-
// .peek(s -> s.remove(CraftingBee.CRAFTED_USING)) // TODO - should we keep this?
212-
.sorted(Comparator.comparing(s -> s.getItemHolder().getRegisteredName()))
213-
.toList());
214-
crafted.set(CraftingBee.CRAFTED_USING, new CraftedUsing(
215-
crafted.getCount(),
216-
usedItems
217-
));
218-
219224
if (task == null || task.done) return;
220225

221226
task.done = true;

src/main/java/com/lovetropics/minigames/common/core/game/behavior/event/GamePlayerEvents.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import net.minecraft.world.entity.item.ItemEntity;
1616
import net.minecraft.world.entity.player.Player;
1717
import net.minecraft.world.item.ItemStack;
18+
import net.minecraft.world.item.crafting.CraftingInput;
19+
import net.minecraft.world.item.crafting.CraftingRecipe;
1820
import net.minecraft.world.level.block.state.BlockState;
1921
import net.minecraft.world.phys.BlockHitResult;
2022

@@ -220,6 +222,13 @@ public final class GamePlayerEvents {
220222
}
221223
});
222224

225+
public static final GameEventType<CraftResult> CRAFT_RESULT = GameEventType.create(CraftResult.class, listeners -> (player, crafted, input, recipe) -> {
226+
for (CraftResult listener : listeners) {
227+
crafted = listener.modifyResult(player, crafted, input, recipe);
228+
}
229+
return crafted;
230+
});
231+
223232
private GamePlayerEvents() {
224233
}
225234

@@ -310,4 +319,8 @@ public interface Return {
310319
public interface Craft {
311320
void onCraft(Player player, ItemStack crafted, Container craftingContainer);
312321
}
322+
323+
public interface CraftResult {
324+
ItemStack modifyResult(ServerPlayer player, ItemStack crafted, CraftingInput input, CraftingRecipe recipe);
325+
}
313326
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.lovetropics.minigames.mixin;
2+
3+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
4+
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
5+
import com.llamalad7.mixinextras.sugar.Local;
6+
import com.lovetropics.minigames.common.core.game.IGameManager;
7+
import com.lovetropics.minigames.common.core.game.IGamePhase;
8+
import com.lovetropics.minigames.common.core.game.behavior.event.GamePlayerEvents;
9+
import net.minecraft.core.HolderLookup;
10+
import net.minecraft.server.level.ServerPlayer;
11+
import net.minecraft.world.inventory.CraftingMenu;
12+
import net.minecraft.world.item.ItemStack;
13+
import net.minecraft.world.item.crafting.CraftingInput;
14+
import net.minecraft.world.item.crafting.CraftingRecipe;
15+
import net.minecraft.world.item.crafting.RecipeInput;
16+
import org.spongepowered.asm.mixin.Mixin;
17+
import org.spongepowered.asm.mixin.injection.At;
18+
19+
@Mixin(CraftingMenu.class)
20+
public class CraftingMenuMixin {
21+
@WrapOperation(method = "slotChangedCraftingGrid", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/crafting/CraftingRecipe;assemble(Lnet/minecraft/world/item/crafting/RecipeInput;Lnet/minecraft/core/HolderLookup$Provider;)Lnet/minecraft/world/item/ItemStack;"))
22+
private static ItemStack modifyResult(CraftingRecipe recipe, RecipeInput input, HolderLookup.Provider provider, Operation<ItemStack> original, @Local ServerPlayer player) {
23+
ItemStack originalResult = original.call(recipe, input, provider);
24+
IGamePhase game = IGameManager.get().getGamePhaseFor(player);
25+
if (game != null) {
26+
return game.invoker(GamePlayerEvents.CRAFT_RESULT).modifyResult(player, originalResult, (CraftingInput) input, recipe);
27+
}
28+
return originalResult;
29+
}
30+
}

src/main/resources/ltminigames.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"BaseSpawnerAccessor",
88
"ChunkStorageMixin",
99
"CommonHooksMixin",
10+
"CraftingMenuMixin",
1011
"DataCommandsMixin",
1112
"EntityMixin",
1213
"EntityTypeMixin",

0 commit comments

Comments
 (0)