Search in sources :

Example 16 with DataContainer

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;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Example 17 with DataContainer

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;
}
Also used : Path(java.nio.file.Path) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) DataContainer(org.spongepowered.api.data.DataContainer)

Example 18 with DataContainer

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();
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) DataQuery(org.spongepowered.api.data.DataQuery) Test(org.junit.Test)

Example 19 with DataContainer

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));
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test)

Example 20 with DataContainer

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"));
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) DataQuery(org.spongepowered.api.data.DataQuery) Test(org.junit.Test)

Aggregations

DataContainer (org.spongepowered.api.data.DataContainer)110 DataView (org.spongepowered.api.data.DataView)30 DataQuery (org.spongepowered.api.data.DataQuery)28 Test (org.junit.Test)24 List (java.util.List)15 Map (java.util.Map)15 ArrayList (java.util.ArrayList)13 Nullable (javax.annotation.Nullable)11 Collection (java.util.Collection)10 ImmutableList (com.google.common.collect.ImmutableList)9 ImmutableMap (com.google.common.collect.ImmutableMap)9 IOException (java.io.IOException)9 Path (java.nio.file.Path)9 Optional (java.util.Optional)9 CatalogType (org.spongepowered.api.CatalogType)9 Sponge (org.spongepowered.api.Sponge)8 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)7 DataSerializable (org.spongepowered.api.data.DataSerializable)7