Take required items from armor but notify for off-hand, fixes #1312

This commit is contained in:
PikaMug 2020-06-23 05:09:36 -04:00
parent edf9334ef8
commit 3ac36aeeed
3 changed files with 59 additions and 3 deletions

View File

@ -547,7 +547,13 @@ public class Quester {
Player p = getPlayer();
for (ItemStack is : reqs.getItems()) {
if (reqs.getRemoveItems().get(reqs.getItems().indexOf(is)) == true) {
InventoryUtil.removeItem(p.getInventory(), is);
if (InventoryUtil.removeItem(p.getInventory(), is) == false) {
if (InventoryUtil.stripItem(p.getEquipment(), is) == false) {
p.sendMessage(Lang.get(p, "requirementsItemFail"));
hardQuit(q);
return;
}
}
}
}
String accepted = Lang.get(getPlayer(), "questAccepted");

View File

@ -16,6 +16,7 @@ import java.util.HashMap;
import java.util.Map;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
@ -84,4 +85,52 @@ public class InventoryUtil {
}
return false;
}
/**
* Removes item from entity's equipment.
*
* @param equipment EntityEquipment to remove from
* @param item Item with amount to remove
* @return true if successful
*/
public static boolean stripItem(EntityEquipment equipment, ItemStack item) {
int amount = item.getAmount();
ItemStack[] allItems = equipment.getArmorContents();
HashMap<Integer, Integer> removeFrom = new HashMap<Integer, Integer>();
int foundAmount = 0;
int index = 0;
for (ItemStack i : allItems) {
if (i == null) {
index++;
continue;
}
if (ItemUtil.compareItems(item, i, true) == 0) {
if (i.getAmount() >= amount - foundAmount) {
removeFrom.put(index, amount - foundAmount);
foundAmount = amount;
} else {
foundAmount += i.getAmount();
removeFrom.put(index, i.getAmount());
}
if (foundAmount >= amount) {
break;
}
}
index++;
}
if (foundAmount == amount) {
for (Map.Entry<Integer, Integer> toRemove : removeFrom.entrySet()) {
ItemStack i = allItems[toRemove.getKey()];
if (i.getAmount() - toRemove.getValue() <= 0) {
allItems[toRemove.getKey()] = null;
} else {
i.setAmount(i.getAmount() - toRemove.getValue());
allItems[toRemove.getKey()] = i;
}
}
return true;
}
return false;
}
}

View File

@ -63,8 +63,8 @@ questEditorPln: "Edit Planner"
questEditorStages: "Edit Stages"
questEditorRews: "Edit Rewards"
questEditorOpts: "Edit Options"
questEditorDefaultAskMessage: "Challenge objectives with haste!"
questEditorDefaultFinishMessage: "You have finished all objectives."
questEditorDefaultAskMessage: "Challenge objectives!"
questEditorDefaultFinishMessage: "Thank you!"
questEditorEnterQuestName: "Enter quest name, <cancel>"
questEditorEnterAskMessage: "Enter ask message, <cancel>"
questEditorEnterFinishMessage: "Enter finish message, <cancel>"
@ -735,6 +735,7 @@ everything: "Everything"
usage: "Usage"
redoableEvery: "Redoable every <time>."
requirements: "Requirements"
requirementsItemFail: "Unable to collect required item. Is it in your off-hand?"
money: "Money"
with: "with"
to: "to"