From 69e8e4055e1881729b58622051cbf2691bb36a93 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Wed, 25 Jun 2025 17:02:19 -0400 Subject: [PATCH] Finish implementing currency I made various mistakes in the initial implementation, which are now fixed. --- resources/opcodes.json | 2 +- src/inventory/currency.rs | 2 +- src/inventory/mod.rs | 5 ++++- src/inventory/storage.rs | 1 + src/ipc/zone/currency_info.rs | 4 +++- src/world/connection.rs | 8 ++++---- src/world/lua.rs | 13 ++++++++----- 7 files changed, 22 insertions(+), 13 deletions(-) diff --git a/resources/opcodes.json b/resources/opcodes.json index 86ac739..464b138 100644 --- a/resources/opcodes.json +++ b/resources/opcodes.json @@ -173,7 +173,7 @@ { "name": "CurrencyCrystalInfo", "opcode": 548, - "size": 32 + "size": 24 } ], "ClientZoneIpcType": [ diff --git a/src/inventory/currency.rs b/src/inventory/currency.rs index 002d268..f30e8a3 100644 --- a/src/inventory/currency.rs +++ b/src/inventory/currency.rs @@ -21,7 +21,7 @@ impl Storage for CurrencyStorage { } fn num_items(&self) -> u32 { - self.gil.quantity + 1 } fn get_slot_mut(&mut self, index: u16) -> &mut Item { diff --git a/src/inventory/mod.rs b/src/inventory/mod.rs index 10ca597..b3a39b0 100644 --- a/src/inventory/mod.rs +++ b/src/inventory/mod.rs @@ -88,7 +88,7 @@ impl<'a> Iterator for InventoryIterator<'a> { let curr = self.curr; self.curr += 1; - if curr >= 17 { + if curr >= 18 { return None; } @@ -115,6 +115,9 @@ impl<'a> Iterator for InventoryIterator<'a> { // equipped 16 => ContainerType::Equipped, + + // currency + 17 => ContainerType::Currency, _ => panic!("Inventory iterator invalid!"), }; diff --git a/src/inventory/storage.rs b/src/inventory/storage.rs index b861fb0..11a6da6 100644 --- a/src/inventory/storage.rs +++ b/src/inventory/storage.rs @@ -2,6 +2,7 @@ use binrw::binrw; use super::Item; +/// When adding a new container type, make sure to add it to InventoryIterator #[binrw] #[brw(little)] #[brw(repr = u16)] diff --git a/src/ipc/zone/currency_info.rs b/src/ipc/zone/currency_info.rs index a9cd1de..5257399 100644 --- a/src/ipc/zone/currency_info.rs +++ b/src/ipc/zone/currency_info.rs @@ -1,11 +1,13 @@ use binrw::binrw; +use crate::inventory::ContainerType; + #[binrw] #[brw(little)] #[derive(Debug, Clone, Default)] pub struct CurrencyInfo { pub sequence: u32, - pub container: u16, + pub container: ContainerType, pub slot: u16, pub quantity: u32, pub unk1: u32, diff --git a/src/world/connection.rs b/src/world/connection.rs index b8489cb..ae3cb9c 100644 --- a/src/world/connection.rs +++ b/src/world/connection.rs @@ -487,16 +487,16 @@ impl ZoneConnection { for (container_type, container) in &self.player_data.inventory.clone() { // currencies if container_type == ContainerType::Currency { - let mut send_currency = async |slot_index: u16, item: &Item| { + let mut send_currency = async |item: &Item| { let ipc = ServerZoneIpcSegment { op_code: ServerZoneIpcType::CurrencyCrystalInfo, timestamp: timestamp_secs(), data: ServerZoneIpcData::CurrencyCrystalInfo(CurrencyInfo { sequence, - container: item.id as u16, - slot: slot_index, + container: container_type, quantity: item.quantity, catalog_id: item.id, + unk1: 1, ..Default::default() }), ..Default::default() @@ -512,7 +512,7 @@ impl ZoneConnection { }; for i in 0..container.max_slots() { - send_currency(i as u16, container.get_slot(i as u16)).await; + send_currency(container.get_slot(i as u16)).await; } } else { // items diff --git a/src/world/lua.rs b/src/world/lua.rs index da95c1e..bc1e119 100644 --- a/src/world/lua.rs +++ b/src/world/lua.rs @@ -225,11 +225,14 @@ impl LuaPlayer { impl UserData for LuaPlayer { fn add_methods>(methods: &mut M) { - methods.add_method_mut("send_message", |lua, this, (message, param): (String, Value)| { - let param: u8 = lua.from_value(param).unwrap_or(0); - this.send_message(&message, param); - Ok(()) - }); + methods.add_method_mut( + "send_message", + |lua, this, (message, param): (String, Value)| { + let param: u8 = lua.from_value(param).unwrap_or(0); + this.send_message(&message, param); + Ok(()) + }, + ); methods.add_method_mut( "give_status_effect", |_, this, (effect_id, duration): (u16, f32)| {