Skip to content

Commit

Permalink
Use per player metadata API
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Dec 24, 2022
1 parent 0e2dd23 commit 8c10fe8
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 79 deletions.
6 changes: 2 additions & 4 deletions main/src/main/java/net/citizensnpcs/EventListen.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
import net.citizensnpcs.trait.CommandTrait;
import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.ScoreboardTrait;
import net.citizensnpcs.trait.ShopTrait;
import net.citizensnpcs.trait.SitTrait;
import net.citizensnpcs.util.ChunkCoord;
Expand Down Expand Up @@ -510,8 +509,7 @@ public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
public void onPlayerJoin(PlayerJoinEvent event) {
skinUpdateTracker.updatePlayer(event.getPlayer(), Setting.INITIAL_PLAYER_JOIN_SKIN_PACKET_DELAY_TICKS.asInt(),
true);

ScoreboardTrait.onPlayerJoin(event);
CitizensAPI.getLocationLookup().onJoin(event);
}

@EventHandler(ignoreCancelled = true)
Expand Down Expand Up @@ -543,7 +541,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
}
}
skinUpdateTracker.removePlayer(event.getPlayer().getUniqueId());
ScoreboardTrait.onPlayerQuit(event);
CitizensAPI.getLocationLookup().onQuit(event);
}

@EventHandler(priority = EventPriority.MONITOR)
Expand Down
45 changes: 17 additions & 28 deletions main/src/main/java/net/citizensnpcs/trait/ScoreboardTrait.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import org.bukkit.scoreboard.Team.Option;
import org.bukkit.scoreboard.Team.OptionStatus;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;

