ViaVersion/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/metadata/MetadataRewriter1_9To1_8.java

152 lines
5.9 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_9to1_8.metadata;
2019-05-09 14:43:48 +02:00
import com.viaversion.viaversion.api.minecraft.EulerAngle;
import com.viaversion.viaversion.api.minecraft.Vector;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.MetaType;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_8;
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ItemRewriter;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
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.rewriter.EntityRewriter;
import com.viaversion.viaversion.rewriter.meta.MetaHandlerEvent;
2019-05-09 14:43:48 +02:00
import java.util.UUID;
public class MetadataRewriter1_9To1_8 extends EntityRewriter<ClientboundPackets1_8, Protocol1_9To1_8> {
public MetadataRewriter1_9To1_8(Protocol1_9To1_8 protocol) {
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
super(protocol);
}
2019-05-09 14:43:48 +02:00
@Override
protected void registerRewrites() {
filter().handler(this::handleMetadata);
}
private void handleMetadata(MetaHandlerEvent event, Metadata metadata) {
EntityType type = event.entityType();
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
MetaIndex metaIndex = MetaIndex.searchIndex(type, metadata.id());
2019-05-09 14:43:48 +02:00
if (metaIndex == null) {
// Almost certainly bad data, remove it
event.cancel();
return;
2019-05-09 14:43:48 +02:00
}
if (metaIndex.getNewType() == null) {
event.cancel();
2019-05-09 14:43:48 +02:00
return;
}
metadata.setId(metaIndex.getNewIndex());
metadata.setMetaTypeUnsafe(metaIndex.getNewType());
2019-05-09 14:43:48 +02:00
Object value = metadata.getValue();
switch (metaIndex.getNewType()) {
case Byte:
// convert from int, byte
if (metaIndex.getOldType() == MetaType1_8.Byte) {
metadata.setValue(value);
}
if (metaIndex.getOldType() == MetaType1_8.Int) {
metadata.setValue(((Integer) value).byteValue());
}
// After writing the last one
2023-10-19 01:28:12 +02:00
if (metaIndex == MetaIndex.ENTITY_STATUS && type == EntityTypes1_10.EntityType.PLAYER) {
2024-02-10 17:58:54 +01:00
byte val = 0;
2019-05-09 14:43:48 +02:00
if ((((Byte) value) & 0x10) == 0x10) { // Player eating/aiming/drinking
val = 1;
}
int newIndex = MetaIndex.PLAYER_HAND.getNewIndex();
MetaType metaType = MetaIndex.PLAYER_HAND.getNewType();
event.createExtraMeta(new Metadata(newIndex, metaType, val));
2019-05-09 14:43:48 +02:00
}
break;
case OptUUID:
String owner = (String) value;
UUID toWrite = null;
if (!owner.isEmpty()) {
try {
toWrite = UUID.fromString(owner);
} catch (Exception ignored) {
}
2019-05-09 14:43:48 +02:00
}
metadata.setValue(toWrite);
break;
case VarInt:
// convert from int, short, byte
if (metaIndex.getOldType() == MetaType1_8.Byte) {
metadata.setValue(((Byte) value).intValue());
}
if (metaIndex.getOldType() == MetaType1_8.Short) {
metadata.setValue(((Short) value).intValue());
}
if (metaIndex.getOldType() == MetaType1_8.Int) {
metadata.setValue(value);
}
break;
case Float:
case String:
metadata.setValue(value);
break;
case Boolean:
if (metaIndex == MetaIndex.AGEABLE_AGE)
metadata.setValue((Byte) value < 0);
else
metadata.setValue((Byte) value != 0);
break;
case Slot:
metadata.setValue(value);
ItemRewriter.toClient((Item) metadata.getValue());
break;
case Position:
Vector vector = (Vector) value;
metadata.setValue(vector);
break;
case Vector3F:
EulerAngle angle = (EulerAngle) value;
metadata.setValue(angle);
break;
case Chat:
2020-08-03 08:39:39 +02:00
value = Protocol1_9To1_8.fixJson(value.toString());
2019-05-09 14:43:48 +02:00
metadata.setValue(value);
break;
case BlockID:
// Convert from int, short, byte
metadata.setValue(((Number) value).intValue());
break;
2019-05-09 14:43:48 +02:00
default:
throw new RuntimeException("Unhandled MetaDataType: " + metaIndex.getNewType());
2019-05-09 14:43:48 +02:00
}
}
@Override
public EntityType typeFromId(int type) {
2023-10-19 01:28:12 +02:00
return EntityTypes1_10.getTypeFromId(type, false);
}
@Override
public EntityType objectTypeFromId(int type) {
2023-10-19 01:28:12 +02:00
return EntityTypes1_10.getTypeFromId(type, true);
}
2019-05-09 14:43:48 +02:00
}