forked from unimatrix/web
Let staff assign registered skins to quest NPCs
This commit is contained in:
parent
897fc46489
commit
8f3d91aedc
3 changed files with 41 additions and 4 deletions
|
|
@ -107,6 +107,11 @@ public final class DuneGUI extends JavaPlugin implements Listener,CommandExecuto
|
|||
if(args.length!=1||!HOUSE_IDS.contains(id)){p.sendMessage("Use /house <"+String.join("|",IDS)+">");return true;}
|
||||
fetch(p,null,0,null,data->declareHouse(p,data,id,false));return true;
|
||||
}
|
||||
if(command.getName().equals("npc")){
|
||||
if(args.length==2&&args[0].equalsIgnoreCase("skin"))polQuestNpcs.assignSkin(p,args[1]);
|
||||
else p.sendMessage("Look at a quest NPC and use /npc skin #id.");
|
||||
return true;
|
||||
}
|
||||
if(command.getName().equals("decisions")){quests.openDecisions(p,0);return true;}
|
||||
if(command.getName().equals("rosterkey")){if(args.length!=1)p.sendMessage("Use /rosterkey <team invite code>");else warMenus.accept(p,args[0]);return true;}
|
||||
if(command.getName().equals("warmuster")){warMenus.open(p);return true;}
|
||||
|
|
|
|||
|
|
@ -50,6 +50,11 @@ final class PolQuestNpc implements Listener {
|
|||
new Role("edric","Edric · Guild logistics","guild_lobby","*",Set.of("guild"),-3),
|
||||
new Role("esmar_tuek","Esmar Tuek · Smuggler contact","arrakis","*",Set.of("smugglers","spice-upgrading","elytra"),-18),
|
||||
new Role("esmar_tuek","Esmar Tuek · Smuggler contact","guild_lobby","*",Set.of("smugglers"),-6));
|
||||
private static final Set<String> SKINS=Set.of(
|
||||
"alia_atreides","chani","duncan_idaho","edric","esmar_tuek","gaius_helen_mohiam",
|
||||
"glossu_rabban","hasimir_fenring","imperial_attendant","irulan_corrino","jessica_atreides",
|
||||
"liet_kynes","piter_de_vries","roma_atani","shadout_mapes","staban_tuek","stilgar","thufir_hawat");
|
||||
private static final NamespacedKey SKIN=new NamespacedKey("dunerealms","quest_character_skin");
|
||||
static final NamespacedKey KEY=new NamespacedKey("dunerealms","quest_character");
|
||||
private final JavaPlugin plugin;private final BiConsumer<Player,Role> open;private final File file;private final YamlConfiguration positions;
|
||||
private UUID imperialUuid;private String imperialName,imperialTerm;private long imperialRetry;
|
||||
|
|
@ -58,7 +63,7 @@ final class PolQuestNpc implements Listener {
|
|||
private static Role role(String id,World world){return ROLES.stream().filter(r->r.key().equals(id)&&r.world().equals(world.getName())).findFirst().orElse(null);}
|
||||
private String base(Role role){return role.world()+"."+role.key();}
|
||||
private Location saved(Role role,World world){String b=base(role);return positions.contains(b+".x")?new Location(world,positions.getDouble(b+".x"),positions.getDouble(b+".y"),positions.getDouble(b+".z"),(float)positions.getDouble(b+".yaw"),(float)positions.getDouble(b+".pitch")):null;}
|
||||
private void save(Role role,LivingEntity npc){Location l=npc.getLocation();String b=base(role);positions.set(b+".entity",npc.getUniqueId().toString());positions.set(b+".x",l.getX());positions.set(b+".y",l.getY());positions.set(b+".z",l.getZ());positions.set(b+".yaw",l.getYaw());positions.set(b+".pitch",l.getPitch());try{Path target=file.toPath(),directory=target.getParent();Files.createDirectories(directory);Path temporary=Files.createTempFile(directory,"quest-npc-positions-",".yml");try{positions.save(temporary.toFile());try{Files.move(temporary,target,StandardCopyOption.ATOMIC_MOVE,StandardCopyOption.REPLACE_EXISTING);}catch(AtomicMoveNotSupportedException unsupported){Files.move(temporary,target,StandardCopyOption.REPLACE_EXISTING);}}finally{Files.deleteIfExists(temporary);}}catch(java.io.IOException failure){plugin.getLogger().warning("Quest NPC position could not be saved: "+role.key());}}
|
||||
private boolean save(Role role,LivingEntity npc){Location l=npc.getLocation();String b=base(role);positions.set(b+".entity",npc.getUniqueId().toString());positions.set(b+".x",l.getX());positions.set(b+".y",l.getY());positions.set(b+".z",l.getZ());positions.set(b+".yaw",l.getYaw());positions.set(b+".pitch",l.getPitch());try{Path target=file.toPath(),directory=target.getParent();Files.createDirectories(directory);Path temporary=Files.createTempFile(directory,"quest-npc-positions-",".yml");try{positions.save(temporary.toFile());try{Files.move(temporary,target,StandardCopyOption.ATOMIC_MOVE,StandardCopyOption.REPLACE_EXISTING);}catch(AtomicMoveNotSupportedException unsupported){Files.move(temporary,target,StandardCopyOption.REPLACE_EXISTING);}}finally{Files.deleteIfExists(temporary);}return true;}catch(java.io.IOException failure){plugin.getLogger().warning("Quest NPC settings could not be saved: "+role.key());return false;}}
|
||||
private LivingEntity existing(Role role,World world){String id=positions.getString(base(role)+".entity");if(id!=null)try{Entity e=Bukkit.getEntity(UUID.fromString(id));if(e instanceof LivingEntity living&&role.key().equals(e.getPersistentDataContainer().get(KEY,PersistentDataType.STRING)))return living;}catch(IllegalArgumentException ignored){}return world.getEntitiesByClass(LivingEntity.class).stream().filter(e->role.key().equals(e.getPersistentDataContainer().get(KEY,PersistentDataType.STRING))).findFirst().orElse(null);}
|
||||
private static boolean usable(Location at){return at.getBlock().isPassable()&&at.clone().add(0,1,0).getBlock().isPassable()&&at.clone().add(0,-1,0).getBlock().getType().isSolid();}
|
||||
private static Location initial(Role role,World world){Location spawn=world.getSpawnLocation();for(int distance:new int[]{role.offset(),-role.offset(),role.offset()-2,role.offset()+2}){Location at=spawn.clone().add(distance,0,0);if(usable(at))return at;}return null;}
|
||||
|
|
@ -66,11 +71,34 @@ final class PolQuestNpc implements Listener {
|
|||
private static Class<?> profileType()throws ClassNotFoundException{return Class.forName("io.papermc.paper.datacomponent.item.ResolvableProfile");}
|
||||
private static Object skin(String role)throws ReflectiveOperationException{Class<?> profile=profileType(),builder=Class.forName(profile.getName()+"$Builder"),patch=Class.forName(profile.getName()+"$SkinPatch"),patchBuilder=Class.forName(profile.getName()+"$SkinPatchBuilder");Object b=profile.getMethod("resolvableProfile").invoke(null),p=patch.getMethod("skinPatch").invoke(null);patchBuilder.getMethod("body",Key.class).invoke(p,Key.key("dunerealms","textures/entity/npc/"+role+".png"));patchBuilder.getMethod("model",org.bukkit.profile.PlayerTextures.SkinModel.class).invoke(p,org.bukkit.profile.PlayerTextures.SkinModel.CLASSIC);Object built=patchBuilder.getMethod("build").invoke(p);builder.getMethod("name",String.class).invoke(b,"DRQuestContact");builder.getMethod("skinPatch",patch).invoke(b,built);return builder.getMethod("build").invoke(b);}
|
||||
private static void profile(LivingEntity npc,Object value)throws ReflectiveOperationException{Class.forName("org.bukkit.entity.Mannequin").getMethod("setProfile",profileType()).invoke(npc,value);}
|
||||
@SuppressWarnings("unchecked") private LivingEntity spawn(Role role,Location at){try{Class<? extends LivingEntity> type=(Class<? extends LivingEntity>)Class.forName("org.bukkit.entity.Mannequin");Object appearance=skin(role.key().equals("war_marshal")?"duncan_idaho":role.key());LivingEntity npc=at.getWorld().spawn(at,type,e->configure(role,e));try{profile(npc,appearance);return npc;}catch(ReflectiveOperationException failure){npc.remove();throw failure;}}catch(ReflectiveOperationException|LinkageError|IllegalArgumentException unavailable){plugin.getLogger().warning("Character appearance is unavailable for "+role.key()+"; using neutral stationary fallback.");return at.getWorld().spawn(at,Villager.class,v->{v.setProfession(Villager.Profession.LIBRARIAN);configure(role,v);});}}
|
||||
@SuppressWarnings("unchecked") private LivingEntity spawn(Role role,Location at){try{Class<? extends LivingEntity> type=(Class<? extends LivingEntity>)Class.forName("org.bukkit.entity.Mannequin");String override=positions.getString(base(role)+".skin");Object appearance=skin(SKINS.contains(override==null?"":override)?override:role.key().equals("war_marshal")?"duncan_idaho":role.key());LivingEntity npc=at.getWorld().spawn(at,type,e->configure(role,e));try{profile(npc,appearance);if(override!=null&&SKINS.contains(override))npc.getPersistentDataContainer().set(SKIN,PersistentDataType.STRING,override);return npc;}catch(ReflectiveOperationException failure){npc.remove();throw failure;}}catch(ReflectiveOperationException|LinkageError|IllegalArgumentException unavailable){plugin.getLogger().warning("Character appearance is unavailable for "+role.key()+"; using neutral stationary fallback.");return at.getWorld().spawn(at,Villager.class,v->{v.setProfession(Villager.Profession.LIBRARIAN);configure(role,v);});}}
|
||||
private void ensure(World world){for(Role role:ROLES){if(!role.world().equals(world.getName()))continue;Location at=saved(role,world);if(at==null)at=initial(role,world);if(at==null){plugin.getLogger().warning("No safe spawn-space for quest NPC "+role.key());continue;}at.getChunk().load();LivingEntity npc=existing(role,world);if(npc==null){if(!usable(at)){plugin.getLogger().warning("Saved quest NPC location obstructed: "+role.key());continue;}npc=spawn(role,at);}configure(role,npc);save(role,npc);if(role.key().equals("imperial_attendant"))applyImperial(npc);}}
|
||||
void imperialAppearance(UUID uuid,String name,String term){boolean changed=!Objects.equals(imperialTerm,term)||!Objects.equals(imperialUuid,uuid);imperialUuid=uuid;imperialName=name;imperialTerm=term;if(!changed&&System.currentTimeMillis()<imperialRetry)return;World world=Bukkit.getWorld("kaitain");if(world==null)return;Role role=role("imperial_attendant",world);Location at=saved(role,world);if(at!=null)at.getChunk().load();LivingEntity e=existing(role,world);if(e!=null)applyImperial(e);}
|
||||
private void applyImperial(LivingEntity npc){String term=imperialTerm;UUID uuid=imperialUuid;String name=imperialName;imperialRetry=System.currentTimeMillis()+600000;try{profile(npc,skin("imperial_attendant"));}catch(ReflectiveOperationException ignored){}npc.customName(Component.text("✦ Imperial attendant",NamedTextColor.AQUA));if(uuid==null||name==null)return;
|
||||
Bukkit.createProfile(uuid).update().whenComplete((publicProfile,error)->{if(!plugin.isEnabled())return;Bukkit.getScheduler().runTask(plugin,()->{if(error!=null||publicProfile==null||!publicProfile.hasTextures()||!npc.isValid()||!Objects.equals(term,imperialTerm)||!Objects.equals(uuid,imperialUuid))return;try{Class<?> pp=Class.forName("com.destroystokyo.paper.profile.PlayerProfile");Object resolved=profileType().getMethod("resolvableProfile",pp).invoke(null,publicProfile);profile(npc,resolved);npc.customName(Component.text("✦ Imperial audience · "+name+" (representation)",NamedTextColor.AQUA));}catch(ReflectiveOperationException ignored){/* Keep the neutral attendant. */}});});
|
||||
private void applyImperial(LivingEntity npc){if(npc.getPersistentDataContainer().has(SKIN))return;String term=imperialTerm;UUID uuid=imperialUuid;String name=imperialName;imperialRetry=System.currentTimeMillis()+600000;try{profile(npc,skin("imperial_attendant"));}catch(ReflectiveOperationException ignored){}npc.customName(Component.text("✦ Imperial attendant",NamedTextColor.AQUA));if(uuid==null||name==null)return;
|
||||
Bukkit.createProfile(uuid).update().whenComplete((publicProfile,error)->{if(!plugin.isEnabled())return;Bukkit.getScheduler().runTask(plugin,()->{if(error!=null||publicProfile==null||!publicProfile.hasTextures()||!npc.isValid()||npc.getPersistentDataContainer().has(SKIN)||!Objects.equals(term,imperialTerm)||!Objects.equals(uuid,imperialUuid))return;try{Class<?> pp=Class.forName("com.destroystokyo.paper.profile.PlayerProfile");Object resolved=profileType().getMethod("resolvableProfile",pp).invoke(null,publicProfile);profile(npc,resolved);npc.customName(Component.text("✦ Imperial audience · "+name+" (representation)",NamedTextColor.AQUA));}catch(ReflectiveOperationException ignored){/* Keep the neutral attendant. */}});});
|
||||
}
|
||||
void assignSkin(Player player,String value){
|
||||
if(!player.hasPermission("dune.admin")){player.sendMessage("Current NPC administration permission required.");return;}
|
||||
String id=value.startsWith("#")?value.substring(1).toLowerCase(Locale.ROOT):"";
|
||||
if(!SKINS.contains(id)){player.sendMessage("Available skins: #"+String.join(", #",new TreeSet<>(SKINS)));return;}
|
||||
if(!(player.getTargetEntity(6) instanceof LivingEntity npc)){player.sendMessage("Look at a quest NPC within six blocks.");return;}
|
||||
String roleKey=npc.getPersistentDataContainer().get(KEY,PersistentDataType.STRING);
|
||||
Role role=roleKey==null?null:role(roleKey,player.getWorld());
|
||||
if(role==null||existing(role,player.getWorld())!=npc){player.sendMessage("Look at a registered quest NPC.");return;}
|
||||
String key=base(role)+".skin",previous=positions.getString(key);
|
||||
try{
|
||||
Object original=npc.getClass().getMethod("getProfile").invoke(npc);
|
||||
profile(npc,skin(id));
|
||||
positions.set(key,id);
|
||||
if(!save(role,npc)){
|
||||
positions.set(key,previous);profile(npc,original);
|
||||
player.sendMessage("The skin could not be saved.");return;
|
||||
}
|
||||
npc.getPersistentDataContainer().set(SKIN,PersistentDataType.STRING,id);
|
||||
player.sendMessage("Equipped #"+id+" on "+role.name()+".");
|
||||
}catch(ReflectiveOperationException|LinkageError unavailable){
|
||||
player.sendMessage("This NPC does not support custom skins on this server.");
|
||||
}
|
||||
}
|
||||
boolean move(Player player,String key){if(!player.hasPermission("dune.admin")){player.sendMessage("Current NPC administration permission required.");return false;}Role role=role(key,player.getWorld());if(role==null){player.sendMessage("Choose a quest character assigned to this world.");return false;}Location at=player.getLocation();if(!usable(at)){player.sendMessage("Choose a clear standing space for the NPC.");return false;}Location old=saved(role,player.getWorld());if(old!=null)old.getChunk().load();LivingEntity npc=existing(role,player.getWorld());if(npc==null){player.sendMessage("The original quest NPC is unavailable; no duplicate was created.");return false;}if(!npc.teleport(at))return false;save(role,npc);player.sendMessage("Moved "+role.name()+"; its original identity was retained.");return true;}
|
||||
@EventHandler public void world(WorldLoadEvent e){Bukkit.getScheduler().runTaskLater(plugin,()->ensure(e.getWorld()),100);}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,10 @@ api-version: '1.21.8'
|
|||
depend: [JarvisTranslator]
|
||||
softdepend: [MCGUI, DecentHolograms, DuneLobby, DuneCore, DuneEss, DuneNPC, Vault, floodgate, ViaVersion]
|
||||
commands:
|
||||
npc:
|
||||
description: Update the quest NPC you are looking at
|
||||
usage: /npc skin #id
|
||||
permission: dune.admin
|
||||
decisions:
|
||||
description: List the POL decisions available to your current offices
|
||||
rosterkey:
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff