-
Notifications
You must be signed in to change notification settings - Fork 755
/
SplashingModifier.java
182 lines (168 loc) · 9.49 KB
/
SplashingModifier.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
package slimeknights.tconstruct.tools.modifiers.ability.fluid;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidType;
import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction;
import slimeknights.tconstruct.library.modifiers.Modifier;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierHooks;
import slimeknights.tconstruct.library.modifiers.fluid.FluidEffectContext;
import slimeknights.tconstruct.library.modifiers.fluid.FluidEffectManager;
import slimeknights.tconstruct.library.modifiers.fluid.FluidEffects;
import slimeknights.tconstruct.library.modifiers.hook.build.ConditionalStatModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.interaction.BlockInteractionModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.interaction.EntityInteractionModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.interaction.InteractionSource;
import slimeknights.tconstruct.library.modifiers.modules.build.StatBoostModule;
import slimeknights.tconstruct.library.module.ModuleHookMap.Builder;
import slimeknights.tconstruct.library.tools.capability.fluid.ToolTankHelper;
import slimeknights.tconstruct.library.tools.definition.module.ToolHooks;
import slimeknights.tconstruct.library.tools.definition.module.aoe.AreaOfEffectIterator;
import slimeknights.tconstruct.library.tools.definition.module.aoe.CircleAOEIterator;
import slimeknights.tconstruct.library.tools.helper.ToolDamageUtil;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.stat.ToolStats;
import slimeknights.tconstruct.shared.TinkerCommons;
import slimeknights.tconstruct.shared.particle.FluidParticleData;
import slimeknights.tconstruct.tools.TinkerModifiers;
import static slimeknights.tconstruct.library.tools.capability.fluid.ToolTankHelper.TANK_HELPER;
import static slimeknights.tconstruct.library.tools.helper.ModifierUtil.asLiving;
/** Modifier to handle spilling recipes on interaction */
public class SplashingModifier extends Modifier implements EntityInteractionModifierHook, BlockInteractionModifierHook {
@Override
protected void registerHooks(Builder hookBuilder) {
super.registerHooks(hookBuilder);
hookBuilder.addModule(ToolTankHelper.TANK_HANDLER);
hookBuilder.addModule(StatBoostModule.add(ToolTankHelper.CAPACITY_STAT).eachLevel(FluidType.BUCKET_VOLUME));
hookBuilder.addHook(this, ModifierHooks.ENTITY_INTERACT, ModifierHooks.BLOCK_INTERACT);
}
@Override
public InteractionResult beforeEntityUse(IToolStackView tool, ModifierEntry modifier, Player player, Entity target, InteractionHand hand, InteractionSource source) {
// melee items get spilling via attack, non melee interact to use it
if (tool.getHook(ToolHooks.INTERACTION).canInteract(tool, modifier.getId(), source)) {
FluidStack fluid = TANK_HELPER.getFluid(tool);
if (!fluid.isEmpty()) {
FluidEffects recipe = FluidEffectManager.INSTANCE.find(fluid.getFluid());
if (recipe.hasEntityEffects()) {
if (!player.level.isClientSide) {
// for the main target, consume fluids
float level = modifier.getEffectiveLevel();
int numTargets = 0;
int consumed = recipe.applyToEntity(fluid, level, new FluidEffectContext.Entity(player.level, player, player, null, target, asLiving(target)), FluidAction.EXECUTE);
if (consumed > 0) {
numTargets++;
UseFluidOnHitModifier.spawnParticles(target, fluid);
}
// expanded logic, they do not consume fluid, you get some splash for free
float range = 1 + tool.getModifierLevel(TinkerModifiers.expanded.get());
float rangeSq = range * range;
for (Entity aoeTarget : player.level.getEntitiesOfClass(Entity.class, target.getBoundingBox().inflate(range, 0.25, range))) {
if (aoeTarget != player && aoeTarget != target && !(aoeTarget instanceof ArmorStand stand && stand.isMarker()) && target.distanceToSqr(aoeTarget) < rangeSq) {
int aoeConsumed = recipe.applyToEntity(fluid, level, new FluidEffectContext.Entity(player.level, player, player, null, aoeTarget, asLiving(aoeTarget)), FluidAction.EXECUTE);
if (aoeConsumed > 0) {
numTargets++;
UseFluidOnHitModifier.spawnParticles(aoeTarget, fluid);
// consume the largest amount requested from any entity
if (aoeConsumed > consumed) {
consumed = aoeConsumed;
}
}
}
}
// consume the fluid last, if any target used fluid
if (!player.isCreative() ) {
if (consumed > 0) {
fluid.shrink(consumed);
TANK_HELPER.setFluid(tool, fluid);
}
// damage the tool, we charge for the multiplier and for the number of targets hit
ToolDamageUtil.damageAnimated(tool, Mth.ceil(numTargets * level), player, hand);
}
}
// cooldown based on attack speed/draw speed. both are on the same scale and default to 1, we don't care which one the tool uses
player.getCooldowns().addCooldown(tool.getItem(), (int)(20 / ConditionalStatModifierHook.getModifiedStat(tool, player, ToolStats.DRAW_SPEED)));
return InteractionResult.SUCCESS;
}
}
}
return InteractionResult.PASS;
}
/** Spawns particles at the given entity */
private static void spawnParticles(Level level, BlockHitResult hit, FluidStack fluid) {
if (level instanceof ServerLevel) {
Vec3 location = hit.getLocation();
((ServerLevel)level).sendParticles(new FluidParticleData(TinkerCommons.fluidParticle.get(), fluid), location.x(), location.y(), location.z(), 10, 0.1, 0.2, 0.1, 0.2);
}
}
@Override
public InteractionResult afterBlockUse(IToolStackView tool, ModifierEntry modifier, UseOnContext context, InteractionSource source) {
if (tool.getHook(ToolHooks.INTERACTION).canInteract(tool, modifier.getId(), source)) {
FluidStack fluid = TANK_HELPER.getFluid(tool);
if (!fluid.isEmpty()) {
FluidEffects recipe = FluidEffectManager.INSTANCE.find(fluid.getFluid());
if (recipe.hasEntityEffects()) {
Player player = context.getPlayer();
Level world = context.getLevel();
if (!context.getLevel().isClientSide) {
Direction face = context.getClickedFace();
BlockPos pos = context.getClickedPos();
float level = modifier.getEffectiveLevel();
int numTargets = 0;
BlockHitResult hit = context.getHitResult();
int consumed = recipe.applyToBlock(fluid, level, new FluidEffectContext.Block(world, player, null, hit), FluidAction.EXECUTE);
if (consumed > 0) {
numTargets++;
spawnParticles(world, hit, fluid);
}
// AOE selection logic, get boosted from both fireprimer (unique modifer) and expanded
int range = tool.getModifierLevel(TinkerModifiers.expanded.getId());
if (range > 0 && player != null) {
for (BlockPos offset : CircleAOEIterator.calculate(tool, ItemStack.EMPTY, world, player, pos, face, 1 + range, false, AreaOfEffectIterator.AOEMatchType.TRANSFORM)) {
BlockHitResult offsetHit = hit.withPosition(offset);
int aoeConsumed = recipe.applyToBlock(fluid, level, new FluidEffectContext.Block(world, player, null, offsetHit), FluidAction.EXECUTE);
if (aoeConsumed > 0) {
numTargets++;
spawnParticles(world, offsetHit, fluid);
if (aoeConsumed > consumed) {
consumed = aoeConsumed;
}
}
}
}
// consume the fluid last, if any target used fluid
if (player == null || !player.isCreative() ) {
if (consumed > 0) {
fluid.shrink(consumed);
TANK_HELPER.setFluid(tool, fluid);
}
// damage the tool, we charge for the multiplier and for the number of targets hit
ItemStack stack = context.getItemInHand();
if (ToolDamageUtil.damage(tool, Mth.ceil(numTargets * level), player, stack) && player != null) {
player.broadcastBreakEvent(source.getSlot(context.getHand()));
}
}
}
// cooldown based on attack speed/draw speed. both are on the same scale and default to 1, we don't care which one the tool uses
if (player != null) {
player.getCooldowns().addCooldown(tool.getItem(), (int)(20 / ConditionalStatModifierHook.getModifiedStat(tool, player, ToolStats.DRAW_SPEED)));
}
return InteractionResult.SUCCESS;
}
}
}
return InteractionResult.PASS;
}
}