From dca4a211f3627a159b788692ea0b1d9457557bee Mon Sep 17 00:00:00 2001 From: Melledy <121644117+Melledy@users.noreply.github.com> Date: Sun, 19 May 2024 02:31:15 -0700 Subject: [PATCH] Implement destroying materials from the inventory menu --- .../proto/DestroyItemCsReqOuterClass.java | 422 ++++++++++++++++++ .../game/inventory/InventoryService.java | 12 + .../packet/recv/HandlerDestroyItemCsReq.java | 20 + 3 files changed, 454 insertions(+) create mode 100644 src/generated/main/emu/lunarcore/proto/DestroyItemCsReqOuterClass.java create mode 100644 src/main/java/emu/lunarcore/server/packet/recv/HandlerDestroyItemCsReq.java diff --git a/src/generated/main/emu/lunarcore/proto/DestroyItemCsReqOuterClass.java b/src/generated/main/emu/lunarcore/proto/DestroyItemCsReqOuterClass.java new file mode 100644 index 000000000..d726ce015 --- /dev/null +++ b/src/generated/main/emu/lunarcore/proto/DestroyItemCsReqOuterClass.java @@ -0,0 +1,422 @@ +// Code generated by protocol buffer compiler. Do not edit! +package emu.lunarcore.proto; + +import java.io.IOException; +import us.hebi.quickbuf.FieldName; +import us.hebi.quickbuf.InvalidProtocolBufferException; +import us.hebi.quickbuf.JsonSink; +import us.hebi.quickbuf.JsonSource; +import us.hebi.quickbuf.MessageFactory; +import us.hebi.quickbuf.ProtoMessage; +import us.hebi.quickbuf.ProtoSink; +import us.hebi.quickbuf.ProtoSource; + +public final class DestroyItemCsReqOuterClass { + /** + * Protobuf type {@code DestroyItemCsReq} + */ + public static final class DestroyItemCsReq extends ProtoMessage implements Cloneable { + private static final long serialVersionUID = 0L; + + /** + * optional uint32 item_id = 1; + */ + private int itemId; + + /** + * optional uint32 item_count = 9; + */ + private int itemCount; + + /** + * optional uint32 cur_item_count = 11; + */ + private int curItemCount; + + private DestroyItemCsReq() { + } + + /** + * @return a new empty instance of {@code DestroyItemCsReq} + */ + public static DestroyItemCsReq newInstance() { + return new DestroyItemCsReq(); + } + + /** + * optional uint32 item_id = 1; + * @return whether the itemId field is set + */ + public boolean hasItemId() { + return (bitField0_ & 0x00000001) != 0; + } + + /** + * optional uint32 item_id = 1; + * @return this + */ + public DestroyItemCsReq clearItemId() { + bitField0_ &= ~0x00000001; + itemId = 0; + return this; + } + + /** + * optional uint32 item_id = 1; + * @return the itemId + */ + public int getItemId() { + return itemId; + } + + /** + * optional uint32 item_id = 1; + * @param value the itemId to set + * @return this + */ + public DestroyItemCsReq setItemId(final int value) { + bitField0_ |= 0x00000001; + itemId = value; + return this; + } + + /** + * optional uint32 item_count = 9; + * @return whether the itemCount field is set + */ + public boolean hasItemCount() { + return (bitField0_ & 0x00000002) != 0; + } + + /** + * optional uint32 item_count = 9; + * @return this + */ + public DestroyItemCsReq clearItemCount() { + bitField0_ &= ~0x00000002; + itemCount = 0; + return this; + } + + /** + * optional uint32 item_count = 9; + * @return the itemCount + */ + public int getItemCount() { + return itemCount; + } + + /** + * optional uint32 item_count = 9; + * @param value the itemCount to set + * @return this + */ + public DestroyItemCsReq setItemCount(final int value) { + bitField0_ |= 0x00000002; + itemCount = value; + return this; + } + + /** + * optional uint32 cur_item_count = 11; + * @return whether the curItemCount field is set + */ + public boolean hasCurItemCount() { + return (bitField0_ & 0x00000004) != 0; + } + + /** + * optional uint32 cur_item_count = 11; + * @return this + */ + public DestroyItemCsReq clearCurItemCount() { + bitField0_ &= ~0x00000004; + curItemCount = 0; + return this; + } + + /** + * optional uint32 cur_item_count = 11; + * @return the curItemCount + */ + public int getCurItemCount() { + return curItemCount; + } + + /** + * optional uint32 cur_item_count = 11; + * @param value the curItemCount to set + * @return this + */ + public DestroyItemCsReq setCurItemCount(final int value) { + bitField0_ |= 0x00000004; + curItemCount = value; + return this; + } + + @Override + public DestroyItemCsReq copyFrom(final DestroyItemCsReq other) { + cachedSize = other.cachedSize; + if ((bitField0_ | other.bitField0_) != 0) { + bitField0_ = other.bitField0_; + itemId = other.itemId; + itemCount = other.itemCount; + curItemCount = other.curItemCount; + } + return this; + } + + @Override + public DestroyItemCsReq mergeFrom(final DestroyItemCsReq other) { + if (other.isEmpty()) { + return this; + } + cachedSize = -1; + if (other.hasItemId()) { + setItemId(other.itemId); + } + if (other.hasItemCount()) { + setItemCount(other.itemCount); + } + if (other.hasCurItemCount()) { + setCurItemCount(other.curItemCount); + } + return this; + } + + @Override + public DestroyItemCsReq clear() { + if (isEmpty()) { + return this; + } + cachedSize = -1; + bitField0_ = 0; + itemId = 0; + itemCount = 0; + curItemCount = 0; + return this; + } + + @Override + public DestroyItemCsReq clearQuick() { + if (isEmpty()) { + return this; + } + cachedSize = -1; + bitField0_ = 0; + return this; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + if (!(o instanceof DestroyItemCsReq)) { + return false; + } + DestroyItemCsReq other = (DestroyItemCsReq) o; + return bitField0_ == other.bitField0_ + && (!hasItemId() || itemId == other.itemId) + && (!hasItemCount() || itemCount == other.itemCount) + && (!hasCurItemCount() || curItemCount == other.curItemCount); + } + + @Override + public void writeTo(final ProtoSink output) throws IOException { + if ((bitField0_ & 0x00000001) != 0) { + output.writeRawByte((byte) 8); + output.writeUInt32NoTag(itemId); + } + if ((bitField0_ & 0x00000002) != 0) { + output.writeRawByte((byte) 72); + output.writeUInt32NoTag(itemCount); + } + if ((bitField0_ & 0x00000004) != 0) { + output.writeRawByte((byte) 88); + output.writeUInt32NoTag(curItemCount); + } + } + + @Override + protected int computeSerializedSize() { + int size = 0; + if ((bitField0_ & 0x00000001) != 0) { + size += 1 + ProtoSink.computeUInt32SizeNoTag(itemId); + } + if ((bitField0_ & 0x00000002) != 0) { + size += 1 + ProtoSink.computeUInt32SizeNoTag(itemCount); + } + if ((bitField0_ & 0x00000004) != 0) { + size += 1 + ProtoSink.computeUInt32SizeNoTag(curItemCount); + } + return size; + } + + @Override + @SuppressWarnings("fallthrough") + public DestroyItemCsReq mergeFrom(final ProtoSource input) throws IOException { + // Enabled Fall-Through Optimization (QuickBuffers) + int tag = input.readTag(); + while (true) { + switch (tag) { + case 8: { + // itemId + itemId = input.readUInt32(); + bitField0_ |= 0x00000001; + tag = input.readTag(); + if (tag != 72) { + break; + } + } + case 72: { + // itemCount + itemCount = input.readUInt32(); + bitField0_ |= 0x00000002; + tag = input.readTag(); + if (tag != 88) { + break; + } + } + case 88: { + // curItemCount + curItemCount = input.readUInt32(); + bitField0_ |= 0x00000004; + tag = input.readTag(); + if (tag != 0) { + break; + } + } + case 0: { + return this; + } + default: { + if (!input.skipField(tag)) { + return this; + } + tag = input.readTag(); + break; + } + } + } + } + + @Override + public void writeTo(final JsonSink output) throws IOException { + output.beginObject(); + if ((bitField0_ & 0x00000001) != 0) { + output.writeUInt32(FieldNames.itemId, itemId); + } + if ((bitField0_ & 0x00000002) != 0) { + output.writeUInt32(FieldNames.itemCount, itemCount); + } + if ((bitField0_ & 0x00000004) != 0) { + output.writeUInt32(FieldNames.curItemCount, curItemCount); + } + output.endObject(); + } + + @Override + public DestroyItemCsReq mergeFrom(final JsonSource input) throws IOException { + if (!input.beginObject()) { + return this; + } + while (!input.isAtEnd()) { + switch (input.readFieldHash()) { + case -1178662002: + case 2116204999: { + if (input.isAtField(FieldNames.itemId)) { + if (!input.trySkipNullValue()) { + itemId = input.readUInt32(); + bitField0_ |= 0x00000001; + } + } else { + input.skipUnknownField(); + } + break; + } + case 2127813052: + case -1966910237: { + if (input.isAtField(FieldNames.itemCount)) { + if (!input.trySkipNullValue()) { + itemCount = input.readUInt32(); + bitField0_ |= 0x00000002; + } + } else { + input.skipUnknownField(); + } + break; + } + case -986513572: + case -1822146494: { + if (input.isAtField(FieldNames.curItemCount)) { + if (!input.trySkipNullValue()) { + curItemCount = input.readUInt32(); + bitField0_ |= 0x00000004; + } + } else { + input.skipUnknownField(); + } + break; + } + default: { + input.skipUnknownField(); + break; + } + } + } + input.endObject(); + return this; + } + + @Override + public DestroyItemCsReq clone() { + return new DestroyItemCsReq().copyFrom(this); + } + + @Override + public boolean isEmpty() { + return ((bitField0_) == 0); + } + + public static DestroyItemCsReq parseFrom(final byte[] data) throws + InvalidProtocolBufferException { + return ProtoMessage.mergeFrom(new DestroyItemCsReq(), data).checkInitialized(); + } + + public static DestroyItemCsReq parseFrom(final ProtoSource input) throws IOException { + return ProtoMessage.mergeFrom(new DestroyItemCsReq(), input).checkInitialized(); + } + + public static DestroyItemCsReq parseFrom(final JsonSource input) throws IOException { + return ProtoMessage.mergeFrom(new DestroyItemCsReq(), input).checkInitialized(); + } + + /** + * @return factory for creating DestroyItemCsReq messages + */ + public static MessageFactory getFactory() { + return DestroyItemCsReqFactory.INSTANCE; + } + + private enum DestroyItemCsReqFactory implements MessageFactory { + INSTANCE; + + @Override + public DestroyItemCsReq create() { + return DestroyItemCsReq.newInstance(); + } + } + + /** + * Contains name constants used for serializing JSON + */ + static class FieldNames { + static final FieldName itemId = FieldName.forField("itemId", "item_id"); + + static final FieldName itemCount = FieldName.forField("itemCount", "item_count"); + + static final FieldName curItemCount = FieldName.forField("curItemCount", "cur_item_count"); + } + } +} diff --git a/src/main/java/emu/lunarcore/game/inventory/InventoryService.java b/src/main/java/emu/lunarcore/game/inventory/InventoryService.java index 09ee204fb..b3b41a8b4 100644 --- a/src/main/java/emu/lunarcore/game/inventory/InventoryService.java +++ b/src/main/java/emu/lunarcore/game/inventory/InventoryService.java @@ -580,6 +580,18 @@ public List sellItems(Player player, boolean toMaterials, List composeItem(Player player, int composeId, int count, List costItems) { // Sanity check if (count <= 0) return null; diff --git a/src/main/java/emu/lunarcore/server/packet/recv/HandlerDestroyItemCsReq.java b/src/main/java/emu/lunarcore/server/packet/recv/HandlerDestroyItemCsReq.java new file mode 100644 index 000000000..30c2508c2 --- /dev/null +++ b/src/main/java/emu/lunarcore/server/packet/recv/HandlerDestroyItemCsReq.java @@ -0,0 +1,20 @@ +package emu.lunarcore.server.packet.recv; + +import emu.lunarcore.proto.DestroyItemCsReqOuterClass.DestroyItemCsReq; +import emu.lunarcore.server.game.GameSession; +import emu.lunarcore.server.packet.CmdId; +import emu.lunarcore.server.packet.Opcodes; +import emu.lunarcore.server.packet.PacketHandler; + +@Opcodes(CmdId.DestroyItemCsReq) +public class HandlerDestroyItemCsReq extends PacketHandler { + + @Override + public void handle(GameSession session, byte[] data) throws Exception { + var req = DestroyItemCsReq.parseFrom(data); + + session.getServer().getInventoryService().destroyItem(session.getPlayer(), req.getItemId(), req.getItemCount()); + session.send(CmdId.DestroyItemScRsp); + } + +}