2019-08-03 03:38:09 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2019-08-03 03:38:09 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.session.cache;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2021-10-05 21:06:15 +00:00
|
|
|
import it.unimi.dsi.fastutil.longs.Long2LongMap;
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
2021-08-17 00:39:29 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
2019-08-03 03:38:09 +00:00
|
|
|
import lombok.Getter;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.entity.type.Entity;
|
|
|
|
import org.geysermc.geyser.entity.type.Tickable;
|
|
|
|
import org.geysermc.geyser.entity.type.player.PlayerEntity;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
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 {
|
2021-01-05 23:41:20 +00:00
|
|
|
private final GeyserSession session;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
|
|
|
@Getter
|
2021-08-17 00:39:29 +00:00
|
|
|
private final Long2ObjectMap<Entity> entities = new Long2ObjectOpenHashMap<>();
|
2021-01-05 23:41:20 +00:00
|
|
|
/**
|
|
|
|
* A list of all entities that must be ticked.
|
|
|
|
*/
|
2021-08-17 00:39:29 +00:00
|
|
|
private final List<Tickable> tickableEntities = new ObjectArrayList<>();
|
|
|
|
private final Long2LongMap entityIdTranslations = new Long2LongOpenHashMap();
|
|
|
|
private final Map<UUID, PlayerEntity> playerEntities = new Object2ObjectOpenHashMap<>();
|
|
|
|
private final Map<UUID, BossBar> bossBars = new Object2ObjectOpenHashMap<>();
|
2019-08-03 06:51:05 +00:00
|
|
|
|
|
|
|
@Getter
|
2021-01-05 23:41:20 +00:00
|
|
|
private final AtomicLong nextEntityId = new AtomicLong(2L);
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public EntityCache(GeyserSession session) {
|
2019-08-03 03:38:09 +00:00
|
|
|
this.session = session;
|
|
|
|
}
|
|
|
|
|
2019-08-03 06:51:05 +00:00
|
|
|
public void spawnEntity(Entity entity) {
|
2020-04-26 04:54:42 +00:00
|
|
|
if (cacheEntity(entity)) {
|
2021-11-18 03:02:38 +00:00
|
|
|
entity.spawnEntity();
|
2021-01-05 23:41:20 +00:00
|
|
|
|
|
|
|
if (entity instanceof Tickable) {
|
|
|
|
// Start ticking it
|
|
|
|
tickableEntities.add((Tickable) entity);
|
|
|
|
}
|
2020-04-26 04:54:42 +00:00
|
|
|
}
|
2020-02-14 02:04:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 04:54:42 +00:00
|
|
|
public boolean cacheEntity(Entity entity) {
|
|
|
|
// Check to see if the entity exists, otherwise we can end up with duplicated mobs
|
|
|
|
if (!entityIdTranslations.containsKey(entity.getEntityId())) {
|
|
|
|
entityIdTranslations.put(entity.getEntityId(), entity.getGeyserId());
|
|
|
|
entities.put(entity.getGeyserId(), entity);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-08-03 06:51:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 20:45:29 +00:00
|
|
|
public boolean removeEntity(Entity entity, boolean force) {
|
2021-11-22 19:49:55 +00:00
|
|
|
if (entity instanceof PlayerEntity player) {
|
|
|
|
session.getPlayerWithCustomHeads().remove(player.getUuid());
|
|
|
|
}
|
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
if (entity != null && entity.isValid() && (force || entity.despawnEntity())) {
|
2019-11-30 19:26:51 +00:00
|
|
|
long geyserId = entityIdTranslations.remove(entity.getEntityId());
|
|
|
|
entities.remove(geyserId);
|
2021-01-05 23:41:20 +00:00
|
|
|
|
|
|
|
if (entity instanceof Tickable) {
|
|
|
|
tickableEntities.remove(entity);
|
|
|
|
}
|
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-12-28 13:35:21 +00:00
|
|
|
public void removeAllEntities() {
|
2021-11-18 03:02:38 +00:00
|
|
|
List<Entity> entities = new ArrayList<>(this.entities.values());
|
2019-12-28 13:35:21 +00:00
|
|
|
for (Entity entity : entities) {
|
2021-11-18 03:02:38 +00:00
|
|
|
removeEntity(entity, false);
|
2019-12-28 13:35:21 +00:00
|
|
|
}
|
2021-03-24 15:09:24 +00:00
|
|
|
|
2021-11-22 19:49:55 +00:00
|
|
|
session.getPlayerWithCustomHeads().clear();
|
2019-12-28 13:35:21 +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) {
|
2021-12-18 16:43:57 +00:00
|
|
|
if (javaId == session.getPlayerEntity().getEntityId()) {
|
|
|
|
return session.getPlayerEntity();
|
|
|
|
}
|
2019-08-03 06:51:05 +00:00
|
|
|
return entities.get(entityIdTranslations.get(javaId));
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2021-01-22 00:03:46 +00:00
|
|
|
public PlayerEntity removePlayerEntity(UUID uuid) {
|
|
|
|
return playerEntities.remove(uuid);
|
2019-09-16 22:45:39 +00:00
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2021-10-01 20:24:25 +00:00
|
|
|
public Collection<PlayerEntity> getAllPlayerEntities() {
|
|
|
|
return playerEntities.values();
|
|
|
|
}
|
|
|
|
|
2020-04-18 13:48:21 +00:00
|
|
|
public void addBossBar(UUID uuid, BossBar bossBar) {
|
|
|
|
bossBars.put(uuid, bossBar);
|
|
|
|
bossBar.addBossBar();
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 13:48:21 +00:00
|
|
|
public BossBar getBossBar(UUID uuid) {
|
|
|
|
return bossBars.get(uuid);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 13:48:21 +00:00
|
|
|
public void removeBossBar(UUID uuid) {
|
|
|
|
BossBar bossBar = bossBars.remove(uuid);
|
|
|
|
if (bossBar != null) {
|
|
|
|
bossBar.removeBossBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateBossBars() {
|
|
|
|
bossBars.values().forEach(BossBar::updateBossBar);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
2019-10-16 18:55:05 +00:00
|
|
|
|
2021-01-05 23:41:20 +00:00
|
|
|
public List<Tickable> getTickableEntities() {
|
|
|
|
return tickableEntities;
|
|
|
|
}
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|