use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.
the class MemoryDataView method set.
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public DataView set(DataQuery path, Object value) {
checkNotNull(path, "path");
checkNotNull(value, "value");
final LanternDataManager manager = getDataManager();
final List<String> parts = path.getParts();
final String key = parts.get(0);
if (parts.size() > 1) {
final DataQuery subQuery = of(key);
final Optional<DataView> subViewOptional = getUnsafeView(subQuery);
final DataView subView;
if (!subViewOptional.isPresent()) {
createView(subQuery);
subView = (DataView) this.map.get(key);
} else {
subView = subViewOptional.get();
}
subView.set(path.popFirst(), value);
return this;
}
Optional<DataTypeSerializer> optDataTypeSerializer;
TypeToken typeToken;
if (value instanceof DataView) {
checkArgument(value != this, "Cannot set a DataView to itself.");
// always have to copy a data view to avoid overwriting existing
// views and to set the interior path correctly.
copyDataView(path, (DataView) value);
} else if (value instanceof DataSerializable) {
final DataContainer valueContainer = ((DataSerializable) value).toContainer();
checkArgument(!(valueContainer).equals(this), "Cannot insert self-referencing DataSerializable");
// see above for why this is copied
copyDataView(path, valueContainer);
} else if (value instanceof CatalogType) {
return set(path, ((CatalogType) value).getId());
} else if (value instanceof Integer || value instanceof Byte || value instanceof Short || value instanceof Float || value instanceof Double || value instanceof Long || value instanceof String || value instanceof Character || value instanceof Boolean) {
this.map.put(key, value);
return this;
} else if (manager != null && (optDataTypeSerializer = manager.getTypeSerializer(typeToken = TypeToken.of(value.getClass()))).isPresent()) {
final DataTypeSerializer serializer = optDataTypeSerializer.get();
final Object serialized = serializer.serialize(typeToken, manager.getTypeSerializerContext(), value);
if (serialized instanceof DataContainer) {
final DataContainer container = (DataContainer) serialized;
checkArgument(!container.equals(this), "Cannot insert self-referencing Objects!");
// see above for why this is copied
copyDataView(path, container);
} else {
this.map.put(key, serialized);
}
} else if (value instanceof Collection) {
setCollection(key, (Collection) value);
} else if (value instanceof Map) {
setMap(key, (Map) value);
} else if (value.getClass().isArray()) {
if (this.safety == SafetyMode.ALL_DATA_CLONED || this.safety == SafetyMode.CLONED_ON_SET) {
if (value instanceof byte[]) {
this.map.put(key, ArrayUtils.clone((byte[]) value));
} else if (value instanceof short[]) {
this.map.put(key, ArrayUtils.clone((short[]) value));
} else if (value instanceof int[]) {
this.map.put(key, ArrayUtils.clone((int[]) value));
} else if (value instanceof long[]) {
this.map.put(key, ArrayUtils.clone((long[]) value));
} else if (value instanceof float[]) {
this.map.put(key, ArrayUtils.clone((float[]) value));
} else if (value instanceof double[]) {
this.map.put(key, ArrayUtils.clone((double[]) value));
} else if (value instanceof boolean[]) {
this.map.put(key, ArrayUtils.clone((boolean[]) value));
} else {
this.map.put(key, ArrayUtils.clone((Object[]) value));
}
} else {
this.map.put(key, value);
}
} else {
this.map.put(key, value);
}
return this;
}
use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.
the class UserIO method load.
public static void load(Path dataFolder, AbstractUser player) throws IOException {
final String fileName = player.getUniqueId().toString() + ".dat";
// Search for the player data and load it
Path dataFile = dataFolder.resolve(PLAYER_DATA_FOLDER).resolve(fileName);
if (Files.exists(dataFile)) {
final DataContainer dataContainer = NbtStreamUtils.read(Files.newInputStream(dataFile), true);
// Load sponge data if present and attach it to the main data
dataFile = dataFolder.resolve(SPONGE_PLAYER_DATA_FOLDER).resolve(fileName);
if (Files.exists(dataFile)) {
final DataContainer spongeDataContainer = NbtStreamUtils.read(Files.newInputStream(dataFile), true);
dataContainer.set(DataQueries.EXTENDED_SPONGE_DATA, spongeDataContainer);
}
final ObjectStore<AbstractUser> objectStore = ObjectStoreRegistry.get().get(AbstractUser.class).get();
objectStore.deserialize(player, dataContainer);
}
final Path statisticsFile = dataFolder.resolve(STATISTICS_FOLDER).resolve(player.getUniqueId().toString() + ".json");
if (Files.exists(statisticsFile)) {
player.getStatisticMap().load(statisticsFile);
}
}
use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.
the class AnvilChunkIOService method getGeneratedChunks.
@Override
public ChunkDataStream getGeneratedChunks() {
return new ChunkDataStream() {
// All the region files
private Path[] paths;
// The current region file that we opened
@Nullable
private RegionFile region;
// The coordinates of the chunk inside the region
private int chunkX;
private int chunkZ;
// The next index of the chunk in the region file
private int regionChunkIndex;
// The next index that we are in the file array
private int regionFileIndex;
// Whether the current fields are cached
private boolean cached;
// Done, no new chunks can be found
private boolean done;
{
// Use the reset to initialize
this.reset();
}
@Override
public DataContainer next() {
if (!this.hasNext()) {
throw new NoSuchElementException();
}
try {
final DataInputStream is = this.region.getChunkDataInputStream(this.chunkX, this.chunkZ);
final DataContainer data;
try (NbtDataContainerInputStream nbt = new NbtDataContainerInputStream(is)) {
data = nbt.read();
}
this.cached = false;
return data;
} catch (IOException e) {
// This shouldn't happen
throw new IllegalStateException(e);
}
}
@Override
public boolean hasNext() {
// Fast fail
if (this.done) {
return false;
}
// Use the cached index if set
if (this.cached) {
return true;
}
// Try first to search for more chunks in the current region
while (true) {
if (this.region != null) {
while (++this.regionChunkIndex < REGION_AREA) {
this.chunkX = this.regionChunkIndex / REGION_SIZE;
this.chunkZ = this.regionChunkIndex % REGION_SIZE;
if (this.region.hasChunk(this.chunkX, this.chunkZ)) {
this.cached = true;
return true;
}
}
}
// There no chunk available in the current region,
// reset the chunk index for the next one
this.regionChunkIndex = -1;
// try the next region
if (++this.regionFileIndex >= this.paths.length) {
this.region = null;
this.done = true;
return false;
}
final Path nextRegionFile = this.paths[this.regionFileIndex];
if (Files.exists(nextRegionFile)) {
Matcher matcher = cache.getFilePattern().matcher(nextRegionFile.getFileName().toString());
int regionX = Integer.parseInt(matcher.group(0));
int regionZ = Integer.parseInt(matcher.group(1));
try {
this.region = cache.getRegionFile(regionX, regionZ);
} catch (IOException e) {
logger.error("Failed to read the region file ({};{}) in the world folder {}", regionX, regionZ, baseDir.getFileName().toString(), e);
this.region = null;
}
} else {
this.region = null;
}
}
}
@Override
public int available() {
// the region files
throw new UnsupportedOperationException();
}
@Override
public void reset() {
this.paths = cache.getRegionFiles();
this.regionFileIndex = -1;
this.regionChunkIndex = -1;
this.region = null;
this.cached = false;
this.done = false;
}
};
}
use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.
the class AnvilChunkIOService method getChunkData.
@Override
public CompletableFuture<Optional<DataContainer>> getChunkData(Vector3i chunkCoords) {
return this.scheduler.submitAsyncTask(() -> {
final int x = chunkCoords.getX();
final int z = chunkCoords.getZ();
final RegionFile region = cache.getRegionFileByChunk(x, z);
final int regionX = x & REGION_MASK;
final int regionZ = z & REGION_MASK;
final DataInputStream is = region.getChunkDataInputStream(regionX, regionZ);
if (is == null) {
return Optional.empty();
}
final DataContainer data;
try (NbtDataContainerInputStream nbt = new NbtDataContainerInputStream(is)) {
data = nbt.read();
}
return Optional.of(data);
});
}
use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.
the class ItemStackStore method serialize.
@Override
public DataView serialize(LanternItemStack object) {
final DataContainer dataContainer = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
dataContainer.set(IDENTIFIER, object.getType().getId());
serialize(object, dataContainer);
return dataContainer;
}
Aggregations