-
Notifications
You must be signed in to change notification settings - Fork 497
/
SchematicRegistry.java
209 lines (176 loc) · 7.13 KB
/
SchematicRegistry.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
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.blueprints;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.entity.Entity;
import net.minecraft.init.Blocks;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.oredict.OreDictionary;
import buildcraft.api.blueprints.ISchematicRegistry;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.SchematicBlock;
import buildcraft.api.blueprints.SchematicEntity;
import buildcraft.api.core.BlockMetaPair;
import buildcraft.api.core.JavaTools;
public class SchematicRegistry implements ISchematicRegistry {
public static SchematicRegistry INSTANCE = new SchematicRegistry();
private final HashMap<BlockMetaPair, SchematicConstructor> schematicBlocks =
new HashMap<BlockMetaPair, SchematicConstructor>();
private final HashMap<Class<? extends Entity>, SchematicConstructor> schematicEntities = new HashMap<Class<? extends Entity>, SchematicConstructor>();
private final HashSet<String> modsForbidden = new HashSet<String>();
private final HashSet<String> blocksForbidden = new HashSet<String>();
private SchematicRegistry() {
}
private class SchematicConstructor {
public final Class<? extends Schematic> clazz;
public final Object[] params;
private final Constructor<?> constructor;
SchematicConstructor(Class<? extends Schematic> clazz, Object[] params) throws IllegalArgumentException {
this.clazz = clazz;
this.params = params;
this.constructor = findConstructor();
}
public Schematic newInstance() throws IllegalAccessException, InvocationTargetException, InstantiationException {
return (Schematic) constructor.newInstance(params);
}
private Constructor<?> findConstructor() throws IllegalArgumentException {
for (Constructor<?> c : clazz.getConstructors()) {
Class<?>[] typesSignature = c.getParameterTypes();
if (typesSignature.length != params.length) {
// non-matching constructor count arguments, skip
continue;
}
boolean valid = true;
for (int i = 0; i < params.length; i++) {
if (params[i] == null) {
// skip checking for null parameters
continue;
}
Class<?> paramClass = params[i].getClass();
if (!(typesSignature[i].isAssignableFrom(paramClass)
|| (typesSignature[i] == int.class && paramClass == Integer.class)
|| (typesSignature[i] == double.class && paramClass == Double.class)
|| (typesSignature[i] == boolean.class && paramClass == Boolean.class))) {
// constructor has non assignable parameters skip constructor...
valid = false;
break;
}
}
if (!valid) {
continue;
}
return c;
}
throw new IllegalArgumentException("Could not find matching constructor for class " + clazz);
}
}
public void registerSchematicBlock(Block block, Class<? extends Schematic> clazz, Object... params) {
registerSchematicBlock(block, OreDictionary.WILDCARD_VALUE, clazz, params);
}
public void registerSchematicBlock(Block block, int meta, Class<? extends Schematic> clazz, Object... params) {
if (schematicBlocks.containsKey(new BlockMetaPair(block, meta))) {
throw new RuntimeException("Block " + Block.blockRegistry.getNameForObject(block) + " is already associated with a schematic.");
}
schematicBlocks.put(new BlockMetaPair(block, meta), new SchematicConstructor(clazz, params));
}
public void registerSchematicEntity(
Class<? extends Entity> entityClass,
Class<? extends SchematicEntity> schematicClass, Object... params) {
if (schematicEntities.containsKey(entityClass)) {
throw new RuntimeException("Entity " + entityClass.getName() + " is already associated with a schematic.");
}
schematicEntities.put(entityClass, new SchematicConstructor(schematicClass, params));
}
public SchematicBlock createSchematicBlock(Block block, int metadata) {
if (block == Blocks.air) {
return null;
}
BlockMetaPair pairWildcard = new BlockMetaPair(block, OreDictionary.WILDCARD_VALUE);
BlockMetaPair pair = new BlockMetaPair(block, metadata);
SchematicConstructor c = null;
if (schematicBlocks.containsKey(pair)) {
c = schematicBlocks.get(pair);
} else if (schematicBlocks.containsKey(pairWildcard)) {
c = schematicBlocks.get(pairWildcard);
}
if (c == null) {
return null;
}
try {
SchematicBlock s = (SchematicBlock) c.newInstance();
s.block = block;
return s;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return null;
}
public SchematicEntity createSchematicEntity(Class<? extends Entity> entityClass) {
if (!schematicEntities.containsKey(entityClass)) {
return null;
}
try {
SchematicConstructor c = schematicEntities.get(entityClass);
SchematicEntity s = (SchematicEntity) c.newInstance();
s.entity = entityClass;
return s;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return null;
}
public boolean isSupported(Block block, int metadata) {
return schematicBlocks.containsKey(new BlockMetaPair(block, OreDictionary.WILDCARD_VALUE))
|| schematicBlocks.containsKey(new BlockMetaPair(block, metadata));
}
public boolean isAllowedForBuilding(Block block, int metadata) {
String name = Block.blockRegistry.getNameForObject(block);
return isSupported(block, metadata) && !blocksForbidden.contains(name) && !modsForbidden.contains(name.split(":", 2)[0]);
}
public void readConfiguration(Configuration conf) {
Property excludedMods = conf.get(Configuration.CATEGORY_GENERAL, "builder.excludedMods", new String[0],
"mods that should be excluded from the builder.");
Property excludedBlocks = conf.get(Configuration.CATEGORY_GENERAL, "builder.excludedBlocks", new String[0],
"blocks that should be excluded from the builder.");
for (String id : excludedMods.getStringList()) {
String strippedId = JavaTools.stripSurroundingQuotes(id.trim());
if (strippedId.length() > 0) {
modsForbidden.add(strippedId);
}
}
for (String id : excludedBlocks.getStringList()) {
String strippedId = JavaTools.stripSurroundingQuotes(id.trim());
if (strippedId.length() > 0) {
blocksForbidden.add(strippedId);
}
}
}
}