ViaVersion/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/metadata/MetadataRewriter1_11To1_10....

234 lines
9.8 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_11to1_10.metadata;
import com.viaversion.viaversion.api.Via;
2023-10-19 01:28:12 +02:00
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_11;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_11.EntityType;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_11to1_10.EntityIdRewriter;
import com.viaversion.viaversion.protocols.protocol1_11to1_10.Protocol1_11To1_10;
import com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
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;
2019-05-09 14:43:48 +02:00
import java.util.List;
import java.util.Optional;
2023-11-20 19:56:48 +01:00
import java.util.logging.Level;
2019-05-09 14:43:48 +02:00
public class MetadataRewriter1_11To1_10 extends EntityRewriter<ClientboundPackets1_9_3, Protocol1_11To1_10> {
public MetadataRewriter1_11To1_10(Protocol1_11To1_10 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
2019-05-09 14:46:24 +02:00
@Override
protected void registerRewrites() {
filter().handler((event, meta) -> {
if (meta.getValue() instanceof DataItem) {
// Apply rewrite
EntityIdRewriter.toClientItem(meta.value());
2019-05-09 14:43:48 +02:00
}
});
2019-05-09 14:43:48 +02:00
filter().type(EntityType.GUARDIAN).index(12).handler((event, meta) -> {
boolean value = (((byte) meta.getValue()) & 0x02) == 0x02;
meta.setTypeAndValue(MetaType1_9.Boolean, value);
});
filter().type(EntityType.ABSTRACT_SKELETON).removeIndex(12);
filter().type(EntityType.ZOMBIE).handler((event, meta) -> {
if ((event.entityType() == EntityType.ZOMBIE || event.entityType() == EntityType.HUSK) && meta.id() == 14) {
event.cancel();
} else if (meta.id() == 15) {
meta.setId(14);
} else if (meta.id() == 14) {
meta.setId(15);
2019-05-09 14:43:48 +02:00
}
});
2019-05-09 14:43:48 +02:00
filter().type(EntityType.ABSTRACT_HORSE).handler((event, metadata) -> {
final com.viaversion.viaversion.api.minecraft.entities.EntityType type = event.entityType();
int id = metadata.id();
if (id == 14) { // Type
event.cancel();
return;
2019-05-09 14:43:48 +02:00
}
if (id == 16) { // Owner
2019-05-09 14:43:48 +02:00
metadata.setId(14);
} else if (id == 17) { // Armor
2019-05-09 14:43:48 +02:00
metadata.setId(16);
}
// Process per type
if (!type.is(EntityType.HORSE) && metadata.id() == 15 || metadata.id() == 16) {
event.cancel();
return;
2019-05-09 14:43:48 +02:00
}
if ((type == EntityType.DONKEY || type == EntityType.MULE) && metadata.id() == 13) {
if ((((byte) metadata.getValue()) & 0x08) == 0x08) {
event.createExtraMeta(new Metadata(15, MetaType1_9.Boolean, true));
} else {
event.createExtraMeta(new Metadata(15, MetaType1_9.Boolean, false));
2019-05-09 14:43:48 +02:00
}
}
});
filter().type(EntityType.ARMOR_STAND).index(0).handler((event, meta) -> {
if (!Via.getConfig().isHologramPatch()) {
return;
}
Metadata flags = event.metaAtIndex(11);
Metadata customName = event.metaAtIndex(2);
Metadata customNameVisible = event.metaAtIndex(3);
if (flags == null || customName == null || customNameVisible == null) {
return;
}
byte data = meta.value();
// Check invisible | Check small | Check if custom name is empty | Check if custom name visible is true
if ((data & 0x20) == 0x20 && ((byte) flags.getValue() & 0x01) == 0x01
&& !((String) customName.getValue()).isEmpty() && (boolean) customNameVisible.getValue()) {
EntityTracker1_11 tracker = tracker(event.user());
int entityId = event.entityId();
if (tracker.addHologram(entityId)) {
try {
// Send movement
PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9_3.ENTITY_POSITION, null, event.user());
wrapper.write(Type.VAR_INT, entityId);
wrapper.write(Type.SHORT, (short) 0);
wrapper.write(Type.SHORT, (short) (128D * (-Via.getConfig().getHologramYOffset() * 32D)));
wrapper.write(Type.SHORT, (short) 0);
wrapper.write(Type.BOOLEAN, true);
wrapper.send(Protocol1_11To1_10.class);
} catch (Exception e) {
Via.getPlatform().getLogger().log(Level.WARNING, "Failed to update hologram position", e);
2019-05-09 14:43:48 +02:00
}
}
}
});
2019-05-09 14:43:48 +02:00
}
@Override
public com.viaversion.viaversion.api.minecraft.entities.EntityType typeFromId(int type) {
2023-10-19 01:28:12 +02:00
return EntityTypes1_11.getTypeFromId(type, false);
}
@Override
public com.viaversion.viaversion.api.minecraft.entities.EntityType objectTypeFromId(int type) {
2023-10-19 01:28:12 +02:00
return EntityTypes1_11.getTypeFromId(type, true);
}
2019-05-09 14:43:48 +02:00
public static EntityType rewriteEntityType(int numType, List<Metadata> metadata) {
Optional<EntityType> optType = EntityType.findById(numType);
if (!optType.isPresent()) {
Via.getManager().getPlatform().getLogger().severe("Error: could not find Entity type " + numType + " with metadata: " + metadata);
return null;
}
EntityType type = optType.get();
try {
if (type.is(EntityType.GUARDIAN)) {
// ElderGuardian - 4
Optional<Metadata> options = getById(metadata, 12);
if (options.isPresent()) {
if ((((byte) options.get().getValue()) & 0x04) == 0x04) {
return EntityType.ELDER_GUARDIAN;
}
}
}
if (type.is(EntityType.SKELETON)) {
// WitherSkeleton - 5
// Stray - 6
Optional<Metadata> options = getById(metadata, 12);
if (options.isPresent()) {
if (((int) options.get().getValue()) == 1) {
return EntityType.WITHER_SKELETON;
}
if (((int) options.get().getValue()) == 2) {
return EntityType.STRAY;
}
}
}
if (type.is(EntityType.ZOMBIE)) {
// ZombieVillager - 27
// Husk - 23
Optional<Metadata> options = getById(metadata, 13);
if (options.isPresent()) {
int value = (int) options.get().getValue();
if (value > 0 && value < 6) {
metadata.add(new Metadata(16, MetaType1_9.VarInt, value - 1)); // Add profession type to new metadata
return EntityType.ZOMBIE_VILLAGER;
}
if (value == 6) {
return EntityType.HUSK;
}
}
}
if (type.is(EntityType.HORSE)) {
// SkeletonHorse - 28
// ZombieHorse - 29
// Donkey - 31
// Mule - 32
Optional<Metadata> options = getById(metadata, 14);
if (options.isPresent()) {
if (((int) options.get().getValue()) == 0) {
return EntityType.HORSE;
}
if (((int) options.get().getValue()) == 1) {
return EntityType.DONKEY;
}
if (((int) options.get().getValue()) == 2) {
return EntityType.MULE;
}
if (((int) options.get().getValue()) == 3) {
return EntityType.ZOMBIE_HORSE;
}
if (((int) options.get().getValue()) == 4) {
return EntityType.SKELETON_HORSE;
}
}
}
} catch (Exception e) {
if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("An error occurred with entity type rewriter");
Via.getPlatform().getLogger().warning("Metadata: " + metadata);
2023-11-20 19:56:48 +01:00
Via.getPlatform().getLogger().log(Level.WARNING, "Error: ", e);
2019-05-09 14:43:48 +02:00
}
}
return type;
}
public static Optional<Metadata> getById(List<Metadata> metadatas, int id) {
for (Metadata metadata : metadatas) {
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
if (metadata.id() == id) return Optional.of(metadata);
2019-05-09 14:43:48 +02:00
}
return Optional.empty();
2019-05-09 14:43:48 +02:00
}
}