ViaVersion/common/src/main/java/com/viaversion/viaversion/data/entity/EntityTrackerBase.java

134 lines
4.1 KiB
Java
Raw Normal View History

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
/*
* 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/>.
*/
package com.viaversion.viaversion.data.entity;
import com.google.common.base.Preconditions;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.entity.EntityTracker;
import com.viaversion.viaversion.api.data.entity.StoredEntityData;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class EntityTrackerBase implements EntityTracker {
private final Map<Integer, EntityType> entityTypes = new ConcurrentHashMap<>();
private final Map<Integer, StoredEntityData> entityData;
private final UserConnection connection;
private final EntityType playerType;
private int clientEntityId = -1;
private int currentWorldSectionHeight = 16;
private int currentMinY;
public EntityTrackerBase(UserConnection connection, @Nullable EntityType playerType) {
this(connection, playerType, false);
}
public EntityTrackerBase(UserConnection connection, @Nullable EntityType playerType, boolean storesEntityData) {
this.connection = connection;
this.playerType = playerType;
this.entityData = storesEntityData ? new ConcurrentHashMap<>() : null;
}
@Override
public UserConnection user() {
return connection;
}
@Override
public void addEntity(int id, EntityType type) {
entityTypes.put(id, type);
}
@Override
public boolean hasEntity(int id) {
return entityTypes.containsKey(id);
}
@Override
public @Nullable EntityType entityType(int id) {
return entityTypes.get(id);
}
@Override
public StoredEntityData entityData(int id) {
EntityType type = entityType(id);
return entityData.computeIfAbsent(id, s -> new StoredEntityImpl(type));
}
@Override
public @Nullable StoredEntityData entityDataIfPresent(int id) {
return entityData.get(id);
}
@Override
public void removeEntity(int id) {
entityTypes.remove(id);
if (entityData != null) {
entityData.remove(id);
}
}
@Override
public int clientEntityId() {
return clientEntityId;
}
@Override
public void setClientEntityId(int clientEntityId) {
Preconditions.checkNotNull(playerType);
entityTypes.put(clientEntityId, playerType);
if (entityData != null) {
StoredEntityData data = entityData.remove(this.clientEntityId);
if (data != null) {
entityData.put(clientEntityId, data);
}
}
this.clientEntityId = clientEntityId;
}
/**
* @return amount of chunk sections of the current world (block height / 16)
*/
@Override
public int currentWorldSectionHeight() {
return currentWorldSectionHeight;
}
@Override
public void setCurrentWorldSectionHeight(int currentWorldSectionHeight) {
this.currentWorldSectionHeight = currentWorldSectionHeight;
}
/**
* @return absolute minimum y coordinate of the current world
*/
@Override
public int currentMinY() {
return currentMinY;
}
@Override
public void setCurrentMinY(int currentMinY) {
this.currentMinY = currentMinY;
}
}