Emulate recovery compass using lodestone compass in 1.19->1.18.2 (#770)

This commit is contained in:
EnZaXD 2024-06-03 09:49:39 +02:00 committed by GitHub
parent 3370383481
commit 8a675923d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 94 additions and 5 deletions

View File

@ -17,10 +17,14 @@
*/ */
package com.viaversion.viabackwards.protocol.v1_19to1_18_2.rewriter; package com.viaversion.viabackwards.protocol.v1_19to1_18_2.rewriter;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.viabackwards.api.rewriters.BackwardsItemRewriter; import com.viaversion.viabackwards.api.rewriters.BackwardsItemRewriter;
import com.viaversion.viabackwards.protocol.v1_19to1_18_2.Protocol1_19To1_18_2; import com.viaversion.viabackwards.protocol.v1_19to1_18_2.Protocol1_19To1_18_2;
import com.viaversion.viabackwards.protocol.v1_19to1_18_2.storage.LastDeathPosition;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.ParticleMappings; import com.viaversion.viaversion.api.data.ParticleMappings;
import com.viaversion.viaversion.api.data.entity.EntityTracker; import com.viaversion.viaversion.api.data.entity.EntityTracker;
import com.viaversion.viaversion.api.minecraft.GlobalBlockPosition;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection;
import com.viaversion.viaversion.api.minecraft.chunks.DataPalette; import com.viaversion.viaversion.api.minecraft.chunks.DataPalette;
@ -28,7 +32,6 @@ import com.viaversion.viaversion.api.minecraft.chunks.PaletteType;
import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_18; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_18;
import com.viaversion.viaversion.protocols.v1_16_4to1_17.packet.ServerboundPackets1_17; import com.viaversion.viaversion.protocols.v1_16_4to1_17.packet.ServerboundPackets1_17;
@ -204,4 +207,51 @@ public final class BlockItemPacketRewriter1_19 extends BackwardsItemRewriter<Cli
} }
}); });
} }
@Override
public Item handleItemToClient(final UserConnection connection, final Item item) {
if (item == null) return null;
final int identifier = item.identifier();
super.handleItemToClient(connection, item);
if (identifier != 834) {
return item;
}
final LastDeathPosition lastDeathPosition = connection.get(LastDeathPosition.class);
if (lastDeathPosition == null) {
return item;
}
final GlobalBlockPosition position = lastDeathPosition.position();
final CompoundTag lodestonePosTag = new CompoundTag();
item.tag().putBoolean(nbtTagName(), true);
item.tag().put("LodestonePos", lodestonePosTag);
item.tag().putString("LodestoneDimension", position.dimension());
lodestonePosTag.putInt("X", position.x());
lodestonePosTag.putInt("Y", position.y());
lodestonePosTag.putInt("Z", position.z());
return item;
}
@Override
public Item handleItemToServer(final UserConnection connection, final Item item) {
if (item == null) return null;
super.handleItemToServer(connection, item);
CompoundTag tag = item.tag();
if (item.identifier() == 834 && tag != null) {
if (tag.contains(nbtTagName())) {
tag.remove(nbtTagName());
tag.remove("LodestonePos");
tag.remove("LodestoneDimension");
}
if (tag.isEmpty()) {
item.setTag(null);
}
}
return item;
}
} }

View File

