Search in sources :

Example 31 with Item

use of mc.dragons.core.gameobject.item.Item in project DragonsOnline by UniverseCraft.

the class User method buyItem.

public void buyItem(ItemClass itemClass, int quantity, double costPer) {
    debug("Attempting to buy " + quantity + " of " + itemClass.getClassName() + " at " + costPer + "g ea");
    double price = costPer * quantity;
    double balance = getGold();
    if (balance < price) {
        player.sendMessage(ChatColor.RED + "Cannot buy this item! Costs " + price + "g, you have " + balance + " (need " + (price - balance) + "g more)");
        return;
    }
    takeGold(price, false);
    Item item = itemLoader.registerNew(itemClass);
    item.setQuantity(quantity);
    giveItem(item, true, false, true);
    player.sendMessage(ChatColor.GREEN + "Purchased " + item.getDecoratedName() + (quantity > 1 ? ChatColor.GRAY + " (x" + quantity + ")" : "") + ChatColor.GREEN + " for " + ChatColor.GOLD + price + "g");
}
Also used : Item(mc.dragons.core.gameobject.item.Item)

Example 32 with Item

use of mc.dragons.core.gameobject.item.Item in project DragonsOnline by UniverseCraft.

the class User method takeItem.

public void takeItem(Item item, int amount, boolean updateDB, boolean updateInventory, boolean notify) {
    debug("Removing " + amount + " of " + item.getName() + " (has " + item.getQuantity() + "=" + item.getItemStack().getAmount() + ", db=" + updateDB + ", inv=" + updateInventory + ", n=" + notify + ")");
    int remaining = amount;
    for (int i = 0; i < player.getInventory().getContents().length; i++) {
        ItemStack itemStack = player.getInventory().getContents()[i];
        if (itemStack == null)
            continue;
        Item testItem = ItemLoader.fromBukkit(itemStack);
        if (testItem != null && item.getClassName().equals(testItem.getClassName()) && !item.isCustom() && !testItem.isCustom()) {
            if (testItem.getQuantity() <= remaining) {
                debug("Removing whole stack " + testItem.getUUID());
                if (updateDB) {
                    instance.getGameObjectRegistry().removeFromDatabase(testItem);
                }
                if (updateInventory) {
                    player.getInventory().remove(itemStack);
                }
                remaining -= testItem.getQuantity();
            } else {
                int newQuantity = testItem.getQuantity() - remaining;
                debug("Removing partial stack " + testItem.getUUID() + " (old=" + testItem.getQuantity() + ", new=" + newQuantity + ")");
                if (updateInventory) {
                    testItem.getItemStack().setAmount(newQuantity);
                    itemStack.setAmount(newQuantity);
                }
                if (updateDB) {
                    testItem.setQuantityNoBukkit(newQuantity);
                }
                debug("-New quantity for this stack: " + testItem.getQuantity() + "=" + testItem.getItemStack().getAmount() + "=" + itemStack.getAmount());
                break;
            }
        }
    }
    if (updateDB) {
        storageAccess.update(new Document("inventory", getInventoryAsDocument()));
    }
    if (notify) {
        player.spigot().sendMessage(StringUtil.hoverableText(ChatColor.RED + "- " + item.getDecoratedName() + (amount > 1 ? ChatColor.GRAY + " (x" + amount + ")" : ""), item.getHoverableItemData()));
    }
    debug("-Final quantity: " + item.getQuantity() + "=" + item.getItemStack().getAmount() + " (might not match if specified item is not the exact item matched and removed)");
}
Also used : Item(mc.dragons.core.gameobject.item.Item) ItemStack(org.bukkit.inventory.ItemStack) Document(org.bson.Document)

Example 33 with Item

use of mc.dragons.core.gameobject.item.Item in project DragonsOnline by UniverseCraft.

the class User method giveItem.

/*
	 * Item management
	 */
/**
 * Give the player an RPG item.
 *
 * <p>This process is non-trivial, as we have to allow stacking
 * in certain cases despite conflicting metadata.
 *
 * <p>Thus, we need to rewrite practically the whole item stacking
 * algorithm, as well as correctly synchronize with the player's
 * stored inventory data.
 *
 * @param item
 * @param updateDB
 * @param dbOnly
 * @param silent
 */
public void giveItem(Item item, boolean updateDB, boolean dbOnly, boolean silent) {
    int giveQuantity = item.getQuantity();
    debug("Giving " + item.getItemClass().getClassName() + " x" + giveQuantity);
    if (item.getClassName().equals(PlayerEventListeners.GOLD_CURRENCY_ITEM_CLASS_NAME)) {
        giveGold(giveQuantity);
        return;
    }
    int maxStackSize = item.getMaxStackSize();
    if (!dbOnly) {
        int remaining = giveQuantity;
        // Try to add to existing items in inventory first
        for (int i = 0; i < player.getInventory().getContents().length; i++) {
            ItemStack itemStack = player.getInventory().getContents()[i];
            if (itemStack == null)
                continue;
            Item testItem = ItemLoader.fromBukkit(itemStack);
            if (testItem != null && item.getClassName().equals(testItem.getClassName()) && !item.isCustom() && !testItem.isCustom()) {
                int quantity = Math.min(maxStackSize, testItem.getQuantity() + remaining);
                int added = quantity - testItem.getQuantity();
                debug("Adding to existing stack: " + testItem.getUUID().toString() + " (curr=" + testItem.getQuantity() + "=" + testItem.getItemStack().getAmount() + ", add=" + added + ", tot=" + quantity + ")");
                remaining -= added;
                testItem.setQuantity(quantity);
                player.getInventory().setItem(i, testItem.getItemStack());
                item.setQuantity(item.getQuantity() - added);
                item.getItemClass().getAddons().forEach(addon -> addon.initialize(this, item));
                debug("-Quantity: " + testItem.getQuantity() + "=" + testItem.getItemStack().getAmount());
                if (remaining == 0) {
                    break;
                }
                debug(" - " + remaining + " remaining to dispense");
            }
        }
        // If we can't fit everything in to existing items, add new ones
        while (remaining > 0) {
            int quantity = Math.min(maxStackSize, item.getItemStack().getAmount());
            if (quantity > remaining) {
                quantity = remaining;
            }
            debug("Adding " + quantity + "/" + remaining + " remaining items as new item stack");
            remaining -= quantity;
            Item r = itemLoader.registerNew(item);
            r.setQuantity(quantity);
            player.getInventory().addItem(new ItemStack[] { r.getItemStack() });
            item.getItemClass().getAddons().forEach(addon -> addon.initialize(this, item));
            debug(" - " + remaining + " remaining to dispense");
        }
    }
    if (updateDB) {
        storageAccess.update(new Document("inventory", getInventoryAsDocument()));
    }
    if (!silent) {
        player.spigot().sendMessage(StringUtil.hoverableText(ChatColor.GRAY + "+ " + item.getDecoratedName() + (item.getQuantity() > 1 ? ChatColor.GRAY + " (x" + giveQuantity + ")" : ""), item.getHoverableItemData()));
    }
}
Also used : Item(mc.dragons.core.gameobject.item.Item) ItemStack(org.bukkit.inventory.ItemStack) Document(org.bson.Document)

Aggregations

Item (mc.dragons.core.gameobject.item.Item)33 ItemStack (org.bukkit.inventory.ItemStack)19 User (mc.dragons.core.gameobject.user.User)15 Player (org.bukkit.entity.Player)11 EventHandler (org.bukkit.event.EventHandler)11 NPC (mc.dragons.core.gameobject.npc.NPC)9 Document (org.bson.Document)9 UUID (java.util.UUID)6 ArrayList (java.util.ArrayList)5 Entity (org.bukkit.entity.Entity)5 List (java.util.List)4 Dragons (mc.dragons.core.Dragons)4 GameObjectType (mc.dragons.core.gameobject.GameObjectType)4 ItemClass (mc.dragons.core.gameobject.item.ItemClass)4 ChatColor (org.bukkit.ChatColor)4 Location (org.bukkit.Location)4 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Floor (mc.dragons.core.gameobject.floor.Floor)3