Skip to content

Commit

Permalink
Force BEPs to return a specific type
Browse files Browse the repository at this point in the history
  • Loading branch information
kindlich committed Dec 8, 2019
1 parent f00cf7d commit cd3bdfb
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 9 deletions.
@@ -0,0 +1,45 @@
package com.blamejared.crafttweaker_annotation_processors.processors;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import java.util.List;
import java.util.Set;

@SupportedAnnotationTypes({"com.blamejared.crafttweaker.api.annotations.BracketResolver"})
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class BracketHandlerCheckValidationProcessor extends AbstractProcessor {
private static final String BEPReturnTypeCanonicalName = "com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable";

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement annotation : annotations) {
for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
if(!(element instanceof ExecutableElement)) {
this.processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "How is this annotated?", element);
continue;
}

ExecutableElement executableElement = (ExecutableElement) element;
final List<? extends VariableElement> parameters = executableElement.getParameters();
if(parameters.size() != 1 || !this.processingEnv.getTypeUtils().isSameType(parameters.get(0).asType(), this.processingEnv.getElementUtils().getTypeElement(String.class.getCanonicalName()).asType())) {
this.processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Element is annotated as BEP but does not accept a String as its only parameter.", element);
}

if(!this.processingEnv.getTypeUtils().isAssignable(executableElement.getReturnType(), this.processingEnv.getElementUtils().getTypeElement(BEPReturnTypeCanonicalName).asType())){
this.processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Element is annotated as BEP but does not return " + BEPReturnTypeCanonicalName + " or any subtype.", element);
}
}
}

return false;
}
}
@@ -1,2 +1,3 @@
com.blamejared.crafttweaker_annotation_processors.processors.DocumentProcessor
com.blamejared.crafttweaker_annotation_processors.processors.AsIActionProcessor
com.blamejared.crafttweaker_annotation_processors.processors.AsIActionProcessor
com.blamejared.crafttweaker_annotation_processors.processors.BracketHandlerCheckValidationProcessor
Expand Up @@ -2,6 +2,7 @@

import com.blamejared.crafttweaker.CraftTweaker;
import com.blamejared.crafttweaker.api.annotations.*;
import com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable;
import com.blamejared.crafttweaker.api.zencode.IPreprocessor;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -137,6 +138,10 @@ private static void handleBracketResolver(Method method) {
} else {
CraftTweakerAPI.logWarning("Method \"%s\" is marked as a BracketResolver, but it does not have a String as it's only parameter.", method.toString());
}

if(!CommandStringDisplayable.class.isAssignableFrom(method.getReturnType())){
CraftTweakerAPI.logWarning("Method \"%s\" is marked as a BracketResolver, so it should return something that implements %s.", method.toString(), CommandStringDisplayable.class.getSimpleName());
}
}

