-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
InventoryTitle.java
122 lines (107 loc) · 4.25 KB
/
InventoryTitle.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
package com.denizenscript.denizen.objects.properties.inventory;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.InventoryTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.Bukkit;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack;
public class InventoryTitle implements Property {
public static boolean describes(ObjectTag inventory) {
// All inventories could possibly have a title
return inventory instanceof InventoryTag;
}
public static InventoryTitle getFrom(ObjectTag inventory) {
if (!describes(inventory)) {
return null;
}
return new InventoryTitle((InventoryTag) inventory);
}
public static final String[] handledMechs = new String[] {
"title"
};
InventoryTag inventory;
public InventoryTitle(InventoryTag inventory) {
this.inventory = inventory;
}
public String getTitle() {
if (inventory.getInventory() != null) {
String title = NMSHandler.getInstance().getTitle(inventory.getInventory());
if (title != null) {
if (!title.startsWith("container.")) {
return title;
}
}
}
return null;
}
@Override
public String getPropertyString() {
// Only show a property string for titles that can actually change
if (inventory.isGeneric() || inventory.isSaving) {
return getTitle();
}
else {
return null;
}
}
@Override
public String getPropertyId() {
return "title";
}
public static void registerTags() {
// <--[tag]
// @attribute <InventoryTag.title>
// @returns ElementTag
// @group properties
// @mechanism InventoryTag.title
// @description
// Returns the title of the inventory.
// -->
PropertyParser.<InventoryTitle>registerTag("title", (attribute, inventory) -> {
return new ElementTag(inventory.getTitle());
});
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object InventoryTag
// @name title
// @input ElementTag
// @description
// Sets the title of the inventory. (Only works for "generic" inventories.)
// @tags
// <InventoryTag.title>
// -->
if (mechanism.matches("title")) {
if (!inventory.isGeneric() && !inventory.isUnique()) {
Debug.echoError("Cannot set a title on a non-generic inventory.");
return;
}
String title = mechanism.getValue().asString();
if (inventory.getInventory() != null && NMSHandler.getInstance().getTitle(inventory.getInventory()).equals(title)) {
return;
}
inventory.uniquifier = null;
if (inventory.getInventory() == null) {
inventory.setInventory(Bukkit.getServer().createInventory(null, InventoryTag.maxSlots, title));
InventoryTag.trackTemporaryInventory(inventory);
return;
}
ItemStack[] contents = inventory.getContents();
if (inventory.getInventory().getType() == InventoryType.CHEST) {
inventory.setInventory(Bukkit.getServer().createInventory(null, inventory.getSize(), title));
}
else {
inventory.setInventory(Bukkit.getServer().createInventory(null, inventory.getInventory().getType(), title));
}
inventory.setContents(contents);
InventoryTag.trackTemporaryInventory(inventory);
Debug.log("Set title: " + NMSHandler.getInstance().getTitle(inventory.getInventory()));
}
}
}