mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-15 08:25:21 +01:00
Attempt to implement item giving to NPCs (untested)
This commit is contained in:
55
src/main/java/emu/grasscutter/game/inventory/BagTab.java
Normal file
55
src/main/java/emu/grasscutter/game/inventory/BagTab.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package emu.grasscutter.game.inventory;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public enum BagTab {
|
||||
TAB_NONE(0),
|
||||
TAB_WEAPON(1),
|
||||
TAB_EQUIP(2),
|
||||
TAB_AVATAR(3),
|
||||
TAB_FOOD(4),
|
||||
TAB_MATERIAL(5),
|
||||
TAB_QUEST(6),
|
||||
TAB_CONSUME(7),
|
||||
TAB_WIDGET(8),
|
||||
TAB_HOMEWORLD(9);
|
||||
|
||||
private static final Int2ObjectMap<BagTab> map = new Int2ObjectOpenHashMap<>();
|
||||
private static final Map<String, BagTab> stringMap = new HashMap<>();
|
||||
|
||||
static {
|
||||
Stream.of(BagTab.values())
|
||||
.forEach(
|
||||
entry -> {
|
||||
map.put(entry.getValue(), entry);
|
||||
stringMap.put(entry.name(), entry);
|
||||
});
|
||||
}
|
||||
|
||||
@Getter private final int value;
|
||||
|
||||
/**
|
||||
* Fetches the bag tab by its value.
|
||||
*
|
||||
* @param value The name of the bag tab.
|
||||
* @return The bag tab.
|
||||
*/
|
||||
public static BagTab getTypeByValue(int value) {
|
||||
return map.getOrDefault(value, TAB_NONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the bag tab by its name.
|
||||
*
|
||||
* @param name The name of the bag tab.
|
||||
* @return The bag tab.
|
||||
*/
|
||||
public static BagTab getTypeByName(String name) {
|
||||
return stringMap.getOrDefault(name, TAB_NONE);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package emu.grasscutter.game.inventory;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.INVENTORY_LIMITS;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.data.GameData;
|
||||
import emu.grasscutter.data.common.ItemParamData;
|
||||
@@ -17,8 +15,11 @@ import emu.grasscutter.server.packet.send.*;
|
||||
import emu.grasscutter.utils.Utils;
|
||||
import it.unimi.dsi.fastutil.ints.*;
|
||||
import it.unimi.dsi.fastutil.longs.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.INVENTORY_LIMITS;
|
||||
|
||||
public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
|
||||
private final Long2ObjectMap<GameItem> store;
|
||||
private final Int2ObjectMap<InventoryTab> inventoryTypes;
|
||||
@@ -150,6 +151,52 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
|
||||
getPlayer().sendPacket(new PacketStoreItemChangeNotify(changedItems));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the player has the item in their inventory.
|
||||
* This will succeed if the player has at least the minimum count of the item.
|
||||
*
|
||||
* @param itemGuid The item id to check for.
|
||||
* @param minCount The minimum count of the item to check for.
|
||||
* @return True if the player has the item, false otherwise.
|
||||
*/
|
||||
public boolean hasItem(long itemGuid, int minCount) {
|
||||
return hasItem(itemGuid, minCount, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the player has the item in their inventory.
|
||||
*
|
||||
* @param itemGuid The item id to check for.
|
||||
* @param count The count of the item to check for.
|
||||
* @param enforce If true, the player must have the exact amount.
|
||||
* If false, the player must have at least the amount.
|
||||
* @return True if the player has the item, false otherwise.
|
||||
*/
|
||||
public boolean hasItem(long itemGuid, int count, boolean enforce) {
|
||||
var item = this.getItemByGuid(itemGuid);
|
||||
if (item == null) return false;
|
||||
|
||||
return enforce ?
|
||||
item.getCount() == count :
|
||||
item.getCount() >= count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the player has the item in their inventory.
|
||||
* This is exact.
|
||||
*
|
||||
* @param items A map of item game IDs to their count.
|
||||
* @return True if the player has the items, false otherwise.
|
||||
*/
|
||||
public boolean hasAllItems(Map<Long, Integer> items) {
|
||||
for (var item : items.entrySet()) {
|
||||
if (!this.hasItem(item.getKey(), item.getValue(), true))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void triggerAddItemEvents(GameItem result) {
|
||||
try {
|
||||
getPlayer()
|
||||
@@ -434,6 +481,17 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a bulk delete of items.
|
||||
*
|
||||
* @param items A map of item game IDs to the amount of items to remove.
|
||||
*/
|
||||
public void removeItems(Map<Long, Integer> items) {
|
||||
for (var entry : items.entrySet()) {
|
||||
this.removeItem(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean removeItem(long guid) {
|
||||
return removeItem(guid, 1);
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ public class Player implements PlayerHook, FieldFetch {
|
||||
@Getter private transient FriendsList friendsList;
|
||||
@Getter private transient MailHandler mailHandler;
|
||||
@Getter private transient AbilityManager abilityManager;
|
||||
@Getter @Setter private transient QuestManager questManager;
|
||||
@Getter private transient QuestManager questManager;
|
||||
@Getter private transient TowerManager towerManager;
|
||||
@Getter private transient SotSManager sotsManager;
|
||||
@Getter private transient MapMarksManager mapMarksManager;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package emu.grasscutter.game.quest;
|
||||
|
||||
import dev.morphia.annotations.Transient;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.data.GameData;
|
||||
import emu.grasscutter.data.binout.MainQuestData;
|
||||
@@ -31,10 +30,13 @@ import static emu.grasscutter.GameConstants.DEBUG;
|
||||
import static emu.grasscutter.config.Configuration.GAME_OPTIONS;
|
||||
import static emu.grasscutter.config.Configuration.SERVER;
|
||||
|
||||
public class QuestManager extends BasePlayerManager {
|
||||
public final class QuestManager extends BasePlayerManager {
|
||||
@Getter private final Player player;
|
||||
|
||||
@Getter private final Int2ObjectMap<GameMainQuest> mainQuests;
|
||||
@Transient @Getter private final List<Integer> loggedQuests;
|
||||
@Getter private final List<Integer> loggedQuests;
|
||||
|
||||
@Getter private final List<Integer> activeGivings = new ArrayList<>();
|
||||
|
||||
private long lastHourCheck = 0;
|
||||
private long lastDayCheck = 0;
|
||||
@@ -45,6 +47,7 @@ public class QuestManager extends BasePlayerManager {
|
||||
60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(1000),
|
||||
FastThreadLocalThread::new, new ThreadPoolExecutor.AbortPolicy());
|
||||
}
|
||||
|
||||
/*
|
||||
On SetPlayerBornDataReq, the server sends FinishedParentQuestNotify, with this exact
|
||||
parentQuestList. Captured on Game version 2.7
|
||||
|
||||
@@ -34,7 +34,7 @@ public enum QuestContent implements QuestTrigger {
|
||||
QUEST_CONTENT_ADD_QUEST_PROGRESS(24),
|
||||
QUEST_CONTENT_INTERACT_GADGET(25),
|
||||
QUEST_CONTENT_DAILY_TASK_COMP_FINISH(26), // missing, currently unused
|
||||
QUEST_CONTENT_FINISH_ITEM_GIVING(27), // missing, finish
|
||||
QUEST_CONTENT_FINISH_ITEM_GIVING(27),
|
||||
QUEST_CONTENT_SKILL(107),
|
||||
QUEST_CONTENT_CITY_LEVEL_UP(109), // missing, finish
|
||||
QUEST_CONTENT_PATTERN_GROUP_CLEAR_MONSTER(110), // missing, finish, for random quests
|
||||
|
||||
@@ -35,7 +35,7 @@ public enum QuestExec implements QuestTrigger {
|
||||
QUEST_EXEC_CREATE_PATTERN_GROUP(25), // missing, used for random quests
|
||||
QUEST_EXEC_REMOVE_PATTERN_GROUP(26), // missing, used for random quests
|
||||
QUEST_EXEC_REFRESH_GROUP_SUITE_RANDOM(27), // missing
|
||||
QUEST_EXEC_ACTIVE_ITEM_GIVING(28), // missing
|
||||
QUEST_EXEC_ACTIVE_ITEM_GIVING(28),
|
||||
QUEST_EXEC_DEL_ALL_SPECIFIC_PACK_ITEM(29), // missing
|
||||
QUEST_EXEC_ROLLBACK_PARENT_QUEST(30),
|
||||
QUEST_EXEC_LOCK_AVATAR_TEAM(31), // missing
|
||||
@@ -71,7 +71,7 @@ public enum QuestExec implements QuestTrigger {
|
||||
QUEST_EXEC_MODIFY_CLIMATE_AREA(60), // missing
|
||||
QUEST_EXEC_GRANT_TRIAL_AVATAR_AND_LOCK_TEAM(61), // missing
|
||||
QUEST_EXEC_CHANGE_MAP_AREA_STATE(62), // missing
|
||||
QUEST_EXEC_DEACTIVE_ITEM_GIVING(63), // missing
|
||||
QUEST_EXEC_DEACTIVE_ITEM_GIVING(63),
|
||||
QUEST_EXEC_CHANGE_SCENE_LEVEL_TAG(64), // missing
|
||||
QUEST_EXEC_UNLOCK_PLAYER_WORLD_SCENE(65), // missing
|
||||
QUEST_EXEC_LOCK_PLAYER_WORLD_SCENE(66), // missing
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package emu.grasscutter.game.quest.exec;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.data.excels.quest.QuestData.QuestExecParam;
|
||||
import emu.grasscutter.game.quest.*;
|
||||
import emu.grasscutter.game.quest.enums.QuestExec;
|
||||
import emu.grasscutter.game.quest.handlers.QuestExecHandler;
|
||||
|
||||
@QuestValueExec(QuestExec.QUEST_EXEC_ACTIVE_ITEM_GIVING)
|
||||
public final class ExecActiveItemGiving extends QuestExecHandler {
|
||||
@Override
|
||||
public boolean execute(GameQuest quest, QuestExecParam condition, String... paramStr) {
|
||||
var questManager = quest.getOwner().getQuestManager();
|
||||
var activeGivings = questManager.getActiveGivings();
|
||||
|
||||
var givingId = Integer.parseInt(condition.getParam()[0]);
|
||||
if (activeGivings.contains(givingId)) {
|
||||
Grasscutter.getLogger().debug("Quest {} attempted to add give action {} twice.",
|
||||
quest.getSubQuestId(), givingId);
|
||||
return false;
|
||||
} else {
|
||||
activeGivings.add(givingId);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package emu.grasscutter.game.quest.exec;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.data.excels.quest.QuestData;
|
||||
import emu.grasscutter.game.quest.*;
|
||||
import emu.grasscutter.game.quest.enums.QuestExec;
|
||||
import emu.grasscutter.game.quest.handlers.QuestExecHandler;
|
||||
|
||||
@QuestValueExec(QuestExec.QUEST_EXEC_DEACTIVE_ITEM_GIVING)
|
||||
public final class ExecDeactivateItemGiving extends QuestExecHandler {
|
||||
@Override
|
||||
public boolean execute(GameQuest quest, QuestData.QuestExecParam condition, String... paramStr) {
|
||||
var questManager = quest.getOwner().getQuestManager();
|
||||
var activeGivings = questManager.getActiveGivings();
|
||||
|
||||
var givingId = Integer.parseInt(condition.getParam()[0]);
|
||||
if (!activeGivings.contains(givingId)) {
|
||||
Grasscutter.getLogger().debug("Quest {} attempted to remove give action {} when it isn't active.",
|
||||
quest.getSubQuestId(), givingId);
|
||||
return false;
|
||||
} else {
|
||||
activeGivings.remove(givingId);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user