Search in sources :

Example 6 with JsonElement

use of com.viaversion.viaversion.libs.gson.JsonElement in project ViaBackwards by ViaVersion.

the class ChatItemRewriter method toClient.

public static void toClient(JsonElement element, UserConnection user) {
    if (element instanceof JsonObject) {
        JsonObject obj = (JsonObject) element;
        if (obj.has("hoverEvent")) {
            if (obj.get("hoverEvent") instanceof JsonObject) {
                JsonObject hoverEvent = (JsonObject) obj.get("hoverEvent");
                if (hoverEvent.has("action") && hoverEvent.has("value")) {
                    String type = hoverEvent.get("action").getAsString();
                    if (type.equals("show_item") || type.equals("show_entity")) {
                        JsonElement value = hoverEvent.get("value");
                        if (value.isJsonArray()) {
                            JsonArray newArray = new JsonArray();
                            int index = 0;
                            for (JsonElement valueElement : value.getAsJsonArray()) {
                                if (valueElement.isJsonPrimitive() && valueElement.getAsJsonPrimitive().isString()) {
                                    String newValue = index + ":" + valueElement.getAsString();
                                    newArray.add(new JsonPrimitive(newValue));
                                }
                            }
                            hoverEvent.add("value", newArray);
                        }
                    }
                }
            }
        } else if (obj.has("extra")) {
            toClient(obj.get("extra"), user);
        }
    } else if (element instanceof JsonArray) {
        JsonArray array = (JsonArray) element;
        for (JsonElement value : array) {
            toClient(value, user);
        }
    }
}
Also used : JsonArray(com.viaversion.viaversion.libs.gson.JsonArray) JsonPrimitive(com.viaversion.viaversion.libs.gson.JsonPrimitive) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject)

Example 7 with JsonElement

use of com.viaversion.viaversion.libs.gson.JsonElement in project ViaBackwards by ViaVersion.

the class Protocol1_11_1To1_12 method registerPackets.

@Override
protected void registerPackets() {
    blockItemPackets.register();
    entityPackets.register();
    new SoundPackets1_12(this).register();
    new ChatPackets1_12(this).register();
    registerClientbound(ClientboundPackets1_12.TITLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int action = wrapper.passthrough(Type.VAR_INT);
                if (action >= 0 && action <= 2) {
                    JsonElement component = wrapper.read(Type.COMPONENT);
                    wrapper.write(Type.COMPONENT, Protocol1_9To1_8.fixJson(component.toString()));
                }
            });
        }
    });
    cancelClientbound(ClientboundPackets1_12.ADVANCEMENTS);
    cancelClientbound(ClientboundPackets1_12.UNLOCK_RECIPES);
    cancelClientbound(ClientboundPackets1_12.SELECT_ADVANCEMENTS_TAB);
}
Also used : Entity1_12Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types) ServerboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3) ServerboundPackets1_12(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ServerboundPackets1_12) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) ClientboundPackets1_12(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ClientboundPackets1_12) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) EntityPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.EntityPackets1_12) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) ChatPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.ChatPackets1_12) BlockItemPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.BlockItemPackets1_12) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8) BackwardsMappings(com.viaversion.viabackwards.api.data.BackwardsMappings) SoundPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.SoundPackets1_12) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ClientboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3) ShoulderTracker(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ShoulderTracker) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) SoundPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.SoundPackets1_12) ChatPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.ChatPackets1_12)

Example 8 with JsonElement

use of com.viaversion.viaversion.libs.gson.JsonElement in project ViaBackwards by ViaVersion.

the class VBMappingDataLoader method mapIdentifiers.

public static void mapIdentifiers(int[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers, JsonObject diffIdentifiers, boolean warnOnMissing) {
    Object2IntMap<String> newIdentifierMap = MappingDataLoader.indexedObjectToMap(newIdentifiers);
    for (Map.Entry<String, JsonElement> entry : oldIdentifiers.entrySet()) {
        String key = entry.getValue().getAsString();
        int mappedId = newIdentifierMap.getInt(key);
        if (mappedId == -1) {
            if (diffIdentifiers != null) {
                // Search in diff mappings
                JsonPrimitive diffValueJson = diffIdentifiers.getAsJsonPrimitive(key);
                String diffValue = diffValueJson != null ? diffValueJson.getAsString() : null;
                int dataIndex;
                if (diffValue == null && (dataIndex = key.indexOf('[')) != -1 && (diffValueJson = diffIdentifiers.getAsJsonPrimitive(key.substring(0, dataIndex))) != null) {
                    // Check for wildcard mappings
                    diffValue = diffValueJson.getAsString();
                    // Keep original properties if value ends with [
                    if (diffValue.endsWith("[")) {
                        diffValue += key.substring(dataIndex + 1);
                    }
                }
                if (diffValue != null) {
                    mappedId = newIdentifierMap.getInt(diffValue);
                }
            }
            if (mappedId == -1) {
                // Nothing found :(
                if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                    ViaBackwards.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( ");
                }
                continue;
            }
        }
        output[Integer.parseInt(entry.getKey())] = mappedId;
    }
}
Also used : JsonPrimitive(com.viaversion.viaversion.libs.gson.JsonPrimitive) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) Object2IntMap(com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap) HashMap(java.util.HashMap) Int2ObjectMap(com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap) Map(java.util.Map) Int2ObjectOpenHashMap(com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap)

Example 9 with JsonElement

use of com.viaversion.viaversion.libs.gson.JsonElement in project ViaBackwards by ViaVersion.

the class VBMappingDataLoader method loadItemMappings.

public static Int2ObjectMap<MappedItem> loadItemMappings(JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping, boolean warnOnMissing) {
    Int2ObjectMap<MappedItem> itemMapping = new Int2ObjectOpenHashMap<>(diffMapping.size(), 0.99F);
    Object2IntMap<String> newIdenfierMap = MappingDataLoader.indexedObjectToMap(newMapping);
    Object2IntMap<String> oldIdenfierMap = MappingDataLoader.indexedObjectToMap(oldMapping);
    for (Map.Entry<String, JsonElement> entry : diffMapping.entrySet()) {
        JsonObject object = entry.getValue().getAsJsonObject();
        String mappedIdName = object.getAsJsonPrimitive("id").getAsString();
        int mappedId = newIdenfierMap.getInt(mappedIdName);
        if (mappedId == -1) {
            if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                ViaBackwards.getPlatform().getLogger().warning("No key for " + mappedIdName + " :( ");
            }
            continue;
        }
        int oldId = oldIdenfierMap.getInt(entry.getKey());
        if (oldId == -1) {
            if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                ViaBackwards.getPlatform().getLogger().warning("No old entry for " + mappedIdName + " :( ");
            }
            continue;
        }
        String name = object.getAsJsonPrimitive("name").getAsString();
        itemMapping.put(oldId, new MappedItem(mappedId, name));
    }
    // Look for missing keys
    if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings()) {
        for (Object2IntMap.Entry<String> entry : oldIdenfierMap.object2IntEntrySet()) {
            if (!newIdenfierMap.containsKey(entry.getKey()) && !itemMapping.containsKey(entry.getIntValue())) {
                ViaBackwards.getPlatform().getLogger().warning("No item mapping for " + entry.getKey() + " :( ");
            }
        }
    }
    return itemMapping;
}
Also used : Int2ObjectOpenHashMap(com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) Object2IntMap(com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject) Object2IntMap(com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap) HashMap(java.util.HashMap) Int2ObjectMap(com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap) Map(java.util.Map) Int2ObjectOpenHashMap(com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap)

Example 10 with JsonElement

use of com.viaversion.viaversion.libs.gson.JsonElement in project ViaBackwards by ViaVersion.

the class PlayerPackets1_11 method register.

public void register(Protocol1_10To1_11 protocol) {
    protocol.registerClientbound(ClientboundPackets1_9_3.TITLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Action
            map(Type.VAR_INT);
            handler(wrapper -> {
                int action = wrapper.get(Type.VAR_INT, 0);
                if (action == 2) {
                    // Handle the new ActionBar
                    JsonElement message = wrapper.read(Type.COMPONENT);
                    wrapper.clearPacket();
                    wrapper.setId(ClientboundPackets1_9_3.CHAT_MESSAGE.ordinal());
                    // https://bugs.mojang.com/browse/MC-119145to
                    String legacy = LegacyComponentSerializer.legacySection().serialize(GsonComponentSerializer.gson().deserialize(message.toString()));
                    message = new JsonObject();
                    message.getAsJsonObject().addProperty("text", legacy);
                    wrapper.write(Type.COMPONENT, message);
                    wrapper.write(Type.BYTE, (byte) 2);
                } else if (action > 2) {
                    // Move everything one position down
                    wrapper.set(Type.VAR_INT, 0, action - 1);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_9_3.COLLECT_ITEM, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Collected entity id
            map(Type.VAR_INT);
            // 1 - Collector entity id
            map(Type.VAR_INT);
            // Ignore item pickup count
            handler(wrapper -> wrapper.read(Type.VAR_INT));
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9_3.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Location
            map(Type.POSITION);
            // 1 - Face
            map(Type.VAR_INT);
            // 2 - Hand
            map(Type.VAR_INT);
            map(Type.UNSIGNED_BYTE, TO_NEW_FLOAT);
            map(Type.UNSIGNED_BYTE, TO_NEW_FLOAT);
            map(Type.UNSIGNED_BYTE, TO_NEW_FLOAT);
        }
    });
}
Also used : PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) LegacyComponentSerializer(com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer) Type(com.viaversion.viaversion.api.type.Type) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject) ServerboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) ClientboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3) GsonComponentSerializer(com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer) Protocol1_10To1_11(com.viaversion.viabackwards.protocol.protocol1_10to1_11.Protocol1_10To1_11) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject)

Aggregations

JsonElement (com.viaversion.viaversion.libs.gson.JsonElement)13 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)7 Type (com.viaversion.viaversion.api.type.Type)7 JsonObject (com.viaversion.viaversion.libs.gson.JsonObject)7 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)5 BackwardsProtocol (com.viaversion.viabackwards.api.BackwardsProtocol)4 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)4 Item (com.viaversion.viaversion.api.minecraft.item.Item)4 EntityTrackerBase (com.viaversion.viaversion.data.entity.EntityTrackerBase)4 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)3 BackwardsMappings (com.viaversion.viabackwards.api.data.BackwardsMappings)3 TranslatableRewriter (com.viaversion.viabackwards.api.rewriters.TranslatableRewriter)3 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)3 SoundRewriter (com.viaversion.viabackwards.api.rewriters.SoundRewriter)2 Via (com.viaversion.viaversion.api.Via)2 RegistryType (com.viaversion.viaversion.api.minecraft.RegistryType)2 ValueTransformer (com.viaversion.viaversion.api.protocol.remapper.ValueTransformer)2