@ -20,9 +20,11 @@ package com.viaversion.viabackwards.protocol.v1_19to1_18_2.rewriter;
import com.viaversion.viabackwards.api.rewriters.EntityRewriter; import com.viaversion.viabackwards.api.rewriters.EntityRewriter;
import com.viaversion.viabackwards.protocol.v1_19to1_18_2.Protocol1_19To1_18_2; import com.viaversion.viabackwards.protocol.v1_19to1_18_2.Protocol1_19To1_18_2;
import com.viaversion.viabackwards.protocol.v1_19to1_18_2.storage.DimensionRegistryStorage; import com.viaversion.viabackwards.protocol.v1_19to1_18_2.storage.DimensionRegistryStorage;
import com.viaversion.viabackwards.protocol.v1_19to1_18_2.storage.LastDeathPosition;
import com.viaversion.viabackwards.protocol.v1_19to1_18_2.storage.StoredPainting; import com.viaversion.viabackwards.protocol.v1_19to1_18_2.storage.StoredPainting;
import com.viaversion.viaversion.api.data.ParticleMappings; import com.viaversion.viaversion.api.data.ParticleMappings;
import com.viaversion.viaversion.api.data.entity.StoredEntityData; import com.viaversion.viaversion.api.data.entity.StoredEntityData;
import com.viaversion.viaversion.api.minecraft.GlobalBlockPosition;
import com.viaversion.viaversion.api.minecraft.Particle; import com.viaversion.viaversion.api.minecraft.Particle;
import com.viaversion.viaversion.api.minecraft.BlockPosition; import com.viaversion.viaversion.api.minecraft.BlockPosition;
import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.entities.EntityType;
@ -30,7 +32,6 @@ import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_19;
import com.viaversion.viaversion.api.minecraft.entitydata.EntityDataType; import com.viaversion.viaversion.api.minecraft.entitydata.EntityDataType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.version.Types1_18; import com.viaversion.viaversion.api.type.types.version.Types1_18;
import com.viaversion.viaversion.api.type.types.version.Types1_19; import com.viaversion.viaversion.api.type.types.version.Types1_19;
@ -39,7 +40,6 @@ import com.viaversion.viaversion.protocols.v1_18_2to1_19.packet.ClientboundPacke
import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.ListTag; import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.NumberTag; import com.viaversion.nbt.tag.NumberTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.Key;
import com.viaversion.viaversion.util.TagUtil; import com.viaversion.viaversion.util.TagUtil;
@ -171,7 +171,14 @@ public final class EntityPacketRewriter1_19 extends EntityRewriter<ClientboundPa
map(Types.BOOLEAN); // Show death screen map(Types.BOOLEAN); // Show death screen
map(Types.BOOLEAN); // Debug map(Types.BOOLEAN); // Debug
map(Types.BOOLEAN); // Flat map(Types.BOOLEAN); // Flat
read(Types.OPTIONAL_GLOBAL_POSITION); // Read last death location handler(wrapper -> {
final GlobalBlockPosition lastDeathPosition = wrapper.read(Types.OPTIONAL_GLOBAL_POSITION);
if (lastDeathPosition != null) {
wrapper.user().put(new LastDeathPosition(lastDeathPosition));
} else {
wrapper.user().remove(LastDeathPosition.class);
}
});
handler(worldDataTrackerHandler(1)); handler(worldDataTrackerHandler(1));
handler(playerTrackerHandler()); handler(playerTrackerHandler());
} }
@ -196,7 +203,14 @@ public final class EntityPacketRewriter1_19 extends EntityRewriter<ClientboundPa
map(Types.BOOLEAN); // Debug map(Types.BOOLEAN); // Debug
map(Types.BOOLEAN); // Flat map(Types.BOOLEAN); // Flat
map(Types.BOOLEAN); // Keep player data map(Types.BOOLEAN); // Keep player data
read(Types.OPTIONAL_GLOBAL_POSITION); // Read last death location handler(wrapper -> {
final GlobalBlockPosition lastDeathPosition = wrapper.read(Types.OPTIONAL_GLOBAL_POSITION);
if (lastDeathPosition != null) {
wrapper.user().put(new LastDeathPosition(lastDeathPosition));
} else {
wrapper.user().remove(LastDeathPosition.class);
}
});
handler(worldDataTrackerHandler(0)); handler(worldDataTrackerHandler(0));
} }
}); });

View File

@ -0,0 +1,25 @@
/*
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
* Copyright (C) 2016-2024 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 <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viabackwards.protocol.v1_19to1_18_2.storage;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.minecraft.GlobalBlockPosition;
public record LastDeathPosition(GlobalBlockPosition position) implements StorableObject {
}