/
MagnetUtil.java
112 lines (89 loc) · 3.48 KB
/
MagnetUtil.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
package com.enderio.core.common.util;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.entity.Entity;
import net.minecraft.entity.IProjectile;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
public class MagnetUtil {
private static final @Nonnull String PREVENT_REMOTE_MOVEMENT = "PreventRemoteMovement";
private static final @Nonnull String ALLOW_MACHINE_MOVEMENT = "AllowMachineRemoteMovement";
public static final @Nonnull String EC_PULLER_TAG = "ECpuller";
@Deprecated
public static boolean shouldAttract(@Nullable BlockPos pullerPos, @Nullable Entity entity) {
return shouldAttract(pullerPos, entity, false);
}
public static boolean shouldAttract(@Nullable BlockPos pullerPos, @Nullable Entity entity, boolean isMachine) {
if (entity == null || entity.isDead) {
return false;
}
if (entity instanceof IProjectile && entity.motionY > 0.01) {
return false;
}
@Nonnull
NBTTagCompound data = entity.getEntityData();
if (isReservedByOthers(data, isMachine)) {
return false;
}
if (!isReservedByUs(data)) {
// if it is not being pulled already, pull it
reserve(data, pullerPos);
return true;
}
if (pullerPos == null) {
// it is already being pulled, so with no further info we are done
return false;
}
long posL = data.getLong(EC_PULLER_TAG);
if (posL == pullerPos.toLong()) {
// item already pulled from pullerPos so done
return true;
}
// it is being pulled by something else, so check to see if we are closer
BlockPos curOwner = BlockPos.fromLong(posL);
double distToCur = curOwner.distanceSqToCenter(entity.posX, entity.posY, entity.posZ);
double distToMe = pullerPos.distanceSqToCenter(entity.posX, entity.posY, entity.posZ);
if (distToMe + 1 < distToCur) {
reserve(data, pullerPos);
return true;
}
return false;
}
public static void reserve(@Nullable Entity entity, @Nullable BlockPos pullerPos) {
if (entity != null && !entity.isDead) {
NBTTagCompound data = entity.getEntityData();
reserve(data, pullerPos);
}
}
public static void reserve(@Nonnull NBTTagCompound data, @Nullable BlockPos pullerPos) {
if (pullerPos != null) {
data.setLong(EC_PULLER_TAG, pullerPos.toLong());
}
}
public static void release(@Nullable Entity entity) {
if (entity != null && !entity.isDead) {
NBTTagCompound data = entity.getEntityData();
release(data);
}
}
public static void release(@Nonnull NBTTagCompound data) {
data.removeTag(EC_PULLER_TAG);
}
@Deprecated
public static boolean isReserved(@Nonnull Entity entity) {
return isReserved(entity, false);
}
public static boolean isReserved(@Nonnull Entity entity, boolean isMachine) {
return isReservedByUs(entity.getEntityData()) || isReservedByOthers(entity.getEntityData(), isMachine);
}
public static boolean isReservedByUs(@Nonnull NBTTagCompound data) {
return data.hasKey(EC_PULLER_TAG);
}
@Deprecated
public static boolean isReservedByOthers(@Nonnull NBTTagCompound data) {
return isReservedByOthers(data, false);
}
public static boolean isReservedByOthers(@Nonnull NBTTagCompound data, boolean isMachine) {
return data.hasKey(PREVENT_REMOTE_MOVEMENT) && (!isMachine || !data.hasKey(ALLOW_MACHINE_MOVEMENT));
}
}