Skip to content

Commit

Permalink
Update displayitem
Browse files Browse the repository at this point in the history
Changed syntax and internal workings noticeably.

(Old version didn't work much anyway...)
  • Loading branch information
mcmonkey4eva committed Sep 21, 2013
1 parent 8a1a338 commit d5ecf6e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 64 deletions.
Expand Up @@ -519,11 +519,11 @@ public void registerCoreMembers() {

// <--[command]
// @Name DisplayItem
// @Usage displayitem (remove) [<item>] [<location>] (duration:<value>)
// @Usage displayitem [<item>] [<location>] (duration:<value>)
// @Required 2
// @Stable Todo
// @Short Makes a non-touchable item spawn for players to view.
// @Author Todo
// @Author aufdemrand, mcmonkey
// @Description
// Todo
// @Tags
Expand All @@ -534,7 +534,7 @@ public void registerCoreMembers() {
// Todo
// -->
registerCoreMember(DisplayItemCommand.class,
"DISPLAYITEM", "displayitem (remove) [<item>] [<location>] (duration:<value>)", 2);
"DISPLAYITEM", "displayitem [<item>] [<location>] (duration:<value>)", 2);

// <--[command]
// @Name Drop
Expand Down
Expand Up @@ -5,64 +5,51 @@

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.Duration;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import org.bukkit.Bukkit;
import org.bukkit.entity.Item;
import org.bukkit.scheduler.BukkitRunnable;

/**
* Displays an item in the world. This item will not disappear (unless set to)
* and cannot be picked up.
*
* @author aufdemrand
* @author aufdemrand, mcmonkey
*/

public class DisplayItemCommand extends AbstractCommand {

private enum Action { PLACE, REMOVE }

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

dItem item = null;
Action action = Action.PLACE;
Duration duration = null;
dLocation location = null;

// Make sure NPC is available
for (String arg : scriptEntry.getArguments()) {

if (aH.matchesDuration(arg))
duration = aH.getDurationFrom(arg);
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

else if (aH.matchesArg("REMOVE", arg))
action = Action.REMOVE;
if (arg.matchesArgumentType(Duration.class))
scriptEntry.addObject("duration", arg.asType(Duration.class));

else if (aH.matchesLocation(arg))
location = aH.getLocationFrom(arg);
else if (arg.matchesArgumentType(dLocation.class))
scriptEntry.addObject("location", arg.asType(dLocation.class));

else if (aH.getItemFrom(arg) != null)
item = aH.getItemFrom(arg);
else if (arg.matchesArgumentType(dItem.class))
scriptEntry.addObject("item", arg.asType(dItem.class));

else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg.raw_value);
}

// Check required args
if (item == null)
if (!scriptEntry.hasObject("item"))
throw new InvalidArgumentsException("Must specify an item to display.");

if (location == null)
if (!scriptEntry.hasObject("location"))
throw new InvalidArgumentsException(Messages.DEBUG_SET_LOCATION);

// Add objects to ScriptEntry for execution
scriptEntry.addObject("item", item)
.addObject("action", action)
.addObject("duration", duration)
.addObject("location", location);
if (!scriptEntry.hasObject("duration"))
scriptEntry.addObject("duration", Duration.valueOf("1m"));
}

@Override
Expand All @@ -72,41 +59,32 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dItem item = (dItem) scriptEntry.getObject("item");
Duration duration = (Duration) scriptEntry.getObject("duration");
dLocation location = (dLocation) scriptEntry.getObject("location");
Action action = (Action) scriptEntry.getObject("action");

// Report to dB
dB.report(getName(), aH.debugObj("Action", action.toString())
+ item.debug()
+ (duration != null ? duration.debug() : "")
dB.report(getName(),
item.debug()
+ duration.debug()
+ location.debug());

if (action == Action.PLACE) {

int ticks = Integer.MAX_VALUE;
if (duration != null) ticks = duration.getTicksAsInt();

// Display the item
if (displayed.containsKey(location.identify())) {
displayed.get(location.identify()).remove();
displayed.remove(location.identify());
}

// Remember the item entity
displayed.put(location.identify(), location.getBlock().getLocation().add(0, 1, 0).getWorld().dropItem(location, item.getItemStack()));
displayed.get(location.identify()).setPickupDelay(Integer.MAX_VALUE);
displayed.get(location.identify()).setTicksLived(ticks);
}

// Remove the item
else if (action == Action.REMOVE) {
if (displayed.containsKey(location.identify())) {
displayed.get(location.identify()).remove();
displayed.remove(location.identify());
}

}
// Drop the item
final Item dropped = location.getBlock().getLocation().add(0, 1, 0).getWorld().dropItem(location, item.getItemStack());
dropped.setPickupDelay(Integer.MAX_VALUE);
dropped.setTicksLived(Integer.MAX_VALUE);

// Remember the item entity
scriptEntry.addObject("dropped", new dEntity(dropped));

// Remove it later
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
@Override
public void run() {
if (dropped.isValid() && !dropped.isDead()) {
dropped.remove();
}
}
}, duration.getTicks());
}

public static Map<String, org.bukkit.entity.Item> displayed = new HashMap<String, org.bukkit.entity.Item>();

}

0 comments on commit d5ecf6e

Please sign in to comment.