
package com.exphc.FreeTrade;

import java.util.logging.Logger;
import java.util.regex.*;

import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.*;
import org.bukkit.entity.*;
import org.bukkit.*;

class ItemSpec
{
    int quantity;
    boolean exact;
    Material material;

    public ItemSpec(String s) {
        Pattern p = Pattern.compile("^(\\d*)(#?)(\\p{Alpha}+)(!?)$");
        Matcher m = p.matcher(s);
        while(m.find()) {
            String quantityString = m.group(1);
            String isStackString = m.group(2);
            String nameString = m.group(3);
            String isExactString = m.group(4);


            quantity = Integer.parseInt(quantityString);
            if (quantity < 0) {
                quantity = 1;
            }

            // TODO: really need better material matching names, shorthands
            // diamond_pickaxe, too long. diamondpick, dpick, would be better.
            // iron_ingot, want just iron or i. shortest match: cobblestone, cobble. common: diamond, d. plural.
            material = Material.matchMaterial(nameString);
            if (material == null) {
                // TODO: exception?
            }

            if (isStackString.length() != 0) {
                quantity *= material.getMaxStackSize();
            }

            exact = isExactString.length() != 0;

        }
    }

    public String toString() {
        return quantity + " " + material + (exact ? " (exact) " : "");
    }
}

public class FreeTrade extends JavaPlugin {
    Logger log = Logger.getLogger("Minecraft");

    public void onEnable() {
        log.info("FreeTrade enabled");
    }

    public void onDisable() {
        log.info("FreeTrade disabled");
    }

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        Player player;

        if (!cmd.getName().equalsIgnoreCase("want")) {
            return false;
        }

        // /want
        if (args.length == 0) {
            return showOutstanding(sender);
        }

        if (sender instanceof Player) {
            player = (Player)sender;
        } else {
            // TODO: get player from name as first argument
            sender.sendMessage("this command can only be run by a player");
            //return false;
        }

        if (args.length < 2) {
            return false;
        }

        String wantedString, givingString;
        wantedString = args[0];
        if (args[1].equalsIgnoreCase("for")) {
            givingString = args[2];
        } else {
            givingString = args[1];
        }

        ItemSpec wanted = new ItemSpec(wantedString);
        ItemSpec giving = new ItemSpec(givingString);

        sender.sendMessage("you want " + wanted.toString() + " for " + giving.toString());

        return true;
    }

    public boolean showOutstanding(CommandSender sender) {
        sender.sendMessage("TODO: show open orders");

        return false;
    }

}



@@ -1477,16 +1477,350 @@
    %7D%0A%7D%0A%0A
+class Order%0A%7B%0A    Player player;%0A    ItemSpec want, give;%0A%0A    public Order(Player p, String wantString, String giveString) %7B%0A        player = p;%0A        want = new ItemSpec(wantString);%0A        give = new ItemSpec(giveString);%0A    %7D%0A%0A    public String toString() %7B%0A        return player + %22 wants %22 + want + %22 for %22 + give;%0A    %7D%0A%7D%0A%0A
 public c
@@ -2604,24 +2604,51 @@
 a player%22);%0A
+            player = null;%0A
             
@@ -2757,26 +2757,24 @@
  String want
-ed
 String, givi
@@ -2768,27 +2768,25 @@
 tString, giv
-ing
+e
 String;%0A    
@@ -2789,26 +2789,24 @@
         want
-ed
 String = arg
@@ -2865,35 +2865,33 @@
 %0A            giv
-ing
+e
 String = args%5B2%5D
@@ -2924,19 +2924,17 @@
      giv
-ing
+e
 String =
@@ -2966,186 +2966,98 @@
     
-ItemSpec wanted = new ItemSpec(
+Order order = new Order(player, 
 want
-ed
 String
-);%0A        ItemSpec giving = new ItemSpec(givingString);%0A%0A        sender.sendMessage(%22you want %22 + wanted.toString() + %22 for %22 + giving
+, giveString);%0A%0A        sender.sendMessage(order
 .toS

Order class, with player, wanted item, and giving item specs