Java tutorial
/** * License * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR * COPYRIGHT LAW IS PROHIBITED. * * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS. * */ package com.lineage.server.model; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.lineage.Config; import com.lineage.server.IdFactory; import com.lineage.server.datatables.FurnitureSpawnTable; import com.lineage.server.datatables.InnKeyTable; import com.lineage.server.datatables.ItemTable; import com.lineage.server.datatables.LetterTable; import com.lineage.server.datatables.PetTable; import com.lineage.server.datatables.RaceTicketTable; import com.lineage.server.model.Instance.L1FurnitureInstance; import com.lineage.server.model.Instance.L1ItemInstance; import com.lineage.server.templates.L1Item; import com.lineage.server.templates.L1RaceTicket; import com.lineage.server.utils.Random; import com.lineage.server.utils.collections.Lists; /** * ? * * @author jrwz */ public class L1Inventory extends L1Object { /** * ? * * @return ?1 - ?2 */ public class DataComparator<T> implements Comparator<L1ItemInstance> { @Override public int compare(final L1ItemInstance item1, final L1ItemInstance item2) { return item1.getEnchantLevel() - item2.getEnchantLevel(); } } /** ??? */ private static final Log _log = LogFactory.getLog(L1Inventory.class); /** ??UID */ private static final long serialVersionUID = 1L; /** ? */ protected List<L1ItemInstance> _items = Lists.newConcurrentList(); /** ? */ public static final int MAX_AMOUNT = 2000000000; // 2G /** ? */ public static final int MAX_WEIGHT = 1500; // ???? /** ?? */ public static final int OK = 0; /** ?? */ public static final int SIZE_OVER = 1; /** ????? */ public static final int WEIGHT_OVER = 2; /** ?LONG */ public static final int AMOUNT_OVER = 3; // ???? /** /? */ public static final int WAREHOUSE_TYPE_PERSONAL = 0; /** */ public static final int WAREHOUSE_TYPE_CLAN = 1; public L1Inventory() { } /** * ??? () * * @param item * ? * @param count * ? * @return 0:? 1:? 2:???? 3:LONG */ public int checkAddItem(final L1ItemInstance item, final int count) { // ? if (item == null) { return -1; } // ??0 if ((item.getCount() <= 0) || (count <= 0)) { return -1; } // ? if ((this.getSize() > Config.MAX_NPC_ITEM) || ((this.getSize() == Config.MAX_NPC_ITEM) && (!item.isStackable() || !this.checkItem(item.getItem().getItemId())))) { // ? return SIZE_OVER; } // ? final int weight = this.getWeight() + item.getItem().getWeight() * count / 1000 + 1; if ((weight < 0) || ((item.getItem().getWeight() * count / 1000) < 0)) { return WEIGHT_OVER; } // ???? if (weight > (MAX_WEIGHT * Config.RATE_WEIGHT_LIMIT_PET)) { return WEIGHT_OVER; } // ? (20) final L1ItemInstance itemExist = this.findItemId(item.getItemId()); if ((itemExist != null) && ((itemExist.getCount() + count) > MAX_AMOUNT)) { return AMOUNT_OVER; } return OK; } /** * ??? () * * @param item * ? * @param count * ? * @param type * 0:/? 1: * @return 0:? 1:? */ public int checkAddItemToWarehouse(final L1ItemInstance item, final int count, final int type) { // ? if (item == null) { return -1; } // ??0 if ((item.getCount() <= 0) || (count <= 0)) { return -1; } // ?? int maxSize = 100; // /? if (type == WAREHOUSE_TYPE_PERSONAL) { maxSize = Config.MAX_PERSONAL_WAREHOUSE_ITEM; } // else if (type == WAREHOUSE_TYPE_CLAN) { maxSize = Config.MAX_CLAN_WAREHOUSE_ITEM; } // ? if ((this.getSize() > maxSize) || ((this.getSize() == maxSize) && (!item.isStackable() || !this.checkItem(item.getItem().getItemId())))) { // ? return SIZE_OVER; } return OK; } // ??????????? // ?????????? /** * ?? (?) * * @param id * ? * @param enchant * * @param count * ? * @return true: false: */ public boolean checkEnchantItem(final int id, final int enchant, final int count) { int num = 0; for (final L1ItemInstance item : this._items) { if (item.isEquipped()) { // ?? continue; } if ((item.getItemId() == id) && (item.getEnchantLevel() == enchant)) { num++; if (num == count) { return true; } } } return false; } /** * ??? (1) ? * * @param id * ?? * @return ?? (1) */ public boolean checkItem(final int id) { return this.checkItem(id, 1); } /** * ??? * * @param id * ?? * @param count * ?? * @return true: false:? */ public boolean checkItem(final int id, final int count) { // ?0 if (count == 0) { return true; } // ?? if (ItemTable.getInstance().getTemplate(id).isStackable()) { final L1ItemInstance item = this.findItemId(id); if ((item != null) && (item.getCount() >= count)) { return true; } } else { // ??? final Object[] itemList = this.findItemsId(id); if (itemList.length >= count) { return true; } } return false; } // ??????????????????????? /** * ?? * * @param ids * ?ID * @return checkItem(ids, counts) */ public boolean checkItem(final int[] ids) { final int len = ids.length; final int[] counts = new int[len]; for (int i = 0; i < len; i++) { counts[i] = 1; } return this.checkItem(ids, counts); } /** * ??? * * @param ids * ?ID * @param counts * ?? * @return true: false:? */ public boolean checkItem(final int[] ids, final int[] counts) { for (int i = 0; i < ids.length; i++) { if (!this.checkItem(ids[i], counts[i])) { return false; } } return true; } // ????????? // ?????????? /** * ?? (?) * * @param id * ? * @param count * ? * @return */ public boolean checkItemNotEquipped(final int id, final int count) { if (count == 0) { return true; } return count <= this.countItems(id); } /** * */ public void clearItems() { for (final Object itemObject : this._items) { final L1ItemInstance item = (L1ItemInstance) itemObject; L1World.getInstance().removeObject(item); } this._items.clear(); } // ???? // ?????????? /** * ?? (?) * * @param id * ? * @param enchant * * @param count * ? * @return true:? false: */ public boolean consumeEnchantItem(final int id, final int enchant, final int count) { for (final L1ItemInstance item : this._items) { if (item.isEquipped()) { // ?? continue; } if ((item.getItemId() == id) && (item.getEnchantLevel() == enchant)) { this.removeItem(item); return true; } } return false; } /** * ???? () * * @param itemid * - ?? * @param count * - ? * @return true:? false: */ public boolean consumeItem(final int itemid, final int count) { // ??0 if (count <= 0) { return false; } // ?? if (ItemTable.getInstance().getTemplate(itemid).isStackable()) { final L1ItemInstance item = this.findItemId(itemid); if ((item != null) && (item.getCount() >= count)) { this.removeItem(item, count); return true; } } else { final L1ItemInstance[] itemList = this.findItemsId(itemid); if (itemList.length == count) { for (int i = 0; i < count; i++) { this.removeItem(itemList[i], 1); } return true; } else if (itemList.length > count) { // ??? final DataComparator<L1ItemInstance> dc = new DataComparator<L1ItemInstance>(); Arrays.sort(itemList, dc); // for (int i = 0; i < count; i++) { this.removeItem(itemList[i], 1); // } return true; } } return false; } /** * ?? * * @param id * @return 0 */ public int countItems(final int id) { // ?? if (ItemTable.getInstance().getTemplate(id).isStackable()) { final L1ItemInstance item = this.findItemId(id); if (item != null) { return item.getCount(); } } else { // ??? final Object[] itemList = this.findItemsIdNotEquipped(id); return itemList.length; } return 0; } // _items?(L1PcInstance?L1DwarfInstance?L1GroundInstance) /** * ? * * @param item * @return */ public void deleteItem(final L1ItemInstance item) { // if (item.getItem().getItemId() == 40312) { InnKeyTable.DeleteKey(item); } this._items.remove(item); } /** * ?? (??) * * @param id * ?ID * @return null */ public L1ItemInstance findItemId(final int id) { for (final L1ItemInstance item : this._items) { if (item.getItem().getItemId() == id) { return item; } } return null; } /** * ???ID * * @param nameId * @return item null */ public L1ItemInstance findItemNameId(final String nameId) { for (final L1ItemInstance item : this._items) { if (nameId.equals(item.getItem().getIdentifiedNameId())) { return item; } } return null; } /** * ??? () * * @param itemId * ?ID * @return ?? */ public L1ItemInstance[] findItemsId(final int id) { final List<L1ItemInstance> itemList = Lists.newList(); for (final L1ItemInstance item : this._items) { if (item.getItemId() == id) { itemList.add(item); } } return itemList.toArray(new L1ItemInstance[itemList.size()]); } /** * ?? () * * @param itemId * ?ID * @return ?? */ public L1ItemInstance[] findItemsIdNotEquipped(final int id) { final List<L1ItemInstance> itemList = Lists.newList(); for (final L1ItemInstance item : this._items) { if (item.getItemId() == id) { if (!item.isEquipped()) { itemList.add(item); } } } return itemList.toArray(new L1ItemInstance[itemList.size()]); } /** * ?Key * * @param id * KeyId * @return null */ public L1ItemInstance findKeyId(final int id) { for (final L1ItemInstance item : this._items) { if (item.getKeyId() == id) { return item; } } return null; } /** * ?Objid? * * @param objectId * @return null */ public L1ItemInstance getItem(final int objectId) { for (final Object itemObject : this._items) { final L1ItemInstance item = (L1ItemInstance) itemObject; if (item.getId() == objectId) { return item; } } return null; } /** * ? * * @return ? */ public List<L1ItemInstance> getItems() { return this._items; } /** * ? * * @return ? */ public int getSize() { if (this._items.isEmpty()) { return 0; } return this._items.size(); } /** * ?? * * @return ?? */ public int getWeight() { int weight = 0; for (final L1ItemInstance item : this._items) { weight += item.getWeight(); } return weight; } public void insertItem(final L1ItemInstance item) { } // public void loadItems() { } /** * ?????? ?? ? * * @param objectId * ?OBJID * @return */ public L1ItemInstance receiveDamage(final int objectId) { final L1ItemInstance item = this.getItem(objectId); return this.receiveDamage(item); } /** * ?????? ?? ? * * @param item * ? * @return */ public L1ItemInstance receiveDamage(final L1ItemInstance item) { return this.receiveDamage(item, 1); } /** * ?????? ?? ? * * @param item * ? * @param count * ??? * @return */ public L1ItemInstance receiveDamage(final L1ItemInstance item, final int count) { // ? if (item == null) { return null; } final int itemType = item.getItem().getType2(); // ? final int currentDurability = item.get_durability(); // ? if (((currentDurability == 0) && (itemType == 0)) || (currentDurability < 0)) { item.set_durability(0); return null; } // ? if (itemType == 0) { final int minDurability = (item.getEnchantLevel() + 5) * -1; int durability = currentDurability - count; if (durability < minDurability) { durability = minDurability; } if (currentDurability > durability) { item.set_durability(durability); } } else { final int maxDurability = item.getEnchantLevel() + 5; int durability = currentDurability + count; if (durability > maxDurability) { durability = maxDurability; } if (currentDurability < durability) { item.set_durability(durability); } } this.updateItem(item, L1PcInventory.COL_DURABILITY); return item; } /** * ?????? () * * @param item * ? * @return */ public L1ItemInstance recoveryDamage(final L1ItemInstance item) { // ? if (item == null) { return null; } final int itemType = item.getItem().getType2(); final int durability = item.get_durability(); if (((durability == 0) && (itemType != 0)) || (durability < 0)) { item.set_durability(0); return null; } if (itemType == 0) { // ? item.set_durability(durability + 1); } else { // ? item.set_durability(durability - 1); } this.updateItem(item, L1PcInventory.COL_DURABILITY); return item; } /** * ?? (Objid?? ?) * * @param objectId * @param count * @return ? */ public int removeItem(final int objectId, final int count) { final L1ItemInstance item = this.getItem(objectId); return this.removeItem(item, count); } /** * ? (?) ? * * @param item * @return ? */ public int removeItem(final L1ItemInstance item) { return this.removeItem(item, item.getCount()); } /** * ??? ? * * @param item * @param count * @return ? */ public int removeItem(final L1ItemInstance item, int count) { // ? if (item == null) { return 0; } if (!this._items.contains(item)) { return 0; } // ??0 if ((item.getCount() <= 0) || (count <= 0)) { return 0; } if (item.getCount() < count) { count = item.getCount(); } if (item.getCount() == count) { final int itemId = item.getItem().getItemId(); if ((itemId == 40314) || (itemId == 40316)) { // PetTable.getInstance().deletePet(item.getId()); } else if ((itemId >= 49016) && (itemId <= 49025)) { // final LetterTable lettertable = new LetterTable(); lettertable.deleteLetter(item.getId()); } else if ((itemId >= 41383) && (itemId <= 41400)) { // for (final L1Object l1object : L1World.getInstance().getObject()) { if (l1object instanceof L1FurnitureInstance) { final L1FurnitureInstance furniture = (L1FurnitureInstance) l1object; if (furniture.getItemObjId() == item.getId()) { // ? FurnitureSpawnTable.getInstance().deleteFurniture(furniture); } } } } else if (item.getItemId() == 40309) {// Race Tickets RaceTicketTable.getInstance().deleteTicket(item.getId()); } this.deleteItem(item); L1World.getInstance().removeObject(item); } else { item.setCount(item.getCount() - count); this.updateItem(item); } return count; } /** * */ public void shuffle() { Collections.shuffle(this._items); } /** * ? () * * @param id * ?ID * @param count * ?? * @return null */ public synchronized L1ItemInstance storeItem(final int id, final int count) { try { // ??0 if (count <= 0) { return null; } // ?? final L1Item temp = ItemTable.getInstance().getTemplate(id); // ? if (temp == null) { return null; } // if (id == 40312) { final L1ItemInstance item = new L1ItemInstance(temp, count); if (this.findKeyId(id) == null) { // ???????????ID??L1World??? item.setId(IdFactory.getInstance().nextId()); L1World.getInstance().storeObject(item); } return this.storeItem(item); } else if (temp.isStackable()) { final L1ItemInstance item = new L1ItemInstance(temp, count); if (this.findItemId(id) == null) { // ???????????ID??L1World??? item.setId(IdFactory.getInstance().nextId()); L1World.getInstance().storeObject(item); } return this.storeItem(item); } // ??? L1ItemInstance result = null; for (int i = 0; i < count; i++) { final L1ItemInstance item = new L1ItemInstance(temp, 1); item.setId(IdFactory.getInstance().nextId()); L1World.getInstance().storeObject(item); this.storeItem(item); result = item; } // ?()??? return result; } catch (final Exception e) { _log.error(e.getLocalizedMessage(), e); } return null; } /** * ? () (? /?) * * @param item * ? * @return null */ public synchronized L1ItemInstance storeItem(final L1ItemInstance item) { try { // ? if (item == null) { return null; } // ??0 if (item.getCount() <= 0) { return null; } // ??ID final int itemId = item.getItem().getItemId(); // ??? if (item.isStackable()) { L1ItemInstance findItem = this.findItemId(itemId); if (itemId == 40309) { // Race Tickets () findItem = this.findItemNameId(item.getItem().getIdentifiedNameId()); } else if (itemId == 40312) { // findItem = this.findKeyId(itemId); } else { findItem = this.findItemId(itemId); } if (findItem != null) { findItem.setCount(findItem.getCount() + item.getCount()); this.updateItem(findItem); return findItem; } } if (itemId == 40309) {// Race Tickets () String[] temp = item.getItem().getIdentifiedNameId().split(" "); temp = temp[temp.length - 1].split("-"); final L1RaceTicket ticket = new L1RaceTicket(); ticket.set_itemobjid(item.getId()); ticket.set_round(Integer.parseInt(temp[0])); ticket.set_allotment_percentage(0.0); ticket.set_victory(0); ticket.set_runner_num(Integer.parseInt(temp[1])); RaceTicketTable.getInstance().storeNewTiket(ticket); } item.setX(this.getX()); item.setY(this.getY()); item.setMap(this.getMapId()); // ?? int chargeCount = item.getItem().getMaxChargeCount(); switch (itemId) { case 40006: // ? case 40007: // ? case 40008: // ?? case 140006: // ??? ? case 140008: // ??? ?? case 41401: // ? chargeCount -= Random.nextInt(5); break; case 20383: // chargeCount = 50; break; default: break; } item.setChargeCount(chargeCount); // light if ((item.getItem().getType2() == 0) && (item.getItem().getType() == 2)) { item.setRemainingTime(item.getItem().getLightFuel()); } else { item.setRemainingTime(item.getItem().getMaxUseTime()); } item.setBless(item.getItem().getBless()); // if (item.getItem().getItemId() == 40312) { if (!InnKeyTable.checkey(item)) { InnKeyTable.StoreKey(item); } } this._items.add(item); this.insertItem(item); return item; } catch (final Exception e) { _log.error(e.getLocalizedMessage(), e); } return null; } // ???nameId?????item? (????) /** * ? () (? ?/??/) * * @param item * ? * @return null */ public synchronized L1ItemInstance storeTradeItem(final L1ItemInstance item) { try { // ? if (item == null) { return null; } // ??0 if (item.getCount() <= 0) { return null; } // if (item.getItem().getItemId() == 40312) { final L1ItemInstance findItem = this.findKeyId(item.getKeyId()); // ??? if (findItem != null) { findItem.setCount(findItem.getCount() + item.getCount()); this.updateItem(findItem); return findItem; } } else if (item.isStackable()) { final L1ItemInstance findItem = this.findItemId(item.getItem().getItemId()); if (findItem != null) { findItem.setCount(findItem.getCount() + item.getCount()); this.updateItem(findItem); return findItem; } } item.setX(this.getX()); item.setY(this.getY()); item.setMap(this.getMapId()); // if (item.getItem().getItemId() == 40312) { if (!InnKeyTable.checkey(item)) { InnKeyTable.StoreKey(item); } } this._items.add(item); this.insertItem(item); return item; } catch (final Exception e) { _log.error(e.getLocalizedMessage(), e); } return null; } /** * ? * * @param objectId * Objid * @param count * ? * @param inventory * * @return */ public synchronized L1ItemInstance tradeItem(final int objectId, final int count, final L1Inventory inventory) { final L1ItemInstance item = this.getItem(objectId); return this.tradeItem(item, count, inventory); } /** * ? * * @param item * ? * @param count * ? * @param inventory * * @return ? */ public synchronized L1ItemInstance tradeItem(final L1ItemInstance item, final int count, final L1Inventory inventory) { // ? if (item == null) { return null; } // ??0 if ((item.getCount() <= 0) || (count <= 0)) { return null; } // if (item.isEquipped()) { return null; } if (!this.checkItem(item.getItem().getItemId(), count)) { return null; } L1ItemInstance carryItem; if (item.getCount() <= count) { this.deleteItem(item); carryItem = item; } else { item.setCount(item.getCount() - count); this.updateItem(item); carryItem = ItemTable.getInstance().createItem(item.getItem().getItemId()); carryItem.setCount(count); carryItem.setEnchantLevel(item.getEnchantLevel()); carryItem.setIdentified(item.isIdentified()); carryItem.set_durability(item.get_durability()); carryItem.setChargeCount(item.getChargeCount()); carryItem.setRemainingTime(item.getRemainingTime()); carryItem.setLastUsed(item.getLastUsed()); carryItem.setBless(item.getBless()); // if (carryItem.getItem().getItemId() == 40312) { carryItem.setInnNpcId(item.getInnNpcId()); // NPC carryItem.setKeyId(item.getKeyId()); // ? carryItem.setHall(item.checkRoomOrHall()); // carryItem.setDueTime(item.getDueTime()); // } } return inventory.storeTradeItem(carryItem); } public void updateEnchantAccessory(final L1ItemInstance item, final int colmn) { } public void updateItem(final L1ItemInstance item) { } public void updateItem(final L1ItemInstance item, final int colmn) { } }