Refactor LootInventory & LootManager

This commit is contained in:
P0nk
2023-03-16 00:25:21 +01:00
parent 45e2b93724
commit a95fa2efc1
2 changed files with 39 additions and 55 deletions

View File

@@ -30,13 +30,13 @@ import java.util.Map;
/** /**
* @author Ronan * @author Ronan
*/ */
public class LootInventory { class LootInventory {
Map<Integer, Integer> items = new HashMap<>(50); private final Map<Integer, Integer> items = new HashMap<>(50);
public LootInventory(Character from) { LootInventory(Character from) {
for (InventoryType values : InventoryType.values()) { for (InventoryType invType : InventoryType.values()) {
for (Item it : from.getInventory(values).list()) { for (Item it : from.getInventory(invType).list()) {
Integer itemQty = items.get(it.getItemId()); Integer itemQty = items.get(it.getItemId());
if (itemQty == null) { if (itemQty == null) {
@@ -48,9 +48,9 @@ public class LootInventory {
} }
} }
public int hasItem(int itemid, int quantity) { boolean hasItem(int itemId, int quantity) {
Integer itemQty = items.get(itemid); Integer itemQty = items.get(itemId);
return itemQty == null ? 0 : itemQty >= quantity ? 2 : itemQty > 0 ? 1 : 0; return itemQty != null && itemQty >= quantity;
} }
} }

View File

@@ -24,7 +24,7 @@ import server.life.MonsterDropEntry;
import server.life.MonsterInformationProvider; import server.life.MonsterInformationProvider;
import server.quest.Quest; import server.quest.Quest;
import java.util.LinkedList; import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@@ -32,66 +32,50 @@ import java.util.List;
*/ */
public class LootManager { public class LootManager {
private static boolean isRelevantDrop(MonsterDropEntry dropEntry, List<Character> players, List<LootInventory> playersInv) { private static boolean isRelevantDrop(MonsterDropEntry dropEntry, List<Character> chrs, List<LootInventory> inventories) {
int qStartAmount = 0, qCompleteAmount = 0; if (dropEntry.questid <= 0) {
Quest quest = Quest.getInstance(dropEntry.questid);
if (quest != null) {
qStartAmount = quest.getStartItemAmountNeeded(dropEntry.itemId);
qCompleteAmount = quest.getCompleteItemAmountNeeded(dropEntry.itemId);
}
//boolean restricted = ItemInformationProvider.getInstance().isPickupRestricted(dropEntry.itemId);
for (int i = 0; i < players.size(); i++) {
LootInventory chrInv = playersInv.get(i);
if (dropEntry.questid > 0) {
int qItemAmount, chrQuestStatus = players.get(i).getQuestStatus(dropEntry.questid);
if (chrQuestStatus == 0) {
qItemAmount = qStartAmount;
} else if (chrQuestStatus != 1) {
continue;
} else {
qItemAmount = qCompleteAmount;
}
// thanks kvmba for noticing quest items with no required amount failing to be detected as such
int qItemStatus = chrInv.hasItem(dropEntry.itemId, qItemAmount);
if (qItemStatus == 2) {
continue;
} /*else if (restricted && qItemStatus == 1) { // one-of-a-kind loots should be available everytime, thanks onechord for noticing
continue;
}*/
} /*else if (restricted && chrInv.hasItem(dropEntry.itemId, 1) > 0) { // thanks Conrad, Legalize for noticing eligible loots not being available to drop for non-killer parties
continue;
}*/
return true; return true;
} }
Quest quest = Quest.getInstance(dropEntry.questid);
int questStartAmount = quest.getStartItemAmountNeeded(dropEntry.itemId);
int questCompleteAmount = quest.getCompleteItemAmountNeeded(dropEntry.itemId);
for (int i = 0; i < chrs.size(); i++) {
int chrQuestStatus = chrs.get(i).getQuestStatus(dropEntry.questid);
final int questItemAmount;
if (chrQuestStatus == 0) {
questItemAmount = questStartAmount;
} else if (chrQuestStatus == 1) {
questItemAmount = questCompleteAmount;
} else {
continue;
}
LootInventory chrInv = inventories.get(i);
boolean hasQuestItems = chrInv.hasItem(dropEntry.itemId, questItemAmount);
if (!hasQuestItems) {
return true;
}
}
return false; return false;
} }
public static List<MonsterDropEntry> retrieveRelevantDrops(int monsterId, List<Character> players) { public static List<MonsterDropEntry> retrieveRelevantDrops(int monsterId, List<Character> chrs) {
List<MonsterDropEntry> loots = MonsterInformationProvider.getInstance().retrieveEffectiveDrop(monsterId); List<MonsterDropEntry> drops = MonsterInformationProvider.getInstance().retrieveEffectiveDrop(monsterId);
if (loots.isEmpty()) { if (drops.isEmpty()) {
return loots; return Collections.emptyList();
} }
List<LootInventory> playersInv = new LinkedList<>(); List<LootInventory> inventories = chrs.stream()
for (Character chr : players) { .map(LootInventory::new)
LootInventory lootInv = new LootInventory(chr); .toList();
playersInv.add(lootInv);
}
List<MonsterDropEntry> effectiveLoot = new LinkedList<>(); return drops.stream()
for (MonsterDropEntry mde : loots) { .filter(entry -> isRelevantDrop(entry, chrs, inventories))
if (isRelevantDrop(mde, players, playersInv)) { .toList();
effectiveLoot.add(mde);
}
}
return effectiveLoot;
} }
} }