|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Zoinkwiz <https://github.com/Zoinkwiz> |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 15 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 18 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 21 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 23 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | +package com.questhelper.bank; |
| 26 | + |
| 27 | +import com.google.gson.Gson; |
| 28 | +import com.google.inject.Inject; |
| 29 | +import com.google.inject.Singleton; |
| 30 | +import lombok.Setter; |
| 31 | +import lombok.extern.slf4j.Slf4j; |
| 32 | +import net.runelite.api.Client; |
| 33 | +import net.runelite.api.Item; |
| 34 | +import net.runelite.client.config.ConfigManager; |
| 35 | + |
| 36 | +import java.util.Arrays; |
| 37 | + |
| 38 | +@Slf4j |
| 39 | +@Singleton |
| 40 | +public class GroupBank extends QuestBank |
| 41 | +{ |
| 42 | + |
| 43 | + @Setter |
| 44 | + private Item[] groupBankDuringEditing; |
| 45 | + |
| 46 | + private Item[] groupInventoryDuringEditing; |
| 47 | + |
| 48 | + // Inventory change event occurs once before actual change from group editing |
| 49 | + // So we need to allow for this |
| 50 | + private int inventoryChangeEvents = 0; |
| 51 | + |
| 52 | + @Inject |
| 53 | + public GroupBank(Client client, ConfigManager configManager, Gson gson) |
| 54 | + { |
| 55 | + super(client, configManager, gson); |
| 56 | + } |
| 57 | + |
| 58 | + // Inventory changed. If it matches group Inventory |
| 59 | + public boolean updateAfterInventoryChange(Item[] inventoryItems) |
| 60 | + { |
| 61 | + if (groupBankDuringEditing == null || inventoryItems == null || groupInventoryDuringEditing == null) return false; |
| 62 | + if (Arrays.equals(inventoryItems, paddedItems(groupInventoryDuringEditing))) |
| 63 | + { |
| 64 | + updateLocalBank(groupBankDuringEditing); |
| 65 | + groupBankDuringEditing = null; |
| 66 | + groupInventoryDuringEditing = null; |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + inventoryChangeEvents++; |
| 71 | + int MAX_INVENTORY_CHANGE_BEFORE_CLEAR = 2; |
| 72 | + if (inventoryChangeEvents == MAX_INVENTORY_CHANGE_BEFORE_CLEAR) |
| 73 | + { |
| 74 | + inventoryChangeEvents = 0; |
| 75 | + groupBankDuringEditing = null; |
| 76 | + groupInventoryDuringEditing = null; |
| 77 | + } |
| 78 | + |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + public void setGroupInventoryDuringEditing(Item[] groupInventoryDuringEditing) |
| 83 | + { |
| 84 | + inventoryChangeEvents = 0; |
| 85 | + this.groupInventoryDuringEditing = groupInventoryDuringEditing; |
| 86 | + } |
| 87 | + |
| 88 | + private Item[] paddedItems(Item[] oldItems) |
| 89 | + { |
| 90 | + final int INVENTORY_SIZE = 28; |
| 91 | + |
| 92 | + Item[] newItems = new Item[28]; |
| 93 | + |
| 94 | + System.arraycopy(oldItems, 0, newItems, 0, oldItems.length); |
| 95 | + |
| 96 | + for (int i = oldItems.length; i < INVENTORY_SIZE; i++) |
| 97 | + { |
| 98 | + newItems[i] = new Item(-1, 0); |
| 99 | + } |
| 100 | + |
| 101 | + return newItems; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected String getKey() |
| 106 | + { |
| 107 | + return "groupbankitems"; |
| 108 | + } |
| 109 | +} |
| 110 | + |
0 commit comments