Java Reference
In-Depth Information
XML or SQLite
In the server configuration file,
server/config/server.cfg
, you can choose the back end that
the
Database
will use for loading and saving. By default, it uses an XML format file,
which will be saved in the
server/db
directory.
You can change that to use any of
xml
,
mysql
, or
sqlite
by editing this line in the server
config file:
data-source=xml
For better performance, you can just change that option to
sqlite
without having to
do any additional configuration. To use the even higher performance
mysql
option,
you'd need to set it up using
server/config/db.cfg
and run a MySQL server.
Plugin: BackCmd with Save
Using the same ideas as with
LocationSnapshot
, you're going to add data storage
to the
BackCmd
plugin so that you can save the locations across server restarts.
I'll provide the outline and function signatures, but you will be writing the
bodies of the functions, the actual code to make it work. Buckle up!
Now, the
BackCmd
plugin is a little more complicated than our simple
Location-
Snapshot
. As you may remember from the plugin
on page 128
, the
BackCmd
plugin
tracks more than just a location per player; it tracks a
Stack
of
Location
objects
for each player.
Bear in mind that the
Database
system can't actually store
Location
objects. It
can store a list, which we can use as a stack, but it can only store a list of a
single basic type. It cannot store a list of separate x-, y-, and z-coordinates,
or a list of arrays. So we're going to cheat a bit: we'll store the coordinates in
a string when we write, and pull them back out from a string into separate
doubles when we read.
Here's your to-do list for the code you're going to write:
•Create a
SavedLocation
, which will be a
DataAccess
object.
•The main plugin deals with players, and will want to just add a new
Location
and retrieve the last location for a given player. So it makes sense to make
the
SavedLocation
look like a stack, with a
pop
and
push
method.
•Because
SavedLocation
will just look like a stack to the plugin, it will do the
database load and save functions internally (as private functions). You'll
need to write the private functions in
SavedLocation
that call the
Database
and check for any exceptions.