summaryrefslogtreecommitdiff
path: root/src/main/java/darkknight/jewelrycraft/thirdparty/ThirdPartyManager.java
blob: bb819d5460c6ebede7d3586ce61665918d56d701 (plain)
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
package darkknight.jewelrycraft.thirdparty;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.Level;

import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.network.IGuiHandler;
import darkknight.jewelrycraft.JewelrycraftMod;
import darkknight.jewelrycraft.config.ConfigHandler;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.world.World;

/**
 * @author MineMarteen from Pneumaticraft
 */
public class ThirdPartyManager implements IGuiHandler {

	private static ThirdPartyManager	INSTANCE	= new ThirdPartyManager();
	private final List<IThirdParty>		thirdPartyMods	= new ArrayList<>();

	public static ThirdPartyManager instance() {
		return INSTANCE;
	}

	public void index() {
		Map<String, Class<? extends IThirdParty>> thirdPartyClasses = new HashMap<>();

		thirdPartyClasses.put(ModIds.EE3, EE3.class);
		thirdPartyClasses.put(ModIds.NEI, NEI.class);

		List<String> enabledThirdParty = new ArrayList<>();

		ConfigHandler.config.addCustomCategoryComment(
				"third_party_enabling",
				"With these options you can disable third party content by mod."
						+ " Useful if something in the mod changes and causes crashes.");

		for (String modid : thirdPartyClasses.keySet()) {
			if (ConfigHandler.config
					.get("Third_Party_Enabling", modid,
							true)
					.getBoolean()) {
				enabledThirdParty.add(modid);
			}
		}

		ConfigHandler.config.save();

		for (Map.Entry<String, Class<? extends IThirdParty>> entry : thirdPartyClasses
				.entrySet()) {
			if (enabledThirdParty.contains(entry.getKey())
					&& Loader.isModLoaded(
							entry.getKey())) {
				try {
					thirdPartyMods.add(entry.getValue()
							.newInstance());
				} catch (Exception e) {
					JewelrycraftMod.logger.log(
							Level.ERROR,
							"Failed to instantiate third party handler!");
					e.printStackTrace();
				}
			}
		}
	}

	public void onItemRegistry(Item item) {
		for (IThirdParty thirdParty : thirdPartyMods) {
			if (thirdParty instanceof IRegistryListener)
				((IRegistryListener) thirdParty)
						.onItemRegistry(item);
		}
	}

	public void onBlockRegistry(Block block) {
		for (IThirdParty thirdParty : thirdPartyMods) {
			if (thirdParty instanceof IRegistryListener)
				((IRegistryListener) thirdParty)
						.onBlockRegistry(block);
		}
	}

	public void preInit() {
		for (IThirdParty thirdParty : thirdPartyMods) {
			try {
				thirdParty.preInit();
			} catch (Throwable e) {
				JewelrycraftMod.logger.log(Level.ERROR,
						"Jewelrycraft wasn't able to load third party content from the third party class "
								+ thirdParty.getClass()
								+ " in the PreInit phase!");
				e.printStackTrace();
			}
		}
	}

	public void init() {
		for (IThirdParty thirdParty : thirdPartyMods) {
			try {
				thirdParty.init();
			} catch (Throwable e) {
				JewelrycraftMod.logger.log(Level.ERROR,
						"Jewelrycraft wasn't able to load third party content from the third party class "
								+ thirdParty.getClass()
								+ " in the Init phase!");
				e.printStackTrace();
			}
		}
	}

	public void postInit() {
		for (IThirdParty thirdParty : thirdPartyMods) {
			try {
				thirdParty.postInit();
			} catch (Throwable e) {
				JewelrycraftMod.logger.log(Level.ERROR,
						"Jewelrycraft wasn't able to load third party content from the third party class "
								+ thirdParty.getClass()
								+ " in the PostInit phase!");
				e.printStackTrace();
			}
		}
	}

	public void clientSide() {
		for (IThirdParty thirdParty : thirdPartyMods) {
			try {
				thirdParty.clientSide();
			} catch (Throwable e) {
				JewelrycraftMod.logger.log(Level.ERROR,
						"Jewelrycraft wasn't able to load third party content from the third party class "
								+ thirdParty.getClass()
								+ " client side!");
				e.printStackTrace();
			}
		}
	}

	public void clientInit() {
		for (IThirdParty thirdParty : thirdPartyMods) {
			try {
				thirdParty.clientInit();
			} catch (Throwable e) {
				JewelrycraftMod.logger.log(Level.ERROR,
						"Jewelrycraft wasn't able to load third party content from the third party class "
								+ thirdParty.getClass()
								+ " client side on the init!");
				e.printStackTrace();
			}
		}
	}

	@Override
	public Object getServerGuiElement(int ID, EntityPlayer player,
			World world, int x, int y, int z) {
		for (IThirdParty thirdParty : thirdPartyMods) {
			if (thirdParty instanceof IGuiHandler) {
				Object obj = ((IGuiHandler) thirdParty)
						.getServerGuiElement(ID,
								player,
								world, x,
								y, z);
				if (obj != null)
					return obj;
			}
		}
		return null;
	}

	@Override
	public Object getClientGuiElement(int ID, EntityPlayer player,
			World world, int x, int y, int z) {
		for (IThirdParty thirdParty : thirdPartyMods) {
			if (thirdParty instanceof IGuiHandler) {
				Object obj = ((IGuiHandler) thirdParty)
						.getClientGuiElement(ID,
								player,
								world, x,
								y, z);
				if (obj != null)
					return obj;
			}
		}
		return null;
	}

}