import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.LocationLookup.PerPlayerMetadata;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
Expand All @@ -32,13 +27,25 @@ public class ScoreboardTrait extends Trait {
private boolean changed;
@Persist
private ChatColor color;
private final PerPlayerMetadata metadata;
private ChatColor previousGlowingColor;

@Persist
private final Set<String> tags = new HashSet<String>();

public ScoreboardTrait() {
super("scoreboardtrait");
metadata = CitizensAPI.getLocationLookup().registerMetadata("scoreboard", (meta, event) -> {
for (NPC npc : CitizensAPI.getNPCRegistry()) {
ScoreboardTrait trait = npc.getTraitNullable(ScoreboardTrait.class);
if (trait == null)
continue;
Team team = trait.getTeam();
if (team == null || meta.sent.containsEntry(event.getPlayer().getUniqueId(), team.getName()))
continue;
NMS.sendTeamPacket(event.getPlayer(), team, 0);
meta.sent.put(event.getPlayer().getUniqueId(), team.getName());
}
});
}

public void addTag(String tag) {
Expand Down Expand Up @@ -87,7 +94,7 @@ public void onDespawn() {
if (team.hasEntry(name)) {
if (team.getSize() == 1) {
for (Player player : Bukkit.getOnlinePlayers()) {
SENT_TEAMS.remove(player.getUniqueId(), team.getName());
metadata.sent.remove(player.getUniqueId(), team.getName());
NMS.sendTeamPacket(player, team, 1);
}
team.unregister();
Expand Down Expand Up @@ -215,34 +222,16 @@ public void update() {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.hasMetadata("NPC"))
continue;
if (SENT_TEAMS.containsEntry(player.getUniqueId(), team.getName())) {
if (metadata.sent.containsEntry(player.getUniqueId(), team.getName())) {
NMS.sendTeamPacket(player, team, 2);
} else {
NMS.sendTeamPacket(player, team, 0);
SENT_TEAMS.put(player.getUniqueId(), team.getName());
metadata.sent.put(player.getUniqueId(), team.getName());
}
}
}
}

public static void onPlayerJoin(PlayerJoinEvent event) {
for (NPC npc : CitizensAPI.getNPCRegistry()) {
ScoreboardTrait trait = npc.getTraitNullable(ScoreboardTrait.class);
if (trait == null)
continue;
Team team = trait.getTeam();
if (team == null || SENT_TEAMS.containsEntry(event.getPlayer().getUniqueId(), team.getName()))
continue;
NMS.sendTeamPacket(event.getPlayer(), team, 0);
SENT_TEAMS.put(event.getPlayer().getUniqueId(), team.getName());
}
}

public static void onPlayerQuit(PlayerQuitEvent event) {
SENT_TEAMS.removeAll(event.getPlayer().getUniqueId());
}

private static SetMultimap<UUID, String> SENT_TEAMS = HashMultimap.create();
private static boolean SUPPORT_COLLIDABLE_SETOPTION = true;
private static boolean SUPPORT_GLOWING_COLOR = true;
private static boolean SUPPORT_TAGS = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
Expand Down Expand Up @@ -67,6 +68,7 @@

import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.LocationLookup.PerPlayerMetadata;
import net.citizensnpcs.api.ai.NavigatorParameters;
import net.citizensnpcs.api.ai.event.CancelReason;
import net.citizensnpcs.api.command.CommandManager;
Expand Down Expand Up @@ -1199,19 +1201,55 @@ public void shutdown() {

@Override
public void sleep(Player entity, boolean sleep) {
EntityPlayer player = (EntityPlayer) getHandle(entity);
EntityPlayer from = (EntityPlayer) getHandle(entity);
PerPlayerMetadata meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
if (sleep) {
Location loc = player.getBukkitEntity().getLocation();
PacketPlayOutBed bed = new PacketPlayOutBed(player,
new BlockPosition((int) player.locX, (int) player.locY, (int) player.locZ));
for (Player nearby : CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64)) {
nearby.sendBlockChange(loc, Material.BED.getId(), (byte) 11);
sendPacket(nearby, bed);
nearby.sendBlockChange(loc, 0, (byte) 0);
List<Player> nearbyPlayers = Lists.newArrayList(
Iterables.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64),
(p) -> !meta.sent.containsEntry(p.getUniqueId(), entity.getUniqueId().toString())));
if (nearbyPlayers.size() == 0)
return;
Location loc = from.getBukkitEntity().getLocation().clone();
BlockFace[] axis = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
BlockFace facing = axis[Math.round(loc.getYaw() / 90f) & 0x3].getOppositeFace();
byte facingByte = 0;
switch (facing) {
case EAST:
facingByte = (byte) 1;
break;
case SOUTH:
facingByte = (byte) 2;
break;
case WEST:
facingByte = (byte) 3;
break;
}
Location bedLoc = loc.clone().add(0, -loc.getY(), 0);
PacketPlayOutBed bed = new PacketPlayOutBed(from,
new BlockPosition(bedLoc.getBlockX(), bedLoc.getBlockY(), bedLoc.getBlockZ()));
List<Packet<?>> list = Lists.newArrayListWithCapacity(3);
from.locX = bedLoc.getBlockX();
from.locY = bedLoc.getBlockY();
from.locZ = bedLoc.getBlockZ();
list.add(new PacketPlayOutEntityTeleport(from));
list.add(bed);
from.locX = loc.getX();
from.locY = loc.getY();
from.locZ = loc.getZ();
list.add(new PacketPlayOutEntityTeleport(from));
for (Player nearby : nearbyPlayers) {
nearby.sendBlockChange(bedLoc, Material.BED_BLOCK, facingByte);
list.forEach((packet) -> sendPacket(nearby, packet));
meta.sent.put(nearby.getUniqueId(), entity.getUniqueId().toString());
}
} else {
PacketPlayOutAnimation packet = new PacketPlayOutAnimation(player, 2);
PacketPlayOutAnimation packet = new PacketPlayOutAnimation(from, 2);
sendPacketNearby(entity, entity.getLocation(), packet, 64);
for (Player player : Bukkit.getOnlinePlayers()) {
if (meta.sent.remove(player.getUniqueId(), entity.getUniqueId().toString())) {
sendPacket(player, packet);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
Expand Down Expand Up @@ -67,6 +68,7 @@

import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.LocationLookup.PerPlayerMetadata;
import net.citizensnpcs.api.ai.NavigatorParameters;
import net.citizensnpcs.api.ai.event.CancelReason;
import net.citizensnpcs.api.command.CommandManager;
Expand Down Expand Up @@ -1266,19 +1268,55 @@ public void shutdown() {

@Override
public void sleep(Player entity, boolean sleep) {
EntityPlayer player = (EntityPlayer) getHandle(entity);
EntityPlayer from = (EntityPlayer) getHandle(entity);
PerPlayerMetadata meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
if (sleep) {
Location loc = player.getBukkitEntity().getLocation();
PacketPlayOutBed bed = new PacketPlayOutBed(player,
new BlockPosition((int) player.locX, (int) player.locY, (int) player.locZ));
for (Player nearby : CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64)) {
nearby.sendBlockChange(loc, Material.BED.getId(), (byte) 11);
sendPacket(nearby, bed);
nearby.sendBlockChange(loc, 0, (byte) 0);
List<Player> nearbyPlayers = Lists.newArrayList(
Iterables.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64),
(p) -> !meta.sent.containsEntry(p.getUniqueId(), entity.getUniqueId().toString())));
if (nearbyPlayers.size() == 0)
return;
Location loc = from.getBukkitEntity().getLocation().clone();
BlockFace[] axis = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
BlockFace facing = axis[Math.round(loc.getYaw() / 90f) & 0x3].getOppositeFace();
byte facingByte = 0;
switch (facing) {
case EAST:
facingByte = (byte) 1;
break;
case SOUTH:
facingByte = (byte) 2;
break;
case WEST:
facingByte = (byte) 3;
break;
}
Location bedLoc = loc.clone().add(0, -loc.getY(), 0);
PacketPlayOutBed bed = new PacketPlayOutBed(from,
new BlockPosition(bedLoc.getBlockX(), bedLoc.getBlockY(), bedLoc.getBlockZ()));
List<Packet<?>> list = Lists.newArrayListWithCapacity(3);
from.locX = bedLoc.getBlockX();
from.locY = bedLoc.getBlockY();
from.locZ = bedLoc.getBlockZ();
list.add(new PacketPlayOutEntityTeleport(from));
list.add(bed);
from.locX = loc.getX();
from.locY = loc.getY();
from.locZ = loc.getZ();
list.add(new PacketPlayOutEntityTeleport(from));
for (Player nearby : nearbyPlayers) {
nearby.sendBlockChange(bedLoc, Material.BED_BLOCK, facingByte);
list.forEach((packet) -> sendPacket(nearby, packet));
meta.sent.put(nearby.getUniqueId(), entity.getUniqueId().toString());
}
} else {
PacketPlayOutAnimation packet = new PacketPlayOutAnimation(player, 2);
PacketPlayOutAnimation packet = new PacketPlayOutAnimation(from, 2);
sendPacketNearby(entity, entity.getLocation(), packet, 64);
for (Player player : Bukkit.getOnlinePlayers()) {
if (meta.sent.remove(player.getUniqueId(), entity.getUniqueId().toString())) {
sendPacket(player, packet);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
Expand Down Expand Up @@ -67,6 +68,7 @@

import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.LocationLookup.PerPlayerMetadata;
import net.citizensnpcs.api.ai.NavigatorParameters;
import net.citizensnpcs.api.ai.event.CancelReason;
import net.citizensnpcs.api.command.CommandManager;
Expand Down Expand Up @@ -1276,19 +1278,55 @@ public void shutdown() {

@Override
public void sleep(Player entity, boolean sleep) {
EntityPlayer player = (EntityPlayer) getHandle(entity);
EntityPlayer from = (EntityPlayer) getHandle(entity);
PerPlayerMetadata meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
if (sleep) {
Location loc = player.getBukkitEntity().getLocation();
PacketPlayOutBed bed = new PacketPlayOutBed(player,
new BlockPosition((int) player.locX, (int) player.locY, (int) player.locZ));
for (Player nearby : CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64)) {
nearby.sendBlockChange(loc, Material.BED.getId(), (byte) 11);
sendPacket(nearby, bed);
nearby.sendBlockChange(loc, 0, (byte) 0);
List<Player> nearbyPlayers = Lists.newArrayList(
Iterables.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64),
(p) -> !meta.sent.containsEntry(p.getUniqueId(), entity.getUniqueId().toString())));
if (nearbyPlayers.size() == 0)
return;
Location loc = from.getBukkitEntity().getLocation().clone();
BlockFace[] axis = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
BlockFace facing = axis[Math.round(loc.getYaw() / 90f) & 0x3].getOppositeFace();
byte facingByte = 0;
switch (facing) {
case EAST:
facingByte = (byte) 1;
break;
case SOUTH:
facingByte = (byte) 2;
break;
case WEST:
facingByte = (byte) 3;
break;
}
Location bedLoc = loc.clone().add(0, -loc.getY(), 0);
PacketPlayOutBed bed = new PacketPlayOutBed(from,
new BlockPosition(bedLoc.getBlockX(), bedLoc.getBlockY(), bedLoc.getBlockZ()));
List<Packet<?>> list = Lists.newArrayListWithCapacity(3);
from.locX = bedLoc.getBlockX();
from.locY = bedLoc.getBlockY();
from.locZ = bedLoc.getBlockZ();
list.add(new PacketPlayOutEntityTeleport(from));
list.add(bed);
from.locX = loc.getX();
from.locY = loc.getY();
from.locZ = loc.getZ();
list.add(new PacketPlayOutEntityTeleport(from));
for (Player nearby : nearbyPlayers) {
nearby.sendBlockChange(bedLoc, Material.BED_BLOCK, facingByte);
list.forEach((packet) -> sendPacket(nearby, packet));
meta.sent.put(nearby.getUniqueId(), entity.getUniqueId().toString());
}
} else {
PacketPlayOutAnimation packet = new PacketPlayOutAnimation(player, 2);
PacketPlayOutAnimation packet = new PacketPlayOutAnimation(from, 2);
sendPacketNearby(entity, entity.getLocation(), packet, 64);
for (Player player : Bukkit.getOnlinePlayers()) {
if (meta.sent.remove(player.getUniqueId(), entity.getUniqueId().toString())) {
sendPacket(player, packet);
}
}
}
}

Expand Down
Loading

0 comments on commit 8c10fe8

Please sign in to comment.