/
EntityDisabledSlots.java
192 lines (161 loc) · 6.66 KB
/
EntityDisabledSlots.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
package com.denizenscript.denizen.objects.properties.entity;
import com.denizenscript.denizen.utilities.nbt.CustomNBT;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.EquipmentSlot;
import java.util.*;
public class EntityDisabledSlots implements Property {
public static boolean describes(ObjectTag entity) {
return entity instanceof EntityTag && ((EntityTag) entity).getBukkitEntityType() == EntityType.ARMOR_STAND;
}
public static EntityDisabledSlots getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityDisabledSlots((EntityTag) entity);
}
}
public static final String[] handledTags = new String[] {
"disabled_slots"
};
public static final String[] handledMechs = new String[] {
"disabled_slots_raw", "disabled_slots"
};
private EntityDisabledSlots(EntityTag entity) {
dentity = entity;
}
EntityTag dentity;
public enum Action {
ALL(0), REMOVE(8), PLACE(16);
private final int id;
Action(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
private ListTag getDisabledSlots() {
Map<EquipmentSlot, Set<Action>> map = CustomNBT.getDisabledSlots(dentity.getBukkitEntity());
ListTag list = new ListTag();
for (Map.Entry<EquipmentSlot, Set<Action>> entry : map.entrySet()) {
for (Action action : entry.getValue()) {
list.add(CoreUtilities.toLowerCase(entry.getKey().name() + "/" + action.name()));
}
}
return list;
}
@Override
public String getPropertyString() {
ListTag list = getDisabledSlots();
return list.isEmpty() ? null : list.identify();
}
@Override
public String getPropertyId() {
return "disabled_slots";
}
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <EntityTag.disabled_slots.raw>
// @returns ElementTag(Number)
// @mechanism EntityTag.disabled_slots_raw
// @group properties
// @description
// If the entity is an armor stand, returns its raw disabled slots value.
// See <@link url https://minecraft.gamepedia.com/Armor_Stand/ED>
// -->
if (attribute.startsWith("disabled_slots.raw")) {
return new ElementTag(CustomNBT.getCustomIntNBT(dentity.getBukkitEntity(), CustomNBT.KEY_DISABLED_SLOTS))
.getObjectAttribute(attribute.fulfill(2));
}
// <--[tag]
// @attribute <EntityTag.disabled_slots>
// @returns ListTag
// @mechanism EntityTag.disabled_slots
// @group properties
// @description
// If the entity is an armor stand, returns a list of its disabled slots in the form slot/action|...
// -->
if (attribute.startsWith("disabled_slots")) {
return getDisabledSlots().getObjectAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object EntityTag
// @name disabled_slots_raw
// @input ElementTag(Number)
// @description
// Sets the raw disabled slots value of an armor stand.
// See <@link url https://minecraft.gamepedia.com/Armor_Stand/ED>
// @tags
// <EntityTag.disabled_slots>
// <EntityTag.disabled_slots.raw>
// -->
if (mechanism.matches("disabled_slots_raw") && mechanism.requireInteger()) {
CustomNBT.addCustomNBT(dentity.getBukkitEntity(), CustomNBT.KEY_DISABLED_SLOTS, mechanism.getValue().asInt());
}
// <--[mechanism]
// @object EntityTag
// @name disabled_slots
// @input ListTag
// @description
// Sets the disabled slots of an armor stand in the form slot(/action)|...
// Optionally include an action to disable specific interactions (defaults to ALL).
// Leave empty to enable all slots.
// Slots: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/EquipmentSlot.html>
// Actions: ALL, REMOVE, PLACE
// NOTE: Minecraft contains a bug where disabling HAND/ALL still allows item removal.
// To fully disable hand interaction, disable HAND/ALL and HAND/REMOVE.
// @tags
// <EntityTag.disabled_slots>
// <EntityTag.disabled_slots.raw>
// -->
if (mechanism.matches("disabled_slots")) {
if (!mechanism.hasValue()) {
CustomNBT.removeCustomNBT(dentity.getBukkitEntity(), CustomNBT.KEY_DISABLED_SLOTS);
return;
}
ListTag list = mechanism.valueAsType(ListTag.class);
Map<EquipmentSlot, Set<Action>> map = new HashMap<>();
for (String string : list) {
String[] split = string.toUpperCase().split("/", 2);
EquipmentSlot slot;
Action action = null;
try {
slot = EquipmentSlot.valueOf(split[0]);
}
catch (IllegalArgumentException e) {
mechanism.echoError("Invalid equipment slot specified: " + split[0]);
continue;
}
if (split.length == 2) {
try {
action = Action.valueOf(split[1]);
}
catch (IllegalArgumentException e) {
mechanism.echoError("Invalid action specified: " + split[1]);
continue;
}
}
Set<Action> set = map.computeIfAbsent(slot, k -> new HashSet<>());
set.add(action == null ? Action.ALL : action);
}
CustomNBT.setDisabledSlots(dentity.getBukkitEntity(), map);
}
}
}