Search in sources :

Example 46 with DataView

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

the class ContainerTileEntityStore method deserialize.

@Override
public void deserialize(T object, DataView dataView) {
    final List<DataView> itemViews = dataView.getViewList(ITEMS).orElse(null);
    if (itemViews != null) {
        dataView.remove(ITEMS);
        final OrderedInventory inventory = (OrderedInventory) object.getInventory();
        final ObjectSerializer<LanternItemStack> itemStackSerializer = ObjectSerializerRegistry.get().get(LanternItemStack.class).get();
        for (DataView itemView : itemViews) {
            final int slot = itemView.getByte(SLOT).get() & 0xff;
            final LanternItemStack itemStack = itemStackSerializer.deserialize(itemView);
            inventory.set(new SlotIndex(slot), itemStack);
        }
    }
    super.deserialize(object, dataView);
}
Also used : DataView(org.spongepowered.api.data.DataView) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) OrderedInventory(org.spongepowered.api.item.inventory.type.OrderedInventory) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 47 with DataView

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

the class TileEntitySerializer method serialize.

@Override
public DataView serialize(LanternTileEntity object) {
    final DataView dataView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    dataView.set(ID, object.getType().getId());
    // noinspection unchecked
    final ObjectStore<LanternTileEntity> store = (ObjectStore) ObjectStoreRegistry.get().get(object.getClass()).get();
    store.serialize(object, dataView);
    return dataView;
}
Also used : DataView(org.spongepowered.api.data.DataView) ObjectStore(org.lanternpowered.server.data.io.store.ObjectStore) LanternTileEntity(org.lanternpowered.server.block.tile.LanternTileEntity)

Example 48 with DataView

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

the class CodecPlayInOutCustomPayload method decode0.

