|
@@ -1,27 +1,36 @@
|
|
|
package me.kcm.events;
|
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
import me.kcm.command.ModServerCommandManager;
|
|
|
import net.minecraft.block.Block;
|
|
|
-import net.minecraft.command.ServerCommandManager;
|
|
|
import net.minecraft.entity.Entity;
|
|
|
-import net.minecraft.entity.EntityLivingBase;
|
|
|
+import net.minecraft.entity.player.EntityPlayer;
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
+import net.minecraft.inventory.Container;
|
|
|
+import net.minecraft.inventory.ContainerPlayer;
|
|
|
+import net.minecraft.inventory.ContainerWorkbench;
|
|
|
+import net.minecraft.inventory.InventoryCrafting;
|
|
|
+import net.minecraft.item.crafting.CraftingManager;
|
|
|
+import net.minecraft.item.crafting.IRecipe;
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
-import net.minecraft.util.DamageSource;
|
|
|
+import net.minecraft.server.management.PlayerList;
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
import net.minecraft.world.World;
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
+import net.minecraftforge.fml.relauncher.ReflectionHelper;
|
|
|
|
|
|
public class Hooks
|
|
|
{
|
|
|
- public static void onDamageCalculation(EntityLivingBase liv, DamageSource ds, float damage)
|
|
|
+ /*public static void onDamageCalculation(EntityLivingBase liv, DamageSource ds, float damage)
|
|
|
{
|
|
|
if(!liv.isEntityInvulnerable(ds))
|
|
|
{
|
|
|
MinecraftForge.EVENT_BUS.post(new LivingDamageCalculationEvent(liv, ds, damage));
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
public static ITextComponent onGetTabListDisplayName(EntityPlayerMP p)
|
|
|
{
|
|
@@ -49,4 +58,87 @@ public class Hooks
|
|
|
{
|
|
|
return new ModServerCommandManager(server);
|
|
|
}
|
|
|
+
|
|
|
+ public static IRecipe onfindMatchingRecipe(InventoryCrafting craftMatrix, World w)
|
|
|
+ {
|
|
|
+ for(IRecipe irecipe : CraftingManager.REGISTRY)
|
|
|
+ {
|
|
|
+ if(irecipe.matches(craftMatrix, w))
|
|
|
+ {
|
|
|
+ Container c = getContainerEventHandler(craftMatrix);
|
|
|
+ EntityPlayer p;
|
|
|
+ if(c == null)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ else if(c instanceof ContainerPlayer)
|
|
|
+ {
|
|
|
+ p = getContainerPlayer((ContainerPlayer) c);
|
|
|
+ }
|
|
|
+ else if(c instanceof ContainerWorkbench)
|
|
|
+ {
|
|
|
+ p = getContainerEventHandler((ContainerWorkbench) c);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ System.out.println(p);
|
|
|
+ RecipeChosenEvent e = new RecipeChosenEvent(p, irecipe);
|
|
|
+ if(MinecraftForge.EVENT_BUS.post(e))
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return e.getRecipe();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Field getField(Class c, String... field)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ return ReflectionHelper.findField(c, field);
|
|
|
+ }
|
|
|
+ catch(SecurityException | ReflectionHelper.UnableToFindFieldException ex)
|
|
|
+ {
|
|
|
+ System.out.println(String.join(", ", field) + " - " + ex);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static <T> T getFieldValue(Class<T> cast, Object o, Field f)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ return (T) f.get(o);
|
|
|
+ }
|
|
|
+ catch(SecurityException | IllegalAccessException | IllegalArgumentException ex)
|
|
|
+ {
|
|
|
+ System.out.println(f + " - " + ex);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private final static Field CONTAINER_EVENT_HANDLER = getField(InventoryCrafting.class, "field_70465_c", "eventHandler");
|
|
|
+
|
|
|
+ public static Container getContainerEventHandler(InventoryCrafting craftMatrix)
|
|
|
+ {
|
|
|
+ return getFieldValue(Container.class, craftMatrix, CONTAINER_EVENT_HANDLER);
|
|
|
+ }
|
|
|
+
|
|
|
+ private final static Field CONTAINER_PLAYER = getField(ContainerPlayer.class, "field_82862_h", "player");
|
|
|
+
|
|
|
+ public static EntityPlayer getContainerPlayer(ContainerPlayer cp)
|
|
|
+ {
|
|
|
+ return getFieldValue(EntityPlayer.class, cp, CONTAINER_PLAYER);
|
|
|
+ }
|
|
|
+
|
|
|
+ private final static Field CONTAINER_WORKBENCH = getField(ContainerWorkbench.class, "field_192390_i", "player");
|
|
|
+
|
|
|
+ public static EntityPlayer getContainerEventHandler(ContainerWorkbench cw)
|
|
|
+ {
|
|
|
+ return getFieldValue(EntityPlayer.class, cw, CONTAINER_WORKBENCH);
|
|
|
+ }
|
|
|
}
|