All pastes #616869 Raw Edit

Mine

public text v1 · immutable
·published 2007-07-12 22:01 UTC
rendered paste body
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ItemDescription {
    private static byte[] stackableData = loadStackable();
    private int itemId, count;

    public ItemDescription(int itemId, int count) {
        this.itemId = itemId;
        this.count = count;
    }

    public int getType() {
        return itemId;
    }

    public int getAmount() {
        return count;
    }

    public boolean isStackable() {
        try {
            return stackableData[itemId] != '0';
        } catch (ArrayIndexOutOfBoundsException e) {
            System.err.println("ItemID out of bounds: " + itemId);
            return false;
        }
    }

    private static byte[] loadStackable() {
        try {
            File file = new File("byteArrayInputTest.txt");
            InputStream is = new FileInputStream(file);
            long length = file.length();

            if (length > Integer.MAX_VALUE) {
                throw new RuntimeException("What the hell, that's a huge-ass file.");
            }

            byte[] bytes = new byte[(int) length];
            int offset = 0, numRead;

            while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }

            if (offset < bytes.length) {
                throw new IOException("Could not completely read file " + file.getName());
            }

            is.close();
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void rehash() {
        stackableData = loadStackable();
    }
}

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Daniel
 * Date: Jul 11, 2007
 * Time: 10:00:19 AM
 * To change this template use File | Settings | File Templates.
 */
public class Inventory extends ArrayList<ItemDescription> {
    private HumanPlayer owner;

    public Inventory(HumanPlayer owner) {
        this.owner = owner;
    }

    public HumanPlayer getOwner() {
        return owner;
    }

    public boolean add(ItemDescription i) {
        return !isFull() && super.add(i);
    }


    public boolean isFull() {
        // It should never be larger than 28, but shit happens.
        return this.size() >= 28;
    }

    public boolean contains(ItemDescription... items) {
        for (ItemDescription item : items) {
            if (!contains(item)) return false;
        }
        return true;
    }

    private boolean contains(ItemDescription item) {
        int itemAmount = item.getAmount();

        if(!item.isStackable()) {
            if(itemAmount > 28) return false;

            /* There are several ways we could get into this, we could either check after every item or
            check at the end. I'm going for a combined approach: if the item amount is smaller than
            10, check every time. */

            int itemId = item.getType();

            if(itemAmount < 10) {
                // Don't let anything else access the inventory while we count.
                synchronized(this) {
                    for(int i = 0, count = 0, size = size(); i < size; i++) {
                        if(get(i).getType() == itemId) count++;
                        if(count > itemAmount) return true;
                    }
                    return false;
                }
            } else {
                synchronized(this) {
                    int count = 0;
                    for(int i = 0, size = size(); i < size; i++) {
                        if(get(i).getType() == itemId) count++;
                    }
                    return count > itemAmount;
                }
            }
        } else {
            synchronized(this) {
                for(int i = 0, size = size(), itemId = item.getType(); i < size; i++) {
                    if(get(i).getType() == itemId) return get(i).getAmount() >= itemAmount;
                }
                return false;
            }
        }
    }
}

public interface EquipmentConstants {
    public static final int
            SLOT_HEAD = 0,
            SLOT_CAPE = 1,
            SLOT_NECK = 2,
            SLOT_ARROWS = 3,
            SLOT_WEAPON = 4,
            SLOT_TORSO = 5,
            SLOT_SHIELD = 6,
            SLOT_LEGS = 7,
            SLOT_GLOVES = 8,
            SLOT_BOOTS = 9,
            SLOT_RING = 10;
            
}

public class Equipment {
    private HumanPlayer owner;
    private ItemDescription[] equipment = new ItemDescription[11];

    public Equipment(HumanPlayer owner) {
        this.owner = owner;
    }

    public HumanPlayer getOwner() {
        return owner;
    }

    public ItemDescription getEquipment(int itemSlot) {
        if(itemSlot > equipment.length) throw new RuntimeException("Invalid Slot");
        return equipment[itemSlot];
    }

    public void setEquipment(int itemSlot, ItemDescription newEquipment) {
        if(itemSlot > equipment.length) throw new RuntimeException("Invalid Slot");
        // if(equipment[itemSlot] == null)
    }
}

public class HumanPlayer {
    private Inventory inventory;
    private Equipment equipment;
}