@Override
protected Message decode0(CodecContext context, String channel, ByteBuffer content) throws CodecException {
    if ("MC|ItemName".equals(channel)) {
        return new MessagePlayInChangeItemName(content.readString());
    } else if ("MC|TrSel".equals(channel)) {
        return new MessagePlayInChangeOffer(content.readInteger());
    } else if ("MC|Brand".equals(channel)) {
        return new MessagePlayInOutBrand(content.readString());
    } else if ("MC|Beacon".equals(channel)) {
        final PotionEffectType primary = PotionEffectTypeRegistryModule.get().getByInternalId(content.readInteger()).orElse(null);
        final PotionEffectType secondary = PotionEffectTypeRegistryModule.get().getByInternalId(content.readInteger()).orElse(null);
        return new MessagePlayInAcceptBeaconEffects(primary, secondary);
    } else if ("MC|AdvCdm".equals(channel)) {
        final byte type = content.readByte();
        Vector3i pos = null;
        int entityId = 0;
        if (type == 0) {
            int x = content.readInteger();
            int y = content.readInteger();
            int z = content.readInteger();
            pos = new Vector3i(x, y, z);
        } else if (type == 1) {
            entityId = content.readInteger();
        } else {
            throw new CodecException("Unknown modify command message type: " + type);
        }
        final String command = content.readString();
        final boolean shouldTrackOutput = content.readBoolean();
        if (pos != null) {
            return new MessagePlayInEditCommandBlock.Block(pos, command, shouldTrackOutput);
        } else {
            return new MessagePlayInEditCommandBlock.Entity(entityId, command, shouldTrackOutput);
        }
    } else if ("MC|AutoCmd".equals(channel)) {
        final int x = content.readInteger();
        final int y = content.readInteger();
        final int z = content.readInteger();
        final String command = content.readString();
        final boolean shouldTrackOutput = content.readBoolean();
        final MessagePlayInEditCommandBlock.AdvancedBlock.Mode mode = MessagePlayInEditCommandBlock.AdvancedBlock.Mode.valueOf(content.readString());
        final boolean conditional = content.readBoolean();
        final boolean automatic = content.readBoolean();
        return new MessagePlayInEditCommandBlock.AdvancedBlock(new Vector3i(x, y, z), command, shouldTrackOutput, mode, conditional, automatic);
    } else if ("MC|BSign".equals(channel)) {
        final RawItemStack rawItemStack = content.read(Types.RAW_ITEM_STACK);
        // noinspection ConstantConditions
        if (rawItemStack == null) {
            throw new CodecException("Signed book may not be null!");
        }
        final DataView dataView = rawItemStack.getDataView();
        if (dataView == null) {
            throw new CodecException("Signed book data view (nbt tag) may not be null!");
        }
        final String author = dataView.getString(AUTHOR).orElseThrow(() -> new CodecException("Signed book author missing!"));
        final String title = dataView.getString(TITLE).orElseThrow(() -> new CodecException("Signed book title missing!"));
        final List<String> pages = dataView.getStringList(PAGES).orElseThrow(() -> new CodecException("Signed book pages missing!"));
        return new MessagePlayInSignBook(author, title, pages);
    } else if ("MC|BEdit".equals(channel)) {
        final RawItemStack rawItemStack = content.read(Types.RAW_ITEM_STACK);
        // noinspection ConstantConditions
        if (rawItemStack == null) {
            throw new CodecException("Edited book may not be null!");
        }
        final DataView dataView = rawItemStack.getDataView();
        if (dataView == null) {
            throw new CodecException("Edited book data view (nbt tag) may not be null!");
        }
        final List<String> pages = dataView.getStringList(PAGES).orElseThrow(() -> new CodecException("Edited book pages missing!"));
        return new MessagePlayInEditBook(pages);
    } else if ("MC|Struct".equals(channel)) {
    // Something related to structure placing in minecraft 1.9,
    // seems like it's something mojang doesn't want to share with use,
    // they used this channel to build and save structures
    } else if ("MC|PickItem".equals(channel)) {
        return new MessagePlayInPickItem(content.readVarInt());
    } else if ("FML|HS".equals(channel)) {
        throw new CodecException("Received and unexpected message with channel: " + channel);
    } else if ("FML".equals(channel)) {
    // Fml channel
    } else if (channel.startsWith("FML")) {
    // A unknown/ignored fml channel
    } else {
        return new MessagePlayInOutChannelPayload(channel, content);
    }
    return NullMessage.INSTANCE;
}
Also used : MessagePlayInChangeOffer(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInChangeOffer) MessagePlayInEditBook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInEditBook) MessagePlayInChangeItemName(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInChangeItemName) MessagePlayInAcceptBeaconEffects(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInAcceptBeaconEffects) MessagePlayInSignBook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInSignBook) PotionEffectType(org.spongepowered.api.effect.potion.PotionEffectType) DataView(org.spongepowered.api.data.DataView) MessagePlayInPickItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPickItem) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) RawItemStack(org.lanternpowered.server.network.objects.RawItemStack) Vector3i(com.flowpowered.math.vector.Vector3i) MessagePlayInEditCommandBlock(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInEditCommandBlock) CodecException(io.netty.handler.codec.CodecException) List(java.util.List) MessagePlayInOutBrand(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutBrand) MessagePlayInEditCommandBlock(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInEditCommandBlock)

Example 49 with DataView

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

the class FlatGeneratorSettingsParser method fromString.

