Java Reference
In-Depth Information
Plugin: ArrayAddMoreBlocks
Let's play with this a bit. We'll start with the
ArrayOfBlocks
plugin but change
it to use an
ArrayList
instead of an
Array
. We'll call the new plugin
ArrayAddMore-
Blocks
.
Since we can add to the array list easily, let's make it static:
public static List
<BlockType> towerMaterials =
new ArrayList
<BlockType>();
Then, thanks to the wonder of
ArrayList
, we can add a couple of blocks to the
new tower each time we call
/arrayaddmoreblocks
:
public void
buildTower(Player me) {
if
(towerLoc == null) {
towerLoc =
new
Location(me.getLocation());
❶
towerLoc.setX(towerLoc.getX() + 2);
// Not right on top of player
towerBase =
new
Location(towerLoc);
❷
}
towerMaterials.add(BlockType.Glass);
towerMaterials.add(BlockType.Stone);
towerMaterials.add(BlockType.OakWood);
for
(BlockType material : towerMaterials) {
logger.info(
"Building block at "
+ printLoc(towerLoc));
setBlockAt(towerLoc, material);
towerLoc.setY(towerLoc.getY() + 1);
// go up one each time
}
}
To reset the list, you use the
clear()
function, and I've set up a separate com-
mand to do just that:
public void
clearTower() {
if
(towerLoc == null) {
return
;
}
while
(towerBase.getY() < towerLoc.getY()) {
setBlockAt(towerBase, BlockType.Air);
logger.info(
"Clearing block at "
+ printLoc(towerBase));
towerBase.setY(towerBase.getY() + 1);
// go up one each time
}
towerLoc = null;
// Reset for next tower
towerBase = null;
towerMaterials.clear();
}