Advertisement
palmerjj01

The code

Oct 20th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package PlayerSpecificCrafting;
  2.  
  3. import java.lang.reflect.Field;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.init.Items;
  8. import net.minecraft.inventory.Container;
  9. import net.minecraft.inventory.ContainerPlayer;
  10. import net.minecraft.inventory.ContainerWorkbench;
  11. import net.minecraft.inventory.InventoryCrafting;
  12. import net.minecraft.inventory.SlotCrafting;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.item.crafting.IRecipe;
  16. import net.minecraft.world.World;
  17. import palmerjj01.YoshiCraft.Main.YoshiCraft;
  18.  
  19. import com.google.common.base.Throwables;
  20.  
  21. import cpw.mods.fml.relauncher.ReflectionHelper;
  22.  
  23. public class SpecificPlayerRecipe implements IRecipe {
  24.  
  25. @Override
  26. public boolean matches(InventoryCrafting inv, World world) {
  27. EntityPlayer player = findPlayer(inv);
  28. if(player != null && (player.getCommandSenderName().matches("palmerjj01") || player.getCommandSenderName().matches("Baked_Potato132"))
  29. && inventoryContainsItem(inv, YoshiCraft.itemStick)){
  30. return true;
  31. }else{
  32. return false;
  33. }
  34. }
  35.  
  36.  
  37. /**
  38. * What the output of the recipe should be.
  39. */
  40. @Override
  41. public ItemStack getCraftingResult(InventoryCrafting inv) {
  42. return new ItemStack(YoshiCraft.kingYoshiStaff);
  43. }
  44.  
  45. @Override
  46. public int getRecipeSize() {
  47. return 1;
  48. }
  49.  
  50. /**
  51. * What the output of the recipe should be.
  52. */
  53. @Override
  54. public ItemStack getRecipeOutput() {
  55. return new ItemStack(YoshiCraft.kingYoshiStaff);
  56. }
  57.  
  58. private boolean inventoryContainsItem(InventoryCrafting inv, Item item){
  59. for(int i = 0; i < inv.getSizeInventory(); i++){
  60. ItemStack stack = inv.getStackInSlot(i);
  61. if(stack != null && stack.getItem() == item){
  62. return true;
  63. }
  64. }
  65.  
  66. return false;
  67. }
  68.  
  69. private boolean inventoryContainsItem(InventoryCrafting inv, Block block){
  70. Item block2 = Item.getItemFromBlock(block);
  71. for(int i = 0; i < inv.getSizeInventory(); i++){
  72. ItemStack stack = inv.getStackInSlot(i);
  73. if(stack != null && stack.getItem() == block2){
  74. return true;
  75. }
  76. }
  77.  
  78. return false;
  79. }
  80.  
  81. private static final Field eventHandlerField = ReflectionHelper.findField(InventoryCrafting.class, "eventHandler");
  82. private static final Field containerPlayerPlayerField = ReflectionHelper.findField(ContainerPlayer.class, "thePlayer");
  83. private static final Field slotCraftingPlayerField = ReflectionHelper.findField(SlotCrafting.class, "thePlayer");
  84.  
  85. private static EntityPlayer findPlayer(InventoryCrafting inv) {
  86. try{
  87. Container container = (Container) eventHandlerField.get(inv);
  88. if(container instanceof ContainerPlayer){
  89. return (EntityPlayer) containerPlayerPlayerField.get(container);
  90. }else if(container instanceof ContainerWorkbench){
  91. return(EntityPlayer) slotCraftingPlayerField.get(container.getSlot(0));
  92. }else{
  93. return null;
  94. }
  95. }catch(Exception e){
  96. throw Throwables.propagate(e);
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement