ViaVersion/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java

264 lines
13 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_14to1_13_2.packets;
2018-10-24 19:05:14 +02:00
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
2023-10-19 01:28:12 +02:00
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_13;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_14;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.version.Types1_13_2;
import com.viaversion.viaversion.api.type.types.version.Types1_14;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14;
2019-02-20 20:28:14 +01:00
import java.util.LinkedList;
import java.util.List;
2018-10-24 19:05:14 +02:00
public class EntityPackets {
public static void register(Protocol1_14To1_13_2 protocol) {
MetadataRewriter1_14To1_13_2 metadataRewriter = protocol.get(MetadataRewriter1_14To1_13_2.class);
2018-10-27 13:25:42 +02:00
protocol.registerClientbound(ClientboundPackets1_13.SPAWN_EXPERIENCE_ORB, wrapper -> {
int entityId = wrapper.passthrough(Type.VAR_INT);
metadataRewriter.tracker(wrapper.user()).addEntity(entityId, EntityTypes1_14.EXPERIENCE_ORB);
});
protocol.registerClientbound(ClientboundPackets1_13.SPAWN_GLOBAL_ENTITY, wrapper -> {
int entityId = wrapper.passthrough(Type.VAR_INT);
if (wrapper.passthrough(Type.BYTE) == 1) {
metadataRewriter.tracker(wrapper.user()).addEntity(entityId, EntityTypes1_14.LIGHTNING_BOLT);
}
});
protocol.registerClientbound(ClientboundPackets1_13.SPAWN_ENTITY, new PacketHandlers() {
2018-10-27 13:25:42 +02:00
@Override
public void register() {
2018-10-27 13:25:42 +02:00
map(Type.VAR_INT); // 0 - Entity id
map(Type.UUID); // 1 - UUID
2019-01-30 17:16:06 +01:00
map(Type.BYTE, Type.VAR_INT); // 2 - Type
2018-10-27 13:25:42 +02:00
map(Type.DOUBLE); // 3 - X
map(Type.DOUBLE); // 4 - Y
map(Type.DOUBLE); // 5 - Z
map(Type.BYTE); // 6 - Pitch
map(Type.BYTE); // 7 - Yaw
map(Type.INT); // 8 - Data
2019-04-21 18:46:17 +02:00
map(Type.SHORT); // 9 - Velocity X
map(Type.SHORT); // 10 - Velocity Y
map(Type.SHORT); // 11 - Velocity Z
2018-10-27 13:25:42 +02:00
// Track Entity
handler(wrapper -> {
int entityId = wrapper.get(Type.VAR_INT, 0);
int typeId = wrapper.get(Type.VAR_INT, 1);
2023-10-19 01:28:12 +02:00
EntityTypes1_13.EntityType type1_13 = EntityTypes1_13.getTypeFromId(typeId, true);
typeId = metadataRewriter.newEntityId(type1_13.getId());
2023-10-19 01:28:12 +02:00
EntityType type1_14 = EntityTypes1_14.getTypeFromId(typeId);
if (type1_14 != null) {
int data = wrapper.get(Type.INT, 0);
2023-10-19 01:28:12 +02:00
if (type1_14.is(EntityTypes1_14.FALLING_BLOCK)) {
wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(data));
2023-10-19 01:28:12 +02:00
} else if (type1_14.is(EntityTypes1_14.MINECART)) {
// default is 0 = rideable minecart
switch (data) {
case 1:
2023-10-19 01:28:12 +02:00
typeId = EntityTypes1_14.CHEST_MINECART.getId();
break;
case 2:
2023-10-19 01:28:12 +02:00
typeId = EntityTypes1_14.FURNACE_MINECART.getId();
break;
case 3:
2023-10-19 01:28:12 +02:00
typeId = EntityTypes1_14.TNT_MINECART.getId();
break;
case 4:
2023-10-19 01:28:12 +02:00
typeId = EntityTypes1_14.SPAWNER_MINECART.getId();
break;
case 5:
2023-10-19 01:28:12 +02:00
typeId = EntityTypes1_14.HOPPER_MINECART.getId();
break;
case 6:
2023-10-19 01:28:12 +02:00
typeId = EntityTypes1_14.COMMAND_BLOCK_MINECART.getId();
break;
2018-10-27 13:25:42 +02:00
}
2023-10-19 01:28:12 +02:00
} else if ((type1_14.is(EntityTypes1_14.ITEM) && data > 0)
|| type1_14.isOrHasParent(EntityTypes1_14.ABSTRACT_ARROW)) {
if (type1_14.isOrHasParent(EntityTypes1_14.ABSTRACT_ARROW)) {
wrapper.set(Type.INT, 0, data - 1);
}
// send velocity in separate packet, 1.14 is now ignoring the velocity
PacketWrapper velocity = wrapper.create(0x45);
velocity.write(Type.VAR_INT, entityId);
velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 0));
velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 1));
velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 2));
velocity.scheduleSend(Protocol1_14To1_13_2.class);
2018-10-27 13:25:42 +02:00
}
2019-01-30 17:16:06 +01:00
// Register Type ID
wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class).addEntity(entityId, type1_14);
2018-10-27 13:25:42 +02:00
}
wrapper.set(Type.VAR_INT, 1, typeId);
2018-10-27 13:25:42 +02:00
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.SPAWN_MOB, new PacketHandlers() {
2018-10-27 13:25:42 +02:00
@Override
public void register() {
2018-10-27 13:25:42 +02:00
map(Type.VAR_INT); // 0 - Entity ID
map(Type.UUID); // 1 - Entity UUID
map(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_13_2.METADATA_LIST, Types1_14.METADATA_LIST); // 12 - Metadata
2018-10-27 13:25:42 +02:00
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
handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST));
2018-10-27 13:25:42 +02:00
}
});
protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PAINTING, new PacketHandlers() {
2018-10-27 13:25:42 +02:00
@Override
public void register() {
2018-10-27 13:25:42 +02:00
map(Type.VAR_INT);
map(Type.UUID);
map(Type.VAR_INT);
2023-10-19 13:15:26 +02:00
map(Type.POSITION1_8, Type.POSITION1_14);
2018-10-27 13:25:42 +02:00
map(Type.BYTE);
handler(wrapper -> metadataRewriter.tracker(wrapper.user()).addEntity(wrapper.get(Type.VAR_INT, 0), EntityTypes1_14.PAINTING));
2018-10-27 13:25:42 +02:00
}
});
protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PLAYER, new PacketHandlers() {
2018-10-27 13:25:42 +02:00
@Override
public void register() {
2018-10-27 13:25:42 +02:00
map(Type.VAR_INT); // 0 - Entity ID
map(Type.UUID); // 1 - Player UUID
map(Type.DOUBLE); // 2 - X
map(Type.DOUBLE); // 3 - Y
map(Type.DOUBLE); // 4 - Z
map(Type.BYTE); // 5 - Yaw
map(Type.BYTE); // 6 - Pitch
map(Types1_13_2.METADATA_LIST, Types1_14.METADATA_LIST); // 7 - Metadata
2018-10-27 13:25:42 +02:00
2023-10-19 01:28:12 +02:00
handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST, EntityTypes1_14.PLAYER));
2018-10-27 13:25:42 +02:00
}
});
protocol.registerClientbound(ClientboundPackets1_13.ENTITY_ANIMATION, new PacketHandlers() {
2019-02-20 20:28:14 +01:00
@Override
public void register() {
2019-02-20 20:28:14 +01:00
map(Type.VAR_INT);
handler(wrapper -> {
short animation = wrapper.passthrough(Type.UNSIGNED_BYTE);
if (animation == 2) { //Leave bed
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
int entityId = wrapper.get(Type.VAR_INT, 0);
tracker.setSleeping(entityId, false);
2019-04-27 21:23:24 +02:00
PacketWrapper metadataPacket = wrapper.create(ClientboundPackets1_14.ENTITY_METADATA);
metadataPacket.write(Type.VAR_INT, entityId);
List<Metadata> metadataList = new LinkedList<>();
if (tracker.clientEntityId() != entityId) {
metadataList.add(new Metadata(6, Types1_14.META_TYPES.poseType, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker)));
2019-02-20 20:28:14 +01:00
}
metadataList.add(new Metadata(12, Types1_14.META_TYPES.optionalPositionType, null));
metadataPacket.write(Types1_14.METADATA_LIST, metadataList);
metadataPacket.scheduleSend(Protocol1_14To1_13_2.class);
2019-02-20 20:28:14 +01:00
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.JOIN_GAME, new PacketHandlers() {
2022-05-20 09:53:39 +02:00
@Override
public void register() {
2022-05-20 09:53:39 +02:00
map(Type.INT); // 0 - Entity ID
map(Type.UNSIGNED_BYTE); // 1 - Gamemode
map(Type.INT); // 2 - Dimension
handler(wrapper -> {
// Store the player
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
});
handler(metadataRewriter.playerTrackerHandler());
handler(wrapper -> {
short difficulty = wrapper.read(Type.UNSIGNED_BYTE); // 19w11a removed difficulty from join game
PacketWrapper difficultyPacket = wrapper.create(ClientboundPackets1_14.SERVER_DIFFICULTY);
difficultyPacket.write(Type.UNSIGNED_BYTE, difficulty);
difficultyPacket.write(Type.BOOLEAN, false); // Unknown value added in 19w11a
difficultyPacket.scheduleSend(protocol.getClass());
wrapper.passthrough(Type.UNSIGNED_BYTE); // Max Players
wrapper.passthrough(Type.STRING); // Level Type
wrapper.write(Type.VAR_INT, WorldPackets.SERVERSIDE_VIEW_DISTANCE); // Serverside view distance, added in 19w13a
});
handler(wrapper -> {
// Manually send the packet
wrapper.send(Protocol1_14To1_13_2.class);
wrapper.cancel();
// View distance has to be sent after the join packet
WorldPackets.sendViewDistancePacket(wrapper.user());
});
}
});
protocol.registerClientbound(ClientboundPackets1_13.USE_BED, ClientboundPackets1_14.ENTITY_METADATA, new PacketHandlers() {
2018-10-27 13:25:42 +02:00
@Override
public void register() {
2018-10-27 13:25:42 +02:00
map(Type.VAR_INT);
handler(wrapper -> {
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
int entityId = wrapper.get(Type.VAR_INT, 0);
tracker.setSleeping(entityId, true);
2023-10-19 13:15:26 +02:00
Position position = wrapper.read(Type.POSITION1_8);
List<Metadata> metadataList = new LinkedList<>();
metadataList.add(new Metadata(12, Types1_14.META_TYPES.optionalPositionType, position));
if (tracker.clientEntityId() != entityId) {
metadataList.add(new Metadata(6, Types1_14.META_TYPES.poseType, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker)));
2019-02-20 18:01:21 +01:00
}
wrapper.write(Types1_14.METADATA_LIST, metadataList);
2019-02-20 18:01:21 +01:00
});
2018-10-27 13:25:42 +02:00
}
});
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
metadataRewriter.registerRemoveEntities(ClientboundPackets1_13.DESTROY_ENTITIES);
metadataRewriter.registerMetadataRewriter(ClientboundPackets1_13.ENTITY_METADATA, Types1_13_2.METADATA_LIST, Types1_14.METADATA_LIST);
2018-10-27 13:25:42 +02:00
}
2018-10-24 19:05:14 +02:00
}