Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
feather/feather/blocks/src/lib.rs at high-level-entity-methods · feather-rs/feather · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
feather-rs
/
feather
Public
Notifications
You must be signed in to change notification settings
Fork
142
Star
2.7k
Code
Issues
66
Pull requests
38
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
high-level-entity-methods
Breadcrumbs
feather
/
feather
/
blocks
/
src
/
lib.rs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
222 lines (180 loc) · 6.58 KB
high-level-entity-methods
Breadcrumbs
feather
/
feather
/
blocks
/
src
/
lib.rs
Copy path
Top
File metadata and controls
Code
Blame
222 lines (180 loc) · 6.58 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use
num_traits
::
FromPrimitive
;
use
std
::
convert
::
TryFrom
;
use
thiserror
::
Error
;
pub
use
feather_generated
::
{
BlockKind
,
SimplifiedBlockKind
}
;
pub
mod
categories
;
mod
directions
;
#
[
allow
(
warnings
)
]
#
[
allow
(
clippy
::
all
)
]
mod
generated
;
mod
wall_blocks
;
static
BLOCK_TABLE
:
Lazy
<
BlockTable
>
=
Lazy
::
new
(
||
{
let
bytes =
include_bytes
!
(
"generated/table.dat"
)
;
bincode
::
deserialize
(
bytes
)
.
expect
(
"failed to deserialize generated block table (bincode)"
)
}
)
;
static
VANILLA_ID_TABLE
:
Lazy
<
Vec
<
Vec
<
u16
>
>
>
=
Lazy
::
new
(
||
{
let
bytes =
include_bytes
!
(
"generated/vanilla_ids.dat"
)
;
bincode
::
deserialize
(
bytes
)
.
expect
(
"failed to deserialize generated vanilla ID table (bincode)"
)
}
)
;
const
HIGHEST_ID
:
u16
=
8596
;
static
FROM_VANILLA_ID_TABLE
:
Lazy
<
Vec
<
BlockId
>
>
=
Lazy
::
new
(
||
{
let
mut
res =
vec
!
[
BlockId
::
default
(
)
;
u16
::
max_value
(
)
as
usize
]
;
for
(
kind_id
,
ids
)
in
VANILLA_ID_TABLE
.
iter
(
)
.
enumerate
(
)
{
let
kind =
BlockKind
::
from_u16
(
kind_id
as
u16
)
.
expect
(
"invalid block kind ID"
)
;
for
(
state
,
id
)
in
ids
.
iter
(
)
.
enumerate
(
)
{
res
[
*
id
as
usize
]
=
BlockId
{
state
:
state
as
u16
,
kind
,
}
;
}
}
debug_assert
!
(
(
1
..=
HIGHEST_ID
)
.
all
(
|id| res
[
id
as
usize
]
!=
BlockId
::
default
(
)
)
)
;
// Verify distinction
if
cfg
!
(
debug_assertions
)
{
let
mut
known_blocks =
HashSet
::
with_capacity
(
HIGHEST_ID
as
usize
)
;
assert
!
(
(
1
..=
HIGHEST_ID
)
.
all
(
|id| known_blocks
.
insert
(
res
[
id
as
usize
]
)
)
)
;
}
res
}
)
;
/// Can be called at startup to pre-initialize the global block table.
pub
fn
init
(
)
{
Lazy
::
force
(
&
FROM_VANILLA_ID_TABLE
)
;
Lazy
::
force
(
&
BLOCK_TABLE
)
;
}
use
once_cell
::
sync
::
Lazy
;
pub
use
crate
::
generated
::
table
::
*
;
use
std
::
collections
::
HashSet
;
#
[
derive
(
Copy
,
Clone
,
Debug
,
PartialEq
,
Eq
,
Hash
,
PartialOrd
,
Ord
)
]
pub
struct
BlockId
{
kind
:
BlockKind
,
state
:
u16
,
}
impl
Default
for
BlockId
{
fn
default
(
)
->
Self
{
BlockId
{
kind
:
BlockKind
::
Air
,
state
:
0
,
}
}
}
impl
BlockId
{
/// Returns the kind of this block.
pub
fn
kind
(
self
)
->
BlockKind
{
self
.
kind
}
/// Returns the simplified kind of this block.
/// This is an arbitrary manual mapping that aims to condense the different
/// vanilla block kinds which have only minor differences (e.g. different colored beds)
/// and is mainly intended to make `match`ing on the block type easier.
/// This mapping in no way stable right now.
pub
fn
simplified_kind
(
self
)
->
SimplifiedBlockKind
{
self
.
kind
.
simplified_kind
(
)
}
/// Returns the vanilla state ID for this block.
pub
fn
vanilla_id
(
self
)
->
u16
{
VANILLA_ID_TABLE
[
self
.
kind
as
u16
as
usize
]
[
self
.
state
as
usize
]
}
/*
/// Returns the vanilla fluid ID for this block in case it is a fluid.
/// The fluid ID is used in the Tags packet.
pub fn vanilla_fluid_id(self) -> Option<u16> {
if self.is_fluid() {
match (self.kind(), self.water_level().unwrap()) {
// could be swapped?
(BlockKind::Water, 0) => Some(2), // stationary water
(BlockKind::Water, _) => Some(1), // flowing water
// tested those
(BlockKind::Lava, 0) => Some(4), // stationary lava
(BlockKind::Lava, _) => Some(3), // flowing lava
_ => unreachable!(),
}
} else {
None
}
}
*/
/// Returns the block corresponding to the given vanilla ID.
///
/// (Invalid IDs currently return `BlockId::air()`).
pub
fn
from_vanilla_id
(
id
:
u16
)
->
Self
{
FROM_VANILLA_ID_TABLE
[
id
as
usize
]
}
}
impl
From
<
BlockId
>
for
u32
{
fn
from
(
id
:
BlockId
)
->
Self
{
(
(
id
.
kind
as
u32
)
<<
16
)
| id
.
state
as
u32
}
}
#
[
derive
(
Debug
,
Error
)
]
pub
enum
BlockIdFromU32Error
{
#
[
error
(
"invalid block kind ID {0}"
)
]
InvalidKind
(
u16
)
,
#
[
error
(
"invalid block state ID {0} for kind {1:?}"
)
]
InvalidState
(
u16
,
BlockKind
)
,
}
impl
TryFrom
<
u32
>
for
BlockId
{
type
Error
=
BlockIdFromU32Error
;
fn
try_from
(
value
:
u32
)
->
Result
<
Self
,
Self
::
Error
>
{
let
kind_id =
(
value >>
16
)
as
u16
;
let
kind =
BlockKind
::
from_u16
(
kind_id
)
.
ok_or
(
BlockIdFromU32Error
::
InvalidKind
(
kind_id
)
)
?
;
let
state =
(
value |
(
(
1
<<
16
)
-
1
)
)
as
u16
;
// TODO: verify state
Ok
(
BlockId
{
kind
,
state
}
)
}
}
// This is where the magic happens.
pub
(
crate
)
fn
n_dimensional_index
(
state
:
u16
,
offset_coefficient
:
u16
,
stride
:
u16
)
->
u16
{
(
state % offset_coefficient
)
/ stride
}
#
[
cfg
(
test
)
]
mod
tests
{
use
super
::
*
;
#
[
test
]
fn
instrument
(
)
{
let
mut
block =
BlockId
{
kind
:
BlockKind
::
NoteBlock
,
state
:
0
,
}
;
assert
!
(
block
.
instrument
(
)
.
is_some
(
)
)
;
block
.
set_instrument
(
Instrument
::
Basedrum
)
;
assert_eq
!
(
block
.
instrument
(
)
,
Some
(
Instrument
::
Basedrum
)
)
;
}
#
[
test
]
fn
vanilla_ids
(
)
{
let
block =
BlockId
::
rose_bush
(
)
.
with_half_upper_lower
(
HalfUpperLower
::
Lower
)
;
assert_eq
!
(
block
.
vanilla_id
(
)
,
7894
)
;
// will have to be changed whenever we update to a newer MC version
assert_eq
!
(
BlockId
::
from_vanilla_id
(
block
.
vanilla_id
(
)
)
,
block
)
;
let
block =
BlockId
::
structure_block
(
)
.
with_structure_block_mode
(
StructureBlockMode
::
Corner
)
;
assert_eq
!
(
block
.
vanilla_id
(
)
,
15745
)
;
assert_eq
!
(
BlockId
::
from_vanilla_id
(
block
.
vanilla_id
(
)
)
,
block
)
;
let
mut
block =
BlockId
::
redstone_wire
(
)
;
block
.
set_power
(
2
)
;
block
.
set_south_wire
(
SouthWire
::
Side
)
;
block
.
set_west_wire
(
WestWire
::
Side
)
;
block
.
set_east_wire
(
EastWire
::
Side
)
;
block
.
set_north_wire
(
NorthWire
::
Up
)
;
assert_eq
!
(
block
.
power
(
)
,
Some
(
2
)
)
;
assert_eq
!
(
block
.
south_wire
(
)
,
Some
(
SouthWire
::
Side
)
)
;
assert_eq
!
(
block
.
west_wire
(
)
,
Some
(
WestWire
::
Side
)
)
;
assert_eq
!
(
block
.
east_wire
(
)
,
Some
(
EastWire
::
Side
)
)
;
assert_eq
!
(
block
.
north_wire
(
)
,
Some
(
NorthWire
::
Up
)
)
;
assert_eq
!
(
block
.
vanilla_id
(
)
,
2512
)
;
assert_eq
!
(
BlockId
::
from_vanilla_id
(
block
.
vanilla_id
(
)
)
,
block
)
;
}
#
[
test
]
fn
vanilla_ids_roundtrip
(
)
{
for
id
in
0
..
8598
{
assert_eq
!
(
BlockId
::
from_vanilla_id
(
id
)
.
vanilla_id
(
)
,
id
)
;
if
id !=
0
{
assert_ne
!
(
BlockId
::
from_vanilla_id
(
id
)
,
BlockId
::
air
(
)
)
;
}
}
}
#
[
test
]
fn
property_starting_at_1
(
)
{
let
block =
BlockId
::
snow
(
)
.
with_layers
(
1
)
;
assert_eq
!
(
block
.
layers
(
)
,
Some
(
1
)
)
;
assert_eq
!
(
block
.
to_properties_map
(
)
[
"layers"
]
,
"1"
)
;
}
}
You can’t perform that action at this time.