2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
|
|
|
* Copyright (C) 2016-2021 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/>.
|
|
|
|
*/
|
2021-04-26 20:52:34 +02:00
|
|
|
package com.viaversion.viaversion.protocols.protocol1_9to1_8.packets;
|
2016-03-13 18:01:36 +01:00
|
|
|
|
2018-08-11 17:31:45 +02:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.Via;
|
|
|
|
import com.viaversion.viaversion.api.minecraft.item.Item;
|
|
|
|
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
|
2021-04-27 13:41:39 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
|
2021-04-26 21:16:10 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
|
|
|
|
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
|
|
|
|
import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.type.Type;
|
|
|
|
import com.viaversion.viaversion.api.type.types.version.Types1_8;
|
|
|
|
import com.viaversion.viaversion.api.type.types.version.Types1_9;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;
|
2021-06-01 23:27:33 +02:00
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ItemRewriter;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ServerboundPackets1_9;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8;
|
|
|
|
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9;
|
2021-04-27 13:41:39 +02:00
|
|
|
import com.viaversion.viaversion.util.Pair;
|
|
|
|
import com.viaversion.viaversion.util.Triple;
|
2016-03-13 18:01:36 +01:00
|
|
|
|
2020-06-06 10:54:16 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
2016-03-15 01:34:20 +01:00
|
|
|
|
2016-03-13 18:01:36 +01:00
|
|
|
public class EntityPackets {
|
2016-09-27 18:31:10 +02:00
|
|
|
public static final ValueTransformer<Byte, Short> toNewShort = new ValueTransformer<Byte, Short>(Type.SHORT) {
|
2016-03-13 18:01:36 +01:00
|
|
|
@Override
|
|
|
|
public Short transform(PacketWrapper wrapper, Byte inputValue) {
|
|
|
|
return (short) (inputValue * 128);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-05 20:30:09 +02:00
|
|
|
public static void register(Protocol1_9To1_8 protocol) {
|
2016-03-13 18:01:36 +01:00
|
|
|
// Attach Entity Packet
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.ATTACH_ENTITY, new PacketRemapper() {
|
2016-03-13 18:01:36 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.INT); // 0 - Entity ID
|
|
|
|
map(Type.INT); // 1 - Vehicle
|
|
|
|
|
|
|
|
// Leash boolean is removed in new versions
|
|
|
|
map(Type.BOOLEAN, new ValueTransformer<Boolean, Void>(Type.NOTHING) {
|
|
|
|
@Override
|
2016-03-14 17:51:56 +01:00
|
|
|
public Void transform(PacketWrapper wrapper, Boolean inputValue) throws Exception {
|
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
|
|
|
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
|
2016-03-14 13:40:23 +01:00
|
|
|
if (!inputValue) {
|
2016-03-21 20:13:55 +01:00
|
|
|
int passenger = wrapper.get(Type.INT, 0);
|
|
|
|
int vehicle = wrapper.get(Type.INT, 1);
|
2016-03-14 17:51:56 +01:00
|
|
|
|
|
|
|
wrapper.cancel(); // Don't send current packet
|
|
|
|
|
2021-06-01 23:27:33 +02:00
|
|
|
PacketWrapper passengerPacket = wrapper.create(ClientboundPackets1_9.SET_PASSENGERS);
|
2016-03-14 17:51:56 +01:00
|
|
|
if (vehicle == -1) {
|
|
|
|
if (!tracker.getVehicleMap().containsKey(passenger))
|
|
|
|
return null; // Cancel
|
|
|
|
passengerPacket.write(Type.VAR_INT, tracker.getVehicleMap().remove(passenger));
|
2019-11-02 14:00:27 +01:00
|
|
|
passengerPacket.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{});
|
2016-03-14 17:51:56 +01:00
|
|
|
} else {
|
|
|
|
passengerPacket.write(Type.VAR_INT, vehicle);
|
2019-11-02 14:00:27 +01:00
|
|
|
passengerPacket.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{passenger});
|
2016-03-18 21:22:38 +01:00
|
|
|
tracker.getVehicleMap().put(passenger, vehicle);
|
2016-03-14 17:51:56 +01:00
|
|
|
}
|
2019-04-23 16:34:17 +02:00
|
|
|
passengerPacket.send(Protocol1_9To1_8.class); // Send the packet
|
2016-03-13 18:01:36 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.ENTITY_TELEPORT, new PacketRemapper() {
|
2016-03-13 18:01:36 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.VAR_INT); // 0 - Entity ID
|
|
|
|
map(Type.INT, SpawnPackets.toNewDouble); // 1 - X - Needs to be divide by 32
|
|
|
|
map(Type.INT, SpawnPackets.toNewDouble); // 2 - Y - Needs to be divide by 32
|
|
|
|
map(Type.INT, SpawnPackets.toNewDouble); // 3 - Z - Needs to be divide by 32
|
|
|
|
|
|
|
|
map(Type.BYTE); // 4 - Pitch
|
|
|
|
map(Type.BYTE); // 5 - Yaw
|
|
|
|
|
|
|
|
map(Type.BOOLEAN); // 6 - On Ground
|
|
|
|
|
2016-03-14 17:51:56 +01:00
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
int entityID = wrapper.get(Type.VAR_INT, 0);
|
2016-09-25 16:40:30 +02:00
|
|
|
if (Via.getConfig().isHologramPatch()) {
|
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
|
|
|
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
|
2016-03-14 17:51:56 +01:00
|
|
|
if (tracker.getKnownHolograms().contains(entityID)) {
|
|
|
|
Double newValue = wrapper.get(Type.DOUBLE, 1);
|
2016-09-25 16:40:30 +02:00
|
|
|
newValue += (Via.getConfig().getHologramYOffset());
|
2016-03-14 17:51:56 +01:00
|
|
|
wrapper.set(Type.DOUBLE, 1, newValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-03-15 01:34:20 +01:00
|
|
|
|
|
|
|
|
2016-03-13 18:01:36 +01:00
|
|
|
}
|
|
|
|
});
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.ENTITY_POSITION_AND_ROTATION, new PacketRemapper() {
|
2016-03-13 18:01:36 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.VAR_INT); // 0 - Entity ID
|
|
|
|
map(Type.BYTE, toNewShort); // 1 - X
|
|
|
|
map(Type.BYTE, toNewShort); // 2 - Y
|
|
|
|
map(Type.BYTE, toNewShort); // 3 - Z
|
|
|
|
|
|
|
|
map(Type.BYTE); // 4 - Yaw
|
|
|
|
map(Type.BYTE); // 5 - Pitch
|
|
|
|
|
|
|
|
map(Type.BOOLEAN); // 6 - On Ground
|
|
|
|
}
|
|
|
|
});
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.ENTITY_POSITION, new PacketRemapper() {
|
2016-03-13 18:01:36 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.VAR_INT); // 0 - Entity ID
|
|
|
|
map(Type.BYTE, toNewShort); // 1 - X
|
|
|
|
map(Type.BYTE, toNewShort); // 2 - Y
|
|
|
|
map(Type.BYTE, toNewShort); // 3 - Z
|
|
|
|
|
|
|
|
map(Type.BOOLEAN); // 4 - On Ground
|
|
|
|
}
|
|
|
|
});
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.ENTITY_EQUIPMENT, new PacketRemapper() {
|
2016-03-13 18:01:36 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.VAR_INT); // 0 - Entity ID
|
|
|
|
// 1 - Slot ID
|
|
|
|
map(Type.SHORT, new ValueTransformer<Short, Integer>(Type.VAR_INT) {
|
|
|
|
@Override
|
2019-09-19 11:22:06 +02:00
|
|
|
public Integer transform(PacketWrapper wrapper, Short slot) throws Exception {
|
|
|
|
int entityId = wrapper.get(Type.VAR_INT, 0);
|
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
|
|
|
int receiverId = wrapper.user().getEntityTracker(Protocol1_9To1_8.class).clientEntityId();
|
2019-09-19 11:22:06 +02:00
|
|
|
// Normally, 0 = hand and 1-4 = armor
|
|
|
|
// ... but if the sent id is equal to the receiver's id, 0-3 will instead mark the armor slots
|
|
|
|
// (In 1.9+, every client treats the received the same: 0=hand, 1=offhand, 2-5=armor)
|
|
|
|
if (entityId == receiverId) {
|
|
|
|
return slot.intValue() + 2;
|
|
|
|
}
|
2016-03-13 18:01:36 +01:00
|
|
|
return slot > 0 ? slot.intValue() + 1 : slot.intValue();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
map(Type.ITEM); // 2 - Item
|
2016-03-16 22:52:00 +01:00
|
|
|
// Item Rewriter
|
2016-03-16 20:46:16 +01:00
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
Item stack = wrapper.get(Type.ITEM, 0);
|
|
|
|
ItemRewriter.toClient(stack);
|
|
|
|
}
|
|
|
|
});
|
2016-03-16 22:52:00 +01:00
|
|
|
// Blocking
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
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
|
|
|
EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
|
2016-03-16 22:52:00 +01:00
|
|
|
int entityID = wrapper.get(Type.VAR_INT, 0);
|
|
|
|
Item stack = wrapper.get(Type.ITEM, 0);
|
|
|
|
|
2016-03-18 22:18:48 +01:00
|
|
|
if (stack != null) {
|
2021-06-04 12:15:14 +02:00
|
|
|
if (Protocol1_9To1_8.isSword(stack.identifier())) {
|
2016-09-25 16:40:30 +02:00
|
|
|
entityTracker.getValidBlocking().add(entityID);
|
|
|
|
return;
|
2016-03-16 22:52:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
entityTracker.getValidBlocking().remove(entityID);
|
|
|
|
}
|
|
|
|
});
|
2016-03-13 18:01:36 +01:00
|
|
|
}
|
|
|
|
});
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.ENTITY_METADATA, new PacketRemapper() {
|
2016-03-13 18:01:36 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.VAR_INT); // 0 - Entity ID
|
2016-07-11 15:52:06 +02:00
|
|
|
map(Types1_8.METADATA_LIST, Types1_9.METADATA_LIST); // 1 - Metadata List
|
2016-03-15 01:34:20 +01:00
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
2016-07-11 15:52:06 +02:00
|
|
|
List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
|
2019-05-09 14:43:48 +02:00
|
|
|
int entityId = wrapper.get(Type.VAR_INT, 0);
|
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
|
|
|
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
|
2019-09-27 20:25:18 +02:00
|
|
|
if (tracker.hasEntity(entityId)) {
|
|
|
|
protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user());
|
2016-05-27 02:00:01 +02:00
|
|
|
} else {
|
|
|
|
// Buffer
|
2019-05-09 14:43:48 +02:00
|
|
|
tracker.addMetadataToBuffer(entityId, metadataList);
|
2016-05-27 02:00:01 +02:00
|
|
|
wrapper.cancel();
|
2016-03-15 01:34:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-03-16 22:52:00 +01:00
|
|
|
|
|
|
|
// Handler for meta data
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
2016-07-11 15:52:06 +02:00
|
|
|
List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
|
2016-03-16 22:52:00 +01:00
|
|
|
int entityID = wrapper.get(Type.VAR_INT, 0);
|
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
|
|
|
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
|
2016-03-16 22:52:00 +01:00
|
|
|
tracker.handleMetadata(entityID, metadataList);
|
|
|
|
}
|
|
|
|
});
|
2016-04-01 19:13:23 +02:00
|
|
|
|
|
|
|
// Cancel packet if list empty
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
2016-07-11 15:52:06 +02:00
|
|
|
List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
|
2020-06-06 10:54:16 +02:00
|
|
|
if (metadataList.isEmpty()) {
|
2016-04-01 19:13:23 +02:00
|
|
|
wrapper.cancel();
|
2016-05-27 02:00:01 +02:00
|
|
|
}
|
2016-04-01 19:13:23 +02:00
|
|
|
}
|
|
|
|
});
|
2016-03-13 18:01:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.ENTITY_EFFECT, new PacketRemapper() {
|
2016-03-13 18:01:36 +01:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.VAR_INT); // 0 - Entity ID
|
|
|
|
map(Type.BYTE); // 1 - Effect ID
|
|
|
|
map(Type.BYTE); // 2 - Amplifier
|
|
|
|
map(Type.VAR_INT); // 3 - Duration
|
2016-03-23 12:42:03 +01:00
|
|
|
handler(new PacketHandler() { //Handle effect indicator
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
boolean showParticles = wrapper.read(Type.BOOLEAN); //In 1.8 = true->Show particles : false->Hide particles
|
2016-09-25 16:40:30 +02:00
|
|
|
boolean newEffect = Via.getConfig().isNewEffectIndicator();
|
2016-03-23 12:42:03 +01:00
|
|
|
//0: hide, 1: shown without indictator, 2: shown with indicator, 3: hide with beacon indicator but we don't use it.
|
|
|
|
wrapper.write(Type.BYTE, (byte) (showParticles ? newEffect ? 2 : 1 : 0));
|
|
|
|
}
|
|
|
|
});
|
2016-03-13 18:01:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.cancelClientbound(ClientboundPackets1_8.UPDATE_ENTITY_NBT);
|
2016-03-29 16:01:38 +02:00
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.COMBAT_EVENT, new PacketRemapper() {
|
2016-04-12 12:18:51 +02:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.VAR_INT); //Event id
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
if (wrapper.get(Type.VAR_INT, 0) == 2) { // entity dead
|
|
|
|
wrapper.passthrough(Type.VAR_INT); //Player id
|
|
|
|
wrapper.passthrough(Type.INT); //Entity id
|
2019-04-23 16:34:17 +02:00
|
|
|
Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
|
2016-04-12 12:18:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-03-29 16:01:38 +02:00
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.ENTITY_PROPERTIES, new PacketRemapper() {
|
2018-08-11 17:31:45 +02:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.VAR_INT);
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
if (!Via.getConfig().isMinimizeCooldown()) return;
|
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
|
|
|
|
|
|
|
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
|
|
|
|
if (wrapper.get(Type.VAR_INT, 0) != tracker.getProvidedEntityId()) {
|
2018-08-11 17:31:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int propertiesToRead = wrapper.read(Type.INT);
|
|
|
|
Map<String, Pair<Double, List<Triple<UUID, Double, Byte>>>> properties = new HashMap<>(propertiesToRead);
|
|
|
|
for (int i = 0; i < propertiesToRead; i++) {
|
|
|
|
String key = wrapper.read(Type.STRING);
|
|
|
|
Double value = wrapper.read(Type.DOUBLE);
|
|
|
|
int modifiersToRead = wrapper.read(Type.VAR_INT);
|
|
|
|
List<Triple<UUID, Double, Byte>> modifiers = new ArrayList<>(modifiersToRead);
|
|
|
|
for (int j = 0; j < modifiersToRead; j++) {
|
|
|
|
modifiers.add(
|
|
|
|
new Triple<>(
|
|
|
|
wrapper.read(Type.UUID),
|
|
|
|
wrapper.read(Type.DOUBLE), // Amount
|
|
|
|
wrapper.read(Type.BYTE) // Operation
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
properties.put(key, new Pair<>(value, modifiers));
|
|
|
|
}
|
2016-03-13 18:01:36 +01:00
|
|
|
|
2018-08-11 17:31:45 +02:00
|
|
|
// == Why 15.9? ==
|
|
|
|
// Higher values hides the cooldown but it bugs visual animation on hand
|
|
|
|
// when removing item from hand with inventory gui
|
|
|
|
properties.put("generic.attackSpeed", new Pair<Double, List<Triple<UUID, Double, Byte>>>(15.9, ImmutableList.of( // Neutralize modifiers
|
|
|
|
new Triple<>(UUID.fromString("FA233E1C-4180-4865-B01B-BCCE9785ACA3"), 0.0, (byte) 0), // Tool and weapon modifier
|
|
|
|
new Triple<>(UUID.fromString("AF8B6E3F-3328-4C0A-AA36-5BA2BB9DBEF3"), 0.0, (byte) 2), // Dig speed
|
|
|
|
new Triple<>(UUID.fromString("55FCED67-E92A-486E-9800-B47F202C4386"), 0.0, (byte) 2) // Dig slow down
|
|
|
|
)));
|
|
|
|
|
|
|
|
wrapper.write(Type.INT, properties.size());
|
|
|
|
for (Map.Entry<String, Pair<Double, List<Triple<UUID, Double, Byte>>>> entry : properties.entrySet()) {
|
|
|
|
wrapper.write(Type.STRING, entry.getKey()); // Key
|
|
|
|
wrapper.write(Type.DOUBLE, entry.getValue().getKey()); // Value
|
|
|
|
wrapper.write(Type.VAR_INT, entry.getValue().getValue().size());
|
|
|
|
for (Triple<UUID, Double, Byte> modifier : entry.getValue().getValue()) {
|
|
|
|
wrapper.write(Type.UUID, modifier.getFirst());
|
|
|
|
wrapper.write(Type.DOUBLE, modifier.getSecond()); // Amount
|
|
|
|
wrapper.write(Type.BYTE, modifier.getThird()); // Operation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-02-26 14:45:55 +01:00
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerClientbound(ClientboundPackets1_8.ENTITY_ANIMATION, new PacketRemapper() {
|
2021-02-26 14:45:55 +01:00
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.VAR_INT); // 0 - Entity ID
|
|
|
|
map(Type.UNSIGNED_BYTE); // 1 - Animation
|
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
2021-04-02 14:15:30 +02:00
|
|
|
if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 3) {
|
2021-02-26 14:45:55 +01:00
|
|
|
wrapper.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-08-11 17:31:45 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-03-14 13:40:23 +01:00
|
|
|
/* Incoming Packets */
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerServerbound(ServerboundPackets1_9.ENTITY_ACTION, new PacketRemapper() {
|
2016-03-14 13:40:23 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.VAR_INT); // 0 - Player ID
|
|
|
|
map(Type.VAR_INT); // 1 - Action
|
|
|
|
map(Type.VAR_INT); // 2 - Jump
|
2016-03-14 17:51:56 +01:00
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
|
|
|
int action = wrapper.get(Type.VAR_INT, 1);
|
|
|
|
if (action == 6 || action == 8)
|
|
|
|
wrapper.cancel();
|
|
|
|
if (action == 7) {
|
|
|
|
wrapper.set(Type.VAR_INT, 1, 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-03-14 13:40:23 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-28 12:59:37 +02:00
|
|
|
protocol.registerServerbound(ServerboundPackets1_9.INTERACT_ENTITY, new PacketRemapper() {
|
2016-03-14 13:40:23 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerMap() {
|
|
|
|
map(Type.VAR_INT); // 0 - Entity ID (Target)
|
|
|
|
map(Type.VAR_INT); // 1 - Action Type
|
2016-09-25 12:19:23 +02:00
|
|
|
|
|
|
|
// Cancel second hand to prevent double interact
|
2016-03-14 13:40:23 +01:00
|
|
|
handler(new PacketHandler() {
|
|
|
|
@Override
|
2016-03-14 17:09:50 +01:00
|
|
|
public void handle(PacketWrapper wrapper) throws Exception {
|
2016-03-14 13:40:23 +01:00
|
|
|
int type = wrapper.get(Type.VAR_INT, 1);
|
|
|
|
if (type == 2) {
|
|
|
|
wrapper.passthrough(Type.FLOAT); // 2 - X
|
|
|
|
wrapper.passthrough(Type.FLOAT); // 3 - Y
|
|
|
|
wrapper.passthrough(Type.FLOAT); // 4 - Z
|
|
|
|
}
|
|
|
|
if (type == 0 || type == 2) {
|
2016-09-25 12:19:23 +02:00
|
|
|
int hand = wrapper.read(Type.VAR_INT); // 2/5 - Hand
|
|
|
|
|
|
|
|
if (hand == 1)
|
|
|
|
wrapper.cancel();
|
2016-03-14 13:40:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-03-13 18:01:36 +01:00
|
|
|
}
|
|
|
|
}
|