Search in sources :

Example 31 with DataView

use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.

the class InventorySnapshotSerializer method deserialize.

public static InventorySnapshot deserialize(List<DataView> itemDataViews) {
    final ObjectSerializer<LanternItemStack> itemStackSerializer = ObjectSerializerRegistry.get().get(LanternItemStack.class).get();
    final Int2ObjectMap<ItemStackSnapshot> itemsByIndex = new Int2ObjectOpenHashMap<>();
    for (DataView itemDataView : itemDataViews) {
        final int slot = itemDataView.getByte(SLOT).get() & 0xff;
        final ItemStackSnapshot itemStackSnapshot = itemStackSerializer.deserialize(itemDataView).createSnapshot();
        itemsByIndex.put(slot, itemStackSnapshot);
    }
    return InventorySnapshot.ofRawMap(itemsByIndex);
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) DataView(org.spongepowered.api.data.DataView) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 32 with DataView

use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.

the class UserStore method serializeSlot.

private static void serializeSlot(int index, Slot slot, ObjectSerializer<LanternItemStack> itemStackSerializer, List<DataView> views) {
    final Optional<ItemStack> optItemStack = slot.peek();
    if (!optItemStack.isPresent()) {
        return;
    }
    final ItemStack itemStack = optItemStack.get();
    final DataView itemView = itemStackSerializer.serialize((LanternItemStack) itemStack);
    itemView.set(SLOT, (byte) index);
    views.add(itemView);
}
Also used : DataView(org.spongepowered.api.data.DataView) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 33 with DataView

use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.

the class UserStore method serializeEnderChest.

private static List<DataView> serializeEnderChest(GridInventory enderChestInventory) {
    final List<DataView> itemViews = new ArrayList<>();
    final Iterable<Slot> slots = enderChestInventory.slots();
    for (Slot slot : slots) {
        final Optional<ItemStack> optItemStack = slot.peek();
        if (!optItemStack.isPresent()) {
            continue;
        }
        final DataView itemView = ItemStackStore.INSTANCE.serialize((LanternItemStack) optItemStack.get());
        itemView.set(SLOT, (byte) enderChestInventory.getProperty(slot, SlotIndex.class, null).get().getValue().intValue());
        itemViews.add(itemView);
    }
    return itemViews;
}
Also used : DataView(org.spongepowered.api.data.DataView) ArrayList(java.util.ArrayList) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) Slot(org.spongepowered.api.item.inventory.Slot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 34 with DataView

use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.

the class UserStore method deserializeValues.

@Override
public void deserializeValues(T player, SimpleValueContainer valueContainer, DataView dataView) {
    // Try to convert old bukkit values first
    dataView.getLong(BUKKIT_FIRST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.FIRST_DATE_PLAYED, Instant.ofEpochMilli(v)));
    dataView.getLong(BUKKIT_LAST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.LAST_DATE_PLAYED, Instant.ofEpochMilli(v)));
    // Deserialize sponge data
    dataView.getView(DataQueries.EXTENDED_SPONGE_DATA).ifPresent(view -> {
        view.getLong(FIRST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.FIRST_DATE_PLAYED, Instant.ofEpochMilli(v)));
        view.getLong(LAST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.LAST_DATE_PLAYED, Instant.ofEpochMilli(v)));
    });
    dataView.getView(ABILITIES).ifPresent(view -> {
        view.getInt(FLYING).ifPresent(v -> valueContainer.set(Keys.IS_FLYING, v > 0));
        view.getDouble(FLYING_SPEED).ifPresent(v -> valueContainer.set(Keys.FLYING_SPEED, v));
        view.getInt(CAN_FLY).ifPresent(v -> valueContainer.set(Keys.CAN_FLY, v > 0));
    });
    final Map<UUID, RespawnLocation> respawnLocations = new HashMap<>();
    // Overworld respawn location is saved in the root container
    final Optional<Double> optSpawnX = dataView.getDouble(RESPAWN_LOCATIONS_X);
    final Optional<Double> optSpawnY = dataView.getDouble(RESPAWN_LOCATIONS_Y);
    final Optional<Double> optSpawnZ = dataView.getDouble(RESPAWN_LOCATIONS_Z);
    if (optSpawnX.isPresent() && optSpawnY.isPresent() && optSpawnZ.isPresent()) {
        UUID uniqueId = Lantern.getWorldManager().getWorldProperties(0).get().getUniqueId();
        respawnLocations.put(uniqueId, deserializeRespawnLocation(dataView, uniqueId, optSpawnX.get(), optSpawnY.get(), optSpawnZ.get()));
    }
    dataView.getViewList(RESPAWN_LOCATIONS).ifPresent(v -> v.forEach(view -> {
        int dimensionId = view.getInt(RESPAWN_LOCATIONS_DIMENSION).get();
        Lantern.getWorldManager().getWorldProperties(dimensionId).ifPresent(props -> {
            UUID uniqueId = props.getUniqueId();
            double x = view.getDouble(RESPAWN_LOCATIONS_X).get();
            double y = view.getDouble(RESPAWN_LOCATIONS_Y).get();
            double z = view.getDouble(RESPAWN_LOCATIONS_Z).get();
            respawnLocations.put(uniqueId, deserializeRespawnLocation(view, uniqueId, x, y, z));
        });
    }));
    valueContainer.set(Keys.RESPAWN_LOCATIONS, respawnLocations);
    dataView.getInt(SCORE).ifPresent(v -> valueContainer.set(LanternKeys.SCORE, v));
    final GameMode gameMode = dataView.getInt(GAME_MODE).flatMap(v -> GameModeRegistryModule.get().getByInternalId(v)).orElse(GameModes.NOT_SET);
    valueContainer.set(Keys.GAME_MODE, gameMode);
    player.getInventory().getHotbar().setSelectedSlotIndex(dataView.getInt(SELECTED_ITEM_SLOT).orElse(0));
    // Deserialize the player inventory
    dataView.getViewList(INVENTORY).ifPresent(views -> deserializePlayerInventory(player.getInventory(), views));
    // Deserialize the ender chest inventory
    dataView.getViewList(ENDER_CHEST_INVENTORY).ifPresent(views -> deserializeEnderChest(player.getEnderChestInventory(), views));
    dataView.getView(RECIPE_BOOK).ifPresent(view -> {
        view.getInt(RECIPE_BOOK_FILTER_ACTIVE).ifPresent(v -> valueContainer.set(LanternKeys.RECIPE_BOOK_FILTER_ACTIVE, v > 0));
        view.getInt(RECIPE_BOOK_GUI_OPEN).ifPresent(v -> valueContainer.set(LanternKeys.RECIPE_BOOK_GUI_OPEN, v > 0));
    });
    dataView.getString(OPEN_ADVANCEMENT_TREE).ifPresent(id -> valueContainer.set(LanternKeys.OPEN_ADVANCEMENT_TREE, AdvancementTreeRegistryModule.get().getById(id)));
    super.deserializeValues(player, valueContainer, dataView);
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) Vector3d(com.flowpowered.math.vector.Vector3d) HashMap(java.util.HashMap) DataHelper.getOrCreateView(org.lanternpowered.server.data.DataHelper.getOrCreateView) ItemStackStore(org.lanternpowered.server.data.io.store.item.ItemStackStore) DataQuery(org.spongepowered.api.data.DataQuery) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) DataQueries(org.lanternpowered.server.data.DataQueries) AbstractUser(org.lanternpowered.server.entity.living.player.AbstractUser) ArrayList(java.util.ArrayList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Queries(org.spongepowered.api.data.Queries) LanternPlayerEquipmentInventory(org.lanternpowered.server.inventory.vanilla.LanternPlayerEquipmentInventory) Map(java.util.Map) LanternWorld(org.lanternpowered.server.world.LanternWorld) GameModeRegistryModule(org.lanternpowered.server.game.registry.type.entity.player.GameModeRegistryModule) ObjectSerializer(org.lanternpowered.server.data.io.store.ObjectSerializer) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory) DataContainer(org.spongepowered.api.data.DataContainer) LanternMainPlayerInventory(org.lanternpowered.server.inventory.vanilla.LanternMainPlayerInventory) Slot(org.spongepowered.api.item.inventory.Slot) RespawnLocation(org.spongepowered.api.util.RespawnLocation) UUID(java.util.UUID) Instant(java.time.Instant) AbstractUserInventory(org.lanternpowered.server.inventory.vanilla.AbstractUserInventory) LanternWorldProperties(org.lanternpowered.server.world.LanternWorldProperties) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) List(java.util.List) SimpleValueContainer(org.lanternpowered.server.data.io.store.SimpleValueContainer) DataView(org.spongepowered.api.data.DataView) Lantern(org.lanternpowered.server.game.Lantern) GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) AdvancementTreeRegistryModule(org.lanternpowered.server.game.registry.type.advancement.AdvancementTreeRegistryModule) Optional(java.util.Optional) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) RespawnLocation(org.spongepowered.api.util.RespawnLocation) HashMap(java.util.HashMap) UUID(java.util.UUID)

