-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
TileEntityLoader.java
54 lines (47 loc) · 1.92 KB
/
TileEntityLoader.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
package com.ewyboy.bibliotheca.common.loaders;
import com.ewyboy.bibliotheca.common.utility.Logger;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fml.common.registry.GameRegistry;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Locale;
/**
* Created by EwyBoy
*/
public class TileEntityLoader {
public static final HashMap<String, TileEntity> TILE_ENTITIES = new HashMap<>();
/**
* Call this from your preInit() in your mod after you have registered blocks
* @param tileEntityRegister a class where your initialize your block fields
* Example: public static final TileEntityTest = new TileEntityTest();
* This @TileEntityLoader detects all the fields and register them for you
*/
public static void init(Class tileEntityRegister) {
registerTileEntity(tileEntityRegister);
}
/**
* Grabs the block fields from the blockRegister class you provided
*/
private static void registerTileEntity(Class tileEntityRegister) {
try {
for (Field field : tileEntityRegister.getDeclaredFields()) {
Object obj = field.get(null);
if (obj instanceof TileEntity) {
TileEntity tileEntity = (TileEntity) obj;
String name = field.getName().toLowerCase(Locale.ENGLISH);
registerTileEntity(tileEntity, name);
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* Registers the tile entities to the game
*/
private static void registerTileEntity(TileEntity tileEntity, String name) {
GameRegistry.registerTileEntity(tileEntity.getClass(), name);
TILE_ENTITIES.put(tileEntity.toString(), tileEntity);
Logger.info("[TILE ENTITY]: " + tileEntity.getClass().getSimpleName() + " has been registered by Bibliotheca");
}
}