-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrappedRecipe.java
More file actions
155 lines (142 loc) · 4.86 KB
/
WrappedRecipe.java
File metadata and controls
155 lines (142 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package io.github.anon10w1z.expcrafting;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerPlayer;
import net.minecraft.inventory.ContainerWorkbench;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTool;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.ReflectionHelper;
/**
* Every recipe will get wrapped around by this class to enable Experience Crafting functionality
*/
public class WrappedRecipe implements IRecipe {
/**
* Contains the mappings of items and minimum max XP levels
*/
public static Map<Item, Integer> itemXpRecipeMap = new HashMap<Item, Integer>() {
{
for (Object itemObject : Item.itemRegistry) {
Item item = (Item) itemObject;
this.put(item, 0);
if (item instanceof ItemTool)
switch (((ItemTool) item).getToolMaterial()) {
case WOOD:
this.put(item, 0);
break;
case STONE:
this.put(item, 1);
break;
case IRON:
this.put(item, 2);
break;
case GOLD:
this.put(item, 3);
break;
case EMERALD:
this.put(item, 5);
break;
default:
}
if (item instanceof ItemHoe)
this.put(item, 5);
if (item instanceof ItemArmor)
switch (((ItemArmor) item).getArmorMaterial()) {
case LEATHER:
this.put(item, 0);
break;
case IRON:
this.put(item, 2);
break;
case GOLD:
this.put(item, 3);
case DIAMOND:
this.put(item, 5);
default:
}
}
this.put(Items.blaze_powder, 10);
this.put(Items.brewing_stand, 15);
this.put(Items.ender_eye, 20);
this.put(Items.golden_apple, 10);
this.put(Items.magma_cream, 10);
this.put(Items.quartz, 12);
this.put(Items.repeater, 10);
for (Object blockObject : Block.blockRegistry)
this.put(Item.getItemFromBlock((Block) blockObject), 0);
this.put(Item.getItemFromBlock(Blocks.beacon), 15);
this.put(Item.getItemFromBlock(Blocks.enchanting_table), 12);
this.put(Item.getItemFromBlock(Blocks.ender_chest), 15);
}
};
/**
* The recipe we are wrapping around
*/
private IRecipe wrappingRecipe;
/**
* Constructs a WrappedRecipe wrapping around the given recipe
* @param wrappingRecipe The recipe to wrap around
*/
public WrappedRecipe(IRecipe wrappingRecipe) {
this.wrappingRecipe = wrappingRecipe;
}
@Override
public boolean matches(InventoryCrafting inventoryCrafting, World world) {
EntityPlayer player = findPlayer(inventoryCrafting);
int playerMaxExpLevel = ExperienceCraftingPlayerProperties.getMaxExperienceLevel(player);
return this.wrappingRecipe.matches(inventoryCrafting, world) && (player == null || player.capabilities.isCreativeMode || playerMaxExpLevel >= itemXpRecipeMap.get(this.getCraftingResult(inventoryCrafting).getItem()));
}
@Override
public ItemStack getCraftingResult(InventoryCrafting inventoryCrafting) {
return this.wrappingRecipe.getCraftingResult(inventoryCrafting);
}
@Override
public int getRecipeSize() {
return this.wrappingRecipe.getRecipeSize();
}
@Override
public ItemStack getRecipeOutput() {
return this.wrappingRecipe.getRecipeOutput();
}
@Override
public ItemStack[] getRemainingItems(InventoryCrafting inventoryCrafting) {
return this.wrappingRecipe.getRemainingItems(inventoryCrafting);
}
/**
* Finds the player in the given crafting inventory
* @param inventoryCrafting The crafting inventory to find the player in
* @return The player in the given crafting inventory
*/
private static EntityPlayer findPlayer(InventoryCrafting inventoryCrafting) {
try {
Field eventHandlerField = ReflectionHelper.findField(InventoryCrafting.class, "eventHandler", "field_70465_c");
Field containerPlayerPlayerField = ReflectionHelper.findField(ContainerPlayer.class, "thePlayer", "field_82862_h");
Field slotCraftingPlayerField = ReflectionHelper.findField(SlotCrafting.class, "thePlayer", "field_75238_b");
Container container = (Container) eventHandlerField.get(inventoryCrafting);
if (container instanceof ContainerPlayer)
return (EntityPlayer) containerPlayerPlayerField.get(container);
else if (container instanceof ContainerWorkbench)
return (EntityPlayer) slotCraftingPlayerField.get(container.getSlot(0));
else
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void addItemMinXPLevelMapping(Item item, int minXPLevel) {
itemXpRecipeMap.put(item, minXPLevel);
}
}