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");
}
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)");
}
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()));
}
}
Aggregations