Skip to content
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

Respect item damage in material conversion #346

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ glutin = "0.31"
glutin-winit = "0.4"

arc-swap = "1.7.0"
# phf = "0.11"

[dependencies.leafish_resources]
path = "./resources"
Expand Down
49 changes: 17 additions & 32 deletions protocol/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,47 +128,42 @@ impl ItemMeta {
Some(tag) => tag
.1
.as_compound()
.map(|comp| comp.get("display").clone().map(|val| val.as_compound()))
.flatten()
.and_then(|comp| comp.get("display").map(|val| val.as_compound()))
.flatten(),
None => None,
}
}

pub fn display_name(&self) -> Option<Component> {
self.display()
.map(|val| {
.and_then(|val| {
val.get("Name")
.map(|name| name.as_str().map(|name| Component::from_str(name)))
.map(|name| name.as_str().map(Component::from_str))
})
.flatten()
.flatten()
}

pub fn lore(&self) -> Vec<Component> {
self.display()
.map(|val| {
.and_then(|val| {
val.get("Lore").map(|lore| {
lore.as_list().map(|lore| {
lore.iter()
.map(|line| line.as_str().map(|line| Component::from_str(line)))
.flatten()
.filter_map(|line| line.as_str().map(Component::from_str))
.collect::<Vec<_>>()
})
})
})
.flatten()
.flatten()
.unwrap_or(vec![])
.unwrap_or_default()
}

pub fn repair_cost(&self) -> Option<i32> {
match self.0.as_ref() {
Some(tag) => tag
.1
.as_compound()
.map(|comp| comp.get("RepairCost").clone().map(|val| val.as_int()))
.flatten()
.and_then(|comp| comp.get("RepairCost").map(|val| val.as_int()))
.flatten(),
None => None,
}
Expand All @@ -179,36 +174,26 @@ impl ItemMeta {
Some(tag) => tag
.1
.as_compound()
.map(|comp| {
.and_then(|comp| {
comp.get("ench").map(|ench| {
ench.as_list().map(|enchs| {
enchs
.iter()
.map(|ench| {
ench.as_compound()
.map(|ench| {
ench.get("lvl")
.map(|lvl| lvl.as_short())
.flatten()
.zip(
ench.get("id")
.map(|id| id.as_short())
.flatten(),
)
.map(|(level, id)| {
Enchantment::new(id as u16, level)
})
.flatten()
})
.flatten()
.filter_map(|ench| {
ench.as_compound().and_then(|ench| {
ench.get("lvl")
.and_then(|lvl| lvl.as_short())
.zip(ench.get("id").and_then(|id| id.as_short()))
.and_then(|(level, id)| {
Enchantment::new(id as u16, level)
})
})
})
.flatten()
.collect::<Vec<_>>()
})
})
})
.flatten()
.flatten()
.unwrap_or(vec![]),
None => vec![],
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/protocol/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3421,8 +3421,8 @@ pub fn send_position(

pub fn send_look(conn: &mut Conn, yaw: f32, pitch: f32, on_ground: bool) -> Result<(), Error> {
conn.write_packet(crate::protocol::packet::play::serverbound::PlayerLook {
yaw,
pitch,
yaw: -yaw * (180.0 / PI),
pitch: (-pitch * (180.0 / PI) + 180.0).min(90.0), // used to make sure, that we don't send impossible pitch values
on_ground,
})
}
Expand Down
73 changes: 37 additions & 36 deletions src/inventory/material/versions/mapping1_10_2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::mapping1_12_2::*;
use crate::inventory::Material;

pub fn to_id(mat: Material) -> u16 {
Expand Down Expand Up @@ -430,53 +431,53 @@ pub fn to_id(mat: Material) -> u16 {
}
}

pub fn to_material(material_id: u16) -> Material {
pub fn to_material(material_id: u16, damage: isize) -> Material {
match material_id {
0 => Material::Air,
1 => Material::Stone,
1 => map_stone(damage),
2 => Material::Grass,
3 => Material::Dirt,
3 => map_dirt(damage),
4 => Material::Cobblestone,
5 => Material::Wood,
6 => Material::Sapling,
5 => map_planks(damage),
6 => map_sapling(damage),
7 => Material::Bedrock,
8 => Material::Water,
9 => Material::StationaryWater,
10 => Material::Lava,
11 => Material::StationaryLava,
12 => Material::Sand,
12 => map_sand(damage),
13 => Material::Gravel,
14 => Material::GoldOre,
15 => Material::IronOre,
16 => Material::CoalOre,
17 => Material::Log,
18 => Material::Leaves,
19 => Material::Sponge,
17 => map_log(damage),
18 => map_leaves(damage),
19 => map_sponge(damage),
20 => Material::Glass,
21 => Material::LapisOre,
22 => Material::LapisBlock,
23 => Material::Dispenser,
24 => Material::Sandstone,
24 => map_sandstone(damage),
25 => Material::NoteBlock,
26 => Material::BedBlock,
27 => Material::PoweredRail,
28 => Material::DetectorRail,
29 => Material::PistonStickyBase,
30 => Material::Web,
31 => Material::LongGrass,
31 => map_tall_grass(damage),
32 => Material::DeadBush,
33 => Material::PistonBase,
34 => Material::PistonExtension,
35 => Material::Wool,
35 => map_wool(damage),
36 => Material::PistonMovingPiece,
37 => Material::YellowFlower,
38 => Material::RedRose,
38 => map_flower(damage),
39 => Material::BrownMushroom,
40 => Material::RedMushroom,
41 => Material::GoldBlock,
42 => Material::IronBlock,
43 => Material::DoubleStep,
44 => Material::Step,
44 => map_stone_slab(damage),
45 => Material::Brick,
46 => Material::Tnt,
47 => Material::Bookshelf,
Expand Down Expand Up @@ -527,10 +528,10 @@ pub fn to_material(material_id: u16) -> Material {
92 => Material::CakeBlock,
93 => Material::DiodeBlockOff,
94 => Material::DiodeBlockOn,
95 => Material::StainedGlass,
95 => map_glass(damage),
96 => Material::TrapDoor,
97 => Material::MonsterEggs,
98 => Material::SmoothBrick,
97 => map_infested_stone(damage),
98 => map_stone_bricks(damage),
99 => Material::HugeMushroom1,
100 => Material::HugeMushroom2,
101 => Material::IronFence,
Expand Down Expand Up @@ -558,7 +559,7 @@ pub fn to_material(material_id: u16) -> Material {
123 => Material::RedstoneLampOff,
124 => Material::RedstoneLampOn,
125 => Material::WoodDoubleStep,
126 => Material::WoodStep,
126 => map_wood_slab(damage),
127 => Material::Cocoa,
128 => Material::SandstoneStairs,
129 => Material::EmeraldOre,
Expand All @@ -571,13 +572,13 @@ pub fn to_material(material_id: u16) -> Material {
136 => Material::JungleWoodStairs,
137 => Material::Command,
138 => Material::Beacon,
139 => Material::CobbleWall,
139 => map_cobble_wall(damage),
140 => Material::FlowerPot,
141 => Material::Carrot,
142 => Material::Potato,
143 => Material::WoodButton,
144 => Material::Skull,
145 => Material::Anvil,
144 => map_skull(damage),
145 => map_anvil(damage),
146 => Material::TrappedChest,
147 => Material::GoldPlate,
148 => Material::IronPlate,
Expand All @@ -587,31 +588,31 @@ pub fn to_material(material_id: u16) -> Material {
152 => Material::RedstoneBlock,
153 => Material::QuartzOre,
154 => Material::Hopper,
155 => Material::QuartzBlock,
155 => map_quartz(damage),
156 => Material::QuartzStairs,
157 => Material::ActivatorRail,
158 => Material::Dropper,
159 => Material::StainedClay,
160 => Material::StainedGlassPane,
161 => Material::Leaves2,
162 => Material::Log2,
159 => map_terracotta(damage),
160 => map_glass_pane(damage),
161 => map_leaves2(damage),
162 => map_log2(damage),
163 => Material::AcaciaStairs,
164 => Material::DarkOakStairs,
165 => Material::SlimeBlock,
166 => Material::Barrier,
167 => Material::IronTrapdoor,
168 => Material::Prismarine,
168 => map_prismarine(damage),
169 => Material::SeaLantern,
170 => Material::HayBlock,
171 => Material::Carpet,
171 => map_carpet(damage),
172 => Material::HardClay,
173 => Material::CoalBlock,
174 => Material::PackedIce,
175 => Material::DoublePlant,
175 => map_double_plant(damage),
176 => Material::StandingBanner,
177 => Material::WallBanner,
178 => Material::DaylightDetectorInverted,
179 => Material::RedSandstone,
179 => map_red_sandstone(damage),
180 => Material::RedSandstoneStairs,
181 => Material::DoubleStoneSlab2,
182 => Material::StoneSlab2,
Expand Down Expand Up @@ -717,7 +718,7 @@ pub fn to_material(material_id: u16) -> Material {
319 => Material::Pork,
320 => Material::GrilledPork,
321 => Material::Painting,
322 => Material::GoldenApple,
322 => map_gapple(damage),
323 => Material::Sign,
324 => Material::WoodDoor,
325 => Material::Bucket,
Expand All @@ -744,13 +745,13 @@ pub fn to_material(material_id: u16) -> Material {
346 => Material::FishingRod,
347 => Material::Watch,
348 => Material::GlowstoneDust,
349 => Material::RawFish,
350 => Material::CookedFish,
351 => Material::InkSack,
349 => map_fish(damage),
350 => map_cooked_fish(damage),
351 => map_dye(damage),
352 => Material::Bone,
353 => Material::Sugar,
354 => Material::Cake,
355 => Material::Bed,
355 => map_bed(damage),
356 => Material::Diode,
357 => Material::Cookie,
358 => Material::Map,
Expand Down Expand Up @@ -820,7 +821,7 @@ pub fn to_material(material_id: u16) -> Material {
422 => Material::CommandMinecart,
423 => Material::Mutton,
424 => Material::CookedMutton,
425 => Material::Banner,
425 => map_banner(damage),
426 => Material::EndCrystal,
427 => Material::SpruceDoorItem,
428 => Material::BirchDoorItem,
Expand Down
Loading
Loading