-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
265 additions
and
21 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/pl/pabilo8/modworks/annotations/item/GeneratedItemModels.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package pl.pabilo8.modworks.annotations.item; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Used to annotate an enum with item | ||
* @author Pabilo8 | ||
* @since 11.08.2023 | ||
*/ | ||
@Retention(RetentionPolicy.SOURCE) | ||
@Target({ElementType.TYPE}) | ||
public @interface GeneratedItemModels | ||
{ | ||
/** | ||
* @return item name | ||
*/ | ||
String itemName(); | ||
|
||
/** | ||
* @return item texture path | ||
*/ | ||
String texturePath() default ""; | ||
|
||
/** | ||
* @return base model type of all entries | ||
*/ | ||
ItemModelType type() default ItemModelType.ITEM_SIMPLE; | ||
|
||
/** | ||
* @return whether models should only be generated by entries annotated with {@link GeneratedSubItemModel} | ||
*/ | ||
boolean onlyInAnnotated() default false; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/pl/pabilo8/modworks/annotations/item/GeneratedSubItemModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package pl.pabilo8.modworks.annotations.item; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Used to annotate an enum with item | ||
* @author Pabilo8 | ||
* @since 11.08.2023 | ||
*/ | ||
@Retention(RetentionPolicy.SOURCE) | ||
@Target({ElementType.FIELD}) | ||
public @interface GeneratedSubItemModel | ||
{ | ||
String customTexturePath() default ""; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/pl/pabilo8/modworks/annotations/item/ItemModelType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pl.pabilo8.modworks.annotations.item; | ||
|
||
/** | ||
* @author Pabilo8 | ||
* @since 12.08.2023 | ||
*/ | ||
public enum ItemModelType | ||
{ | ||
//For simple, batched items like crafting materials | ||
ITEM_SIMPLE("item/generated"), | ||
//For tool item models, like hammers, wrenches, pickaxes | ||
ITEM_SIMPLE_TOOL("item/handheld"), | ||
//For empty item models that will have contents generated dynamically | ||
ITEM_SIMPLE_AUTOREPLACED("item/generated"); | ||
|
||
private final String parentModel; | ||
|
||
ItemModelType(String parentModel) | ||
{ | ||
this.parentModel = parentModel; | ||
} | ||
|
||
public String getParentModel() | ||
{ | ||
return parentModel; | ||
} | ||
} |
10 changes: 0 additions & 10 deletions
10
src/main/java/pl/pabilo8/modworks/annotations/model/GeneratedItemModel.java
This file was deleted.
Oops, something went wrong.
95 changes: 95 additions & 0 deletions
95
src/main/java/pl/pabilo8/modworks/processors/ItemModelProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package pl.pabilo8.modworks.processors; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.gson.stream.JsonWriter; | ||
import pl.pabilo8.modworks.annotations.item.GeneratedItemModels; | ||
import pl.pabilo8.modworks.annotations.item.GeneratedSubItemModel; | ||
import pl.pabilo8.modworks.annotations.item.ItemModelType; | ||
import pl.pabilo8.modworks.utils.GeneralUtils; | ||
|
||
import javax.annotation.processing.Processor; | ||
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.TypeElement; | ||
import javax.tools.Diagnostic.Kind; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@SupportedAnnotationTypes({ | ||
"pl.pabilo8.modworks.annotations.item.GeneratedItemModels", | ||
"pl.pabilo8.modworks.annotations.item.GeneratedSubItemModel" | ||
}) | ||
@SupportedSourceVersion(SourceVersion.RELEASE_8) | ||
@AutoService(Processor.class) | ||
public class ItemModelProcessor extends AbstractModProcessor | ||
{ | ||
@Override | ||
protected boolean doProcessing(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) | ||
{ | ||
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(GeneratedItemModels.class); | ||
for(Element element : elements) | ||
{ | ||
GeneratedItemModels itemModel = element.getAnnotation(GeneratedItemModels.class); | ||
List<Element> enumValues = GeneralUtils.getEnumValues(element); | ||
|
||
//Model properties | ||
String fileName = itemModel.itemName(); | ||
String texturePath = itemModel.texturePath().isEmpty()?fileName: itemModel.itemName(); | ||
|
||
if(!enumValues.isEmpty()) //is an enum with multiple models | ||
for(Element value : enumValues) | ||
{ | ||
GeneratedSubItemModel subModel = value.getAnnotation(GeneratedSubItemModel.class); | ||
if(itemModel.onlyInAnnotated()&&subModel==null) | ||
continue; | ||
|
||
String subName = fileName+"/"+GeneralUtils.simpleNameOf(value); | ||
String subTexture = subName; | ||
|
||
if(subModel!=null&&!subModel.customTexturePath().isEmpty()) | ||
subTexture = subModel.customTexturePath(); | ||
|
||
tryWriteModel(subName, subTexture, itemModel.type()); | ||
|
||
} | ||
else //is a class with a single model | ||
tryWriteModel(fileName, texturePath, itemModel.type()); | ||
} | ||
|
||
|
||
return !elements.isEmpty(); | ||
} | ||
|
||
void tryWriteModel(String modelName, String texturePath, ItemModelType type) | ||
{ | ||
try(JsonWriter writer = GeneralUtils.writeJSON(processingEnv, String.format("%s/assets/%s/models/item/%s.json", DIR_RESOURCES, MODID, modelName))) | ||
{ | ||
writer.beginObject(); | ||
writer.name("parent").value(type.getParentModel()); | ||
|
||
//Add textures | ||
writer.name("textures").beginObject(); | ||
switch(type) | ||
{ | ||
case ITEM_SIMPLE: | ||
case ITEM_SIMPLE_TOOL: | ||
writer.name("layer0").value(String.format("%s:items/%s", MODID, texturePath)); | ||
break; | ||
case ITEM_SIMPLE_AUTOREPLACED: | ||
break; | ||
} | ||
writer.endObject(); | ||
|
||
writer.endObject(); | ||
} catch(IOException e) | ||
{ | ||
processingEnv.getMessager().printMessage(Kind.NOTE, | ||
"Could not build a sound file for, "+modelName); | ||
processingEnv.getMessager().printMessage(Kind.NOTE, e.getLocalizedMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import com.google.testing.compile.JavaFileObjects; | ||
import org.junit.jupiter.api.Test; | ||
import pl.pabilo8.modworks.processors.ItemModelProcessor; | ||
|
||
import javax.tools.JavaFileObject; | ||
import javax.tools.StandardLocation; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import static com.google.common.truth.Truth.assertAbout; | ||
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource; | ||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
|
||
/** | ||
* @author Pabilo8 | ||
* @since 01.08.2023 | ||
*/ | ||
class ItemModelProcessorTest | ||
{ | ||
public static final JavaFileObject SOURCE = JavaFileObjects.forResource("test/ItemTestDevice.java"); | ||
public static final JavaFileObject MCMOD = JavaFileObjects.forResource("jsons/mcmod.info"); | ||
|
||
@Test | ||
void doProcessing() | ||
{ | ||
final CharSequence[] text = new CharSequence[1]; | ||
assertDoesNotThrow(() -> text[0] = MCMOD.getCharContent(true)); | ||
|
||
assertAbout(javaSource()) | ||
.that(SOURCE) | ||
.withCompilerOptions("-Amodworks.modid=testmod") | ||
.withCompilerOptions("-Amodworks.mcmod=true") | ||
.processedWith(new ItemModelProcessor()) | ||
.compilesWithoutError() | ||
.and() | ||
.generatesFileNamed(StandardLocation.SOURCE_OUTPUT, "resources/assets/testmod/models/item/test_device", "test1.json") | ||
.and() | ||
.generatesFileNamed(StandardLocation.SOURCE_OUTPUT, "resources/assets/testmod/models/item/test_device", "test2.json") | ||
.and() | ||
.generatesFileNamed(StandardLocation.SOURCE_OUTPUT, "resources/assets/testmod/models/item/test_device", "test3.json"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package test; | ||
|
||
import pl.pabilo8.modworks.annotations.item.GeneratedItemModels; | ||
import pl.pabilo8.modworks.annotations.item.GeneratedSubItemModel; | ||
import pl.pabilo8.modworks.annotations.item.ItemModelType; | ||
import pl.pabilo8.modworks.annotations.sound.ModSound; | ||
import pl.pabilo8.modworks.annotations.MCModInfo; | ||
|
||
/** | ||
* @author Pabilo8 | ||
* @since 01.08.2023 | ||
*/ | ||
public class ItemTestDevice | ||
{ | ||
|
||
@GeneratedItemModels(itemName = "test_device") | ||
public enum Devices | ||
{ | ||
TEST1, | ||
@GeneratedSubItemModel(customTexturePath = "nucleardevice") | ||
TEST2, | ||
TEST3; | ||
} | ||
} |