ViaVersion/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java

361 lines
15 KiB
Java
Raw Normal View History

/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
2024-01-01 12:39:45 +01:00
* 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.viaversion.protocols.protocol1_11to1_10;
2016-08-10 14:51:32 +02:00
2017-05-14 16:02:04 +02:00
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.viaversion.viaversion.api.Via;
2021-04-26 21:35:29 +02:00
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
2023-10-19 01:28:12 +02:00
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_11;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
2021-04-26 21:16:10 +02:00
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.version.Types1_9;
import com.viaversion.viaversion.protocols.protocol1_11to1_10.data.PotionColorMapping;
import com.viaversion.viaversion.protocols.protocol1_11to1_10.metadata.MetadataRewriter1_11To1_10;
import com.viaversion.viaversion.protocols.protocol1_11to1_10.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;
2023-10-19 13:03:00 +02:00
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_9_3;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.util.Pair;
2016-08-10 14:51:32 +02:00
public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3, ClientboundPackets1_9_3, ServerboundPackets1_9_3, ServerboundPackets1_9_3> {
2016-09-30 17:14:14 +02:00
private static final ValueTransformer<Float, Short> toOldByte = new ValueTransformer<Float, Short>(Type.UNSIGNED_BYTE) {
@Override
public Short transform(PacketWrapper wrapper, Float inputValue) throws Exception {
return (short) (inputValue * 16);
}
};
private final MetadataRewriter1_11To1_10 entityRewriter = new MetadataRewriter1_11To1_10(this);
private final InventoryPackets itemRewriter = new InventoryPackets(this);
public Protocol1_11To1_10() {
super(ClientboundPackets1_9_3.class, ClientboundPackets1_9_3.class, ServerboundPackets1_9_3.class, ServerboundPackets1_9_3.class);
}
2016-08-10 14:51:32 +02:00
@Override
protected void registerPackets() {
super.registerPackets();
2016-08-10 20:12:27 +02:00
registerClientbound(ClientboundPackets1_9_3.SPAWN_ENTITY, new PacketHandlers() {
@Override
public void register() {
map(Type.VAR_INT); // 0 - Entity id
map(Type.UUID); // 1 - UUID
map(Type.BYTE); // 2 - Type
// Track Entity
handler(entityRewriter.objectTrackerHandler());
}
});
registerClientbound(ClientboundPackets1_9_3.SPAWN_MOB, new PacketHandlers() {
2016-08-10 15:59:13 +02:00
@Override
public void register() {
2016-08-10 15:59:13 +02:00
map(Type.VAR_INT); // 0 - Entity ID
map(Type.UUID); // 1 - Entity UUID
map(Type.UNSIGNED_BYTE, Type.VAR_INT); // 2 - Entity Type
map(Type.DOUBLE); // 3 - X
map(Type.DOUBLE); // 4 - Y
map(Type.DOUBLE); // 5 - Z
map(Type.BYTE); // 6 - Yaw
map(Type.BYTE); // 7 - Pitch
map(Type.BYTE); // 8 - Head Pitch
map(Type.SHORT); // 9 - Velocity X
map(Type.SHORT); // 10 - Velocity Y
map(Type.SHORT); // 11 - Velocity Z
map(Types1_9.METADATA_LIST); // 12 - Metadata
2016-08-10 14:51:32 +02:00
handler(wrapper -> {
int entityId = wrapper.get(Type.VAR_INT, 0);
// Change Type :)
int type = wrapper.get(Type.VAR_INT, 1);
2023-10-19 01:28:12 +02:00
EntityTypes1_11.EntityType entType = MetadataRewriter1_11To1_10.rewriteEntityType(type, wrapper.get(Types1_9.METADATA_LIST, 0));
if (entType != null) {
wrapper.set(Type.VAR_INT, 1, entType.getId());
// Register Type ID
wrapper.user().getEntityTracker(Protocol1_11To1_10.class).addEntity(entityId, entType);
entityRewriter.handleMetadata(entityId, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user());
}
});
2016-08-10 15:59:13 +02:00
}
});
new SoundRewriter<>(this, this::getNewSoundId).registerSound(ClientboundPackets1_9_3.SOUND);
registerClientbound(ClientboundPackets1_9_3.COLLECT_ITEM, new PacketHandlers() {
2016-08-10 15:59:13 +02:00
@Override
public void register() {
2016-08-10 15:59:13 +02:00
map(Type.VAR_INT); // 0 - Collected entity id
map(Type.VAR_INT); // 1 - Collector entity id
handler(wrapper -> {
wrapper.write(Type.VAR_INT, 1); // 2 - Pickup Count
2016-08-10 15:59:13 +02:00
});
}
});
entityRewriter.registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_9.METADATA_LIST);
registerClientbound(ClientboundPackets1_9_3.ENTITY_TELEPORT, new PacketHandlers() {
@Override
public void register() {
map(Type.VAR_INT); // 0 - Entity id
map(Type.DOUBLE); // 1 - x
map(Type.DOUBLE); // 2 - y
map(Type.DOUBLE); // 3 - z
map(Type.BYTE); // 4 - yaw
map(Type.BYTE); // 5 - pitch
map(Type.BOOLEAN); // 6 - onGround
handler(wrapper -> {
int entityID = wrapper.get(Type.VAR_INT, 0);
if (Via.getConfig().isHologramPatch()) {
EntityTracker1_11 tracker = wrapper.user().getEntityTracker(Protocol1_11To1_10.class);
if (tracker.isHologram(entityID)) {
Double newValue = wrapper.get(Type.DOUBLE, 1);
newValue -= (Via.getConfig().getHologramYOffset());
wrapper.set(Type.DOUBLE, 1, newValue);
2016-08-10 21:23:13 +02:00
}
}
});
}
});
entityRewriter.registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
2016-09-30 17:14:14 +02:00
registerClientbound(ClientboundPackets1_9_3.TITLE, new PacketHandlers() {
2016-11-06 10:37:26 +01:00
@Override
public void register() {
2016-11-06 10:37:26 +01:00
map(Type.VAR_INT); // 0 - Action
handler(wrapper -> {
int action = wrapper.get(Type.VAR_INT, 0);
2016-11-06 10:37:26 +01:00
// Handle the new ActionBar
if (action >= 2) {
wrapper.set(Type.VAR_INT, 0, action + 1);
2016-11-06 10:37:26 +01:00
}
});
}
});
registerClientbound(ClientboundPackets1_9_3.BLOCK_ACTION, new PacketHandlers() {
@Override
public void register() {
2023-10-19 13:15:26 +02:00
map(Type.POSITION1_8); // 0 - Position
map(Type.UNSIGNED_BYTE); // 1 - Action ID
map(Type.UNSIGNED_BYTE); // 2 - Action Param
map(Type.VAR_INT); // 3 - Block Type
// Cheap hack to ensure it's always right block
handler(actionWrapper -> {
if (Via.getConfig().isPistonAnimationPatch()) {
int id = actionWrapper.get(Type.VAR_INT, 0);
if (id == 33 || id == 29) {
actionWrapper.cancel();
}
}
});
}
});
registerClientbound(ClientboundPackets1_9_3.BLOCK_ENTITY_DATA, new PacketHandlers() {
2017-05-14 16:02:04 +02:00
@Override
public void register() {
2023-10-19 13:15:26 +02:00
map(Type.POSITION1_8); // 0 - Position
2017-05-14 16:02:04 +02:00
map(Type.UNSIGNED_BYTE); // 1 - Action
map(Type.NAMED_COMPOUND_TAG); // 2 - NBT data
2017-05-14 16:02:04 +02:00
handler(wrapper -> {
CompoundTag tag = wrapper.get(Type.NAMED_COMPOUND_TAG, 0);
if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 1)
EntityIdRewriter.toClientSpawner(tag);
if (tag.contains("id"))
// Handle new identifier
((StringTag) tag.get("id")).setValue(BlockEntityRewriter.toNewIdentifier((String) tag.get("id").getValue()));
2017-05-14 16:02:04 +02:00
});
}
});
registerClientbound(ClientboundPackets1_9_3.CHUNK_DATA, wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
Chunk chunk = wrapper.passthrough(ChunkType1_9_3.forEnvironment(clientWorld.getEnvironment()));
if (chunk.getBlockEntities() == null) return;
for (CompoundTag tag : chunk.getBlockEntities()) {
if (tag.contains("id")) {
String identifier = ((StringTag) tag.get("id")).getValue();
if (identifier.equals("MobSpawner")) {
EntityIdRewriter.toClientSpawner(tag);
2017-05-14 16:02:04 +02:00
}
// Handle new identifier
((StringTag) tag.get("id")).setValue(BlockEntityRewriter.toNewIdentifier(identifier));
}
2017-05-14 16:02:04 +02:00
}
});
registerClientbound(ClientboundPackets1_9_3.JOIN_GAME, new PacketHandlers() {
@Override
public void register() {
map(Type.INT); // 0 - Entity ID
map(Type.UNSIGNED_BYTE); // 1 - Gamemode
map(Type.INT); // 2 - Dimension
handler(wrapper -> {
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
});
}
});
registerClientbound(ClientboundPackets1_9_3.RESPAWN, new PacketHandlers() {
@Override
public void register() {
map(Type.INT);
handler(wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId);
});
}
});
2017-05-14 16:02:04 +02:00
this.registerClientbound(ClientboundPackets1_9_3.EFFECT, new PacketHandlers() {
@Override
public void register() {
this.map(Type.INT); //effectID
2023-10-19 13:15:26 +02:00
this.map(Type.POSITION1_8); //pos
this.map(Type.INT); //effectData
this.map(Type.BOOLEAN); //serverwide / global
handler(packetWrapper -> {
2020-08-10 22:28:40 +02:00
int effectID = packetWrapper.get(Type.INT, 0);
if (effectID == 2002) {
int data = packetWrapper.get(Type.INT, 1);
2020-08-10 22:28:40 +02:00
boolean isInstant = false;
Pair<Integer, Boolean> newData = PotionColorMapping.getNewData(data);
if (newData == null) {
Via.getPlatform().getLogger().warning("Received unknown 1.11 -> 1.10.2 potion data (" + data + ")");
data = 0;
} else {
2021-09-14 11:13:39 +02:00
data = newData.key();
isInstant = newData.value();
}
2020-08-10 22:28:40 +02:00
if (isInstant) {
packetWrapper.set(Type.INT, 0, 2007);
}
packetWrapper.set(Type.INT, 1, data);
}
});
}
});
2016-09-30 17:14:14 +02:00
/*
INCOMING PACKETS
*/
2016-09-30 17:14:14 +02:00
registerServerbound(ServerboundPackets1_9_3.PLAYER_BLOCK_PLACEMENT, new PacketHandlers() {
2016-09-30 17:14:14 +02:00
@Override
public void register() {
2023-10-19 13:15:26 +02:00
map(Type.POSITION1_8); // 0 - Location
2016-09-30 17:14:14 +02:00
map(Type.VAR_INT); // 1 - Face
map(Type.VAR_INT); // 2 - Hand
map(Type.FLOAT, toOldByte);
map(Type.FLOAT, toOldByte);
map(Type.FLOAT, toOldByte);
}
});
registerServerbound(ServerboundPackets1_9_3.CHAT_MESSAGE, new PacketHandlers() {
@Override
public void register() {
map(Type.STRING); // 0 - Message
handler(wrapper -> {
// 100 character limit on older servers
String msg = wrapper.get(Type.STRING, 0);
if (msg.length() > 100) {
wrapper.set(Type.STRING, 0, msg.substring(0, 100));
}
});
}
});
2016-08-10 14:51:32 +02:00
}
private int getNewSoundId(int id) {
if (id == 196) // Experience orb sound got removed
return -1;
if (id >= 85) // Shulker boxes
id += 2;
if (id >= 176) // Guardian flop
id += 1;
if (id >= 197) // evocation things
id += 8;
if (id >= 207) // Rip the Experience orb touch sound :'(
id -= 1;
if (id >= 279) // Liama's
id += 9;
if (id >= 296) // Mule chest
id += 1;
if (id >= 390) // Vex
id += 4;
if (id >= 400) // vindication
id += 3;
if (id >= 450) // Elytra
id += 1;
if (id >= 455) // Empty bottle
id += 1;
if (id >= 470) // Totem use
id += 1;
return id;
}
2016-08-10 14:51:32 +02:00
@Override
public void init(UserConnection userConnection) {
// Entity tracker
Refactor entity tracking and meta handling This essentially merges the two approaches to the metadata handling from ViaVersion and ViaBackwards and improves on both designs. ViaVersion did not track every single entity, but only those needed (at least in theory) and can work with untracked entities' metadata. It had a very simple method overridden by metadata rewriter implementations, directly operating on the full metadata list and manually handling meta index changes as well as item/block/particle id changes. ViaBackwards on the other hand had to track *every single* entity and threw warnings otherwise - while less prone to errors due to giving obvious warnings in the console, it unnecessarily tracks a lot of entities, and those warnings also annoys users when encountering virtual entity plugins (operating asynchronously and sending update packets while already untracked or not yet tracked). Dedicated MetaHandlers made id changes and filtering a lot easier to read and write. However, the actual metadata list handling and its distribution to handlers was not very well implemented and required a lot of list copying and creation as well as exception throws to cancel individual metadata entries. This version has MetaFilters built with a Builder containing multiple helper functions, and the entity tracking is properly given its own map, hashed by a Protocol's class, to be easily and generically accessible from anywhere with only a Protocol class from the UserConnection, along with more optimized metadata list iteration. The entity tracking is largely unchanged, keeping ViaVersion's approach to not having to track *all* entities (and being able to handle null types in meta handlers). All of this is by no means absolutely perfect, but is much less prone to errors than both previous systems and takes a lot less effort to actually write. A last possible change would be to use a primitive int to object map that is built to be concurrency save for the EntityTracker, tho that would have to be chosen carefully.
2021-05-24 23:24:50 +02:00
userConnection.addEntityTracker(this.getClass(), new EntityTracker1_11(userConnection));
2017-05-14 16:02:04 +02:00
if (!userConnection.has(ClientWorld.class))
userConnection.put(new ClientWorld());
2016-08-10 14:51:32 +02:00
}
@Override
public MetadataRewriter1_11To1_10 getEntityRewriter() {
return entityRewriter;
}
@Override
public InventoryPackets getItemRewriter() {
return itemRewriter;
}
2016-08-10 14:51:32 +02:00
}