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 :
ArrayAddMoreBlocks/src/arrayaddmoreblocks/ArrayAddMoreBlocks.java
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:
ArrayAddMoreBlocks/src/arrayaddmoreblocks/ArrayAddMoreBlocks.java
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();
}
 
 
 
Search WWH ::




Custom Search