mirror of
https://github.com/GTNewHorizons/waila.git
synced 2026-11-27 15:33:10 +00:00
Added an helper for reflection.
Moved all mod identification to a static class. Dump of KeyHandler string + mod on startup.
This commit is contained in:
1 parent
fde934363c
commit
7eb3ddb50c
7 files changed
+268
-52
No files matched your search
@@ -9,6 +9,7 @@ import mcp.mobius.waila.addons.ExternalModulesHandler;
|
||||
import mcp.mobius.waila.gui_old.GuiIngameWiki;
|
||||
import mcp.mobius.waila.gui_old.widget.Label;
|
||||
import mcp.mobius.waila.handlers.SummaryProvider;
|
||||
import mcp.mobius.waila.tools.ModIdentification;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -57,7 +58,7 @@ public class HandlerWiki implements IContainerInputHandler {
|
||||
Minecraft mc = Minecraft.getMinecraft();
|
||||
GuiIngameWiki wikiScreen = new GuiIngameWiki(mc.currentScreen);
|
||||
|
||||
wikiScreen.widReader.setText(this.getDescriptionString(mod_Waila.instance.getModID(stackover), stackover.getItemName(), String.valueOf(stackover.getItemDamage())));
|
||||
wikiScreen.widReader.setText(this.getDescriptionString(ModIdentification.idFromStack(stackover), stackover.getItemName(), String.valueOf(stackover.getItemDamage())));
|
||||
wikiScreen.widStack.setStack(stackover);
|
||||
wikiScreen.widItemName.setLabel(stackover.getDisplayName());
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
import mcp.mobius.waila.Constants;
|
||||
import mcp.mobius.waila.mod_Waila;
|
||||
import mcp.mobius.waila.addons.ConfigHandler;
|
||||
import mcp.mobius.waila.tools.ModIdentification;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.MovingObjectPosition;
|
||||
@@ -23,7 +24,7 @@ public class HUDHandlerWaila implements IHighlightHandler {
|
||||
public List<String> handleTextData(ItemStack itemStack, World world, EntityPlayer player, MovingObjectPosition mop, List<String> currenttip, Layout layout) {
|
||||
|
||||
if (layout == Layout.FOOTER){
|
||||
String modName = mod_Waila.instance.getModName(itemStack);
|
||||
String modName = ModIdentification.nameFromStack(itemStack);
|
||||
if (modName != null && !modName.equals(""))
|
||||
currenttip.add("\u00a79\u00a7o" + modName);
|
||||
} else if (layout == Layout.HEADER && ConfigHandler.instance().getConfig(Constants.CFG_WAILA_METADATA, false)){
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.logging.Level;
|
||||
import mcp.mobius.waila.Constants;
|
||||
import mcp.mobius.waila.mod_Waila;
|
||||
import mcp.mobius.waila.gui.screens.info.ScreenEnchants;
|
||||
import mcp.mobius.waila.tools.ModIdentification;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||
import net.minecraft.enchantment.Enchantment;
|
||||
@@ -93,7 +94,7 @@ public class HandlerEnchants implements IContainerInputHandler {
|
||||
colorcode + String.valueOf(minLevel),
|
||||
colorcode + String.valueOf(maxLevel),
|
||||
colorcode + String.valueOf(enchant.getWeight()),
|
||||
"\u00a79\u00a7o"+ this.getEnchantModName(enchant));
|
||||
"\u00a79\u00a7o"+ ModIdentification.nameFromObject(enchant));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,6 +104,7 @@ public class HandlerEnchants implements IContainerInputHandler {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
public String getEnchantModName(Enchantment enchant){
|
||||
String enchantPath = enchant.getClass().getProtectionDomain().getCodeSource().getLocation().toString();
|
||||
|
||||
@@ -116,7 +118,8 @@ public class HandlerEnchants implements IContainerInputHandler {
|
||||
if (enchantModName.equals("Minecraft Coder Pack"))
|
||||
enchantModName = "Minecraft";
|
||||
return enchantModName;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
@Override
|
||||
public boolean mouseClicked(GuiContainer gui, int mousex, int mousey, int button) { return false; }
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package mcp.mobius.waila.handlers.tooltip;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import mcp.mobius.waila.mod_Waila;
|
||||
import mcp.mobius.waila.tools.ModIdentification;
|
||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import codechicken.nei.forge.IContainerTooltipHandler;
|
||||
|
||||
public class TooltipHandlerWaila implements IContainerTooltipHandler {
|
||||
|
||||
private mod_Waila waila = mod_Waila.instance;
|
||||
|
||||
@Override
|
||||
public List<String> handleTooltipFirst(GuiContainer gui, int mousex, int mousey, List<String> currenttip) {
|
||||
return currenttip;
|
||||
@@ -17,7 +17,7 @@ public class TooltipHandlerWaila implements IContainerTooltipHandler {
|
||||
|
||||
@Override
|
||||
public List<String> handleItemTooltip(GuiContainer gui, ItemStack itemstack, List<String> currenttip) {
|
||||
String canonicalName = waila.getModName(itemstack);
|
||||
String canonicalName = ModIdentification.nameFromStack(itemstack);
|
||||
if (canonicalName != null && !canonicalName.equals(""))
|
||||
currenttip.add("\u00a79\u00a7o" + canonicalName);
|
||||
return currenttip;
|
||||
|
||||
@@ -1,20 +1,29 @@
|
||||
package mcp.mobius.waila;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import mcp.mobius.antigregtech.AccessHelper;
|
||||
import mcp.mobius.waila.addons.ConfigHandler;
|
||||
import mcp.mobius.waila.addons.ExternalModulesHandler;
|
||||
import mcp.mobius.waila.api.IWailaRegistrar;
|
||||
import mcp.mobius.waila.network.WailaConnectionHandler;
|
||||
import mcp.mobius.waila.network.WailaPacketHandler;
|
||||
import mcp.mobius.waila.server.ProxyServer;
|
||||
import mcp.mobius.waila.tools.ModIdentification;
|
||||
import mcp.mobius.waila.tools.Reflect;
|
||||
import net.minecraft.enchantment.Enchantment;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.common.Configuration;
|
||||
import codechicken.core.CommonUtils;
|
||||
import cpw.mods.fml.client.registry.KeyBindingRegistry;
|
||||
import cpw.mods.fml.client.registry.KeyBindingRegistry.KeyHandler;
|
||||
import cpw.mods.fml.common.Loader;
|
||||
import cpw.mods.fml.common.Mod;
|
||||
import cpw.mods.fml.common.Mod.IMCCallback;
|
||||
@@ -46,10 +55,8 @@ public class mod_Waila {
|
||||
public static ProxyServer proxy;
|
||||
|
||||
public static Logger log = Logger.getLogger("Waila");
|
||||
private HashMap<Integer, String> itemMap = new HashMap<Integer, String>();
|
||||
public Configuration config = null;
|
||||
public boolean serverPresent = false;
|
||||
public HashMap<String, String> modSourceList = new HashMap<String, String>();
|
||||
|
||||
public static int posX;
|
||||
public static int posY;
|
||||
@@ -91,29 +98,23 @@ public class mod_Waila {
|
||||
|
||||
@PostInit
|
||||
public void postInit(FMLPostInitializationEvent event) {
|
||||
/* This section is about preloading the name/modid of the items in a table, so we don't look it up at every cycle */
|
||||
|
||||
NBTTagList itemDataList = new NBTTagList();
|
||||
GameData.writeItemData(itemDataList);
|
||||
|
||||
for(int i = 0; i < itemDataList.tagCount(); i++)
|
||||
{
|
||||
ItemData itemData = new ItemData((NBTTagCompound) itemDataList.tagAt(i));
|
||||
this.itemMap.put(itemData.getItemId(), itemData.getModId());
|
||||
}
|
||||
|
||||
for (ModContainer mod : Loader.instance().getModList()) {
|
||||
this.modSourceList.put(mod.getSource().getName(), mod.getName());
|
||||
//log.log(Level.INFO, String.format("%s %s", mod.getSource().getName(), mod.getName()));
|
||||
}
|
||||
ModIdentification.init();
|
||||
|
||||
// Code to retrieve all the registered keybindings along with the mod adding them.
|
||||
Field keyHandlers_Field = AccessHelper.getDeclaredField("cpw.mods.fml.client.registry.KeyBindingRegistry", "keyHandlers");
|
||||
try {
|
||||
Set<KeyHandler> keyHandlers = (Set<KeyHandler>)keyHandlers_Field.get(KeyBindingRegistry.instance());
|
||||
for (KeyHandler keyhandler : keyHandlers)
|
||||
for (int i = 0; i < keyhandler.getKeyBindings().length; i++)
|
||||
log.log(Level.INFO, String.format("%s %s", ModIdentification.idFromObject(keyhandler), keyhandler.getKeyBindings()[i].keyDescription));
|
||||
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
this.modSourceList.put("1.6.2.jar", "Minecraft");
|
||||
this.modSourceList.put("1.6.3.jar", "Minecraft");
|
||||
this.modSourceList.put("1.6.4.jar", "Minecraft");
|
||||
|
||||
// for (String s : this.modSourceList.keySet())
|
||||
// if (this.modSourceList.get(s) == "Minecraft Coder Pack")
|
||||
// this.modSourceList.put(s, "Minecraft");
|
||||
|
||||
}
|
||||
|
||||
@@ -164,28 +165,6 @@ public class mod_Waila {
|
||||
|
||||
}
|
||||
|
||||
public String getModName(ItemStack itemstack){
|
||||
try{
|
||||
String modID = this.itemMap.get(itemstack.itemID);
|
||||
ModContainer mod = CommonUtils.findModContainer(modID);
|
||||
String modname = mod == null ? modID : mod.getName();
|
||||
return modname;
|
||||
} catch (NullPointerException e){
|
||||
//System.out.printf("NPE : %s\n",itemstack.toString());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public String getModID(ItemStack itemstack){
|
||||
try{
|
||||
String modID = this.itemMap.get(itemstack.itemID);
|
||||
return modID;
|
||||
} catch (NullPointerException e){
|
||||
//System.out.printf("NPE : %s\n",itemstack.toString());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateColors(){
|
||||
alpha = (int)(ConfigHandler.instance().getConfigInt(Constants.CFG_WAILA_ALPHA) / 100.0f * 256) << 24;
|
||||
bgcolor = alpha + ConfigHandler.instance().getConfigInt(Constants.CFG_WAILA_BGCOLOR);
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package mcp.mobius.waila.tools;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import mcp.mobius.waila.mod_Waila;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.CraftingManager;
|
||||
import net.minecraft.item.crafting.FurnaceRecipes;
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
|
||||
public class AccessHelper {
|
||||
public static Field getDeclaredField(String classname, String fieldname){
|
||||
|
||||
try {
|
||||
Class class_ = Class.forName(classname);
|
||||
Field field_ = class_.getDeclaredField(fieldname);
|
||||
field_.setAccessible(true);
|
||||
mod_Waila.log.fine(String.format("++ Found field %s %s\n", classname, fieldname));
|
||||
return field_;
|
||||
}
|
||||
catch (NoSuchFieldException e) {
|
||||
mod_Waila.log.warning(String.format("== Field %s %s not found !\n", classname, fieldname));
|
||||
return null;
|
||||
}
|
||||
catch (SecurityException e) {
|
||||
mod_Waila.log.warning(String.format("== Field %s %s security exception !\n", classname, fieldname));
|
||||
return null;
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
mod_Waila.log.warning(String.format("== Class %s not found !\n", classname));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Block getBlock(String classname, String fieldname){
|
||||
Field field_ = getDeclaredField(classname, fieldname);
|
||||
try{
|
||||
return (Block)field_.get(Block.class);
|
||||
} catch (Exception e) {
|
||||
System.out.printf("%s\n", e);
|
||||
mod_Waila.log.warning(String.format("== ERROR GETTING BLOCK %s %s\n", classname, fieldname));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Item getItem(String classname, String fieldname){
|
||||
Field field_ = getDeclaredField(classname, fieldname);
|
||||
try{
|
||||
return (Item)field_.get(Item.class);
|
||||
} catch (Exception e) {
|
||||
System.out.printf("%s\n", e);
|
||||
mod_Waila.log.warning(String.format("== ERROR GETTING ITEM %s %s\n", classname, fieldname));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ArrayList<IRecipe> getCraftingRecipes(ItemStack stack){
|
||||
ArrayList<IRecipe> recipes = new ArrayList<IRecipe>();
|
||||
|
||||
for (IRecipe recipe : (ArrayList<IRecipe>)CraftingManager.getInstance().getRecipeList()){
|
||||
if (recipe != null && recipe.getRecipeOutput() != null){
|
||||
if (recipe.getRecipeOutput().isItemEqual(stack))
|
||||
recipes.add(recipe);
|
||||
}
|
||||
}
|
||||
|
||||
return recipes;
|
||||
}
|
||||
|
||||
public static void cleanCraftingRecipes(ItemStack stack){
|
||||
for (IRecipe recipe: getCraftingRecipes(stack)){
|
||||
CraftingManager.getInstance().getRecipeList().remove(recipe);
|
||||
}
|
||||
}
|
||||
|
||||
public static void cleanSmeltingRecipes(ItemStack stack){
|
||||
Map smeltingList = FurnaceRecipes.smelting().getSmeltingList();
|
||||
Map<List<Integer>, ItemStack> smeltingListMeta = FurnaceRecipes.smelting().getMetaSmeltingList();
|
||||
|
||||
HashMap<Integer, ItemStack> matchingRecipes = new HashMap<Integer, ItemStack>();
|
||||
HashMap<List<Integer>, ItemStack> matchingRecipesMeta = new HashMap<List<Integer>, ItemStack>();
|
||||
|
||||
// We walk the none meta list to find all the recipes
|
||||
for (Object id_ : smeltingList.keySet()){
|
||||
Integer id = (Integer)id_;
|
||||
ItemStack result = (ItemStack)smeltingList.get(id_);
|
||||
|
||||
if (stack.isItemEqual(result))
|
||||
matchingRecipes.put(id, result);
|
||||
}
|
||||
|
||||
for (List<Integer> key : smeltingListMeta.keySet()){
|
||||
ItemStack result = smeltingListMeta.get(key);
|
||||
|
||||
if (stack.isItemEqual(result))
|
||||
matchingRecipesMeta.put(key, result);
|
||||
}
|
||||
|
||||
for (Integer key : matchingRecipes.keySet())
|
||||
FurnaceRecipes.smelting().getSmeltingList().remove(key);
|
||||
|
||||
for (List<Integer> key : matchingRecipesMeta.keySet())
|
||||
FurnaceRecipes.smelting().getMetaSmeltingList().remove(key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package mcp.mobius.waila.tools;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import codechicken.core.CommonUtils;
|
||||
import cpw.mods.fml.common.Loader;
|
||||
import cpw.mods.fml.common.ModContainer;
|
||||
import cpw.mods.fml.common.registry.GameData;
|
||||
import cpw.mods.fml.common.registry.ItemData;
|
||||
import mcp.mobius.waila.mod_Waila;
|
||||
import net.minecraft.enchantment.Enchantment;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
public class ModIdentification {
|
||||
|
||||
public static HashMap<String, String> modSource_Name = new HashMap<String, String>();
|
||||
public static HashMap<String, String> modSource_ID = new HashMap<String, String>();
|
||||
public static HashMap<Integer, String> itemMap = new HashMap<Integer, String>();
|
||||
|
||||
public static void init(){
|
||||
|
||||
NBTTagList itemDataList = new NBTTagList();
|
||||
GameData.writeItemData(itemDataList);
|
||||
|
||||
for(int i = 0; i < itemDataList.tagCount(); i++)
|
||||
{
|
||||
ItemData itemData = new ItemData((NBTTagCompound) itemDataList.tagAt(i));
|
||||
itemMap.put(itemData.getItemId(), itemData.getModId());
|
||||
}
|
||||
|
||||
for (ModContainer mod : Loader.instance().getModList()){
|
||||
modSource_Name.put(mod.getSource().getName(), mod.getName());
|
||||
modSource_ID.put(mod.getSource().getName(), mod.getModId());
|
||||
}
|
||||
|
||||
modSource_Name.put("1.6.2.jar", "Minecraft");
|
||||
modSource_Name.put("1.6.3.jar", "Minecraft");
|
||||
modSource_Name.put("1.6.4.jar", "Minecraft");
|
||||
modSource_ID.put("1.6.2.jar", "Minecraft");
|
||||
modSource_ID.put("1.6.3.jar", "Minecraft");
|
||||
modSource_ID.put("1.6.4.jar", "Minecraft");
|
||||
|
||||
|
||||
// for (String s : this.modSourceList.keySet())
|
||||
// if (this.modSourceList.get(s) == "Minecraft Coder Pack")
|
||||
// this.modSourceList.put(s, "Minecraft");
|
||||
|
||||
}
|
||||
|
||||
public static String nameFromObject(Object obj){
|
||||
String objPath = obj.getClass().getProtectionDomain().getCodeSource().getLocation().toString();
|
||||
|
||||
try {
|
||||
objPath = URLDecoder.decode(objPath, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String modName = "<Unknown>";
|
||||
for (String s: modSource_Name.keySet())
|
||||
if (objPath.contains(s))
|
||||
modName = modSource_Name.get(s);
|
||||
|
||||
if (modName.equals("Minecraft Coder Pack"))
|
||||
modName = "Minecraft";
|
||||
|
||||
return modName;
|
||||
}
|
||||
|
||||
public static String idFromObject(Object obj){
|
||||
String objPath = obj.getClass().getProtectionDomain().getCodeSource().getLocation().toString();
|
||||
|
||||
try {
|
||||
objPath = URLDecoder.decode(objPath, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
String modName = "<Unknown>";
|
||||
for (String s: modSource_ID.keySet())
|
||||
if (objPath.contains(s))
|
||||
modName = modSource_ID.get(s);
|
||||
|
||||
if (modName.equals("Minecraft Coder Pack"))
|
||||
modName = "Minecraft";
|
||||
|
||||
if (modName.equals("<Unknown>"))
|
||||
mod_Waila.log.log(Level.INFO, String.format("%s %s", obj, objPath));
|
||||
|
||||
return modName;
|
||||
}
|
||||
|
||||
public static String nameFromStack(ItemStack itemstack){
|
||||
try{
|
||||
String modID = itemMap.get(itemstack.itemID);
|
||||
ModContainer mod = CommonUtils.findModContainer(modID);
|
||||
String modname = mod == null ? modID : mod.getName();
|
||||
return modname;
|
||||
} catch (NullPointerException e){
|
||||
//System.out.printf("NPE : %s\n",itemstack.toString());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String idFromStack(ItemStack itemstack){
|
||||
try{
|
||||
String modID = itemMap.get(itemstack.itemID);
|
||||
return modID;
|
||||
} catch (NullPointerException e){
|
||||
//System.out.printf("NPE : %s\n",itemstack.toString());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user