-
Notifications
You must be signed in to change notification settings - Fork 6
/
MaterialTag.java
166 lines (141 loc) · 5.17 KB
/
MaterialTag.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
package com.denizenscript.clientizen.objects;
import com.denizenscript.clientizen.util.Utilities;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
public class MaterialTag implements ObjectTag, Adjustable {
// <--[ObjectType]
// @name MaterialTag
// @prefix m
// @base ElementTag
// @implements PropertyHolderObject
// @ExampleTagBase material[stone]
// @ExampleValues stone,dirt,stick,iron_sword
// @ExampleForReturns
// - narrate "The material is %VALUE%!"
// @format
// The identity format for materials is the material type name.
// For example, 'm@stick'.
//
// @description
// A MaterialTag represents a type of block or item.
//
// Block materials may sometimes also contain property data
// for specific values on the block material such as the growth stage of a plant or the orientation of a stair block.
//
// Material types: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html>.
//
// -->
// Needs to match the server-side impl, which uses bukkit's all-in-one Material enum
public BlockState state;
public Item item;
public MaterialTag(BlockState state) {
this.state = state;
}
public MaterialTag(Block block) {
this.state = block.getDefaultState();
}
public MaterialTag(Item item) {
this.item = item;
}
@Fetchable("m")
public static MaterialTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
if (ObjectFetcher.isObjectWithProperties(string)) {
return ObjectFetcher.getObjectFromWithProperties(ClientizenObjectRegistry.TYPE_MATERIAL, string, context);
}
if (string.startsWith("m@")) {
string = string.substring("m@".length());
}
Identifier identifier = Identifier.tryParse(string);
if (identifier == null) {
if (context == null || context.showErrors()) {
Debug.echoError("valueOf MaterialTag returning null, invalid material/item name or identifier specified. For input: " + string);
}
return null;
}
MaterialTag material = Registries.BLOCK.getOrEmpty(identifier).map(MaterialTag::new)
.orElseGet(() -> Registries.ITEM.getOrEmpty(identifier).map(MaterialTag::new).orElse(null));
if (material != null) {
return material;
}
if (context == null || context.showErrors()) {
Debug.echoError("valueOf MaterialTag returning null, invalid material/item specified. For input: " + string);
}
return null;
}
public static boolean matches(String string) {
if (string.startsWith("m@")) {
return true;
}
return valueOf(string, CoreUtilities.noDebugContext) != null;
}
public String getName() {
return Utilities.idToString(state != null ? Registries.BLOCK.getId(state.getBlock()) : Registries.ITEM.getId(item));
}
public static void register() {
PropertyParser.registerPropertyTagHandlers(MaterialTag.class, tagProcessor);
tagProcessor.registerTag(ElementTag.class, "name", (attribute, object) -> {
return new ElementTag(object.getName());
});
}
public static final ObjectTagProcessor<MaterialTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
@Override
public void adjust(Mechanism mechanism) {
tagProcessor.processMechanism(this, mechanism);
}
@Override
public void applyProperty(Mechanism mechanism) {
adjust(mechanism);
}
@Override
public String identify() {
return "m@" + getName() + PropertyParser.getPropertiesString(this);
}
@Override
public String identifySimple() {
return "m@" + getName();
}
@Override
public String debuggable() {
return "<LG>m@<Y>" + getName() + PropertyParser.getPropertiesDebuggable(this);
}
@Override
public String toString() {
return identify();
}
@Override
public boolean isUnique() {
return false;
}
@Override
public ObjectTag duplicate() {
return state != null ? new MaterialTag(state) : new MaterialTag(item);
}
private String prefix = "Material";
@Override
public String getPrefix() {
return prefix;
}
@Override
public ObjectTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
}