Persist contact positions atomically and gate Home Block relocation acknowledgement on NPC cleanup
This commit is contained in:
parent
28e196d068
commit
476ca452ae
1 changed files with 5 additions and 3 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package com.dunerealms.gui;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import org.bukkit.*;
|
||||
|
|
@ -45,7 +46,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{positions.save(file);}catch(java.io.IOException failure){plugin.getLogger().warning("Quest NPC position could not be saved: "+role.key());}}
|
||||
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 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;}
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff