Skip to content

Commit

Permalink
Update version to 4.0
Browse files Browse the repository at this point in the history
Add config file system
  • Loading branch information
dorianpb committed Jul 1, 2021
1 parent 613d78c commit 9140507
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 59 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ This mod is currently in ***ALPHA***, meaning that things *MAY* or *MAY NOT* wor

Check out the [CurseForge page for downloads](https://www.curseforge.com/minecraft/mc-mods/custom-entity-models-cem "CurseForge Page"), or build it yourself. This mod *DOES*
require the installation of the [Fabric Loader](https://fabricmc.net/use/ "Fabric Loader") and
the [Fabric API](https://www.curseforge.com/minecraft/mc-mods/fabric-api "Fabric API").
the [Fabric API](https://www.curseforge.com/minecraft/mc-mods/fabric-api "Fabric API"). If you want to configure this mod, the installation
of [completeconfig](https://www.curseforge.com/minecraft/mc-mods/completeconfig "completeconfig")
and [Cloth Config API](https://www.curseforge.com/minecraft/mc-mods/cloth-config "cloth api")
are also required.

## How to use

Expand Down
16 changes: 16 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//file:noinspection GroovyAssignabilityCheck
//file:noinspection GroovyAccessibility
plugins {
id 'fabric-loom' version "${loom_version}"
id 'maven-publish'
Expand All @@ -11,13 +13,27 @@ version = project.mod_version
group = project.maven_group

repositories {
maven {
name = 'TerraformersMC'
url = 'https://maven.terraformersmc.com/releases'
}
maven {
name = 'Shedaniel'
url "https://maven.shedaniel.me/"
}
maven {
name = 'JitPack'
url 'https://jitpack.io'
}

}

dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}+${project.minecraft_version}"
modImplementation "com.gitlab.Lortseam:completeconfig:${project.complete_config_version}"
}

loom {
Expand Down
17 changes: 9 additions & 8 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version = 1.17
yarn_mappings = 1.17+build.13
loader_version = 0.11.6
fabric_version = 0.36.0
loom_version = 0.8-SNAPSHOT
minecraft_version = 1.17
yarn_mappings = 1.17+build.13
loader_version = 0.11.6
fabric_version = 0.36.0
loom_version = 0.8-SNAPSHOT
complete_config_version = 1.0.0
# Mod Properties
mod_version = 0.3.3
maven_group = net.dorianpb
archives_base_name = cem
mod_version = 0.4.0
maven_group = net.dorianpb
archives_base_name = cem
2 changes: 2 additions & 0 deletions src/main/java/net/dorianpb/cem/internal/CemInit.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.dorianpb.cem.internal;

import net.dorianpb.cem.internal.api.CemEntityInitializer;
import net.dorianpb.cem.internal.config.CemConfigFairy;
import net.dorianpb.cem.internal.util.CemFairy;
import net.dorianpb.cem.mixins.BlockEntityRendererAccessor;
import net.dorianpb.cem.mixins.EntityRendererAccessor;
Expand All @@ -13,6 +14,7 @@ public class CemInit implements ClientModInitializer{
@Override
@SuppressWarnings({"unchecked", "RedundantSuppression"})
public void onInitializeClient(){
CemConfigFairy.loadConfig();
FabricLoader.getInstance().getEntrypointContainers("cem", CemEntityInitializer.class).forEach((container) -> {
ModMetadata provider = container.getProvider().getMetadata();
CemEntityInitializer entrypoint = container.getEntrypoint();
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/net/dorianpb/cem/internal/config/CemConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.dorianpb.cem.internal.config;

import me.lortseam.completeconfig.api.ConfigContainer;
import me.lortseam.completeconfig.api.ConfigEntry;
import me.lortseam.completeconfig.data.Config;
import me.lortseam.completeconfig.gui.ConfigScreenBuilder;
import me.lortseam.completeconfig.gui.cloth.ClothConfigScreenBuilder;
import net.fabricmc.loader.api.FabricLoader;

@SuppressWarnings({"FieldMayBeFinal"})
public final class CemConfig extends Config implements ConfigContainer, CemOptions{
@ConfigEntry
private boolean use_optifine_folder = false;
@ConfigEntry
private boolean use_new_model_creation_fix = true;

private CemConfig(){
super("cem");
}

public static CemConfig getConfig(){
CemConfig hello = new CemConfig();
hello.load();
return hello;
}

public static void createScreen(){
if(FabricLoader.getInstance().isModLoaded("cloth-config2")){
ConfigScreenBuilder.setMain("cem", new ClothConfigScreenBuilder());
}
}

@Override
public boolean useOptifineFolder(){
return this.use_optifine_folder;
}

@Override
public boolean useTransparentParts(){
return this.use_new_model_creation_fix;
}
}
24 changes: 24 additions & 0 deletions src/main/java/net/dorianpb/cem/internal/config/CemConfigFairy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.dorianpb.cem.internal.config;

import net.dorianpb.cem.internal.util.CemFairy;
import net.fabricmc.loader.api.FabricLoader;

public class CemConfigFairy{
private static CemOptions config;

public static void loadConfig(){
if(FabricLoader.getInstance().isModLoaded("completeconfig")){
config = CemConfig.getConfig();
CemConfig.createScreen();
}
else{
CemFairy.getLogger().warn("Unable to set up config due to missing dependencies; using defaults!");
config = new CemOptions(){
};
}
}

public static CemOptions getConfig(){
return config;
}
}
13 changes: 13 additions & 0 deletions src/main/java/net/dorianpb/cem/internal/config/CemOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.dorianpb.cem.internal.config;

@SuppressWarnings("unused")
public interface CemOptions{

default boolean useOptifineFolder(){
return false;
}

default boolean useTransparentParts(){
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.collect.HashBiMap;
import net.dorianpb.cem.internal.CemStringParser;
import net.dorianpb.cem.internal.CemStringParser.ParsedExpression;
import net.dorianpb.cem.internal.config.CemConfigFairy;
import net.dorianpb.cem.internal.file.JemFile;
import net.dorianpb.cem.internal.file.JemFile.JemModel;
import net.dorianpb.cem.internal.models.CemModelEntry.CemModelPart;
Expand Down Expand Up @@ -71,11 +72,14 @@ public CemModelPart prepRootPart(Map<String, String> partNameMap, ModelPart vani
if(inflate != null){
part.inflate(inflate);
}
for(String key : part.children.keySet()){
try{
TransparentCemModelPart replacement = new TransparentCemModelPart(part.getChild(key), vanillaModel.getChild(key).getTransform());
part.addChild(key, replacement);
} catch(Exception ignored){
//new model creation fix!
if(CemConfigFairy.getConfig().useTransparentParts()){
for(String key : part.children.keySet()){
try{
TransparentCemModelPart replacement = new TransparentCemModelPart(part.getChild(key), vanillaModel.getChild(key).getTransform());
part.addChild(key, replacement);
} catch(Exception ignored){
}
}
}
return part;
Expand Down
96 changes: 51 additions & 45 deletions src/main/java/net/dorianpb/cem/mixins/EntityModelLoaderMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.google.gson.internal.LinkedTreeMap;
import net.dorianpb.cem.internal.config.CemConfigFairy;
import net.dorianpb.cem.internal.file.JemFile;
import net.dorianpb.cem.internal.util.CemFairy;
import net.dorianpb.cem.internal.util.CemRegistryManager;
Expand All @@ -28,58 +29,63 @@ public abstract class EntityModelLoaderMixin{
@Inject(method = "reload", at = @At(value = "HEAD"))
private void injectReload(ResourceManager manager, CallbackInfo ci){
CemRegistryManager.clearRegistries();
for(Identifier id : manager.findResources("cem", path -> path.endsWith(".jem"))){
if(!id.getNamespace().equals("dorianpb")){
continue;
manager.findResources("cem", path -> path.endsWith(".jem")).forEach((id -> loadResourceFromId(manager, id, "dorianpb")));
if(CemConfigFairy.getConfig().useOptifineFolder()){
manager.findResources("optifine/cem", path -> path.endsWith(".jem")).forEach((id -> loadResourceFromId(manager, id, "minecraft")));
}
}

private void loadResourceFromId(ResourceManager manager, Identifier id, String namespace){
if(!id.getNamespace().equals(namespace)){
return;
}
CemFairy.getLogger().info(id.toString());
try(InputStream stream = manager.getResource(id).getInputStream()){
//initialize the file
@SuppressWarnings("unchecked")
LinkedTreeMap<String, Object> json = CemFairy.getGson().fromJson(new InputStreamReader(stream, StandardCharsets.UTF_8), LinkedTreeMap.class);
if(json == null){
throw new Exception("Invalid File");
}
CemFairy.getLogger().info(id.toString());
try(InputStream stream = manager.getResource(id).getInputStream()){
//initialize the file
@SuppressWarnings("unchecked")
LinkedTreeMap<String, Object> json = CemFairy.getGson().fromJson(new InputStreamReader(stream, StandardCharsets.UTF_8), LinkedTreeMap.class);
if(json == null){
throw new Exception("Invalid File");
JemFile file = new JemFile(json, id, manager);

String entityName = CemFairy.getEntityNameFromId(id);
Optional<EntityType<?>> entityTypeOptional = EntityType.get(entityName);
Optional<BlockEntityType<?>> blockEntityTypeOptional = Registry.BLOCK_ENTITY_TYPE.getOrEmpty(Identifier.tryParse(entityName));

if(entityTypeOptional.isPresent()){
EntityType<? extends Entity> entityType = entityTypeOptional.get();
if(CemFairy.isUnsupported(entityType)){
throw new Exception("Entity \"" + EntityType.getId(entityType) + "\" is unsupported!");
}
JemFile file = new JemFile(json, id, manager);

String entityName = CemFairy.getEntityNameFromId(id);
Optional<EntityType<?>> entityTypeOptional = EntityType.get(entityName);
Optional<BlockEntityType<?>> blockEntityTypeOptional = Registry.BLOCK_ENTITY_TYPE.getOrEmpty(Identifier.tryParse(entityName));

if(entityTypeOptional.isPresent()){
EntityType<? extends Entity> entityType = entityTypeOptional.get();
if(CemFairy.isUnsupported(entityType)){
throw new Exception("Entity \"" + EntityType.getId(entityType) + "\" is unsupported!");
}
CemRegistryManager.addRegistry(entityType, file);
CemRegistryManager.addRegistry(entityType, file);
}

else if(blockEntityTypeOptional.isPresent()){
BlockEntityType<? extends BlockEntity> entityType = blockEntityTypeOptional.get();
if(CemFairy.isUnsupported(entityType)){
throw new Exception("Block Entity \"" + BlockEntityType.getId(entityType) + "\" is unsupported!");
}

else if(blockEntityTypeOptional.isPresent()){
BlockEntityType<? extends BlockEntity> entityType = blockEntityTypeOptional.get();
if(CemFairy.isUnsupported(entityType)){
throw new Exception("Block Entity \"" + BlockEntityType.getId(entityType) + "\" is unsupported!");
}
CemRegistryManager.addRegistry(entityType, file);
CemRegistryManager.addRegistry(entityType, file);
}

else{
if(CemFairy.isUnsupported(entityName)){
throw new Exception("Unknown object \"" + entityName + "\"!");
}

else{
if(CemFairy.isUnsupported(entityName)){
throw new Exception("Unknown object \"" + entityName + "\"!");
}
else{
CemRegistryManager.addRegistry(entityName, file);
}
}
} catch(Exception exception){
CemFairy.getLogger().error("Error parsing " + id + ":");
String message = exception.getMessage();
CemFairy.getLogger().error(exception);
if(message == null || message.trim().equals("")){
CemFairy.getLogger().error(exception.getStackTrace()[0]);
CemFairy.getLogger().error(exception.getStackTrace()[1]);
CemFairy.getLogger().error(exception.getStackTrace()[2]);
CemRegistryManager.addRegistry(entityName, file);
}
}
} catch(Exception exception){
CemFairy.getLogger().error("Error parsing " + id + ":");
String message = exception.getMessage();
CemFairy.getLogger().error(exception);
if(message == null || message.trim().equals("")){
CemFairy.getLogger().error(exception.getStackTrace()[0]);
CemFairy.getLogger().error(exception.getStackTrace()[1]);
CemFairy.getLogger().error(exception.getStackTrace()[2]);
}
}
}
}
16 changes: 16 additions & 0 deletions src/main/resources/assets/cem/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"config.cem.use_optifine_folder": "Use \"assets/minecraft/optifine/cem\"?",
"config.cem.use_optifine_folder.tooltip.0": "This will load CEM models from ",
"config.cem.use_optifine_folder.tooltip.1": "assets/minecraft/optifine/cem,",
"config.cem.use_optifine_folder.tooltip.2": "which is exactly what optifine does.",
"config.cem.use_optifine_folder.tooltip.3": "Reload resources (F3 + T) for",
"config.cem.use_optifine_folder.tooltip.4": "this to take effect!",
"config.cem.use_new_model_creation_fix": "Use model creation fix?",
"config.cem.use_new_model_creation_fix.tooltip.0": "This prevents minecraft from",
"config.cem.use_new_model_creation_fix.tooltip.1": "directly moving your model parts",
"config.cem.use_new_model_creation_fix.tooltip.2": "around when animating. This breaks",
"config.cem.use_new_model_creation_fix.tooltip.3": "a few models but fixes other ones.",
"config.cem.use_new_model_creation_fix.tooltip.4": "Please use if possible.",
"config.cem.use_new_model_creation_fix.tooltip.5": "Reload resources (F3 + T) for",
"config.cem.use_new_model_creation_fix.tooltip.6": "this to take effect!"
}
4 changes: 4 additions & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"fabric": "*",
"minecraft": ">=1.17"
},
"recommends": {
"completeconfig": "*",
"cloth-config2": "*"
},
"conflicts": {
"optifabric": "*"
}
Expand Down

0 comments on commit 9140507

Please sign in to comment.