#1136: Add API to allow entities to be invisible by default
Designed to make creating per-player entities easier By: md_5 <git@md-5.net>
This commit is contained in:
@@ -177,6 +177,7 @@ import org.bukkit.craftbukkit.persistence.CraftPersistentDataTypeRegistry;
|
||||
import org.bukkit.craftbukkit.util.CraftChatMessage;
|
||||
import org.bukkit.craftbukkit.util.CraftSpawnCategory;
|
||||
import org.bukkit.craftbukkit.util.CraftVector;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Pose;
|
||||
import org.bukkit.entity.SpawnCategory;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
@@ -880,6 +881,30 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
|
||||
return getHandle().isCustomNameVisible();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVisibleByDefault(boolean visible) {
|
||||
if (getHandle().visibleByDefault != visible) {
|
||||
if (visible) {
|
||||
// Making visible by default, reset and show to all players
|
||||
for (Player player : server.getOnlinePlayers()) {
|
||||
((CraftPlayer) player).resetAndShowEntity(this);
|
||||
}
|
||||
} else {
|
||||
// Hiding by default, reset and hide from all players
|
||||
for (Player player : server.getOnlinePlayers()) {
|
||||
((CraftPlayer) player).resetAndHideEntity(this);
|
||||
}
|
||||
}
|
||||
|
||||
getHandle().visibleByDefault = visible;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVisibleByDefault() {
|
||||
return getHandle().visibleByDefault;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String message) {
|
||||
|
||||
|
||||
@@ -165,7 +165,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
private boolean hasPlayedBefore = false;
|
||||
private final ConversationTracker conversationTracker = new ConversationTracker();
|
||||
private final Set<String> channels = new HashSet<String>();
|
||||
private final Map<UUID, Set<WeakReference<Plugin>>> hiddenEntities = new HashMap<>();
|
||||
private final Map<UUID, Set<WeakReference<Plugin>>> invertedVisibilityEntities = new HashMap<>();
|
||||
private static final WeakHashMap<Plugin, WeakReference<Plugin>> pluginWeakReferences = new WeakHashMap<>();
|
||||
private int hash = 0;
|
||||
private double health = 20;
|
||||
@@ -1292,17 +1292,34 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
if (getHandle().connection == null) return;
|
||||
if (equals(entity)) return;
|
||||
|
||||
Set<WeakReference<Plugin>> hidingPlugins = hiddenEntities.get(entity.getUniqueId());
|
||||
if (hidingPlugins != null) {
|
||||
// Some plugins are already hiding the entity. Just mark that this
|
||||
// plugin wants the entity hidden too and end.
|
||||
hidingPlugins.add(getPluginWeakReference(plugin));
|
||||
return;
|
||||
boolean shouldHide;
|
||||
if (entity.isVisibleByDefault()) {
|
||||
shouldHide = addInvertedVisibility(plugin, entity);
|
||||
} else {
|
||||
shouldHide = removeInvertedVisiblity(plugin, entity);
|
||||
}
|
||||
hidingPlugins = new HashSet<>();
|
||||
hidingPlugins.add(getPluginWeakReference(plugin));
|
||||
hiddenEntities.put(entity.getUniqueId(), hidingPlugins);
|
||||
|
||||
if (shouldHide) {
|
||||
untrackAndHideEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean addInvertedVisibility(@Nullable Plugin plugin, org.bukkit.entity.Entity entity) {
|
||||
Set<WeakReference<Plugin>> invertedPlugins = invertedVisibilityEntities.get(entity.getUniqueId());
|
||||
if (invertedPlugins != null) {
|
||||
// Some plugins are already inverting the entity. Just mark that this
|
||||
// plugin wants the entity inverted too and end.
|
||||
invertedPlugins.add(getPluginWeakReference(plugin));
|
||||
return false;
|
||||
}
|
||||
invertedPlugins = new HashSet<>();
|
||||
invertedPlugins.add(getPluginWeakReference(plugin));
|
||||
invertedVisibilityEntities.put(entity.getUniqueId(), invertedPlugins);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void untrackAndHideEntity(org.bukkit.entity.Entity entity) {
|
||||
// Remove this entity from the hidden player's EntityTrackerEntry
|
||||
PlayerChunkMap tracker = ((WorldServer) getHandle().level).getChunkSource().chunkMap;
|
||||
Entity other = ((CraftEntity) entity).getHandle();
|
||||
@@ -1322,6 +1339,12 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
server.getPluginManager().callEvent(new PlayerHideEntityEvent(this, entity));
|
||||
}
|
||||
|
||||
void resetAndHideEntity(org.bukkit.entity.Entity entity) {
|
||||
if (invertedVisibilityEntities.remove(entity.getUniqueId()) == null) {
|
||||
untrackAndHideEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public void showPlayer(Player player) {
|
||||
@@ -1346,16 +1369,33 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
if (getHandle().connection == null) return;
|
||||
if (equals(entity)) return;
|
||||
|
||||
Set<WeakReference<Plugin>> hidingPlugins = hiddenEntities.get(entity.getUniqueId());
|
||||
if (hidingPlugins == null) {
|
||||
return; // Entity isn't hidden
|
||||
boolean shouldShow;
|
||||
if (entity.isVisibleByDefault()) {
|
||||
shouldShow = removeInvertedVisiblity(plugin, entity);
|
||||
} else {
|
||||
shouldShow = addInvertedVisibility(plugin, entity);
|
||||
}
|
||||
hidingPlugins.remove(getPluginWeakReference(plugin));
|
||||
if (!hidingPlugins.isEmpty()) {
|
||||
return; // Some other plugins still want the entity hidden
|
||||
}
|
||||
hiddenEntities.remove(entity.getUniqueId());
|
||||
|
||||
if (shouldShow) {
|
||||
trackAndShowEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean removeInvertedVisiblity(@Nullable Plugin plugin, org.bukkit.entity.Entity entity) {
|
||||
Set<WeakReference<Plugin>> invertedPlugins = invertedVisibilityEntities.get(entity.getUniqueId());
|
||||
if (invertedPlugins == null) {
|
||||
return false; // Entity isn't inverted
|
||||
}
|
||||
invertedPlugins.remove(getPluginWeakReference(plugin));
|
||||
if (!invertedPlugins.isEmpty()) {
|
||||
return false; // Some other plugins still want the entity inverted
|
||||
}
|
||||
invertedVisibilityEntities.remove(entity.getUniqueId());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void trackAndShowEntity(org.bukkit.entity.Entity entity) {
|
||||
PlayerChunkMap tracker = ((WorldServer) getHandle().level).getChunkSource().chunkMap;
|
||||
Entity other = ((CraftEntity) entity).getHandle();
|
||||
|
||||
@@ -1372,8 +1412,14 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
server.getPluginManager().callEvent(new PlayerShowEntityEvent(this, entity));
|
||||
}
|
||||
|
||||
void resetAndShowEntity(org.bukkit.entity.Entity entity) {
|
||||
if (invertedVisibilityEntities.remove(entity.getUniqueId()) == null) {
|
||||
trackAndShowEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public void onEntityRemove(Entity entity) {
|
||||
hiddenEntities.remove(entity.getUUID());
|
||||
invertedVisibilityEntities.remove(entity.getUUID());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1383,11 +1429,16 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
|
||||
@Override
|
||||
public boolean canSee(org.bukkit.entity.Entity entity) {
|
||||
return canSee(entity.getUniqueId());
|
||||
return entity.isVisibleByDefault() ^ invertedVisibilityEntities.containsKey(entity.getUniqueId());
|
||||
}
|
||||
|
||||
public boolean canSee(UUID uuid) {
|
||||
return !hiddenEntities.containsKey(uuid);
|
||||
org.bukkit.entity.Entity entity = getServer().getPlayer(uuid);
|
||||
if (entity == null) {
|
||||
entity = getServer().getEntity(uuid); // Also includes players, but check players first for efficiency
|
||||
}
|
||||
|
||||
return (entity != null) ? canSee(entity) : false; // If we can't find it, we can't see it
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user