Archiviert
13
0

Add in basic metadata API

Fixes #80
Dieser Commit ist enthalten in:
Dan Mulloy 2015-04-26 15:47:44 -04:00
Ursprung e4d1f8d992
Commit 1da65317c4

Datei anzeigen

@ -32,6 +32,7 @@ import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -986,7 +987,72 @@ public class PacketContainer implements Serializable {
private ByteBuf createPacketBuffer() {
return MinecraftReflection.getPacketDataSerializer(UnpooledByteBufAllocator.DEFAULT.buffer());
}
// ---- Metadata
// This map will only be initialized if it is actually used
private Map<String, Object> metadata;
/**
* Gets the metadata value for a given key.
*
* @param key Metadata key
* @return Metadata value, or null if nonexistent.
*/
public Object getMetadata(String key) {
if (metadata != null) {
return metadata.get(key);
}
return null;
}
/**
* Adds metadata to this packet.
* <p>
* Note: Since metadata is lazily initialized, this may result in the creation of the metadata map.
*
* @param key Metadata key
* @param value Metadata value
*/
public void addMetadata(String key, Object value) {
if (metadata == null) {
metadata = new HashMap<String, Object>();
}
metadata.put(key, value);
}
/**
* Removes metadata from this packet.
* <p>
* Note: If this operation leaves the metadata map empty, the map will be set to null.
*
* @param key Metadata key
* @return The previous value, or null if nonexistant.
*/
public Object removeMetadata(String key) {
if (metadata != null) {
Object value = metadata.remove(key);
if (metadata.isEmpty()) {
metadata = null;
}
return value;
}
return null;
}
/**
* Whether or not this packet has metadata for a given key.
*
* @param key Metadata key
* @return True if this packet has metadata for the key, false if not.
*/
public boolean hasMetadata(String key) {
return metadata != null && metadata.containsKey(key);
}
/**
* Retrieve the cached method concurrently.
* @param lookup - a lazy lookup cache.