-
Notifications
You must be signed in to change notification settings - Fork 754
/
TastyModifier.java
100 lines (89 loc) · 4.59 KB
/
TastyModifier.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
package slimeknights.tconstruct.tools.modifiers.traits.general;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.UseAnim;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraftforge.common.util.Lazy;
import slimeknights.tconstruct.common.TinkerTags;
import slimeknights.tconstruct.library.modifiers.Modifier;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierHooks;
import slimeknights.tconstruct.library.modifiers.hook.interaction.GeneralInteractionModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.interaction.InteractionSource;
import slimeknights.tconstruct.library.module.ModuleHookMap.Builder;
import slimeknights.tconstruct.library.tools.helper.ModifierUtil;
import slimeknights.tconstruct.library.tools.helper.ToolDamageUtil;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT;
import slimeknights.tconstruct.shared.TinkerCommons;
import java.util.List;
public class TastyModifier extends Modifier implements GeneralInteractionModifierHook {
// TODO: consider making this modifier dynamic and letting addons swap out representative items and food rewards
private static final Lazy<ItemStack> BACON_STACK = Lazy.of(() -> new ItemStack(TinkerCommons.bacon));
@Override
protected void registerHooks(Builder hookBuilder) {
hookBuilder.addHook(this, ModifierHooks.GENERAL_INTERACT);
}
@Override
public InteractionResult onToolUse(IToolStackView tool, ModifierEntry modifier, Player player, InteractionHand hand, InteractionSource source) {
if (source == InteractionSource.RIGHT_CLICK && !tool.isBroken() && player.canEat(false)) {
GeneralInteractionModifierHook.startUsing(tool, modifier.getId(), player, hand);
return InteractionResult.CONSUME;
}
return InteractionResult.PASS;
}
@Override
public void onFinishUsing(IToolStackView tool, ModifierEntry modifier, LivingEntity entity) {
// remove is eating tag to prevent from messing with other modifiers
ModDataNBT persistentData = tool.getPersistentData();
if (!tool.isBroken() && entity instanceof Player player && player.canEat(false)) {
// eat the food
int level = modifier.getLevel();
Level world = entity.getLevel();
player.getFoodData().eat(level, level * 0.1f);
ModifierUtil.foodConsumer.onConsume(player, BACON_STACK.get(), level, level * 0.1f);
player.awardStat(Stats.ITEM_USED.get(tool.getItem()));
world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.GENERIC_EAT, SoundSource.NEUTRAL, 1.0F, 1.0F + (world.random.nextFloat() - world.random.nextFloat()) * 0.4F);
world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.PLAYER_BURP, SoundSource.NEUTRAL, 0.5F, world.random.nextFloat() * 0.1F + 0.9F);
// 15 damage for a bite per level, does not process reinforced/overslime, your teeth are tough
if (ToolDamageUtil.directDamage(tool, 15 * level, player, player.getUseItem())) {
player.broadcastBreakEvent(player.getUsedItemHand());
}
}
}
@Override
public UseAnim getUseAction(IToolStackView tool, ModifierEntry modifier) {
return UseAnim.EAT;
}
@Override
public int getUseDuration(IToolStackView tool, ModifierEntry modifier) {
return 16;
}
@Override
public void processLoot(IToolStackView tool, int level, List<ItemStack> generatedLoot, LootContext context) {
// if no damage source, probably not a mob
// otherwise blocks breaking (where THIS_ENTITY is the player) start dropping bacon
if (!context.hasParam(LootContextParams.DAMAGE_SOURCE)) {
return;
}
// must have an entity
Entity entity = context.getParamOrNull(LootContextParams.THIS_ENTITY);
if (entity != null && entity.getType().is(TinkerTags.EntityTypes.BACON_PRODUCER)) {
// at tasty 1, 2, 3, and 4 its a 2%, 4.15%, 6.25%, 8% per level
int looting = context.getLootingModifier();
if (RANDOM.nextInt(48 / level) <= looting) {
// bacon
generatedLoot.add(new ItemStack(TinkerCommons.bacon));
}
}
}
}