Skip to content

Commit

Permalink
Fix: modify craft result when assembling, supports quick crafting
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Nov 15, 2024
1 parent f5620f4 commit c1cccdf
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.ItemContainerContents;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.phys.BlockHitResult;
Expand Down Expand Up @@ -125,6 +127,7 @@ public void register(IGamePhase game, EventRegistrar events) throws GameExceptio
events.listen(GamePhaseEvents.DESTROY, () -> timerBars.values().forEach(b -> b.bar().close()));
events.listen(GamePhaseEvents.STOP, reason -> timerBars.values().forEach(b -> b.bar().close()));

events.listen(GamePlayerEvents.CRAFT_RESULT, this::modifyCraftResult);
events.listen(GamePlayerEvents.CRAFT, this::onCraft);
events.listen(GamePlayerEvents.USE_BLOCK, this::useBlock);

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

private ItemStack modifyCraftResult(ServerPlayer player, ItemStack result, CraftingInput craftingInput, CraftingRecipe recipe) {
ItemContainerContents usedItems = ItemContainerContents.fromItems(craftingInput.items().stream()
.filter(stack -> !stack.isEmpty())
.map(stack -> stack.copyWithCount(1))
// .peek(s -> s.remove(CraftingBee.CRAFTED_USING)) // TODO - should we keep this?
.sorted(Comparator.comparing(s -> s.getItemHolder().getRegisteredName()))
.toList());
result.set(CraftingBee.CRAFTED_USING, new CraftedUsing(
result.getCount(),
usedItems
));
return result;
}

private void onCraft(Player player, ItemStack crafted, Container container) {
var team = teams.getTeamForPlayer(player);
if (team == null || done) return;

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

ItemContainerContents usedItems = ItemContainerContents.fromItems(IntStream.range(0, container.getContainerSize())
.mapToObj(container::getItem)
.filter(Predicate.not(ItemStack::isEmpty))
.map(s -> s.copyWithCount(1))
// .peek(s -> s.remove(CraftingBee.CRAFTED_USING)) // TODO - should we keep this?
.sorted(Comparator.comparing(s -> s.getItemHolder().getRegisteredName()))
.toList());
crafted.set(CraftingBee.CRAFTED_USING, new CraftedUsing(
crafted.getCount(),
usedItems
));

if (task == null || task.done) return;

task.done = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;

Expand Down Expand Up @@ -220,6 +222,13 @@ public final class GamePlayerEvents {
}
});

public static final GameEventType<CraftResult> CRAFT_RESULT = GameEventType.create(CraftResult.class, listeners -> (player, crafted, input, recipe) -> {
for (CraftResult listener : listeners) {
crafted = listener.modifyResult(player, crafted, input, recipe);
}
return crafted;
});

private GamePlayerEvents() {
}

Expand Down Expand Up @@ -310,4 +319,8 @@ public interface Return {
public interface Craft {
void onCraft(Player player, ItemStack crafted, Container craftingContainer);
}

public interface CraftResult {
ItemStack modifyResult(ServerPlayer player, ItemStack crafted, CraftingInput input, CraftingRecipe recipe);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.lovetropics.minigames.mixin;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import com.lovetropics.minigames.common.core.game.IGameManager;
import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.event.GamePlayerEvents;
import net.minecraft.core.HolderLookup;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.CraftingMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.RecipeInput;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(CraftingMenu.class)
public class CraftingMenuMixin {
@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;"))
private static ItemStack modifyResult(CraftingRecipe recipe, RecipeInput input, HolderLookup.Provider provider, Operation<ItemStack> original, @Local ServerPlayer player) {
ItemStack originalResult = original.call(recipe, input, provider);
IGamePhase game = IGameManager.get().getGamePhaseFor(player);
if (game != null) {
return game.invoker(GamePlayerEvents.CRAFT_RESULT).modifyResult(player, originalResult, (CraftingInput) input, recipe);
}
return originalResult;
}
}
1 change: 1 addition & 0 deletions src/main/resources/ltminigames.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"BaseSpawnerAccessor",
"ChunkStorageMixin",
"CommonHooksMixin",
"CraftingMenuMixin",
"DataCommandsMixin",
"EntityMixin",
"EntityTypeMixin",
Expand Down

0 comments on commit c1cccdf

Please sign in to comment.