-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConductEventListener.java
251 lines (206 loc) · 7.54 KB
/
ConductEventListener.java
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package conducts;
import conducts.client.GuiGameOverConducts;
import cpw.mods.fml.common.ICraftingHandler;
import net.minecraft.block.Block;
import net.minecraft.client.gui.GuiGameOver;
import net.minecraft.client.gui.achievement.GuiAchievement;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.ShapelessOreRecipe;
public class ConductEventListener implements ICraftingHandler {
/**
* Forge event that fires when the player uses an item. Currently we are using this to detect food eating.
*
* TODO: detect food actually being consumed instead of just clicked
*
* @param event
*/
@ForgeSubscribe
public void interact(PlayerInteractEvent event)
{
if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR) {
if (isItemUsedInRecipeFor(new ItemStack(Item.book), event.entityPlayer.getCurrentEquippedItem()) || isItemUsedInRecipeFor(new ItemStack(Item.paper), event.entityPlayer.getCurrentEquippedItem())) {
event.entityPlayer.triggerAchievement(ConductsPage.illiterate);
}
}
if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK && event.entity.worldObj.getBlockId(event.x, event.y, event.z) == Block.cake.blockID) {
// hardcoded cake detector
event.entityPlayer.triggerAchievement(ConductsPage.breatharian);
event.entityPlayer.triggerAchievement(ConductsPage.vegan);
event.entityPlayer.triggerAchievement(ConductsPage.carnivore);
}
}
/**
* Helper method used to find forbidden items. Checks the recipe used to make an item for the taboo item.
*
* @param checkFor
* @param checking
* @return
*/
public static boolean isItemUsedInRecipeFor(ItemStack checkFor, ItemStack checking)
{
//System.out.println("I am looking for a recipe for " + checking);
for (Object maybeARecipe : CraftingManager.getInstance().getRecipeList()) {
if (maybeARecipe instanceof IRecipe) {
IRecipe iRecipe = (IRecipe)maybeARecipe;
if (checking != null && iRecipe.getRecipeOutput() != null && iRecipe.getRecipeOutput().isItemEqual(checking))
{
//System.out.println("I found a recipe for your current item. It is " + iRecipe);
// shaped recipe checker
if (maybeARecipe instanceof ShapedRecipes)
{
ShapedRecipes recipe = (ShapedRecipes)maybeARecipe;
for (ItemStack input : recipe.recipeItems) {
if (input != null && input.isItemEqual(checkFor))
{
return true;
}
}
}
// shapeless checker
if (maybeARecipe instanceof ShapelessRecipes)
{
ShapelessRecipes recipe = (ShapelessRecipes)maybeARecipe;
for (Object input : recipe.recipeItems) {
System.out.println("Matching recipe contains " + ((ItemStack)input).getDisplayName());
if (((ItemStack)input).isItemEqual(checkFor))
{
return true;
}
}
}
// shapeless ore checker
if (maybeARecipe instanceof ShapelessOreRecipe)
{
ShapelessOreRecipe recipe = (ShapelessOreRecipe)maybeARecipe;
for (Object input : recipe.getInput()) {
System.out.println("Matching recipe contains " + input);
if (input instanceof ItemStack && ((ItemStack)input).isItemEqual(checkFor))
{
return true;
}
if (input instanceof Item && checkFor.itemID == ((Item)input).itemID)
{
return true;
}
}
}
}
}
}
return false;
}
/**
* Forge event that fires when an entity gets hurt. Used for the pacifist tree conducts
*
* @param event
*/
@ForgeSubscribe
public void entityHurts(LivingHurtEvent event)
{
if (event.source.getSourceOfDamage() instanceof EntityPlayer)
{
// discover the cause of the damage
EntityPlayer cause = (EntityPlayer)(event.source.getSourceOfDamage());
cause.triggerAchievement(ConductsPage.pacifist);
// check what the player is holding
if (cause.getCurrentEquippedItem() != null) {
Item itemUsed = cause.getCurrentEquippedItem().getItem();
if (itemUsed instanceof ItemSword || itemUsed instanceof ItemTool) {
cause.triggerAchievement(ConductsPage.neverSword);
}
}
if (event.source.isProjectile())
{
cause.triggerAchievement(ConductsPage.neverRange);
}
}
}
/**
* Forge event that fires when an entity dies. Used for the no-kill conducts
*
* @param event
*/
@ForgeSubscribe
public void entityDies(LivingDeathEvent event)
{
if (event.source.getSourceOfDamage() instanceof EntityPlayer)
{
// discover the cause of the death
EntityPlayer cause = (EntityPlayer)(event.source.getSourceOfDamage());
if (event.entityLiving instanceof EntityAnimal)
{
cause.triggerAchievement(ConductsPage.noKillAnimal);
}
if (event.entityLiving instanceof IMob)
{
cause.triggerAchievement(ConductsPage.noKillMob);
}
if (event.entityLiving instanceof EntityPlayer)
{
cause.triggerAchievement(ConductsPage.noKillPlayers);
}
}
}
/**
* Forge event that fires when the player is about to break a block. Used for the block breaking conducts.
*
* @param event
*/
@ForgeSubscribe
public void blockBroken(BreakEvent event) {
if (event.block != null) {
event.getPlayer().triggerAchievement(ConductsPage.noHarvest);
}
if (OreDictionary.getOreID(new ItemStack(event.block, 1, event.blockMetadata)) == OreDictionary.getOreID("logWood")) {
event.getPlayer().triggerAchievement(ConductsPage.noHarvest);
event.getPlayer().triggerAchievement(ConductsPage.noTree);
}
if (OreDictionary.getOreID(new ItemStack(event.block, 1, event.blockMetadata)) == OreDictionary.getOreID("stone")) {
event.getPlayer().triggerAchievement(ConductsPage.noHarvest);
event.getPlayer().triggerAchievement(ConductsPage.noStone);
}
}
/**
* Handler that triggers when the player crafts an item. We currently check for taboo items for the illiterate conduct
*/
@Override
public void onCrafting(EntityPlayer player, ItemStack item, IInventory craftMatrix) {
if (isItemUsedInRecipeFor(new ItemStack(Item.book), item) || isItemUsedInRecipeFor(new ItemStack(Item.paper), item)) {
player.triggerAchievement(ConductsPage.illiterate);
}
}
@Override
public void onSmelting(EntityPlayer player, ItemStack item) {
// not used
}
/**
* Look for the "You Died!" screen so that we can put the achievements button on there.
*/
@ForgeSubscribe
public void guiScreenListener(GuiOpenEvent event) {
if (event.gui instanceof GuiGameOver && !(event.gui instanceof GuiGameOverConducts)) {
event.gui = new GuiGameOverConducts();
}
}
}