Search in sources :

Example 1 with IHasClickToggle

use of com.lothrazar.cyclic.api.IHasClickToggle in project Cyclic by Lothrazar.

the class CharmUtil method getIfEnabled.

public static ItemStack getIfEnabled(Player player, Item match) {
    Triple<String, Integer, ItemStack> found = isCurioOrInventory(player, match);
    ItemStack stack = found == null ? ItemStack.EMPTY : found.getRight();
    if (stack.getItem() instanceof IHasClickToggle) {
        IHasClickToggle testMe = (IHasClickToggle) stack.getItem();
        if (testMe.isOn(stack) == false) {
            // found but player turned it off so dont use it
            return ItemStack.EMPTY;
        }
    }
    return stack;
}
Also used : IHasClickToggle(com.lothrazar.cyclic.api.IHasClickToggle) ItemStack(net.minecraft.world.item.ItemStack)

Example 2 with IHasClickToggle

use of com.lothrazar.cyclic.api.IHasClickToggle in project Cyclic by Lothrazar.

the class PacketItemToggle method handle.

public static void handle(PacketItemToggle message, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> {
        ServerPlayer player = ctx.get().getSender();
        if (player.containerMenu == null) {
            return;
        }
        int scount = player.containerMenu.slots.size();
        // this is an edge case but it DID happen: put charmin your hotbar and then open a creative inventory tab. avoid index OOB
        if (message.slot >= scount) {
            // will NOT work in creative mode. slots are messed up
            return;
        }
        Slot slotObj = player.containerMenu.getSlot(message.slot);
        if (slotObj != null && !slotObj.getItem().isEmpty()) {
            ItemStack maybeCharm = slotObj.getItem();
            if (maybeCharm.getItem() instanceof IHasClickToggle) {
                // example: is a charm or something
                IHasClickToggle c = (IHasClickToggle) maybeCharm.getItem();
                c.toggle(player, maybeCharm);
            }
        }
    });
    message.done(ctx);
}
Also used : IHasClickToggle(com.lothrazar.cyclic.api.IHasClickToggle) ServerPlayer(net.minecraft.server.level.ServerPlayer) Slot(net.minecraft.world.inventory.Slot) ItemStack(net.minecraft.world.item.ItemStack)

Example 3 with IHasClickToggle

use of com.lothrazar.cyclic.api.IHasClickToggle in project Cyclic by Lothrazar.

the class ClientInputEvents method onMouseEvent.

@SubscribeEvent(priority = EventPriority.HIGH)
public void onMouseEvent(ScreenEvent.MouseClickedEvent.Pre event) {
    if (event.getScreen() == null || !(event.getScreen() instanceof AbstractContainerScreen<?>)) {
        return;
    }
    AbstractContainerScreen<?> gui = (AbstractContainerScreen<?>) event.getScreen();
    boolean rightClickDown = event.getButton() == 1;
    try {
        if (rightClickDown && gui.getSlotUnderMouse() != null) {
            Slot slotHit = gui.getSlotUnderMouse();
            if (!slotHit.getItem().isEmpty()) {
                ItemStack maybeCharm = slotHit.getItem();
                if (maybeCharm.getItem() instanceof IHasClickToggle) {
                    PacketRegistry.INSTANCE.sendToServer(new PacketItemToggle(slotHit.index));
                    event.setCanceled(true);
                // UtilSound.playSound(ModCyclic.proxy.getClientPlayer(), SoundEvents.UI_BUTTON_CLICK);
                } else if (maybeCharm.getItem() instanceof ItemStorageBag) {
                    PacketRegistry.INSTANCE.sendToServer(new PacketItemGui(slotHit.index));
                    event.setCanceled(true);
                }
            }
        }
    } catch (Exception e) {
        // array out of bounds, or we are in a strange third party GUI that doesnt have slots like this
        // EXAMPLE:  mod.chiselsandbits.bitbag.BagGui
        ModCyclic.LOGGER.error("click error", e);
    // so this fixes ithttps://github.com/PrinceOfAmber/Cyclic/issues/410
    }
}
Also used : PacketItemToggle(com.lothrazar.cyclic.net.PacketItemToggle) IHasClickToggle(com.lothrazar.cyclic.api.IHasClickToggle) AbstractContainerScreen(net.minecraft.client.gui.screens.inventory.AbstractContainerScreen) PacketItemGui(com.lothrazar.cyclic.net.PacketItemGui) Slot(net.minecraft.world.inventory.Slot) ItemStack(net.minecraft.world.item.ItemStack) ItemStorageBag(com.lothrazar.cyclic.item.storagebag.ItemStorageBag) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

IHasClickToggle (com.lothrazar.cyclic.api.IHasClickToggle)3 ItemStack (net.minecraft.world.item.ItemStack)3 Slot (net.minecraft.world.inventory.Slot)2 ItemStorageBag (com.lothrazar.cyclic.item.storagebag.ItemStorageBag)1 PacketItemGui (com.lothrazar.cyclic.net.PacketItemGui)1 PacketItemToggle (com.lothrazar.cyclic.net.PacketItemToggle)1 AbstractContainerScreen (net.minecraft.client.gui.screens.inventory.AbstractContainerScreen)1 ServerPlayer (net.minecraft.server.level.ServerPlayer)1 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)1