/
ItemHidden.java
151 lines (132 loc) · 4.61 KB
/
ItemHidden.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
package com.denizenscript.denizen.objects.properties.item;
import com.denizenscript.denizen.objects.ItemTag;
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.Deprecations;
import org.bukkit.Material;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.meta.ItemMeta;
public class ItemHidden implements Property {
public static boolean describes(ObjectTag item) {
// All items can have flags
return item instanceof ItemTag && ((ItemTag) item).getItemStack().getType() != Material.AIR;
}
public static ItemHidden getFrom(ObjectTag _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemHidden((ItemTag) _item);
}
}
public static final String[] handledTags = new String[] {
"flags", "hides"
};
public static final String[] handledMechs = new String[] {
"flags", "hides"
};
private ItemHidden(ItemTag _item) {
item = _item;
}
@Deprecated
public ListTag flags() {
ListTag output = new ListTag();
for (ItemFlag flag : item.getItemMeta().getItemFlags()) {
output.add(flag.name());
}
return output;
}
public ListTag hides() {
ListTag output = new ListTag();
for (ItemFlag flag : item.getItemMeta().getItemFlags()) {
if (flag == ItemFlag.HIDE_POTION_EFFECTS) {
output.add("ITEM_DATA");
}
else {
output.add(flag.name().substring("HIDE_".length()));
}
}
return output;
}
ItemTag item;
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <ItemTag.hides>
// @returns ListTag
// @mechanism ItemTag.hides
// @group properties
// @description
// Returns a list of item data types to be hidden from view on this item.
// Valid hide types include: ATTRIBUTES, DESTROYS, ENCHANTS, PLACED_ON, ITEM_DATA, and UNBREAKABLE
// ITEM_DATA hides potion effects, banner patterns, etc.
// -->
if (attribute.startsWith("hides")) {
return hides().getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("flags")) {
Deprecations.itemFlagsProperty.warn(attribute.context);
return flags().getObjectAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public String getPropertyString() {
ListTag hidden = hides();
if (hidden.size() > 0) {
return hidden.identify();
}
else {
return null;
}
}
@Override
public String getPropertyId() {
return "hides";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name hides
// @input ListTag
// @description
// Sets the item's list of data types to hide.
// Valid hide types include: ATTRIBUTES, DESTROYS, ENCHANTS, PLACED_ON, ITEM_DATA, UNBREAKABLE, or ALL.
// ITEM_DATA hides potion effects, banner patterns, etc.
// Use "ALL" to automatically hide all hideable item data.
// @tags
// <ItemTag.hides>
// -->
if (mechanism.matches("flags") || mechanism.matches("hides")) {
if (mechanism.matches("flags")) {
Deprecations.itemFlagsProperty.warn(mechanism.context);
}
ItemMeta meta = item.getItemMeta();
meta.removeItemFlags(ItemFlag.values());
ListTag new_hides = mechanism.valueAsType(ListTag.class);
for (String str : new_hides) {
str = str.toUpperCase();
if (!str.startsWith("HIDE_")) {
str = "HIDE_" + str;
}
if (str.equals("HIDE_ALL")) {
meta.addItemFlags(ItemFlag.values());
}
else if (str.equals("HIDE_ITEM_DATA")) {
meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
}
else {
meta.addItemFlags(ItemFlag.valueOf(str));
}
}
item.setItemMeta(meta);
}
}
}