Skip to content

Commit

Permalink
Filter out invalid actions for Inventory command.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Oct 20, 2013
1 parent 5a42c0b commit e5549e6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dList.java
Expand Up @@ -161,10 +161,27 @@ public String[] toArray() {
return list.toArray(new String[list.size()]);
}

// Return a list that includes only strings that match the values
// of an Enum array
public List<String> filter(Enum[] values) {
List<String> list = new ArrayList<String>();

for (String string : this) {
for (Enum value : values)
if (value.name().equalsIgnoreCase(string))
list.add(string);
}

if (!list.isEmpty())
return list;
else return null;
}

// Return a list that includes only elements belonging to a certain class
public List<dObject> filter(Class<? extends dObject> dClass) {
return filter(dClass, null);
}

public List<dObject> filter(Class<? extends dObject> dClass, ScriptEntry entry) {

List<dObject> results = new ArrayList<dObject>();
Expand Down
@@ -1,6 +1,6 @@
package net.aufdemrand.denizen.scripts.commands.item;

import org.bukkit.Bukkit;
import java.util.List;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
Expand Down Expand Up @@ -29,7 +29,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException

// Check for a dList of actions
if (arg.matchesEnumList(Action.values())) {
scriptEntry.addObject("actions", arg.asType(dList.class));
scriptEntry.addObject("actions", ((dList) arg.asType(dList.class)).filter(Action.values()));
}

// Check for an origin, which can be a dInventory, dEntity, dLocation
Expand Down Expand Up @@ -60,19 +60,19 @@ else if (!scriptEntry.hasObject("destination")
scriptEntry.hasPlayer() ? scriptEntry.getPlayer().getDenizenEntity().getInventory() : null);
}

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

// Get objects
dList actions = (dList) scriptEntry.getObject("actions");

List<String> actions = (List<String>) scriptEntry.getObject("actions");
dInventory origin = (dInventory) scriptEntry.getObject("origin");
dInventory destination = (dInventory) scriptEntry.getObject("destination");

dB.report(getName(),
aH.debugObj("actions", actions.toString()) +
destination.debug()
+ (origin != null ? origin.debug() : "")
+ actions.debug());
+ (origin != null ? origin.debug() : ""));

for (String action : actions) {
switch (Action.valueOf(action.toUpperCase())) {
Expand Down

0 comments on commit e5549e6

Please sign in to comment.