From 5593dfb8e56aba9f85f3f78b586d854e1a1f618c Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Thu, 20 Jan 2022 08:34:10 -0300 Subject: [PATCH] Fix 1.14->1.13 difficulty (#427) --- .../Protocol1_13_2To1_14.java | 3 ++ .../packets/EntityPackets1_14.java | 42 +++++++++---------- .../packets/PlayerPackets1_14.java | 5 +++ .../storage/DifficultyStorage.java | 37 ++++++++++++++++ 4 files changed, 65 insertions(+), 22 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/storage/DifficultyStorage.java diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java index a4dc05e8..d29cf548 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java @@ -26,6 +26,7 @@ import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.packets.EntityP import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.packets.PlayerPackets1_14; import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.packets.SoundPackets1_14; import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage; +import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; @@ -195,6 +196,8 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol { + short difficulty = wrapper.user().get(DifficultyStorage.class).getDifficulty(); + wrapper.write(Type.UNSIGNED_BYTE, difficulty); - wrapper.passthrough(Type.UNSIGNED_BYTE); // Max Players - wrapper.passthrough(Type.STRING); // Level Type - wrapper.read(Type.VAR_INT); // Read View Distance + wrapper.passthrough(Type.UNSIGNED_BYTE); // Max Players + wrapper.passthrough(Type.STRING); // Level Type + wrapper.read(Type.VAR_INT); // Read View Distance - //TODO Track client position - // Manually add position storage - /*int entitiyId = wrapper.get(Type.INT, 0); - StoredEntityData storedEntity = protocol.getEntityRewriter().tracker(wrapper.user()).entityData(entitiyId); - storedEntity.put(new EntityPositionStorage1_14());*/ - } + //TODO Track client position + // Manually add position storage + /*int entitiyId = wrapper.get(Type.INT, 0); + StoredEntityData storedEntity = protocol.getEntityRewriter().tracker(wrapper.user()).entityData(entitiyId); + storedEntity.put(new EntityPositionStorage1_14());*/ }); } }); @@ -333,16 +332,15 @@ public class EntityPackets1_14 extends LegacyEntityRewriter { + ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); + int dimensionId = wrapper.get(Type.INT, 0); + clientWorld.setEnvironment(dimensionId); - wrapper.write(Type.UNSIGNED_BYTE, (short) 0); // Difficulty - wrapper.user().get(ChunkLightStorage.class).clear(); - } + short difficulty = wrapper.user().get(DifficultyStorage.class).getDifficulty(); + wrapper.write(Type.UNSIGNED_BYTE, difficulty); + + wrapper.user().get(ChunkLightStorage.class).clear(); }); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java index d5aca0bf..20a7d848 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java @@ -18,6 +18,7 @@ package com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.packets; import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14; +import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage; import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; @@ -40,6 +41,10 @@ public class PlayerPackets1_14 extends RewriterBase { public void registerMap() { map(Type.UNSIGNED_BYTE); map(Type.BOOLEAN, Type.NOTHING); // Locked + handler(wrapper -> { + byte difficulty = wrapper.get(Type.UNSIGNED_BYTE, 0).byteValue(); + wrapper.user().get(DifficultyStorage.class).setDifficulty(difficulty); + }); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/storage/DifficultyStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/storage/DifficultyStorage.java new file mode 100644 index 00000000..6fced60f --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/storage/DifficultyStorage.java @@ -0,0 +1,37 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2022 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_13_2to1_14.storage; + +import com.viaversion.viaversion.api.connection.StoredObject; +import com.viaversion.viaversion.api.connection.UserConnection; + +public class DifficultyStorage extends StoredObject { + private byte difficulty; + + public DifficultyStorage(UserConnection user) { + super(user); + } + + public byte getDifficulty() { + return difficulty; + } + + public void setDifficulty(byte difficulty) { + this.difficulty = difficulty; + } +}