Skip to content

Commit

Permalink
I'm going to hell for this.
Browse files Browse the repository at this point in the history
  • Loading branch information
darkevilmac committed Mar 14, 2017
1 parent 9615ce9 commit b94bd5a
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 545 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ dependencies {

compile 'com.elytradev:concrete:0.1.0'
shadow 'com.elytradev:concrete:0.1.0'

compile 'cglib:cglib:3.2.5'
compile 'org.objenesis:objenesis:2.5.1'
}

shadowJar {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package com.elytradev.movingworld.client.experiments;

import com.elytradev.movingworld.common.experiments.interact.ContainerWrapper;
import com.elytradev.movingworld.common.experiments.entity.EntityMobileRegion;
import com.mojang.authlib.GameProfile;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.GuiEnchantment;
import net.minecraft.client.gui.GuiHopper;
import net.minecraft.client.gui.GuiRepair;
import net.minecraft.client.gui.inventory.*;
import net.minecraft.entity.MoverType;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.stats.StatBase;
import net.minecraft.util.math.Vec3d;
Expand Down Expand Up @@ -59,20 +51,6 @@ public EntityPlayerSPProxy(EntityPlayerSP playerSP, EntityMobileRegion region) {
this.motionZ = parent.motionZ;
}

public static void onUpdateHook(EntityPlayerSP realPlayer) {
EntityPlayer proxy = null;

if (EntityPlayerSPProxy.PROXIES.containsKey(realPlayer.getGameProfile())) {
proxy = EntityPlayerSPProxy.PROXIES.get(realPlayer.getGameProfile());
}

if (proxy != null && proxy.openContainer instanceof ContainerWrapper) {
realPlayer.openContainer = proxy.openContainer;
} else if (proxy != null) {
proxy.openContainer = realPlayer.openContainer;
}
}

public void setRegion(EntityMobileRegion region) {
this.region = region;
}
Expand Down Expand Up @@ -111,22 +89,13 @@ public void addStat(StatBase stat, int amount) {

@Override
public void displayGUIChest(IInventory chestInventory) {
Container lastContainer = this.openContainer;
super.displayGUIChest(chestInventory);
Container currentContainer = this.openContainer;

validateWrapping(lastContainer, currentContainer);

parent.displayGUIChest(chestInventory);
}

@Override
public void displayGui(IInteractionObject guiOwner) {
Container lastContainer = this.openContainer;
displayGuiSuper(guiOwner);
Container currentContainer = this.openContainer;

validateWrapping(lastContainer, currentContainer);
super.displayGui(guiOwner);
parent.displayGui(guiOwner);
}

Expand All @@ -135,67 +104,14 @@ public void move(MoverType type, double x, double y, double z) {
parent.move(type, x, y, z);
}

public void displayGUIChestSuper(IInventory chestInventory) {
String s = chestInventory instanceof IInteractionObject ? ((IInteractionObject) chestInventory).getGuiID() : "minecraft:container";

if ("minecraft:chest".equals(s)) {
this.mc.displayGuiScreen(new GuiChest(this.inventory, chestInventory));
} else if ("minecraft:hopper".equals(s)) {
this.mc.displayGuiScreen(new GuiHopper(this.inventory, chestInventory));
} else if ("minecraft:furnace".equals(s)) {
this.mc.displayGuiScreen(new GuiFurnace(this.inventory, chestInventory));
} else if ("minecraft:brewing_stand".equals(s)) {
this.mc.displayGuiScreen(new GuiBrewingStand(this.inventory, chestInventory));
} else if ("minecraft:beacon".equals(s)) {
this.mc.displayGuiScreen(new GuiBeacon(this.inventory, chestInventory));
} else if (!"minecraft:dispenser".equals(s) && !"minecraft:dropper".equals(s)) {
if ("minecraft:shulker_box".equals(s)) {
this.mc.displayGuiScreen(new GuiShulkerBox(this.inventory, chestInventory));
} else {
this.mc.displayGuiScreen(new GuiChest(this.inventory, chestInventory));
}
} else {
this.mc.displayGuiScreen(new GuiDispenser(this.inventory, chestInventory));
}
}

public void displayGuiSuper(IInteractionObject guiOwner) {
String s = guiOwner.getGuiID();

if ("minecraft:crafting_table".equals(s)) {
this.mc.displayGuiScreen(new GuiCrafting(this.inventory, this.world));
} else if ("minecraft:enchanting_table".equals(s)) {
this.mc.displayGuiScreen(new GuiEnchantment(this.inventory, this.world, guiOwner));
} else if ("minecraft:anvil".equals(s)) {
this.mc.displayGuiScreen(new GuiRepair(this.inventory, this.world));
}
}

@Override
public void setPositionAndUpdate(double x, double y, double z) {
super.setPositionAndUpdate(x, y, z);
}

public void validateWrapping(Container lastContainer, Container currentContainer) {
if (lastContainer != currentContainer && !(currentContainer instanceof ContainerPlayer)) {
if (currentContainer instanceof ContainerWrapper)
return;

this.openContainer = new ContainerWrapper(this.openContainer);
}
}

public void openGuiSuper(Object mod, int modGuiId, World world, int x, int y, int z) {
net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.openGui(this, mod, modGuiId, world, x, y, z);
}

@Override
public void openGui(Object mod, int modGuiId, World world, int x, int y, int z) {
Container lastContainer = this.openContainer;
super.openGui(mod, modGuiId, world, x, y, z);
Container currentContainer = this.openContainer;

validateWrapping(lastContainer, currentContainer);
parent.openGui(mod, modGuiId, world, x, y, z);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public boolean isPosWithinRegion(BlockPos pos) {
@Override
public void tick() {
parentWorld.tick();
}

@Override
public void invalidateBlockReceiveRegion(int x1, int y1, int z1, int x2, int y2, int z2) {
parentWorld.invalidateBlockReceiveRegion(x1, y1, z1, x2, y2, z2);
} @Override
public void initCapabilities() {
if (initCapabilities == null) {
Expand All @@ -84,11 +89,6 @@ public void initCapabilities() {
super.initCapabilities();
}

@Override
public void invalidateBlockReceiveRegion(int x1, int y1, int z1, int x2, int y2, int z2) {
parentWorld.invalidateBlockReceiveRegion(x1, y1, z1, x2, y2, z2);
}

@Override
public IChunkProvider createChunkProvider() {
if (parentWorld != null)
Expand Down Expand Up @@ -249,9 +249,6 @@ public Biome getBiome(BlockPos pos) {
public Biome getBiomeForCoordsBody(BlockPos pos) {
pos = region.convertRegionPosToRealWorld(pos);
return getRealWorld().getBiomeForCoordsBody(pos);
} @Override
public ChunkProviderClient getChunkProvider() {
return parentWorld.getChunkProvider();
}

@Override
Expand All @@ -262,6 +259,9 @@ public BiomeProvider getBiomeProvider() {
@Override
public void initialize(WorldSettings settings) {
parentWorld.initialize(settings);
} @Override
public ChunkProviderClient getChunkProvider() {
return parentWorld.getChunkProvider();
}

@Nullable
Expand Down Expand Up @@ -1147,6 +1147,9 @@ public void checkSessionLock() throws MinecraftException {
parentWorld.checkSessionLock();
}

public WorldClient getRealWorld() {
return Minecraft.getMinecraft().world;
}



Expand Down Expand Up @@ -1470,8 +1473,6 @@ public BlockPos findNearestStructure(String p_190528_1_, BlockPos pos, boolean p

return getRealWorld().findNearestStructure(p_190528_1_, pos, p_190528_3_);
}

public WorldClient getRealWorld(){
return Minecraft.getMinecraft().world;
}


}
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);
}
}
}
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);
}
}
Loading

1 comment on commit b94bd5a

@unascribed
Copy link

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

Please sign in to comment.