3838public class BlockIdConfig implements BlockIdMapper {
3939
4040 private ConfigurationLoader <? extends ConfigurationNode > autopoulationConfigLoader ;
41- private Map <BlockIDMeta , BlockState > mappings ;
41+ private Map <BlockNumeralIDMeta , BlockState > numeralMappings ;
42+ private Map <BlockIDMeta , BlockState > idMappings ;
4243
4344 public BlockIdConfig (ConfigurationNode node ) {
4445 this (node , null );
@@ -47,29 +48,39 @@ public BlockIdConfig(ConfigurationNode node) {
4748 public BlockIdConfig (ConfigurationNode node , ConfigurationLoader <? extends ConfigurationNode > autopoulationConfigLoader ) {
4849 this .autopoulationConfigLoader = autopoulationConfigLoader ;
4950
50- mappings = new HashMap <>();
51+ numeralMappings = new HashMap <>();
52+ idMappings = new HashMap <>();
5153
5254 for (Entry <Object , ? extends ConfigurationNode > e : node .getChildrenMap ().entrySet ()){
5355 String key = e .getKey ().toString ();
5456 String value = e .getValue ().getString ();
5557
5658 try {
57- int splitIndex = key .indexOf (':' );
58- int blockId , blockMeta ;
59- if (splitIndex > 0 && splitIndex < key .length () - 1 ) {
60- blockId = Integer .parseInt (key .substring (0 , splitIndex ));
61- blockMeta = Integer .parseInt (key .substring (splitIndex + 1 ));
62- } else {
63- blockId = Integer .parseInt (key );
64- blockMeta = 0 ;
65- }
59+ int splitIndex = key .lastIndexOf (':' );
6660
67- BlockIDMeta idmeta = new BlockIDMeta (blockId , blockMeta );
68- BlockState state = BlockState .fromString (value );
61+ if (splitIndex <= 0 || splitIndex >= key .length () - 1 ) {
62+ Logger .global .logWarning ("Loading BlockIdConfig: Failed to parse blockid:meta from key '" + key + "'" );
63+ continue ;
64+ }
6965
70- if (blockId == 0 ) state = BlockState .AIR ; //use the static field to increase render speed (== comparison)
66+ String blockId = key .substring (0 , splitIndex );
67+ int blockNumeralId ;
68+ try {
69+ blockNumeralId = Integer .parseInt (blockId );
70+ } catch (NumberFormatException ex ) {
71+ blockNumeralId = -1 ;
72+ }
73+ int blockMeta = Integer .parseInt (key .substring (splitIndex + 1 ));
74+ BlockState state = BlockState .fromString (value );
7175
72- mappings .put (idmeta , state );
76+ if (blockNumeralId >= 0 ) {
77+ BlockNumeralIDMeta idmeta = new BlockNumeralIDMeta (blockNumeralId , blockMeta );
78+ if (blockNumeralId == 0 ) state = BlockState .AIR ; //use the static field to increase render speed (== comparison)
79+ numeralMappings .put (idmeta , state );
80+ } else {
81+ BlockIDMeta idmeta = new BlockIDMeta (blockId , blockMeta );
82+ idMappings .put (idmeta , state );
83+ }
7384 } catch (NumberFormatException ex ) {
7485 Logger .global .logWarning ("Loading BlockIdConfig: Failed to parse blockid:meta from key '" + key + "'" );
7586 } catch (IllegalArgumentException ex ) {
@@ -79,22 +90,22 @@ public BlockIdConfig(ConfigurationNode node, ConfigurationLoader<? extends Confi
7990 }
8091
8192 @ Override
82- public BlockState get (int id , int meta ) {
83- if (id == 0 ) return BlockState .AIR ;
93+ public BlockState get (int numeralId , int meta ) {
94+ if (numeralId == 0 ) return BlockState .AIR ;
8495
85- BlockIDMeta idmeta = new BlockIDMeta ( id , meta );
86- BlockState state = mappings .get (idmeta );
96+ BlockNumeralIDMeta numidmeta = new BlockNumeralIDMeta ( numeralId , meta );
97+ BlockState state = numeralMappings .get (numidmeta );
8798
8899 if (state == null ) {
89- state = mappings .getOrDefault (new BlockIDMeta ( id , 0 ), BlockState .MISSING ); //meta-fallback
100+ state = numeralMappings .getOrDefault (new BlockNumeralIDMeta ( numeralId , 0 ), BlockState .MISSING ); //meta-fallback
90101
91102 if (autopoulationConfigLoader != null ) {
92- mappings .put (idmeta , state );
103+ numeralMappings .put (numidmeta , state );
93104
94105 synchronized (autopoulationConfigLoader ) {
95106 try {
96107 ConfigurationNode node = autopoulationConfigLoader .load ();
97- node .getNode (id + ":" + meta ).setValue (state .toString ());
108+ node .getNode (numeralId + ":" + meta ).setValue (state .toString ());
98109 autopoulationConfigLoader .save (node );
99110 } catch (IOException ex ) {
100111 Logger .global .noFloodError ("blockidconf-autopopulate-ioex" , "Failed to auto-populate BlockIdConfig!" , ex );
@@ -105,12 +116,50 @@ public BlockState get(int id, int meta) {
105116
106117 return state ;
107118 }
119+
120+ @ Override
121+ public BlockState get (String id , int numeralId , int meta ) {
122+ if (numeralId == 0 ) return BlockState .AIR ;
123+
124+ BlockIDMeta idmeta = new BlockIDMeta (id , meta );
125+ BlockState state = idMappings .get (idmeta );
126+ if (state == null ) {
127+ BlockNumeralIDMeta numidmeta = new BlockNumeralIDMeta (numeralId , meta );
128+ state = numeralMappings .get (numidmeta );
129+ if (state == null ) {
130+
131+ state = idMappings .get (new BlockIDMeta (id , 0 ));
132+ if (state == null ) {
133+ state = numeralMappings .get (new BlockNumeralIDMeta (numeralId , 0 ));
134+ if (state == null ) state = new BlockState (id );
135+ }
136+
137+ if (autopoulationConfigLoader != null ) {
138+ idMappings .put (idmeta , state );
139+ numeralMappings .put (numidmeta , state );
140+
141+ synchronized (autopoulationConfigLoader ) {
142+ try {
143+ ConfigurationNode node = autopoulationConfigLoader .load ();
144+ node .getNode (id + ":" + meta ).setValue (state .toString ());
145+ autopoulationConfigLoader .save (node );
146+ } catch (IOException ex ) {
147+ Logger .global .noFloodError ("blockidconf-autopopulate-ioex" , "Failed to auto-populate BlockIdConfig!" , ex );
148+ }
149+ }
150+ }
151+
152+ }
153+ }
154+
155+ return state ;
156+ }
108157
109- class BlockIDMeta {
158+ class BlockNumeralIDMeta {
110159 private final int id ;
111160 private final int meta ;
112161
113- public BlockIDMeta (int id , int meta ) {
162+ public BlockNumeralIDMeta (int id , int meta ) {
114163 this .id = id ;
115164 this .meta = meta ;
116165 }
@@ -125,14 +174,47 @@ public int getMeta() {
125174
126175 @ Override
127176 public int hashCode () {
128- return id * 0xFFFF + meta ;
177+ return id * 16 + meta ;
178+ }
179+
180+ @ Override
181+ public boolean equals (Object obj ) {
182+ if (obj instanceof BlockNumeralIDMeta ) {
183+ BlockNumeralIDMeta other = (BlockNumeralIDMeta ) obj ;
184+ return other .id == id && other .meta == meta ;
185+ }
186+
187+ return false ;
188+ }
189+ }
190+
191+ class BlockIDMeta {
192+ private final String id ;
193+ private final int meta ;
194+
195+ public BlockIDMeta (String id , int meta ) {
196+ this .id = id ;
197+ this .meta = meta ;
198+ }
199+
200+ public String getId () {
201+ return id ;
202+ }
203+
204+ public int getMeta () {
205+ return meta ;
206+ }
207+
208+ @ Override
209+ public int hashCode () {
210+ return id .hashCode () * 16 + meta ;
129211 }
130212
131213 @ Override
132214 public boolean equals (Object obj ) {
133215 if (obj instanceof BlockIDMeta ) {
134216 BlockIDMeta other = (BlockIDMeta ) obj ;
135- return other .id == id && other .meta == meta ;
217+ return other .id . equals ( id ) && other .meta == meta ;
136218 }
137219
138220 return false ;
0 commit comments