2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
2023-01-12 12:45:53 +01:00
|
|
|
* Copyright (C) 2016-2023 ViaVersion and contributors
|
2021-03-22 23:06:40 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2021-04-26 20:52:34 +02:00
|
|
|
package com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets;
|
2020-11-12 21:34:12 +01:00
|
|
|
|
2022-05-20 09:53:39 +02:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.IntTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.ListTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
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
|
|
|
import com.viaversion.viaversion.api.data.entity.EntityTracker;
|
2021-06-06 10:08:15 +02:00
|
|
|
import com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types;
|
2021-04-26 21:16:10 +02:00
|
|
|
import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types;
|
2021-06-06 10:08:15 +02:00
|
|
|
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
|
2021-04-26 21:16:10 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
|
2021-04-28 17:40:57 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
|
2023-02-12 10:54:47 +01:00
|
|
|
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.type.Type;
|
2021-06-17 11:55:22 +02:00
|
|
|
import com.viaversion.viaversion.api.type.types.version.Types1_16;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.type.types.version.Types1_17;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4;
|
2021-06-06 10:08:15 +02:00
|
|
|
import com.viaversion.viaversion.rewriter.EntityRewriter;
|
2020-11-12 21:34:12 +01:00
|
|
|
|
2023-02-04 18:29:22 +01:00
|
|
|
public final class EntityPackets extends EntityRewriter<ClientboundPackets1_16_2, Protocol1_17To1_16_4> {
|
2020-11-12 21:34:12 +01:00
|
|
|
|
2021-06-06 10:08:15 +02:00
|
|
|
public EntityPackets(Protocol1_17To1_16_4 protocol) {
|
|
|
|
super(protocol);
|
|
|
|
mapTypes(Entity1_16_2Types.values(), Entity1_17Types.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerPackets() {
|
|
|
|
registerTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, Entity1_17Types.FALLING_BLOCK);
|
|
|
|
registerTracker(ClientboundPackets1_16_2.SPAWN_MOB);
|
|
|
|
registerTracker(ClientboundPackets1_16_2.SPAWN_PLAYER, Entity1_17Types.PLAYER);
|
2021-06-17 11:55:22 +02:00
|
|
|
registerMetadataRewriter(ClientboundPackets1_16_2.ENTITY_METADATA, Types1_16.METADATA_LIST, Types1_17.METADATA_LIST);
|
2021-04-28 17:40:57 +02:00
|
|
|
|
2023-02-12 11:44:25 +01:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_16_2.DESTROY_ENTITIES, null, wrapper -> {
|
|
|
|
int[] entityIds = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
|
|
|
|
wrapper.cancel();
|
|
|
|
|
|
|
|
EntityTracker entityTracker = wrapper.user().getEntityTracker(Protocol1_17To1_16_4.class);
|
|
|
|
for (int entityId : entityIds) {
|
|
|
|
entityTracker.removeEntity(entityId);
|
|
|
|
|
|
|
|
// Send individual remove packets
|
|
|
|
PacketWrapper newPacket = wrapper.create(ClientboundPackets1_17.REMOVE_ENTITY);
|
|
|
|
newPacket.write(Type.VAR_INT, entityId);
|
|
|
|
newPacket.send(Protocol1_17To1_16_4.class);
|
2021-04-28 17:40:57 +02:00
|
|
|
}
|
|
|
|
});
|
2021-02-03 19:30:28 +01:00
|
|
|
|
2023-02-12 10:54:47 +01:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_16_2.JOIN_GAME, new PacketHandlers() {
|
2022-05-20 09:53:39 +02:00
|
|
|
@Override
|
2023-02-12 10:54:47 +01:00
|
|
|
public void register() {
|
2022-05-20 09:53:39 +02:00
|
|
|
map(Type.INT); // Entity ID
|
|
|
|
map(Type.BOOLEAN); // Hardcore
|
|
|
|
map(Type.UNSIGNED_BYTE); // Gamemode
|
|
|
|
map(Type.BYTE); // Previous Gamemode
|
|
|
|
map(Type.STRING_ARRAY); // World List
|
2023-10-06 13:37:52 +02:00
|
|
|
map(Type.NAMED_COMPOUND_TAG); // Registry
|
|
|
|
map(Type.NAMED_COMPOUND_TAG); // Current dimension
|
2022-05-20 09:53:39 +02:00
|
|
|
handler(wrapper -> {
|
|
|
|
// Add new dimension fields
|
2023-10-06 13:37:52 +02:00
|
|
|
CompoundTag dimensionRegistry = wrapper.get(Type.NAMED_COMPOUND_TAG, 0).get("minecraft:dimension_type");
|
2022-05-20 09:53:39 +02:00
|
|
|
ListTag dimensions = dimensionRegistry.get("value");
|
|
|
|
for (Tag dimension : dimensions) {
|
|
|
|
CompoundTag dimensionCompound = ((CompoundTag) dimension).get("element");
|
|
|
|
addNewDimensionData(dimensionCompound);
|
|
|
|
}
|
|
|
|
|
2023-10-06 13:37:52 +02:00
|
|
|
CompoundTag currentDimensionTag = wrapper.get(Type.NAMED_COMPOUND_TAG, 1);
|
2022-05-20 09:53:39 +02:00
|
|
|
addNewDimensionData(currentDimensionTag);
|
|
|
|
});
|
|
|
|
handler(playerTrackerHandler());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-12 11:44:25 +01:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, wrapper -> {
|
2023-10-06 13:37:52 +02:00
|
|
|
CompoundTag dimensionData = wrapper.passthrough(Type.NAMED_COMPOUND_TAG);
|
2023-02-12 11:44:25 +01:00
|
|
|
addNewDimensionData(dimensionData);
|
2022-05-20 09:53:39 +02:00
|
|
|
});
|
|
|
|
|
2023-02-12 10:54:47 +01:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_16_2.ENTITY_PROPERTIES, new PacketHandlers() {
|
2021-02-24 22:28:39 +01:00
|
|
|
@Override
|
2023-02-12 10:54:47 +01:00
|
|
|
public void register() {
|
2021-02-24 22:28:39 +01:00
|
|
|
map(Type.VAR_INT); // Entity id
|
|
|
|
handler(wrapper -> {
|
|
|
|
// Collection length is now a var int
|
|
|
|
wrapper.write(Type.VAR_INT, wrapper.read(Type.INT));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-12 10:54:47 +01:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_16_2.PLAYER_POSITION, new PacketHandlers() {
|
2021-02-03 19:30:28 +01:00
|
|
|
@Override
|
2023-02-12 10:54:47 +01:00
|
|
|
public void register() {
|
2021-02-03 19:30:28 +01:00
|
|
|
map(Type.DOUBLE);
|
|
|
|
map(Type.DOUBLE);
|
|
|
|
map(Type.DOUBLE);
|
|
|
|
map(Type.FLOAT);
|
|
|
|
map(Type.FLOAT);
|
2021-02-11 19:54:00 +01:00
|
|
|
map(Type.BYTE);
|
2021-02-03 19:30:28 +01:00
|
|
|
map(Type.VAR_INT);
|
|
|
|
handler(wrapper -> {
|
|
|
|
// Dismount vehicle
|
|
|
|
wrapper.write(Type.BOOLEAN, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-02-24 22:28:39 +01:00
|
|
|
|
2023-02-12 11:44:25 +01:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_16_2.COMBAT_EVENT, null, wrapper -> {
|
|
|
|
// Combat packet actions have been split into individual packets (the content hasn't changed)
|
|
|
|
int type = wrapper.read(Type.VAR_INT);
|
|
|
|
ClientboundPacketType packetType;
|
|
|
|
switch (type) {
|
|
|
|
case 0:
|
|
|
|
packetType = ClientboundPackets1_17.COMBAT_ENTER;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
packetType = ClientboundPackets1_17.COMBAT_END;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
packetType = ClientboundPackets1_17.COMBAT_KILL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Invalid combat type received: " + type);
|
2021-02-24 22:28:39 +01:00
|
|
|
}
|
2023-02-12 11:44:25 +01:00
|
|
|
|
|
|
|
wrapper.setPacketType(packetType);
|
2021-02-24 22:28:39 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// The parent class of the other entity move packets that is never actually used has finally been removed from the id list
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.cancelClientbound(ClientboundPackets1_16_2.ENTITY_MOVEMENT);
|
2020-11-12 21:34:12 +01:00
|
|
|
}
|
2021-06-06 10:08:15 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void registerRewrites() {
|
|
|
|
filter().handler((event, meta) -> {
|
2021-11-10 10:30:17 +01:00
|
|
|
meta.setMetaType(Types1_17.META_TYPES.byId(meta.metaType().typeId()));
|
2021-06-06 10:08:15 +02:00
|
|
|
|
2021-11-10 10:30:17 +01:00
|
|
|
if (meta.metaType() == Types1_17.META_TYPES.poseType) {
|
2021-06-06 10:08:15 +02:00
|
|
|
int pose = meta.value();
|
|
|
|
if (pose > 5) {
|
|
|
|
// Added LONG_JUMP at 6
|
|
|
|
meta.setValue(pose + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-04-18 13:34:20 +02:00
|
|
|
registerMetaTypeHandler(Types1_17.META_TYPES.itemType, Types1_17.META_TYPES.blockStateType, null, Types1_17.META_TYPES.particleType);
|
2021-06-06 10:08:15 +02:00
|
|
|
|
|
|
|
// Ticks frozen added with id 7
|
|
|
|
filter().filterFamily(Entity1_17Types.ENTITY).addIndex(7);
|
|
|
|
|
|
|
|
filter().filterFamily(Entity1_17Types.MINECART_ABSTRACT).index(11).handler((event, meta) -> {
|
|
|
|
// Convert to new block id
|
|
|
|
int data = (int) meta.getValue();
|
|
|
|
meta.setValue(protocol.getMappingData().getNewBlockStateId(data));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Attachment position removed
|
|
|
|
filter().type(Entity1_17Types.SHULKER).removeIndex(17);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EntityType typeFromId(int type) {
|
|
|
|
return Entity1_17Types.getTypeFromId(type);
|
|
|
|
}
|
2022-05-20 09:53:39 +02:00
|
|
|
|
|
|
|
private static void addNewDimensionData(CompoundTag tag) {
|
|
|
|
tag.put("min_y", new IntTag(0));
|
|
|
|
tag.put("height", new IntTag(256));
|
|
|
|
}
|
2020-11-12 21:34:12 +01:00
|
|
|
}
|