-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from PauMAVA/items
ItemStack refactor Work on this is incomplete and will be followed up by PRs in Feather once `libcraft` is transferred to the new monorepo.
- Loading branch information
Showing
10 changed files
with
456 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule minecraft-data
updated
13 files
+8 −1 | .github/workflows/tag.yml | |
+1 −0 | README.md | |
+200 −28 | data/pc/1.10/enchantments.json | |
+218 −31 | data/pc/1.11/enchantments.json | |
+218 −31 | data/pc/1.12/enchantments.json | |
+744 −88 | data/pc/1.13.2/enchantments.json | |
+103 −35 | data/pc/1.13/enchantments.json | |
+115 −39 | data/pc/1.16.4/enchantments.json | |
+184 −26 | data/pc/1.7/enchantments.json | |
+184 −26 | data/pc/1.8/enchantments.json | |
+200 −28 | data/pc/1.9/enchantments.json | |
+7 −0 | data/pc/common/protocolVersions.json | |
+6 −0 | doc/history.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
[package] | ||
name = "libcraft-items" | ||
version = "0.1.0" | ||
authors = ["Kalle Kankaanpää"] | ||
authors = ["Kalle Kankaanpää", "Pau Machetti <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
serde = { version = "1", features = ["derive"] } | ||
serde = { version = "1", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::item_stack::ItemStackError; | ||
use crate::ItemStack; | ||
use core::mem; | ||
|
||
/// Represents an Inventory slot. May be empty | ||
/// or filled (contains an `ItemStack`). | ||
pub enum InventorySlot { | ||
Filled(ItemStack), | ||
Empty, | ||
} | ||
|
||
impl Default for InventorySlot { | ||
fn default() -> Self { | ||
InventorySlot::Empty | ||
} | ||
} | ||
|
||
impl InventorySlot { | ||
/// Tries to take all items from the `InventorySlot`. | ||
/// If the `InventorySlot` is filled returns the `ItemStack`. | ||
/// If the `InventorySlot` is empty returns `None`. | ||
pub fn take_all(&mut self) -> Option<ItemStack> { | ||
match mem::take(self) { | ||
Self::Filled(stack) => Some(stack), | ||
Self::Empty => None, | ||
} | ||
} | ||
|
||
/// Tries to take (split) the specified amount from the associated | ||
/// `ItemStack` to this `InventorySlot`. If this `InventorySlot` is | ||
/// empty, the specified amount is zero, or the resulting stack is | ||
/// empty will return the `ItemStackError::EmptyStack` error. If the | ||
/// amount to take is bigger than the current amount it will return | ||
/// the `ItemStackError::NotEnoughAmount` error. On success returns | ||
/// the taken part of the `ItemStack` as a new one. | ||
pub fn take(&mut self, amount: u32) -> Result<ItemStack, ItemStackError> { | ||
let split = match mem::take(self) { | ||
Self::Empty => return Err(ItemStackError::EmptyStack), | ||
Self::Filled(mut stack) => stack.split(amount), | ||
}; | ||
let (stack, res) = match split { | ||
Ok((original, new)) => (original, Ok(new)), | ||
Err((original, error)) => (Some(original), Err(error)), | ||
}; | ||
if let Some(stack) = stack { | ||
*self = Self::Filled(stack) | ||
} | ||
res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.