Advertisement
Sarada-L2

Fix TradeList Acis 398

Feb 18th, 2021
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.42 KB | None | 0 0
  1. diff --git a/java/net/sf/l2j/gameserver/model/itemcontainer/PcInventory.java b/java/net/sf/l2j/gameserver/model/itemcontainer/PcInventory.java
  2. index eadf661..dc45840 100644
  3. --- a/java/net/sf/l2j/gameserver/model/itemcontainer/PcInventory.java
  4. +++ b/java/net/sf/l2j/gameserver/model/itemcontainer/PcInventory.java
  5. @@ -335,31 +335,58 @@
  6.  
  7. /**
  8. * Adjust TradeItem according his status in inventory
  9. - * @param item : ItemInstance to be adjusten
  10. + * @param item : ItemInstance to be adjusted
  11. + * @param list
  12. + * @return
  13. */
  14. - public void adjustAvailableItem(TradeItem item)
  15. + public TradeItem adjustAvailableItem(TradeItem item, List<TradeItem> list)
  16. {
  17. - // For all ItemInstance with same item id.
  18. - for (ItemInstance adjItem : getItemsByItemId(item.getItem().getItemId()))
  19. + for(ItemInstance adjItem : _items)
  20. {
  21. - // If enchant level is different, bypass.
  22. - if (adjItem.getEnchantLevel() != item.getEnchant())
  23. - continue;
  24. -
  25. - // If item isn't equipable, or equipable but not equiped it is a success.
  26. - if (!adjItem.isEquipable() || (adjItem.isEquipable() && !adjItem.isEquipped()))
  27. + if (adjItem.isStackable())
  28. {
  29. - item.setObjectId(adjItem.getObjectId());
  30. - item.setEnchant(adjItem.getEnchantLevel());
  31. -
  32. - if (adjItem.getCount() < item.getCount())
  33. - item.setCount(adjItem.getCount());
  34. -
  35. - return;
  36. + if (adjItem.getItemId() == item.getItem().getItemId() && (adjItem.getEnchantLevel() == item.getEnchant()))
  37. + {
  38. + item.setObjectId(adjItem.getObjectId());
  39. + if (adjItem.getCount() < item.getCount())
  40. + item.setCount(adjItem.getCount());
  41. + else
  42. + {
  43. + item.setCount(item.getCount());
  44. + }
  45. + return item;
  46. + }
  47. + }
  48. + else
  49. + {
  50. + if (adjItem.getItemId() == item.getItem().getItemId() && (adjItem.getEnchantLevel() == item.getEnchant()))
  51. + {
  52. + boolean found = false;
  53. + for (TradeItem actual:list)
  54. + {
  55. + if (actual.getObjectId() == adjItem.getObjectId())
  56. + {
  57. + found = true;
  58. + break;
  59. + }
  60. + }
  61. + if (found)
  62. + {
  63. + continue;
  64. + }
  65. + item.setObjectId(adjItem.getObjectId());
  66. + if (adjItem.getCount() < item.getCount())
  67. + item.setCount(adjItem.getCount());
  68. + else
  69. + {
  70. + item.setCount(item.getCount());
  71. + }
  72. + return item;
  73. + }
  74. }
  75. }
  76. - // None item matched conditions ; return as invalid count.
  77. item.setCount(0);
  78. + return item;
  79. }
  80.  
  81. /**
  82. diff --git a/java/net/sf/l2j/gameserver/model/trade/TradeList.java b/java/net/sf/l2j/gameserver/model/trade/TradeList.java
  83. index 0fe29fd..c494222 100644
  84. --- a/java/net/sf/l2j/gameserver/model/trade/TradeList.java
  85. +++ b/java/net/sf/l2j/gameserver/model/trade/TradeList.java
  86. @@ -22,7 +22,6 @@
  87. public class TradeList extends CopyOnWriteArrayList<TradeItem>
  88. {
  89. private static final long serialVersionUID = 1L;
  90. -
  91. private final Player _owner;
  92.  
  93. private Player _partner;
  94. @@ -87,35 +86,39 @@
  95. {
  96. return _locked;
  97. }
  98. -
  99. /**
  100. - * @param inventory : The {@link PcInventory} to test.
  101. - * @return A cloned {@link List} of this {@link TradeList} adjusted to {@link PcInventory} available items.
  102. + * Returns the list of items in inventory available for transaction
  103. + * @param inventory The inventory to make checks on.
  104. + * @return ItemInstance : items in inventory
  105. */
  106. public List<TradeItem> getAvailableItems(PcInventory inventory)
  107. {
  108. - final List<TradeItem> list = new ArrayList<>(this);
  109. - list.forEach(ti -> inventory.adjustAvailableItem(ti));
  110. + List<TradeItem> list = new ArrayList<>();
  111. + for (TradeItem item : this)
  112. + {
  113. + item = new TradeItem(item, item.getCount(), item.getPrice());
  114. + list.add(inventory.adjustAvailableItem(item,list));
  115. + }
  116. return list;
  117. }
  118.  
  119. /**
  120. - * Create a {@link TradeItem} based on an existing {@link ItemInstance}.
  121. - * @param item : The {@link ItemInstance} to test.
  122. - * @return A {@link TradeItem} based on {@link ItemInstance}.
  123. + * Adjust available item from Inventory by the one in this list
  124. + * @param item : ItemInstance to be adjusted
  125. + * @return TradeItem representing adjusted item
  126. */
  127. public TradeItem adjustAvailableItem(ItemInstance item)
  128. {
  129. if (item.isStackable())
  130. {
  131. - for (TradeItem tradeItem : this)
  132. + for (TradeItem exclItem : this)
  133. {
  134. - if (tradeItem.getItem().getItemId() == item.getItemId())
  135. + if (exclItem.getItem().getItemId() == item.getItemId() && (exclItem.getEnchant() == item.getEnchantLevel()))
  136. {
  137. - if (item.getCount() <= tradeItem.getCount())
  138. + if (item.getCount() <= exclItem.getCount())
  139. return null;
  140. -
  141. - return new TradeItem(item, item.getCount() - tradeItem.getCount(), item.getReferencePrice());
  142. +
  143. + return new TradeItem(item, item.getCount() - exclItem.getCount(), item.getReferencePrice());
  144. }
  145. }
  146. }
  147. @@ -123,129 +126,130 @@
  148. }
  149.  
  150. /**
  151. - * Create a {@link TradeItem} based on an existing {@link ItemInstance}, and add it to this {@link TradeList}.
  152. - * @param objectId : The {@link WorldObject} objectId to test.
  153. - * @param count : The amount of newly formed {@link TradeItem}.
  154. - * @param price : The price of newly formed {@link TradeItem}.
  155. - * @return A {@link TradeItem} based on {@link ItemInstance}, which is itself retrieved from its objectId from {@link World#getObject(int)}.
  156. + * Add item to TradeList
  157. + * @param objectId : int
  158. + * @param count : int
  159. + * @param price : int
  160. + * @return
  161. */
  162. public synchronized TradeItem addItem(int objectId, int count, int price)
  163. {
  164. if (isLocked())
  165. return null;
  166. -
  167. - final WorldObject object = World.getInstance().getObject(objectId);
  168. - if (!(object instanceof ItemInstance))
  169. +
  170. + WorldObject o = World.getInstance().getObject(objectId);
  171. + if (!(o instanceof ItemInstance))
  172. return null;
  173. -
  174. - final ItemInstance item = (ItemInstance) object;
  175. -
  176. +
  177. + ItemInstance item = (ItemInstance) o;
  178. +
  179. if (!item.isTradable() || item.isQuestItem())
  180. return null;
  181. -
  182. +
  183. if (count <= 0 || count > item.getCount())
  184. return null;
  185. -
  186. +
  187. if (!item.isStackable() && count > 1)
  188. return null;
  189. -
  190. +
  191. if ((Integer.MAX_VALUE / count) < price)
  192. return null;
  193. -
  194. +
  195. for (TradeItem checkitem : this)
  196. {
  197. if (checkitem.getObjectId() == objectId)
  198. return null;
  199. }
  200. -
  201. - final TradeItem tradeItem = new TradeItem(item, count, price);
  202. - add(tradeItem);
  203. -
  204. - // If Player has already confirmed this trade, invalidate the confirmation.
  205. +
  206. + TradeItem tradeItem = new TradeItem(item, count, price);
  207. + this.add(tradeItem);
  208. +
  209. + // If Player has already confirmed this trade, invalidate the confirmation
  210. invalidateConfirmation();
  211. -
  212. return tradeItem;
  213. }
  214.  
  215. /**
  216. - * Create a {@link TradeItem} based on itemId, and add it to this {@link TradeList}.
  217. - * @param itemId : The itemId of newly formed {@link TradeItem}.
  218. - * @param count : The amount of newly formed {@link TradeItem}.
  219. - * @param price : The price of newly formed {@link TradeItem}.
  220. - * @param enchant : The enchant value of newly formed {@link TradeItem}.
  221. - * @return A {@link TradeItem} based on itemId.
  222. + * Add item to TradeList
  223. + * @param itemId : int
  224. + * @param count : int
  225. + * @param price : int
  226. + * @param enchant
  227. + * @return
  228. */
  229. public synchronized TradeItem addItemByItemId(int itemId, int count, int price, int enchant)
  230. {
  231. if (isLocked())
  232. return null;
  233. -
  234. - final Item item = ItemData.getInstance().getTemplate(itemId);
  235. +
  236. + Item item = ItemData.getInstance().getTemplate(itemId);
  237. if (item == null)
  238. return null;
  239. -
  240. +
  241. if (!item.isTradable() || item.isQuestItem())
  242. return null;
  243. -
  244. +
  245. if (!item.isStackable() && count > 1)
  246. return null;
  247. -
  248. +
  249. if ((Integer.MAX_VALUE / count) < price)
  250. return null;
  251. -
  252. - final TradeItem tradeItem = new TradeItem(item, count, price, enchant);
  253. - add(tradeItem);
  254. -
  255. - // If Player has already confirmed this trade, invalidate the confirmation.
  256. +
  257. + TradeItem tradeItem = new TradeItem(item, count, price, enchant);
  258. + tradeItem.setEnchant(enchant);
  259. + this.add(tradeItem);
  260. +
  261. + // If Player has already confirmed this trade, invalidate the confirmation
  262. invalidateConfirmation();
  263. -
  264. return tradeItem;
  265. }
  266.  
  267. /**
  268. - * Remove or decrease amount of a {@link TradeItem} from this {@link TradeList}, by either its objectId or itemId.
  269. - * @param objectId : The objectId to test.
  270. - * @param itemId : The itemId ot test.
  271. - * @param count : The amount to remove.
  272. + * Remove item from TradeList
  273. + * @param objectId : int
  274. + * @param itemId : int
  275. + * @param count : int
  276. + * @return
  277. */
  278. - public synchronized void removeItem(int objectId, int itemId, int count)
  279. + public synchronized TradeItem removeItem(int objectId, int itemId, int count)
  280. {
  281. if (isLocked())
  282. - return;
  283. -
  284. + return null;
  285. +
  286. for (TradeItem tradeItem : this)
  287. {
  288. if (tradeItem.getObjectId() == objectId || tradeItem.getItem().getItemId() == itemId)
  289. {
  290. - // If Partner has already confirmed this trade, invalidate the confirmation.
  291. + // If Partner has already confirmed this trade, invalidate the confirmation
  292. if (_partner != null)
  293. {
  294. TradeList partnerList = _partner.getActiveTradeList();
  295. if (partnerList == null)
  296. - break;
  297. -
  298. + return null;
  299. +
  300. partnerList.invalidateConfirmation();
  301. }
  302. -
  303. - // Reduce item count or complete item.
  304. +
  305. + // Reduce item count or complete item
  306. if (count != -1 && tradeItem.getCount() > count)
  307. tradeItem.setCount(tradeItem.getCount() - count);
  308. else
  309. - remove(tradeItem);
  310. -
  311. - break;
  312. + this.remove(tradeItem);
  313. +
  314. + return tradeItem;
  315. }
  316. }
  317. + return null;
  318. }
  319.  
  320. /**
  321. - * Update {@link TradeItem}s from this {@link TradeList} according to their quantity in owner inventory.
  322. + * Update items in TradeList according their quantity in owner inventory
  323. */
  324. public synchronized void updateItems()
  325. {
  326. for (TradeItem tradeItem : this)
  327. {
  328. - final ItemInstance item = _owner.getInventory().getItemByObjectId(tradeItem.getObjectId());
  329. + ItemInstance item = _owner.getInventory().getItemByObjectId(tradeItem.getObjectId());
  330. if (item == null || tradeItem.getCount() < 1)
  331. removeItem(tradeItem.getObjectId(), -1, -1);
  332. else if (item.getCount() < tradeItem.getCount())
  333. @@ -265,7 +269,6 @@
  334. public synchronized void clear()
  335. {
  336. super.clear();
  337. -
  338. _locked = false;
  339. }
  340.  
  341.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement