-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
CitizensBlockBreaker.java
102 lines (90 loc) · 3.64 KB
/
CitizensBlockBreaker.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 net.citizensnpcs.nms.v1_18_R2.util;
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack;
import net.citizensnpcs.util.AbstractBlockBreaker;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.effect.MobEffectUtil;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.block.state.BlockState;
public class CitizensBlockBreaker extends AbstractBlockBreaker {
public CitizensBlockBreaker(org.bukkit.entity.Entity entity, org.bukkit.block.Block target,
BlockBreakerConfiguration config) {
super(entity, target, config);
}
private ItemStack getCurrentItem() {
return configuration.item() != null ? CraftItemStack.asNMSCopy(configuration.item())
: getHandle() instanceof LivingEntity ? ((LivingEntity) getHandle()).getMainHandItem() : null;
}
@Override
protected float getDamage(int tickDifference) {
return getStrength(getHandle().level.getBlockState(new BlockPos(x, y, z))) * (tickDifference + 1)
* configuration.blockStrengthModifier();
}
private Entity getHandle() {
return NMSImpl.getHandle(entity);
}
protected float getStrength(BlockState block) {
float base = block.getDestroySpeed(null, BlockPos.ZERO);
return base < 0.0F ? 0.0F : (!isDestroyable(block) ? 1.0F / base / 100.0F : strengthMod(block) / base / 30.0F);
}
private boolean isDestroyable(BlockState block) {
if (block.requiresCorrectToolForDrops()) {
return true;
} else {
ItemStack current = getCurrentItem();
return current != null ? current.isCorrectToolForDrops(block) : false;
}
}
@Override
protected void setBlockDamage(int modifiedDamage) {
((ServerLevel) getHandle().level).destroyBlockProgress(getHandle().getId(), new BlockPos(x, y, z),
modifiedDamage);
// TODO: check this works
}
private float strengthMod(BlockState block) {
ItemStack itemstack = getCurrentItem();
float f = itemstack.getDestroySpeed(block);
if (getHandle() instanceof LivingEntity) {
LivingEntity handle = (LivingEntity) getHandle();
if (f > 1.0F) {
int i = EnchantmentHelper.getBlockEfficiency(handle);
if (i > 0) {
f += i * i + 1;
}
}
if (MobEffectUtil.hasDigSpeed(handle)) {
f *= 1.0F + (MobEffectUtil.getDigSpeedAmplification(handle) + 1) * 0.2F;
}
if (handle.hasEffect(MobEffects.DIG_SLOWDOWN)) {
float f1 = 1.0F;
switch (handle.getEffect(MobEffects.DIG_SLOWDOWN).getAmplifier()) {
case 0:
f1 = 0.3F;
break;
case 1:
f1 = 0.09F;
break;
case 2:
f1 = 0.0027F;
break;
case 3:
default:
f1 = 8.1E-4F;
}
f *= f1;
}
if (handle.isEyeInFluid(FluidTags.WATER) && !EnchantmentHelper.hasAquaAffinity(handle)) {
f /= 5.0F;
}
}
if (!entity.isOnGround()) {
f /= 5.0F;
}
return f;
}
}