gt22

AdvThaumApi

Jun 5th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.51 KB | None | 0 0
  1. package com.gt22.elementalmagic.api;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;
  7.  
  8. import net.minecraft.block.Block;
  9. import net.minecraft.client.Minecraft;
  10. import net.minecraft.entity.EntityLivingBase;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.entity.player.EntityPlayerMP;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.world.World;
  15. import thaumcraft.api.ThaumcraftApiHelper;
  16. import thaumcraft.api.aspects.Aspect;
  17. import thaumcraft.api.aspects.AspectList;
  18. import thaumcraft.api.wands.ItemFocusBasic;
  19. import cpw.mods.fml.common.network.simpleimpl.IMessage;
  20. import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
  21.  
  22. public class AdvThaumApi {
  23.    
  24.     /**
  25.      * Return aspect list of primals with the given amount
  26.      * @param amount
  27.      * @return
  28.      */
  29.     public static AspectList getPrimals(int amount)
  30.     {
  31.         return new AspectList().add(Aspect.AIR, amount).add(Aspect.EARTH, amount).add(Aspect.FIRE, amount).add(Aspect.WATER, amount).add(Aspect.ORDER, amount).add(Aspect.ENTROPY, amount);
  32.     }
  33.    
  34.     /**
  35.      * Works just like {@link getPrimas()}, but returns only elemental primals (Aer, terra, ignis, aqua). Used in ElementalMagic
  36.      * @param amount
  37.      * @return
  38.      */
  39.     public static AspectList getElementas(int amount)
  40.     {
  41.         return new AspectList().add(Aspect.AIR, amount).add(Aspect.EARTH, amount).add(Aspect.FIRE, amount).add(Aspect.WATER, amount);
  42.     }
  43.    
  44.     /**
  45.      * Return aspect list of primals getted by splitting another aspect list
  46.      * @param al List to reduce
  47.      * @param merge If true will use <code>merge</code> method instead of <code>add</code>, this method use only highrs of the amounts. <code>{@link AspectList#merge(Aspect, int)}</code>
  48.      * @return AspectList of primal aspects
  49.      */
  50.     public static AspectList reduceToPrimals(AspectList al, boolean merge)
  51.       {
  52.         AspectList out = new AspectList();
  53.         for (Aspect aspect : al.getAspects()) {
  54.           if (aspect != null) {
  55.             if (aspect.isPrimal())
  56.             {
  57.               if (merge) {
  58.                 out.merge(aspect, al.getAmount(aspect));
  59.               } else {
  60.                 out.add(aspect, al.getAmount(aspect));
  61.               }
  62.             }
  63.             else
  64.             {
  65.               AspectList send = new AspectList();
  66.               send.add(aspect.getComponents()[0], al.getAmount(aspect));
  67.               send.add(aspect.getComponents()[1], al.getAmount(aspect));
  68.               send = reduceToPrimals(send, merge);
  69.               for (Aspect a : send.getAspects()) {
  70.                 if (merge) {
  71.                   out.merge(a, send.getAmount(a));
  72.                 } else {
  73.                   out.add(a, send.getAmount(a));
  74.                 }
  75.               }
  76.             }
  77.           }
  78.         }
  79.         return out;
  80.       }
  81.    
  82.     private static final String wandpath = "thaumcraft.common.items.wands.ItemWandCasting";
  83.    
  84.     /**
  85.      * Add an research points to player, also render visual effect (Aspect flying to thaumonomicon)
  86.      * @param player to give research
  87.      * @param aspect to give
  88.      * @param amount to give, can be negative
  89.      * @return true if aspect was succesfully added/removed
  90.      */
  91.     public boolean giveAspect(EntityPlayer player, Aspect aspect, short amount)
  92.     {
  93.         try
  94.         {
  95.             SimpleNetworkWrapper instance = (SimpleNetworkWrapper) Class.forName("thaumcraft.common.lib.network.PacketHandler").getField("INSTANCE").get(this);
  96.             Field proxy = Class.forName("thaumcraft.common.Thaumcraft").getField("proxy");
  97.             Class <? extends IMessage> pap = (Class<? extends IMessage>) Class.forName("thaumcraft.common.lib.network.playerdata.PacketAspectPool");
  98.             Constructor <? extends IMessage> conspap = pap.getConstructor(String.class, Short.class, Short.class);
  99.             AspectList al = ThaumcraftApiHelper.getDiscoveredAspects(player.getCommandSenderName());
  100.             short value;
  101.             if(al != null)
  102.             {
  103.                 value = (short) al.getAmount(aspect);
  104.             }
  105.             else
  106.             {
  107.                 value = 0;
  108.             }
  109.             instance.sendTo(conspap.newInstance(aspect.getTag(), Short.valueOf((short) 1), Short.valueOf(value)), (EntityPlayerMP)player);
  110.             Class core = proxy.getDeclaringClass().getClass();
  111.             Field playerKnowledge = proxy.get(proxy.get(core)).getClass().getField("playerKnowledge");
  112.             Class  playerK = Class.forName("thaumcraft.common.lib.research.PlayerKnowledge");
  113.             Object pk = playerKnowledge.get(proxy.get(core));
  114.             Class[] params =
  115.             {
  116.                 String.class,
  117.                 Aspect.class,
  118.                 short.class,
  119.             };
  120.             Method add = pk.getClass().getMethod("addAspectPool", params);
  121.             return (boolean) add.invoke(pk, player.getCommandSenderName(), aspect, amount);
  122.         }
  123.         catch (Exception e)
  124.         {
  125.             e.printStackTrace();
  126.         }
  127.         return false;
  128.     }
  129.    
  130.     /**
  131.      * Play visual warp effects for player
  132.      * @param player to play warp effects
  133.      */
  134.     public void playWarpEffects(EntityPlayer player)
  135.     {
  136.         try
  137.         {
  138.             SimpleNetworkWrapper instance = (SimpleNetworkWrapper) Class.forName("thaumcraft.common.lib.network.PacketHandler").getField("INSTANCE").get(this);
  139.             Class <? extends IMessage> pmc = (Class<? extends IMessage>) Class.forName("thaumcraft.common.lib.network.misc.PacketMiscEvent");
  140.             Constructor <? extends IMessage> conspmc = pmc.getConstructor(short.class);
  141.             instance.sendTo(conspmc.newInstance((short)0), (EntityPlayerMP)player);
  142.         }
  143.         catch (Exception e)
  144.         {
  145.             System.out.println("Unable to play warp effects");
  146.             e.printStackTrace();
  147.         }
  148.     }
  149.    
  150.     /**
  151.      * Complite resesarch for player
  152.      * @param player to complite
  153.      * @param research Research key
  154.      */
  155.     public void compliteResearch(EntityPlayer player, String research)
  156.     {
  157.         try {
  158.                 SimpleNetworkWrapper instance = (SimpleNetworkWrapper) Class.forName("thaumcraft.common.lib.network.PacketHandler").getField("INSTANCE").get(this);
  159.                 Field proxy = Class.forName("thaumcraft.common.Thaumcraft").getField("proxy");
  160.                 Class <? extends IMessage> prc = (Class<? extends IMessage>) Class.forName("thaumcraft.common.lib.network.playerdata.PacketResearchComplete");
  161.                 Constructor <? extends IMessage> consprc = prc.getConstructor(String.class);
  162.                 instance.sendTo(consprc.newInstance(research), (EntityPlayerMP)player);
  163.                 Class clientproxy = Class.forName("thaumcraft.client.ClientProxy");
  164.                 Field researchManag = proxy.get(clientproxy).getClass().getField("researchManager");
  165.                 Class[] params = new Class[2];
  166.                 params[0] = EntityPlayerMP.class;
  167.                 params[1] = String.class;
  168.                 Class resMan = Class.forName("thaumcraft.common.lib.research.ResearchManager");
  169.                 Object resm = researchManag.get(clientproxy);
  170.                 resm.getClass().getMethod("completeResearch", params).invoke(resm, (EntityPlayerMP)player, research);
  171.         } catch (Exception e) {
  172.             System.out.println("Unable to give research");
  173.             e.printStackTrace();
  174.             //Minecraft.getMinecraft().shutdown();
  175.         }
  176.        
  177.        
  178.     }
  179.    
  180.     /**
  181.      * Set vis in wand
  182.      * @param wand to set
  183.      * @param aspects to set
  184.      */
  185.     public static void setVis(ItemStack wand, AspectList aspects)
  186.     {
  187.         Class wandclass = null;
  188.         try {
  189.             wandclass = Class.forName(wandpath);
  190.         } catch (ClassNotFoundException e1) {
  191.             System.out.println("Unable to find wand class from thaumcraft");
  192.             e1.printStackTrace();
  193.             Minecraft.getMinecraft().shutdown();
  194.         }
  195.         Method set = null;
  196.         try {
  197.             Class[] params = new Class[2];
  198.             params[0] = ItemStack.class;
  199.             params[1] = AspectList.class;
  200.             set = wandclass.getMethod("storeAllVis", params);
  201.         } catch (NoSuchMethodException | SecurityException e) {
  202.             System.out.println("Unable to find storeAllVis method from wand class from thaumcraft");
  203.             e.printStackTrace();
  204.             Minecraft.getMinecraft().shutdown();
  205.         }
  206.         try {
  207.             set.invoke(wandclass.newInstance(), wand, aspects);
  208.         } catch (IllegalAccessException | IllegalArgumentException
  209.                 | InvocationTargetException | InstantiationException e) {
  210.             System.out.println("Unable to invoke storeAllVis method from thaumcraft");
  211.             e.printStackTrace();
  212.             Minecraft.getMinecraft().shutdown();
  213.         }
  214.     }
  215.    
  216.     /**
  217.      * Return amount of passed aspect in wand
  218.      * @param wand
  219.      * @param aspect
  220.      * @return
  221.      */
  222.     public static int getVis(ItemStack wand, Aspect aspect)
  223.     {
  224.         Class wandclass = null;
  225.         try {
  226.             wandclass = Class.forName(wandpath);
  227.         } catch (ClassNotFoundException e1) {
  228.             System.out.println("Unable to find wand class from thaumcraft");
  229.             e1.printStackTrace();
  230.             Minecraft.getMinecraft().shutdown();
  231.         }
  232.         Method get = null;
  233.         try {
  234.             Class[] params = new Class[2];
  235.             params[0] = ItemStack.class;
  236.             params[1] = Aspect.class;
  237.             get = wandclass.getMethod("getVis", params);
  238.         } catch (NoSuchMethodException | SecurityException e) {
  239.             System.out.println("Unable to find getVis method from wand class from thaumcraft");
  240.             e.printStackTrace();
  241.             Minecraft.getMinecraft().shutdown();
  242.         }
  243.         try {
  244.             return (int) get.invoke(wandclass.newInstance(), wand, aspect);
  245.         } catch (IllegalAccessException | IllegalArgumentException
  246.                 | InvocationTargetException | InstantiationException e) {
  247.             System.out.println("Unable to invoke getVis method from thaumcraft");
  248.             e.printStackTrace();
  249.             Minecraft.getMinecraft().shutdown();
  250.         }
  251.         return 0;
  252.     }
  253.    
  254.     /**
  255.      * Add vis in wand
  256.      * @param wand to add
  257.      * @param aspect to add
  258.      * @param amount to add
  259.      * @param insertit If false this only check can it insert
  260.      * @return amount inserted
  261.      */
  262.     public static int insertVis(ItemStack wand, Aspect aspect, int amount, boolean insertit)
  263.     {
  264.         Class wandclass = null;
  265.         try {
  266.             wandclass = Class.forName(wandpath);
  267.         } catch (ClassNotFoundException e1) {
  268.             System.out.println("Unable to find wand class from thaumcraft");
  269.             e1.printStackTrace();
  270.             Minecraft.getMinecraft().shutdown();
  271.         }
  272.         Method insert = null;
  273.         try {
  274.             Class[] params = new Class[4];
  275.             params[0] = ItemStack.class;
  276.             params[1] = Aspect.class;
  277.             params[2] = int.class;
  278.             params[3] = boolean.class;
  279.             insert = wandclass.getMethod("addVis", params);
  280.         } catch (NoSuchMethodException | SecurityException e) {
  281.             System.out.println("Unable to find addVis method from wand class from thaumcraft");
  282.             e.printStackTrace();
  283.             Minecraft.getMinecraft().shutdown();
  284.         }
  285.         try {
  286.             return (int) insert.invoke(wandclass.newInstance(), wand, aspect, amount, insertit);
  287.         } catch (IllegalAccessException | IllegalArgumentException
  288.                 | InvocationTargetException | InstantiationException e) {
  289.             System.out.println("Unable to invoke addVis method from thaumcraft");
  290.             e.printStackTrace();
  291.             Minecraft.getMinecraft().shutdown();
  292.         }
  293.         return 0;
  294.     }
  295.    
  296.     /**
  297.      * Used for getting foci from wand
  298.      * @param wand
  299.      * @return ItemStack of the foci
  300.      */
  301.     public static ItemStack getFocusStack(ItemStack wand)
  302.     {
  303.         Class wandclass = null;
  304.         try {
  305.             wandclass = Class.forName(wandpath);
  306.         } catch (ClassNotFoundException e) {
  307.             System.out.println("Unable to find wand class from thaumcraft");
  308.             e.printStackTrace();
  309.             Minecraft.getMinecraft().shutdown();
  310.         }
  311.         Class param = ItemStack.class;
  312.         Method foci = null;
  313.         try {
  314.             foci = wandclass.getMethod("getFocusItem", param);
  315.         } catch (NoSuchMethodException | SecurityException e) {
  316.             System.out.println("Unable to find getFocusItem method from wand class from thaumcraft");
  317.             e.printStackTrace();
  318.             Minecraft.getMinecraft().shutdown();
  319.         }
  320.        
  321.         try {
  322.             return (ItemStack) foci.invoke(wandclass.newInstance(), wand);
  323.         } catch (IllegalAccessException | IllegalArgumentException
  324.                 | InvocationTargetException | InstantiationException e) {
  325.             System.out.println("Unable to invoke getFocusItem method from thaumcraft");
  326.             e.printStackTrace();
  327.             return null;
  328.         }
  329.     }
  330.    
  331.     /**
  332.      * Get max capcity of wand
  333.      * @param wand to check
  334.      * @return max amount
  335.      */
  336.     public static int getMaxVis(ItemStack wand)
  337.     {
  338.         Class wandclass = null;
  339.         try {
  340.             wandclass = Class.forName(wandpath);
  341.         } catch (ClassNotFoundException e) {
  342.             System.out.println("Unable to find wand class from thaumcraft");
  343.             e.printStackTrace();
  344.             Minecraft.getMinecraft().shutdown();
  345.         }
  346.         Class param = ItemStack.class;
  347.         Method get = null;
  348.         try {
  349.             get = wandclass.getMethod("getMaxVis", param);
  350.         } catch (NoSuchMethodException | SecurityException e) {
  351.             System.out.println("Unable to find getMaxVis method from wand class from thaumcraft");
  352.             e.printStackTrace();
  353.             Minecraft.getMinecraft().shutdown();
  354.         }
  355.        
  356.         try {
  357.             return (int) get.invoke(wandclass.newInstance(), wand);
  358.         } catch (IllegalAccessException | IllegalArgumentException
  359.                 | InvocationTargetException | InstantiationException e) {
  360.             System.out.println("Unable to invoke getMaxVis method from thaumcraft");
  361.             e.printStackTrace();
  362.             return 0;
  363.         }
  364.     }
  365.    
  366.     /**
  367.      * Allowing to get item of the foci. I don't know for what
  368.      * @param wand
  369.      * @return Item of the foci
  370.      */
  371.     public static ItemFocusBasic getFoci(ItemStack wand)
  372.     {
  373.         Class wandclass = null;
  374.         try {
  375.             wandclass = Class.forName(wandpath);
  376.         } catch (ClassNotFoundException e1) {
  377.             System.out.println("Unable to find wand class from thaumcraft");
  378.             e1.printStackTrace();
  379.             Minecraft.getMinecraft().shutdown();
  380.         }
  381.         Method get = null;
  382.         Class params = ItemStack.class;
  383.         try {
  384.             get = wandclass.getMethod("getFocus", params);
  385.         } catch (NoSuchMethodException | SecurityException e) {
  386.             System.out.println("Unable to find getFocus method from wand class from thaumcraft");
  387.             e.printStackTrace();
  388.             Minecraft.getMinecraft().shutdown();
  389.         }
  390.             try {
  391.                 return (ItemFocusBasic) get.invoke(wandclass.newInstance(), wand);
  392.             } catch (IllegalAccessException | IllegalArgumentException
  393.                     | InvocationTargetException | InstantiationException e) {
  394.                 System.out.println("Cannot invoke getFocus method from thaumcraft");
  395.                 e.printStackTrace();
  396.                 Minecraft.getMinecraft().shutdown();
  397.             }
  398.         return null;
  399.     }
  400.    
  401.     /**
  402.      * Used for focies that can work with holding
  403.      * @param entity Player that use foci
  404.      * @param cd Cooldown before use
  405.      */
  406.     public static void setCooldown(EntityLivingBase entity, int cd)
  407.     {
  408.         Class manage = null;
  409.         try {
  410.             manage = Class.forName("thaumcraft.common.items.wands.WandManager");
  411.         } catch (ClassNotFoundException e1) {
  412.             System.out.println("Unable to find wand manager class from thaumcraft");
  413.             e1.printStackTrace();
  414.             Minecraft.getMinecraft().shutdown();
  415.         }
  416.        
  417.         Method setcd = null;
  418.         try {
  419.             Class[] params = new Class[2];
  420.             params[0] = EntityLivingBase.class;
  421.             params[1] = int.class;
  422.             setcd = manage.getMethod("setCooldown", params);
  423.         } catch (NoSuchMethodException | SecurityException e) {
  424.             System.out.println("Unable to find setCooldown method in wand manger class from thaumcraft");
  425.             e.printStackTrace();
  426.             Minecraft.getMinecraft().shutdown();
  427.         }
  428.         if(setcd != null)
  429.         {
  430.             try {
  431.                 setcd.invoke(manage.newInstance(), entity, cd);
  432.             } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
  433.                 System.out.println("Unable to invoke setCooldown method from thaumcraft");
  434.                 e.printStackTrace();
  435.                 Minecraft.getMinecraft().shutdown();
  436.             }
  437.         }
  438.     }
  439.  
  440.     /**
  441.      * Simulating axe of the stream effect
  442.      * @param worldObj
  443.      * @param x
  444.      * @param y
  445.      * @param z
  446.      * @param bi
  447.      * @param player
  448.      * @param b
  449.      * @param i
  450.      */
  451.     public static void breakFurthestBlock(World worldObj, int x, int y, int z,
  452.             Block bi, EntityPlayer player, boolean b, int i) {
  453.        
  454.         Class utils = null;
  455.         try {
  456.             utils = Class.forName("thaumcraft.common.lib.utils.BlockUtils");
  457.         } catch (ClassNotFoundException e1) {
  458.             System.out.println("Unable to find BlockUtils class from thaumcraft");
  459.             e1.printStackTrace();
  460.             Minecraft.getMinecraft().shutdown();
  461.         }
  462.        
  463.         Method breakblock = null;
  464.         try {
  465.             Class[] params = new Class[8];
  466.             params[0] = World.class;
  467.             params[1] = int.class;
  468.             params[2] = int.class;
  469.             params[3] = int.class;
  470.             params[4] = Block.class;
  471.             params[5] = EntityPlayer.class;
  472.             params[6] = boolean.class;
  473.             params[7] = int.class;
  474.             breakblock = utils.getMethod("breakFurthestBlock", params);
  475.         } catch (NoSuchMethodException | SecurityException e) {
  476.             System.out.println("Unable to find breakFurthestBlock method in wand manger class from thaumcraft");
  477.             e.printStackTrace();
  478.             Minecraft.getMinecraft().shutdown();
  479.         }
  480.         if(breakblock != null)
  481.         {
  482.             try {
  483.                 breakblock.invoke(utils.newInstance(), worldObj, x, y, z, bi, player, b, i);
  484.             } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
  485.                 System.out.println("Unable to invoke setCooldown method from thaumcraft");
  486.                 e.printStackTrace();
  487.                 Minecraft.getMinecraft().shutdown();
  488.             }
  489.         }
  490.     }
  491. }
Advertisement
Add Comment
Please, Sign In to add comment