2019-08-03 03:38:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 GeyserMC. http://geysermc.org
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.network.session.cache;
|
|
|
|
|
2019-12-27 12:03:12 +00:00
|
|
|
import it.unimi.dsi.fastutil.longs.*;
|
2019-08-03 03:38:09 +00:00
|
|
|
import lombok.Getter;
|
|
|
|
import org.geysermc.connector.entity.Entity;
|
2019-09-16 22:28:29 +00:00
|
|
|
import org.geysermc.connector.entity.PlayerEntity;
|
2019-08-03 03:38:09 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
import java.util.*;
|
2019-08-03 03:38:09 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
2019-08-03 06:51:05 +00:00
|
|
|
/**
|
|
|
|
* Each session has its own EntityCache in the occasion that an entity packet is sent specifically
|
|
|
|
* for that player (e.g. seeing vanished players from /vanish)
|
|
|
|
*/
|
2019-08-03 03:38:09 +00:00
|
|
|
public class EntityCache {
|
|
|
|
private GeyserSession session;
|
|
|
|
|
|
|
|
@Getter
|
2019-12-27 12:03:12 +00:00
|
|
|
private Long2ObjectMap<Entity> entities = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>());
|
|
|
|
private Long2LongMap entityIdTranslations = Long2LongMaps.synchronize(new Long2LongOpenHashMap());
|
|
|
|
private Map<UUID, PlayerEntity> playerEntities = Collections.synchronizedMap(new HashMap<>());
|
2019-09-25 21:52:28 +00:00
|
|
|
private Map<UUID, Long> bossbars = new HashMap<>();
|
2019-08-03 06:51:05 +00:00
|
|
|
|
|
|
|
@Getter
|
2019-08-03 03:38:09 +00:00
|
|
|
private AtomicLong nextEntityId = new AtomicLong(2L);
|
|
|
|
|
|
|
|
public EntityCache(GeyserSession session) {
|
|
|
|
this.session = session;
|
|
|
|
}
|
|
|
|
|
2019-08-03 06:51:05 +00:00
|
|
|
public void spawnEntity(Entity entity) {
|
|
|
|
entity.moveAbsolute(entity.getPosition(), entity.getRotation().getX(), entity.getRotation().getY());
|
|
|
|
entityIdTranslations.put(entity.getEntityId(), entity.getGeyserId());
|
|
|
|
entities.put(entity.getGeyserId(), entity);
|
|
|
|
entity.spawnEntity(session);
|
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
public boolean removeEntity(Entity entity, boolean force) {
|
|
|
|
if (entity != null && entity.isValid() && (force || entity.despawnEntity(session))) {
|
2019-11-30 19:26:51 +00:00
|
|
|
long geyserId = entityIdTranslations.remove(entity.getEntityId());
|
|
|
|
entities.remove(geyserId);
|
|
|
|
if (entity.is(PlayerEntity.class)) {
|
|
|
|
playerEntities.remove(entity.as(PlayerEntity.class).getUuid());
|
2019-09-21 07:42:44 +00:00
|
|
|
}
|
2019-10-02 20:45:29 +00:00
|
|
|
return true;
|
2019-09-21 07:42:44 +00:00
|
|
|
}
|
2019-10-02 20:45:29 +00:00
|
|
|
return false;
|
2019-08-03 06:51:05 +00:00
|
|
|
}
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2019-08-03 06:51:05 +00:00
|
|
|
public Entity getEntityByGeyserId(long geyserId) {
|
|
|
|
return entities.get(geyserId);
|
|
|
|
}
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2019-08-03 06:51:05 +00:00
|
|
|
public Entity getEntityByJavaId(long javaId) {
|
|
|
|
return entities.get(entityIdTranslations.get(javaId));
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
2019-09-16 22:45:39 +00:00
|
|
|
|
2019-10-09 18:39:38 +00:00
|
|
|
public <T extends Entity> Set<T> getEntitiesByType(Class<T> entityType) {
|
|
|
|
Set<T> entitiesOfType = new HashSet<>();
|
|
|
|
for (Entity entity : (entityType == PlayerEntity.class ? playerEntities : entities).values()) {
|
|
|
|
if (entity.is(entityType)) {
|
|
|
|
entitiesOfType.add(entity.as(entityType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return entitiesOfType;
|
|
|
|
}
|
|
|
|
|
2019-09-16 22:45:39 +00:00
|
|
|
public void addPlayerEntity(PlayerEntity entity) {
|
|
|
|
playerEntities.put(entity.getUuid(), entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public PlayerEntity getPlayerEntity(UUID uuid) {
|
|
|
|
return playerEntities.get(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removePlayerEntity(UUID uuid) {
|
|
|
|
playerEntities.remove(uuid);
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
|
|
|
|
public long addBossBar(UUID uuid) {
|
|
|
|
long entityId = getNextEntityId().incrementAndGet();
|
|
|
|
bossbars.put(uuid, entityId);
|
|
|
|
return entityId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getBossBar(UUID uuid) {
|
|
|
|
return bossbars.containsKey(uuid) ? bossbars.get(uuid) : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long removeBossBar(UUID uuid) {
|
|
|
|
return bossbars.remove(uuid);
|
|
|
|
}
|
2019-10-16 18:55:05 +00:00
|
|
|
|
|
|
|
public void clear() {
|
|
|
|
entities = null;
|
|
|
|
entityIdTranslations = null;
|
|
|
|
playerEntities = null;
|
|
|
|
bossbars = null;
|
|
|
|
}
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|