From b46a1d3cb2371706ba49c9cf6c1b8919dc6edaa8 Mon Sep 17 00:00:00 2001 From: md_5 Date: Tue, 19 Feb 2013 18:30:10 +1100 Subject: [PATCH] Implement TagAPI into Spigot - much more efficient and only a dozen lines of code. --- .../net/minecraft/server/EntityTrackerEntry.java | 12 +++- .../java/net/minecraft/server/MinecraftServer.java | 1 + .../org/kitteh/tag/PlayerReceiveNameTagEvent.java | 49 +++++++++++++++ src/main/java/org/kitteh/tag/TagAPI.java | 73 ++++++++++++++++++++++ 4 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/kitteh/tag/PlayerReceiveNameTagEvent.java create mode 100644 src/main/java/org/kitteh/tag/TagAPI.java diff --git a/src/main/java/net/minecraft/server/EntityTrackerEntry.java b/src/main/java/net/minecraft/server/EntityTrackerEntry.java index 27a548f..d95d5af 100644 --- a/src/main/java/net/minecraft/server/EntityTrackerEntry.java +++ b/src/main/java/net/minecraft/server/EntityTrackerEntry.java @@ -301,7 +301,7 @@ public class EntityTrackerEntry { // CraftBukkit end this.trackedPlayers.add(entityplayer); - Packet packet = this.b(); + Packet packet = this.b(entityplayer); // Spigot entityplayer.playerConnection.sendPacket(packet); if (!this.tracker.getDataWatcher().d()) { @@ -370,7 +370,7 @@ public class EntityTrackerEntry { } } - private Packet b() { + private Packet b(EntityPlayer target) { // Spigot if (this.tracker.dead) { // CraftBukkit start - remove useless error spam, just return // System.out.println("Fetching addPacket for removed entity"); @@ -381,7 +381,13 @@ public class EntityTrackerEntry { if (this.tracker instanceof EntityItem) { return new Packet23VehicleSpawn(this.tracker, 2, 1); } else if (this.tracker instanceof EntityPlayer) { - return new Packet20NamedEntitySpawn((EntityHuman) this.tracker); + // Spigot start + Packet20NamedEntitySpawn packet = new Packet20NamedEntitySpawn((EntityHuman) this.tracker); + org.kitteh.tag.PlayerReceiveNameTagEvent event = new org.kitteh.tag.PlayerReceiveNameTagEvent(target.getBukkitEntity(), ((EntityPlayer) tracker).getBukkitEntity()); + tracker.world.getServer().getPluginManager().callEvent(event); + packet.b = event.getTag(); + return packet; + // Spigot end } else { if (this.tracker instanceof EntityMinecart) { EntityMinecart entityminecart = (EntityMinecart) this.tracker; diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index 99a6cf4..69564cd 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -512,6 +512,7 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IMo this.methodProfiler.b(); this.methodProfiler.b(); + org.kitteh.tag.TagAPI.tick(); // Spigot } public void r() { diff --git a/src/main/java/org/kitteh/tag/PlayerReceiveNameTagEvent.java b/src/main/java/org/kitteh/tag/PlayerReceiveNameTagEvent.java new file mode 100644 index 0000000..2ea9e07 --- /dev/null +++ b/src/main/java/org/kitteh/tag/PlayerReceiveNameTagEvent.java @@ -0,0 +1,49 @@ +package org.kitteh.tag; + +import org.apache.commons.lang.Validate; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; + +public class PlayerReceiveNameTagEvent extends PlayerEvent { + + private static final HandlerList handlers = new HandlerList(); + private boolean modified; + private final Player named; + private String tag; + + public PlayerReceiveNameTagEvent(Player who, Player named) { + super(who); + this.modified = false; + this.named = named; + this.tag = named.getName(); + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + public Player getNamedPlayer() { + return named; + } + + public String getTag() { + return tag; + } + + public boolean isModified() { + return modified; + } + + public boolean setTag(String newTag) { + Validate.notNull(newTag, "New nametag cannot be null!"); + tag = newTag; + modified = true; + return (newTag.length() < 16) ? true : false; + } +} diff --git a/src/main/java/org/kitteh/tag/TagAPI.java b/src/main/java/org/kitteh/tag/TagAPI.java new file mode 100644 index 0000000..1e2693c --- /dev/null +++ b/src/main/java/org/kitteh/tag/TagAPI.java @@ -0,0 +1,73 @@ +package org.kitteh.tag; + +import com.google.common.base.Preconditions; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import org.bukkit.entity.Player; + +public class TagAPI { + + private static Set tasks = new HashSet(); + + private static class DelayedShowTask { + + private final Map pairs = new HashMap(); + private int countdown = 2; + } + + public static void tick() { + for (Iterator iter = tasks.iterator(); iter.hasNext();) { + DelayedShowTask task = iter.next(); + if (--task.countdown == 0) { + iter.remove(); + for (Map.Entry pair : task.pairs.entrySet()) { + if (pair.getKey() != null && pair.getValue() != null) { + pair.getValue().showPlayer(pair.getValue()); + } + } + } + } + } + + public static void refreshPlayer(Player player) { + Preconditions.checkArgument(player != null, "player"); + Preconditions.checkArgument(player.isOnline(), "offline"); + + DelayedShowTask task = new DelayedShowTask(); + for (Player otherGuy : player.getWorld().getPlayers()) { + if (player != otherGuy && otherGuy.canSee(player)) { + otherGuy.hidePlayer(player); + task.pairs.put(otherGuy, player); + } + } + + tasks.add(task); + } + + public static void refreshPlayer(Player player, Player forWhom) { + Preconditions.checkArgument(player != null, "player"); + Preconditions.checkArgument(forWhom != null, "forWhom"); + Preconditions.checkArgument(player != forWhom, "self"); + DelayedShowTask task = new DelayedShowTask(); + + if (forWhom.canSee(player) && player.getWorld() == forWhom.getWorld()) { + forWhom.hidePlayer(player); + task.pairs.put(forWhom, player); + } + + tasks.add(task); + } + + public static void refreshPlayer(Player player, Set forWhom) { + Preconditions.checkArgument(player != null, "player"); + Preconditions.checkArgument(forWhom != null, "forWhom"); + for (Player whom : forWhom) { + if (player != whom) { + refreshPlayer(player, whom); + } + } + } +} -- 1.8.1-rc2