-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
CitizensBlockBreaker.java
186 lines (171 loc) · 7.09 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
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
package net.citizensnpcs.nms.v1_14_R1.util;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import net.citizensnpcs.api.ai.tree.BehaviorStatus;
import net.citizensnpcs.api.npc.BlockBreaker;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.util.PlayerAnimation;
import net.citizensnpcs.util.Util;
import net.minecraft.server.v1_14_R1.BlockPosition;
import net.minecraft.server.v1_14_R1.Blocks;
import net.minecraft.server.v1_14_R1.EnchantmentManager;
import net.minecraft.server.v1_14_R1.Entity;
import net.minecraft.server.v1_14_R1.EntityLiving;
import net.minecraft.server.v1_14_R1.EntityPlayer;
import net.minecraft.server.v1_14_R1.EnumItemSlot;
import net.minecraft.server.v1_14_R1.IBlockData;
import net.minecraft.server.v1_14_R1.ItemStack;
import net.minecraft.server.v1_14_R1.MobEffects;
import net.minecraft.server.v1_14_R1.TagsFluid;
import net.minecraft.server.v1_14_R1.WorldServer;
public class CitizensBlockBreaker extends BlockBreaker {
private final BlockBreakerConfiguration configuration;
private int currentDamage;
private int currentTick;
private final Entity entity;
private boolean isDigging = true;
private final Location location;
private boolean setTarget;
private int startDigTick;
private final int x, y, z;
public CitizensBlockBreaker(org.bukkit.entity.Entity entity, org.bukkit.block.Block target,
BlockBreakerConfiguration config) {
this.entity = ((CraftEntity) entity).getHandle();
this.x = target.getX();
this.y = target.getY();
this.z = target.getZ();
this.location = target.getLocation();
this.startDigTick = (int) (System.currentTimeMillis() / 50);
this.configuration = config;
}
private double distanceSquared() {
return Math.pow(entity.locX - x, 2) + Math.pow(entity.locY - y, 2) + Math.pow(entity.locZ - z, 2);
}
private net.minecraft.server.v1_14_R1.ItemStack getCurrentItem() {
return configuration.item() != null ? CraftItemStack.asNMSCopy(configuration.item())
: entity instanceof EntityLiving ? ((EntityLiving) entity).getEquipment(EnumItemSlot.MAINHAND) : null;
}
private float getStrength(IBlockData block) {
float base = block.getBlock().a(block, null, new BlockPosition(0, 0, 0));
return base < 0.0F ? 0.0F : (!isDestroyable(block) ? 1.0F / base / 100.0F : strengthMod(block) / base / 30.0F);
}
private boolean isDestroyable(IBlockData block) {
if (block.getMaterial().isAlwaysDestroyable()) {
return true;
} else {
ItemStack current = getCurrentItem();
return current != null ? current.b(block) : false;
}
}
@Override
public void reset() {
if (setTarget && entity instanceof NPCHolder) {
NPC npc = ((NPCHolder) entity).getNPC();
if (npc != null && npc.getNavigator().isNavigating()) {
npc.getNavigator().cancelNavigation();
}
}
setTarget = false;
if (configuration.callback() != null) {
configuration.callback().run();
}
isDigging = false;
setBlockDamage(currentDamage = -1);
}
@Override
public BehaviorStatus run() {
if (entity.dead) {
return BehaviorStatus.FAILURE;
}
if (!isDigging) {
return BehaviorStatus.SUCCESS;
}
currentTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit
if (configuration.radiusSquared() > 0 && distanceSquared() >= configuration.radiusSquared()) {
startDigTick = currentTick;
if (entity instanceof NPCHolder) {
NPC npc = ((NPCHolder) entity).getNPC();
if (npc != null && !npc.getNavigator().isNavigating()) {
npc.getNavigator()
.setTarget(entity.world.getWorld().getBlockAt(x, y, z).getLocation().add(0, 1, 0));
setTarget = true;
}
}
return BehaviorStatus.RUNNING;
}
Util.faceLocation(entity.getBukkitEntity(), location);
if (entity instanceof EntityPlayer) {
PlayerAnimation.ARM_SWING.play((Player) entity.getBukkitEntity());
}
IBlockData block = entity.world.getType(new BlockPosition(x, y, z));
if (block == null || block.getBlock() == Blocks.AIR) {
return BehaviorStatus.SUCCESS;
} else {
int tickDifference = currentTick - startDigTick;
float damage = getStrength(block) * (tickDifference + 1) * configuration.blockStrengthModifier();
if (damage >= 1F) {
entity.world.getWorld().getBlockAt(x, y, z)
.breakNaturally(CraftItemStack.asCraftMirror(getCurrentItem()));
return BehaviorStatus.SUCCESS;
}
int modifiedDamage = (int) (damage * 10.0F);
if (modifiedDamage != currentDamage) {
setBlockDamage(modifiedDamage);
currentDamage = modifiedDamage;
}
}
return BehaviorStatus.RUNNING;
}
private void setBlockDamage(int modifiedDamage) {
((WorldServer) entity.world).a(entity.getId(), new BlockPosition(x, y, z), modifiedDamage); // TODO: does this
// work?
}
@Override
public boolean shouldExecute() {
return entity.world.getType(new BlockPosition(x, y, z)).getBlock() != Blocks.AIR;
}
private float strengthMod(IBlockData block) {
ItemStack itemstack = getCurrentItem();
float f = itemstack.a(block);
if (entity instanceof EntityLiving) {
EntityLiving handle = (EntityLiving) entity;
if (f > 1.0F) {
int i = EnchantmentManager.getDigSpeedEnchantmentLevel(handle);
if (i > 0) {
f += i * i + 1;
}
}
if (handle.hasEffect(MobEffects.FASTER_DIG)) {
f *= (1.0F + (handle.getEffect(MobEffects.FASTER_DIG).getAmplifier() + 1) * 0.2F);
}
if (handle.hasEffect(MobEffects.SLOWER_DIG)) {
float f1 = 1.0F;
switch (handle.getEffect(MobEffects.SLOWER_DIG).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.a(TagsFluid.WATER) && !EnchantmentManager.h(handle)) {
f /= 5.0F;
}
}
if (!entity.onGround) {
f /= 5.0F;
}
return f;
}
}