Skip to content

Commit

Permalink
feat: add ability to buy resin with primogems (Grasscutters#2104)
Browse files Browse the repository at this point in the history
* feat: add ability to buy resin with primogems

* ResinManager#buy returns RetCode value
  • Loading branch information
hamusuke0323 authored and eternalcomet committed Apr 12, 2023
1 parent aa9778f commit 3c03bfe
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 68 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 27 additions & 3 deletions src/main/java/emu/grasscutter/game/managers/ResinManager.java
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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;
}
}
11 changes: 8 additions & 3 deletions src/main/java/emu/grasscutter/game/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -208,6 +209,7 @@ public class Player {
@Getter @Setter private long springLastUsed;
private HashMap<String, MapMark> 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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3c03bfe

Please sign in to comment.