Skip to content

Commit

Permalink
customEntity
Browse files Browse the repository at this point in the history
BeiShanair committed Feb 19, 2024
1 parent 82723f5 commit 5e26cba
Showing 9 changed files with 182 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/com/besson/tutorialmod/TutorialMod.java
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

import com.besson.tutorialmod.block.ModBlocks;
import com.besson.tutorialmod.block.entity.ModBlockEntities;
import com.besson.tutorialmod.entity.ModEntities;
import com.besson.tutorialmod.entity.custom.TigerEntity;
import com.besson.tutorialmod.item.ModItemGroup;
import com.besson.tutorialmod.item.ModItems;
import com.besson.tutorialmod.recipe.ModRecipes;
@@ -11,6 +13,7 @@
import com.besson.tutorialmod.util.ModTrades;
import com.besson.tutorialmod.villager.ModVillagers;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;
import net.fabricmc.fabric.api.registry.FuelRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -38,5 +41,7 @@ public void onInitialize() {
ModRecipes.registerRecipes();

FuelRegistry.INSTANCE.add(ModItems.ANTHRACITE,2000);

FabricDefaultAttributeRegistry.register(ModEntities.TIGER, TigerEntity.createTigerAttributes());
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/besson/tutorialmod/TutorialModClient.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.besson.tutorialmod;

import com.besson.tutorialmod.block.ModBlocks;
import com.besson.tutorialmod.entity.ModEntities;
import com.besson.tutorialmod.entity.client.ModModelLayers;
import com.besson.tutorialmod.entity.client.TigerModel;
import com.besson.tutorialmod.entity.client.TigerRenderer;
import com.besson.tutorialmod.screen.ModScreenHandlers;
import com.besson.tutorialmod.screen.PolishingMachineScreen;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.rendering.v1.EntityModelLayerRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry;
import net.minecraft.client.gui.screen.ingame.HandledScreens;
import net.minecraft.client.render.RenderLayer;

@@ -17,5 +23,8 @@ public void onInitializeClient() {
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.CORN_CROP,RenderLayer.getCutout());

HandledScreens.register(ModScreenHandlers.POLISHING_MACHINE_SCREEN_HANDLER, PolishingMachineScreen::new);

EntityModelLayerRegistry.registerModelLayer(ModModelLayers.TIGER, TigerModel::getTexturedModelData);
EntityRendererRegistry.register(ModEntities.TIGER, TigerRenderer::new);
}
}
Original file line number Diff line number Diff line change
@@ -122,11 +122,11 @@ private void resetProgress() {
}

private void craftItem() {
this.removeStack(INPUT_SLOT,1);
Optional<PolishingMachineRecipe> recipe = getCurrentRecipe();

this.setStack(OUTPUT_SLOT,new ItemStack(recipe.get().getOutput(null).getItem(),
getStack(OUTPUT_SLOT).getCount() + recipe.get().getOutput(null).getCount()));
this.removeStack(INPUT_SLOT,1);
}

private boolean hasCraftingFinished() {
18 changes: 18 additions & 0 deletions src/main/java/com/besson/tutorialmod/entity/ModEntities.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.besson.tutorialmod.entity;

import com.besson.tutorialmod.TutorialMod;
import com.besson.tutorialmod.entity.custom.TigerEntity;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;

public class ModEntities {
public static final EntityType<TigerEntity> TIGER = Registry.register(Registries.ENTITY_TYPE,
new Identifier(TutorialMod.MOD_ID,"tiger"),
FabricEntityTypeBuilder.create(SpawnGroup.CREATURE,TigerEntity::new)
.dimensions(EntityDimensions.fixed(1f,1f)).build());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.besson.tutorialmod.entity.client;

import com.besson.tutorialmod.TutorialMod;
import net.minecraft.client.render.entity.model.EntityModelLayer;
import net.minecraft.util.Identifier;

public class ModModelLayers {
public static final EntityModelLayer TIGER =
new EntityModelLayer(new Identifier(TutorialMod.MOD_ID,"tiger"),"main");
}
51 changes: 51 additions & 0 deletions src/main/java/com/besson/tutorialmod/entity/client/TigerModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.besson.tutorialmod.entity.client;

import com.besson.tutorialmod.entity.custom.TigerEntity;
import net.minecraft.client.model.*;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.entity.model.SinglePartEntityModel;
import net.minecraft.client.util.math.MatrixStack;

// Made with Blockbench 4.9.3
// Exported for Minecraft version 1.17+ for Yarn
// Paste this class into your mod and generate all required imports
public class TigerModel<T extends TigerEntity> extends SinglePartEntityModel<T> {
private final ModelPart tiger;
public TigerModel(ModelPart root) {
this.tiger = root.getChild("tiger");
}
public static TexturedModelData getTexturedModelData() {
ModelData modelData = new ModelData();
ModelPartData modelPartData = modelData.getRoot();
ModelPartData tiger = modelPartData.addChild("tiger", ModelPartBuilder.create(), ModelTransform.pivot(0.0F, 24.0F, 0.0F));

ModelPartData right_back_leg = tiger.addChild("right_back_leg", ModelPartBuilder.create().uv(0, 0).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 4.0F, 2.0F, new Dilation(0.0F)), ModelTransform.pivot(-3.0F, -4.0F, 4.0F));

ModelPartData tail = tiger.addChild("tail", ModelPartBuilder.create(), ModelTransform.pivot(0.0F, -7.2914F, 4.9639F));

ModelPartData cube_r1 = tail.addChild("cube_r1", ModelPartBuilder.create().uv(8, 0).cuboid(-0.5F, -4.0F, -3.9F, 1.0F, 6.0F, 1.0F, new Dilation(0.0F)), ModelTransform.of(0.0F, 1.2343F, 3.9312F, -0.7418F, 0.0F, 0.0F));

ModelPartData left_front_leg = tiger.addChild("left_front_leg", ModelPartBuilder.create().uv(22, 21).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 4.0F, 2.0F, new Dilation(0.0F)), ModelTransform.pivot(3.0F, -4.0F, -4.0F));

ModelPartData body = tiger.addChild("body", ModelPartBuilder.create().uv(0, 0).cuboid(-3.0F, -2.5F, -6.0F, 6.0F, 5.0F, 12.0F, new Dilation(0.0F)), ModelTransform.pivot(0.0F, -5.5F, 0.0F));

ModelPartData left_back_leg = tiger.addChild("left_back_leg", ModelPartBuilder.create().uv(16, 17).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 4.0F, 2.0F, new Dilation(0.0F)), ModelTransform.pivot(3.0F, -4.0F, 4.0F));

ModelPartData right_front_leg = tiger.addChild("right_front_leg", ModelPartBuilder.create().uv(0, 6).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 4.0F, 2.0F, new Dilation(0.0F)), ModelTransform.pivot(-3.0F, -4.0F, -4.0F));

