-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ItemBaseColor.java
146 lines (128 loc) · 4.88 KB
/
ItemBaseColor.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
package com.denizenscript.denizen.objects.properties.item;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.TagContext;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.Banner;
import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
public class ItemBaseColor implements Property {
public static boolean describes(ObjectTag item) {
if (item instanceof ItemTag) {
return ((ItemTag) item).getBukkitMaterial() == Material.SHIELD;
}
return false;
}
public static ItemBaseColor getFrom(ObjectTag item) {
if (!describes(item)) {
return null;
}
else {
return new ItemBaseColor((ItemTag) item);
}
}
public static final String[] handledTags = new String[] {
"base_color"
};
public static final String[] handledMechs = new String[] {
"base_color"
};
public ItemBaseColor(ItemTag item) {
this.item = item;
}
ItemTag item;
public DyeColor getBaseColor() {
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta instanceof BlockStateMeta) {
DyeColor color = ((Banner) ((BlockStateMeta) itemMeta).getBlockState()).getBaseColor();
if (color == DyeColor.WHITE && item.getBukkitMaterial() == Material.SHIELD) { // Hack to avoid blank shields misdisplaying as white
if (ItemRawNBT.getFrom(item).getFullNBTMap().getObject("BlockEntityTag") == null) {
return null;
}
}
return color;
}
else {
return ((BannerMeta) itemMeta).getBaseColor();
}
}
public void setBaseColor(DyeColor color, TagContext context) {
if (color == null && item.getBukkitMaterial() == Material.SHIELD) {
ItemRawNBT property = ItemRawNBT.getFrom(item);
MapTag nbt = property.getFullNBTMap();
nbt.putObject("BlockEntityTag", null);
property.setFullNBT(item, nbt, context, false);
return;
}
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta instanceof BlockStateMeta) {
Banner banner = (Banner) ((BlockStateMeta) itemMeta).getBlockState();
banner.setBaseColor(color);
banner.update();
((BlockStateMeta) itemMeta).setBlockState(banner);
}
else {
((BannerMeta) itemMeta).setBaseColor(color);
}
item.setItemMeta(itemMeta);
}
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <ItemTag.base_color>
// @returns ElementTag
// @group properties
// @mechanism ItemTag.base_color
// @description
// Gets the base color of a shield.
// For the list of possible colors, see <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/DyeColor.html>.
// -->
if (attribute.startsWith("base_color")) {
DyeColor baseColor = getBaseColor();
if (baseColor != null) {
return new ElementTag(baseColor).getObjectAttribute(attribute.fulfill(1));
}
return null;
}
return null;
}
@Override
public String getPropertyString() {
DyeColor baseColor = getBaseColor();
if (baseColor != null) {
return baseColor.name();
}
return null;
}
@Override
public String getPropertyId() {
return "base_color";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name base_color
// @input ElementTag
// @description
// Changes the base color of a shield.
// For the list of possible colors, see <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/DyeColor.html>.
// Give no input with a shield to remove the base color (and any patterns).
// @tags
// <ItemTag.base_color>
// -->
if (mechanism.matches("base_color")) {
setBaseColor(mechanism.hasValue() ? DyeColor.valueOf(mechanism.getValue().asString().toUpperCase()) : null, mechanism.context);
}
}
}