Java Reference
In-Depth Information
loadlocations
Sometime later, when you type the
loadlocations
command, you'll run through
the list of players who are online and look up their saved location from the
database; convert the x-, y-, and z-coordinates back to a proper
Location
; and
teleport them there:
private void
loadLocations() {
//Go through list of players; if they are in the hash, teleport them.
List
<Player> playerList = Canary.getServer().getPlayerList();
for
(Player player : playerList) {
String
name = player.getDisplayName();
AllPlayerLocations apl =
new
AllPlayerLocations();
HashMap
<
String
,
Object
> search =
new HashMap
<
String
,
Object
>();
search.put(
"player_name"
, name);
try
{
Database.get().load(apl, search);
}
catch
(DatabaseReadException e) {
logger.info(name +
" is not online"
);
continue
;
}
// Reconstitute a Location from coordinates
Location loc =
new
Location(apl.x, apl.y, apl.z);
logger.info(
"Teleporting "
+ name +
" to "
+ printLoc(loc));
player.teleportTo(loc);
}
}
The “save” obviously needs to be done by going through the list of all players
online, and it's easiest to do the same thing for the load as well. We
could
start with a list of all the players we saved in the database, and then check
to see if they are online, but it's probably easier this way. In either case, there
may have been players who were online before and aren't now, or vice versa.
So all we have to do is find this player in the database, using a search map
again, and the
load
function. When we are ready to teleport the player, we
make a new
Location
object based on the coordinates we saved and off they go.
Compile and install
LocationSnapshot
and give it a try. Connect from your client
and run the
/savelocations
command.
Exit your client, stop the server, and restart everything. Go somewhere else
in the game and try the
/loadlocations
command. You're back at the snapshot
location (as is every other player on your server).