11package parallelmc .parallelworlds .registry ;
22
33import net .minecraft .core .*;
4+ import net .minecraft .core .component .DataComponentPatch ;
5+ import net .minecraft .core .component .DataComponents ;
6+ import net .minecraft .core .component .TypedDataComponent ;
47import net .minecraft .core .registries .BuiltInRegistries ;
58import net .minecraft .core .registries .Registries ;
9+ import net .minecraft .network .chat .Component ;
610import net .minecraft .resources .ResourceKey ;
11+ import net .minecraft .world .item .ItemStack ;
12+ import net .minecraft .world .item .Items ;
13+ import net .minecraft .world .item .component .CustomModelData ;
714import net .minecraft .world .level .block .Block ;
815import net .minecraft .world .level .block .Blocks ;
9- import net .minecraft .world .level .block .NoteBlock ;
1016import net .minecraft .world .level .block .state .BlockState ;
11- import net .minecraft .world .level .block . state . properties . NoteBlockInstrument ;
17+ import net .minecraft .world .level .storage . loot . functions .* ;
1218import org .jetbrains .annotations .NotNull ;
1319import org .jetbrains .annotations .Nullable ;
1420
15- import java .util .ArrayDeque ;
16- import java .util .HashMap ;
17- import java .util .Map ;
18- import java .util .Queue ;
21+ import java .util .*;
1922import java .util .logging .Level ;
2023import java .util .logging .Logger ;
2124
@@ -33,9 +36,11 @@ public class ParallelBlockRegistry {
3336 // this can be converted into map of BlockType -> Queue<Integer> since we can choose values entirely server side
3437 private final HashMap <BlockState , Integer > availableStates ;
3538
39+ private final HashMap <Integer , List <ItemStack >> dropMap ;
40+
3641 private boolean frozen = false ;
3742
38- private ParallelBlockRegistry () throws RuntimeException {
43+ private ParallelBlockRegistry () throws RuntimeException , NoSuchMethodException {
3944 // This is a ridiculous hack to force internal blocks to register first
4045 Block dummy = Blocks .DIRT ;
4146 Logger .getGlobal ().log (Level .INFO , dummy .toString ());
@@ -50,6 +55,7 @@ private ParallelBlockRegistry() throws RuntimeException {
5055
5156 stateMap = new HashMap <>();
5257 availableStates = new HashMap <>();
58+ dropMap = new HashMap <>();
5359
5460 // TODO: Fill available states
5561 for (BlockState state : Blocks .NOTE_BLOCK .getStateDefinition ().getPossibleStates ()) {
@@ -69,7 +75,12 @@ private void addFullBlock(BlockState state) {
6975
7076 public static ParallelBlockRegistry getInstance () {
7177 if (instance == null ) {
72- instance = new ParallelBlockRegistry ();
78+ try {
79+ instance = new ParallelBlockRegistry ();
80+ } catch (NoSuchMethodException e ) {
81+ e .printStackTrace ();
82+ return null ;
83+ }
7384 }
7485
7586 return instance ;
@@ -100,7 +111,18 @@ public void freeze() {
100111 return null ;
101112 }
102113
103- public boolean registerBlock (ResourceKey <@ NotNull Block > key , Block block , BlockState targetBlockstate ) {
114+ public boolean registerBlock (ResourceKey <@ NotNull Block > key , Block block , BlockState targetBlockstate , Component name , float customModelData ) {
115+ ItemStack stack = Items .PAPER .getDefaultInstance ();
116+
117+ stack .applyComponentsAndValidate (
118+ DataComponentPatch .builder ()
119+ .set (TypedDataComponent .createUnchecked (DataComponents .CUSTOM_MODEL_DATA , new CustomModelData (List .of (customModelData ), List .of (), List .of (), List .of ())))
120+ .set (TypedDataComponent .createUnchecked (DataComponents .ITEM_NAME , name )).build ());
121+
122+ return registerBlock (key , block , targetBlockstate , List .of (stack ));
123+ }
124+
125+ public boolean registerBlock (ResourceKey <@ NotNull Block > key , Block block , BlockState targetBlockstate , List <ItemStack > item ) {
104126 if (frozen ) return false ;
105127
106128 if (!availableStates .containsKey (targetBlockstate )) throw new IllegalStateException ("Block state is already used or does not exist" );
@@ -113,6 +135,7 @@ public boolean registerBlock(ResourceKey<@NotNull Block> key, Block block, Block
113135 Integer stateId = availableStates .remove (targetBlockstate );
114136
115137 stateMap .put (Block .BLOCK_STATE_REGISTRY .size (), stateId ); // Map the new block state to an unused state
138+ dropMap .put (Block .BLOCK_STATE_REGISTRY .size (), item );
116139
117140 Block .BLOCK_STATE_REGISTRY .add (blockState );
118141
@@ -130,4 +153,9 @@ public static int getFirstCustomId() {
130153 public Integer getMappedState (int state ) {
131154 return stateMap .get (state );
132155 }
156+
157+ @ Nullable
158+ public List <ItemStack > getDrops (int state ) {
159+ return dropMap .get (state );
160+ }
133161}
0 commit comments