use of org.spongepowered.api.data.DataContainer in project SpongeCommon by SpongePowered.
the class MixinItemStack method toContainer.
@Override
public DataContainer toContainer() {
final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, getContentVersion()).set(DataQueries.ITEM_TYPE, this.itemstack$getType().getId()).set(DataQueries.ITEM_COUNT, this.itemstack$getQuantity()).set(DataQueries.ITEM_DAMAGE_VALUE, this.getItemDamage());
if (hasTagCompound()) {
// no tag? no data, simple as that.
final NBTTagCompound compound = getTagCompound().copy();
if (compound.hasKey(NbtDataUtil.SPONGE_DATA)) {
final NBTTagCompound spongeCompound = compound.getCompoundTag(NbtDataUtil.SPONGE_DATA);
if (spongeCompound.hasKey(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST)) {
spongeCompound.removeTag(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST);
}
}
// We must filter the custom data so it isn't stored twice
NbtDataUtil.filterSpongeCustomData(compound);
if (!compound.hasNoTags()) {
final DataContainer unsafeNbt = NbtTranslator.getInstance().translateFrom(compound);
container.set(DataQueries.UNSAFE_NBT, unsafeNbt);
}
}
// We only need to include the custom data, not vanilla manipulators supported by sponge implementation
final Collection<DataManipulator<?, ?>> manipulators = getCustomManipulators();
if (!manipulators.isEmpty()) {
container.set(DataQueries.DATA_MANIPULATORS, DataUtil.getSerializedManipulatorList(manipulators));
}
return container;
}
use of org.spongepowered.api.data.DataContainer in project SpongeCommon by SpongePowered.
the class SpongePlayerDataHandler method init.
public static void init() {
final SpongePlayerDataHandler handlerInstance = Holder.INSTANCE;
if (!Sponge.isServerAvailable()) {
return;
}
handlerInstance.playerDataMap = new ConcurrentHashMap<>();
final Path filePath = WorldManager.getCurrentSavesDirectory().get().resolve("data").resolve(SPONGE_DATA);
try {
handlerInstance.playerDir = filePath;
Files.createDirectories(handlerInstance.playerDir);
final List<Path> playerFiles = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(filePath, "*.{dat}")) {
for (Path entry : stream) {
playerFiles.add(entry);
}
} catch (DirectoryIteratorException e) {
SpongeImpl.getLogger().error("Something happened when trying to gather all player files", e);
}
for (Path playerFile : playerFiles) {
if (Files.isReadable(playerFile)) {
NBTTagCompound compound;
try (final InputStream stream = Files.newInputStream(playerFile)) {
compound = CompressedStreamTools.readCompressed(stream);
}
// TODO Hard exception? Logger entry?
if (compound == null) {
throw new RuntimeException("Failed to decompress player data within [" + playerFile + "]!");
}
DataContainer container = NbtTranslator.getInstance().translateFrom(compound);
SpongePlayerData data = container.getSerializable(DataQuery.of(), SpongePlayerData.class).get();
handlerInstance.playerDataMap.put(data.uuid, data);
}
}
playerFiles.clear();
} catch (FileAlreadyExistsException e) {
SpongeImpl.getLogger().error("Someone went and created a file for the desired path: {}", filePath);
} catch (Exception e) {
e.printStackTrace();
}
handlerInstance.hasInitialized = true;
}
use of org.spongepowered.api.data.DataContainer in project SpongeCommon by SpongePowered.
the class MemoryDataTest method testToString.
@Test
public void testToString() {
DataContainer container = DataContainer.createNew();
DataQuery testQuery = of("foo", "bar", "baz");
List<Integer> intList = ImmutableList.of(1, 2, 3, 4);
container.set(testQuery, intList);
assertTrue(container.getIntegerList(testQuery).isPresent());
assertTrue(container.getIntegerList(testQuery).get().equals(intList));
List<Double> doubleList = ImmutableList.of(1.0D, 2.0D, 3.0D, 4.0D);
container.set(testQuery, doubleList);
assertTrue(container.getDoubleList(testQuery).isPresent());
assertTrue(container.getDoubleList(testQuery).get().equals(doubleList));
List<Short> shortList = ImmutableList.of((short) 1, (short) 2, (short) 3, (short) 4);
container.set(testQuery, shortList);
assertTrue(container.getShortList(testQuery).isPresent());
assertTrue(container.getShortList(testQuery).get().equals(shortList));
List<Byte> byteList = ImmutableList.of((byte) 1, (byte) 2, (byte) 3, (byte) 4);
container.set(testQuery, byteList);
List<SimpleData> list = Lists.newArrayList();
for (int i = 0; i < 1000; i++) {
String number = Integer.toString(i);
list.add(new SimpleData(i, 0.1 * i, "i", Lists.asList(number, new String[] { " foo", "bar" })));
}
container.set(of("SimpleData"), list);
container.toString();
}
use of org.spongepowered.api.data.DataContainer in project SpongeCommon by SpongePowered.
the class MemoryDataTest method testGetSerializableList.
@Test
public void testGetSerializableList() throws Exception {
initBuilder();
List<SimpleData> list = Lists.newArrayList();
for (int i = 0; i < 1000; i++) {
String number = Integer.toString(i);
list.add(new SimpleData(i, 0.1 * i, "i", Lists.asList(number, new String[] { " foo", "bar" })));
}
DataContainer container = DataContainer.createNew();
container.set(of("foo", "bar"), list);
assertTrue(container.contains(of("foo", "bar")));
Optional<List<SimpleData>> fromContainer = container.getSerializableList(of("foo", "bar"), SimpleData.class);
assertTrue(fromContainer.isPresent());
List<SimpleData> memoryList = fromContainer.get();
assertTrue(Objects.equal(memoryList, list));
}
use of org.spongepowered.api.data.DataContainer in project SpongeCommon by SpongePowered.
the class MemoryDataTest method testString.
@Test
public void testString() {
DataContainer container = DataContainer.createNew();
DataQuery testQuery = of("foo", "bar");
container.set(testQuery, "foo");
Optional<String> stringOptional = container.getString(testQuery);
assertTrue(stringOptional.isPresent());
assertTrue(stringOptional.get().equals("foo"));
}
Aggregations