ModelPartData head = tiger.addChild("head", ModelPartBuilder.create().uv(0, 17).cuboid(-2.0F, -2.0F, -2.0F, 4.0F, 4.0F, 4.0F, new Dilation(0.0F)), ModelTransform.pivot(0.0F, -9.0F, -7.0F));
return TexturedModelData.of(modelData, 64, 64);
}
@Override
public void setAngles(TigerEntity entity, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) {
}
@Override
public void render(MatrixStack matrices, VertexConsumer vertexConsumer, int light, int overlay, float red, float green, float blue, float alpha) {
tiger.render(matrices, vertexConsumer, light, overlay, red, green, blue, alpha);
}

@Override
public ModelPart getPart() {
return this.tiger;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.besson.tutorialmod.entity.client;

import com.besson.tutorialmod.TutorialMod;
import com.besson.tutorialmod.entity.custom.TigerEntity;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.EntityRendererFactory;
import net.minecraft.client.render.entity.MobEntityRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;

public class TigerRenderer extends MobEntityRenderer<TigerEntity,TigerModel<TigerEntity>> {
private static final Identifier TEXTURE = new Identifier(TutorialMod.MOD_ID,"textures/entity/tiger.png");
public TigerRenderer(EntityRendererFactory.Context context) {
super(context, new TigerModel<>(context.getPart(ModModelLayers.TIGER)),0.5f);
}

@Override
public Identifier getTexture(TigerEntity entity) {
return TEXTURE;
}

@Override
public void render(TigerEntity mobEntity, float f, float g, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i) {
if (mobEntity.isBaby()){
matrixStack.scale(0.5f,0.5f,0.5f);
}else {
matrixStack.scale(1.5f,1.5f,1.5f);
}
super.render(mobEntity, f, g, matrixStack, vertexConsumerProvider, i);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.besson.tutorialmod.entity.custom;

import com.besson.tutorialmod.entity.ModEntities;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ai.goal.*;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.passive.AnimalEntity;
import net.minecraft.entity.passive.PassiveEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.recipe.Ingredient;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class TigerEntity extends AnimalEntity {
public TigerEntity(EntityType<? extends AnimalEntity> entityType, World world) {
super(entityType, world);
}

@Override
public void tick() {
super.tick();
}

@Override
protected void initGoals() {
this.goalSelector.add(0,new SwimGoal(this));
this.goalSelector.add(1,new AnimalMateGoal(this,1.0D));
this.goalSelector.add(2,new TemptGoal(this,1.25D, Ingredient.ofItems(Items.BEEF),false));
this.goalSelector.add(3,new FollowParentGoal(this,1.0D));
this.goalSelector.add(4,new WanderAroundFarGoal(this,1.0D));
this.goalSelector.add(5,new LookAtEntityGoal(this, PlayerEntity.class,3f));
this.goalSelector.add(6,new LookAroundGoal(this));
}
public static DefaultAttributeContainer.Builder createTigerAttributes(){
return MobEntity.createMobAttributes()
.add(EntityAttributes.GENERIC_MAX_HEALTH,200)
.add(EntityAttributes.GENERIC_MOVEMENT_SPEED,0.2F)
.add(EntityAttributes.GENERIC_ARMOR,0.5F)
.add(EntityAttributes.GENERIC_ATTACK_DAMAGE,5);
}

@Override
public boolean isBreedingItem(ItemStack stack) {
return stack.isOf(Items.BEEF);
}

@Nullable
@Override
public PassiveEntity createChild(ServerWorld world, PassiveEntity entity) {
return ModEntities.TIGER.create(world);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5e26cba

Please sign in to comment.