Skip to content

Commit

Permalink
Add a named zenevent converter
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredlll08 committed Jun 18, 2023
1 parent 8f0489f commit 4b7359a
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.blamejared.crafttweaker.annotation.processor.document.conversion.converter;

import com.blamejared.crafttweaker.annotation.processor.document.DocumentRegistry;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.event.EventConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.event.NamedEventConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.event.NativeEventConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.expansion.ExpansionConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.named_type.NamedTypeConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.native_registration.NativeRegistrationConverter;
Expand All @@ -24,7 +25,8 @@ public DocumentConversionRegistry(DocumentRegistry documentRegistry, DependencyC
this.documentRegistry = documentRegistry;
//
// TODO: Add converters
converters.add((dependencyContainer.getInstanceOfClass(EventConverter.class)));
converters.add((dependencyContainer.getInstanceOfClass(NativeEventConverter.class)));
converters.add((dependencyContainer.getInstanceOfClass(NamedEventConverter.class)));
converters.add(dependencyContainer.getInstanceOfClass(NativeRegistrationConverter.class));
converters.add(dependencyContainer.getInstanceOfClass(ExpansionConverter.class));
converters.add(dependencyContainer.getInstanceOfClass(NamedTypeConverter.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.event;

import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.DocumentConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.comment.CommentConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.member.EnumConstantConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.member.header.GenericParameterConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.member.static_member.StaticMemberConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.named_type.ImplementationConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.named_type.SuperTypeConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.converter.named_type.member.NamedTypeVirtualMemberConverter;
import com.blamejared.crafttweaker.annotation.processor.document.conversion.mods.KnownModList;
import com.blamejared.crafttweaker.annotation.processor.document.page.info.DocumentationPageInfo;
import com.blamejared.crafttweaker.annotation.processor.document.page.info.TypeName;
import com.blamejared.crafttweaker.annotation.processor.document.page.info.TypePageInfo;
import com.blamejared.crafttweaker.annotation.processor.document.page.member.header.DocumentedGenericParameter;
import com.blamejared.crafttweaker.annotation.processor.document.page.member.header.examples.Example;
import com.blamejared.crafttweaker.annotation.processor.document.page.member.static_member.DocumentedStaticMembers;
import com.blamejared.crafttweaker.annotation.processor.document.page.member.virtual_member.DocumentedVirtualMembers;
import com.blamejared.crafttweaker.annotation.processor.document.page.page.DocumentationPage;
import com.blamejared.crafttweaker.annotation.processor.document.page.page.EventPage;
import com.blamejared.crafttweaker.annotation.processor.document.page.type.AbstractTypeInfo;
import com.blamejared.crafttweaker.api.event.ZenEventWrapper;
import org.openzen.zencode.java.ZenCodeType;

import javax.annotation.Nonnull;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import java.util.List;
import java.util.stream.Collectors;

public class NamedEventConverter extends DocumentConverter {

private final GenericParameterConverter genericParameterConverter;
private final SuperTypeConverter superTypeConverter;
private final NamedTypeVirtualMemberConverter namedTypeVirtualMemberConverter;
private final ImplementationConverter implementationConverter;
private final StaticMemberConverter staticMemberConverter;
private final EnumConstantConverter enumConstantConverter;

public NamedEventConverter(KnownModList knownModList, CommentConverter commentConverter, GenericParameterConverter genericParameterConverter, SuperTypeConverter superTypeConverter, NamedTypeVirtualMemberConverter namedTypeVirtualMemberConverter, ImplementationConverter implementationConverter, StaticMemberConverter staticMemberConverter, EnumConstantConverter enumConstantConverter) {

super(knownModList, commentConverter);

this.genericParameterConverter = genericParameterConverter;
this.superTypeConverter = superTypeConverter;
this.namedTypeVirtualMemberConverter = namedTypeVirtualMemberConverter;
this.implementationConverter = implementationConverter;
this.staticMemberConverter = staticMemberConverter;
this.enumConstantConverter = enumConstantConverter;
}

@Override
public boolean canConvert(TypeElement typeElement) {

return typeElement.getAnnotation(ZenCodeType.Name.class) != null && ZenEventWrapper.isAnnotated(typeElement);
}

@Override
protected DocumentationPageInfo prepareConversion(TypeElement element) {

final DocumentationPageInfo documentationPageInfo = super.prepareConversion(element);

final TypeName name = getName(element);
final String declaringModId = documentationPageInfo.declaringModId;
final String outputPath = documentationPageInfo.getOutputPath();
return new TypePageInfo(declaringModId, outputPath, name);
}

@Nonnull
private TypeName getName(TypeElement element) {

return new TypeName(element.getAnnotation(ZenCodeType.Name.class).value());
}

private boolean isEnum(TypeElement element) {

return element.getKind() == ElementKind.ENUM;
}

@Override
protected Example getFallbackThisInformationFor(TypeElement typeElement) {

final String text = "my" + getName(typeElement).getSimpleName();
return new Example("this", text);
}

@Override
public DocumentationPage convert(TypeElement typeElement, DocumentationPageInfo pageInfo) {

final TypePageInfo typePageInfo = (TypePageInfo) pageInfo;
final DocumentedVirtualMembers virtualMembers = convertVirtualMembers(typeElement, typePageInfo);
final DocumentedStaticMembers staticMembers = convertStaticMembers(typeElement, typePageInfo);
final AbstractTypeInfo superType = convertSuperType(typeElement);
final List<AbstractTypeInfo> implementations = convertImplementations(typeElement);
final List<DocumentedGenericParameter> genericParameters = convertGenericParameters(typeElement);

return new EventPage(typePageInfo, virtualMembers, superType, implementations, staticMembers, genericParameters);
}

private DocumentedVirtualMembers convertVirtualMembers(TypeElement typeElement, TypePageInfo typePageInfo) {

return namedTypeVirtualMemberConverter.convertFor(typeElement, typePageInfo);
}

private DocumentedStaticMembers convertStaticMembers(TypeElement typeElement, TypePageInfo typePageInfo) {

return staticMemberConverter.convertFor(typeElement, typePageInfo);
}

private AbstractTypeInfo convertSuperType(TypeElement typeElement) {

if(isEnum(typeElement)) {
// we should not print redundant information "extending Enum<E>"
return null;
}

return superTypeConverter.convertSuperTypeFor(typeElement).orElse(null);
}

private List<AbstractTypeInfo> convertImplementations(TypeElement typeElement) {

return implementationConverter.convertInterfacesFor(typeElement);
}

private List<DocumentedGenericParameter> convertGenericParameters(TypeElement typeElement) {

return typeElement.getTypeParameters()
.stream()
.map(genericParameterConverter::convertGenericParameter)
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.List;
import java.util.stream.Collectors;

public class EventConverter extends DocumentConverter {
public class NativeEventConverter extends DocumentConverter {

private final StaticMemberConverter staticMemberConverter;
private final NativeTypeVirtualMemberConverter virtualMemberConverter;
Expand All @@ -40,7 +40,7 @@ public class EventConverter extends DocumentConverter {
private final ClassTypeConverter classTypeConverter;
private final EnumConstantConverter enumConstantConverter;

public EventConverter(KnownModList knownModList, CommentConverter commentConverter, StaticMemberConverter staticMemberConverter, NativeTypeVirtualMemberConverter virtualMemberConverter, SuperTypeConverter superTypeConverter, ImplementationConverter implementationConverter, GenericParameterConverter genericParameterConverter, NativeConversionRegistry nativeConversionRegistry, Types typeUtils, ClassTypeConverter classTypeConverter, EnumConstantConverter enumConstantConverter) {
public NativeEventConverter(KnownModList knownModList, CommentConverter commentConverter, StaticMemberConverter staticMemberConverter, NativeTypeVirtualMemberConverter virtualMemberConverter, SuperTypeConverter superTypeConverter, ImplementationConverter implementationConverter, GenericParameterConverter genericParameterConverter, NativeConversionRegistry nativeConversionRegistry, Types typeUtils, ClassTypeConverter classTypeConverter, EnumConstantConverter enumConstantConverter) {

super(knownModList, commentConverter);
this.staticMemberConverter = staticMemberConverter;
Expand Down

0 comments on commit 4b7359a

Please sign in to comment.