Skip to content

Commit

Permalink
Remove mixin and move to reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
miozune committed Oct 18, 2022
1 parent fd980c7 commit 42130cd
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 391 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ apiPackage =
accessTransformersFile =

# Provides setup for Mixins if enabled. If you don't know what mixins are: Keep it disabled!
usesMixins = true
usesMixins = false
# Adds some debug arguments like verbose output and export
usesMixinDebug = false
# Specify the location of your implementation of IMixinConfigPlugin. Leave it empty otherwise.
mixinPlugin = mixinplugin.MixinPlugin
mixinPlugin =
# Specify the package that contains all of your Mixins. You may only place Mixins in this package or the build will fail!
mixinsPackage = mixins
mixinsPackage =
# Specify the core mod entry class if you use a core mod. This class must implement IFMLLoadingPlugin!
# This parameter is for legacy compatibility only
# Example value: coreModClass = asm.FMLPlugin + modGroup = com.myname.mymodid -> com.myname.mymodid.asm.FMLPlugin
Expand Down
28 changes: 27 additions & 1 deletion repositories.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
// Add any additional repositories for your dependencies here

repositories {

maven {
name = "GTNH"
url = "http://jenkins.usrv.eu:8081/nexus/content/groups/public/"
}
maven {
name = "ic2"
url = "https://maven.ic2.player.to/"
metadataSources {
mavenPom()
artifact()
}
}
maven {
name = "ic2"
url = "https://maven2.ic2.player.to/"
metadataSources {
mavenPom()
artifact()
}
}
maven {
url "https://cursemaven.com"
}
maven {
name = "jitpack.io"
url = "https://jitpack.io"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.gtnewhorizons.modularui.common.internal.wrapper;

import static com.gtnewhorizons.modularui.ModularUI.isDevEnv;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

@SuppressWarnings("JavaReflectionMemberAccess")
public abstract class GuiContainerAccessor extends GuiContainer {

private static final Field fieldTheSlot;
private static final Field fieldClickedSlot;
private static final Field fieldDraggedStack;
private static final Field fieldIsRightMouseClick;
private static final Field fieldDragSplittingLimit;
private static final Method methodUpdateDragSplitting;
private static final Field fieldDragSplittingRemnant;
private static final Field fieldReturningStack;
private static final Field fieldReturningStackDestSlot;
private static final Field fieldTouchUpX;
private static final Field fieldTouchUpY;
private static final Field fieldReturningStackTime;

static {
final Class<?> gc = GuiContainer.class;
try {
fieldTheSlot = isDevEnv ? gc.getDeclaredField("theSlot") : gc.getDeclaredField("field_147006_u");
fieldTheSlot.setAccessible(true);
fieldClickedSlot = isDevEnv ? gc.getDeclaredField("clickedSlot") : gc.getDeclaredField("field_147005_v");
fieldClickedSlot.setAccessible(true);
fieldDraggedStack = isDevEnv ? gc.getDeclaredField("draggedStack") : gc.getDeclaredField("field_147012_x");
fieldDraggedStack.setAccessible(true);
fieldIsRightMouseClick =
isDevEnv ? gc.getDeclaredField("isRightMouseClick") : gc.getDeclaredField("field_147004_w");
fieldIsRightMouseClick.setAccessible(true);
fieldDragSplittingLimit = gc.getDeclaredField("field_146987_F");
fieldDragSplittingLimit.setAccessible(true);
methodUpdateDragSplitting = gc.getDeclaredMethod("func_146980_g");
methodUpdateDragSplitting.setAccessible(true);
fieldDragSplittingRemnant = gc.getDeclaredField("field_146996_I");
fieldDragSplittingRemnant.setAccessible(true);
fieldReturningStack =
isDevEnv ? gc.getDeclaredField("returningStack") : gc.getDeclaredField("field_146991_C");
fieldReturningStack.setAccessible(true);
fieldReturningStackDestSlot =
isDevEnv ? gc.getDeclaredField("returningStackDestSlot") : gc.getDeclaredField("field_146989_A");
fieldReturningStackDestSlot.setAccessible(true);
fieldTouchUpX = gc.getDeclaredField("field_147011_y");
fieldTouchUpX.setAccessible(true);
fieldTouchUpY = gc.getDeclaredField("field_147010_z");
fieldTouchUpY.setAccessible(true);
fieldReturningStackTime =
isDevEnv ? gc.getDeclaredField("returningStackTime") : gc.getDeclaredField("field_146990_B");
fieldReturningStackTime.setAccessible(true);
} catch (NoSuchFieldException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}

public void setHoveredSlot(Slot slot) {
try {
fieldTheSlot.set(this, slot);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public Slot getClickedSlot() {
try {
return (Slot) fieldClickedSlot.get(this);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public ItemStack getDraggedStack() {
try {
return (ItemStack) fieldDraggedStack.get(this);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public boolean getIsRightMouseClick() {
try {
return (boolean) fieldIsRightMouseClick.get(this);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public int getDragSplittingLimit() {
try {
return (int) fieldDragSplittingLimit.get(this);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public void invokeUpdateDragSplitting() {
try {
methodUpdateDragSplitting.invoke(this);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

@SuppressWarnings("unchecked")
public Set<Slot> getDragSplittingSlots() {
return field_147008_s;
}

public boolean isDragSplittingInternal() {
return field_147007_t;
}

public int getDragSplittingRemnant() {
try {
return (int) fieldDragSplittingRemnant.get(this);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public ItemStack getReturningStack() {
try {
return (ItemStack) fieldReturningStack.get(this);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public void setReturningStack(ItemStack stack) {
try {
fieldReturningStack.set(this, stack);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public Slot getReturningStackDestSlot() {
try {
return (Slot) fieldReturningStackDestSlot.get(this);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public int getTouchUpX() {
try {
return (int) fieldTouchUpX.get(this);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public int getTouchUpY() {
try {
return (int) fieldTouchUpY.get(this);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public long getReturningStackTime() {
try {
return (long) fieldReturningStackTime.get(this);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public GuiContainerAccessor(Container p_i1072_1_) {
super(p_i1072_1_);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.gtnewhorizons.modularui.api.widget.Widget;
import com.gtnewhorizons.modularui.common.widget.SlotWidget;
import com.gtnewhorizons.modularui.config.Config;
import com.gtnewhorizons.modularui.mixins.GuiContainerMixin;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
Expand Down Expand Up @@ -56,7 +55,7 @@

@SideOnly(Side.CLIENT)
@Optional.Interface(modid = "NotEnoughItems", iface = "codechicken.nei.api.INEIGuiHandler")
public class ModularGui extends GuiContainer implements INEIGuiHandler {
public class ModularGui extends GuiContainerAccessor implements INEIGuiHandler {

private final ModularUIContext context;
private Pos2d mousePos = Pos2d.ZERO;
Expand Down Expand Up @@ -111,8 +110,8 @@ public void initGui() {
this.context.getCurrentWindow().onOpen();
}

public GuiContainerMixin getAccessor() {
return (GuiContainerMixin) this;
public GuiContainerAccessor getAccessor() {
return this;
}

@Override
Expand All @@ -129,7 +128,7 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {
GlStateManager.pushMatrix();
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
GlStateManager.enableRescaleNormal();
getAccessor().setHoveredSlot(null);
setHoveredSlot(null);
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240.0F, 240.0F);
GlStateManager.enableRescaleNormal();
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
Expand All @@ -153,10 +152,10 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.drawGuiContainerForegroundLayer(mouseX, mouseY);
RenderHelper.enableGUIStandardItemLighting();

getAccessor().setHoveredSlot(null);
setHoveredSlot(null);
Widget hovered = getCursor().getHovered();
if (hovered instanceof IVanillaSlot) {
getAccessor().setHoveredSlot(((IVanillaSlot) hovered).getMcSlot());
setHoveredSlot(((IVanillaSlot) hovered).getMcSlot());
}

GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
Expand All @@ -165,20 +164,18 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {
GlStateManager.popMatrix();

InventoryPlayer inventoryplayer = this.mc.thePlayer.inventory;
ItemStack itemstack = getAccessor().getDraggedStack() == null
? inventoryplayer.getItemStack()
: getAccessor().getDraggedStack();
ItemStack itemstack = getDraggedStack() == null ? inventoryplayer.getItemStack() : getDraggedStack();
GlStateManager.translate((float) i, (float) j, 0.0F);
if (itemstack != null) {
int k2 = getAccessor().getDraggedStack() == null ? 8 : 16;
int k2 = getDraggedStack() == null ? 8 : 16;
String s = null;

if (getAccessor().getDraggedStack() != null && getAccessor().getIsRightMouseClick()) {
if (getDraggedStack() != null && getIsRightMouseClick()) {
itemstack = itemstack.copy();
itemstack.stackSize = (int) Math.ceil((float) itemstack.stackSize / 2.0F);
} else if (this.isDragSplitting() && this.getDragSlots().size() > 1) {
itemstack = itemstack.copy();
itemstack.stackSize = getAccessor().getDragSplittingRemnant();
itemstack.stackSize = getDragSplittingRemnant();

if (itemstack.stackSize < 1) {
s = EnumChatFormatting.YELLOW + "0";
Expand All @@ -188,21 +185,19 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.drawItemStack(itemstack, mouseX - i - 8, mouseY - j - k2, s);
}

if (getAccessor().getReturningStack() != null) {
float f = (float) (Minecraft.getSystemTime() - getAccessor().getReturningStackTime()) / 100.0F;
if (getReturningStack() != null) {
float f = (float) (Minecraft.getSystemTime() - getReturningStackTime()) / 100.0F;

if (f >= 1.0F) {
f = 1.0F;
getAccessor().setReturningStack(null);
setReturningStack(null);
}

int l2 = getAccessor().getReturningStackDestSlot().xDisplayPosition
- getAccessor().getTouchUpX();
int i3 = getAccessor().getReturningStackDestSlot().yDisplayPosition
- getAccessor().getTouchUpY();
int l1 = getAccessor().getTouchUpX() + (int) ((float) l2 * f);
int i2 = getAccessor().getTouchUpY() + (int) ((float) i3 * f);
this.drawItemStack(getAccessor().getReturningStack(), l1, i2, null);
int l2 = getReturningStackDestSlot().xDisplayPosition - getTouchUpX();
int i3 = getReturningStackDestSlot().yDisplayPosition - getTouchUpY();
int l1 = getTouchUpX() + (int) ((float) l2 * f);
int i2 = getTouchUpY() + (int) ((float) i3 * f);
this.drawItemStack(getReturningStack(), l1, i2, null);
}

GlStateManager.popMatrix();
Expand Down Expand Up @@ -601,11 +596,11 @@ public void onGuiClosed() {
}

public boolean isDragSplitting() {
return getAccessor().isDragSplittingInternal();
return isDragSplittingInternal();
}

public Set<Slot> getDragSlots() {
return getAccessor().getDragSplittingSlots();
return getDragSplittingSlots();
}

public static RenderItem getItemRenderer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import com.gtnewhorizons.modularui.api.widget.Widget;
import com.gtnewhorizons.modularui.common.internal.Theme;
import com.gtnewhorizons.modularui.common.internal.wrapper.BaseSlot;
import com.gtnewhorizons.modularui.common.internal.wrapper.GuiContainerAccessor;
import com.gtnewhorizons.modularui.common.internal.wrapper.ModularGui;
import com.gtnewhorizons.modularui.mixins.GuiContainerMixin;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.io.IOException;
Expand Down Expand Up @@ -395,7 +395,7 @@ public boolean handleDragAndDrop(ItemStack draggedStack, int button) {
return true;
}

private GuiContainerMixin getGuiAccessor() {
private GuiContainerAccessor getGuiAccessor() {
return getContext().getScreen().getAccessor();
}

Expand Down
Loading

0 comments on commit 42130cd

Please sign in to comment.