Geyser/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java

156 lines
5.4 KiB
Java
Raw Normal View History

2019-08-03 03:38:09 +00:00
/*
* Copyright (c) 2019-2020 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
*/
package org.geysermc.connector.network.session.cache;
import it.unimi.dsi.fastutil.longs.*;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
2019-08-03 03:38:09 +00:00
import lombok.Getter;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.PlayerEntity;
2019-08-03 03:38:09 +00:00
import org.geysermc.connector.network.session.GeyserSession;
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
private Long2ObjectMap<Entity> entities = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>());
private Long2LongMap entityIdTranslations = Long2LongMaps.synchronize(new Long2LongOpenHashMap());
private Map<UUID, PlayerEntity> playerEntities = Collections.synchronizedMap(new HashMap<>());
2020-04-18 13:48:21 +00:00
private Map<UUID, BossBar> bossBars = Collections.synchronizedMap(new HashMap<>());
Merge entity mounts branch to master (#589) * Initial support for entity mounts* * This only works for viewing other players on mounts/vehicles. Currently, mounting on vehicles through Geyser with bedrock does not work at all, though, you can see other Java players on mounts just fine. * Fix Bedrock player mounting; add minecart offset * Remove debug code * Fix boat animation * Remove debug code * Add notice of possible steering flip * Add translator for PlayerInputPacket * Upload WIP code for BoatEntity.java * Add animation for rowing on Bedrock side * Clean up debug code, start on boat movement * Add notice about flying horses * Rename BedrockPlayerInputPacket.java to BedrockPlayerInputTranslator.java * Delete BedrockPlayerInputPacket.java * Use Translator Annotation again; Thanks to LegacyGamerHD * Upload ineffective mount-on-login code * Upload current changes with no debug code * Change case where applicable * Change Integer[] to int[]; Change schedule() to execute() * Don't use Thread.Sleep() and instead call itself again * Fix players not being linked on login/chunk load * Little changes * Minor improvements/fixes to boats * Remove empty file * Fix horse flying. * Various entity mounting fixes * Add mounting offsets for skeleton and zombie horses * Another round of entity mount-related fixes - Add offsets for skeleton and zombie horses (Thanks to tester DirtNasty) - Boats can now be placed in survival (Thanks again to tester DirtNasty) - Boats and minecarts can now shake * Add translating for ServerVehicleMovePacket * Cleaning up * More cleaning up * Add interactive tag support for mountable entities * Boats move far more nicely * Add horse heart visuals * Update interactive tags Co-authored-by: RednedEpic <redned235@gmail.com>
2020-05-23 21:39:17 +00:00
private Long2LongMap cachedPlayerEntityLinks = Long2LongMaps.synchronize(new Long2LongOpenHashMap());
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) {
if (cacheEntity(entity)) {
entity.spawnEntity(session);
}
}
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
}
public boolean removeEntity(Entity entity, boolean force) {
if (entity != null && entity.isValid() && (force || entity.despawnEntity(session))) {
long geyserId = entityIdTranslations.remove(entity.getEntityId());
entities.remove(geyserId);
return true;
}
return false;
2019-08-03 06:51:05 +00:00
}
2019-08-03 03:38:09 +00:00
public void removeAllEntities() {
List<Entity> entities = new ArrayList<>(session.getEntityCache().getEntities().values());
for (Entity entity : entities) {
session.getEntityCache().removeEntity(entity, false);
}
}
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
public <T extends Entity> Set<T> getEntitiesByType(Class<T> entityType) {
Set<T> entitiesOfType = new ObjectOpenHashSet<>();
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);
}
2020-04-18 13:48:21 +00:00
public void addBossBar(UUID uuid, BossBar bossBar) {
bossBars.put(uuid, bossBar);
bossBar.addBossBar();
}
2020-04-18 13:48:21 +00:00
public BossBar getBossBar(UUID uuid) {
return bossBars.get(uuid);
}
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);
}
public void clear() {
entities = null;
entityIdTranslations = null;
playerEntities = null;
2020-04-18 13:48:21 +00:00
bossBars = null;
}
Merge entity mounts branch to master (#589) * Initial support for entity mounts* * This only works for viewing other players on mounts/vehicles. Currently, mounting on vehicles through Geyser with bedrock does not work at all, though, you can see other Java players on mounts just fine. * Fix Bedrock player mounting; add minecart offset * Remove debug code * Fix boat animation * Remove debug code * Add notice of possible steering flip * Add translator for PlayerInputPacket * Upload WIP code for BoatEntity.java * Add animation for rowing on Bedrock side * Clean up debug code, start on boat movement * Add notice about flying horses * Rename BedrockPlayerInputPacket.java to BedrockPlayerInputTranslator.java * Delete BedrockPlayerInputPacket.java * Use Translator Annotation again; Thanks to LegacyGamerHD * Upload ineffective mount-on-login code * Upload current changes with no debug code * Change case where applicable * Change Integer[] to int[]; Change schedule() to execute() * Don't use Thread.Sleep() and instead call itself again * Fix players not being linked on login/chunk load * Little changes * Minor improvements/fixes to boats * Remove empty file * Fix horse flying. * Various entity mounting fixes * Add mounting offsets for skeleton and zombie horses * Another round of entity mount-related fixes - Add offsets for skeleton and zombie horses (Thanks to tester DirtNasty) - Boats can now be placed in survival (Thanks again to tester DirtNasty) - Boats and minecarts can now shake * Add translating for ServerVehicleMovePacket * Cleaning up * More cleaning up * Add interactive tag support for mountable entities * Boats move far more nicely * Add horse heart visuals * Update interactive tags Co-authored-by: RednedEpic <redned235@gmail.com>
2020-05-23 21:39:17 +00:00
public long getCachedPlayerEntityLink(long playerId) {
return cachedPlayerEntityLinks.getOrDefault(playerId, -1);
}
public void addCachedPlayerEntityLink(long playerId, long linkedEntityId) {
cachedPlayerEntityLinks.put(playerId, linkedEntityId);
}
2019-08-03 03:38:09 +00:00
}