
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;
    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);

            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.equals("#")) {
                quantity *= material.getMaxStackSize();
            }
        }
    }

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

class Order
{
    Player player;
    ItemSpec want, give;
    boolean exact;

    public Order(Player p, String wantString, String giveString) {
        player = p;

        if (wantString.contains("!")) {
            exact = true;
            wantString = wantString.replace("!", "");
        }
        if (giveString.contains("!")) {
            exact = true;
            giveString = giveString.replace("!", "");
        }

        want = new ItemSpec(wantString);
        give = new ItemSpec(giveString);
    }

    public String toString() {
        return player + " wants " + want + " for " + give + (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");
            player = null;
            //return false;
        }

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

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

        Order order = new Order(player, wantString, giveString);

        sender.sendMessage(order.toString());

        return true;
    }

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

        return false;
    }

}



@@ -82,16 +82,44 @@
 regex.*;
+%0Aimport java.util.ArrayList;
 %0A%0Aimport
@@ -2090,16 +2090,45 @@
 craft%22);
+%0A    ArrayList%3COrder%3E orders;
 %0A%0A    pu
@@ -2180,32 +2180,106 @@
 Trade enabled%22);
+%0A%0A        // TODO: load from file%0A        orders = new ArrayList%3COrder%3E();
 %0A    %7D%0A%0A    publ
@@ -2339,16 +2339,47 @@
 abled%22);
+%0A%0A        // TODO: save to file
 %0A    %7D%0A%0A
@@ -3385,24 +3385,52 @@
 String());%0A%0A
+        orders.add(order);%0A%0A
         retu
@@ -3555,24 +3555,143 @@
  orders%22);%0A%0A
+        for (int i = 0; i %3C orders.size(); i++) %7B%0A            sender.sendMessage(i + %22. %22 + orders.get(i));%0A        %7D%0A%0A
         retu

Store orders in ArrayList in-memory, /want to show all outstanding orders