Added classes for custom model entity, its model and renderer

This commit is contained in:
Goodbird committed 2024-01-24 11:12:51 +03:00
1 parent 49fb0541d6
commit 151943c138
3 files changed
+212

No files matched your search

@@ -0,0 +1,22 @@
package com.goodbird.cnpcgeckoaddon.client.model;
import com.goodbird.cnpcgeckoaddon.entity.EntityCustomModel;
import net.minecraft.util.ResourceLocation;
import software.bernie.geckolib3.model.AnimatedGeoModel;
public class ModelCustom extends AnimatedGeoModel<EntityCustomModel> {
@Override
public ResourceLocation getAnimationFileLocation(EntityCustomModel entity) {
return entity.animResLoc;
}
@Override
public ResourceLocation getModelLocation(EntityCustomModel entity) {
return entity.modelResLoc;
}
@Override
public ResourceLocation getTextureLocation(EntityCustomModel entity) {
return entity.textureResLoc;
}
}
@@ -0,0 +1,88 @@
package com.goodbird.cnpcgeckoaddon.client.renderer;
import com.goodbird.cnpcgeckoaddon.client.model.ModelCustom;
import com.goodbird.cnpcgeckoaddon.entity.EntityCustomModel;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.IItemRenderer;
import org.lwjgl.opengl.GL11;
import software.bernie.geckolib3.geo.render.built.GeoBone;
import software.bernie.geckolib3.geo.render.built.GeoModel;
import software.bernie.geckolib3.renderers.geo.GeoEntityRenderer;
import java.util.ArrayList;
public class RenderCustomModel extends GeoEntityRenderer<EntityCustomModel> {
public RenderCustomModel() {
super(new ModelCustom());
}
public void renderAfter(GeoModel model, EntityCustomModel animatable, float ticks, float red, float green, float blue, float alpha) {
super.renderAfter(model, animatable, ticks, red, green, blue, alpha);
if (model.getBone("held_item").isPresent() && animatable.getHeldItem() != null) {
GeoBone bone = model.getBone("held_item").get();
this.renderItem(animatable, animatable.getHeldItem(), bone);
}
if (model.getBone("left_held_item").isPresent() && animatable.leftHeldItem != null) {
GeoBone bone = model.getBone("left_held_item").get();
this.renderLeftHandItem(animatable, animatable.leftHeldItem, bone);
}
}
public GeoBone[] getPathFromRoot(GeoBone bone) {
ArrayList<GeoBone> bones;
for(bones = new ArrayList<>(); bone != null; bone = bone.parent) {
bones.add(0, bone);
}
return bones.toArray(new GeoBone[0]);
}
public void renderItem(EntityCustomModel animatable, ItemStack stack, GeoBone locator) {
GL11.glPushMatrix();
float scale = 0.5F;
GL11.glScaled(scale, scale, scale);
GeoBone[] bonePath = this.getPathFromRoot(locator);
for (GeoBone b : bonePath) {
GL11.glTranslatef(b.getPositionX() / (16.0F * scale), b.getPositionY() / (16.0F * scale), b.getPositionZ() / (16.0F * scale));
GL11.glTranslatef(b.getPivotX() / (16.0F * scale), b.getPivotY() / (16.0F * scale), b.getPivotZ() / (16.0F * scale));
GL11.glRotated((double) b.getRotationZ() / Math.PI * 180.0, 0.0, 0.0, 1.0);
GL11.glRotated((double) b.getRotationY() / Math.PI * 180.0, 0.0, 1.0, 0.0);
GL11.glRotated((double) b.getRotationX() / Math.PI * 180.0, 1.0, 0.0, 0.0);
GL11.glScalef(b.getScaleX(), b.getScaleY(), b.getScaleZ());
GL11.glTranslatef(-b.getPivotX() / (16.0F * scale), -b.getPivotY() / (16.0F * scale), -b.getPivotZ() / (16.0F * scale));
}
GL11.glRotatef(250.0F, 1.0F, 0.0F, 0.0F);
GL11.glRotatef(40.0F, 0.0F, 1.0F, 0.0F);
GL11.glTranslatef(-0.4F, -0.55F, 1.7F);
RenderManager.instance.itemRenderer.renderItem(animatable, stack, 0, IItemRenderer.ItemRenderType.INVENTORY);
GL11.glPopMatrix();
}
public void renderLeftHandItem(EntityCustomModel animatable, ItemStack stack, GeoBone locator) {
GL11.glPushMatrix();
float scale = 0.5F;
GL11.glScaled(scale, scale, scale);
GeoBone[] bonePath = this.getPathFromRoot(locator);
for (GeoBone b : bonePath) {
GL11.glTranslatef(b.getPositionX() / (16.0F * scale), b.getPositionY() / (16.0F * scale), b.getPositionZ() / (16.0F * scale));
GL11.glTranslatef(b.getPivotX() / (16.0F * scale), b.getPivotY() / (16.0F * scale), b.getPivotZ() / (16.0F * scale));
GL11.glRotated((double) b.getRotationZ() / Math.PI * 180.0, 0.0, 0.0, 1.0);
GL11.glRotated((double) b.getRotationY() / Math.PI * 180.0, 0.0, 1.0, 0.0);
GL11.glRotated((double) b.getRotationX() / Math.PI * 180.0, 1.0, 0.0, 0.0);
GL11.glScalef(b.getScaleX(), b.getScaleY(), b.getScaleZ());
GL11.glTranslatef(-b.getPivotX() / (16.0F * scale), -b.getPivotY() / (16.0F * scale), -b.getPivotZ() / (16.0F * scale));
}
GL11.glRotatef(250.0F, 1.0F, 0.0F, 0.0F);
GL11.glRotatef(40.0F, 0.0F, 1.0F, 0.0F);
GL11.glTranslatef(-1.51F, -0.55F, 0.7F);
RenderManager.instance.itemRenderer.renderItem(animatable, stack, 0, IItemRenderer.ItemRenderType.INVENTORY);
GL11.glPopMatrix();
}
public boolean isBoneRenderOverriden(EntityCustomModel entity, GeoBone bone) {
return bone.name.equals("held_item") || bone.name.equals("left_held_item");
}
}
@@ -0,0 +1,102 @@
package com.goodbird.cnpcgeckoaddon.entity;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import software.bernie.geckolib3.core.AnimationState;
import software.bernie.geckolib3.core.IAnimatable;
import software.bernie.geckolib3.core.IAnimationTickable;
import software.bernie.geckolib3.core.PlayState;
import software.bernie.geckolib3.core.builder.AnimationBuilder;
import software.bernie.geckolib3.core.controller.AnimationController;
import software.bernie.geckolib3.core.event.predicate.AnimationEvent;
import software.bernie.geckolib3.core.manager.AnimationData;
import software.bernie.geckolib3.core.manager.AnimationFactory;
public class EntityCustomModel extends EntityCreature implements IAnimatable, IAnimationTickable {
private final AnimationFactory factory = new AnimationFactory(this);
public ResourceLocation modelResLoc = new ResourceLocation("geckolib3", "geo/npc.geo.json");
public ResourceLocation animResLoc = new ResourceLocation("custom", "geo_npc.animation.json");
public ResourceLocation textureResLoc = new ResourceLocation("geckolib3", "textures/model/entity/geo_npc.png");
public String idleAnim = "";
public String walkAnim = "";
public String hurtAnim = "";
public String attackAnim = "";
public AnimationBuilder dialogAnim = null;
public AnimationBuilder manualAnim = null;
public ItemStack leftHeldItem;
private <E extends IAnimatable> PlayState predicateMovement(AnimationEvent<E> event) {
if (manualAnim != null) {
if (event.getController().getAnimationState() == AnimationState.Stopped) {
manualAnim = null;
} else {
if (event.getController().currentAnimationBuilder != manualAnim) {
event.getController().markNeedsReload();
}
event.getController().setAnimation(manualAnim);
return PlayState.CONTINUE;
}
}
if (dialogAnim != null) {
if (event.getController().getAnimationState() == AnimationState.Stopped) {
dialogAnim = null;
} else {
if (event.getController().currentAnimationBuilder != dialogAnim) {
event.getController().markNeedsReload();
}
event.getController().setAnimation(dialogAnim);
return PlayState.CONTINUE;
}
}
if (!event.isMoving() || walkAnim.isEmpty()) {
if (!idleAnim.isEmpty()) {
event.getController().setAnimation(new AnimationBuilder().loop(idleAnim));
} else {
return PlayState.STOP;
}
} else {
event.getController().setAnimation(new AnimationBuilder().loop(walkAnim));
}
return PlayState.CONTINUE;
}
private <E extends IAnimatable> PlayState predicateAttack(AnimationEvent<E> event) {
return PlayState.CONTINUE;
}
public void setDialogAnim(String name) {
dialogAnim = new AnimationBuilder().playOnce(name);
}
public EntityCustomModel(World worldIn) {
super(worldIn);
this.ignoreFrustumCheck = true;
this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
this.setSize(0.7F, 2F);
}
@Override
public void registerControllers(AnimationData data) {
data.addAnimationController(new AnimationController<>(this, "movement", 10, this::predicateMovement));
data.addAnimationController(new AnimationController<>(this, "attack", 10, this::predicateAttack));
}
@Override
public AnimationFactory getFactory() {
return this.factory;
}
@Override
public int tickTimer() {
return ticksExisted;
}
@Override
public void tick() {
super.onUpdate();
}
}