Skip to content

Commit ccc9f36

Browse files
committed
Use NamespacedKey wrapper
1 parent d10e2e5 commit ccc9f36

File tree

9 files changed

+111
-51
lines changed

9 files changed

+111
-51
lines changed

src/main/java/com/laytonsmith/abstraction/Convertor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,16 @@ public interface Convertor {
269269
* @return The username
270270
*/
271271
String GetUser(Environment env);
272+
273+
/**
274+
* Returns a Minecraft namespaced key object from a string.
275+
* The key can only alphanumeric characters, dots, underscores, and dashes.
276+
* A preceding namespace can be delimited with a single colon, which can also have forward slashes.
277+
* Example: "path/commandhelper:my_tag".
278+
* If no namespace is given, it will default to "commandhelper".
279+
*
280+
* @param key a string formatted key
281+
* @return a key object
282+
*/
283+
MCNamespacedKey GetNamespacedKey(String key);
272284
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.laytonsmith.abstraction;
2+
3+
public interface MCNamespacedKey extends AbstractionObject {
4+
}

src/main/java/com/laytonsmith/abstraction/MCTagContainer.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.laytonsmith.abstraction.enums.MCTagType;
44

5-
import java.util.Collection;
5+
import java.util.Set;
66

77
/**
88
* Minecraft NBT containers that can be used to read and modify tags in supported game objects.
@@ -17,12 +17,10 @@ public interface MCTagContainer extends AbstractionObject {
1717
boolean isEmpty();
1818

1919
/**
20-
* Gets a set of key objects for each tag that exists in this container.
21-
* These are minecraft formatted namespaced keys. (e.g. "namespace:key")
22-
* These key objects can be passed to other methods in this class.
20+
* Gets a set of namespaced keys for each tag that exists in this container. (e.g. "namespace:key")
2321
* @return a set of keys
2422
*/
25-
Collection getKeys();
23+
Set<MCNamespacedKey> getKeys();
2624

2725
/**
2826
* Returns the tag type with the given key.
@@ -31,7 +29,7 @@ public interface MCTagContainer extends AbstractionObject {
3129
* @param key the tag key
3230
* @return the type for the tag
3331
*/
34-
MCTagType getType(Object key);
32+
MCTagType getType(MCNamespacedKey key);
3533

3634
/**
3735
* Returns the tag value with the given key and tag type.
@@ -40,7 +38,7 @@ public interface MCTagContainer extends AbstractionObject {
4038
* @param type the tag type
4139
* @return the value for the tag
4240
*/
43-
Object get(Object key, MCTagType type);
41+
Object get(MCNamespacedKey key, MCTagType type);
4442

4543
/**
4644
* Sets the tag value with the given key and tag type.
@@ -49,13 +47,13 @@ public interface MCTagContainer extends AbstractionObject {
4947
* @param type the tag type
5048
* @param value the tag value
5149
*/
52-
void set(Object key, MCTagType type, Object value);
50+
void set(MCNamespacedKey key, MCTagType type, Object value);
5351

5452
/**
5553
* Deletes the tag with the given key from this container.
5654
* @param key the tag key
5755
*/
58-
void remove(Object key);
56+
void remove(MCNamespacedKey key);
5957

6058
/**
6159
* Creates a new tag container from this container context.

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitConvertor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.laytonsmith.abstraction.MCItemStack;
1919
import com.laytonsmith.abstraction.MCLocation;
2020
import com.laytonsmith.abstraction.MCMetadataValue;
21+
import com.laytonsmith.abstraction.MCNamespacedKey;
2122
import com.laytonsmith.abstraction.MCNote;
2223
import com.laytonsmith.abstraction.MCPattern;
2324
import com.laytonsmith.abstraction.MCPlugin;
@@ -871,4 +872,9 @@ public String GetUser(Environment env) {
871872
return name;
872873
}
873874
}
875+
876+
@Override
877+
public MCNamespacedKey GetNamespacedKey(String key) {
878+
return new BukkitMCNamespacedKey(NamespacedKey.fromString(key, CommandHelperPlugin.self));
879+
}
874880
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.laytonsmith.abstraction.bukkit;
2+
3+
import com.laytonsmith.abstraction.MCNamespacedKey;
4+
import org.bukkit.NamespacedKey;
5+
6+
public class BukkitMCNamespacedKey implements MCNamespacedKey {
7+
8+
NamespacedKey nsk;
9+
10+
public BukkitMCNamespacedKey(NamespacedKey nsk) {
11+
this.nsk = nsk;
12+
}
13+
14+
@Override
15+
public Object getHandle() {
16+
return this.nsk;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return this.nsk.toString();
22+
}
23+
24+
@Override
25+
public boolean equals(Object obj) {
26+
return obj instanceof MCNamespacedKey && this.nsk.equals(((MCNamespacedKey) obj).getHandle());
27+
}
28+
29+
@Override
30+
public int hashCode() {
31+
return this.nsk.hashCode();
32+
}
33+
}

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCTagContainer.java

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.laytonsmith.abstraction.bukkit;
22

3+
import com.laytonsmith.abstraction.MCNamespacedKey;
34
import com.laytonsmith.abstraction.MCTagContainer;
45
import com.laytonsmith.abstraction.enums.MCTagType;
5-
import com.laytonsmith.commandhelper.CommandHelperPlugin;
66
import org.bukkit.NamespacedKey;
77
import org.bukkit.persistence.PersistentDataContainer;
88
import org.bukkit.persistence.PersistentDataType;
99

10-
import java.util.Collection;
10+
import java.util.HashSet;
11+
import java.util.Set;
1112

1213
public class BukkitMCTagContainer implements MCTagContainer {
1314

@@ -19,7 +20,7 @@ public BukkitMCTagContainer(PersistentDataContainer pdc) {
1920

2021
@Override
2122
public MCTagContainer newContainer() {
22-
return new BukkitMCTagContainer(pdc.getAdapterContext().newPersistentDataContainer());
23+
return new BukkitMCTagContainer(this.pdc.getAdapterContext().newPersistentDataContainer());
2324
}
2425

2526
@Override
@@ -28,46 +29,50 @@ public boolean isEmpty() {
2829
}
2930

3031
@Override
31-
public Collection getKeys() {
32-
return pdc.getKeys();
32+
public Set<MCNamespacedKey> getKeys() {
33+
Set<MCNamespacedKey> keys = new HashSet<>();
34+
for(NamespacedKey key : this.pdc.getKeys()) {
35+
keys.add(new BukkitMCNamespacedKey(key));
36+
}
37+
return keys;
3338
}
3439

3540
@Override
36-
public MCTagType getType(Object key) {
37-
NamespacedKey namespacedKey = NamespacedKey(key);
41+
public MCTagType getType(MCNamespacedKey key) {
42+
NamespacedKey namespacedKey = (NamespacedKey) key.getHandle();
3843
// Check tag types in order of most frequently used
39-
if(pdc.has(namespacedKey, PersistentDataType.STRING)) {
44+
if(this.pdc.has(namespacedKey, PersistentDataType.STRING)) {
4045
return MCTagType.STRING;
41-
} else if(pdc.has(namespacedKey, PersistentDataType.INTEGER)) {
46+
} else if(this.pdc.has(namespacedKey, PersistentDataType.INTEGER)) {
4247
return MCTagType.INTEGER;
43-
} else if(pdc.has(namespacedKey, PersistentDataType.BYTE)) {
48+
} else if(this.pdc.has(namespacedKey, PersistentDataType.BYTE)) {
4449
return MCTagType.BYTE;
45-
} else if(pdc.has(namespacedKey, PersistentDataType.DOUBLE)) {
50+
} else if(this.pdc.has(namespacedKey, PersistentDataType.DOUBLE)) {
4651
return MCTagType.DOUBLE;
47-
} else if(pdc.has(namespacedKey, PersistentDataType.LONG)) {
52+
} else if(this.pdc.has(namespacedKey, PersistentDataType.LONG)) {
4853
return MCTagType.LONG;
49-
} else if(pdc.has(namespacedKey, PersistentDataType.FLOAT)) {
54+
} else if(this.pdc.has(namespacedKey, PersistentDataType.FLOAT)) {
5055
return MCTagType.FLOAT;
51-
} else if(pdc.has(namespacedKey, PersistentDataType.TAG_CONTAINER)) {
56+
} else if(this.pdc.has(namespacedKey, PersistentDataType.TAG_CONTAINER)) {
5257
return MCTagType.TAG_CONTAINER;
53-
} else if(pdc.has(namespacedKey, PersistentDataType.BYTE_ARRAY)) {
58+
} else if(this.pdc.has(namespacedKey, PersistentDataType.BYTE_ARRAY)) {
5459
return MCTagType.BYTE_ARRAY;
55-
} else if(pdc.has(namespacedKey, PersistentDataType.SHORT)) {
60+
} else if(this.pdc.has(namespacedKey, PersistentDataType.SHORT)) {
5661
return MCTagType.SHORT;
57-
} else if(pdc.has(namespacedKey, PersistentDataType.INTEGER_ARRAY)) {
62+
} else if(this.pdc.has(namespacedKey, PersistentDataType.INTEGER_ARRAY)) {
5863
return MCTagType.INTEGER_ARRAY;
59-
} else if(pdc.has(namespacedKey, PersistentDataType.LONG_ARRAY)) {
64+
} else if(this.pdc.has(namespacedKey, PersistentDataType.LONG_ARRAY)) {
6065
return MCTagType.LONG_ARRAY;
61-
} else if(pdc.has(namespacedKey, PersistentDataType.TAG_CONTAINER_ARRAY)) {
66+
} else if(this.pdc.has(namespacedKey, PersistentDataType.TAG_CONTAINER_ARRAY)) {
6267
return MCTagType.TAG_CONTAINER_ARRAY;
6368
}
6469
return null;
6570
}
6671

6772
@Override
68-
public Object get(Object key, MCTagType type) {
73+
public Object get(MCNamespacedKey key, MCTagType type) {
6974
PersistentDataType bukkitType = GetPersistentDataType(type);
70-
Object value = pdc.get(NamespacedKey(key), bukkitType);
75+
Object value = this.pdc.get((NamespacedKey) key.getHandle(), bukkitType);
7176
if(value instanceof PersistentDataContainer) {
7277
return new BukkitMCTagContainer((PersistentDataContainer) value);
7378
} else if(value instanceof PersistentDataContainer[] concreteContainers) {
@@ -81,7 +86,7 @@ public Object get(Object key, MCTagType type) {
8186
}
8287

8388
@Override
84-
public void set(Object key, MCTagType type, Object value) {
89+
public void set(MCNamespacedKey key, MCTagType type, Object value) {
8590
PersistentDataType bukkitType = GetPersistentDataType(type);
8691
if(value instanceof MCTagContainer) {
8792
value = ((MCTagContainer) value).getHandle();
@@ -92,24 +97,12 @@ public void set(Object key, MCTagType type, Object value) {
9297
}
9398
value = concreteContainers;
9499
}
95-
pdc.set(NamespacedKey(key), bukkitType, value);
100+
this.pdc.set((NamespacedKey) key.getHandle(), bukkitType, value);
96101
}
97102

98103
@Override
99-
public void remove(Object key) {
100-
pdc.remove(NamespacedKey(key));
101-
}
102-
103-
private static NamespacedKey NamespacedKey(Object key) {
104-
if(key instanceof NamespacedKey) {
105-
return (NamespacedKey) key;
106-
} else if(key instanceof String) {
107-
NamespacedKey namespacedKey = NamespacedKey.fromString((String) key, CommandHelperPlugin.self);
108-
if(namespacedKey != null) {
109-
return namespacedKey;
110-
}
111-
}
112-
throw new IllegalArgumentException("Invalid namespaced key.");
104+
public void remove(MCNamespacedKey key) {
105+
this.pdc.remove((NamespacedKey) key.getHandle());
113106
}
114107

115108
private static PersistentDataType GetPersistentDataType(MCTagType type) {
@@ -144,7 +137,7 @@ private static PersistentDataType GetPersistentDataType(MCTagType type) {
144137

145138
@Override
146139
public Object getHandle() {
147-
return pdc;
140+
return this.pdc;
148141
}
149142

150143
@Override
@@ -154,11 +147,11 @@ public boolean equals(Object o) {
154147

155148
@Override
156149
public int hashCode() {
157-
return pdc.hashCode();
150+
return this.pdc.hashCode();
158151
}
159152

160153
@Override
161154
public String toString() {
162-
return pdc.toString();
155+
return this.pdc.toString();
163156
}
164157
}

src/main/java/com/laytonsmith/abstraction/enums/MCTagType.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.laytonsmith.abstraction.enums;
22

3+
import com.laytonsmith.abstraction.MCNamespacedKey;
34
import com.laytonsmith.abstraction.MCTagContainer;
5+
import com.laytonsmith.abstraction.StaticLayer;
46
import com.laytonsmith.core.ArgumentValidation;
57
import com.laytonsmith.core.constructs.CArray;
68
import com.laytonsmith.core.constructs.CDouble;
@@ -163,7 +165,7 @@ public Object convert(MCTagContainer container, Mixed value) {
163165
tagValue = tagType.convert(container, entryValue);
164166
}
165167
try {
166-
container.set(key, tagType, tagValue);
168+
container.set(StaticLayer.GetConvertor().GetNamespacedKey(key), tagType, tagValue);
167169
} catch (ClassCastException ex) {
168170
throw new CREFormatException("Tag value does not match expected type.", entryValue.getTarget());
169171
} catch (IllegalArgumentException ex) {
@@ -199,7 +201,7 @@ public Mixed construct(Object value) throws ClassCastException {
199201
if(this == TAG_CONTAINER) {
200202
MCTagContainer container = (MCTagContainer) value;
201203
CArray containerArray = CArray.GetAssociativeArray(Target.UNKNOWN);
202-
for(Object key : container.getKeys()) {
204+
for(MCNamespacedKey key : container.getKeys()) {
203205
CArray entry = CArray.GetAssociativeArray(Target.UNKNOWN);
204206
MCTagType type = container.getType(key);
205207
entry.set("type", type.name(), Target.UNKNOWN);

src/main/java/com/laytonsmith/tools/Interpreter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.laytonsmith.abstraction.MCItemStack;
3131
import com.laytonsmith.abstraction.MCLocation;
3232
import com.laytonsmith.abstraction.MCMetadataValue;
33+
import com.laytonsmith.abstraction.MCNamespacedKey;
3334
import com.laytonsmith.abstraction.MCNote;
3435
import com.laytonsmith.abstraction.MCPattern;
3536
import com.laytonsmith.abstraction.MCPlugin;
@@ -1291,6 +1292,11 @@ public MCPlugin GetPlugin() {
12911292
public String GetUser(Environment env) {
12921293
return System.getProperty("user.name");
12931294
}
1295+
1296+
@Override
1297+
public MCNamespacedKey GetNamespacedKey(String key) {
1298+
throw new UnsupportedOperationException("This method is not supported from a shell.");
1299+
}
12941300
}
12951301

12961302
}

src/test/java/com/laytonsmith/testing/StaticTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.laytonsmith.abstraction.MCItemStack;
2222
import com.laytonsmith.abstraction.MCLocation;
2323
import com.laytonsmith.abstraction.MCMetadataValue;
24+
import com.laytonsmith.abstraction.MCNamespacedKey;
2425
import com.laytonsmith.abstraction.MCNote;
2526
import com.laytonsmith.abstraction.MCPattern;
2627
import com.laytonsmith.abstraction.MCPlayer;
@@ -890,6 +891,11 @@ public MCPlugin GetPlugin() {
890891
public String GetUser(Environment env) {
891892
return "testUser";
892893
}
894+
895+
@Override
896+
public MCNamespacedKey GetNamespacedKey(String key) {
897+
throw new UnsupportedOperationException("Not supported yet.");
898+
}
893899
}
894900

895901
public static class FakeServerMixin implements EventMixinInterface {

0 commit comments

Comments
 (0)