diff --git a/src/main/java/de/dafuqs/spectrum/blocks/present/PresentBlock.java b/src/main/java/de/dafuqs/spectrum/blocks/present/PresentBlock.java index e11c4b428e..974a8e1b5c 100644 --- a/src/main/java/de/dafuqs/spectrum/blocks/present/PresentBlock.java +++ b/src/main/java/de/dafuqs/spectrum/blocks/present/PresentBlock.java @@ -3,6 +3,7 @@ import com.mojang.serialization.*; import de.dafuqs.spectrum.api.energy.color.*; import de.dafuqs.spectrum.api.item.*; +import de.dafuqs.spectrum.components.*; import de.dafuqs.spectrum.helpers.*; import de.dafuqs.spectrum.networking.s2c_payloads.*; import de.dafuqs.spectrum.particle.effect.*; @@ -17,6 +18,7 @@ import net.minecraft.world.entity.*; import net.minecraft.world.entity.player.*; import net.minecraft.world.item.*; +import net.minecraft.world.item.context.*; import net.minecraft.world.level.*; import net.minecraft.world.level.block.*; import net.minecraft.world.level.block.entity.*; @@ -106,9 +108,8 @@ public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { @Override public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) { BlockEntity blockEntity = world.getBlockEntity(pos); - world.setBlockAndUpdate(pos, state.setValue(PresentBlock.VARIANT, PresentBlockItem.getWrapData(itemStack).variant())); if (blockEntity instanceof PresentBlockEntity presentBlockEntity) { - presentBlockEntity.setPresent(itemStack); + presentBlockEntity.setPresent(itemStack.copyWithCount(1)); } } @@ -220,7 +221,16 @@ private static void spawnParticlesClient(Level world, BlockPos pos, int color, i world.addParticle(particleEffect, posX, posY, posZ, randX, randY, randZ); } } - + + @Override + public @Nullable BlockState getStateForPlacement(BlockPlaceContext context) { + WrappedPresentComponent presentComponent = PresentBlockItem.getWrapData(context.getItemInHand()); + if (presentComponent != null) { + return this.defaultBlockState().setValue(VARIANT, presentComponent.variant()); + } + return super.getStateForPlacement(context); + } + @Override public @Nullable BlockEntity newBlockEntity(BlockPos pos, BlockState state) { return new PresentBlockEntity(pos, state); diff --git a/src/main/java/de/dafuqs/spectrum/blocks/present/PresentBlockItem.java b/src/main/java/de/dafuqs/spectrum/blocks/present/PresentBlockItem.java index d3d5b9ad33..223ffb7ccf 100644 --- a/src/main/java/de/dafuqs/spectrum/blocks/present/PresentBlockItem.java +++ b/src/main/java/de/dafuqs/spectrum/blocks/present/PresentBlockItem.java @@ -151,6 +151,11 @@ public InteractionResultHolder use(Level world, Player user, Interact return InteractionResultHolder.pass(itemStack); } + @Override + public int getMaxStackSize(ItemStack stack) { + return stack.has(SpectrumDataComponentTypes.WRAPPED_PRESENT) || !isEmpty(stack) ? 1 : super.getMaxStackSize(stack); + } + // CraftingInventory does not recalculate the recipe after inputting / retrieving stacks from the present. // The recipes output will still hold the original present data from when it was put into the crafting grid // If the player then puts / receives items from the present they are able to duplicate items diff --git a/src/main/java/de/dafuqs/spectrum/mixin/InventoryMenuMixin.java b/src/main/java/de/dafuqs/spectrum/mixin/InventoryMenuMixin.java new file mode 100644 index 0000000000..48377b6184 --- /dev/null +++ b/src/main/java/de/dafuqs/spectrum/mixin/InventoryMenuMixin.java @@ -0,0 +1,31 @@ +package de.dafuqs.spectrum.mixin; + +import com.llamalad7.mixinextras.injector.wrapoperation.*; +import de.dafuqs.spectrum.blocks.present.*; +import net.minecraft.world.entity.player.*; +import net.minecraft.world.inventory.*; +import net.minecraft.world.item.*; +import org.spongepowered.asm.mixin.*; +import org.spongepowered.asm.mixin.injection.*; + +@Mixin(InventoryMenu.class) +public abstract class InventoryMenuMixin { + @Shadow + @Final + private Player owner; + + @WrapOperation( + method = "quickMoveStack", + slice = @Slice( + from = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getEquipmentSlotForItem(Lnet/minecraft/world/item/ItemStack;)Lnet/minecraft/world/entity/EquipmentSlot;"), + to = @At(value = "INVOKE", target = "Lnet/minecraft/world/inventory/Slot;onQuickCraft(Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/item/ItemStack;)V") + ), + at = @At(value = "INVOKE", target = "Lnet/minecraft/world/inventory/InventoryMenu;moveItemStackTo(Lnet/minecraft/world/item/ItemStack;IIZ)Z") + ) + private boolean spectrum$triggerOnCraftedBeforeQuickMove(InventoryMenu instance, ItemStack itemStack, int startIndex, int endIndex, boolean reverseDirection, Operation original) { + if (itemStack.getItem() instanceof PresentBlockItem) { + itemStack.getItem().onCraftedBy(itemStack, this.owner.level(), this.owner); + } + return original.call(instance, itemStack, startIndex, endIndex, reverseDirection); + } +} diff --git a/src/main/java/de/dafuqs/spectrum/recipe/crafting/dynamic/WrapPresentRecipe.java b/src/main/java/de/dafuqs/spectrum/recipe/crafting/dynamic/WrapPresentRecipe.java index 803af48657..54f8636fa4 100644 --- a/src/main/java/de/dafuqs/spectrum/recipe/crafting/dynamic/WrapPresentRecipe.java +++ b/src/main/java/de/dafuqs/spectrum/recipe/crafting/dynamic/WrapPresentRecipe.java @@ -66,7 +66,7 @@ public ItemStack assemble(CraftingInput input, HolderLookup.Provider registryLoo for (int j = 0; j < input.size(); ++j) { ItemStack stack = input.getItem(j); if (stack.getItem() instanceof PresentBlockItem) { - presentStack = stack.copy(); + presentStack = stack.copyWithCount(1); } else if (stack.getItem() instanceof PigmentItem pigmentItem) { InkColor color = pigmentItem.getInkColor(); if (colors.containsKey(color)) { @@ -101,14 +101,14 @@ public ItemStack assemble(CraftingInput input, HolderLookup.Provider registryLoo return PresentBlock.WrappingPaper.PURPLE; } else if (item == Items.CAKE) { return PresentBlock.WrappingPaper.CAKE; - } else if (stack.is(ItemTags.FLOWERS)) { - return PresentBlock.WrappingPaper.STRIPED; + } else if (item == Items.SPORE_BLOSSOM) { + return PresentBlock.WrappingPaper.PRIDE; } else if (item == Items.FIREWORK_STAR) { return PresentBlock.WrappingPaper.STARRY; } else if (item == Items.SNOWBALL) { return PresentBlock.WrappingPaper.WINTER; - } else if (item == Items.SPORE_BLOSSOM) { - return PresentBlock.WrappingPaper.PRIDE; + } else if (stack.is(ItemTags.FLOWERS)) { + return PresentBlock.WrappingPaper.STRIPED; } return null; } diff --git a/src/main/resources/spectrum.mixins.json b/src/main/resources/spectrum.mixins.json index 76d767e19d..a67e8ac815 100644 --- a/src/main/resources/spectrum.mixins.json +++ b/src/main/resources/spectrum.mixins.json @@ -35,6 +35,7 @@ "GeodesGenerateWithGemstoneOresMixin", "GlassBottleItemMixin", "InfestedBlockMixin", + "InventoryMenuMixin", "ItemEntityMixin", "ItemStackMixin", "KilledByPlayerLootConditionMixin",