mirror of
https://github.com/GTNewHorizons/CraftTweaker.git
synced 2026-11-28 07:35:40 +00:00
- Ported setLocalization from ModTweaker - Ported seed modification from ModTweaker - Fixed IClient setting - Added dev builds to the build script
307 lines
9.9 KiB
Java
307 lines
9.9 KiB
Java
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
|
|
package minetweaker.mc164.util;
|
|
|
|
import com.google.common.collect.BiMap;
|
|
import cpw.mods.fml.common.registry.EntityRegistry;
|
|
import java.io.File;
|
|
import java.lang.reflect.Constructor;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
import minetweaker.MineTweakerAPI;
|
|
import minetweaker.api.item.WeightedItemStack;
|
|
import minetweaker.api.minecraft.MineTweakerMC;
|
|
import net.minecraft.entity.Entity;
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
import net.minecraft.inventory.Container;
|
|
import net.minecraft.inventory.InventoryCrafting;
|
|
import net.minecraft.inventory.SlotCrafting;import net.minecraft.item.ItemStack;
|
|
import net.minecraft.nbt.NBTBase;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.server.MinecraftServer;
|
|
import net.minecraft.util.StatCollector;
|
|
import net.minecraft.util.StringTranslate;
|
|
import net.minecraft.util.WeightedRandomItem;
|
|
import net.minecraftforge.common.ChestGenHooks;
|
|
import net.minecraftforge.common.ForgeHooks;
|
|
import net.minecraftforge.fluids.FluidContainerRegistry;
|
|
import net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData;
|
|
import net.minecraftforge.oredict.OreDictionary;
|
|
import net.minecraftforge.oredict.ShapedOreRecipe;
|
|
|
|
/**
|
|
* Common class for all runtime hacks (stuff requiring reflection). It is not
|
|
* unexpected to have it break with new minecraft versions and may need regular
|
|
* adjustment - as such, those have been collected here.
|
|
*
|
|
* @author Stan Hebben
|
|
*/
|
|
public class MineTweakerHacks {
|
|
private static final Field NBTTAGCOMPOUND_TAGMAP;
|
|
private static final Field OREDICTIONARY_ORESTACKS;
|
|
private static final Field MINECRAFTSERVER_ANVILFILE;
|
|
private static final Field SHAPEDORERECIPE_WIDTH;
|
|
|
|
private static final Field INVENTORYCRAFTING_EVENTHANDLER;
|
|
private static final Field SLOTCRAFTING_PLAYER;
|
|
|
|
private static final Field ENTITYREGISTRY_CLASSREGISTRATIONS;
|
|
private static final Field SEEDENTRY_SEED;
|
|
private static final Constructor<? extends WeightedRandomItem> SEEDENTRY_CONSTRUCTOR;
|
|
|
|
static {
|
|
NBTTAGCOMPOUND_TAGMAP = getField(NBTTagCompound.class, MineTweakerObfuscation.NBTTAGCOMPOUND_TAGMAP);
|
|
OREDICTIONARY_ORESTACKS = getField(OreDictionary.class, MineTweakerObfuscation.OREDICTIONARY_ORESTACKS);
|
|
MINECRAFTSERVER_ANVILFILE = getField(MinecraftServer.class, MineTweakerObfuscation.MINECRAFTSERVER_ANVILFILE);
|
|
SHAPEDORERECIPE_WIDTH = getField(ShapedOreRecipe.class, new String[] {"width"});
|
|
|
|
INVENTORYCRAFTING_EVENTHANDLER = getField(InventoryCrafting.class, MineTweakerObfuscation.INVENTORYCRAFTING_EVENTHANDLER);
|
|
SLOTCRAFTING_PLAYER = getField(SlotCrafting.class, MineTweakerObfuscation.SLOTCRAFTING_PLAYER);
|
|
|
|
ENTITYREGISTRY_CLASSREGISTRATIONS = getField(EntityRegistry.class, new String[] {"entityClassRegistrations"});
|
|
|
|
Class<? extends WeightedRandomItem> forgeSeedEntry = null;
|
|
try {
|
|
forgeSeedEntry = (Class<? extends WeightedRandomItem>) Class.forName("net.minecraftforge.common.ForgeHooks$SeedEntry");
|
|
} catch (ClassNotFoundException ex) {}
|
|
|
|
SEEDENTRY_SEED = getField(forgeSeedEntry, "seed");
|
|
|
|
Constructor<? extends WeightedRandomItem> seedEntryConstructor = null;
|
|
|
|
try {
|
|
seedEntryConstructor = forgeSeedEntry.getConstructor(ItemStack.class, int.class);
|
|
seedEntryConstructor.setAccessible(true);
|
|
} catch (NoSuchMethodException ex) {
|
|
Logger.getLogger(MineTweakerHacks.class.getName()).log(Level.SEVERE, null, ex);
|
|
} catch (SecurityException ex) {
|
|
Logger.getLogger(MineTweakerHacks.class.getName()).log(Level.SEVERE, null, ex);
|
|
}
|
|
|
|
SEEDENTRY_CONSTRUCTOR = seedEntryConstructor;
|
|
}
|
|
|
|
private MineTweakerHacks() {}
|
|
|
|
public static BiMap<Class<? extends Entity>, EntityRegistry.EntityRegistration> getEntityClassRegistrations() {
|
|
try {
|
|
return (BiMap<Class<? extends Entity>, EntityRegistry.EntityRegistration>) ENTITYREGISTRY_CLASSREGISTRATIONS.get(EntityRegistry.instance());
|
|
} catch (IllegalArgumentException ex) {
|
|
return null;
|
|
} catch (IllegalAccessException ex) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static Map<String, NBTBase> getNBTCompoundMap(NBTTagCompound tag) {
|
|
try {
|
|
return (Map<String, NBTBase>) NBTTAGCOMPOUND_TAGMAP.get(tag);
|
|
} catch (IllegalArgumentException ex) {
|
|
return null;
|
|
} catch (IllegalAccessException ex) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
public static List getSeeds() {
|
|
return getPrivateStaticObject(ForgeHooks.class, "seedList");
|
|
}
|
|
|
|
public static Map<String, ChestGenHooks> getChestLoot() {
|
|
return getPrivateStaticObject(ChestGenHooks.class, "chestInfo");
|
|
}
|
|
|
|
public static Map getTranslations() {
|
|
return getPrivateObject(
|
|
getPrivateStaticObject(StatCollector.class, "localizedName", "field_74839_a"),
|
|
"languageList",
|
|
"field_74816_c");
|
|
}
|
|
|
|
public static Map<Integer, List<ItemStack>> getOreStacks() {
|
|
try {
|
|
return (Map<Integer, List<ItemStack>>) OREDICTIONARY_ORESTACKS.get(null);
|
|
} catch (IllegalAccessException ex) {
|
|
MineTweakerAPI.logError("ERROR - could not load ore dictionary stacks!");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static File getAnvilFile(MinecraftServer server) {
|
|
try {
|
|
return (File) MINECRAFTSERVER_ANVILFILE.get(server);
|
|
} catch (IllegalAccessException ex) {
|
|
MineTweakerAPI.logError("could not load anvil file!");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static File getWorldDirectory(MinecraftServer server) {
|
|
if (server.isDedicatedServer()) {
|
|
return server.getFile("world");
|
|
} else {
|
|
File worldsDir = getAnvilFile(server);
|
|
if (worldsDir == null) return null;
|
|
|
|
return new File(worldsDir, server.getFolderName());
|
|
}
|
|
}
|
|
|
|
public static int getShapedOreRecipeWidth(ShapedOreRecipe recipe) {
|
|
try {
|
|
return SHAPEDORERECIPE_WIDTH.getInt(recipe);
|
|
} catch (IllegalAccessException ex) {
|
|
MineTweakerAPI.logError("could not load anvil file!");
|
|
return 3;
|
|
}
|
|
}
|
|
|
|
public static Container getCraftingContainer(InventoryCrafting inventory) {
|
|
try {
|
|
return (Container) INVENTORYCRAFTING_EVENTHANDLER.get(inventory);
|
|
} catch (IllegalAccessException ex) {
|
|
MineTweakerAPI.logError("could not get inventory eventhandler");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static EntityPlayer getCraftingSlotPlayer(SlotCrafting slot) {
|
|
try {
|
|
return (EntityPlayer) SLOTCRAFTING_PLAYER.get(slot);
|
|
} catch (IllegalAccessException ex) {
|
|
MineTweakerAPI.logError("could not get inventory eventhandler");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static StringTranslate getStringTranslateInstance() {
|
|
try {
|
|
Field field = getField(StringTranslate.class, MineTweakerObfuscation.STRINGTRANSLATE_INSTANCE);
|
|
return (StringTranslate) field.get(null);
|
|
} catch (IllegalAccessException ex) {
|
|
MineTweakerAPI.logError("could not get string translator");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static Map<List, FluidContainerData> getFluidContainerMap() {
|
|
try {
|
|
Field field = getField(FluidContainerRegistry.class, new String[] {"containerFluidMap"});
|
|
return (Map<List, FluidContainerData>) field.get(null);
|
|
} catch (IllegalAccessException ex) {
|
|
MineTweakerAPI.logError("could not get fluid container registry");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static Map<List, FluidContainerData> getFilledContainerMap() {
|
|
try {
|
|
Field field = getField(FluidContainerRegistry.class, new String[] {"filledContainerMap"});
|
|
return (Map<List, FluidContainerData>) field.get(null);
|
|
} catch (IllegalAccessException ex) {
|
|
MineTweakerAPI.logError("could not get filled container map");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static Set<List> getEmptyContainers() {
|
|
try {
|
|
Field field = getField(FluidContainerRegistry.class, new String[] {"emptyContainers"});
|
|
return (Set<List>) field.get(null);
|
|
} catch (IllegalAccessException ex) {
|
|
MineTweakerAPI.logError("could not get filled container map");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static ItemStack getSeedEntrySeed(Object entry) {
|
|
try {
|
|
return (ItemStack) SEEDENTRY_SEED.get(entry);
|
|
} catch (IllegalAccessException ex) {
|
|
MineTweakerAPI.logError("could not get SeedEntry seed");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static WeightedRandomItem constructSeedEntry(WeightedItemStack stack) {
|
|
try {
|
|
return SEEDENTRY_CONSTRUCTOR.newInstance(MineTweakerMC.getItemStack(stack.getStack()), (int) stack.getChance());
|
|
} catch (InstantiationException ex) {
|
|
MineTweakerAPI.logError("could not construct SeedEntry");
|
|
} catch (IllegalAccessException ex) {
|
|
MineTweakerAPI.logError("could not construct SeedEntry");
|
|
} catch (IllegalArgumentException ex) {
|
|
MineTweakerAPI.logError("could not construct SeedEntry");
|
|
} catch (InvocationTargetException ex) {
|
|
MineTweakerAPI.logError("could not construct SeedEntry");
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static <T> T getPrivateStaticObject(Class<?> cls, String... names) {
|
|
for (String name : names) {
|
|
try {
|
|
Field field = cls.getDeclaredField(name);
|
|
field.setAccessible(true);
|
|
return (T) field.get(null);
|
|
} catch (NoSuchFieldException ex) {
|
|
|
|
} catch (SecurityException ex) {
|
|
|
|
} catch (IllegalAccessException ex) {
|
|
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static <T> T getPrivateObject(Object object, String... names) {
|
|
Class<?> cls = object.getClass();
|
|
for (String name : names) {
|
|
try {
|
|
Field field = cls.getDeclaredField(name);
|
|
field.setAccessible(true);
|
|
return (T) field.get(object);
|
|
} catch (NoSuchFieldException ex) {
|
|
|
|
} catch (SecurityException ex) {
|
|
|
|
} catch (IllegalAccessException ex) {
|
|
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// #######################
|
|
// ### Private Methods ###
|
|
// #######################
|
|
|
|
private static Field getField(Class cls, String... names) {
|
|
for (String name : names) {
|
|
try {
|
|
Field field = cls.getDeclaredField(name);
|
|
field.setAccessible(true);
|
|
return field;
|
|
} catch (NoSuchFieldException ex) {
|
|
} catch (SecurityException ex) {
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|