Skip to content

Commit

Permalink
feat: generalize job models for client (#1074) - thanks @GaeaKat
Browse files Browse the repository at this point in the history
* feat: move job models to generic system

* fix: copy glowtexture in addition to normal texture for afrit

* fix: removed debug line and extraneous override

* refactor: move second generic into addition of first
  • Loading branch information
klikli-dev committed Mar 11, 2024
1 parent bac30e3 commit 28e43f2
Show file tree
Hide file tree
Showing 19 changed files with 1,124 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.klikli_dev.occultism.client.entities;

import com.klikli_dev.occultism.Occultism;
import net.minecraft.resources.ResourceLocation;


public class SpiritJobClient {
protected ResourceLocation modelID;

public SpiritJobClient(ResourceLocation modelID) {
this.modelID = modelID;
}

public ResourceLocation modelID() {
return modelID;
}

public static SpiritJobClient create(ResourceLocation modelID) {
return new SpiritJobClient(modelID);
}

public static SpiritJobClient create(String modelId) {
return create(new ResourceLocation(Occultism.MODID,modelId));
}

public static SpiritJobClient create() {
return create("worker");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,9 @@
import software.bernie.geckolib.model.DefaultedGeoModel;


public class AfritModel extends DefaultedEntityGeoModel<AfritEntity> {

public class AfritModel extends DefaultedJobEntityModel<AfritEntity> {
public final static String ASSET_SUBPATH = "afrit";
public AfritModel() {
super(new ResourceLocation(Occultism.MODID, "afrit"), true);
}

@Override
public RenderType getRenderType(AfritEntity animatable, ResourceLocation texture) {
return RenderType.entityTranslucent(this.getTextureResource(animatable));
super(new ResourceLocation(Occultism.MODID, "afrit"), true,ASSET_SUBPATH);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.klikli_dev.occultism.client.model.entity;

import com.klikli_dev.occultism.Occultism;
import com.klikli_dev.occultism.common.entity.job.SpiritJobFactory;
import com.klikli_dev.occultism.common.entity.spirit.DjinniEntity;
import com.klikli_dev.occultism.common.entity.spirit.SpiritEntity;
import com.klikli_dev.occultism.registry.OccultismSpiritJobs;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;
import software.bernie.geckolib.cache.GeckoLibCache;
import software.bernie.geckolib.core.animatable.GeoAnimatable;
import software.bernie.geckolib.model.DefaultedEntityGeoModel;

import java.util.HashMap;
import java.util.Map;

public abstract class DefaultedJobEntityModel<T extends SpiritEntity & GeoAnimatable> extends DefaultedEntityGeoModel<T> {
private final String entity_subpath;
protected final Map<String, ModelData> jobModels;
protected final ModelData worker;

public DefaultedJobEntityModel(ResourceLocation assetSubpath, boolean turnsHead, String entity_subpath) {
super(assetSubpath, turnsHead);
this.entity_subpath = entity_subpath;
jobModels = new HashMap<>();
this.worker = this.buildModelData("worker");
for(var job: OccultismSpiritJobs.JOBS.getEntries()) {
SpiritJobFactory factory = job.get();
jobModels.put(job.getId().toString(), this.buildModelData(factory.client().modelID(),"_"));
}
}

public ModelData getModelData(T animatable) {
var job = animatable.getJobID();
var model = jobModels.getOrDefault(job, this.worker);
if(!GeckoLibCache.getBakedModels().containsKey(model.model()))
model=this.worker;
return model;
}
public ModelData buildModelData(String job) {
return this.buildModelData(job, "_");
}

public ModelData buildModelData(ResourceLocation basePath) {
return new ModelData(
this.buildFormattedModelPath(basePath),
this.buildFormattedTexturePath(basePath),
this.buildFormattedAnimationPath(basePath)
);
}
public ModelData buildModelData(ResourceLocation location, String separator) {
return this.buildModelData(new ResourceLocation(location.getNamespace(), entity_subpath + separator + location.getPath()));
}
public ModelData buildModelData(String job, String separator) {
return this.buildModelData(new ResourceLocation(Occultism.MODID, job),separator);
}

public record ModelData(ResourceLocation model, ResourceLocation texture, ResourceLocation animation) {
}

@Override
public RenderType getRenderType(T animatable, ResourceLocation texture) {
return RenderType.entityTranslucent(this.getTextureResource(animatable));
}

@Override
public ResourceLocation getModelResource(T animatable) {
return this.getModelData(animatable).model();
}

@Override
public ResourceLocation getTextureResource(T animatable) {
return this.getModelData(animatable).texture();
}

@Override
public ResourceLocation getAnimationResource(T animatable) {
return this.getModelData(animatable).animation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,62 +33,12 @@
import java.util.Objects;


public class DjinniModel extends DefaultedEntityGeoModel<DjinniEntity> {
public class DjinniModel extends DefaultedJobEntityModel<DjinniEntity> {

public final static String ASSET_SUBPATH = "djinni";
public final ModelData worker;
public final ModelData machineManager;

public DjinniModel() {
super(new ResourceLocation(Occultism.MODID, ASSET_SUBPATH), false);

this.worker = this.buildModelData("worker");
this.machineManager = this.buildModelData("machine_manager");
}

public ModelData getModelData(DjinniEntity animatable) {
var job = animatable.getJobID();

if (Objects.equals(job, OccultismSpiritJobs.MANAGE_MACHINE.getId().toString())) {
return this.machineManager;
}

return this.worker;
}

@Override
public RenderType getRenderType(DjinniEntity animatable, ResourceLocation texture) {
return RenderType.entityTranslucent(this.getTextureResource(animatable));
}

@Override
public ResourceLocation getModelResource(DjinniEntity animatable) {
return this.getModelData(animatable).model();
}

@Override
public ResourceLocation getTextureResource(DjinniEntity animatable) {
return this.getModelData(animatable).texture();
}

@Override
public ResourceLocation getAnimationResource(DjinniEntity animatable) {
return this.getModelData(animatable).animation();
}

public ModelData buildModelData(String job) {
return this.buildModelData(job, "_");
}

public ModelData buildModelData(String job, String separator) {
return new ModelData(
this.buildFormattedModelPath(new ResourceLocation(Occultism.MODID, ASSET_SUBPATH + separator + job)),
this.buildFormattedTexturePath(new ResourceLocation(Occultism.MODID, ASSET_SUBPATH + separator + job)),
this.buildFormattedAnimationPath(new ResourceLocation(Occultism.MODID, ASSET_SUBPATH + separator + job))
);
}

public record ModelData(ResourceLocation model, ResourceLocation texture, ResourceLocation animation) {
super(new ResourceLocation(Occultism.MODID, ASSET_SUBPATH), false,ASSET_SUBPATH);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,90 +23,23 @@
package com.klikli_dev.occultism.client.model.entity;

import com.klikli_dev.occultism.Occultism;
import com.klikli_dev.occultism.common.entity.job.SpiritJobFactory;
import com.klikli_dev.occultism.common.entity.spirit.FoliotEntity;
import com.klikli_dev.occultism.registry.OccultismSpiritJobs;
import net.minecraft.resources.ResourceLocation;
import software.bernie.geckolib.model.DefaultedEntityGeoModel;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


public class FoliotModel extends DefaultedEntityGeoModel<FoliotEntity> {
public class FoliotModel extends DefaultedJobEntityModel<FoliotEntity> {

public final static String ASSET_SUBPATH = "foliot";

public final ModelData crusher;
public final ModelData transporter;
public final ModelData janitor;
public final ModelData lumberjack;
public final ModelData saplingTrader;
public final ModelData otherstoneTrader;

public FoliotModel() {
super(new ResourceLocation(Occultism.MODID, ASSET_SUBPATH), false);

this.crusher = this.buildModelData("crusher");
this.transporter = this.buildModelData("transporter");
this.janitor = this.buildModelData("janitor");
this.lumberjack = this.buildModelData("lumberjack");
this.otherstoneTrader = this.buildModelData("otherstone_trader");
this.saplingTrader = this.buildModelData("sapling_trader");
}

public ModelData getModelData(FoliotEntity animatable) {
var job = animatable.getJobID();

if (Objects.equals(job, OccultismSpiritJobs.TRANSPORT_ITEMS.getId().toString())) {
return this.transporter;
}

if (Objects.equals(job, OccultismSpiritJobs.CLEANER.getId().toString())) {
return this.janitor;
}

if (Objects.equals(job, OccultismSpiritJobs.LUMBERJACK.getId().toString())) {
return this.lumberjack;
}

if (Objects.equals(job, OccultismSpiritJobs.TRADE_OTHERSTONE_T1.getId().toString())) {
return this.otherstoneTrader;
}

if (Objects.equals(job, OccultismSpiritJobs.TRADE_OTHERWORLD_SAPLINGS_T2.getId().toString())) {
return this.saplingTrader;
}

return this.crusher;
}

@Override
public ResourceLocation getModelResource(FoliotEntity animatable) {
return this.getModelData(animatable).model();
}

@Override
public ResourceLocation getTextureResource(FoliotEntity animatable) {
return this.getModelData(animatable).texture();
}

@Override
public ResourceLocation getAnimationResource(FoliotEntity animatable) {
return this.getModelData(animatable).animation();
}

public ModelData buildModelData(String job) {
return this.buildModelData(job, "_");
}

public ModelData buildModelData(String job, String separator) {
return new ModelData(
this.buildFormattedModelPath(new ResourceLocation(Occultism.MODID, ASSET_SUBPATH + separator + job)),
this.buildFormattedTexturePath(new ResourceLocation(Occultism.MODID, ASSET_SUBPATH + separator + job)),
this.buildFormattedAnimationPath(new ResourceLocation(Occultism.MODID, ASSET_SUBPATH + separator + job))
);
}

public record ModelData(ResourceLocation model, ResourceLocation texture, ResourceLocation animation) {
super(new ResourceLocation(Occultism.MODID, ASSET_SUBPATH), false,ASSET_SUBPATH);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@
import software.bernie.geckolib.model.DefaultedGeoModel;


public class MaridModel extends DefaultedEntityGeoModel<MaridEntity> {

public class MaridModel extends DefaultedJobEntityModel<MaridEntity> {
public final static String ASSET_SUBPATH = "marid";
public MaridModel() {
super(new ResourceLocation(Occultism.MODID, "marid"), true);
}

@Override
public RenderType getRenderType(MaridEntity animatable, ResourceLocation texture) {
return RenderType.entityTranslucent(this.getTextureResource(animatable));
super(new ResourceLocation(Occultism.MODID, "marid"), true,ASSET_SUBPATH);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@

package com.klikli_dev.occultism.common.entity.job;

import com.klikli_dev.occultism.client.entities.SpiritJobClient;
import com.klikli_dev.occultism.common.entity.spirit.SpiritEntity;
import com.klikli_dev.occultism.registry.OccultismSpiritJobs;
import net.minecraft.resources.ResourceLocation;

import java.util.function.Function;

public class SpiritJobFactory {

Function<SpiritEntity, ? extends SpiritJob> constructor;
SpiritJobClient client;

public SpiritJobFactory(Function<SpiritEntity, ? extends SpiritJob> constructor) {
public SpiritJobFactory(Function<SpiritEntity, ? extends SpiritJob> constructor, SpiritJobClient client) {
this.constructor = constructor;
this.client = client;
}

public SpiritJob create(SpiritEntity entity) {
Expand All @@ -41,4 +45,8 @@ public SpiritJob create(SpiritEntity entity) {
return job;
}

public SpiritJobClient client() {
return this.client;
}

}
Loading

0 comments on commit 28e43f2

Please sign in to comment.