mirror of
https://github.com/Melledy/Nebula.git
synced 2025-12-14 13:24:43 +01:00
Implement item quick growth
This commit is contained in:
@@ -537,11 +537,16 @@ public class Inventory extends PlayerManager implements GameDatabaseObject {
|
||||
|
||||
// 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
|
||||
var data = GameData.getProductionDataTable().get(id);
|
||||
if (data == null) {
|
||||
return null;
|
||||
return change;
|
||||
}
|
||||
|
||||
// Get materials
|
||||
@@ -549,12 +554,9 @@ public class Inventory extends PlayerManager implements GameDatabaseObject {
|
||||
|
||||
// Verify that we have the materials
|
||||
if (!this.verifyItems(materials)) {
|
||||
return null;
|
||||
return change;
|
||||
}
|
||||
|
||||
// Create change info
|
||||
var change = new PlayerChangeInfo();
|
||||
|
||||
// Remove items
|
||||
this.removeItems(materials, change);
|
||||
|
||||
|
||||
@@ -27,6 +27,10 @@ public class PlayerChangeInfo {
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.list == null || this.list.isEmpty();
|
||||
}
|
||||
|
||||
public void add(ProtoMessage<?> proto) {
|
||||
var any = Any.newInstance()
|
||||
.setTypeUrl(GameConstants.PROTO_BASE_TYPE_URL + proto.getClass().getSimpleName())
|
||||
|
||||
@@ -15,14 +15,14 @@ public class HandlerItemProductReq extends NetHandler {
|
||||
var req = ItemProductReq.parseFrom(message);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Send response
|
||||
return session.encodeMsg(NetMsgId.item_product_succeed_ack, changes.toProto());
|
||||
return session.encodeMsg(NetMsgId.item_product_succeed_ack, change.toProto());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user