Implement buying energy

This commit is contained in:
Melledy
2025-11-01 08:14:34 -07:00
parent b4e008fb9a
commit 5bcff7d588
3 changed files with 52 additions and 1 deletions

View File

@@ -9,8 +9,9 @@ public class GameConstants {
public static final int INTRO_GUIDE_ID = 1;
public static final int GOLD_ITEM_ID = 1;
public static final int ENERGY_BUY_ITEM_ID = 2;
public static final int EXP_ITEM_ID = 21;
public static final int MAX_ENERGY = 240;
public static final int ENERGY_REGEN_TIME = 360; // Seconds

View File

@@ -2,6 +2,7 @@ package emu.nebula.game.inventory;
import java.util.List;
import emu.nebula.GameConstants;
import emu.nebula.Nebula;
import emu.nebula.data.GameData;
import emu.nebula.game.player.PlayerManager;
@@ -337,6 +338,25 @@ public class Inventory extends PlayerManager {
return change.setSuccess(true);
}
public PlayerChangeInfo buyEnergy() {
// Create change info
var change = new PlayerChangeInfo();
// Make sure we have the gems
if (!this.verifyItem(GameConstants.ENERGY_BUY_ITEM_ID, 30)) {
return change;
}
// Remove gems
this.removeItem(GameConstants.ENERGY_BUY_ITEM_ID, 30, change);
// Add energy
this.getPlayer().addEnergy(60, change);
// Success
return change;
}
public PlayerChangeInfo useItem(int id, int count, PlayerChangeInfo change) {
// Changes
if (change == null) {

View File

@@ -0,0 +1,30 @@
package emu.nebula.server.handlers;
import emu.nebula.net.NetHandler;
import emu.nebula.net.NetMsgId;
import emu.nebula.proto.EnergyBuy.EnergyBuyResp;
import emu.nebula.net.HandlerId;
import emu.nebula.net.GameSession;
@HandlerId(NetMsgId.energy_buy_req)
public class HandlerEnergyBuyReq extends NetHandler {
@Override
public byte[] handle(GameSession session, byte[] message) throws Exception {
// Buy energy
var change = session.getPlayer().getInventory().buyEnergy();
if (change == null) {
return session.encodeMsg(NetMsgId.energy_buy_failed_ack);
}
// Build response
var rsp = EnergyBuyResp.newInstance()
.setChange(change.toProto())
.setCount(0); // TODO max energy buy count per day
// Encode and send
return session.encodeMsg(NetMsgId.energy_buy_succeed_ack, rsp);
}
}