Search in sources :

Example 1 with EnhancedRuntimeException

use of net.minecraftforge.fml.common.EnhancedRuntimeException in project MinecraftForge by MinecraftForge.

the class PersistentRegistryManager method loadRegistry.

private static <T extends IForgeRegistryEntry<T>> void loadRegistry(final ResourceLocation registryName, final PersistentRegistry from, final PersistentRegistry to, final Class<T> regType) {
    FMLControlledNamespacedRegistry<T> fromRegistry = from.getRegistry(registryName, regType);
    if (fromRegistry == null) {
        FMLControlledNamespacedRegistry<T> toRegistry = to.getRegistry(registryName, regType);
        if (toRegistry == null) {
            throw new EnhancedRuntimeException("Could not find registry to load: " + registryName) {

                private static final long serialVersionUID = 1L;

                @Override
                protected void printStackTrace(WrappedPrintStream stream) {
                    stream.println("Looking For: " + registryName);
                    stream.println("Found From:");
                    for (ResourceLocation name : from.registries.keySet()) stream.println("  " + name);
                    stream.println("Found To:");
                    for (ResourceLocation name : to.registries.keySet()) stream.println("  " + name);
                }
            };
        }
        // We found it in to, so lets trust to's state...
        // This happens when connecting to a server that doesn't have this registry.
        // Such as a 1.8.0 Forge server with 1.8.8+ Forge.
        // We must however, re-fire the callbacks as some internal data may be corrupted {potions}
        toRegistry.notifyCallbacks();
    } else {
        FMLControlledNamespacedRegistry<T> toRegistry = to.getOrShallowCopyRegistry(registryName, regType, fromRegistry);
        toRegistry.set(fromRegistry);
    }
}
Also used : EnhancedRuntimeException(net.minecraftforge.fml.common.EnhancedRuntimeException) ResourceLocation(net.minecraft.util.ResourceLocation)

Example 2 with EnhancedRuntimeException

use of net.minecraftforge.fml.common.EnhancedRuntimeException in project MinecraftForge by MinecraftForge.

the class EnumHelper method addEnum.

@SuppressWarnings({ "unchecked", "serial" })
@Nullable
private static <T extends Enum<?>> T addEnum(boolean test, final Class<T> enumType, @Nullable String enumName, final Class<?>[] paramTypes, @Nullable Object[] paramValues) {
    if (!isSetup) {
        setup();
    }
    Field valuesField = null;
    Field[] fields = enumType.getDeclaredFields();
    for (Field field : fields) {
        String name = field.getName();
        if (//Added 'ENUM$VALUES' because Eclipse's internal compiler doesn't follow standards
        name.equals("$VALUES") || name.equals("ENUM$VALUES")) {
            valuesField = field;
            break;
        }
    }
    int flags = (FMLForgePlugin.RUNTIME_DEOBF ? Modifier.PUBLIC : Modifier.PRIVATE) | Modifier.STATIC | Modifier.FINAL | 0x1000;
    if (valuesField == null) {
        String valueType = String.format("[L%s;", enumType.getName().replace('.', '/'));
        for (Field field : fields) {
            if ((field.getModifiers() & flags) == flags && //Apparently some JVMs return .'s and some don't..
            field.getType().getName().replace('.', '/').equals(valueType)) {
                valuesField = field;
                break;
            }
        }
    }
    if (valuesField == null) {
        final List<String> lines = Lists.newArrayList();
        lines.add(String.format("Could not find $VALUES field for enum: %s", enumType.getName()));
        lines.add(String.format("Runtime Deobf: %s", FMLForgePlugin.RUNTIME_DEOBF));
        lines.add(String.format("Flags: %s", String.format("%16s", Integer.toBinaryString(flags)).replace(' ', '0')));
        lines.add("Fields:");
        for (Field field : fields) {
            String mods = String.format("%16s", Integer.toBinaryString(field.getModifiers())).replace(' ', '0');
            lines.add(String.format("       %s %s: %s", mods, field.getName(), field.getType().getName()));
        }
        for (String line : lines) FMLLog.severe(line);
        if (test) {
            throw new EnhancedRuntimeException("Could not find $VALUES field for enum: " + enumType.getName()) {

                @Override
                protected void printStackTrace(WrappedPrintStream stream) {
                    for (String line : lines) stream.println(line);
                }
            };
        }
        return null;
    }
    if (test) {
        Object ctr = null;
        Exception ex = null;
        try {
            ctr = getConstructorAccessor(enumType, paramTypes);
        } catch (Exception e) {
            ex = e;
        }
        if (ctr == null || ex != null) {
            throw new EnhancedRuntimeException(String.format("Could not find constructor for Enum %s", enumType.getName()), ex) {

                private String toString(Class<?>[] cls) {
                    StringBuilder b = new StringBuilder();
                    for (int x = 0; x < cls.length; x++) {
                        b.append(cls[x].getName());
                        if (x != cls.length - 1)
                            b.append(", ");
                    }
                    return b.toString();
                }

                @Override
                protected void printStackTrace(WrappedPrintStream stream) {
                    stream.println("Target Arguments:");
                    stream.println("    java.lang.String, int, " + toString(paramTypes));
                    stream.println("Found Constructors:");
                    for (Constructor<?> ctr : enumType.getDeclaredConstructors()) {
                        stream.println("    " + toString(ctr.getParameterTypes()));
                    }
                }
            };
        }
        return null;
    }
    valuesField.setAccessible(true);
    try {
        T[] previousValues = (T[]) valuesField.get(enumType);
        List<T> values = new ArrayList<T>(Arrays.asList(previousValues));
        T newValue = makeEnum(enumType, enumName, values.size(), paramTypes, paramValues);
        values.add(newValue);
        setFailsafeFieldValue(valuesField, null, values.toArray((T[]) Array.newInstance(enumType, 0)));
        cleanEnumCache(enumType);
        return newValue;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : EnhancedRuntimeException(net.minecraftforge.fml.common.EnhancedRuntimeException) EnhancedRuntimeException(net.minecraftforge.fml.common.EnhancedRuntimeException) EnhancedRuntimeException(net.minecraftforge.fml.common.EnhancedRuntimeException) Nullable(javax.annotation.Nullable)

Aggregations

EnhancedRuntimeException (net.minecraftforge.fml.common.EnhancedRuntimeException)2 Nullable (javax.annotation.Nullable)1 ResourceLocation (net.minecraft.util.ResourceLocation)1