changed look property a bit

This commit is contained in:
D3v1s0m 2023-09-17 21:22:48 +05:30
parent dfe2d22da3
commit 9d17a5bdd3
No known key found for this signature in database
GPG Key ID: FA1F770C7B1D40C1
10 changed files with 65 additions and 6 deletions

@ -0,0 +1,7 @@
package lol.pyr.znpcsplus.util;
public enum LookType {
FIXED,
CLOSEST_PLAYER,
PER_PLAYER
}

@ -276,6 +276,7 @@ public class ZNpcsPlus extends JavaPlugin {
registerEnumParser(manager, OcelotType.class, incorrectUsageMessage); registerEnumParser(manager, OcelotType.class, incorrectUsageMessage);
registerEnumParser(manager, PandaGene.class, incorrectUsageMessage); registerEnumParser(manager, PandaGene.class, incorrectUsageMessage);
registerEnumParser(manager, PuffState.class, incorrectUsageMessage); registerEnumParser(manager, PuffState.class, incorrectUsageMessage);
registerEnumParser(manager, LookType.class, incorrectUsageMessage);
manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root")) manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root"))
.addSubcommand("center", new CenterCommand(npcRegistry)) .addSubcommand("center", new CenterCommand(npcRegistry))

@ -3,6 +3,7 @@ package lol.pyr.znpcsplus.conversion.citizens.model.traits;
import lol.pyr.znpcsplus.api.entity.EntityPropertyRegistry; import lol.pyr.znpcsplus.api.entity.EntityPropertyRegistry;
import lol.pyr.znpcsplus.conversion.citizens.model.SectionCitizensTrait; import lol.pyr.znpcsplus.conversion.citizens.model.SectionCitizensTrait;
import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcImpl;
import lol.pyr.znpcsplus.util.LookType;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -16,7 +17,7 @@ public class LookTrait extends SectionCitizensTrait {
@Override @Override
public @NotNull NpcImpl apply(NpcImpl npc, ConfigurationSection section) { public @NotNull NpcImpl apply(NpcImpl npc, ConfigurationSection section) {
if (section.getBoolean("enabled")) npc.setProperty(registry.getByName("look", Boolean.class), true); if (section.getBoolean("enabled")) npc.setProperty(registry.getByName("look", LookType.class), LookType.CLOSEST_PLAYER);
return npc; return npc;
} }
} }

@ -53,6 +53,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
registerSerializer(new Vector3fPropertySerializer()); registerSerializer(new Vector3fPropertySerializer());
registerSerializer(new BlockStatePropertySerializer()); registerSerializer(new BlockStatePropertySerializer());
registerSerializer(new IntegerPropertySerializer()); registerSerializer(new IntegerPropertySerializer());
registerSerializer(new LookTypeSerializer());
registerEnumSerializer(NpcPose.class); registerEnumSerializer(NpcPose.class);
registerEnumSerializer(DyeColor.class); registerEnumSerializer(DyeColor.class);
@ -142,7 +143,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
register(new NameProperty(legacyNames, optionalComponents)); register(new NameProperty(legacyNames, optionalComponents));
register(new DinnerboneProperty(legacyNames, optionalComponents)); register(new DinnerboneProperty(legacyNames, optionalComponents));
register(new DummyProperty<>("look", false)); register(new DummyProperty<>("look", LookType.FIXED));
register(new GlowProperty(packetFactory)); register(new GlowProperty(packetFactory));
register(new BitsetProperty("fire", 0, 0x01)); register(new BitsetProperty("fire", 0, 0x01));
register(new BitsetProperty("invisible", 0, 0x20)); register(new BitsetProperty("invisible", 0, 0x20));

@ -60,6 +60,10 @@ public class PacketEntity implements PropertyHolder {
else packetFactory.spawnEntity(player, this, properties); else packetFactory.spawnEntity(player, this, properties);
} }
public void setHeadRotation(Player player, float yaw, float pitch) {
packetFactory.sendHeadRotation(player, this, yaw, pitch);
}
public void despawn(Player player) { public void despawn(Player player) {
packetFactory.destroyEntity(player, this, properties); packetFactory.destroyEntity(player, this, properties);
} }

@ -0,0 +1,26 @@
package lol.pyr.znpcsplus.entity.serializers;
import lol.pyr.znpcsplus.entity.PropertySerializer;
import lol.pyr.znpcsplus.util.LookType;
public class LookTypeSerializer implements PropertySerializer<LookType> {
@Override
public String serialize(LookType property) {
return property.name();
}
@Override
public LookType deserialize(String property) {
if (property.equals("true")) return LookType.CLOSEST_PLAYER;
try {
return LookType.valueOf(property);
} catch (IllegalArgumentException ignored) {
return LookType.FIXED;
}
}
@Override
public Class<LookType> getTypeClass() {
return LookType.class;
}
}

