-
Notifications
You must be signed in to change notification settings - Fork 755
/
ToolEvents.java
446 lines (415 loc) · 20.3 KB
/
ToolEvents.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package slimeknights.tconstruct.tools.logic;
import com.google.common.collect.Multiset;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BeehiveBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.CampfireBlock;
import net.minecraft.world.level.block.CarvedPumpkinBlock;
import net.minecraft.world.level.block.entity.BeehiveBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraftforge.event.entity.ProjectileImpactEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingTickEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingVisibilityEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.Event.Result;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.common.TinkerTags;
import slimeknights.tconstruct.common.config.Config;
import slimeknights.tconstruct.library.events.TinkerToolEvent.ToolHarvestEvent;
import slimeknights.tconstruct.library.modifiers.Modifier;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierHooks;
import slimeknights.tconstruct.library.modifiers.data.ModifierMaxLevel;
import slimeknights.tconstruct.library.modifiers.hook.armor.ModifyDamageModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.armor.OnAttackedModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.armor.ProtectionModifierHook;
import slimeknights.tconstruct.library.modifiers.modules.armor.MobDisguiseModule;
import slimeknights.tconstruct.library.modifiers.modules.unserializable.ArmorStatModule;
import slimeknights.tconstruct.library.tools.capability.EntityModifierCapability;
import slimeknights.tconstruct.library.tools.capability.PersistentDataCapability;
import slimeknights.tconstruct.library.tools.capability.TinkerDataCapability;
import slimeknights.tconstruct.library.tools.context.EquipmentContext;
import slimeknights.tconstruct.library.tools.definition.ModifiableArmorMaterial;
import slimeknights.tconstruct.library.tools.helper.ArmorUtil;
import slimeknights.tconstruct.library.tools.helper.ModifierUtil;
import slimeknights.tconstruct.library.tools.helper.ToolAttackUtil;
import slimeknights.tconstruct.library.tools.helper.ToolDamageUtil;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ModifierNBT;
import slimeknights.tconstruct.library.tools.nbt.NamespacedNBT;
import slimeknights.tconstruct.library.tools.nbt.ToolStack;
import slimeknights.tconstruct.library.utils.BlockSideHitListener;
import slimeknights.tconstruct.tools.TinkerModifiers;
import slimeknights.tconstruct.tools.modifiers.defense.ProjectileProtectionModifier;
import slimeknights.tconstruct.tools.modifiers.upgrades.armor.HasteModifier;
import java.util.List;
import java.util.Objects;
/**
* Event subscriber for tool events
*/
@SuppressWarnings("unused")
@EventBusSubscriber(modid = TConstruct.MOD_ID, bus = Bus.FORGE)
public class ToolEvents {
@SubscribeEvent
static void onBreakSpeed(PlayerEvent.BreakSpeed event) {
Player player = event.getEntity();
// tool break speed hook
ItemStack stack = player.getMainHandItem();
if (stack.is(TinkerTags.Items.HARVEST)) {
ToolStack tool = ToolStack.from(stack);
if (!tool.isBroken()) {
List<ModifierEntry> modifiers = tool.getModifierList();
if (!modifiers.isEmpty()) {
// modifiers using additive boosts may want info on the original boosts provided
float miningSpeedModifier = Modifier.getMiningModifier(player);
boolean isEffective = stack.isCorrectToolForDrops(event.getState());
Direction direction = BlockSideHitListener.getSideHit(player);
for (ModifierEntry entry : tool.getModifierList()) {
entry.getHook(ModifierHooks.BREAK_SPEED).onBreakSpeed(tool, entry, event, direction, isEffective, miningSpeedModifier);
// if any modifier cancels mining, stop right here
if (event.isCanceled()) {
return;
}
}
}
}
}
// next, add in armor haste
float armorHaste = ArmorStatModule.getStat(player, HasteModifier.HASTE);
if (armorHaste > 0) {
// adds in 10% per level
event.setNewSpeed(event.getNewSpeed() * (1 + armorHaste));
}
}
@SubscribeEvent
static void onHarvest(ToolHarvestEvent event) {
// prevent processing if already processed
if (event.getResult() != Result.DEFAULT) {
return;
}
BlockState state = event.getState();
Block block = state.getBlock();
Level world = event.getWorld();
BlockPos pos = event.getPos();
// carve pumpkins
if (block == Blocks.PUMPKIN) {
Direction facing = event.getContext().getClickedFace();
if (facing.getAxis() == Direction.Axis.Y) {
facing = event.getContext().getHorizontalDirection().getOpposite();
}
// carve block
world.playSound(null, pos, SoundEvents.PUMPKIN_CARVE, SoundSource.BLOCKS, 1.0F, 1.0F);
world.setBlock(pos, Blocks.CARVED_PUMPKIN.defaultBlockState().setValue(CarvedPumpkinBlock.FACING, facing), 11);
// spawn seeds
ItemEntity itemEntity = new ItemEntity(
world,
pos.getX() + 0.5D + facing.getStepX() * 0.65D,
pos.getY() + 0.1D,
pos.getZ() + 0.5D + facing.getStepZ() * 0.65D,
new ItemStack(Items.PUMPKIN_SEEDS, 4));
itemEntity.setDeltaMovement(
0.05D * facing.getStepX() + world.random.nextDouble() * 0.02D,
0.05D,
0.05D * facing.getStepZ() + world.random.nextDouble() * 0.02D);
world.addFreshEntity(itemEntity);
event.setResult(Result.ALLOW);
}
// hives: get the honey
if (block instanceof BeehiveBlock beehive) {
int level = state.getValue(BeehiveBlock.HONEY_LEVEL);
if (level >= 5) {
// first, spawn the honey
world.playSound(null, pos, SoundEvents.BEEHIVE_SHEAR, SoundSource.NEUTRAL, 1.0F, 1.0F);
Block.popResource(world, pos, new ItemStack(Items.HONEYCOMB, 3));
// if not smoking, make the bees angry
if (!CampfireBlock.isSmokeyPos(world, pos)) {
if (beehive.hiveContainsBees(world, pos)) {
beehive.angerNearbyBees(world, pos);
}
beehive.releaseBeesAndResetHoneyLevel(world, state, pos, event.getPlayer(), BeehiveBlockEntity.BeeReleaseStatus.EMERGENCY);
} else {
beehive.resetHoneyLevel(world, state, pos);
}
event.setResult(Result.ALLOW);
} else {
event.setResult(Result.DENY);
}
}
}
@SubscribeEvent(priority = EventPriority.LOW)
static void livingAttack(LivingAttackEvent event) {
LivingEntity entity = event.getEntity();
// client side always returns false, so this should be fine?
if (entity.level.isClientSide() || entity.isDeadOrDying()) {
return;
}
// I cannot think of a reason to run when invulnerable
DamageSource source = event.getSource();
if (entity.isInvulnerableTo(source)) {
return;
}
// a lot of counterattack hooks want to detect direct attacks, so save time by calculating once
boolean isDirectDamage = OnAttackedModifierHook.isDirectDamage(source);
// determine if there is any modifiable armor, handles the target wearing modifiable armor
EquipmentContext context = new EquipmentContext(entity);
float amount = event.getAmount();
if (context.hasModifiableArmor()) {
// first we need to determine if any of the four slots want to cancel the event
for (EquipmentSlot slotType : EquipmentSlot.values()) {
if (ModifierUtil.validArmorSlot(entity, slotType)) {
IToolStackView toolStack = context.getToolInSlot(slotType);
if (toolStack != null && !toolStack.isBroken()) {
for (ModifierEntry entry : toolStack.getModifierList()) {
if (entry.getHook(ModifierHooks.DAMAGE_BLOCK).isDamageBlocked(toolStack, entry, context, slotType, source, amount)) {
event.setCanceled(true);
return;
}
}
}
}
}
// then we need to determine if any want to respond assuming its not canceled
OnAttackedModifierHook.handleAttack(ModifierHooks.ON_ATTACKED, context, source, amount, isDirectDamage);
}
// next, consider the attacker is wearing modifiable armor
Entity attacker = source.getEntity();
if (attacker instanceof LivingEntity livingAttacker) {
context = new EquipmentContext(livingAttacker);
if (context.hasModifiableArmor()) {
for (EquipmentSlot slotType : ModifiableArmorMaterial.ARMOR_SLOTS) {
IToolStackView toolStack = context.getToolInSlot(slotType);
if (toolStack != null && !toolStack.isBroken()) {
for (ModifierEntry entry : toolStack.getModifierList()) {
entry.getHook(ModifierHooks.DAMAGE_DEALT).onDamageDealt(toolStack, entry, context, slotType, entity, source, amount, isDirectDamage);
}
}
}
}
}
}
/**
* Determines how much to damage armor based on the given damage to the player
* @param damage Amount to damage the player
* @return Amount to damage the armor
*/
private static int getArmorDamage(float damage) {
damage /= 4;
if (damage < 1) {
return 1;
}
return (int)damage;
}
// low priority to minimize conflict as we apply reduction as if we are the final change to damage before vanilla
@SubscribeEvent(priority = EventPriority.LOW)
static void livingHurt(LivingHurtEvent event) {
LivingEntity entity = event.getEntity();
// determine if there is any modifiable armor, if not nothing to do
DamageSource source = event.getSource();
EquipmentContext context = new EquipmentContext(entity);
int vanillaModifier = 0;
float modifierValue = 0;
float originalDamage = event.getAmount();
// for our own armor, we have boosts from modifiers to consider
if (context.hasModifiableArmor()) {
// first, allow modifiers to change the damage being dealt and respond to it happening
originalDamage = ModifyDamageModifierHook.modifyDamageTaken(ModifierHooks.MODIFY_HURT, context, source, originalDamage, OnAttackedModifierHook.isDirectDamage(source));
event.setAmount(originalDamage);
if (originalDamage <= 0) {
event.setCanceled(true);
return;
}
// remaining logic is reducing damage like vanilla protection
// fetch vanilla enchant level, assuming its not bypassed in vanilla
if (!source.isBypassMagic()) {
modifierValue = vanillaModifier = EnchantmentHelper.getDamageProtection(entity.getArmorSlots(), source);
}
// next, determine how much tinkers armor wants to change it
// note that armor modifiers can choose to block "absolute damage" if they wish, currently just starving damage I think
for (EquipmentSlot slotType : EquipmentSlot.values()) {
if (ModifierUtil.validArmorSlot(entity, slotType)) {
IToolStackView tool = context.getToolInSlot(slotType);
if (tool != null && !tool.isBroken()) {
for (ModifierEntry entry : tool.getModifierList()) {
modifierValue = entry.getHook(ModifierHooks.PROTECTION).getProtectionModifier(tool, entry, context, slotType, source, modifierValue);
}
}
}
}
// give slimes a 4x armor boost
if (entity.getType().is(TinkerTags.EntityTypes.SMALL_ARMOR)) {
modifierValue *= 4;
}
} else if (!source.isBypassMagic() && entity.getType().is(TinkerTags.EntityTypes.SMALL_ARMOR)) {
vanillaModifier = EnchantmentHelper.getDamageProtection(entity.getArmorSlots(), source);
modifierValue = vanillaModifier * 4;
}
// if we changed anything, run our logic. Changing the cap has 2 problematic cases where same value will work:
// * increased cap and vanilla is over the vanilla cap
// * decreased cap and vanilla is now under the cap
// that said, don't actually care about cap unless we have some protection, can use vanilla to simplify logic
float cap = 20f;
if (modifierValue > 0) {
cap = ProtectionModifierHook.getProtectionCap(context.getTinkerData());
}
if (vanillaModifier != modifierValue || (cap > 20 && vanillaModifier > 20) || (cap < 20 && vanillaModifier > cap)) {
// fetch armor and toughness if blockable, passing in 0 to the logic will skip the armor calculations
float armor = 0, toughness = 0;
if (!source.isBypassArmor()) {
armor = entity.getArmorValue();
toughness = (float)entity.getAttributeValue(Attributes.ARMOR_TOUGHNESS);
}
// set the final dealt damage
float finalDamage = ArmorUtil.getDamageForEvent(originalDamage, armor, toughness, vanillaModifier, modifierValue, cap);
event.setAmount(finalDamage);
// armor is damaged less as a result of our math, so damage the armor based on the difference if there is one
if (!source.isBypassArmor()) {
int damageMissed = getArmorDamage(originalDamage) - getArmorDamage(finalDamage);
// TODO: is this check sufficient for whether the armor should be damaged? I partly wonder if I need to use reflection to call damageArmor
if (damageMissed > 0 && entity instanceof Player) {
for (EquipmentSlot slotType : ModifiableArmorMaterial.ARMOR_SLOTS) {
// for our own armor, saves effort to damage directly with our utility
IToolStackView tool = context.getToolInSlot(slotType);
if (tool != null && (!source.isFire() || !tool.getItem().isFireResistant())) {
ToolDamageUtil.damageAnimated(tool, damageMissed, entity, slotType);
} else {
// if not our armor, damage using vanilla like logic
ItemStack armorStack = entity.getItemBySlot(slotType);
if (!armorStack.isEmpty() && (!source.isFire() || !armorStack.getItem().isFireResistant()) && armorStack.getItem() instanceof ArmorItem) {
armorStack.hurtAndBreak(damageMissed, entity, e -> e.broadcastBreakEvent(slotType));
}
}
}
}
}
}
}
@SubscribeEvent
static void livingDamage(LivingDamageEvent event) {
LivingEntity entity = event.getEntity();
DamageSource source = event.getSource();
// give modifiers a chance to respond to damage happening
EquipmentContext context = new EquipmentContext(entity);
if (context.hasModifiableArmor()) {
float amount = ModifyDamageModifierHook.modifyDamageTaken(ModifierHooks.MODIFY_DAMAGE, context, source, event.getAmount(), OnAttackedModifierHook.isDirectDamage(source));
event.setAmount(amount);
if (amount <= 0) {
event.setCanceled(true);
return;
}
}
// when damaging ender dragons, may drop scales - must be player caused explosion, end crystals and TNT are examples
if (Config.COMMON.dropDragonScales.get() && entity.getType() == EntityType.ENDER_DRAGON && event.getAmount() > 0
&& source.isExplosion() && source.getEntity() != null && source.getEntity().getType() == EntityType.PLAYER) {
// drops 1 - 8 scales
ModifierUtil.dropItem(entity, new ItemStack(TinkerModifiers.dragonScale, 1 + entity.level.random.nextInt(8)));
}
}
/** Called the modifier hook when an entity's position changes */
@SubscribeEvent
static void livingWalk(LivingTickEvent event) {
LivingEntity living = event.getEntity();
// this event runs before vanilla updates prevBlockPos
BlockPos pos = living.blockPosition();
if (!living.isSpectator() && !living.level.isClientSide() && living.isAlive() && !Objects.equals(living.lastPos, pos)) {
ItemStack boots = living.getItemBySlot(EquipmentSlot.FEET);
if (!boots.isEmpty() && boots.is(TinkerTags.Items.BOOTS)) {
ToolStack tool = ToolStack.from(boots);
for (ModifierEntry entry : tool.getModifierList()) {
entry.getHook(ModifierHooks.BOOT_WALK).onWalk(tool, entry, living, living.lastPos, pos);
}
}
}
}
/** Handles visibility effects of mob disguise and projectile protection */
@SubscribeEvent
static void livingVisibility(LivingVisibilityEvent event) {
// always nonnull in vanilla, not sure when it would be nullable but I dont see a need for either modifier
Entity lookingEntity = event.getLookingEntity();
if (lookingEntity == null) {
return;
}
LivingEntity living = event.getEntity();
living.getCapability(TinkerDataCapability.CAPABILITY).ifPresent(data -> {
// mob disguise
Multiset<EntityType<?>> disguises = data.get(MobDisguiseModule.DISGUISES);
if (disguises != null && disguises.contains(lookingEntity.getType())) {
// not as good as a real head
event.modifyVisibility(0.65f);
}
// projectile protection
ModifierMaxLevel projData = data.get(ProjectileProtectionModifier.PROJECTILE_DATA);
if (projData != null) {
float max = projData.getMax();
if (max > 0) {
// reduces visibility by 5% per level
event.modifyVisibility(Math.max(0, 1 - (max * 0.05)));
}
}
});
}
/** Implements projectile hit hook */
@SubscribeEvent
static void projectileHit(ProjectileImpactEvent event) {
Projectile projectile = event.getProjectile();
ModifierNBT modifiers = EntityModifierCapability.getOrEmpty(projectile);
if (!modifiers.isEmpty()) {
NamespacedNBT nbt = PersistentDataCapability.getOrWarn(projectile);
HitResult hit = event.getRayTraceResult();
HitResult.Type type = hit.getType();
// extract a firing entity as that is a common need
LivingEntity attacker = projectile.getOwner() instanceof LivingEntity l ? l : null;
switch(type) {
case ENTITY -> {
EntityHitResult entityHit = (EntityHitResult)hit;
// cancel all effects on endermen unless we have enderference, endermen like to teleport away
// yes, hardcoded to enderference, if you need your own enderference for whatever reason, talk to us
if (entityHit.getEntity().getType() != EntityType.ENDERMAN || modifiers.getLevel(TinkerModifiers.enderference.getId()) > 0) {
// extract a living target as that is the most common need
LivingEntity target = ToolAttackUtil.getLivingEntity(entityHit.getEntity());
for (ModifierEntry entry : modifiers.getModifiers()) {
if (entry.getHook(ModifierHooks.PROJECTILE_HIT).onProjectileHitEntity(modifiers, nbt, entry, projectile, entityHit, attacker, target)) {
event.setCanceled(true);
}
}
}
}
case BLOCK -> {
BlockHitResult blockHit = (BlockHitResult)hit;
for (ModifierEntry entry : modifiers.getModifiers()) {
if (entry.getHook(ModifierHooks.PROJECTILE_HIT).onProjectileHitBlock(modifiers, nbt, entry, projectile, blockHit, attacker)) {
event.setCanceled(true);
}
}
}
}
}
}
}