Skip to content

Commit

Permalink
Use NamespacedKey wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
PseudoKnight committed Sep 23, 2023
1 parent d10e2e5 commit ccc9f36
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 51 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/laytonsmith/abstraction/Convertor.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,16 @@ public interface Convertor {
* @return The username
*/
String GetUser(Environment env);

/**
* Returns a Minecraft namespaced key object from a string.
* The key can only alphanumeric characters, dots, underscores, and dashes.
* A preceding namespace can be delimited with a single colon, which can also have forward slashes.
* Example: "path/commandhelper:my_tag".
* If no namespace is given, it will default to "commandhelper".
*
* @param key a string formatted key
* @return a key object
*/
MCNamespacedKey GetNamespacedKey(String key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.laytonsmith.abstraction;

public interface MCNamespacedKey extends AbstractionObject {
}
16 changes: 7 additions & 9 deletions src/main/java/com/laytonsmith/abstraction/MCTagContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.laytonsmith.abstraction.enums.MCTagType;

import java.util.Collection;
import java.util.Set;

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

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

/**
* Returns the tag type with the given key.
Expand All @@ -31,7 +29,7 @@ public interface MCTagContainer extends AbstractionObject {
* @param key the tag key
* @return the type for the tag
*/
MCTagType getType(Object key);
MCTagType getType(MCNamespacedKey key);

/**
* Returns the tag value with the given key and tag type.
Expand All @@ -40,7 +38,7 @@ public interface MCTagContainer extends AbstractionObject {
* @param type the tag type
* @return the value for the tag
*/
Object get(Object key, MCTagType type);
Object get(MCNamespacedKey key, MCTagType type);

/**
* Sets the tag value with the given key and tag type.
Expand All @@ -49,13 +47,13 @@ public interface MCTagContainer extends AbstractionObject {
* @param type the tag type
* @param value the tag value
*/
void set(Object key, MCTagType type, Object value);
void set(MCNamespacedKey key, MCTagType type, Object value);

/**
* Deletes the tag with the given key from this container.
* @param key the tag key
*/
void remove(Object key);
void remove(MCNamespacedKey key);

/**
* Creates a new tag container from this container context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCLocation;
import com.laytonsmith.abstraction.MCMetadataValue;
import com.laytonsmith.abstraction.MCNamespacedKey;
import com.laytonsmith.abstraction.MCNote;
import com.laytonsmith.abstraction.MCPattern;
import com.laytonsmith.abstraction.MCPlugin;
Expand Down Expand Up @@ -871,4 +872,9 @@ public String GetUser(Environment env) {
return name;
}
}

@Override
public MCNamespacedKey GetNamespacedKey(String key) {
return new BukkitMCNamespacedKey(NamespacedKey.fromString(key, CommandHelperPlugin.self));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.laytonsmith.abstraction.bukkit;

import com.laytonsmith.abstraction.MCNamespacedKey;
import org.bukkit.NamespacedKey;

public class BukkitMCNamespacedKey implements MCNamespacedKey {

NamespacedKey nsk;

public BukkitMCNamespacedKey(NamespacedKey nsk) {
this.nsk = nsk;
}

@Override
public Object getHandle() {
return this.nsk;
}

@Override
public String toString() {
return this.nsk.toString();
}

@Override
public boolean equals(Object obj) {
return obj instanceof MCNamespacedKey && this.nsk.equals(((MCNamespacedKey) obj).getHandle());
}

@Override
public int hashCode() {
return this.nsk.hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.laytonsmith.abstraction.bukkit;

import com.laytonsmith.abstraction.MCNamespacedKey;
import com.laytonsmith.abstraction.MCTagContainer;
import com.laytonsmith.abstraction.enums.MCTagType;
import com.laytonsmith.commandhelper.CommandHelperPlugin;
import org.bukkit.NamespacedKey;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class BukkitMCTagContainer implements MCTagContainer {

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

@Override
public MCTagContainer newContainer() {
return new BukkitMCTagContainer(pdc.getAdapterContext().newPersistentDataContainer());
return new BukkitMCTagContainer(this.pdc.getAdapterContext().newPersistentDataContainer());
}

@Override
Expand All @@ -28,46 +29,50 @@ public boolean isEmpty() {
}

@Override
public Collection getKeys() {
return pdc.getKeys();
public Set<MCNamespacedKey> getKeys() {
Set<MCNamespacedKey> keys = new HashSet<>();
for(NamespacedKey key : this.pdc.getKeys()) {
keys.add(new BukkitMCNamespacedKey(key));
}
return keys;
}

@Override
public MCTagType getType(Object key) {
NamespacedKey namespacedKey = NamespacedKey(key);
public MCTagType getType(MCNamespacedKey key) {
NamespacedKey namespacedKey = (NamespacedKey) key.getHandle();
// Check tag types in order of most frequently used
if(pdc.has(namespacedKey, PersistentDataType.STRING)) {
if(this.pdc.has(namespacedKey, PersistentDataType.STRING)) {
return MCTagType.STRING;
} else if(pdc.has(namespacedKey, PersistentDataType.INTEGER)) {
} else if(this.pdc.has(namespacedKey, PersistentDataType.INTEGER)) {
return MCTagType.INTEGER;
} else if(pdc.has(namespacedKey, PersistentDataType.BYTE)) {
} else if(this.pdc.has(namespacedKey, PersistentDataType.BYTE)) {
return MCTagType.BYTE;
} else if(pdc.has(namespacedKey, PersistentDataType.DOUBLE)) {
} else if(this.pdc.has(namespacedKey, PersistentDataType.DOUBLE)) {
return MCTagType.DOUBLE;
} else if(pdc.has(namespacedKey, PersistentDataType.LONG)) {
} else if(this.pdc.has(namespacedKey, PersistentDataType.LONG)) {
return MCTagType.LONG;
} else if(pdc.has(namespacedKey, PersistentDataType.FLOAT)) {
} else if(this.pdc.has(namespacedKey, PersistentDataType.FLOAT)) {
return MCTagType.FLOAT;
} else if(pdc.has(namespacedKey, PersistentDataType.TAG_CONTAINER)) {
} else if(this.pdc.has(namespacedKey, PersistentDataType.TAG_CONTAINER)) {
return MCTagType.TAG_CONTAINER;
} else if(pdc.has(namespacedKey, PersistentDataType.BYTE_ARRAY)) {
} else if(this.pdc.has(namespacedKey, PersistentDataType.BYTE_ARRAY)) {
return MCTagType.BYTE_ARRAY;
} else if(pdc.has(namespacedKey, PersistentDataType.SHORT)) {
} else if(this.pdc.has(namespacedKey, PersistentDataType.SHORT)) {
return MCTagType.SHORT;
} else if(pdc.has(namespacedKey, PersistentDataType.INTEGER_ARRAY)) {
} else if(this.pdc.has(namespacedKey, PersistentDataType.INTEGER_ARRAY)) {
return MCTagType.INTEGER_ARRAY;
} else if(pdc.has(namespacedKey, PersistentDataType.LONG_ARRAY)) {
} else if(this.pdc.has(namespacedKey, PersistentDataType.LONG_ARRAY)) {
return MCTagType.LONG_ARRAY;
} else if(pdc.has(namespacedKey, PersistentDataType.TAG_CONTAINER_ARRAY)) {
} else if(this.pdc.has(namespacedKey, PersistentDataType.TAG_CONTAINER_ARRAY)) {
return MCTagType.TAG_CONTAINER_ARRAY;
}
return null;
}

@Override
public Object get(Object key, MCTagType type) {
public Object get(MCNamespacedKey key, MCTagType type) {
PersistentDataType bukkitType = GetPersistentDataType(type);
Object value = pdc.get(NamespacedKey(key), bukkitType);
Object value = this.pdc.get((NamespacedKey) key.getHandle(), bukkitType);
if(value instanceof PersistentDataContainer) {
return new BukkitMCTagContainer((PersistentDataContainer) value);
} else if(value instanceof PersistentDataContainer[] concreteContainers) {
Expand All @@ -81,7 +86,7 @@ public Object get(Object key, MCTagType type) {
}

@Override
public void set(Object key, MCTagType type, Object value) {
public void set(MCNamespacedKey key, MCTagType type, Object value) {
PersistentDataType bukkitType = GetPersistentDataType(type);
if(value instanceof MCTagContainer) {
value = ((MCTagContainer) value).getHandle();
Expand All @@ -92,24 +97,12 @@ public void set(Object key, MCTagType type, Object value) {
}
value = concreteContainers;
}
pdc.set(NamespacedKey(key), bukkitType, value);
this.pdc.set((NamespacedKey) key.getHandle(), bukkitType, value);
}

@Override
public void remove(Object key) {
pdc.remove(NamespacedKey(key));
}

private static NamespacedKey NamespacedKey(Object key) {
if(key instanceof NamespacedKey) {
return (NamespacedKey) key;
} else if(key instanceof String) {
NamespacedKey namespacedKey = NamespacedKey.fromString((String) key, CommandHelperPlugin.self);
if(namespacedKey != null) {
return namespacedKey;
}
}
throw new IllegalArgumentException("Invalid namespaced key.");
public void remove(MCNamespacedKey key) {
this.pdc.remove((NamespacedKey) key.getHandle());
}

private static PersistentDataType GetPersistentDataType(MCTagType type) {
Expand Down Expand Up @@ -144,7 +137,7 @@ private static PersistentDataType GetPersistentDataType(MCTagType type) {

@Override
public Object getHandle() {
return pdc;
return this.pdc;
}

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

@Override
public int hashCode() {
return pdc.hashCode();
return this.pdc.hashCode();
}

@Override
public String toString() {
return pdc.toString();
return this.pdc.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.laytonsmith.abstraction.enums;

import com.laytonsmith.abstraction.MCNamespacedKey;
import com.laytonsmith.abstraction.MCTagContainer;
import com.laytonsmith.abstraction.StaticLayer;
import com.laytonsmith.core.ArgumentValidation;
import com.laytonsmith.core.constructs.CArray;
import com.laytonsmith.core.constructs.CDouble;
Expand Down Expand Up @@ -163,7 +165,7 @@ public Object convert(MCTagContainer container, Mixed value) {
tagValue = tagType.convert(container, entryValue);
}
try {
container.set(key, tagType, tagValue);
container.set(StaticLayer.GetConvertor().GetNamespacedKey(key), tagType, tagValue);
} catch (ClassCastException ex) {
throw new CREFormatException("Tag value does not match expected type.", entryValue.getTarget());
} catch (IllegalArgumentException ex) {
Expand Down Expand Up @@ -199,7 +201,7 @@ public Mixed construct(Object value) throws ClassCastException {
if(this == TAG_CONTAINER) {
MCTagContainer container = (MCTagContainer) value;
CArray containerArray = CArray.GetAssociativeArray(Target.UNKNOWN);
for(Object key : container.getKeys()) {
for(MCNamespacedKey key : container.getKeys()) {
CArray entry = CArray.GetAssociativeArray(Target.UNKNOWN);
MCTagType type = container.getType(key);
entry.set("type", type.name(), Target.UNKNOWN);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/laytonsmith/tools/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCLocation;
import com.laytonsmith.abstraction.MCMetadataValue;
import com.laytonsmith.abstraction.MCNamespacedKey;
import com.laytonsmith.abstraction.MCNote;
import com.laytonsmith.abstraction.MCPattern;
import com.laytonsmith.abstraction.MCPlugin;
Expand Down Expand Up @@ -1291,6 +1292,11 @@ public MCPlugin GetPlugin() {
public String GetUser(Environment env) {
return System.getProperty("user.name");
}

@Override
public MCNamespacedKey GetNamespacedKey(String key) {
throw new UnsupportedOperationException("This method is not supported from a shell.");
}
}

}
6 changes: 6 additions & 0 deletions src/test/java/com/laytonsmith/testing/StaticTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCLocation;
import com.laytonsmith.abstraction.MCMetadataValue;
import com.laytonsmith.abstraction.MCNamespacedKey;
import com.laytonsmith.abstraction.MCNote;
import com.laytonsmith.abstraction.MCPattern;
import com.laytonsmith.abstraction.MCPlayer;
Expand Down Expand Up @@ -890,6 +891,11 @@ public MCPlugin GetPlugin() {
public String GetUser(Environment env) {
return "testUser";
}

@Override
public MCNamespacedKey GetNamespacedKey(String key) {
throw new UnsupportedOperationException("Not supported yet.");
}
}

public static class FakeServerMixin implements EventMixinInterface {
Expand Down

0 comments on commit ccc9f36

Please sign in to comment.