-
Notifications
You must be signed in to change notification settings - Fork 7
ItemStack refactor #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c440ecd
Non-function base struct and functions.
PauMAVA 7f83704
Remove dummy struct and move enchantments.rs into items crate.
PauMAVA c677b94
Add ItemStackMeta, handle ItemStack serialization and deserialization.
PauMAVA 54294b3
Add some stack count limitations.
PauMAVA a8025d2
Restricted stack sizes on set_count and set_item. Add unchecked_add a…
PauMAVA 0bb498c
Add unchecked_set_item.
PauMAVA 1e064ea
Make ItemMeta optional.
PauMAVA d008bfa
Use TryFrom and Into in serialization and deserialization. Autogenera…
PauMAVA f6a2304
Use &'static str instead of String and delete extra generate_enum_* f…
PauMAVA 816782e
Item operations error handling. Slot type WIP.
PauMAVA c64f103
Impl FromStr for Item, improve take_half, use as_ref instead of clone…
PauMAVA b0afab3
WIP Does not work. Migrating to NonZeroU32 and implementing Inventory…
PauMAVA 7e4a013
Now split and split_half will clone.
PauMAVA ed86f76
Merge branch 'main' into items
caelunshun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 <paumachetti@gmail.com>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
serde = { version = "1", features = ["derive"] } | ||
serde = { version = "1", features = ["derive"] } |
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.