private static void handleBracketDumper(Method method) {
Expand Down
@@ -0,0 +1,21 @@
package com.blamejared.crafttweaker.api.brackets;

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker_annotations.annotations.Document;
import org.openzen.zencode.java.ZenCodeType;

/**
* This is A helper interface for every item that is returned by a BEP!
*
* @docParam this null
*/
@ZenRegister
@ZenCodeType.Name("crafttweaker.api.brackets.CommandStringDisplayable")
@Document("vanilla/brackets/CommandStringDisplayable")
public interface CommandStringDisplayable {
/**
* Returns the BEP to get this thingy
*/
@ZenCodeType.Getter("commandString")
String getCommandString();
}
@@ -1,6 +1,7 @@
package com.blamejared.crafttweaker.api.item;

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable;
import com.blamejared.crafttweaker.impl.item.MCIngredientList;
import com.blamejared.crafttweaker.impl.item.MCItemStack;
import com.blamejared.crafttweaker_annotations.annotations.Document;
Expand All @@ -22,7 +23,7 @@
@ZenCodeType.Name("crafttweaker.api.item.IIngredient")
@Document("vanilla/items/IIngredient")
@ZenWrapper(wrappedClass = "net.minecraft.item.crafting.Ingredient", conversionMethodFormat = "%s.asVanillaIngredient()", displayStringFormat = "%.getCommandString()")
public interface IIngredient {
public interface IIngredient extends CommandStringDisplayable {

/**
* Does the given stack match the ingredient?
Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.blamejared.crafttweaker.CraftTweaker;
import com.blamejared.crafttweaker.api.CraftTweakerAPI;
import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable;
import com.blamejared.crafttweaker.api.data.IData;
import com.blamejared.crafttweaker.api.item.IItemStack;
import com.blamejared.crafttweaker.impl.actions.recipes.ActionAddRecipe;
Expand Down Expand Up @@ -33,7 +34,7 @@
@ZenRegister
@ZenCodeType.Name("crafttweaker.api.registries.IRecipeManager")
@Document("vanilla/managers/IRecipeManager")
public interface IRecipeManager {
public interface IRecipeManager extends CommandStringDisplayable {

Gson JSON_RECIPE_GSON = new GsonBuilder().create();

Expand Down Expand Up @@ -168,5 +169,10 @@ interface RecipeFunctionMatrix {

IItemStack process(IItemStack usualOut, IItemStack[][] inputs);
}

@Override
default String getCommandString() {
return "<recipetype:" + getRecipeType().toString() + ">";
}

}
Expand Up @@ -2,6 +2,7 @@

import com.blamejared.crafttweaker.api.CraftTweakerAPI;
import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import net.minecraft.block.BlockState;
Expand All @@ -17,7 +18,7 @@

@ZenRegister
@ZenCodeType.Name("crafttweaker.api.block.MCBlockState")
public class MCBlockState {
public class MCBlockState implements CommandStringDisplayable {

private final BlockState internal;

Expand Down Expand Up @@ -138,6 +139,7 @@ public static MCBlock asBlock(MCBlockState block) {
}

@ZenCodeType.Getter("commandString")
@Override
public String getCommandString() {
StringBuilder builder = new StringBuilder("<blockstate:");
builder.append(getBlock().getInternal().getRegistryName().toString());
Expand Down
@@ -1,12 +1,13 @@
package com.blamejared.crafttweaker.impl.entity;

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable;
import net.minecraft.entity.EntityClassification;
import org.openzen.zencode.java.ZenCodeType;

@ZenRegister
@ZenCodeType.Name("crafttweaker.api.entity.MCEntityClassification")
public class MCEntityClassification {
public class MCEntityClassification implements CommandStringDisplayable {

private EntityClassification internal;

Expand All @@ -33,8 +34,15 @@ public boolean isPeacefulCreature() {
public boolean isAnimal() {
return internal.getAnimal();
}



public EntityClassification getInternal() {
return internal;
}

@Override
public String getCommandString() {
return "<entityclassification:" + internal.func_220363_a() + ">";
}
}
@@ -1,12 +1,13 @@
package com.blamejared.crafttweaker.impl.entity;

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable;
import net.minecraft.entity.EntityType;
import org.openzen.zencode.java.ZenCodeType;

@ZenRegister
@ZenCodeType.Name("crafttweaker.api.entity.MCEntityType")
public class MCEntityType {
public class MCEntityType implements CommandStringDisplayable {

private final EntityType internal;
private final MCEntityClassification classification;
Expand Down Expand Up @@ -63,6 +64,7 @@ public float getHeight() {
}

@ZenCodeType.Getter("commandString")
@Override
public String getCommandString() {
return "<entityType:" + internal.getRegistryName() + ">";
}
Expand Down
@@ -1,6 +1,7 @@
package com.blamejared.crafttweaker.impl.potion;

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable;
import com.blamejared.crafttweaker.api.item.IItemStack;
import com.blamejared.crafttweaker.impl.helper.CraftTweakerHelper;
import net.minecraft.potion.Effect;
Expand All @@ -11,7 +12,7 @@

@ZenRegister
@ZenCodeType.Name("crafttweaker.api.potion.MCPotionEffect")
public class MCEffect {
public class MCEffect implements CommandStringDisplayable {

private final Effect internal;

Expand Down Expand Up @@ -62,4 +63,9 @@ public List<IItemStack> getCurativeItems() {
public Effect getInternal() {
return internal;
}

@Override
public String getCommandString() {
return "<effect:" + internal.getRegistryName() + ">";
}
}
Expand Up @@ -2,6 +2,7 @@


import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable;
import net.minecraft.potion.Potion;
import org.openzen.zencode.java.ZenCodeType;

Expand All @@ -10,7 +11,7 @@

@ZenRegister
@ZenCodeType.Name("crafttweaker.api.potion.MCPotion")
public class MCPotion {
public class MCPotion implements CommandStringDisplayable {

private final Potion internal;

Expand All @@ -36,4 +37,9 @@ public boolean hasInstantEffect() {
public Potion getInternal() {
return internal;
}

@Override
public String getCommandString() {
return "<potion:" + internal.getRegistryName() + ">";
}
}
@@ -1,6 +1,7 @@
package com.blamejared.crafttweaker.impl.util;

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable;
import com.blamejared.crafttweaker_annotations.annotations.Document;
import net.minecraft.util.Direction;
import net.minecraft.util.Util;
Expand All @@ -17,7 +18,7 @@
@ZenRegister
@ZenCodeType.Name("crafttweaker.api.util.DirectionAxis")
@Document("vanilla/util/DirectionAxis")
public class CTDirectionAxis {
public class CTDirectionAxis implements CommandStringDisplayable {

private static final Map<Direction.Axis, CTDirectionAxis> AXIS_MAP = Util.make(new HashMap<>(), map -> {
map.put(Direction.Axis.X, new CTDirectionAxis(Direction.Axis.X));
Expand Down Expand Up @@ -104,4 +105,9 @@ public Direction.Axis getInternal() {
public static CTDirectionAxis getAxis(Direction.Axis axis) {
return AXIS_MAP.get(axis);
}

@Override
public String getCommandString() {
return "<directionaxis:" + internal.getName2() + ">";
}
}

0 comments on commit cd3bdfb

Please sign in to comment.