Use new API

This commit is contained in:
fullwall 2022-12-24 21:43:08 +08:00
parent 8c10fe8b18
commit 886d83eeb4
9 changed files with 84 additions and 73 deletions

View File

@ -30,8 +30,8 @@ public class AdminCommands {
public void citizens(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Messaging.send(sender, StringHelper.wrapHeader("<green>Citizens v" + plugin.getDescription().getVersion()));
Messaging.send(sender, " <yellow>-- <green>Author: fullwall");
Messaging.send(sender, " <yellow>-- <green>Website: " + plugin.getDescription().getWebsite());
Messaging.send(sender, " <yellow>-- <green>Support: https://discord.gg/Q6pZGSR");
Messaging.send(sender, " <yellow>-- <green><click:open_url:" + plugin.getDescription().getWebsite()
+ "><hover:show_text:Citizens website including wiki><u>Website</hover></click> <click:open_url:https://discord.gg/Q6pZGSR><hover:show_text:Citizens Support Discord><u>Support</hover></click>");
}
@Command(

View File

@ -616,9 +616,9 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "create [name] ((-b(aby),u(nspawned),s(ilent),t(emporary)) --at [x:y:z:world] --type [type] --item (item) --trait ['trait1, trait2...'] --registry [registry name])",
usage = "create [name] ((-b(aby),u(nspawned),s(ilent),t(emporary),c(enter)) --at [x:y:z:world] --type [type] --item (item) --trait ['trait1, trait2...'] --registry [registry name])",
desc = "Create a new NPC",
flags = "bust",
flags = "bustc",
modifiers = { "create" },
min = 2,
permission = "citizens.npc.create")
@ -723,6 +723,10 @@ public class NPCCommands {
throw new CommandException(Messages.INVALID_SPAWN_LOCATION);
}
if (args.hasFlag('c')) {
spawnLoc = Util.getCenterLocation(spawnLoc.getBlock());
}
if (!args.hasFlag('u')) {
npc.spawn(spawnLoc, SpawnReason.CREATE);
}
@ -2171,6 +2175,9 @@ public class NPCCommands {
}
if (yaw != null) {
NMS.setBodyYaw(npc.getEntity(), yaw);
if (npc.getEntity().getType() == EntityType.PLAYER) {
PlayerAnimation.ARM_SWING.play((Player) npc.getEntity());
}
}
if (pitch != null) {
NMS.setPitch(npc.getEntity(), pitch);

View File

@ -7,11 +7,11 @@ import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
@ -360,34 +360,26 @@ public class RotationTrait extends Trait {
public class RotationSession {
private final RotationParams params;
private int t = -1;
private double tx, ty, tz;
private Supplier<Float> targetPitch = () -> 0F;
private Supplier<Float> targetYaw = targetPitch;
public RotationSession(RotationParams params) {
this.params = params;
}
public float getTargetPitch() {
double dx = tx - getX();
double dy = ty - (getY() + getEyeY());
double dz = tz - getZ();
double diag = Math.sqrt((float) (dx * dx + dz * dz));
return (float) -Math.toDegrees(Math.atan2(dy, diag));
}
public double getTargetX() {
return tx;
}
public double getTargetY() {
return ty;
return targetPitch.get();
}
public float getTargetYaw() {
return (float) Math.toDegrees(Math.atan2(tz - getZ(), tx - getX())) - 90.0F;
}
public double getTargetZ() {
return tz;
switch (npc.getEntity().getType()) {
case PHANTOM:
return Util.clamp(targetYaw.get() + 45);
case ENDER_DRAGON:
return Util.clamp(targetYaw.get() - 180);
default:
return targetYaw.get();
}
}
public boolean isActive() {
@ -413,7 +405,17 @@ public class RotationTrait extends Trait {
* The target location to face
*/
public void rotateToFace(Location target) {
setTarget(target);
t = 0;
targetPitch = () -> {
double dx = target.getX() - getX();
double dy = target.getY() - (getY() + getEyeY());
double dz = target.getZ() - getZ();
double diag = Math.sqrt((float) (dx * dx + dz * dz));
return (float) -Math.toDegrees(Math.atan2(dy, diag));
};
targetYaw = () -> {
return (float) Math.toDegrees(Math.atan2(target.getZ() - getZ(), target.getX() - getX())) - 90.0F;
};
}
/**
@ -423,10 +425,9 @@ public class RotationTrait extends Trait {
* @param pitch
*/
public void rotateToHave(float yaw, float pitch) {
double pitchCos = Math.cos(Math.toRadians(pitch));
Vector vector = new Vector(Math.sin(Math.toRadians(yaw)) * -pitchCos, -Math.sin(Math.toRadians(pitch)),
Math.cos(Math.toRadians(yaw)) * pitchCos).normalize();
rotateToFace(npc.getStoredLocation().clone().add(vector));
t = 0;
targetYaw = () -> yaw;
targetPitch = () -> pitch;
}
private void run(RotationTriple rot) {
@ -459,13 +460,6 @@ public class RotationTrait extends Trait {
rot.apply();
}
public void setTarget(Location target) {
tx = target.getX();
ty = target.getY();
tz = target.getZ();
t = 0;
}
}
private static abstract class RotationTriple implements Cloneable {

View File

@ -27,23 +27,23 @@ public class ScoreboardTrait extends Trait {
private boolean changed;
@Persist
private ChatColor color;
private final PerPlayerMetadata metadata;
private final PerPlayerMetadata<Boolean> metadata;
private ChatColor previousGlowingColor;
@Persist
private final Set<String> tags = new HashSet<String>();
public ScoreboardTrait() {
super("scoreboardtrait");
metadata = CitizensAPI.getLocationLookup().registerMetadata("scoreboard", (meta, event) -> {
metadata = CitizensAPI.getLocationLookup().<Boolean> 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()))
if (team == null || meta.has(event.getPlayer().getUniqueId(), team.getName()))
continue;
NMS.sendTeamPacket(event.getPlayer(), team, 0);
meta.sent.put(event.getPlayer().getUniqueId(), team.getName());
meta.set(event.getPlayer().getUniqueId(), team.getName(), true);
}
});
}
@ -94,7 +94,7 @@ public class ScoreboardTrait extends Trait {
if (team.hasEntry(name)) {
if (team.getSize() == 1) {
for (Player player : Bukkit.getOnlinePlayers()) {
metadata.sent.remove(player.getUniqueId(), team.getName());
metadata.remove(player.getUniqueId(), team.getName());
NMS.sendTeamPacket(player, team, 1);
}
team.unregister();
@ -222,11 +222,11 @@ public class ScoreboardTrait extends Trait {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.hasMetadata("NPC"))
continue;
if (metadata.sent.containsEntry(player.getUniqueId(), team.getName())) {
if (metadata.has(player.getUniqueId(), team.getName())) {
NMS.sendTeamPacket(player, team, 2);
} else {
NMS.sendTeamPacket(player, team, 0);
metadata.sent.put(player.getUniqueId(), team.getName());
metadata.set(player.getUniqueId(), team.getName(), true);
}
}
}

View File

@ -1202,11 +1202,13 @@ public class NMSImpl implements NMSBridge {
@Override
public void sleep(Player entity, boolean sleep) {
EntityPlayer from = (EntityPlayer) getHandle(entity);
PerPlayerMetadata meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
PerPlayerMetadata<Long> meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
if (sleep) {
List<Player> nearbyPlayers = Lists.newArrayList(
Iterables.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64),
(p) -> !meta.sent.containsEntry(p.getUniqueId(), entity.getUniqueId().toString())));
List<Player> nearbyPlayers = Lists.newArrayList(Iterables
.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64), (p) -> {
Long time = meta.getMarker(p.getUniqueId(), entity.getUniqueId().toString());
return time == null || Math.abs(entity.getWorld().getFullTime() - time) > 100;
}));
if (nearbyPlayers.size() == 0)
return;
Location loc = from.getBukkitEntity().getLocation().clone();
@ -1240,13 +1242,13 @@ public class NMSImpl implements NMSBridge {
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());
meta.set(nearby.getUniqueId(), entity.getUniqueId().toString(), nearby.getWorld().getFullTime());
}
} else {
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())) {
if (meta.remove(player.getUniqueId(), entity.getUniqueId().toString())) {
sendPacket(player, packet);
}
}

View File

@ -1269,11 +1269,13 @@ public class NMSImpl implements NMSBridge {
@Override
public void sleep(Player entity, boolean sleep) {
EntityPlayer from = (EntityPlayer) getHandle(entity);
PerPlayerMetadata meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
PerPlayerMetadata<Long> meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
if (sleep) {
List<Player> nearbyPlayers = Lists.newArrayList(
Iterables.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64),
(p) -> !meta.sent.containsEntry(p.getUniqueId(), entity.getUniqueId().toString())));
List<Player> nearbyPlayers = Lists.newArrayList(Iterables
.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64), (p) -> {
Long time = meta.getMarker(p.getUniqueId(), entity.getUniqueId().toString());
return time == null || Math.abs(entity.getWorld().getFullTime() - time) > 100;
}));
if (nearbyPlayers.size() == 0)
return;
Location loc = from.getBukkitEntity().getLocation().clone();
@ -1307,13 +1309,13 @@ public class NMSImpl implements NMSBridge {
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());
meta.set(nearby.getUniqueId(), entity.getUniqueId().toString(), nearby.getWorld().getFullTime());
}
} else {
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())) {
if (meta.remove(player.getUniqueId(), entity.getUniqueId().toString())) {
sendPacket(player, packet);
}
}

