Implement item quick growth

This commit is contained in:
Melledy
2025-11-08 22:00:06 -08:00
parent 2c18963b08
commit 66ce7cf698
4 changed files with 55 additions and 9 deletions

View File

@@ -537,11 +537,16 @@ public class Inventory extends PlayerManager implements GameDatabaseObject {
// Utility functions // Utility functions
public PlayerChangeInfo produce(int id, int num) { public PlayerChangeInfo produce(int id, int num, PlayerChangeInfo change) {
// Init change info
if (change == null) {
change = new PlayerChangeInfo();
}
// Get production data // Get production data
var data = GameData.getProductionDataTable().get(id); var data = GameData.getProductionDataTable().get(id);
if (data == null) { if (data == null) {
return null; return change;
} }
// Get materials // Get materials
@@ -549,12 +554,9 @@ public class Inventory extends PlayerManager implements GameDatabaseObject {
// Verify that we have the materials // Verify that we have the materials
if (!this.verifyItems(materials)) { if (!this.verifyItems(materials)) {
return null; return change;
} }
// Create change info
var change = new PlayerChangeInfo();
// Remove items // Remove items
this.removeItems(materials, change); this.removeItems(materials, change);

View File

@@ -27,6 +27,10 @@ public class PlayerChangeInfo {
return this; return this;
} }
public boolean isEmpty() {
return this.list == null || this.list.isEmpty();
}
public void add(ProtoMessage<?> proto) { public void add(ProtoMessage<?> proto) {
var any = Any.newInstance() var any = Any.newInstance()
.setTypeUrl(GameConstants.PROTO_BASE_TYPE_URL + proto.getClass().getSimpleName()) .setTypeUrl(GameConstants.PROTO_BASE_TYPE_URL + proto.getClass().getSimpleName())

View File

@@ -15,14 +15,14 @@ public class HandlerItemProductReq extends NetHandler {
var req = ItemProductReq.parseFrom(message); var req = ItemProductReq.parseFrom(message);
// Produce item // Produce item
var changes = session.getPlayer().getInventory().produce(req.getId(), req.getNum()); var change = session.getPlayer().getInventory().produce(req.getId(), req.getNum(), null);
if (changes == null) { if (change.isEmpty()) {
return session.encodeMsg(NetMsgId.item_product_failed_ack); return session.encodeMsg(NetMsgId.item_product_failed_ack);
} }
// Send response // Send response
return session.encodeMsg(NetMsgId.item_product_succeed_ack, changes.toProto()); return session.encodeMsg(NetMsgId.item_product_succeed_ack, change.toProto());
} }
} }

View File

@@ -0,0 +1,40 @@
package emu.nebula.server.handlers;
import emu.nebula.net.NetHandler;
import emu.nebula.net.NetMsgId;
import emu.nebula.proto.ItemQuickGrowth.ItemGrowthReq;
import emu.nebula.net.HandlerId;
import emu.nebula.game.player.PlayerChangeInfo;
import emu.nebula.net.GameSession;
@HandlerId(NetMsgId.item_quick_growth_req)
public class HandlerItemQuickGrowthReq extends NetHandler {
@Override
public byte[] handle(GameSession session, byte[] message) throws Exception {
// Parse request
var req = ItemGrowthReq.parseFrom(message);
// Init change
var change = new PlayerChangeInfo();
// Create items
for (var item : req.getList()) {
if (item.hasProduct()) {
session.getPlayer().getInventory().produce(
item.getProduct().getId(),
item.getProduct().getNum(),
change
);
}
}
if (change.isEmpty()) {
return session.encodeMsg(NetMsgId.item_quick_growth_failed_ack);
}
// Send response
return session.encodeMsg(NetMsgId.item_quick_growth_succeed_ack, change.toProto());
}
}