mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2026-06-06 05:14:09 +08:00
Implement group-based item giving & Add content handler for item giving
This commit is contained in:
@@ -2,7 +2,6 @@ package emu.grasscutter.server.packet.recv;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.data.GameData;
|
||||
import emu.grasscutter.data.excels.giving.GivingData.GiveMethod;
|
||||
import emu.grasscutter.game.quest.enums.QuestContent;
|
||||
import emu.grasscutter.net.packet.*;
|
||||
import emu.grasscutter.net.proto.ItemGivingReqOuterClass.ItemGivingReq;
|
||||
@@ -10,6 +9,8 @@ import emu.grasscutter.server.game.GameSession;
|
||||
import emu.grasscutter.server.packet.send.PacketItemGivingRsp;
|
||||
import emu.grasscutter.server.packet.send.PacketItemGivingRsp.Mode;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Opcodes(PacketOpcodes.ItemGivingReq)
|
||||
public final class HandlerItemGivingReq extends PacketHandler {
|
||||
@Override
|
||||
@@ -20,38 +21,83 @@ public final class HandlerItemGivingReq extends PacketHandler {
|
||||
var inventory = player.getInventory();
|
||||
|
||||
var giveId = req.getGivingId();
|
||||
var items = req.getItemGuidCountMapMap();
|
||||
var items = req.getItemParamListList();
|
||||
|
||||
switch (req.getItemGivingType()) {
|
||||
case QUEST -> {
|
||||
var questManager = player.getQuestManager();
|
||||
var activeGivings = questManager.getActiveGivings();
|
||||
if (!activeGivings.contains(giveId)) return;
|
||||
var activeGivings = player.getPlayerProgress().getItemGivings();
|
||||
if (!activeGivings.containsKey(giveId)) return;
|
||||
|
||||
// Check the items against the resources.
|
||||
var data = GameData.getGivingDataMap().get(giveId);
|
||||
if (data == null) throw new IllegalArgumentException("No giving data found for " + giveId + ".");
|
||||
|
||||
if (data.getGivingMethod() == GiveMethod.EXACT) {
|
||||
if (!inventory.hasAllItems(items)) {
|
||||
player.sendPacket(new PacketItemGivingRsp());
|
||||
return;
|
||||
}
|
||||
switch (data.getGivingMethod()) {
|
||||
case GIVING_METHOD_EXACT -> {
|
||||
// Check if the player has all the items.
|
||||
if (!inventory.hasAllItems(items)) {
|
||||
player.sendPacket(new PacketItemGivingRsp());
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove the items if the quest specifies.
|
||||
if (data.isRemoveItem()) {
|
||||
inventory.removeItems(items);
|
||||
}
|
||||
// Remove the items if the quest specifies.
|
||||
if (data.isRemoveItem()) {
|
||||
inventory.removeItems(items);
|
||||
}
|
||||
|
||||
// Send the response packet.
|
||||
player.sendPacket(new PacketItemGivingRsp(giveId, Mode.EXACT_SUCCESS));
|
||||
// Remove the action from the active givings.
|
||||
activeGivings.remove(giveId);
|
||||
// Queue the content action.
|
||||
questManager.queueEvent(QuestContent.QUEST_CONTENT_FINISH_ITEM_GIVING, giveId);
|
||||
} else {
|
||||
// TODO: Handle group givings.
|
||||
player.sendPacket(new PacketItemGivingRsp());
|
||||
// Send the response packet.
|
||||
player.sendPacket(new PacketItemGivingRsp(giveId, Mode.EXACT_SUCCESS));
|
||||
// Remove the action from the active givings.
|
||||
questManager.removeGivingItemAction(giveId);
|
||||
// Queue the content action.
|
||||
questManager.queueEvent(QuestContent.QUEST_CONTENT_FINISH_ITEM_GIVING, giveId, 0);
|
||||
}
|
||||
case GIVING_METHOD_VAGUE_GROUP -> {
|
||||
var matchedGroups = new ArrayList<Integer>();
|
||||
var givenItems = new HashMap<Integer, Integer>();
|
||||
|
||||
// Resolve potential item IDs.
|
||||
var groupData = GameData.getGivingGroupDataMap();
|
||||
data.getGivingGroupIds().stream()
|
||||
.map(groupId -> groupData.get((int) groupId))
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(group -> {
|
||||
var itemIds = group.getItemIds();
|
||||
|
||||
// Match item stacks to the group items.
|
||||
items.forEach(param -> {
|
||||
// Get the item instance.
|
||||
var itemInstance = inventory.getFirstItem(param.getItemId());
|
||||
if (itemInstance == null) return;
|
||||
|
||||
// Get the item ID.
|
||||
var itemId = itemInstance.getItemId();
|
||||
if (!itemIds.contains(itemId)) return;
|
||||
|
||||
// Add the item to the given items.
|
||||
givenItems.put(itemId, param.getCount());
|
||||
matchedGroups.add(group.getId());
|
||||
});
|
||||
});
|
||||
|
||||
// Check if the player has any items.
|
||||
if (givenItems.isEmpty() && matchedGroups.isEmpty()) {
|
||||
player.sendPacket(new PacketItemGivingRsp());
|
||||
} else {
|
||||
// Remove the items if the quest specifies.
|
||||
if (data.isRemoveItem()) {
|
||||
inventory.removeItems(items);
|
||||
}
|
||||
|
||||
// Send the response packet.
|
||||
player.sendPacket(new PacketItemGivingRsp(matchedGroups.get(0), Mode.GROUP_SUCCESS));
|
||||
// Mark the giving action as completed.
|
||||
questManager.markCompleted(giveId);
|
||||
// Queue the content action.
|
||||
questManager.queueEvent(QuestContent.QUEST_CONTENT_FINISH_ITEM_GIVING, giveId, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case GADGET -> {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package emu.grasscutter.server.packet.send;
|
||||
|
||||
import emu.grasscutter.net.packet.*;
|
||||
import emu.grasscutter.net.proto.GivingRecordNotifyOuterClass.GivingRecordNotify;
|
||||
import emu.grasscutter.net.proto.GivingRecordOuterClass.GivingRecord;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public final class PacketGivingRecordNotify extends BasePacket {
|
||||
public PacketGivingRecordNotify(Collection<GivingRecord> records) {
|
||||
super(PacketOpcodes.GivingRecordNotify);
|
||||
|
||||
this.setData(GivingRecordNotify.newBuilder()
|
||||
.addAllGivingRecordList(records)
|
||||
.build());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user