Skip to content

Commit

Permalink
added ItemMenuInteractionEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
CammiePone committed Nov 2, 2024
1 parent 78d430e commit 3589206
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.upcraft.sparkweave.api.event;

import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ClickAction;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

public interface ItemMenuInteractionEvent {
Event<ItemMenuInteractionEvent> EVENT = Event.create(ItemMenuInteractionEvent.class, listeners -> (menu, player, level, clickAction, slot, slotStack, cursorStack) -> {
for(ItemMenuInteractionEvent listener : listeners) {
boolean cancel = listener.interactWithItemInMenu(menu, player, level, clickAction, slot, slotStack, cursorStack);

if(cancel)
return true;
}

return false;
});

/**
* @param menu The menu the player is in
* @param player The player doing the interaction
* @param clickAction The mouse button being clicked
* @param slot The slot the item being interacted with is in
* @param slotStack The {@link ItemStack} in the menu that's being interacted with
* @param cursorStack The {@link ItemStack} currently being held by the user's cursor
* @return Whether to cancel the normal interaction that would happen
*/
boolean interactWithItemInMenu(AbstractContainerMenu menu, Player player, Level level, ClickAction clickAction, Slot slot, ItemStack slotStack, ItemStack cursorStack);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.upcraft.sparkweave.mixin;

import net.minecraft.world.inventory.AbstractContainerMenu;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(AbstractContainerMenu.class)
public class AbstractContainerMenuMixin {
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package dev.upcraft.sparkweave.testmod;

import dev.upcraft.sparkweave.api.entrypoint.MainEntryPoint;
import dev.upcraft.sparkweave.api.event.ItemMenuInteractionEvent;
import dev.upcraft.sparkweave.api.platform.ModContainer;
import dev.upcraft.sparkweave.api.platform.services.RegistryService;
import dev.upcraft.sparkweave.testmod.init.TestCreativeTabs;
import dev.upcraft.sparkweave.testmod.init.TestItems;
import dev.upcraft.sparkweave.testmod.init.TestStatusEffects;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.inventory.ChestMenu;
import net.minecraft.world.inventory.ClickAction;
import net.minecraft.world.item.Items;

public class SparkweaveTestmod implements MainEntryPoint {

Expand All @@ -19,6 +26,16 @@ public void onInitialize(ModContainer mod) {
TestItems.ITEMS.accept(registryService);
TestCreativeTabs.TABS.accept(registryService);
TestStatusEffects.STATUS_EFFECTS.accept(registryService);

ItemMenuInteractionEvent.EVENT.register((menu, player, level, clickAction, slot, slotStack, cursorStack) -> {
if(level.isClientSide() && menu instanceof ChestMenu && clickAction == ClickAction.SECONDARY && slotStack.is(Items.DEEPSLATE_COAL_ORE) && cursorStack.is(Items.DEEPSLATE_EMERALD_ORE) && player.isCrouching()) {
player.displayClientMessage(Component.literal("Uh oh stinky"), false);
level.playSound(null, player.blockPosition(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS);
return true;
}

return false;
});
}

public static ResourceLocation id(String path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.upcraft.sparkweave.fabric.mixin;

import dev.upcraft.sparkweave.api.event.ItemMenuInteractionEvent;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ClickAction;
import net.minecraft.world.inventory.ClickType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(AbstractContainerMenu.class)
public class AbstractContainerMenuMixin {
@Inject(
method = "doClick",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;updateTutorialInventoryAction(Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/inventory/ClickAction;)V"),
locals = LocalCapture.CAPTURE_FAILSOFT, cancellable = true
)
private void handleItemClickEvent(int slotId, int button, ClickType clickType, Player player, CallbackInfo info, ClickAction clickAction, Slot slot, ItemStack slotStack, ItemStack cursorStack) {
if(ItemMenuInteractionEvent.EVENT.invoker().interactWithItemInMenu((AbstractContainerMenu) (Object) this, player, player.level(), clickAction, slot, slotStack, cursorStack))
info.cancel();
}
}
1 change: 1 addition & 0 deletions Fabric/src/main/resources/sparkweave.fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"refmap": "${mod_id}.refmap.json",
"compatibilityLevel": "JAVA_21",
"mixins": [
"AbstractContainerMenuMixin",
"impl.ArgumentTypeInfosAccessor",
"impl.registry.MappedRegistryMixin"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.upcraft.sparkweave.neoforge.mixin;

import dev.upcraft.sparkweave.api.event.ItemMenuInteractionEvent;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ClickAction;
import net.minecraft.world.inventory.ClickType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@SuppressWarnings("InvalidInjectorMethodSignature")
@Mixin(AbstractContainerMenu.class)
public class AbstractContainerMenuMixin {
@Inject(
method = "doClick",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;updateTutorialInventoryAction(Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/inventory/ClickAction;)V"),
locals = LocalCapture.CAPTURE_FAILSOFT, cancellable = true
)
private void handleItemClickEvent(int slotId, int button, ClickType clickType, Player player, CallbackInfo info, Inventory inventory, ClickAction clickAction, Slot slot, ItemStack slotStack, ItemStack cursorStack) {
if(ItemMenuInteractionEvent.EVENT.invoker().interactWithItemInMenu((AbstractContainerMenu) (Object) this, player, player.level(), clickAction, slot, slotStack, cursorStack))
info.cancel();
}
}
3 changes: 2 additions & 1 deletion NeoForge/src/main/resources/sparkweave.neoforge.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"plugin": "dev.upcraft.sparkweave.SparkweaveMixinConfigPlugin",
"compatibilityLevel": "JAVA_21",
"mixins": [
"AbstractContainerMenuMixin",
"internal.DeferredHolderMixin",
"internal.DeferredRegisterMixin",
"internal.RegisterMenuScreensEventMixin"
Expand All @@ -14,5 +15,5 @@
],
"injectors": {
"defaultRequire": 1
}
}
}

0 comments on commit 3589206

Please sign in to comment.