-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGuiHandlerPrefab.java
58 lines (48 loc) · 1.77 KB
/
GuiHandlerPrefab.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
package me.planetguy.lib.prefab;
import java.lang.reflect.Constructor;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.network.NetworkRegistry;
public class GuiHandlerPrefab implements IGuiHandler {
public static void create(Object owner, Class<ContainerPrefab>[] containers, Class<GuiPrefab>[] clientGuis) {
try{
NetworkRegistry.INSTANCE.registerGuiHandler(owner, new GuiHandlerPrefab(containers, clientGuis));
}catch(Exception e){
throw new RuntimeException(e);
}
}
private final Constructor[] commonConstructors;
private final Constructor[] clientConstructors;
private GuiHandlerPrefab(Class[] containers, Class[] guis) throws Exception{
commonConstructors=new Constructor[containers.length];
for(int i=0; i<containers.length; i++){
commonConstructors[i]=containers[i].getConstructor(InventoryPlayer.class, TileEntity.class);
}
clientConstructors=new Constructor[containers.length];
for(int i=0; i<guis.length; i++){
clientConstructors[i]=guis[i].getConstructor(InventoryPlayer.class, TileEntity.class);
}
}
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world,
int x, int y, int z) {
try{
return commonConstructors[ID].newInstance(player.inventory, world.getTileEntity(x, y, z));
}catch(Exception e){
}
return null;
}
@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world,
int x, int y, int z) {
try{
return clientConstructors[ID].newInstance(player.inventory, world.getTileEntity(x, y, z));
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}