diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java index b08e90e8df..e50dda3889 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java @@ -519,11 +519,11 @@ public void registerCoreMembers() { // <--[command] // @Name DisplayItem - // @Usage displayitem (remove) [] [] (duration:) + // @Usage displayitem [] [] (duration:) // @Required 2 // @Stable Todo // @Short Makes a non-touchable item spawn for players to view. - // @Author Todo + // @Author aufdemrand, mcmonkey // @Description // Todo // @Tags @@ -534,7 +534,7 @@ public void registerCoreMembers() { // Todo // --> registerCoreMember(DisplayItemCommand.class, - "DISPLAYITEM", "displayitem (remove) [] [] (duration:)", 2); + "DISPLAYITEM", "displayitem [] [] (duration:)", 2); // <--[command] // @Name Drop diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/item/DisplayItemCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/item/DisplayItemCommand.java index 54e0ed0fe8..e86828ac19 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/item/DisplayItemCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/item/DisplayItemCommand.java @@ -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 @@ -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 displayed = new HashMap(); }