From ef1b0624499bb041761b8ee833a5aee7a3e43586 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 24 Mar 2023 11:36:32 +0100 Subject: [PATCH] Handle changed sign nbt --- build-logic/build.gradle.kts | 2 +- .../packets/BlockItemPackets1_20.java | 69 +++++++++++++++---- .../packets/EntityPackets1_20.java | 21 ++++++ .../storage/BackSignEditStorage.java | 34 +++++++++ settings.gradle.kts | 2 +- 5 files changed, 111 insertions(+), 17 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/storage/BackSignEditStorage.java diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index eda1ea5b..4733ddfa 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -8,5 +8,5 @@ repositories { dependencies { // version must be manually kept in sync with the one in root project settings.gradle.kts - implementation("com.github.johnrengelman", "shadow", "8.1.0") + implementation("com.github.johnrengelman", "shadow", "8.1.1") } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/packets/BlockItemPackets1_20.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/packets/BlockItemPackets1_20.java index d8ec0fee..78731a0c 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/packets/BlockItemPackets1_20.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/packets/BlockItemPackets1_20.java @@ -18,8 +18,13 @@ package com.viaversion.viabackwards.protocol.protocol1_19_4to1_20.packets; import com.viaversion.viabackwards.protocol.protocol1_19_4to1_20.Protocol1_19_4To1_20; +import com.viaversion.viabackwards.protocol.protocol1_19_4to1_20.storage.BackSignEditStorage; import com.viaversion.viaversion.api.minecraft.Position; +import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity; import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type; import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4; import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ServerboundPackets1_19_4; @@ -40,8 +45,8 @@ public final class BlockItemPackets1_20 extends ItemRewriter { - wrapper.passthrough(Type.POSITION1_14); + final Position position = wrapper.passthrough(Type.POSITION1_14); final boolean frontSide = wrapper.read(Type.BOOLEAN); - if (!frontSide) { - // TODO track front side to check in serverbound - wrapper.cancel(); + if (frontSide) { + wrapper.user().remove(BackSignEditStorage.class); + } else { + wrapper.user().put(new BackSignEditStorage(position)); } }); protocol.registerServerbound(ServerboundPackets1_19_4.UPDATE_SIGN, wrapper -> { final Position position = wrapper.passthrough(Type.POSITION1_14); - wrapper.write(Type.BOOLEAN, true); // Front side - - //TODO Track lines to send back with edited - /*final CompoundTag tag = new CompoundTag(); - final PacketWrapper signUpdate = wrapper.create(ClientboundPackets1_19_4.BLOCK_ENTITY_DATA); - signUpdate.send(Protocol1_19_4To1_20.class); - signUpdate.write(Type.POSITION1_14, position); - signUpdate.write(Type.VAR_INT, 5); // Sign - signUpdate.write(Type.NBT, tag);*/ + final BackSignEditStorage backSignEditStorage = wrapper.user().remove(BackSignEditStorage.class); + final boolean frontSide = backSignEditStorage == null || !backSignEditStorage.position().equals(position); + wrapper.write(Type.BOOLEAN, frontSide); }); new RecipeRewriter1_19_4<>(protocol).register(ClientboundPackets1_19_4.DECLARE_RECIPES); } + + private void handleBlockEntity(final BlockEntity blockEntity) { + // Check for signs + if (blockEntity.typeId() != 7 && blockEntity.typeId() != 8) { + return; + } + + final CompoundTag tag = blockEntity.tag(); + final CompoundTag frontText = tag.remove("front_text"); + tag.remove("back_text"); + + if (frontText != null) { + writeMessages(frontText, tag, false); + writeMessages(frontText, tag, true); + + final Tag color = frontText.remove("color"); + if (color != null) { + tag.put("Color", color); + } + + final Tag glowing = frontText.remove("has_glowing_text"); + if (glowing != null) { + tag.put("GlowingText", glowing); + } + } + } + + private void writeMessages(final CompoundTag frontText, final CompoundTag tag, final boolean filtered) { + final ListTag messages = frontText.get(filtered ? "filtered_messages" : "messages"); + if (messages == null) { + return; + } + + int i = 0; + for (final Tag message : messages) { + tag.put((filtered ? "FilteredText" : "Text") + ++i, message); + } + + } } \ No newline at end of file diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/packets/EntityPackets1_20.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/packets/EntityPackets1_20.java index ba22c1ab..e0b2a633 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/packets/EntityPackets1_20.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/packets/EntityPackets1_20.java @@ -17,6 +17,7 @@ */ package com.viaversion.viabackwards.protocol.protocol1_19_4to1_20.packets; +import com.google.common.collect.Sets; import com.viaversion.viabackwards.api.rewriters.EntityRewriter; import com.viaversion.viabackwards.protocol.protocol1_19_4to1_20.Protocol1_19_4To1_20; import com.viaversion.viaversion.api.minecraft.entities.Entity1_19_4Types; @@ -25,10 +26,19 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_19_4; import com.viaversion.viaversion.api.type.types.version.Types1_20; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4; +import com.viaversion.viaversion.util.Key; +import java.util.Set; public final class EntityPackets1_20 extends EntityRewriter { + private final Set newTrimPatterns = Sets.newHashSet("host_armor_trim_smithing_template", "raiser_armor_trim_smithing_template", + "silence_armor_trim_smithing_template", "shaper_armor_trim_smithing_template", "wayfinder_armor_trim_smithing_template"); + public EntityPackets1_20(final Protocol1_19_4To1_20 protocol) { super(protocol); } @@ -53,6 +63,17 @@ public final class EntityPackets1_20 extends EntityRewriter { + final CompoundTag registry = wrapper.get(Type.NBT, 0); + final ListTag values = ((CompoundTag) registry.get("minecraft:trim_pattern")).get("value"); + for (final Tag entry : values) { + final CompoundTag element = ((CompoundTag) entry).get("element"); + final StringTag templateItem = element.get("template_item"); + if (newTrimPatterns.contains(Key.stripMinecraftNamespace(templateItem.getValue()))) { + templateItem.setValue("minecraft:spire_armor_trim_smithing_template"); + } + } + }); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/storage/BackSignEditStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/storage/BackSignEditStorage.java new file mode 100644 index 00000000..2ab0f32a --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/storage/BackSignEditStorage.java @@ -0,0 +1,34 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2023 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viabackwards.protocol.protocol1_19_4to1_20.storage; + +import com.viaversion.viaversion.api.connection.StorableObject; +import com.viaversion.viaversion.api.minecraft.Position; + +public final class BackSignEditStorage implements StorableObject { + + private final Position position; + + public BackSignEditStorage(final Position position) { + this.position = position; + } + + public Position position() { + return position; + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 9c0697ee..69e747f3 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -16,7 +16,7 @@ dependencyResolutionManagement { pluginManagement { plugins { id("net.kyori.blossom") version "1.2.0" - id("com.github.johnrengelman.shadow") version "8.1.0" + id("com.github.johnrengelman.shadow") version "8.1.1" } }