Skip to content

Commit

Permalink
Fixed: Accessing entity state of owning region's thread in #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram1903 committed Mar 25, 2024
1 parent ee44040 commit 4a5b880
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 16 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'com.deathmotion.antihealthindicator'
version '1.1.2'
version '1.1.3-SNAPSHOT'

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down Expand Up @@ -46,7 +46,7 @@ tasks {

runServer {
// The version of the server to run
def version = "1.20.4"
def version = "1.8.8"

minecraftVersion(version)
runDirectory.set(file("server/$version"))
Expand All @@ -55,7 +55,7 @@ tasks {
// 1.17 = Java 16
// 1.18 - 1.20.4 = Java 17
javaLauncher.set(project.javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(17))
languageVersion.set(JavaLanguageVersion.of(8))
})

downloadPlugins {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.deathmotion.antihealthindicator.data;

import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

@Getter
@Setter
public class WolfData {
@Nullable
UUID ownerUniqueId;

boolean isTamed;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.deathmotion.antihealthindicator.events;

import com.deathmotion.antihealthindicator.AntiHealthIndicator;
import com.deathmotion.antihealthindicator.data.WolfData;
import com.deathmotion.antihealthindicator.managers.CacheManager;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.entity.EntityTameEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.world.EntitiesLoadEvent;
Expand All @@ -34,7 +34,10 @@ public EntityState(AntiHealthIndicator plugin) {
*/
@EventHandler(priority = EventPriority.MONITOR)
public void entitySpawn(EntitySpawnEvent event) {
createEntityDataMap(event.getEntity());
Entity entity = event.getEntity();

createEntityDataMap(entity);
createWolfDataMap(entity);
}

/**
Expand All @@ -46,6 +49,7 @@ public void entitySpawn(EntitySpawnEvent event) {
public void entityLoad(EntitiesLoadEvent event) {
for (Entity entity : event.getEntities()) {
createEntityDataMap(entity);
createWolfDataMap(entity);
}
}

Expand All @@ -59,18 +63,41 @@ public void playerJoin(PlayerJoinEvent event) {
createEntityDataMap(event.getPlayer());
}

/**
* Handles the EntityTameEvent, creating a new instance of WolfData for the tamed wolf.
*
* @param event the event
*/
@EventHandler(priority = EventPriority.MONITOR)
public void animalTamed(EntityTameEvent event) {
Entity entity = event.getEntity();

if (entity instanceof Wolf) {
AnimalTamer animalTamer = event.getOwner();

WolfData wolfData = new WolfData();
wolfData.setTamed(true);
wolfData.setOwnerUniqueId(animalTamer.getUniqueId());

this.cacheManager.updateWolfDataInCache(entity.getEntityId(), wolfData);
}
}

/**
* Handles the EntityDeathEvent, removing the died entity from the entityDataMap.
*
* @param event the event
*/
@EventHandler(priority = EventPriority.MONITOR)
public void entityDeath(EntityDeathEvent event) {
if (event.getEntity() instanceof Player) {
Entity entity = event.getEntity();

if (entity instanceof Player) {
return;
}

cacheManager.removeEntityFromCache(event.getEntity().getEntityId());
removeWolfDataMap(entity);
}

/**
Expand All @@ -81,9 +108,11 @@ public void entityDeath(EntityDeathEvent event) {
@EventHandler(priority = EventPriority.MONITOR)
public void entityUnload(EntitiesUnloadEvent event) {
List<Entity> entityList = event.getEntities();

for (Entity entity : entityList) {
if (entity instanceof LivingEntity) {
cacheManager.removeEntityFromCache(entity.getEntityId());
removeWolfDataMap(entity);
}
}
}
Expand All @@ -110,4 +139,30 @@ private void createEntityDataMap(Entity entity) {

this.cacheManager.addEntityToCache(entity);
}

/**
* Create a map of WolfData instances.
*
* @param entity the entity for which the map to be created
*/
private void createWolfDataMap(Entity entity) {
if (!(entity instanceof Wolf)) {
return;
}

this.cacheManager.addWolfDataToCache(entity.getEntityId(), (Wolf) entity);
}

/**
* Remove a map of WolfData instances.
*
* @param entity the entity for which the map to be removed
*/
private void removeWolfDataMap(Entity entity) {
if (!(entity instanceof Wolf)) {
return;
}

this.cacheManager.removeWolfDataFromCache(entity.getEntityId());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.deathmotion.antihealthindicator.managers;

import com.deathmotion.antihealthindicator.AntiHealthIndicator;
import com.deathmotion.antihealthindicator.data.WolfData;
import io.github.retrooper.packetevents.util.FoliaCompatUtil;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Wolf;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -16,8 +18,9 @@ public class CacheManager {

private final AntiHealthIndicator plugin;

private final ConcurrentHashMap<Integer, Entity> entityDataMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Integer, Entity> entityData = new ConcurrentHashMap<>();
private final ConcurrentHashMap<UUID, Integer> vehicles = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Integer, WolfData> wolfData = new ConcurrentHashMap<>();

public CacheManager(AntiHealthIndicator plugin) {
this.plugin = plugin;
Expand All @@ -27,22 +30,42 @@ public void cacheLivingEntityData() {
FoliaCompatUtil.runTask(this.plugin, (Object unused) -> {
for (World world : Bukkit.getWorlds()) {
for (LivingEntity livingEntity : world.getLivingEntities()) {
entityDataMap.putIfAbsent(livingEntity.getEntityId(), livingEntity);
entityData.putIfAbsent(livingEntity.getEntityId(), livingEntity);

if (livingEntity instanceof Wolf) {
addWolfDataToCache(livingEntity.getEntityId(), (Wolf) livingEntity);
}
}
}
});
}

public void addEntityToCache(Entity entity) {
entityDataMap.putIfAbsent(entity.getEntityId(), entity);
entityData.putIfAbsent(entity.getEntityId(), entity);
}

public void addWolfDataToCache(Integer entityId, Wolf wolf) {
wolfData.computeIfAbsent(entityId, key -> createWolfData(wolf));
}

public Entity getEntityFromCache(int entityId) {
return entityDataMap.get(entityId);
return entityData.get(entityId);
}

public WolfData getWolfDataFromCache(int entityId) {
return wolfData.get(entityId);
}

public void updateWolfDataInCache(int entityId, WolfData wolfData) {
this.wolfData.put(entityId, wolfData);
}

public void removeEntityFromCache(int entityId) {
entityDataMap.remove(entityId);
entityData.remove(entityId);
}

public void removeWolfDataFromCache(int entityId) {
wolfData.remove(entityId);
}

public void addVehicleToCache(UUID playerUniqueId, Integer entityId) {
Expand All @@ -56,4 +79,16 @@ public boolean isPlayerVehicleInCache(UUID playerUniqueId, Integer vehicleId) {
public void removeVehicle(UUID playerUniqueId) {
vehicles.remove(playerUniqueId);
}

private WolfData createWolfData(Wolf wolf) {
WolfData wolfData = new WolfData();

wolfData.setTamed(wolf.isTamed());

if (wolf.isTamed() && wolf.getOwner() != null) {
wolfData.setOwnerUniqueId(wolf.getOwner().getUniqueId());
}

return wolfData;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.deathmotion.antihealthindicator.packetlisteners.abstracts;

import com.deathmotion.antihealthindicator.AntiHealthIndicator;
import com.deathmotion.antihealthindicator.data.WolfData;
import com.deathmotion.antihealthindicator.enums.ConfigOption;
import com.deathmotion.antihealthindicator.managers.CacheManager;
import com.deathmotion.antihealthindicator.managers.ConfigManager;
Expand All @@ -14,17 +15,20 @@
import org.bukkit.entity.*;

import java.util.List;
import java.util.UUID;

public abstract class EntityListenerAbstract extends PacketListenerAbstract {
private final ConfigManager configManager;
private final CacheManager cacheManager;

private final boolean useEntityCache;
private final boolean healthTexturesSupported;

public EntityListenerAbstract(AntiHealthIndicator plugin) {
cacheManager = plugin.getCacheManager();
configManager = plugin.getConfigManager();

useEntityCache = PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_18);
healthTexturesSupported = PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_15);
}

Expand Down Expand Up @@ -130,12 +134,28 @@ private boolean shouldIgnoreWolf(Player player, Wolf wolf) {
return true;
}

if (ignoreTamedWolves && wolf.isTamed()) {
boolean isTamed;
UUID ownerUniqueId = null;

if (useEntityCache) {
WolfData wolfData = cacheManager.getWolfDataFromCache(wolf.getEntityId());
if (wolfData == null) return false;

isTamed = wolfData.isTamed();
ownerUniqueId = wolfData.getOwnerUniqueId();
} else {
isTamed = wolf.isTamed();
if (wolf.getOwner() != null) {
ownerUniqueId = wolf.getOwner().getUniqueId();
}
}

if (ignoreTamedWolves && isTamed) {
return true;
}

if (ignoreOwnedWolves) {
return wolf.isTamed() && wolf.getOwner() != null && wolf.getOwner().getUniqueId().equals(player.getUniqueId());
return isTamed && ownerUniqueId != null && ownerUniqueId.equals(player.getUniqueId());
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ author: Bram
description: Spoofs certain information sent by the server to other players to prevent them from deducing information about the player's health, armor, held item, etc.
website: https://github.com/Bram1903/AntiHealthIndicator
main: com.deathmotion.antihealthindicator.AntiHealthIndicator
version: '1.1.2'
version: '1.1.3'
api-version: '1.13'
folia-supported: true
load: POSTWORLD
Expand Down

0 comments on commit 4a5b880

Please sign in to comment.