pick 97420a31d The big reformat of 2020
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
|
||||
package appeng.items.parts;
|
||||
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -15,130 +14,111 @@ import net.minecraft.util.ResourceLocation;
|
||||
import appeng.api.parts.IPartModel;
|
||||
import appeng.core.AELog;
|
||||
|
||||
|
||||
/**
|
||||
* Helps with the reflection magic needed to gather all models for AE2 cable bus parts.
|
||||
* Helps with the reflection magic needed to gather all models for AE2 cable bus
|
||||
* parts.
|
||||
*/
|
||||
class PartModelsHelper
|
||||
{
|
||||
class PartModelsHelper {
|
||||
|
||||
static List<ResourceLocation> createModels( Class<?> clazz )
|
||||
{
|
||||
List<ResourceLocation> locations = new ArrayList<>();
|
||||
static List<ResourceLocation> createModels(Class<?> clazz) {
|
||||
List<ResourceLocation> locations = new ArrayList<>();
|
||||
|
||||
// Check all static fields for used models
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for( Field field : fields )
|
||||
{
|
||||
if( field.getAnnotation( PartModels.class ) == null )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// Check all static fields for used models
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
if (field.getAnnotation(PartModels.class) == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if( !Modifier.isStatic( field.getModifiers() ) )
|
||||
{
|
||||
AELog.error( "The @PartModels annotation can only be used on static fields or methods. Was seen on: " + field );
|
||||
continue;
|
||||
}
|
||||
if (!Modifier.isStatic(field.getModifiers())) {
|
||||
AELog.error("The @PartModels annotation can only be used on static fields or methods. Was seen on: "
|
||||
+ field);
|
||||
continue;
|
||||
}
|
||||
|
||||
Object value;
|
||||
try
|
||||
{
|
||||
field.setAccessible( true );
|
||||
value = field.get( null );
|
||||
}
|
||||
catch( IllegalAccessException e )
|
||||
{
|
||||
AELog.error( e, "Cannot access field annotated with @PartModels: " + field );
|
||||
continue;
|
||||
}
|
||||
Object value;
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
value = field.get(null);
|
||||
} catch (IllegalAccessException e) {
|
||||
AELog.error(e, "Cannot access field annotated with @PartModels: " + field);
|
||||
continue;
|
||||
}
|
||||
|
||||
convertAndAddLocation( field, value, locations );
|
||||
}
|
||||
convertAndAddLocation(field, value, locations);
|
||||
}
|
||||
|
||||
// Check all static methods for the annotation
|
||||
for( Method method : clazz.getDeclaredMethods() )
|
||||
{
|
||||
if( method.getAnnotation( PartModels.class ) == null )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// Check all static methods for the annotation
|
||||
for (Method method : clazz.getDeclaredMethods()) {
|
||||
if (method.getAnnotation(PartModels.class) == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if( !Modifier.isStatic( method.getModifiers() ) )
|
||||
{
|
||||
AELog.error( "The @PartModels annotation can only be used on static fields or methods. Was seen on: " + method );
|
||||
continue;
|
||||
}
|
||||
if (!Modifier.isStatic(method.getModifiers())) {
|
||||
AELog.error("The @PartModels annotation can only be used on static fields or methods. Was seen on: "
|
||||
+ method);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for parameter count
|
||||
if( method.getParameters().length != 0 )
|
||||
{
|
||||
AELog.error( "The @PartModels annotation can only be used on static methods without parameters. Was seen on: " + method );
|
||||
continue;
|
||||
}
|
||||
// Check for parameter count
|
||||
if (method.getParameters().length != 0) {
|
||||
AELog.error(
|
||||
"The @PartModels annotation can only be used on static methods without parameters. Was seen on: "
|
||||
+ method);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Make sure we can handle the return type
|
||||
Class<?> returnType = method.getReturnType();
|
||||
if( !ResourceLocation.class.isAssignableFrom( returnType ) && !Collection.class.isAssignableFrom( returnType ) )
|
||||
{
|
||||
AELog.error(
|
||||
"The @PartModels annotation can only be used on static methods that return a ResourceLocation or Collection of " + "ResourceLocations. Was seen on: " + method );
|
||||
continue;
|
||||
}
|
||||
// Make sure we can handle the return type
|
||||
Class<?> returnType = method.getReturnType();
|
||||
if (!ResourceLocation.class.isAssignableFrom(returnType)
|
||||
&& !Collection.class.isAssignableFrom(returnType)) {
|
||||
AELog.error(
|
||||
"The @PartModels annotation can only be used on static methods that return a ResourceLocation or Collection of "
|
||||
+ "ResourceLocations. Was seen on: " + method);
|
||||
continue;
|
||||
}
|
||||
|
||||
Object value = null;
|
||||
try
|
||||
{
|
||||
method.setAccessible( true );
|
||||
value = method.invoke( null );
|
||||
}
|
||||
catch( IllegalAccessException | InvocationTargetException e )
|
||||
{
|
||||
AELog.error( e, "Failed to invoke the @PartModels annotated method " + method );
|
||||
continue;
|
||||
}
|
||||
Object value = null;
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
value = method.invoke(null);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
AELog.error(e, "Failed to invoke the @PartModels annotated method " + method);
|
||||
continue;
|
||||
}
|
||||
|
||||
convertAndAddLocation( method, value, locations );
|
||||
}
|
||||
convertAndAddLocation(method, value, locations);
|
||||
}
|
||||
|
||||
if( clazz.getSuperclass() != null )
|
||||
{
|
||||
locations.addAll( createModels( clazz.getSuperclass() ) );
|
||||
}
|
||||
if (clazz.getSuperclass() != null) {
|
||||
locations.addAll(createModels(clazz.getSuperclass()));
|
||||
}
|
||||
|
||||
return locations;
|
||||
}
|
||||
return locations;
|
||||
}
|
||||
|
||||
private static void convertAndAddLocation( Object source, Object value, List<ResourceLocation> locations )
|
||||
{
|
||||
if( value == null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
private static void convertAndAddLocation(Object source, Object value, List<ResourceLocation> locations) {
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if( value instanceof ResourceLocation )
|
||||
{
|
||||
locations.add( (ResourceLocation) value );
|
||||
}
|
||||
else if( value instanceof IPartModel )
|
||||
{
|
||||
locations.addAll( ( (IPartModel) value ).getModels() );
|
||||
}
|
||||
else if( value instanceof Collection )
|
||||
{
|
||||
// Check that each object is an IPartModel
|
||||
Collection values = (Collection) value;
|
||||
for( Object candidate : values )
|
||||
{
|
||||
if( !( candidate instanceof IPartModel ) )
|
||||
{
|
||||
AELog.error( "List of locations obtained from {} contains a non resource location: {}", source, candidate );
|
||||
continue;
|
||||
}
|
||||
if (value instanceof ResourceLocation) {
|
||||
locations.add((ResourceLocation) value);
|
||||
} else if (value instanceof IPartModel) {
|
||||
locations.addAll(((IPartModel) value).getModels());
|
||||
} else if (value instanceof Collection) {
|
||||
// Check that each object is an IPartModel
|
||||
Collection values = (Collection) value;
|
||||
for (Object candidate : values) {
|
||||
if (!(candidate instanceof IPartModel)) {
|
||||
AELog.error("List of locations obtained from {} contains a non resource location: {}", source,
|
||||
candidate);
|
||||
continue;
|
||||
}
|
||||
|
||||
locations.addAll( ( (IPartModel) candidate ).getModels() );
|
||||
}
|
||||
}
|
||||
}
|
||||
locations.addAll(((IPartModel) candidate).getModels());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user