@Nullable
public static FlatGeneratorSettings fromString(@Nullable String value) {
    if (value == null) {
        return null;
    }
    // Split the value into parts
    final List<String> parts = Lists.newArrayList(Splitter.on(';').split(value));
    // Try to extract the version from the parts
    int version = 0;
    if (parts.size() > 1) {
        version = Coerce.toInteger(parts.remove(0));
    }
    // Smaller then 0 is unknown? and 3 is the latest format version
    if (version < 0 || version > 3) {
        return null;
    }
    // The layers are stored in the first part
    final String layersPart = parts.remove(0);
    // The parsed layers
    final List<FlatLayer> layers = new ArrayList<>();
    // Can be empty if there are no layers
    if (!layersPart.isEmpty()) {
        // The separator that can be used to create a layer
        // of x amount of blocks
        final char depthSeparator = version >= 3 ? '*' : 'x';
        Splitter.on(',').split(layersPart).forEach(s -> {
            // The block type
            BlockType blockType;
            // The data value (optional)
            int blockData = 0;
            // The depth of the layer
            int depth = 1;
            // The depth separated by the depth separator followed by the block state
            final List<String> parts1 = Lists.newArrayList(Splitter.on(depthSeparator).limit(2).split(s));
            if (parts1.size() > 1) {
                final Optional<Integer> optDepth = Coerce.asInteger(parts1.remove(0));
                if (optDepth.isPresent()) {
                    depth = GenericMath.clamp(optDepth.get(), 0, 255);
                    if (depth <= 0) {
                        // Skip to the next layer
                        return;
                    }
                }
            }
            String blockStatePart = parts1.get(0);
            final int index = blockStatePart.lastIndexOf(':');
            if (index > 0) {
                Optional<Integer> optData = Coerce.asInteger(blockStatePart.substring(index + 1));
                if (optData.isPresent()) {
                    blockData = GenericMath.clamp(optData.get(), 0, 15);
                    blockStatePart = blockStatePart.substring(0, index);
                }
            }
            // Try to parse the block id as internal (int) id
            final Optional<Integer> optId = Coerce.asInteger(blockStatePart);
            if (optId.isPresent()) {
                blockType = BlockRegistryModule.get().getStateByInternalId(optId.get()).orElse(BlockTypes.STONE.getDefaultState()).getType();
            // Not an integer, try the catalog system
            } else {
                blockType = BlockRegistryModule.get().getById(blockStatePart).orElse(BlockTypes.STONE);
            }
            layers.add(new FlatLayer(BlockRegistryModule.get().getStateByTypeAndData(blockType, (byte) blockData).get(), depth));
        });
    }
    // Try to parse the biome type if present
    BiomeType biomeType = BiomeTypes.PLAINS;
    if (!parts.isEmpty()) {
        final String biomePart = parts.remove(0);
        final Optional<Integer> optBiomeId = Coerce.asInteger(biomePart);
        final Optional<BiomeType> optBiome;
        if (optBiomeId.isPresent()) {
            optBiome = BiomeRegistryModule.get().getByInternalId(optBiomeId.get());
        } else {
            optBiome = BiomeRegistryModule.get().getById(biomePart);
        }
        if (optBiome.isPresent()) {
            biomeType = optBiome.get();
        }
    }
    // Extra data (like structures)
    final DataContainer extraData = DataContainer.createNew();
    if (!parts.isEmpty()) {
        final String extraPart = parts.remove(0);
        if (!extraPart.isEmpty()) {
            Splitter.on(',').split(extraPart).forEach(s -> {
                String key = s;
                // Check if there is extra data attached to the key
                final int valuesIndex = s.indexOf('(');
                if (valuesIndex != -1) {
                    // Separate the key from the values
                    key = s.substring(0, valuesIndex);
                    int endIndex = s.lastIndexOf(')');
                    if (endIndex == -1) {
                        endIndex = s.length();
                    }
                    // Get the values section from the string
                    s = s.substring(valuesIndex + 1, endIndex);
                    // Create the view to store the values
                    final DataView dataView = extraData.createView(DataQuery.of(key));
                    if (!s.isEmpty()) {
                        Splitter.on(' ').split(s).forEach(v -> {
                            final List<String> parts1 = Splitter.on('=').limit(2).splitToList(v);
                            // Must be greater then 1, otherwise it's invalid
                            if (parts1.size() > 1) {
                                // Currently, only integer values seem to be supported
                                dataView.set(DataQuery.of(parts1.get(0)), Coerce.toInteger(parts1.get(1)));
                            }
                        });
                    }
                } else {
                    extraData.createView(DataQuery.of(key));
                }
            });
        }
    }
    return new FlatGeneratorSettings(biomeType, layers, extraData);
}
Also used : ArrayList(java.util.ArrayList) BiomeType(org.spongepowered.api.world.biome.BiomeType) DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) BlockType(org.spongepowered.api.block.BlockType) Nullable(javax.annotation.Nullable)

Example 50 with DataView

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

the class LanternPlayer method sendBookView.

@Override
public void sendBookView(BookView bookView) {
    checkNotNull(bookView, "bookView");
    final DataView dataView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    WrittenBookItemTypeObjectSerializer.writeBookData(dataView, bookView, this.locale);
    // Written book internal id
    final RawItemStack rawItemStack = new RawItemStack(387, 0, 1, dataView);
    final int slot = this.inventory.getHotbar().getSelectedSlotIndex();
    this.session.send(new MessagePlayOutSetWindowSlot(-2, slot, rawItemStack));
    this.session.send(new MessagePlayOutOpenBook(HandTypes.MAIN_HAND));
    this.session.send(new MessagePlayOutSetWindowSlot(-2, slot, this.inventory.getHotbar().getSelectedSlot().peek().orElse(null)));
}
Also used : DataView(org.spongepowered.api.data.DataView) MessagePlayOutSetWindowSlot(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSetWindowSlot) RawItemStack(org.lanternpowered.server.network.objects.RawItemStack) MessagePlayOutOpenBook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutOpenBook)

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