Skip to content

Commit

Permalink
Add Sign command. Example usage: - sign location:<anchor:Sign> "Oooh|…
Browse files Browse the repository at this point in the history
…aaaah|eeeeh|hmmmm"
  • Loading branch information
davidcernat committed Jul 1, 2013
1 parent 01fc4a3 commit 99070f7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 24 deletions.
36 changes: 13 additions & 23 deletions src/main/java/net/aufdemrand/denizen/objects/dInventory.java
Expand Up @@ -76,7 +76,14 @@ public Inventory getInventory() {
return inventory;
}


/**
* Adds an array of items to this inventory,
* and return the result
*
* @param items The array of items
* @return The resulting dInventory
*
*/

public dInventory add(ItemStack[] items) {

Expand Down Expand Up @@ -137,32 +144,15 @@ public int count(ItemStack item, boolean stacks)

public void replace(dInventory destination) {

if (this == null || destination == null) return;

// If the destination is smaller than our current inventory,
// remove empty stacks from our current inventory in the hope
// that there will then be enough room
// add as many items as possible

if (destination.getSize() < this.getSize()) {

List<ItemStack> itemList = new ArrayList<ItemStack>();

for (ItemStack item : this.getContents()) {

if (item != null) itemList.add(item);
}

// If there is still not enough room, crop our list of items
// so it fits

if (destination.getSize() < itemList.size()) {

itemList = itemList.subList(0, destination.getSize());
}

// Set the contents of the destination to our modified
// item list

ItemStack[] results = itemList.toArray(new ItemStack[itemList.size()]);
destination.setContents(results);
destination.clear();
destination.add(this.getContents());
}
else {

Expand Down
Expand Up @@ -243,11 +243,14 @@ public void registerCoreMembers() {
registerCoreMember(ShootCommand.class,
"SHOOT", "shoot [projectile(s):<entity>|...] (origin:p@<name>/n@<id>) (destination:<x,y,z,world>) (script:<name>)", 1);

registerCoreMember(SignCommand.class,
"SIGN", "sign [\"<text>|<text>|<text>|<text>\"] [location:<x,y,z,world>]", 1);

registerCoreMember(SitCommand.class,
"SIT", "sit (location:x,y,z,world)", 0);

registerCoreMember(SpawnCommand.class,
"SPAWN", "spawn [entity:name] (location:x,y,z,world) (target:[n@#]|[p@name])", 1);
"SPAWN", "spawn [entity:name] (location:<x,y,z,world>) (target:[n@#]|[p@name])", 1);

registerCoreMember(StandCommand.class,
"STAND", "stand", 0);
Expand Down
Expand Up @@ -159,6 +159,12 @@ else if (destinationEntity != null) {
case CLEAR:
destination.clear();
return;

case REMOVE:
return;

default:
return;

}

Expand Down
@@ -0,0 +1,68 @@
package net.aufdemrand.denizen.scripts.commands.world;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;

/**
* Creates a sign with a certain text at a location.
*
* @author David Cernat
*/

public class SignCommand extends AbstractCommand {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class))
// Location arg
scriptEntry.addObject("location", arg.asType(dLocation.class).setPrefix("location"));

else if (!scriptEntry.hasObject("text")
&& arg.matchesArgumentType(dList.class))
// add text
scriptEntry.addObject("text", arg.asType(dList.class));
}

// Check to make sure required arguments have been filled

if (!scriptEntry.hasObject("location"))
throw new InvalidArgumentsException("Must specify a Sign location!");
}

@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {

// Get objects
dList text = (dList) scriptEntry.getObject("text");
dLocation location = (dLocation) scriptEntry.getObject("location");

Block sign = location.getBlock();
sign.setType(Material.valueOf("SIGN_POST"));
BlockState signState = sign.getState();

int n = 0;

for (String line : text) {

((Sign) signState).setLine(n, line);
n++;
}

signState.update();
}
}

0 comments on commit 99070f7

Please sign in to comment.