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

187 lines
8.2 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_17to1_16_4.packets;
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;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
2023-10-19 01:28:12 +02:00
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_16_2;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_17;
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;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.version.Types1_16;
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;
import com.viaversion.viaversion.rewriter.EntityRewriter;
public final class EntityPackets extends EntityRewriter<ClientboundPackets1_16_2, Protocol1_17To1_16_4> {
public EntityPackets(Protocol1_17To1_16_4 protocol) {
super(protocol);
2023-10-19 01:28:12 +02:00
mapTypes(EntityTypes1_16_2.values(), EntityTypes1_17.class);
}
@Override
public void registerPackets() {
2023-10-19 01:28:12 +02:00
registerTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, EntityTypes1_17.FALLING_BLOCK);
registerTracker(ClientboundPackets1_16_2.SPAWN_MOB);
2023-10-19 01:28:12 +02:00
registerTracker(ClientboundPackets1_16_2.SPAWN_PLAYER, EntityTypes1_17.PLAYER);
registerMetadataRewriter(ClientboundPackets1_16_2.ENTITY_METADATA, Types1_16.METADATA_LIST, Types1_17.METADATA_LIST);
2021-04-28 17:40:57 +02: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
protocol.registerClientbound(ClientboundPackets1_16_2.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); // Entity ID
map(Type.BOOLEAN); // Hardcore
map(Type.BYTE); // Gamemode
2022-05-20 09:53:39 +02:00
map(Type.BYTE); // Previous Gamemode
map(Type.STRING_ARRAY); // World List
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
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);
}
CompoundTag currentDimensionTag = wrapper.get(Type.NAMED_COMPOUND_TAG, 1);
2022-05-20 09:53:39 +02:00
addNewDimensionData(currentDimensionTag);
});
handler(playerTrackerHandler());
}
});
protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, wrapper -> {
CompoundTag dimensionData = wrapper.passthrough(Type.NAMED_COMPOUND_TAG);
addNewDimensionData(dimensionData);
2022-05-20 09:53:39 +02:00
});
protocol.registerClientbound(ClientboundPackets1_16_2.ENTITY_PROPERTIES, new PacketHandlers() {
2021-02-24 22:28:39 +01:00
@Override
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));
});
}
});
protocol.registerClientbound(ClientboundPackets1_16_2.PLAYER_POSITION, new PacketHandlers() {
2021-02-03 19:30:28 +01:00
@Override
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
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
}
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
protocol.cancelClientbound(ClientboundPackets1_16_2.ENTITY_MOVEMENT);
}
@Override
protected void registerRewrites() {
filter().mapMetaType(Types1_17.META_TYPES::byId);
filter().metaType(Types1_17.META_TYPES.poseType).handler((event, meta) -> {
int pose = meta.value();
if (pose > 5) {
// Added LONG_JUMP at 6
meta.setValue(pose + 1);
}
});
registerMetaTypeHandler(Types1_17.META_TYPES.itemType, Types1_17.META_TYPES.blockStateType, null, Types1_17.META_TYPES.particleType);
// Ticks frozen added with id 7
filter().type(EntityTypes1_17.ENTITY).addIndex(7);
filter().type(EntityTypes1_17.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
2023-10-19 01:28:12 +02:00
filter().type(EntityTypes1_17.SHULKER).removeIndex(17);
}
@Override
public EntityType typeFromId(int type) {
2023-10-19 01:28:12 +02:00
return EntityTypes1_17.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));
}
}