-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9615ce9
commit b94bd5a
Showing
13 changed files
with
362 additions
and
545 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/java/com/elytradev/movingworld/common/experiments/interact/ContainerChecks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.elytradev.movingworld.common.experiments.interact; | ||
|
||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.gui.inventory.GuiContainer; | ||
import net.minecraft.entity.player.EntityPlayer; | ||
import net.minecraft.inventory.Container; | ||
import net.minecraftforge.fml.relauncher.Side; | ||
import net.minecraftforge.fml.relauncher.SideOnly; | ||
import net.sf.cglib.proxy.Enhancer; | ||
|
||
/** | ||
* Created by darkevilmac on 3/14/2017. | ||
*/ | ||
public class ContainerChecks { | ||
|
||
@SideOnly(Side.CLIENT) | ||
public static void checkCurrentScreen(EntityPlayer player, Minecraft mc) { | ||
if (mc.currentScreen != null && mc.currentScreen instanceof GuiContainer) { | ||
if (player.openContainer != null && !Enhancer.isEnhanced(player.openContainer.getClass())) { | ||
System.out.println("Setting current screen slots."); | ||
((GuiContainer) mc.currentScreen).inventorySlots = player.openContainer; | ||
} | ||
} | ||
} | ||
|
||
public static void checkContainer(EntityPlayer player) { | ||
if (player.openContainer != null | ||
&& player.openContainer != player.inventoryContainer | ||
&& !Enhancer.isEnhanced(player.openContainer.getClass())) { | ||
System.out.println("Making container proxy... " + player.openContainer); | ||
player.openContainer = (Container) ContainerInterceptor.createProxy(player.openContainer); | ||
System.out.println("Proxy made... " + player.openContainer); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...test/java/com/elytradev/movingworld/common/experiments/interact/ContainerInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.elytradev.movingworld.common.experiments.interact; | ||
|
||
import com.elytradev.movingworld.client.experiments.EntityPlayerSPProxy; | ||
import net.minecraft.client.entity.EntityPlayerSP; | ||
import net.minecraft.entity.player.EntityPlayer; | ||
import net.minecraft.entity.player.EntityPlayerMP; | ||
import net.minecraft.inventory.Container; | ||
import net.sf.cglib.proxy.Callback; | ||
import net.sf.cglib.proxy.Enhancer; | ||
import net.sf.cglib.proxy.MethodInterceptor; | ||
import net.sf.cglib.proxy.MethodProxy; | ||
import org.apache.commons.lang3.reflect.FieldUtils; | ||
import org.objenesis.ObjenesisHelper; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Pretty much entirely magic, really horrible code that | ||
*/ | ||
public class ContainerInterceptor implements MethodInterceptor { | ||
|
||
public static Object createProxy(Object realObject) { | ||
MethodInterceptor interceptor = new ContainerInterceptor(); | ||
Enhancer e = new Enhancer(); | ||
e.setUseCache(false); | ||
e.setSuperclass(realObject.getClass()); | ||
e.setCallbackType(interceptor.getClass()); | ||
Class classForProxy = e.createClass(); | ||
Enhancer.registerCallbacks(classForProxy, new Callback[]{interceptor}); | ||
Object createdProxy = ObjenesisHelper.newInstance(classForProxy); | ||
|
||
// Attempt to move data to prevent inital NPEs. We're also instantiating from absolutely nothing so this is probably the best thing we can do... | ||
try { | ||
for (Field realField : FieldUtils.getAllFieldsList(realObject.getClass())) { | ||
realField.setAccessible(true); | ||
Field proxyField = FieldUtils.getField(createdProxy.getClass(), realField.getName(), true); | ||
proxyField.set(createdProxy, realField.get(realObject)); | ||
} | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
} | ||
// Done. | ||
|
||
return createdProxy; | ||
} | ||
|
||
private EntityPlayer getProxy(Object[] args) { | ||
Object arg0 = null; | ||
if (args.length != 0) { | ||
arg0 = args[0]; | ||
} | ||
|
||
if (arg0 != null && arg0 instanceof EntityPlayer) { | ||
EntityPlayer playerProxy = null; | ||
|
||
EntityPlayer playerIn = (EntityPlayer) arg0; | ||
if (playerIn instanceof EntityPlayerMP && EntityPlayerMPProxy.PROXIES.containsKey(playerIn.getGameProfile())) { | ||
playerProxy = (EntityPlayerMPProxy.PROXIES.get(playerIn.getGameProfile())); | ||
} else if (playerIn instanceof EntityPlayerSP && EntityPlayerSPProxy.PROXIES.containsKey(playerIn.getGameProfile())) { | ||
playerProxy = (EntityPlayerSPProxy.PROXIES.get(playerIn.getGameProfile())); | ||
} | ||
|
||
return playerProxy; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { | ||
if (obj instanceof Container && ( | ||
method.getName().equals("canInteractWith") | ||
|| method.getName().equals("func_75145_c"))) { | ||
EntityPlayer proxyPlayer = getProxy(args); | ||
Boolean proxyCan = proxyPlayer != null ? (Boolean) methodProxy.invokeSuper(obj, new Object[]{proxyPlayer}) : false; | ||
return proxyCan || (Boolean) methodProxy.invokeSuper(obj, args); | ||
} | ||
|
||
return methodProxy.invokeSuper(obj, args); | ||
} | ||
} |
Oops, something went wrong.
b94bd5a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
L:FDhy dlok;h l;kHDLFK hldkfu ydrfklh D:LKJH ;ldfhl;k hdlf dsf