To save teleportation points on command, I have an HashMap:
public HashMap<Player, Location> mapHomes = new HashMap<>();
Which is accessed like this:
if(cmd.getName().equalsIgnoreCase("sethome")){
Location loc = player.getLocation();
mapHomes.put(player, loc);
sender.sendMessage("Home set !");
return true;
}
if(cmd.getName().equalsIgnoreCase("home")){
Location loc1 = mapHomes.get(player);
player.teleport(loc1);
sender.sendMessage("Teleported to home");
return true;
}
return false;
Since these settings should be kept on restart, I’ve implemented a save method:
public void save(HashMap<Player,Location> mapHome, String path) throws NotSerializableException{
try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
oos.writeObject(mapHome);
oos.flush();
oos.close();
}catch(Exception e){
e.printStackTrace();
}
}
But it’s not working. It throws NotSerializableException.
I think the main problem is Player and Location are not serializable types, so what should I do to write this HashMap?
HashMapis alreadySerializable.The problem is that the objects inside the map are not, so you’ll have to make them serializable, too.
When adding to the map: