/** * Destroy item from inventory and updates database * * @param process : String Identifier of process triggering this action * @param item : L2ItemInstance to be destroyed * @param actor : L2PcInstance Player requesting the item destroy * @param reference : Object Object referencing current action like NPC selling item or previous * item in transformation * @return L2ItemInstance corresponding to the destroyed item or the updated item in inventory */ public L2ItemInstance destroyItem( String process, L2ItemInstance item, long count, L2PcInstance actor, Object reference) { synchronized (item) { // Adjust item quantity if (item.getCount() > count) { item.changeCount(process, -count, actor, reference); item.setLastChange(L2ItemInstance.MODIFIED); // don't update often for untraced items if (process != null || GameTimeController.getGameTicks() % 10 == 0) { item.updateDatabase(); } refreshWeight(); return item; } else { if (item.getCount() < count) return null; boolean removed = this.removeItem(item); if (!removed) return null; ItemTable.getInstance().destroyItem(process, item, actor, reference); item.updateDatabase(); refreshWeight(); } } return item; }
public Ingredient( int itemId, long itemCount, boolean isTaxIngredient, boolean maintainIngredient) { _itemId = itemId; _itemCount = itemCount; _isTaxIngredient = isTaxIngredient; _maintainIngredient = maintainIngredient; if (_itemId > 0) _template = ItemTable.getInstance().getTemplate(_itemId); }
/** * Adds item to inventory * * @param process : String Identifier of process triggering this action * @param itemId : int Item Identifier of the item to be added * @param count : int Quantity of items to be added * @param actor : L2PcInstance Player requesting the item add * @param reference : Object Object referencing current action like NPC selling item or previous * item in transformation * @return L2ItemInstance corresponding to the new item or the updated item in inventory */ public L2ItemInstance addItem( String process, int itemId, long count, L2PcInstance actor, Object reference) { L2ItemInstance item = getItemByItemId(itemId); // If stackable item is found in inventory just add to current quantity if (item != null && item.isStackable()) { item.changeCount(process, count, actor, reference); item.setLastChange(L2ItemInstance.MODIFIED); // Updates database if (itemId == 57 && count < 10000 * Config.RATE_DROP_ITEMS_ID.get(57)) { // Small adena changes won't be saved to database all the time if (GameTimeController.getGameTicks() % 5 == 0) item.updateDatabase(); } else item.updateDatabase(); } // If item hasn't be found in inventory, create new one else { for (int i = 0; i < count; i++) { L2Item template = ItemTable.getInstance().getTemplate(itemId); if (template == null) { _log.log( Level.WARNING, (actor != null ? "[" + actor.getName() + "] " : "") + "Invalid ItemId requested: ", itemId); return null; } item = ItemTable.getInstance() .createItem(process, itemId, template.isStackable() ? count : 1, actor, reference); item.setOwnerId(getOwnerId()); item.setLocation(getBaseLocation()); item.setLastChange(L2ItemInstance.ADDED); // Add item in inventory addItem(item); // Updates database item.updateDatabase(); // If stackable, end loop as entire count is included in 1 instance of item if (template.isStackable() || !Config.MULTIPLE_ITEM_DROP) break; } } refreshWeight(); return item; }
@Override public void deleteMe() { try { for (L2ItemInstance item : _items) { if (item != null) { ItemTable.getInstance().destroyItem("ClearRefund", item, getOwner(), null); item.updateDatabase(true); } } } catch (Exception e) { _log.log(Level.SEVERE, "deleteMe()", e); } _items.clear(); }
private static String getRequiredItems(int level) { if (Config.CLASS_MASTER_SETTINGS.getRequireItems(level) == null || Config.CLASS_MASTER_SETTINGS.getRequireItems(level).isEmpty()) return "<tr><td>none</td></r>"; StringBuilder sb = new StringBuilder(); for (int _itemId : Config.CLASS_MASTER_SETTINGS.getRequireItems(level).keys()) { int _count = Config.CLASS_MASTER_SETTINGS.getRequireItems(level).get(_itemId); sb.append( "<tr><td><font color=\"LEVEL\">" + _count + "</font></td><td>" + ItemTable.getInstance().getTemplate(_itemId).getName() + "</td></tr>"); } return sb.toString(); }
@Override protected void addItem(L2ItemInstance item) { super.addItem(item); try { if (getSize() > 12) { L2ItemInstance removedItem; synchronized (_items) { removedItem = _items.remove(0); } if (removedItem != null) { ItemTable.getInstance().destroyItem("ClearRefund", removedItem, getOwner(), null); removedItem.updateDatabase(true); } } } catch (Exception e) { _log.log(Level.SEVERE, "addItem()", e); } }
/** * Adds item to inventory * * @param process : String Identifier of process triggering this action * @param item : L2ItemInstance to be added * @param actor : L2PcInstance Player requesting the item add * @param reference : Object Object referencing current action like NPC selling item or previous * item in transformation * @return L2ItemInstance corresponding to the new item or the updated item in inventory */ public L2ItemInstance addItem( String process, L2ItemInstance item, L2PcInstance actor, Object reference) { L2ItemInstance olditem = getItemByItemId(item.getItemId()); // If stackable item is found in inventory just add to current quantity if (olditem != null && olditem.isStackable()) { long count = item.getCount(); olditem.changeCount(process, count, actor, reference); olditem.setLastChange(L2ItemInstance.MODIFIED); // And destroys the item ItemTable.getInstance().destroyItem(process, item, actor, reference); item.updateDatabase(); item = olditem; // Updates database if (item.getItemId() == 57 && count < 10000 * Config.RATE_DROP_ITEMS_ID.get(57)) { // Small adena changes won't be saved to database all the time if (GameTimeController.getGameTicks() % 5 == 0) item.updateDatabase(); } else item.updateDatabase(); } // If item hasn't be found in inventory, create new one else { item.setOwnerId(process, getOwnerId(), actor, reference); item.setLocation(getBaseLocation()); item.setLastChange((L2ItemInstance.ADDED)); // Add item in inventory addItem(item); // Updates database item.updateDatabase(); } refreshWeight(); return item; }
private TradeController() { _lists.clear(); Connection con = null; /* * Initialize Shop buylist */ try { con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement1 = con.prepareStatement("SELECT shop_id, npc_id FROM merchant_shopids"); ResultSet rset1 = statement1.executeQuery(); int itemId, price, maxCount, currentCount, time; long saveTimer; PreparedStatement statement = con.prepareStatement( "SELECT item_id, price, shop_id, " + L2DatabaseFactory.getInstance().safetyString("order") + ", count, currentCount, time, savetimer FROM merchant_buylists WHERE shop_id=? ORDER BY " + L2DatabaseFactory.getInstance().safetyString("order") + " ASC"); while (rset1.next()) { statement.setString(1, String.valueOf(rset1.getInt("shop_id"))); ResultSet rset = statement.executeQuery(); statement.clearParameters(); int shopId = rset1.getInt("shop_id"); L2TradeList buy1 = new L2TradeList(shopId); while (rset.next()) { itemId = rset.getInt("item_id"); price = rset.getInt("price"); maxCount = rset.getInt("count"); currentCount = rset.getInt("currentCount"); time = rset.getInt("time"); saveTimer = rset.getLong("saveTimer"); L2TradeItem item = new L2TradeItem(shopId, itemId); if (ItemTable.getInstance().getTemplate(itemId) == null) { _log.warning( "Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId() + ", missing data for that item."); continue; } if (price <= -1) { price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice(); } if (Config.DEBUG) { // debug double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice(); if (diff < 0.8 || diff > 1.2) { _log.severe( "PRICING DEBUG: TradeListId: " + buy1.getListId() + " - ItemId: " + itemId + " (" + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price + " - Reference: " + ItemTable.getInstance().getTemplate(itemId).getReferencePrice()); } } item.setPrice(price); item.setRestoreDelay(time); item.setNextRestoreTime(saveTimer); item.setMaxCount(maxCount); if (currentCount > -1) { item.setCurrentCount(currentCount); } else { item.setCurrentCount(maxCount); } buy1.addItem(item); } buy1.setNpcId(rset1.getString("npc_id")); _lists.put(buy1.getListId(), buy1); _nextListId = Math.max(_nextListId, buy1.getListId() + 1); rset.close(); } statement.close(); rset1.close(); statement1.close(); _log.info("TradeController: Loaded " + _lists.size() + " Buylists."); } catch (Exception e) { // problem with initializing spawn, go to next one _log.log( Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e); } finally { L2DatabaseFactory.close(con); } /* * If enabled, initialize the custom buylist */ if (Config.CUSTOM_MERCHANT_TABLES) { try { int initialSize = _lists.size(); con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement1 = con.prepareStatement("SELECT shop_id, npc_id FROM custom_merchant_shopids"); ResultSet rset1 = statement1.executeQuery(); int itemId, price, maxCount, currentCount, time; long saveTimer; PreparedStatement statement = con.prepareStatement( "SELECT item_id, price, shop_id, " + L2DatabaseFactory.getInstance().safetyString("order") + ", count, currentCount, time, savetimer FROM custom_merchant_buylists WHERE shop_id=? ORDER BY " + L2DatabaseFactory.getInstance().safetyString("order") + " ASC"); while (rset1.next()) { statement.setString(1, String.valueOf(rset1.getInt("shop_id"))); ResultSet rset = statement.executeQuery(); statement.clearParameters(); int shopId = rset1.getInt("shop_id"); L2TradeList buy1 = new L2TradeList(shopId); while (rset.next()) { itemId = rset.getInt("item_id"); price = rset.getInt("price"); maxCount = rset.getInt("count"); currentCount = rset.getInt("currentCount"); time = rset.getInt("time"); saveTimer = rset.getLong("saveTimer"); L2TradeItem item = new L2TradeItem(shopId, itemId); if (ItemTable.getInstance().getTemplate(itemId) == null) { _log.warning( "Skipping itemId: " + itemId + " on buylistId: " + buy1.getListId() + ", missing data for that item."); continue; } if (price <= -1) { price = ItemTable.getInstance().getTemplate(itemId).getReferencePrice(); } if (Config.DEBUG) { // debug double diff = ((double) (price)) / ItemTable.getInstance().getTemplate(itemId).getReferencePrice(); if (diff < 0.8 || diff > 1.2) { _log.severe( "PRICING DEBUG: TradeListId: " + buy1.getListId() + " - ItemId: " + itemId + " (" + ItemTable.getInstance().getTemplate(itemId).getName() + ") diff: " + diff + " - Price: " + price + " - Reference: " + ItemTable.getInstance().getTemplate(itemId).getReferencePrice()); } } item.setPrice(price); item.setRestoreDelay(time); item.setNextRestoreTime(saveTimer); item.setMaxCount(maxCount); if (currentCount > -1) { item.setCurrentCount(currentCount); } else { item.setCurrentCount(maxCount); } buy1.addItem(item); } buy1.setNpcId(rset1.getString("npc_id")); _lists.put(buy1.getListId(), buy1); _nextListId = Math.max(_nextListId, buy1.getListId() + 1); rset.close(); } statement.close(); rset1.close(); statement1.close(); _log.info("TradeController: Loaded " + (_lists.size() - initialSize) + " Custom Buylists."); } catch (Exception e) { // problem with initializing spawn, go to next one _log.log( Level.WARNING, "TradeController: Buylists could not be initialized: " + e.getMessage(), e); } finally { L2DatabaseFactory.close(con); } } }
@Override public void unregister() { ItemTable.removeNewItemListener(this); }
@Override public void register() { ItemTable.addNewItemListener(this); }
/** * Transfers item to another inventory * * @param process : String Identifier of process triggering this action * @param itemId : int Item Identifier of the item to be transfered * @param count : int Quantity of items to be transfered * @param actor : L2PcInstance Player requesting the item transfer * @param reference : Object Object referencing current action like NPC selling item or previous * item in transformation * @return L2ItemInstance corresponding to the new item or the updated item in inventory */ public L2ItemInstance transferItem( String process, int objectId, long count, ItemContainer target, L2PcInstance actor, Object reference) { if (target == null) { return null; } L2ItemInstance sourceitem = getItemByObjectId(objectId); if (sourceitem == null) { return null; } L2ItemInstance targetitem = sourceitem.isStackable() ? target.getItemByItemId(sourceitem.getItemId()) : null; synchronized (sourceitem) { // check if this item still present in this container if (getItemByObjectId(objectId) != sourceitem) { return null; } // Check if requested quantity is available if (count > sourceitem.getCount()) count = sourceitem.getCount(); // If possible, move entire item object if (sourceitem.getCount() == count && targetitem == null) { removeItem(sourceitem); target.addItem(process, sourceitem, actor, reference); targetitem = sourceitem; } else { if (sourceitem.getCount() > count) // If possible, only update counts { sourceitem.changeCount(process, -count, actor, reference); } else // Otherwise destroy old item { removeItem(sourceitem); ItemTable.getInstance().destroyItem(process, sourceitem, actor, reference); } if (targetitem != null) // If possible, only update counts { targetitem.changeCount(process, count, actor, reference); } else // Otherwise add new item { targetitem = target.addItem(process, sourceitem.getItemId(), count, actor, reference); } } // Updates database sourceitem.updateDatabase(true); if (targetitem != sourceitem && targetitem != null) targetitem.updateDatabase(); if (sourceitem.isAugmented()) sourceitem.getAugmentation().removeBonus(actor); refreshWeight(); target.refreshWeight(); } return targetitem; }