@@ -41,8 +41,9 @@ public class ParallelBlockRegistry {
4141 private final WritableRegistry <Block > blockRegistry ;
4242 private final HashMap <Integer , Integer > stateMap ;
4343
44- // By type of custom block, a queue of each available ID for the register functions to consume
45- private final HashMap <BlockType , Queue <Integer >> availableStates ;
44+ // NOTE: If the resource pack can be automatically generated and served
45+ // this can be converted into map of BlockType -> Queue<Integer> since we can choose values entirely server side
46+ private final HashMap <BlockState , Integer > availableStates ;
4647
4748 private final HashMap <Integer , ParallelBlockData > blockDataMap ;
4849 private final HashMap <Integer , BlockState > placeMap ;
@@ -72,32 +73,20 @@ private ParallelBlockRegistry() throws RuntimeException, NoSuchMethodException {
7273 placeMap = new HashMap <>();
7374
7475 // TODO: Fill available states
75- addFullBlock (Blocks .NOTE_BLOCK , BlockType . FULL_BLOCK );
76- addFullBlock (Blocks .SCULK_SENSOR , BlockType . TRANSPARENT_SLAB );
77- addFullBlock (Blocks .CALIBRATED_SCULK_SENSOR , BlockType . TRANSPARENT_SLAB );
76+ addFullBlock (Blocks .NOTE_BLOCK );
77+ addFullBlock (Blocks .SCULK_SENSOR );
78+ addFullBlock (Blocks .CALIBRATED_SCULK_SENSOR );
7879
7980 }
8081
81- private void addFullBlock (Block block , BlockType type ) {
82-
83- Queue <Integer > queue ;
84-
85- if (!availableStates .containsKey (type )) {
86- // Create new queue if it doesn't exist, and set its initial size for a slight efficiency boost
87- queue = new ArrayDeque <>(block .getStateDefinition ().getPossibleStates ().size ());
88- availableStates .put (type , queue );
89- } else {
90- queue = availableStates .get (type );
91- }
82+ private void addFullBlock (Block block ) {
9283
9384 for (BlockState state : block .getStateDefinition ().getPossibleStates ()) {
9485 if (state .equals (block .defaultBlockState ())) continue ; // This is the one actually used for rendering
9586
96- int stateId = Block . BLOCK_STATE_REGISTRY . getId ( state );
87+ if ( availableStates . containsKey ( state )) throw new IllegalStateException ( "Cannot add state that already exists" );
9788
98- if (queue .contains (stateId )) throw new IllegalStateException ("Cannot add state that already exists" );
99-
100- queue .add (stateId );
89+ availableStates .put (state , Block .BLOCK_STATE_REGISTRY .getId (state ));
10190 }
10291 }
10392
@@ -128,8 +117,7 @@ public void freeze() {
128117 MappedRegistry <@ NotNull WritableRegistry <?>> writable_registry = (MappedRegistry <WritableRegistry <?>>) getPrivateField ("WRITABLE_REGISTRY" , BuiltInRegistries .class , null , WritableRegistry .class );
129118
130119 // Search registry byValue (since byKey doesn't work for whatever reason...)
131- Map <WritableRegistry <?>, Holder .Reference <@ NotNull WritableRegistry <?>>> byValue =
132- getPrivateField ("byValue" , MappedRegistry .class , writable_registry , Map .class );
120+ Map <WritableRegistry <?>, Holder .Reference <@ NotNull WritableRegistry <?>>> byValue = getPrivateField ("byValue" , MappedRegistry .class , writable_registry , Map .class );
133121
134122 for (WritableRegistry i : byValue .keySet ()) {
135123 if (String .valueOf (i .key ().identifier ()).equals (String .valueOf (registryKey .identifier ()))) {
@@ -140,30 +128,21 @@ public void freeze() {
140128 return null ;
141129 }
142130
143- public boolean registerBlock (ResourceKey <@ NotNull Block > key , Block block , BlockType targetType ,
144- BlockState particleState , Component name ) {
131+ public boolean registerBlock (ResourceKey <@ NotNull Block > key , Block block , BlockState targetBlockstate , BlockState particleState , Component name ) {
145132 ItemStack stack = Items .BARRIER .getDefaultInstance ();
146133
147134 stack .applyComponentsAndValidate (
148135 DataComponentPatch .builder ()
149136 .set (TypedDataComponent .createUnchecked (DataComponents .ITEM_MODEL , key .identifier ()))
150137 .set (TypedDataComponent .createUnchecked (DataComponents .ITEM_NAME , name )).build ());
151138
152- return registerBlock (key , block , targetType , List .of (stack ), new BlockParticleOption (ParticleTypes .BLOCK , particleState ),stack );
139+ return registerBlock (key , block , targetBlockstate , List .of (stack ), new BlockParticleOption (ParticleTypes .BLOCK , particleState ),stack );
153140 }
154141
155- public boolean registerBlock (ResourceKey <@ NotNull Block > key , Block block , BlockType targetType , List <ItemStack > drops , ParticleOptions particle , ItemStack placeBlock ) {
142+ public boolean registerBlock (ResourceKey <@ NotNull Block > key , Block block , BlockState targetBlockstate , List <ItemStack > drops , ParticleOptions particle , ItemStack placeBlock ) {
156143 if (frozen ) return false ;
157144
158- Queue <Integer > blockQueue = availableStates .get (targetType );
159- if (blockQueue == null ) {
160- throw new IllegalStateException ("No BlockState of requested type is registered" );
161- }
162-
163- Integer nextState = blockQueue .poll ();
164- if (nextState == null ) {
165- throw new IllegalStateException ("No remaining BlockState of requested type" );
166- }
145+ if (!availableStates .containsKey (targetBlockstate )) throw new IllegalStateException ("Block state is already used or does not exist" );
167146
168147 try {
169148 Material newMat = ReflectionHelper .makeEnum (Material .class ,
@@ -176,15 +155,17 @@ public boolean registerBlock(ResourceKey<@NotNull Block> key, Block block, Block
176155 } catch (Throwable t ) {
177156 Logger .getGlobal ().log (Level .SEVERE , "Unable to create new Material!" );
178157 t .printStackTrace ();
179- throw new IllegalStateException ();
158+
180159 }
181160
182161 Holder .Reference <Block > registeredBlock = blockRegistry .register (key , block , RegistrationInfo .BUILT_IN );
183162
184163 // TODO: This technically doesn't work since several blockstates are mapped to a single one, but it's easier for now
185164 for (BlockState blockState : registeredBlock .value ().getStateDefinition ().getPossibleStates ()) {
186165
187- stateMap .put (Block .BLOCK_STATE_REGISTRY .size (), nextState ); // Map the new block state to an unused state
166+ Integer stateId = availableStates .remove (targetBlockstate );
167+
168+ stateMap .put (Block .BLOCK_STATE_REGISTRY .size (), stateId ); // Map the new block state to an unused state
188169 blockDataMap .put (Block .BLOCK_STATE_REGISTRY .size (), new ParallelBlockData (drops , particle ));
189170 placeMap .put (ItemStack .hashItemAndComponents (placeBlock ), blockState );
190171
@@ -214,13 +195,4 @@ public ParallelBlockData getBlockData(int state) {
214195 public BlockState getBlockState (@ NotNull ItemStack item ) {
215196 return placeMap .get (ItemStack .hashItemAndComponents (item ));
216197 }
217-
218- public enum BlockType {
219- FULL_BLOCK ,
220- TRANSPARENT_BLOCK ,
221- TRANSPARENT_BLOCK_WATERLOGGED ,
222- BIOME_TRANSPARENT_BLOCK ,
223- TRANSPARENT_SLAB ,
224-
225- }
226198}
0 commit comments