View File

@ -1279,11 +1279,13 @@ public class NMSImpl implements NMSBridge {
@Override
public void sleep(Player entity, boolean sleep) {
EntityPlayer from = (EntityPlayer) getHandle(entity);
PerPlayerMetadata meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
PerPlayerMetadata<Long> meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
if (sleep) {
List<Player> nearbyPlayers = Lists.newArrayList(
Iterables.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64),
(p) -> !meta.sent.containsEntry(p.getUniqueId(), entity.getUniqueId().toString())));
List<Player> nearbyPlayers = Lists.newArrayList(Iterables
.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64), (p) -> {
Long time = meta.getMarker(p.getUniqueId(), entity.getUniqueId().toString());
return time == null || Math.abs(entity.getWorld().getFullTime() - time) > 100;
}));
if (nearbyPlayers.size() == 0)
return;
Location loc = from.getBukkitEntity().getLocation().clone();
@ -1317,13 +1319,13 @@ public class NMSImpl implements NMSBridge {
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());
meta.set(nearby.getUniqueId(), entity.getUniqueId().toString(), nearby.getWorld().getFullTime());
}
} else {
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())) {
if (meta.remove(player.getUniqueId(), entity.getUniqueId().toString())) {
sendPacket(player, packet);
}
}

View File

@ -1319,11 +1319,13 @@ public class NMSImpl implements NMSBridge {
@Override
public void sleep(Player entity, boolean sleep) {
EntityPlayer from = (EntityPlayer) getHandle(entity);
PerPlayerMetadata meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
PerPlayerMetadata<Long> meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
if (sleep) {
List<Player> nearbyPlayers = Lists.newArrayList(
Iterables.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64),
(p) -> !meta.sent.containsEntry(p.getUniqueId(), entity.getUniqueId().toString())));
List<Player> nearbyPlayers = Lists.newArrayList(Iterables
.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64), (p) -> {
Long time = meta.getMarker(p.getUniqueId(), entity.getUniqueId().toString());
return time == null || Math.abs(entity.getWorld().getFullTime() - time) > 100;
}));
if (nearbyPlayers.size() == 0)
return;
Location loc = from.getBukkitEntity().getLocation().clone();
@ -1357,13 +1359,13 @@ public class NMSImpl implements NMSBridge {
for (Player nearby : nearbyPlayers) {
nearby.sendBlockChange(bedLoc, Material.BLACK_BED, facingByte);
list.forEach((packet) -> sendPacket(nearby, packet));
meta.sent.put(nearby.getUniqueId(), entity.getUniqueId().toString());
meta.set(nearby.getUniqueId(), entity.getUniqueId().toString(), nearby.getWorld().getFullTime());
}
} else {
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())) {
if (meta.remove(player.getUniqueId(), entity.getUniqueId().toString())) {
sendPacket(player, packet);
}
}

