Skip to content

Commit

Permalink
more mixins, and actually apply them
Browse files Browse the repository at this point in the history
  • Loading branch information
ghzdude committed Nov 6, 2024
1 parent fb85203 commit dcb34f9
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public List<String> getMixinConfigs() {
configs.add("mixins.gregtech.ccl.json");
configs.add("mixins.gregtech.littletiles.json");
configs.add("mixins.gregtech.vintagium.json");
configs.add("mixins.gregtech.mui2.json");

return configs;
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/gregtech/mixins/mui2/ClientEventHandlerMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gregtech.mixins.mui2;

import net.minecraftforge.client.event.GuiScreenEvent;

import com.cleanroommc.modularui.ClientEventHandler;
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;

@Mixin(value = ClientEventHandler.class, remap = false)
@SuppressWarnings("UnstableApiUsage")
public abstract class ClientEventHandlerMixin {

@Inject(method = "onGuiInput(Lnet/minecraftforge/client/event/GuiScreenEvent$MouseInputEvent$Pre;)V",
at = @At("HEAD"),
cancellable = true)
private static void fixMouseInput(GuiScreenEvent.MouseInputEvent.Pre event, CallbackInfo ci) {
ci.cancel();
}

@Inject(method = "onGuiInput(Lnet/minecraftforge/client/event/GuiScreenEvent$KeyboardInputEvent$Pre;)V",
at = @At("HEAD"),
cancellable = true)
private static void fixKeyInput(GuiScreenEvent.KeyboardInputEvent.Pre event, CallbackInfo ci) {
ci.cancel();
}
}
18 changes: 18 additions & 0 deletions src/main/java/gregtech/mixins/mui2/ItemSlotSHMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.items.ItemHandlerHelper;

import com.cleanroommc.modularui.screen.ModularContainer;
import com.cleanroommc.modularui.utils.MouseData;
import com.cleanroommc.modularui.value.sync.ItemSlotSH;
import com.cleanroommc.modularui.value.sync.SyncHandler;
import com.cleanroommc.modularui.widgets.slot.ModularSlot;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

import java.io.IOException;

@Mixin(value = ItemSlotSH.class, remap = false)
public abstract class ItemSlotSHMixin extends SyncHandler {

@Unique
private boolean gregTech$registered;

@Shadow
protected abstract void phantomScroll(MouseData mouseData);

Expand All @@ -36,6 +43,17 @@ public abstract class ItemSlotSHMixin extends SyncHandler {
@Shadow
public abstract void incrementStackCount(int amount);

@WrapOperation(method = "init",
at = @At(value = "INVOKE",
target = "Lcom/cleanroommc/modularui/screen/ModularContainer;registerSlot(Ljava/lang/String;Lcom/cleanroommc/modularui/widgets/slot/ModularSlot;)V"))
protected void wrapRegister(ModularContainer instance, String slotGroup, ModularSlot modularSlot,
Operation<Void> original) {
if (!gregTech$registered) {
original.call(instance, slotGroup, modularSlot);
gregTech$registered = true;
}
}

/**
* @author GTCEu - Ghzdude
* @reason Implement <a href="https://github.com/CleanroomMC/ModularUI/pull/90">MUI2 PR#90</a>
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/gregtech/mixins/mui2/PanelSyncHandlerMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package gregtech.mixins.mui2;

import com.cleanroommc.modularui.api.widget.ISynced;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.value.sync.ModularSyncManager;
import com.cleanroommc.modularui.value.sync.PanelSyncHandler;
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
import com.cleanroommc.modularui.widget.WidgetTree;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.concurrent.atomic.AtomicInteger;

@Mixin(PanelSyncHandler.class)
public abstract class PanelSyncHandlerMixin {

@Shadow
private PanelSyncManager syncManager;

@Shadow
public abstract boolean isPanelOpen();

@Redirect(method = "openPanel(Z)V",
at = @At(value = "INVOKE",
target = "Lcom/cleanroommc/modularui/widget/WidgetTree;collectSyncValues(Lcom/cleanroommc/modularui/value/sync/PanelSyncManager;Lcom/cleanroommc/modularui/screen/ModularPanel;)V"))
protected void redirectCollectValues(PanelSyncManager syncManager, ModularPanel panel) {
gregTech$collectSyncValues(this.syncManager, panel);
}

@Inject(method = "openPanel(Z)V", at = @At("HEAD"), cancellable = true)
protected void openCheck(boolean syncToServer, CallbackInfo ci) {
if (isPanelOpen()) ci.cancel();
}

@Unique
private static void gregTech$collectSyncValues(PanelSyncManager syncManager, ModularPanel panel) {
AtomicInteger id = new AtomicInteger(0);
String syncKey = ModularSyncManager.AUTO_SYNC_PREFIX + panel.getName();
WidgetTree.foreachChildBFS(panel, widget -> {
if (widget instanceof ISynced<?>synced) {
if (synced.isSynced() && !syncManager.hasSyncHandler(synced.getSyncHandler())) {
syncManager.syncValue(syncKey, id.getAndIncrement(), synced.getSyncHandler());
}
}
return true;
}, false);
}
}
36 changes: 36 additions & 0 deletions src/main/java/gregtech/mixins/mui2/PanelSyncManagerMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gregtech.mixins.mui2;

import gregtech.api.util.GTLog;

import net.minecraft.network.PacketBuffer;

import com.cleanroommc.modularui.value.sync.PanelSyncManager;
import com.cleanroommc.modularui.value.sync.SyncHandler;
import org.spongepowered.asm.mixin.Final;
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.CallbackInfo;

import java.util.Map;

@Mixin(PanelSyncManager.class)
public abstract class PanelSyncManagerMixin {

@Shadow
@Final
private Map<String, SyncHandler> syncHandlers;

@Shadow
private String panelName;

@Inject(method = "receiveWidgetUpdate", at = @At("HEAD"), cancellable = true)
public void injectCheck(String mapKey, int id, PacketBuffer buf, CallbackInfo ci) {
if (!this.syncHandlers.containsKey(mapKey)) {
GTLog.logger.warn("[ModularUI] SyncHandler \"{}\" does not exist for panel \"{}\"! ID was {}.", mapKey,
panelName, id);
ci.cancel();
}
}
}
61 changes: 61 additions & 0 deletions src/main/java/gregtech/mixins/mui2/StyledTextMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package gregtech.mixins.mui2;

import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.drawable.text.AnimatedText;
import com.cleanroommc.modularui.drawable.text.StyledText;
import com.cleanroommc.modularui.utils.Alignment;
import com.cleanroommc.modularui.widgets.TextWidget;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(value = StyledText.class, remap = false)
public class StyledTextMixin {

@Shadow
@Final
private IKey key;

@Shadow
private Alignment alignment;

@Shadow
private Integer color;

@Shadow
private float scale;

@Shadow
private Boolean shadow;

/**
* @author GTCEu - Ghzdude
* @reason Implement <a href="https://github.com/CleanroomMC/ModularUI/pull/86">MUI2 PR#86</a>
*/
@Overwrite
public TextWidget asWidget() {
int color = this.color == null ? 0 : this.color;
boolean shadow = this.shadow == null || this.shadow;
return new TextWidget(this.key)
.alignment(this.alignment)
.color(color)
.scale(this.scale)
.shadow(shadow);
}

/**
* @author GTCEu - Ghzdude
* @reason Implement <a href="https://github.com/CleanroomMC/ModularUI/pull/86">MUI2 PR#86</a>
*/
@Overwrite
public AnimatedText withAnimation() {
int color = this.color == null ? 0 : this.color;
boolean shadow = this.shadow == null || this.shadow;
return new AnimatedText(this.key)
.alignment(this.alignment)
.color(color)
.scale(this.scale)
.shadow(shadow);
}
}
15 changes: 15 additions & 0 deletions src/main/java/gregtech/mixins/mui2/TextWidgetMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gregtech.mixins.mui2;

import com.cleanroommc.modularui.widgets.TextWidget;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(value = TextWidget.class, remap = false)
public class TextWidgetMixin {

@ModifyReturnValue(method = { "getDefaultHeight", "getDefaultWidth" }, at = @At("TAIL"))
public int clamp(int r) {
return Math.max(1, r);
}
}
8 changes: 7 additions & 1 deletion src/main/resources/mixins.gregtech.mui2.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
},
"mixins": [
"ItemSlotSHMixin",
"PanelSyncHandlerMixin",
"PanelSyncManagerMixin",
"StyledTextMixin",
"TextWidgetMixin",
"ToggleButtonMixin",
"TooltipMixin",
"TooltipMixin$RichTextWidgetMixin"
],
"client": [],
"client": [
"ClientEventHandlerMixin"
],
"server": []
}

0 comments on commit dcb34f9

Please sign in to comment.