From 0e1c8dc5262fb29197c08b5132f71d831a4685c5 Mon Sep 17 00:00:00 2001 From: ImmuState Date: Sun, 5 Jun 2022 12:03:45 -0700 Subject: [PATCH] Cancel forging and refund items. --- .../ForgingManager/ForgingManager.java | 34 +++++++++++++++++++ .../send/PacketForgeQueueManipulateRsp.java | 3 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/emu/grasscutter/game/managers/ForgingManager/ForgingManager.java b/src/main/java/emu/grasscutter/game/managers/ForgingManager/ForgingManager.java index 8812778e1..235bddb9c 100644 --- a/src/main/java/emu/grasscutter/game/managers/ForgingManager/ForgingManager.java +++ b/src/main/java/emu/grasscutter/game/managers/ForgingManager/ForgingManager.java @@ -1,5 +1,6 @@ package emu.grasscutter.game.managers.ForgingManager; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -221,7 +222,40 @@ public class ForgingManager { } private void cancelForge(int queueId) { + // Make sure there are no unfinished items. + int currentTime = Utils.getCurrentSeconds(); + ActiveForgeData forge = this.player.getActiveForges().get(queueId - 1); + + if (forge.getFinishedCount(currentTime) > 0) { + return; + } + // Return material items to the player. + ForgeData data = GameData.getForgeDataMap().get(forge.getForgeId()); + + var returnItems = new ArrayList(); + for (var material : data.getMaterialItems()) { + if (material.getItemId() == 0) { + continue; + } + + ItemData resultItemData = GameData.getItemDataMap().get(material.getItemId()); + GameItem returnItem = new GameItem(resultItemData, material.getItemCount() * forge.getCount()); + + this.player.getInventory().addItem(returnItem); + returnItems.add(returnItem); + } + + // Return Mora to the player. + this.player.setMora(this.player.getMora() + data.getScoinCost() * forge.getCount()); + + ItemData moraItem = GameData.getItemDataMap().get(202); + GameItem returnMora = new GameItem(moraItem, data.getScoinCost() * forge.getCount()); + returnItems.add(returnMora); + + // Send response. + this.player.sendPacket(new PacketForgeQueueManipulateRsp(Retcode.RET_SUCC, ForgeQueueManipulateType.FORGE_QUEUE_MANIPULATE_TYPE_STOP_FORGE, List.of(), returnItems, List.of())); + this.sendForgeDataNotify(); } public void handleForgeQueueManipulateReq(ForgeQueueManipulateReq req) { diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketForgeQueueManipulateRsp.java b/src/main/java/emu/grasscutter/server/packet/send/PacketForgeQueueManipulateRsp.java index ac07f47cb..98aaea717 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketForgeQueueManipulateRsp.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketForgeQueueManipulateRsp.java @@ -16,7 +16,8 @@ public class PacketForgeQueueManipulateRsp extends BasePacket { super(PacketOpcodes.ForgeQueueManipulateRsp); ForgeQueueManipulateRsp.Builder builder = ForgeQueueManipulateRsp.newBuilder() - .setRetcode(retcode.getNumber()); + .setRetcode(retcode.getNumber()) + .setManipulateType(type); for (GameItem item : output) { ItemParam toAdd = ItemParam.newBuilder()