View File

@ -1139,11 +1139,13 @@ public class NMSImpl implements NMSBridge {
@Override
public void sleep(Player entity, boolean sleep) {
EntityPlayer from = (EntityPlayer) getHandle(entity);
PerPlayerMetadata meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
PerPlayerMetadata<Long> meta = CitizensAPI.getLocationLookup().registerMetadata("sleeping", null);
if (sleep) {
List<Player> nearbyPlayers = Lists.newArrayList(
Iterables.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64),
(p) -> !meta.sent.containsEntry(p.getUniqueId(), entity.getUniqueId().toString())));
List<Player> nearbyPlayers = Lists.newArrayList(Iterables
.filter(CitizensAPI.getLocationLookup().getNearbyPlayers(entity.getLocation(), 64), (p) -> {
Long time = meta.getMarker(p.getUniqueId(), entity.getUniqueId().toString());
return time == null || Math.abs(entity.getWorld().getFullTime() - time) > 100;
}));
if (nearbyPlayers.size() == 0)
return;
Location loc = from.getBukkitEntity().getLocation().clone();
@ -1177,13 +1179,13 @@ public class NMSImpl implements NMSBridge {
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());
meta.set(nearby.getUniqueId(), entity.getUniqueId().toString(), nearby.getWorld().getFullTime());
}
} else {
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())) {
if (meta.remove(player.getUniqueId(), entity.getUniqueId().toString())) {
sendPacket(player, packet);
}
}