From 3c03bfec0a4bc2c1f029c673e4a897c44f80c277 Mon Sep 17 00:00:00 2001 From: hamusuke Date: Mon, 10 Apr 2023 09:20:12 +0900 Subject: [PATCH] feat: add ability to buy resin with primogems (#2104) * feat: add ability to buy resin with primogems * ResinManager#buy returns RetCode value --- .../proto/ResinChangeNotifyOuterClass.java | 50 ------------------- .../game/managers/ResinManager.java | 30 +++++++++-- .../emu/grasscutter/game/player/Player.java | 11 ++-- .../props/ItemUseAction/ItemUseAddItem.java | 3 +- .../packet/recv/HandlerBuyResinReq.java | 16 ++++++ .../server/packet/send/PacketBuyResinRsp.java | 18 +++++++ .../packet/send/PacketResinChangeNotify.java | 21 ++++---- 7 files changed, 81 insertions(+), 68 deletions(-) create mode 100644 src/main/java/emu/grasscutter/server/packet/recv/HandlerBuyResinReq.java create mode 100644 src/main/java/emu/grasscutter/server/packet/send/PacketBuyResinRsp.java diff --git a/src/generated/main/java/emu/grasscutter/net/proto/ResinChangeNotifyOuterClass.java b/src/generated/main/java/emu/grasscutter/net/proto/ResinChangeNotifyOuterClass.java index f51c98df861..f76117abb67 100644 --- a/src/generated/main/java/emu/grasscutter/net/proto/ResinChangeNotifyOuterClass.java +++ b/src/generated/main/java/emu/grasscutter/net/proto/ResinChangeNotifyOuterClass.java @@ -25,31 +25,18 @@ public interface ResinChangeNotifyOrBuilder extends int getCurValue(); /** - *
-     *KDJDKFKHOEN snootodo swapperoo 2
-     * 
- * * uint32 next_add_timestamp = 13; * @return The nextAddTimestamp. */ int getNextAddTimestamp(); /** - *
-     *NJLHPIKCNOP snootodo swapperoo 2
-     * 
- * * uint32 cur_buy_count = 12; * @return The curBuyCount. */ int getCurBuyCount(); } /** - *
-   * Name: IBIMGHOHNDK
-   * CmdId: 649
-   * 
- * * Protobuf type {@code ResinChangeNotify} */ public static final class ResinChangeNotify extends @@ -155,10 +142,6 @@ public int getCurValue() { public static final int NEXT_ADD_TIMESTAMP_FIELD_NUMBER = 13; private int nextAddTimestamp_; /** - *
-     *KDJDKFKHOEN snootodo swapperoo 2
-     * 
- * * uint32 next_add_timestamp = 13; * @return The nextAddTimestamp. */ @@ -170,10 +153,6 @@ public int getNextAddTimestamp() { public static final int CUR_BUY_COUNT_FIELD_NUMBER = 12; private int curBuyCount_; /** - *
-     *NJLHPIKCNOP snootodo swapperoo 2
-     * 
- * * uint32 cur_buy_count = 12; * @return The curBuyCount. */ @@ -360,11 +339,6 @@ protected Builder newBuilderForType( return builder; } /** - *
-     * Name: IBIMGHOHNDK
-     * CmdId: 649
-     * 
- * * Protobuf type {@code ResinChangeNotify} */ public static final class Builder extends @@ -556,10 +530,6 @@ public Builder clearCurValue() { private int nextAddTimestamp_ ; /** - *
-       *KDJDKFKHOEN snootodo swapperoo 2
-       * 
- * * uint32 next_add_timestamp = 13; * @return The nextAddTimestamp. */ @@ -568,10 +538,6 @@ public int getNextAddTimestamp() { return nextAddTimestamp_; } /** - *
-       *KDJDKFKHOEN snootodo swapperoo 2
-       * 
- * * uint32 next_add_timestamp = 13; * @param value The nextAddTimestamp to set. * @return This builder for chaining. @@ -583,10 +549,6 @@ public Builder setNextAddTimestamp(int value) { return this; } /** - *
-       *KDJDKFKHOEN snootodo swapperoo 2
-       * 
- * * uint32 next_add_timestamp = 13; * @return This builder for chaining. */ @@ -599,10 +561,6 @@ public Builder clearNextAddTimestamp() { private int curBuyCount_ ; /** - *
-       *NJLHPIKCNOP snootodo swapperoo 2
-       * 
- * * uint32 cur_buy_count = 12; * @return The curBuyCount. */ @@ -611,10 +569,6 @@ public int getCurBuyCount() { return curBuyCount_; } /** - *
-       *NJLHPIKCNOP snootodo swapperoo 2
-       * 
- * * uint32 cur_buy_count = 12; * @param value The curBuyCount to set. * @return This builder for chaining. @@ -626,10 +580,6 @@ public Builder setCurBuyCount(int value) { return this; } /** - *
-       *NJLHPIKCNOP snootodo swapperoo 2
-       * 
- * * uint32 cur_buy_count = 12; * @return This builder for chaining. */ diff --git a/src/main/java/emu/grasscutter/game/managers/ResinManager.java b/src/main/java/emu/grasscutter/game/managers/ResinManager.java index d6056571ed9..6e865b525f4 100644 --- a/src/main/java/emu/grasscutter/game/managers/ResinManager.java +++ b/src/main/java/emu/grasscutter/game/managers/ResinManager.java @@ -1,16 +1,22 @@ package emu.grasscutter.game.managers; -import static emu.grasscutter.config.Configuration.GAME_OPTIONS; - +import emu.grasscutter.game.inventory.GameItem; import emu.grasscutter.game.player.BasePlayerManager; import emu.grasscutter.game.player.Player; +import emu.grasscutter.game.props.ActionReason; import emu.grasscutter.game.props.PlayerProperty; import emu.grasscutter.game.props.WatcherTriggerType; -import emu.grasscutter.server.packet.send.PacketPlayerPropNotify; +import emu.grasscutter.net.proto.RetcodeOuterClass; +import emu.grasscutter.server.packet.send.PacketItemAddHintNotify; import emu.grasscutter.server.packet.send.PacketResinChangeNotify; import emu.grasscutter.utils.Utils; +import static emu.grasscutter.config.Configuration.GAME_OPTIONS; + public class ResinManager extends BasePlayerManager { + public static final int MAX_RESIN_BUYING_COUNT = 6; + public static final int AMOUNT_TO_ADD = 60; + public static final int[] HCOIN_NUM_TO_BUY_RESIN = new int[]{50, 100, 100, 150, 200, 200}; public ResinManager(Player player) { super(player); @@ -146,4 +152,22 @@ public synchronized void onPlayerLogin() { // Send initial notifications on logon. this.player.sendPacket(new PacketResinChangeNotify(this.player)); } + + public int buy() { + if (this.player.getResinBuyCount() >= MAX_RESIN_BUYING_COUNT) { + return RetcodeOuterClass.Retcode.RET_RESIN_BOUGHT_COUNT_EXCEEDED_VALUE; + } + + var res = this.player.getInventory().payItem(201, HCOIN_NUM_TO_BUY_RESIN[this.player.getResinBuyCount()]); + if (!res) { + return RetcodeOuterClass.Retcode.RET_HCOIN_NOT_ENOUGH_VALUE; + } + + this.player.setResinBuyCount(this.player.getResinBuyCount() + 1); + this.player.setProperty(PlayerProperty.PROP_PLAYER_WAIT_SUB_HCOIN, 0); + this.addResin(AMOUNT_TO_ADD); + this.player.sendPacket(new PacketItemAddHintNotify(new GameItem(106, AMOUNT_TO_ADD), ActionReason.BuyResin)); + + return 0; + } } diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index 9478288de14..4039860b5a9 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -28,13 +28,14 @@ import emu.grasscutter.game.inventory.Inventory; import emu.grasscutter.game.mail.Mail; import emu.grasscutter.game.mail.MailHandler; -import emu.grasscutter.game.managers.cooking.ActiveCookCompoundData; -import emu.grasscutter.game.managers.cooking.CookingCompoundManager; -import emu.grasscutter.game.managers.cooking.CookingManager; import emu.grasscutter.game.managers.FurnitureManager; import emu.grasscutter.game.managers.ResinManager; import emu.grasscutter.game.managers.SotSManager; import emu.grasscutter.game.managers.SatiationManager; +import emu.grasscutter.game.managers.SotSManager; +import emu.grasscutter.game.managers.cooking.ActiveCookCompoundData; +import emu.grasscutter.game.managers.cooking.CookingCompoundManager; +import emu.grasscutter.game.managers.cooking.CookingManager; import emu.grasscutter.game.managers.deforestation.DeforestationManager; import emu.grasscutter.game.managers.energy.EnergyManager; import emu.grasscutter.game.managers.forging.ActiveForgeData; @@ -208,6 +209,7 @@ public class Player { @Getter @Setter private long springLastUsed; private HashMap mapMarks; // Getter makes an empty hashmap - maybe do this elsewhere? @Getter @Setter private int nextResinRefresh; + @Getter @Setter private int resinBuyCount; @Getter @Setter private int lastDailyReset; @Getter private transient MpSettingType mpSetting = MpSettingType.MP_SETTING_TYPE_ENTER_AFTER_APPLY; // TODO @Getter private long playerGameTime = 0; @@ -1295,6 +1297,9 @@ private synchronized void doDailyReset() { this.getBattlePassManager().resetWeeklyMissions(); } + // Reset resin-buying count. + this.setResinBuyCount(0); + // Done. Update last reset time. this.setLastDailyReset(currentTime); } diff --git a/src/main/java/emu/grasscutter/game/props/ItemUseAction/ItemUseAddItem.java b/src/main/java/emu/grasscutter/game/props/ItemUseAction/ItemUseAddItem.java index 9f5c7ad39a5..0d36d486fb6 100644 --- a/src/main/java/emu/grasscutter/game/props/ItemUseAction/ItemUseAddItem.java +++ b/src/main/java/emu/grasscutter/game/props/ItemUseAction/ItemUseAddItem.java @@ -1,5 +1,6 @@ package emu.grasscutter.game.props.ItemUseAction; +import emu.grasscutter.game.props.ActionReason; import emu.grasscutter.game.props.ItemUseOp; public class ItemUseAddItem extends ItemUseInt { @@ -19,6 +20,6 @@ public ItemUseAddItem(String[] useParam) { @Override public boolean useItem(UseItemParams params) { - return params.player.getInventory().addItem(this.i, this.count * params.count); + return params.player.getInventory().addItem(this.i, this.count * params.count, ActionReason.PlayerUseItem); } } diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerBuyResinReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerBuyResinReq.java new file mode 100644 index 00000000000..13ada5de721 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerBuyResinReq.java @@ -0,0 +1,16 @@ +package emu.grasscutter.server.packet.recv; + +import emu.grasscutter.net.packet.Opcodes; +import emu.grasscutter.net.packet.PacketHandler; +import emu.grasscutter.net.packet.PacketOpcodes; +import emu.grasscutter.server.game.GameSession; +import emu.grasscutter.server.packet.send.PacketBuyResinRsp; + +@Opcodes(PacketOpcodes.BuyResinReq) +public class HandlerBuyResinReq extends PacketHandler { + @Override + public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { + var player = session.getPlayer(); + session.send(new PacketBuyResinRsp(player, player.getResinManager().buy())); + } +} diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketBuyResinRsp.java b/src/main/java/emu/grasscutter/server/packet/send/PacketBuyResinRsp.java new file mode 100644 index 00000000000..07f8bba01ce --- /dev/null +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketBuyResinRsp.java @@ -0,0 +1,18 @@ +package emu.grasscutter.server.packet.send; + +import emu.grasscutter.game.player.Player; +import emu.grasscutter.game.props.PlayerProperty; +import emu.grasscutter.net.packet.BasePacket; +import emu.grasscutter.net.packet.PacketOpcodes; +import emu.grasscutter.net.proto.BuyResinRspOuterClass; + +public class PacketBuyResinRsp extends BasePacket { + public PacketBuyResinRsp(Player player, int ret) { + super(PacketOpcodes.BuyResinRsp); + + this.setData(BuyResinRspOuterClass.BuyResinRsp.newBuilder() + .setCurValue(player.getProperty(PlayerProperty.PROP_PLAYER_RESIN)) + .setRetcode(ret) + .build()); + } +} diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketResinChangeNotify.java b/src/main/java/emu/grasscutter/server/packet/send/PacketResinChangeNotify.java index b61c1ab0aae..5f9b8bd92a7 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketResinChangeNotify.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketResinChangeNotify.java @@ -7,17 +7,16 @@ import emu.grasscutter.net.proto.ResinChangeNotifyOuterClass.ResinChangeNotify; public class PacketResinChangeNotify extends BasePacket { - + public PacketResinChangeNotify(Player player) { - super(PacketOpcodes.ResinChangeNotify); - - ResinChangeNotify proto = ResinChangeNotify.newBuilder() - .setCurValue(player.getProperty(PlayerProperty.PROP_PLAYER_RESIN)) - .setNextAddTimestamp(player.getNextResinRefresh()) - .build(); + super(PacketOpcodes.ResinChangeNotify); + + ResinChangeNotify proto = ResinChangeNotify.newBuilder() + .setCurValue(player.getProperty(PlayerProperty.PROP_PLAYER_RESIN)) + .setNextAddTimestamp(player.getNextResinRefresh()) + .setCurBuyCount(player.getResinBuyCount()) + .build(); - // ToDo: Add ability to buy resin with primogems, has to be included here. - - this.setData(proto); - } + this.setData(proto); + } }