aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/shedaniel/mixins/MixinGuiContainerCreative.java
blob: 948a5a2748de904f356d2bb67a1724f2d7e07302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package me.shedaniel.mixins;

import me.shedaniel.listenerdefinitions.GuiCickListener;
import me.shedaniel.listenerdefinitions.GuiKeyDown;
import net.minecraft.client.gui.inventory.GuiContainerCreative;
import net.minecraft.client.renderer.InventoryEffectRenderer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemGroup;
import org.dimdev.riftloader.RiftLoader;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(GuiContainerCreative.class)
public abstract class MixinGuiContainerCreative extends InventoryEffectRenderer {
    
    @Shadow
    public abstract int getSelectedTabIndex();
    
    @Shadow
    protected abstract boolean needsScrollBars();
    
    public MixinGuiContainerCreative(Container container_1) {
        super(container_1);
    }
    
    @Inject(method = "keyPressed(III)Z", at = @At("HEAD"), cancellable = true)
    public void keyPressed(int p_keyPressed_1_, int p_keyPressed_2_, int p_keyPressed_3_, CallbackInfoReturnable<Boolean> ci) {
        boolean handled = false;
        if (getSelectedTabIndex() != ItemGroup.SEARCH.getIndex()) {
            if (getSelectedTabIndex() == ItemGroup.INVENTORY.getIndex()) {
                for(GuiKeyDown listener : RiftLoader.instance.getListeners(GuiKeyDown.class))
                    if (listener.keyDown(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_)) {
                        ci.setReturnValue(true);
                        handled = true;
                    }
            }
        }
        if (handled)
            ci.cancel();
    }
    
    @Inject(method = "mouseScrolled(D)Z", at = @At("HEAD"), cancellable = true)
    public void mouseScrolled(double p_mouseScrolled_1_, CallbackInfoReturnable<Boolean> ci) {
        if (!this.needsScrollBars()) {
            ci.setReturnValue(super.mouseScrolled(p_mouseScrolled_1_));
            ci.cancel();
        }
    }
    
    @Inject(method = "mouseClicked", at = @At("HEAD"), cancellable = true)
    private void onMouseClicked(double p_mouseClicked_1_, double p_mouseClicked_3_, int p_mouseClicked_5_, CallbackInfoReturnable<Boolean> ci) {
        if (getSelectedTabIndex() != ItemGroup.INVENTORY.getIndex())
            return;
        boolean handled = false;
        for(GuiCickListener listener : RiftLoader.instance.getListeners(GuiCickListener.class)) {
            if (listener.onClick((int) p_mouseClicked_1_, (int) p_mouseClicked_3_, p_mouseClicked_5_)) {
                ci.setReturnValue(true);
                handled = true;
            }
        }
        if (handled)
            ci.cancel();
    }
    
}