@ -83,6 +83,10 @@ public class NpcImpl extends Viewable implements Npc {
hologram.setLocation(location.withY(location.getY() + type.getHologramOffset())); hologram.setLocation(location.withY(location.getY() + type.getHologramOffset()));
} }
public void setHeadRotation(Player player, float yaw, float pitch) {
entity.setHeadRotation(player, yaw, pitch);
}
public HologramImpl getHologram() { public HologramImpl getHologram() {
return hologram; return hologram;
} }

@ -22,4 +22,5 @@ public interface PacketFactory {
void sendAllMetadata(Player player, PacketEntity entity, PropertyHolder properties); void sendAllMetadata(Player player, PacketEntity entity, PropertyHolder properties);
void sendEquipment(Player player, PacketEntity entity, Equipment equipment); void sendEquipment(Player player, PacketEntity entity, Equipment equipment);
void sendMetadata(Player player, PacketEntity entity, List<EntityData> data); void sendMetadata(Player player, PacketEntity entity, List<EntityData> data);
void sendHeadRotation(Player player, PacketEntity entity, float yaw, float pitch);
} }

@ -137,6 +137,12 @@ public class V1_8PacketFactory implements PacketFactory {
sendPacket(player, new WrapperPlayServerEntityMetadata(entity.getEntityId(), data)); sendPacket(player, new WrapperPlayServerEntityMetadata(entity.getEntityId(), data));
} }
@Override
public void sendHeadRotation(Player player, PacketEntity entity, float yaw, float pitch) {
sendPacket(player, new WrapperPlayServerEntityHeadLook(entity.getEntityId(),yaw));
sendPacket(player, new WrapperPlayServerEntityRotation(entity.getEntityId(), yaw, pitch, true));
}
@Override @Override
public void sendEquipment(Player player, PacketEntity entity, Equipment equipment) { public void sendEquipment(Player player, PacketEntity entity, Equipment equipment) {
sendPacket(player, new WrapperPlayServerEntityEquipment(entity.getEntityId(), Collections.singletonList(equipment))); sendPacket(player, new WrapperPlayServerEntityEquipment(entity.getEntityId(), Collections.singletonList(equipment)));

@ -8,6 +8,7 @@ import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
import lol.pyr.znpcsplus.util.LookType;
import lol.pyr.znpcsplus.util.NpcLocation; import lol.pyr.znpcsplus.util.NpcLocation;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -28,13 +29,14 @@ public class NpcProcessorTask extends BukkitRunnable {
public void run() { public void run() {
double distSq = NumberConversions.square(configManager.getConfig().viewDistance()); double distSq = NumberConversions.square(configManager.getConfig().viewDistance());
double lookPropertyDistSq = NumberConversions.square(configManager.getConfig().lookPropertyDistance()); double lookPropertyDistSq = NumberConversions.square(configManager.getConfig().lookPropertyDistance());
EntityPropertyImpl<Boolean> lookProperty = propertyRegistry.getByName("look", Boolean.class); EntityPropertyImpl<LookType> lookProperty = propertyRegistry.getByName("look", LookType.class);
for (NpcEntryImpl entry : npcRegistry.getProcessable()) { for (NpcEntryImpl entry : npcRegistry.getProcessable()) {
NpcImpl npc = entry.getNpc(); NpcImpl npc = entry.getNpc();
if (!npc.isEnabled()) continue; if (!npc.isEnabled()) continue;
double closestDist = Double.MAX_VALUE; double closestDist = Double.MAX_VALUE;
Player closest = null; Player closest = null;
LookType lookType = npc.getProperty(lookProperty);
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
if (!player.getWorld().equals(npc.getWorld())) { if (!player.getWorld().equals(npc.getWorld())) {
if (npc.isVisibleTo(player)) npc.hide(player); if (npc.isVisibleTo(player)) npc.hide(player);
@ -60,12 +62,18 @@ public class NpcProcessorTask extends BukkitRunnable {
closestDist = distance; closestDist = distance;
closest = player; closest = player;
} }
if (lookType.equals(LookType.PER_PLAYER) && lookPropertyDistSq >= distance) {
NpcLocation expected = npc.getLocation().lookingAt(player.getLocation().add(0, -npc.getType().getHologramOffset(), 0));
if (!expected.equals(npc.getLocation())) npc.setHeadRotation(player, expected.getYaw(), expected.getPitch());
}
} }
} }
// look property // look property
if (closest != null && npc.getProperty(lookProperty) && lookPropertyDistSq >= closestDist) { if (lookType.equals(LookType.CLOSEST_PLAYER)) {
NpcLocation expected = npc.getLocation().lookingAt(closest.getLocation().add(0, -npc.getType().getHologramOffset(), 0)); if (closest != null && lookPropertyDistSq >= closestDist) {
if (!expected.equals(npc.getLocation())) npc.setLocation(expected); NpcLocation expected = npc.getLocation().lookingAt(closest.getLocation().add(0, -npc.getType().getHologramOffset(), 0));
if (!expected.equals(npc.getLocation())) npc.setLocation(expected);
}
} }
} }
} }