Java Reference
In-Depth Information
Act Like a Stack
Even though this is a DataAccess object, we are free to add other functions and
bits of data to it to help make building our plugin easier. So from the perspec-
tive of the main plugin, we'd like to be able to treat a SavedLocation for a given
player as a stack of Location s. That means we'll need to expose at least a public
push and pop function.
Here's the first hitch: location_strings is declared as an ArrayList<String> , not a Stack .
So how would you implement push and pop using just a plain old list?
Well, the push is easy: an array add method will add a new item to the end of
the list. So you just use that for the push.
In order to pop the stack, you have to remove the last item in the list using its
index, the size() of the list minus one.
Okay, that sounds reasonable. In the public push functions, you'll need to
read this player's stack from the database; call an internal, private function
to actually push that location onto the stack; then save it back to the database.
pop will be similar: read from the database, pop the stack, and write the new
stack back to the database. If you remember back in the BackCmd plugin, it
wasn't just enough to pop the latest value—we had to use an equalsIsh function
to make sure we'd moved far enough away from the teleport location.
Putting it all together, the public functions in SavedLocation would look like this:
BackCmdSave/src/backcmdsave/SavedLocation.java
public void push(Location loc) {
myRead(player_name);
//Make sure previous location is different if it exists
if (peek_stack() == null ||
!equalsIsh(peek_stack(), loc)) {
push_stack(loc);
myWrite();
}
}
public Location pop(Location here) {
myRead(player_name);
if (location_strings.size() == 0) {
return null;
}
Location loc = pop_stack();
myWrite();
return loc;
}
 
 
 
Search WWH ::




Custom Search