ViaVersion/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java

130 lines
4.0 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
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.viaversion.viaversion.api.data.entity;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import org.checkerframework.checker.nullness.qual.Nullable;
public interface EntityTracker extends ClientEntityIdChangeListener {
/**
* User connection the tracker belongs to.
*
* @return user connection
*/
UserConnection user();
/**
* Tracks an entity.
*
* @param id entity id
* @param type entity type
*/
void addEntity(int id, EntityType type);
/**
* Returns whether the entity is currently tracked.
*
* @param id entity id
* @return whether the entity is tracked
*/
boolean hasEntity(int id);
/**
* Entity type of the entity if tracked.
* This returning null does not necessarily mean no entity by the id exists.
*
* @param id entity id
* @return entity type of the entity if tracked
*/
@Nullable EntityType entityType(int id);
/**
* Untracks an entity.
*
* @param id entity id
*/
void removeEntity(int id);
/**
* Returns the stored entity data if an entity with the id is tracked, else null.
* If no data has been initialized yet, it will be done and returned by this method.
*
* @param id entity id
* @return stored entity data if an entity with the id is tracked, else null
*/
@Nullable StoredEntityData entityData(int id);
/**
* Returns stored entity data if it has previously been initialized by {@link #entityData(int)}, else null.
*
* @param id entity id
* @return stored entity data if it has previously been initialized by {@link #entityData(int)}
*/
@Nullable StoredEntityData entityDataIfPresent(int id);
/**
* Returns the client entity id or -1 if unset.
*
* @return client entity id or -1 if unset
*/
int clientEntityId();
/**
* Sets the client entity id.
*
* @param clientEntityId client entity id
*/
void setClientEntityId(int clientEntityId);
/**
* Returns the current world section height.
* Always 16 for sub 1.17 worlds.
*
* @return current world section height
*/
int currentWorldSectionHeight();
/**
* Sets the current world section height.
*
* @param currentWorldSectionHeight world section height
*/
void setCurrentWorldSectionHeight(int currentWorldSectionHeight);
/**
* Returns the minimum y of the current player world.
*
* @return minimum y of the current world
*/
int currentMinY();
/**
* Sets the minimum y of the current player world.
*
* @param currentMinY minimum y of the current world
*/
void setCurrentMinY(int currentMinY);
}