-
Notifications
You must be signed in to change notification settings - Fork 18
/
RecipesEvents.java
102 lines (91 loc) · 3.43 KB
/
RecipesEvents.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
package knightminer.inspirations.recipes;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.common.network.InspirationsNetwork;
import knightminer.inspirations.common.network.MilkablePacket;
import knightminer.inspirations.library.InspirationsRegistry;
import knightminer.inspirations.recipes.tileentity.TileCauldron;
import knightminer.inspirations.shared.SharedEvents;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.passive.SquidEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.DyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.EntityInteract;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.items.ItemHandlerHelper;
public class RecipesEvents {
/**
* Event to handle cauldron clicking.
* Done though an event instead of the block so we can ensure it runs before other cauldron handlers, since we cancel for non-water.
*/
@SubscribeEvent(priority = EventPriority.HIGH)
public static void clickCauldron(RightClickBlock event) {
if(!Config.enableCauldronRecipes()) {
return;
}
PlayerEntity player = event.getEntityPlayer();
if(player.isSneaking()) {
return;
}
// basic properties
World world = event.getWorld();
BlockPos pos = event.getPos();
BlockState state = world.getBlockState(pos);
if(!InspirationsRegistry.isNormalCauldron(state)) {
return;
}
ItemStack stack = event.getItemStack();
if(stack.isEmpty()) {
return;
}
boolean result = TileCauldron.interact(world, pos, state, player, event.getHand());
if(result || InspirationsRegistry.isCauldronBlacklist(stack)) {
event.setCanceled(true);
event.setCancellationResult(ActionResultType.SUCCESS);
}
}
@SubscribeEvent
public static void milkSquid(EntityInteract event) {
if(!Config.milkSquids.get()) {
return;
}
// only care about cows
Entity target = event.getTarget();
if(!(target instanceof SquidEntity)) {
return;
}
// must be holding a glass bottle
PlayerEntity player = event.getEntityPlayer();
ItemStack stack = player.getHeldItem(event.getHand());
if(stack.getItem() == Items.GLASS_BOTTLE) {
// if has tag, cannot be milked
CompoundNBT tags = target.getEntityData();
if (tags.getShort(SharedEvents.TAG_MILKCOOLDOWN) == 0) {
// give ink bottle to player
if (!player.isCreative()) {
stack.shrink(1);
}
ItemHandlerHelper.giveItemToPlayer(player, new ItemStack(InspirationsRecipes.simpleDyedWaterBottle.get(DyeColor.BLACK)), player.inventory.currentItem);
// sound
player.playSound(SoundEvents.ITEM_BOTTLE_FILL_DRAGONBREATH, 1.0F, 1.0F);
// set cooldown
tags.putShort(SharedEvents.TAG_MILKCOOLDOWN, Config.milkSquidCooldown.get().shortValue());
if (!event.getWorld().isRemote) {
InspirationsNetwork.sendToClients(event.getWorld(), target.getPosition(), new MilkablePacket(target, false));
}
// set success
event.setCancellationResult(ActionResultType.SUCCESS);
event.setCanceled(true);
}
}
}
}