mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Fixing #1169 - use strum macros for BlockKind conversion
This commit is contained in:
@ -218,9 +218,8 @@ lazy_static! {
|
|||||||
buff_pack
|
buff_pack
|
||||||
};
|
};
|
||||||
|
|
||||||
static ref BLOCK_KINDS: Vec<String> = terrain::block::BLOCK_KINDS
|
static ref BLOCK_KINDS: Vec<String> = terrain::block::BlockKind::iter()
|
||||||
.keys()
|
.map(|bk| bk.to_string())
|
||||||
.cloned()
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
static ref SPRITE_KINDS: Vec<String> = terrain::sprite::SPRITE_KINDS
|
static ref SPRITE_KINDS: Vec<String> = terrain::sprite::SPRITE_KINDS
|
||||||
|
@ -3,13 +3,11 @@ use crate::{
|
|||||||
comp::{fluid_dynamics::LiquidKind, tool::ToolKind},
|
comp::{fluid_dynamics::LiquidKind, tool::ToolKind},
|
||||||
make_case_elim,
|
make_case_elim,
|
||||||
};
|
};
|
||||||
use enum_iterator::IntoEnumIterator;
|
|
||||||
use hashbrown::HashMap;
|
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use num_derive::FromPrimitive;
|
use num_derive::FromPrimitive;
|
||||||
use num_traits::FromPrimitive;
|
use num_traits::FromPrimitive;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{convert::TryFrom, fmt, ops::Deref};
|
use std::ops::Deref;
|
||||||
|
use strum_macros::{EnumIter, EnumString, ToString};
|
||||||
use vek::*;
|
use vek::*;
|
||||||
|
|
||||||
make_case_elim!(
|
make_case_elim!(
|
||||||
@ -23,8 +21,10 @@ make_case_elim!(
|
|||||||
PartialEq,
|
PartialEq,
|
||||||
Serialize,
|
Serialize,
|
||||||
Deserialize,
|
Deserialize,
|
||||||
IntoEnumIterator,
|
|
||||||
FromPrimitive,
|
FromPrimitive,
|
||||||
|
EnumString,
|
||||||
|
EnumIter,
|
||||||
|
ToString,
|
||||||
)]
|
)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum BlockKind {
|
pub enum BlockKind {
|
||||||
@ -88,22 +88,6 @@ impl BlockKind {
|
|||||||
pub const fn has_color(&self) -> bool { self.is_filled() }
|
pub const fn has_color(&self) -> bool { self.is_filled() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for BlockKind {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) }
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
pub static ref BLOCK_KINDS: HashMap<String, BlockKind> = BlockKind::into_enum_iter()
|
|
||||||
.map(|bk| (bk.to_string(), bk))
|
|
||||||
.collect();
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> TryFrom<&'a str> for BlockKind {
|
|
||||||
type Error = ();
|
|
||||||
|
|
||||||
fn try_from(s: &'a str) -> Result<Self, Self::Error> { BLOCK_KINDS.get(s).copied().ok_or(()) }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
|
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
kind: BlockKind,
|
kind: BlockKind,
|
||||||
|
@ -43,6 +43,7 @@ use hashbrown::{HashMap, HashSet};
|
|||||||
use humantime::Duration as HumanDuration;
|
use humantime::Duration as HumanDuration;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use specs::{storage::StorageEntry, Builder, Entity as EcsEntity, Join, WorldExt};
|
use specs::{storage::StorageEntry, Builder, Entity as EcsEntity, Join, WorldExt};
|
||||||
|
use std::str::FromStr;
|
||||||
use vek::*;
|
use vek::*;
|
||||||
use wiring::{Circuit, Wire, WiringAction, WiringActionEffect, WiringElement};
|
use wiring::{Circuit, Wire, WiringAction, WiringActionEffect, WiringElement};
|
||||||
use world::util::Sampler;
|
use world::util::Sampler;
|
||||||
@ -515,7 +516,7 @@ fn handle_make_block(
|
|||||||
action: &ChatCommand,
|
action: &ChatCommand,
|
||||||
) -> CmdResult<()> {
|
) -> CmdResult<()> {
|
||||||
if let Some(block_name) = scan_fmt_some!(&args, &action.arg_fmt(), String) {
|
if let Some(block_name) = scan_fmt_some!(&args, &action.arg_fmt(), String) {
|
||||||
if let Ok(bk) = BlockKind::try_from(block_name.as_str()) {
|
if let Ok(bk) = BlockKind::from_str(block_name.as_str()) {
|
||||||
let pos = position(server, target, "target")?;
|
let pos = position(server, target, "target")?;
|
||||||
server.state.set_block(
|
server.state.set_block(
|
||||||
pos.0.map(|e| e.floor() as i32),
|
pos.0.map(|e| e.floor() as i32),
|
||||||
|
Reference in New Issue
Block a user