-
Notifications
You must be signed in to change notification settings - Fork 497
/
MappingRegistry.java
executable file
·426 lines (350 loc) · 12 KB
/
MappingRegistry.java
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* The BuildCraft API is distributed under the terms of the MIT License.
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
*/
package buildcraft.api.blueprints;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import org.apache.logging.log4j.Level;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.item.Item;
import net.minecraft.nbt.NBTTagByte;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagShort;
import cpw.mods.fml.common.FMLModContainer;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.event.FMLMissingMappingsEvent;
import net.minecraftforge.common.util.Constants;
import buildcraft.api.core.BCLog;
public class MappingRegistry {
public HashMap<Block, Integer> blockToId = new HashMap<Block, Integer>();
public ArrayList<Block> idToBlock = new ArrayList<Block>();
public HashMap<Item, Integer> itemToId = new HashMap<Item, Integer>();
public ArrayList<Item> idToItem = new ArrayList<Item>();
public HashMap<Class<? extends Entity>, Integer> entityToId = new HashMap<Class<? extends Entity>, Integer>();
public ArrayList<Class<? extends Entity>> idToEntity = new ArrayList<Class<? extends Entity>>();
private void registerItem (Item item) {
if (!itemToId.containsKey(item)) {
idToItem.add(item);
itemToId.put(item, idToItem.size() - 1);
}
}
private void registerBlock (Block block) {
if (!blockToId.containsKey(block)) {
idToBlock.add(block);
blockToId.put(block, idToBlock.size() - 1);
}
}
private void registerEntity(Class<? extends Entity> entityClass) {
if (!entityToId.containsKey(entityClass)) {
idToEntity.add(entityClass);
entityToId.put(entityClass, idToEntity.size() - 1);
}
}
public Item getItemForId(int id) throws MappingNotFoundException {
if (id >= idToItem.size()) {
throw new MappingNotFoundException("no item mapping at position " + id);
}
Item result = idToItem.get(id);
if (result == null) {
throw new MappingNotFoundException("no item mapping at position " + id);
} else {
return result;
}
}
public int getIdForItem(Item item) {
if (!itemToId.containsKey(item)) {
registerItem(item);
}
return itemToId.get(item);
}
public int itemIdToRegistry(int id) {
Item item = Item.getItemById(id);
return getIdForItem(item);
}
public int itemIdToWorld(int id) throws MappingNotFoundException {
Item item = getItemForId(id);
return Item.getIdFromItem(item);
}
public Block getBlockForId(int id) throws MappingNotFoundException {
if (id >= idToBlock.size()) {
throw new MappingNotFoundException("no block mapping at position " + id);
}
Block result = idToBlock.get(id);
if (result == null) {
throw new MappingNotFoundException("no block mapping at position " + id);
} else {
return result;
}
}
public int getIdForBlock(Block block) {
if (!blockToId.containsKey(block)) {
registerBlock (block);
}
return blockToId.get(block);
}
public int blockIdToRegistry(int id) {
Block block = Block.getBlockById(id);
return getIdForBlock(block);
}
public int blockIdToWorld(int id) throws MappingNotFoundException {
Block block = getBlockForId(id);
return Block.getIdFromBlock(block);
}
public Class<? extends Entity> getEntityForId(int id) throws MappingNotFoundException {
if (id >= idToEntity.size()) {
throw new MappingNotFoundException("no entity mapping at position " + id);
}
Class<? extends Entity> result = idToEntity.get(id);
if (result == null) {
throw new MappingNotFoundException("no entity mapping at position " + id);
} else {
return result;
}
}
public int getIdForEntity(Class<? extends Entity> entity) {
if (!entityToId.containsKey(entity)) {
registerEntity (entity);
}
return entityToId.get(entity);
}
/**
* Relocates a stack nbt from the world referential to the registry
* referential.
*/
public void stackToRegistry(NBTTagCompound nbt) {
Item item = Item.getItemById(nbt.getShort("id"));
nbt.setShort("id", (short) getIdForItem(item));
}
/**
* Relocates a stack nbt from the registry referential to the world
* referential.
*/
public void stackToWorld(NBTTagCompound nbt) throws MappingNotFoundException {
Item item = getItemForId(nbt.getShort("id"));
nbt.setShort("id", (short) Item.getIdFromItem(item));
}
private boolean isStackLayout(NBTTagCompound nbt) {
return nbt.hasKey("id") &&
nbt.hasKey("Count") &&
nbt.hasKey("Damage") &&
nbt.getTag("id") instanceof NBTTagShort &&
nbt.getTag("Count") instanceof NBTTagByte &&
nbt.getTag("Damage") instanceof NBTTagShort;
}
public void scanAndTranslateStacksToRegistry(NBTTagCompound nbt) {
// First, check if this nbt is itself a stack
if (isStackLayout(nbt)) {
stackToRegistry(nbt);
}
// Then, look at the nbt compound contained in this nbt (even if it's a
// stack) and checks for stacks in it.
for (Object keyO : nbt.func_150296_c()) {
String key = (String) keyO;
if (nbt.getTag(key) instanceof NBTTagCompound) {
scanAndTranslateStacksToRegistry(nbt.getCompoundTag(key));
}
if (nbt.getTag(key) instanceof NBTTagList) {
NBTTagList list = (NBTTagList) nbt.getTag(key);
if (list.func_150303_d() == Constants.NBT.TAG_COMPOUND) {
for (int i = 0; i < list.tagCount(); ++i) {
scanAndTranslateStacksToRegistry(list.getCompoundTagAt(i));
}
}
}
}
}
public void scanAndTranslateStacksToWorld(NBTTagCompound nbt) throws MappingNotFoundException {
// First, check if this nbt is itself a stack
if (isStackLayout(nbt)) {
stackToWorld(nbt);
}
// Then, look at the nbt compound contained in this nbt (even if it's a
// stack) and checks for stacks in it.
for (Object keyO : new HashSet(nbt.func_150296_c())) {
String key = (String) keyO;
if (nbt.getTag(key) instanceof NBTTagCompound) {
try {
scanAndTranslateStacksToWorld(nbt.getCompoundTag(key));
} catch (MappingNotFoundException e) {
nbt.removeTag(key);
}
}
if (nbt.getTag(key) instanceof NBTTagList) {
NBTTagList list = (NBTTagList) nbt.getTag(key);
if (list.func_150303_d() == Constants.NBT.TAG_COMPOUND) {
for (int i = list.tagCount() - 1; i >= 0; --i) {
try {
scanAndTranslateStacksToWorld(list.getCompoundTagAt(i));
} catch (MappingNotFoundException e) {
list.removeTag(i);
}
}
}
}
}
}
public void write (NBTTagCompound nbt) {
NBTTagList blocksMapping = new NBTTagList();
for (Block b : idToBlock) {
NBTTagCompound sub = new NBTTagCompound();
if (b != null) {
String name = Block.blockRegistry.getNameForObject(b);
if (name == null || name.length() == 0) {
BCLog.logger.error("Block " + b.getUnlocalizedName() + " (" + b.getClass().getName() + ") has an empty registry name! This is a bug!");
} else {
sub.setString("name", name);
}
}
blocksMapping.appendTag(sub);
}
nbt.setTag("blocksMapping", blocksMapping);
NBTTagList itemsMapping = new NBTTagList();
for (Item i : idToItem) {
NBTTagCompound sub = new NBTTagCompound();
if (i != null) {
String name = Item.itemRegistry.getNameForObject(i);
if (name == null || name.length() == 0) {
BCLog.logger.error("Item " + i.getUnlocalizedName() + " (" + i.getClass().getName() + ") has an empty registry name! This is a bug!");
} else {
sub.setString("name", name);
}
}
itemsMapping.appendTag(sub);
}
nbt.setTag("itemsMapping", itemsMapping);
NBTTagList entitiesMapping = new NBTTagList();
for (Class<? extends Entity> e : idToEntity) {
NBTTagCompound sub = new NBTTagCompound();
sub.setString("name", e.getCanonicalName());
entitiesMapping.appendTag(sub);
}
nbt.setTag("entitiesMapping", entitiesMapping);
// System.out.println("[W] idToItem size : " + idToItem.size());
// for (Item i : idToItem) {
// System.out.println("- " + (i != null ? i.toString() : "null"));
// }
}
private Object getMissingMappingFromFML(boolean isBlock, String name, int i) {
String modName = name.split(":")[0];
if (Loader.isModLoaded(modName)) {
try {
FMLMissingMappingsEvent.MissingMapping mapping = new FMLMissingMappingsEvent.MissingMapping(
(isBlock ? '\u0001' : '\u0020') + name, i
);
ListMultimap<String, FMLMissingMappingsEvent.MissingMapping> missingMapping
= ArrayListMultimap.create();
missingMapping.put(modName, mapping);
FMLMissingMappingsEvent event = new FMLMissingMappingsEvent(missingMapping);
for (ModContainer container : Loader.instance().getModList()) {
if (container instanceof FMLModContainer) {
event.applyModContainer(container);
((FMLModContainer) container).handleModStateEvent(event);
if (mapping.getAction() != FMLMissingMappingsEvent.Action.DEFAULT) {
break;
}
}
}
if (mapping.getAction() == FMLMissingMappingsEvent.Action.REMAP) {
return mapping.getTarget();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public void read (NBTTagCompound nbt) {
NBTTagList blocksMapping = nbt.getTagList("blocksMapping",
Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < blocksMapping.tagCount(); ++i) {
NBTTagCompound sub = blocksMapping.getCompoundTagAt(i);
if (!sub.hasKey("name")) {
// Keeping the order correct
idToBlock.add(null);
BCLog.logger.log(Level.WARN, "Can't load a block - corrupt blueprint!");
continue;
}
String name = sub.getString("name");
Block b = null;
if (!Block.blockRegistry.containsKey(name) && name.contains(":")) {
b = (Block) getMissingMappingFromFML(true, name, i);
if (b != null) {
BCLog.logger.info("Remapped " + name + " to " + Block.blockRegistry.getNameForObject(b));
}
}
if (b == null && Block.blockRegistry.containsKey(name)) {
b = (Block) Block.blockRegistry.getObject(name);
}
if (b != null) {
registerBlock(b);
} else {
// Keeping the order correct
idToBlock.add(null);
BCLog.logger.log(Level.WARN, "Can't load block " + name);
}
}
NBTTagList itemsMapping = nbt.getTagList("itemsMapping",
Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < itemsMapping.tagCount(); ++i) {
NBTTagCompound sub = itemsMapping.getCompoundTagAt(i);
if (!sub.hasKey("name")) {
// Keeping the order correct
idToItem.add(null);
BCLog.logger.log(Level.WARN, "Can't load an item - corrupt blueprint!");
continue;
}
String name = sub.getString("name");
Item item = null;
if (!Item.itemRegistry.containsKey(name) && name.contains(":")) {
item = (Item) getMissingMappingFromFML(false, name, i);
if (item != null) {
BCLog.logger.info("Remapped " + name + " to " + Item.itemRegistry.getNameForObject(item));
}
}
if (item == null && Item.itemRegistry.containsKey(name)) {
item = (Item) Item.itemRegistry.getObject(name);
}
if (item != null) {
registerItem(item);
} else {
// Keeping the order correct
idToItem.add(null);
BCLog.logger.log(Level.WARN, "Can't load item " + name);
}
}
NBTTagList entitiesMapping = nbt.getTagList("entitiesMapping",
Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < entitiesMapping.tagCount(); ++i) {
NBTTagCompound sub = entitiesMapping.getCompoundTagAt(i);
String name = sub.getString("name");
Class<? extends Entity> e = null;
try {
e = (Class<? extends Entity>) Class.forName(name);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
if (e != null) {
registerEntity(e);
} else {
// Keeping the order correct
idToEntity.add(null);
BCLog.logger.log(Level.WARN, "Can't load entity " + name);
}
}
// System.out.println("[R] idToItem size : " + idToItem.size());
// for (Item i : idToItem) {
// System.out.println("- " + (i != null ? i.toString() : "null"));
// }
}
}