Assigning a data key to null will remove the key

This commit is contained in:
themode 2020-10-26 01:47:41 +01:00
parent 91301102d3
commit 322acca51e
4 changed files with 27 additions and 12 deletions

View File

@ -15,7 +15,7 @@ public interface Data {
Data EMPTY = new Data() {
@Override
public <T> void set(@NotNull String key, @NotNull T value, @NotNull Class<T> type) {
public <T> void set(@NotNull String key, @Nullable T value, @Nullable Class<T> type) {
}
@Override
@ -55,11 +55,11 @@ public interface Data {
* Assigns a value to a specific key.
*
* @param key the key
* @param value the value object
* @param type the value type
* @param value the value object, null to remove the key
* @param type the value type, can be null if not in a {@link SerializableData}
* @param <T> the value generic
*/
<T> void set(@NotNull String key, @NotNull T value, @NotNull Class<T> type);
<T> void set(@NotNull String key, @Nullable T value, @Nullable Class<T> type);
/**
* Retrieves a value based on its key.

View File

@ -1,6 +1,7 @@
package net.minestom.server.data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.Set;
@ -14,8 +15,12 @@ public class DataImpl implements Data {
protected final ConcurrentHashMap<String, Object> data = new ConcurrentHashMap<>();
@Override
public <T> void set(@NotNull String key, @NotNull T value, @NotNull Class<T> type) {
this.data.put(key, value);
public <T> void set(@NotNull String key, @Nullable T value, @Nullable Class<T> type) {
if (value != null) {
this.data.put(key, value);
} else {
this.data.remove(key);
}
}
@Override

View File

@ -6,6 +6,7 @@ import net.minestom.server.MinecraftServer;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Represents a {@link Data} object which can be serialized and read back.
@ -16,6 +17,9 @@ public interface SerializableData extends Data {
DataManager DATA_MANAGER = MinecraftServer.getDataManager();
@Override
<T> void set(@NotNull String key, @Nullable T value, @NotNull Class<T> type);
/**
* Serializes the data into an array of bytes.
* <p>

View File

@ -8,6 +8,7 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -41,13 +42,18 @@ public class SerializableDataImpl extends DataImpl implements SerializableData {
* @throws UnsupportedOperationException if {@code type} is not registered in {@link DataManager}
*/
@Override
public <T> void set(@NotNull String key, @NotNull T value, @NotNull Class<T> type) {
if (DATA_MANAGER.getDataType(type) == null) {
throw new UnsupportedOperationException("Type " + type.getName() + " hasn't been registered in DataManager#registerType");
}
public <T> void set(@NotNull String key, @Nullable T value, @NotNull Class<T> type) {
if (value != null) {
if (DATA_MANAGER.getDataType(type) == null) {
throw new UnsupportedOperationException("Type " + type.getName() + " hasn't been registered in DataManager#registerType");
}
super.set(key, value, type);
this.dataType.put(key, type);
this.data.put(key, value);
this.dataType.put(key, type);
} else {
this.data.remove(key);
this.dataType.remove(key);
}
}
@NotNull