|
|
@@ -1,39 +1,20 @@
|
|
|
-package me.lethunderhawk.npc.abstraction;
|
|
|
+package me.lethunderhawk.fluxapi.npc.abstraction;
|
|
|
|
|
|
import com.destroystokyo.paper.profile.ProfileProperty;
|
|
|
import io.papermc.paper.datacomponent.item.ResolvableProfile;
|
|
|
-import me.lethunderhawk.fluxapi.FluxService;
|
|
|
-import me.lethunderhawk.npc.manager.NPCManager;
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
+import org.bukkit.Bukkit;
|
|
|
import org.bukkit.Location;
|
|
|
+import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|
|
import org.bukkit.entity.*;
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
-public abstract class NPC{
|
|
|
+public abstract class NPC implements ConfigurationSerializable {
|
|
|
private final NPCOptions npcOptions;
|
|
|
- private final Entity entity;
|
|
|
|
|
|
public NPC(NPCOptions npcOptions) {
|
|
|
- this.entity = npcOptions.getLocation().getWorld().spawnEntity(npcOptions.getLocation(), npcOptions.getEntityType());
|
|
|
this.npcOptions = npcOptions;
|
|
|
- if(npcOptions.getEntityType() == EntityType.MANNEQUIN){
|
|
|
- Mannequin mannequin = (Mannequin) entity;
|
|
|
-
|
|
|
- ProfileProperty skin = new ProfileProperty("textures", npcOptions.getTexture(), npcOptions.getTextureSignature());
|
|
|
- ResolvableProfile profile = ResolvableProfile.resolvableProfile().addProperty(skin).build();
|
|
|
-
|
|
|
- mannequin.setProfile(profile);
|
|
|
- mannequin.setDescription(npcOptions.getDescription());
|
|
|
- }
|
|
|
- entity.customName(Component.text(npcOptions.getName()));
|
|
|
- entity.setCustomNameVisible(npcOptions.isNameVisible());
|
|
|
- entity.setPersistent(true);
|
|
|
- entity.setGravity(false);
|
|
|
- entity.setNoPhysics(true);
|
|
|
- if(entity instanceof LivingEntity livingEntity){
|
|
|
- livingEntity.setAI(false);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
public abstract void onLeftClick(Player player);
|
|
|
@@ -48,20 +29,20 @@ public abstract class NPC{
|
|
|
onRightClick(player);
|
|
|
}
|
|
|
|
|
|
- public Entity getEntity() {
|
|
|
- return entity;
|
|
|
- }
|
|
|
-
|
|
|
public String getName() {
|
|
|
return npcOptions.getName();
|
|
|
}
|
|
|
|
|
|
- public void delete() {
|
|
|
- entity.remove();
|
|
|
+ public void deleteEntity() {
|
|
|
+ Entity entity = npcOptions.getEntity();
|
|
|
+ if(entity != null) {
|
|
|
+ entity.remove();
|
|
|
+ npcOptions.setEntityUUID(null);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public UUID getUUID() {
|
|
|
- return entity.getUniqueId();
|
|
|
+ public UUID getNpcUUID() {
|
|
|
+ return npcOptions.getNpcUUID();
|
|
|
}
|
|
|
|
|
|
public NPCOptions getOptions() {
|
|
|
@@ -70,20 +51,29 @@ public abstract class NPC{
|
|
|
|
|
|
public void rename(String newName) {
|
|
|
npcOptions.setName(newName);
|
|
|
- entity.customName(Component.text(newName));
|
|
|
- }
|
|
|
- public void register(){
|
|
|
- FluxService.get(NPCManager.class).registerNPC(this);
|
|
|
+ npcOptions.getEntity().customName(Component.text(newName));
|
|
|
}
|
|
|
|
|
|
public void lookAt(Location target) {
|
|
|
- if (target == null || this.entity == null || !(entity instanceof LivingEntity livingEntity)) return;
|
|
|
+ Entity entity = npcOptions.getEntity();
|
|
|
+ entity = Bukkit.getEntity(entity.getUniqueId());
|
|
|
+ if (target == null || entity == null) {
|
|
|
+ Bukkit.getLogger().info("HEY I SHOULD LOOK!!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- Location npcLoc = this.entity.getLocation();
|
|
|
+ Location npcLoc = entity.getLocation();
|
|
|
|
|
|
// Use eye height for more natural look
|
|
|
+
|
|
|
double dx = target.getX() - npcLoc.getX();
|
|
|
- double dy = target.getY() - (npcLoc.getY() + livingEntity.getEyeHeight());
|
|
|
+ double dy;
|
|
|
+ if(entity instanceof LivingEntity livingEntity) {
|
|
|
+ dy = target.getY() - (npcLoc.getY() + livingEntity.getEyeHeight());
|
|
|
+ }else{
|
|
|
+ dy = target.getY() - npcLoc.getY();
|
|
|
+ }
|
|
|
+
|
|
|
double dz = target.getZ() - npcLoc.getZ();
|
|
|
|
|
|
// Prevent division by zero
|
|
|
@@ -97,7 +87,7 @@ public abstract class NPC{
|
|
|
// Normalize yaw to Minecraft format
|
|
|
yaw = normalizeYaw(yaw);
|
|
|
|
|
|
- this.entity.setRotation(yaw, pitch);
|
|
|
+ entity.setRotation(yaw, pitch);
|
|
|
}
|
|
|
private float normalizeYaw(float yaw) {
|
|
|
yaw %= 360.0F;
|
|
|
@@ -105,4 +95,39 @@ public abstract class NPC{
|
|
|
if (yaw < -180.0F) yaw += 360.0F;
|
|
|
return yaw;
|
|
|
}
|
|
|
+
|
|
|
+ public Entity spawn(){
|
|
|
+ Entity entity = npcOptions.getLocation().getWorld().spawnEntity(npcOptions.getLocation(), npcOptions.getEntityType());
|
|
|
+
|
|
|
+ if(npcOptions.getEntityType() == EntityType.MANNEQUIN){
|
|
|
+ Mannequin mannequin = (Mannequin) entity;
|
|
|
+
|
|
|
+ ProfileProperty skin = new ProfileProperty("textures", npcOptions.getTexture(), npcOptions.getTextureSignature());
|
|
|
+ ResolvableProfile profile = ResolvableProfile.resolvableProfile().addProperty(skin).build();
|
|
|
+
|
|
|
+ mannequin.setProfile(profile);
|
|
|
+ if(npcOptions.getDescription() != null && !npcOptions.getDescription().isEmpty()){
|
|
|
+ mannequin.setDescription(Component.text(npcOptions.getDescription()));
|
|
|
+ }else{
|
|
|
+ mannequin.setDescription(null);
|
|
|
+ }
|
|
|
+ mannequin.setImmovable(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ entity.customName(Component.text(npcOptions.getName()));
|
|
|
+ entity.setCustomNameVisible(npcOptions.isNameVisible());
|
|
|
+ entity.setGravity(false);
|
|
|
+
|
|
|
+ if(entity instanceof LivingEntity livingEntity){
|
|
|
+ livingEntity.setAI(false);
|
|
|
+ }
|
|
|
+ npcOptions.setEntityUUID(entity.getUniqueId());
|
|
|
+ Bukkit.getLogger().info("Spawned entity UUID: " + entity.getUniqueId());
|
|
|
+ return entity;
|
|
|
+ }
|
|
|
+
|
|
|
+ public UUID getEntityUUID() {
|
|
|
+ return npcOptions.getEntityUUID();
|
|
|
+ }
|
|
|
+
|
|
|
}
|