aboutsummaryrefslogtreecommitdiff
path: root/azalea-world
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2023-07-20 05:14:30 -0500
committermat <git@matdoes.dev>2023-07-20 05:14:30 -0500
commit5062a8c8cd828e0719f93bf6931949971fed84df (patch)
treeace2e2a300a45ca097e46bef9d0b211ecb06e531 /azalea-world
parenteb596d921bf9e1e0294e2814ee90f8258935333c (diff)
downloadazalea-drasl-5062a8c8cd828e0719f93bf6931949971fed84df.tar.xz
make PalettedContainer::new less weird
Diffstat (limited to 'azalea-world')
-rwxr-xr-xazalea-world/src/chunk_storage.rs10
-rwxr-xr-xazalea-world/src/palette.rs35
2 files changed, 21 insertions, 24 deletions
diff --git a/azalea-world/src/chunk_storage.rs b/azalea-world/src/chunk_storage.rs
index 61669cca..3c8d3555 100755
--- a/azalea-world/src/chunk_storage.rs
+++ b/azalea-world/src/chunk_storage.rs
@@ -1,5 +1,5 @@
use crate::palette::PalettedContainer;
-use crate::palette::PalettedContainerType;
+use crate::palette::PalettedContainerKind;
use azalea_block::BlockState;
use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
use azalea_core::{BlockPos, ChunkBlockPos, ChunkPos, ChunkSectionBlockPos};
@@ -57,8 +57,8 @@ impl Default for Section {
fn default() -> Self {
Section {
block_count: 0,
- states: PalettedContainer::new(&PalettedContainerType::BlockStates).unwrap(),
- biomes: PalettedContainer::new(&PalettedContainerType::Biomes).unwrap(),
+ states: PalettedContainer::new(PalettedContainerKind::BlockStates),
+ biomes: PalettedContainer::new(PalettedContainerKind::Biomes),
}
}
}
@@ -306,7 +306,7 @@ impl McBufReadable for Section {
// "A section has more blocks than what should be possible. This is a bug!"
// );
- let states = PalettedContainer::read_with_type(buf, &PalettedContainerType::BlockStates)?;
+ let states = PalettedContainer::read_with_type(buf, &PalettedContainerKind::BlockStates)?;
for i in 0..states.storage.size() {
if !BlockState::is_valid_state(states.storage.get(i) as u32) {
@@ -318,7 +318,7 @@ impl McBufReadable for Section {
}
}
- let biomes = PalettedContainer::read_with_type(buf, &PalettedContainerType::Biomes)?;
+ let biomes = PalettedContainer::read_with_type(buf, &PalettedContainerKind::Biomes)?;
Ok(Section {
block_count,
states,
diff --git a/azalea-world/src/palette.rs b/azalea-world/src/palette.rs
index d10357ad..38cf5853 100755
--- a/azalea-world/src/palette.rs
+++ b/azalea-world/src/palette.rs
@@ -4,7 +4,7 @@ use std::io::{Cursor, Write};
use crate::BitStorage;
#[derive(Clone, Debug, Copy)]
-pub enum PalettedContainerType {
+pub enum PalettedContainerKind {
Biomes,
BlockStates,
}
@@ -20,26 +20,26 @@ pub struct PalettedContainer {
pub palette: Palette,
/// Compacted list of indices pointing to entry IDs in the Palette.
pub storage: BitStorage,
- pub container_type: PalettedContainerType,
+ pub container_type: PalettedContainerKind,
}
impl PalettedContainer {
- pub fn new(container_type: &'static PalettedContainerType) -> Result<Self, String> {
+ pub fn new(container_type: PalettedContainerKind) -> Self {
let palette = Palette::SingleValue(0);
let size = container_type.size();
let storage = BitStorage::new(0, size, Some(vec![])).unwrap();
- Ok(PalettedContainer {
+ PalettedContainer {
bits_per_entry: 0,
palette,
storage,
- container_type: *container_type,
- })
+ container_type,
+ }
}
pub fn read_with_type(
buf: &mut Cursor<&[u8]>,
- container_type: &'static PalettedContainerType,
+ container_type: &'static PalettedContainerKind,
) -> Result<Self, BufReadError> {
let bits_per_entry = u8::read_from(buf)?;
let palette_type = PaletteKind::from_bits_and_type(bits_per_entry, container_type);
@@ -260,15 +260,15 @@ impl McBufWritable for Palette {
}
impl PaletteKind {
- pub fn from_bits_and_type(bits_per_entry: u8, container_type: &PalettedContainerType) -> Self {
+ pub fn from_bits_and_type(bits_per_entry: u8, container_type: &PalettedContainerKind) -> Self {
match container_type {
- PalettedContainerType::BlockStates => match bits_per_entry {
+ PalettedContainerKind::BlockStates => match bits_per_entry {
0 => PaletteKind::SingleValue,
1..=4 => PaletteKind::Linear,
5..=8 => PaletteKind::Hashmap,
_ => PaletteKind::Global,
},
- PalettedContainerType::Biomes => match bits_per_entry {
+ PalettedContainerKind::Biomes => match bits_per_entry {
0 => PaletteKind::SingleValue,
1..=3 => PaletteKind::Linear,
_ => PaletteKind::Global,
@@ -306,11 +306,11 @@ impl From<&Palette> for PaletteKind {
}
}
-impl PalettedContainerType {
+impl PalettedContainerKind {
fn size_bits(&self) -> usize {
match self {
- PalettedContainerType::BlockStates => 4,
- PalettedContainerType::Biomes => 2,
+ PalettedContainerKind::BlockStates => 4,
+ PalettedContainerKind::Biomes => 2,
}
}
@@ -325,8 +325,7 @@ mod tests {
#[test]
fn test_resize_0_bits_to_1() {
- let mut palette_container =
- PalettedContainer::new(&PalettedContainerType::BlockStates).unwrap();
+ let mut palette_container = PalettedContainer::new(PalettedContainerKind::BlockStates);
assert_eq!(palette_container.bits_per_entry, 0);
assert_eq!(palette_container.get_at_index(0), 0);
@@ -344,8 +343,7 @@ mod tests {
#[test]
fn test_resize_0_bits_to_5() {
- let mut palette_container =
- PalettedContainer::new(&PalettedContainerType::BlockStates).unwrap();
+ let mut palette_container = PalettedContainer::new(PalettedContainerKind::BlockStates);
palette_container.set_at_index(0, 0); // 0 bits
assert_eq!(palette_container.bits_per_entry, 0);
@@ -380,8 +378,7 @@ mod tests {
#[test]
fn test_coords_from_index() {
- let palette_container =
- PalettedContainer::new(&PalettedContainerType::BlockStates).unwrap();
+ let palette_container = PalettedContainer::new(PalettedContainerKind::BlockStates);
for x in 0..15 {
for y in 0..15 {