|
| 1 | +/* |
| 2 | + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion |
| 3 | + * Copyright (C) 2016-2024 ViaVersion and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package com.viaversion.viaversion.protocols.protocol1_12to1_11_1.rewriter; |
| 19 | + |
| 20 | +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; |
| 21 | +import com.google.gson.JsonArray; |
| 22 | +import com.google.gson.JsonElement; |
| 23 | +import com.google.gson.JsonObject; |
| 24 | +import net.lenni0451.mcstructs.snbt.SNbtSerializer; |
| 25 | +import net.lenni0451.mcstructs.snbt.exceptions.SNbtDeserializeException; |
| 26 | +import net.lenni0451.mcstructs.snbt.exceptions.SNbtSerializeException; |
| 27 | +import net.lenni0451.mcstructs.text.ATextComponent; |
| 28 | +import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer; |
| 29 | + |
| 30 | +public class ChatItemRewriter { |
| 31 | + |
| 32 | + public static void toClient(JsonElement element) throws SNbtDeserializeException, SNbtSerializeException { |
| 33 | + if (element instanceof JsonObject) { |
| 34 | + JsonObject obj = (JsonObject) element; |
| 35 | + if (obj.has("hoverEvent")) { |
| 36 | + if (obj.get("hoverEvent") instanceof JsonObject) { |
| 37 | + final JsonObject hoverEvent = (JsonObject) obj.get("hoverEvent"); |
| 38 | + if (hoverEvent.has("action") && hoverEvent.has("value")) { |
| 39 | + final String type = hoverEvent.get("action").getAsString(); |
| 40 | + final JsonElement value = hoverEvent.get("value"); |
| 41 | + |
| 42 | + if (type.equals("show_item")) { |
| 43 | + final ATextComponent component = TextComponentSerializer.V1_8.deserialize(value); |
| 44 | + |
| 45 | + final CompoundTag compound = SNbtSerializer.V1_8.deserialize(component.asUnformattedString()); |
| 46 | + hoverEvent.addProperty("value", SNbtSerializer.V1_12.serialize(compound)); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } else if (obj.has("extra")) { |
| 51 | + toClient(obj.get("extra")); |
| 52 | + } |
| 53 | + } else if (element instanceof JsonArray) { |
| 54 | + JsonArray array = (JsonArray) element; |
| 55 | + for (JsonElement value : array) { |
| 56 | + toClient(value); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments