aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/firmament/mixins/customgui/PatchHandledScreen.java
blob: b0fbbe10ea0a263778aa7bfe4100e13947198069 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package moe.nea.firmament.mixins.customgui;

import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import moe.nea.firmament.events.HandledScreenKeyReleasedEvent;
import moe.nea.firmament.keybindings.GenericInputAction;
import moe.nea.firmament.keybindings.InputModifiers;
import moe.nea.firmament.util.customgui.CoordRememberingSlot;
import moe.nea.firmament.util.customgui.CustomGui;
import moe.nea.firmament.util.customgui.HasCustomGui;
import net.minecraft.client.input.MouseButtonEvent;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.input.CharacterEvent;
import net.minecraft.client.input.KeyEvent;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
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.CallbackInfoReturnable;

@Mixin(AbstractContainerScreen.class)
public class PatchHandledScreen<T extends AbstractContainerMenu> extends Screen implements HasCustomGui {
	@Shadow
	@Final
	protected T menu;
	@Shadow
	protected int leftPos;
	@Shadow
	protected int topPos;
	@Shadow
	protected int imageHeight;
	@Shadow
	protected int imageWidth;
	@Unique
	public CustomGui override;
	@Unique
	public boolean hasRememberedSlots = false;
	@Unique
	private int originalBackgroundWidth;
	@Unique
	private int originalBackgroundHeight;

	protected PatchHandledScreen(Component title) {
		super(title);
	}

	@Nullable
	@Override
	public CustomGui getCustomGui_Firmament() {
		return override;
	}

	@Override
	public void setCustomGui_Firmament(@Nullable CustomGui gui) {
		if (this.override != null) {
			imageHeight = originalBackgroundHeight;
			imageWidth = originalBackgroundWidth;
		}
		if (gui != null) {
			originalBackgroundHeight = imageHeight;
			originalBackgroundWidth = imageWidth;
		}
		this.override = gui;
	}

	public boolean mouseScrolled_firmament(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
		return override != null && override.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
	}

	public boolean keyReleased_firmament(KeyEvent input) {
		if (HandledScreenKeyReleasedEvent.Companion.publish(new HandledScreenKeyReleasedEvent(
			(AbstractContainerScreen<?>) (Object) this,
			GenericInputAction.of(input),
			InputModifiers.of(input))).getCancelled())
			return true;
		return override != null && override.keyReleased(input);
	}

	public boolean charTyped_firmament(CharacterEvent input) {
		return override != null && override.charTyped(input);
	}

	@Inject(method = "init", at = @At("TAIL"))
	private void onInit(CallbackInfo ci) {
		if (override != null) {
			override.onInit();
		}
	}

	@Inject(method = "renderLabels", at = @At("HEAD"), cancellable = true)
	private void onDrawForeground(GuiGraphics context, int mouseX, int mouseY, CallbackInfo ci) {
		if (override != null && !override.shouldDrawForeground())
			ci.cancel();
	}


	@WrapOperation(
		method = "renderSlots",
		at = @At(
			value = "INVOKE",
			target = "Lnet/minecraft/client/gui/screens/inventory/AbstractContainerScreen;renderSlot(Lnet/minecraft/client/gui/GuiGraphics;Lnet/minecraft/world/inventory/Slot;)V"))
	private void beforeSlotRender(AbstractContainerScreen instance, GuiGraphics context, Slot slot, Operation<Void> original) {
		if (override != null) {
			override.beforeSlotRender(context, slot);
		}
		original.call(instance, context, slot);
		if (override != null) {
			override.afterSlotRender(context, slot);
		}
	}

	@Inject(method = "hasClickedOutside", at = @At("HEAD"), cancellable = true)
	public void onIsClickOutsideBounds(
		double mouseX, double mouseY, int left, int top,
		CallbackInfoReturnable<Boolean> cir) {
		if (override != null) {
			cir.setReturnValue(override.isClickOutsideBounds(mouseX, mouseY));
		}
	}

	@Inject(method = "isHovering(IIIIDD)Z", at = @At("HEAD"), cancellable = true)
	public void onIsPointWithinBounds(int x, int y, int width, int height, double pointX, double pointY, CallbackInfoReturnable<Boolean> cir) {
		if (override != null) {
			cir.setReturnValue(override.isPointWithinBounds(x + this.leftPos, y + this.topPos, width, height, pointX, pointY));
		}
	}

	@Inject(method = "isHovering(Lnet/minecraft/world/inventory/Slot;DD)Z", at = @At("HEAD"), cancellable = true)
	public void onIsPointOverSlot(Slot slot, double pointX, double pointY, CallbackInfoReturnable<Boolean> cir) {
		if (override != null) {
			cir.setReturnValue(override.isPointOverSlot(slot, this.leftPos, this.topPos, pointX, pointY));
		}
	}

	@Inject(method = "renderBackground", at = @At("HEAD"))
	public void moveSlots(GuiGraphics context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
		if (override != null) {
			for (Slot slot : menu.slots) {
				if (!hasRememberedSlots) {
					((CoordRememberingSlot) slot).rememberCoords_firmament();
				}
				override.moveSlot(slot);
			}
			hasRememberedSlots = true;
		} else {
			if (hasRememberedSlots) {
				for (Slot slot : menu.slots) {
					((CoordRememberingSlot) slot).restoreCoords_firmament();
				}
				hasRememberedSlots = false;
			}
		}
	}

	@Inject(at = @At("HEAD"), method = "onClose", cancellable = true)
	private void onVoluntaryExit(CallbackInfo ci) {
		if (override != null) {
			if (!override.onVoluntaryExit())
				ci.cancel();
		}
	}

	@WrapWithCondition(method = "renderBackground", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/inventory/AbstractContainerScreen;renderBg(Lnet/minecraft/client/gui/GuiGraphics;FII)V"))
	public boolean preventDrawingBackground(AbstractContainerScreen instance, GuiGraphics drawContext, float delta, int mouseX, int mouseY) {
		if (override != null) {
			override.render(drawContext, delta, mouseX, mouseY);
		}
		return override == null;
	}

	@WrapOperation(
		method = "mouseClicked",
		at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screens/Screen;mouseClicked(Lnet/minecraft/client/input/MouseButtonEvent;Z)Z"))
	public boolean overrideMouseClicks(AbstractContainerScreen instance, MouseButtonEvent click, boolean doubled, Operation<Boolean> original) {
		if (override != null) {
			if (override.mouseClick(click, doubled))
				return true;
		}
		return original.call(instance, click, doubled);
	}

	@Inject(method = "mouseDragged", at = @At("HEAD"), cancellable = true)
	public void overrideMouseDrags(MouseButtonEvent click, double offsetX, double offsetY, CallbackInfoReturnable<Boolean> cir) {
		if (override != null) {
			if (override.mouseDragged(click, offsetX, offsetY))
				cir.setReturnValue(true);
		}
	}

	@Inject(method = "keyPressed", at = @At("HEAD"), cancellable = true)
	private void overrideKeyPressed(KeyEvent input, CallbackInfoReturnable<Boolean> cir) {
		if (override != null) {
			if (override.keyPressed(input)) {
				cir.setReturnValue(true);
			}
		}
	}


	@Inject(
		method = "mouseReleased",
		at = @At("HEAD"), cancellable = true)
	public void overrideMouseReleases(MouseButtonEvent click, CallbackInfoReturnable<Boolean> cir) {
		if (override != null) {
			if (override.mouseReleased(click))
				cir.setReturnValue(true);
		}
	}
}