Example 35 with DataView

use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.

the class FireworkChargeItemTypeObjectSerializer method serializeExplosion.

static DataView serializeExplosion(FireworkEffect effect) {
    final DataView dataView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    dataView.set(FLICKER, (byte) (effect.flickers() ? 1 : 0));
    dataView.set(TRAIL, (byte) (effect.hasTrail() ? 1 : 0));
    dataView.set(TYPE, (byte) ((LanternFireworkShape) effect.getShape()).getInternalId());
    dataView.set(COLORS, effect.getColors().stream().mapToInt(Color::getRgb).toArray());
    dataView.set(FADE_COLORS, effect.getFadeColors().stream().mapToInt(Color::getRgb).toArray());
    return dataView;
}
Also used : LanternFireworkShape(org.lanternpowered.server.item.firework.LanternFireworkShape) DataView(org.spongepowered.api.data.DataView) Color(org.spongepowered.api.util.Color)

Aggregations

DataView (org.spongepowered.api.data.DataView)100 DataContainer (org.spongepowered.api.data.DataContainer)30 DataQuery (org.spongepowered.api.data.DataQuery)24 ArrayList (java.util.ArrayList)21 Map (java.util.Map)17 List (java.util.List)13 Vector3i (com.flowpowered.math.vector.Vector3i)11 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)11 UUID (java.util.UUID)10 Nullable (javax.annotation.Nullable)10 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)10 ImmutableList (com.google.common.collect.ImmutableList)8 IOException (java.io.IOException)8 Test (org.junit.Test)8 CatalogType (org.spongepowered.api.CatalogType)8 Path (java.nio.file.Path)7 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)7 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)7 ImmutableMap (com.google.common.collect.ImmutableMap)6