Skip to content

Commit

Permalink
Collection of Guide hacks to get everything working
Browse files Browse the repository at this point in the history
Allows straight XML files without having to go through the Markdown processing first
Fixes the flipping of checking for metadata and NBT for ItemStackValueFilters, although only for engines

The port is now complete with the bumping of Gradle version
  • Loading branch information
Chocohead committed Feb 14, 2018
1 parent 1021c73 commit bb7429f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
}
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.2-SNAPSHOT'
classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT'
}
}
apply plugin: 'net.minecraftforge.gradle.forge'
Expand Down
63 changes: 63 additions & 0 deletions src/com/chocohead/eumj/EngineMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
import static com.chocohead.eumj.EngineMod.MODID;

import java.io.File;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
Expand All @@ -33,8 +42,12 @@
import buildcraft.api.enums.EnumEngineType;
import buildcraft.api.mj.MjAPI;
import buildcraft.lib.client.guide.GuideManager;
import buildcraft.lib.client.guide.PageEntry;
import buildcraft.lib.client.guide.PageLine;
import buildcraft.lib.client.guide.loader.IPageLoader;
import buildcraft.lib.client.guide.loader.XmlPageLoader;
import buildcraft.lib.client.guide.loader.entry.EntryTypeItem;
import buildcraft.lib.client.guide.loader.entry.ItemStackValueFilter;
import buildcraft.lib.client.guide.parts.GuideText;
import buildcraft.lib.gui.GuiStack;
import buildcraft.lib.gui.ISimpleDrawable;
Expand All @@ -47,6 +60,7 @@
import ic2.core.block.BlockTileEntity;
import ic2.core.block.TeBlockRegistry;
import ic2.core.item.ItemIC2;
import ic2.core.util.ReflectionUtil;
import ic2.core.util.StackUtil;

import com.chocohead.eumj.item.ItemReaderMJ;
Expand Down Expand Up @@ -202,6 +216,9 @@ public void init(FMLInitializationEvent event) {
}
}

//BuildCraft bounces the loading of Markdown into XML anyway, we'll just go straight there thank you.
ReflectionUtil.<Map<String, IPageLoader>>getFieldValue(ReflectionUtil.getField(GuideManager.class, "PAGE_LOADERS"), null).put("xml", XmlPageLoader.INSTANCE);

//BuildCraft Lib loads pages in post-init, but it also loads first, so we do this here
XmlPageLoader.TAG_FACTORIES.put("engineLink", tag -> {
ItemStack stack = XmlPageLoader.loadItemStack(tag);
Expand Down Expand Up @@ -234,6 +251,52 @@ public void postInit(FMLPostInitializationEvent event) {

return te instanceof TileEntityEngine && ((TileEntityEngine) te).trySpin(side.getOpposite()) ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;
});

if (event.getSide().isClient()) {
((IReloadableResourceManager)Minecraft.getMinecraft().getResourceManager()).registerReloadListener(new IResourceManagerReloadListener() {
private final MethodHandle entries = findEntries();
private final MethodHandle meta = makeSetter("matchMeta");
private final MethodHandle nbt = makeSetter("matchNbt");

private MethodHandle findEntries() {
try {
return MethodHandles.lookup().unreflectGetter(ReflectionUtil.getField(GuideManager.class, "entries"));
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Error finding list of guide book page entries", e);
}
}

private MethodHandle makeSetter(String name) {
try {
return MethodHandles.lookup().unreflectSetter(ReflectionUtil.getField(ItemStackValueFilter.class, name));
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Error finding setting for ItemStackValueFilter", e);
}
}

@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
try {
onUnsafeReload();
} catch (Throwable t) {
throw new RuntimeException("Error reloading engine guide book pages", t);
}
}

private void onUnsafeReload() throws Throwable {
for (PageEntry<?> page : (List<PageEntry<?>>) entries.invokeExact(GuideManager.INSTANCE)) {
if (page.entryType == EntryTypeItem.INSTANCE) {
ItemStackValueFilter value = (ItemStackValueFilter) page.value;

if (StackUtil.checkItemEquality(value.stack.baseStack, engine.getItem())) {
meta.invokeExact(value, true);
nbt.invokeExact(value, false);
}
}
}
}
});
}
}


Expand Down

0 comments on commit bb7429f

Please sign in to comment.