Skip to content

Commit

Permalink
Let you set dItem quantities using brackets. Example usage: - determi…
Browse files Browse the repository at this point in the history
…ne "drops:stone[20]|Magical Scroll[3]"
  • Loading branch information
davidcernat committed Jul 13, 2013
1 parent fc46b33 commit 672cb6e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 39 deletions.
124 changes: 86 additions & 38 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -22,7 +22,19 @@

public class dItem implements dObject {

final static Pattern itemPattern = Pattern.compile("(?:item:)?(\\w+)[:,]?(\\d+)?", Pattern.CASE_INSENSITIVE);
// An item pattern with the following groups:
//
// 1) An optional item: prefix.
// 2) Word characters (letters and digits) and
// spaces that specify the name or ID of the item
// 3) Digits that specify the special data value
// of the item
// 4) Digits between [] brackets that specify the
// quantity of the item

final static Pattern itemPattern =
Pattern.compile("(?:item:)?([\\w ]+)[:,]?(\\d+)?\\[?(\\d+)?\\]?",

This comment has been minimized.

Copy link
@aufdemrand

aufdemrand Jul 15, 2013

Contributor

Can we leave out the 'prefix'? No other dObject has this anymore since the new Argument object will split them automatically. Plus, it's ugly and unnecessary. No command should require the item: prefix once all commands are converted to 0.9-style.

This comment has been minimized.

Copy link
@spaceemotion

spaceemotion Jul 15, 2013

Contributor

+1 for the loss of 'item:' requirement

This comment has been minimized.

Copy link
@davidcernat

davidcernat Jul 15, 2013

Author Contributor

You can leave it out once you actually convert Give and Take to 0.9's style. :P

Pattern.CASE_INSENSITIVE);

/////////////////////
// STATIC METHODS
Expand Down Expand Up @@ -83,60 +95,90 @@ public static dItem valueOf(String string) {
@ObjectFetcher("i")
public static dItem valueOf(String string, dPlayer player, dNPC npc) {
if (string == null) return null;

Matcher m;
dItem stack = null;

///////
// Match @object format for spawned Item entities

final Pattern item_by_entity_id = Pattern.compile("(i@)(\\d+)");
final Pattern item_by_entity_id = Pattern.compile("(i@)(\\d+)\\[?(\\d+)?\\]?");

This comment has been minimized.

Copy link
@aufdemrand

aufdemrand Jul 15, 2013

Contributor

Do you need to be able to change the amount of items in a item that is already on the ground?

This comment has been minimized.

Copy link
@spaceemotion

spaceemotion Jul 15, 2013

Contributor

Well, that functionality is certainly nice to see, even though i don't have any practical examples at the moment...
Maybe for an item 'display' function? (Left click on a block adds more items to the stack, right click takes items away) - just a thought :D

This comment has been minimized.

Copy link
@davidcernat

davidcernat Jul 15, 2013

Author Contributor

auf, you don't need to, but does it really hurt? I wanted brackets to work in every possible case.

As for an example of a possible use, players could have an altar they place items on that are then teleported elsewhere in a smaller quantity. I'm fond of allowing people to do anything they want instead of limiting them just because they probably won't do certain things.

This comment has been minimized.

Copy link
@aufdemrand

aufdemrand Jul 15, 2013

Contributor

Okay, fair enough.

m = item_by_entity_id.matcher(string);

// Check if it's an entity in the world
if (m.matches())
for (World world : Bukkit.getWorlds())
for (Entity entity : world.getEntitiesByClass(Item.class))
if (entity.getEntityId() == Integer.valueOf(m.group(2)))
return new dItem(((Item) entity).getItemStack());
if (m.matches()) {
for (World world : Bukkit.getWorlds()) {
for (Entity entity : world.getEntitiesByClass(Item.class)) {
if (entity.getEntityId() == Integer.valueOf(m.group(2))) {
stack = new dItem(((Item) entity).getItemStack());

if (m.group(3) != null) {
stack.setAmount(Integer.valueOf(m.group(3)));
}

return stack;
}
}
}
}

////////
// Match @object format for saved dItems

final Pattern item_by_saved = Pattern.compile("(i@)(.+)");
final Pattern item_by_saved = Pattern.compile("(i@)(.+)\\[?(\\d+)?\\]?");
m = item_by_saved.matcher(string);

if (m.matches() && isSaved(m.group(2)))
return getSaved(m.group(2));
if (m.matches() && isSaved(m.group(2))) {
stack = getSaved(m.group(2));

if (m.group(3) != null) {
stack.setAmount(Integer.valueOf(m.group(3)));
}

return stack;
}

string = string.replace("i@", "");

///////
// Match item and book script custom items

// Check if it's a valid item/book script

if (ScriptRegistry.containsScript(string, ItemScriptContainer.class))
// Get item from script
return ScriptRegistry.getScriptContainerAs(string, ItemScriptContainer.class).getItemFrom(player, npc);

else if (ScriptRegistry.containsScript(string, BookScriptContainer.class))
// Get book from script
return ScriptRegistry.getScriptContainerAs(string, BookScriptContainer.class).getBookFrom(player, npc);

///////
// Match bukkit/minecraft standard items format

dItem stack = null;

m = itemPattern.matcher(string);

if (m.matches()) {

try {

///////
// Match item and book script custom items

if (ScriptRegistry.containsScript(m.group(1), ItemScriptContainer.class)) {
// Get item from script
stack = ScriptRegistry.getScriptContainerAs
(m.group(1), ItemScriptContainer.class).getItemFrom(player, npc);
}

else if (ScriptRegistry.containsScript(m.group(1), BookScriptContainer.class)) {
// Get book from script
stack = ScriptRegistry.getScriptContainerAs
(m.group(1), BookScriptContainer.class).getBookFrom(player, npc);
}

if (stack != null) {

if (m.group(3) != null) {
stack.setAmount(Integer.valueOf(m.group(3)));
}
return stack;
}
}
catch (Exception e) {
// Just a catch, might be a regular item...
}


///////
// Match Bukkit/Minecraft standard items format

try {
String material = m.group(1).toUpperCase();
String data = null;

if (m.groupCount() > 1) {
data = m.group(2);
}

if (aH.matchesInteger(material)) {
stack = new dItem(Integer.valueOf(material));
Expand All @@ -145,17 +187,20 @@ else if (ScriptRegistry.containsScript(string, BookScriptContainer.class))
stack = new dItem(Material.valueOf(material));
}

if (data != null) {
if (m.group(2) != null) {
stack.setDurability(Short.valueOf(m.group(2)));
}
if (m.group(3) != null) {
stack.setAmount(Integer.valueOf(m.group(3)));
}

return stack;
}
catch (Exception e) {
// Just a catch, might be an item script...
dB.log("Does not match a valid item ID or material: " + string);
}
}

if (!nope) dB.log("valueOf dItem returning null: " + string);

// No match! Return null.
Expand Down Expand Up @@ -302,12 +347,15 @@ public int comparesTo(ItemStack item) {

// Additional helper methods

public void setAmount(int value) {
if (item != null)
item.setAmount(value);
}

public void setDurability(short value) {
if (item != null)
item.setDurability(value);
}

// Additional helper methods

public void setData(byte value) {
if (item != null)
Expand Down
Expand Up @@ -165,7 +165,7 @@ public void blockBreak(BlockBreakEvent event) {
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);

if (determination.toUpperCase().startsWith("DROPS")) {
if (determination.toUpperCase().startsWith("DROP")) {

// Cancel the event
event.setCancelled(true);
Expand Down

0 comments on commit 672cb6e

Please sign in to comment.