mirror of
https://github.com/TechnoVisionDev/Forge-Modding-Tutorial-1.16
synced 2024-10-27 20:34:04 +00:00
Custom Entity
This commit is contained in:
parent
005bbd7429
commit
40c38da37f
@ -1,10 +1,14 @@
|
||||
package com.technovision.tutorial;
|
||||
|
||||
import com.technovision.tutorial.entities.HogEntity;
|
||||
import com.technovision.tutorial.init.ModBlocks;
|
||||
import com.technovision.tutorial.init.ModEntityType;
|
||||
import com.technovision.tutorial.init.ModItems;
|
||||
import net.minecraft.entity.ai.attributes.GlobalEntityTypeAttributes;
|
||||
import net.minecraft.item.ItemGroup;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.DeferredWorkQueue;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
@ -24,11 +28,16 @@ public class Tutorial
|
||||
|
||||
ModBlocks.BLOCKS.register(FMLJavaModLoadingContext.get().getModEventBus());
|
||||
ModItems.ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
|
||||
ModEntityType.ENTITY_TYPES.register(FMLJavaModLoadingContext.get().getModEventBus());
|
||||
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
|
||||
private void setup(final FMLCommonSetupEvent event) { }
|
||||
private void setup(final FMLCommonSetupEvent event) {
|
||||
DeferredWorkQueue.runLater(() -> {
|
||||
GlobalEntityTypeAttributes.put(ModEntityType.GOAT.get(), HogEntity.setCustomAttributes().func_233813_a_());
|
||||
});
|
||||
}
|
||||
|
||||
private void doClientStuff(final FMLClientSetupEvent event) { }
|
||||
|
||||
|
@ -0,0 +1,80 @@
|
||||
package com.technovision.tutorial.client.entity.model;
|
||||
|
||||
import com.mojang.blaze3d.matrix.MatrixStack;
|
||||
import com.mojang.blaze3d.vertex.IVertexBuilder;
|
||||
import com.technovision.tutorial.entities.HogEntity;
|
||||
import net.minecraft.client.renderer.entity.model.EntityModel;
|
||||
import net.minecraft.client.renderer.entity.model.PigModel;
|
||||
import net.minecraft.client.renderer.model.ModelRenderer;
|
||||
|
||||
public class HogModel<T extends HogEntity> extends EntityModel<T> {
|
||||
|
||||
private final ModelRenderer body;
|
||||
private final ModelRenderer rotation;
|
||||
private final ModelRenderer body_sub_1;
|
||||
private final ModelRenderer head;
|
||||
private final ModelRenderer leg1;
|
||||
private final ModelRenderer leg2;
|
||||
private final ModelRenderer leg3;
|
||||
private final ModelRenderer leg4;
|
||||
|
||||
public HogModel() {
|
||||
textureWidth = 64;
|
||||
textureHeight = 32;
|
||||
|
||||
body = new ModelRenderer(this);
|
||||
body.setRotationPoint(0.0F, 11.0F, 2.0F);
|
||||
|
||||
|
||||
rotation = new ModelRenderer(this);
|
||||
rotation.setRotationPoint(0.0F, 0.0F, 0.0F);
|
||||
body.addChild(rotation);
|
||||
setRotationAngle(rotation, 1.5708F, 0.0F, 0.0F);
|
||||
|
||||
|
||||
body_sub_1 = new ModelRenderer(this);
|
||||
body_sub_1.setRotationPoint(0.0F, 0.0F, 0.0F);
|
||||
rotation.addChild(body_sub_1);
|
||||
body_sub_1.setTextureOffset(28, 8).addBox(-5.0F, -10.0F, -7.0F, 10.0F, 16.0F, 8.0F, 0.0F, false);
|
||||
|
||||
head = new ModelRenderer(this);
|
||||
head.setRotationPoint(0.0F, 12.0F, -6.0F);
|
||||
head.setTextureOffset(0, 0).addBox(-4.0F, -4.0F, -8.0F, 8.0F, 8.0F, 8.0F, 0.0F, false);
|
||||
head.setTextureOffset(16, 16).addBox(-2.0F, 0.0F, -9.0F, 4.0F, 3.0F, 1.0F, 0.0F, false);
|
||||
|
||||
leg1 = new ModelRenderer(this);
|
||||
leg1.setRotationPoint(3.0F, 18.0F, 7.0F);
|
||||
leg1.setTextureOffset(0, 16).addBox(-2.0F, 0.0F, -2.0F, 4.0F, 6.0F, 4.0F, 0.0F, false);
|
||||
|
||||
leg2 = new ModelRenderer(this);
|
||||
leg2.setRotationPoint(-3.0F, 18.0F, 7.0F);
|
||||
leg2.setTextureOffset(0, 16).addBox(-2.0F, 0.0F, -2.0F, 4.0F, 6.0F, 4.0F, 0.0F, false);
|
||||
|
||||
leg3 = new ModelRenderer(this);
|
||||
leg3.setRotationPoint(3.0F, 18.0F, -5.0F);
|
||||
leg3.setTextureOffset(0, 16).addBox(-2.0F, 0.0F, -2.0F, 4.0F, 6.0F, 4.0F, 0.0F, false);
|
||||
|
||||
leg4 = new ModelRenderer(this);
|
||||
leg4.setRotationPoint(-3.0F, 18.0F, -5.0F);
|
||||
leg4.setTextureOffset(0, 16).addBox(-2.0F, 0.0F, -2.0F, 4.0F, 6.0F, 4.0F, 0.0F, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(MatrixStack matrixStack, IVertexBuilder buffer, int packedLight, int packedOverlay, float red, float green, float blue, float alpha){
|
||||
body.render(matrixStack, buffer, packedLight, packedOverlay);
|
||||
head.render(matrixStack, buffer, packedLight, packedOverlay);
|
||||
leg1.render(matrixStack, buffer, packedLight, packedOverlay);
|
||||
leg2.render(matrixStack, buffer, packedLight, packedOverlay);
|
||||
leg3.render(matrixStack, buffer, packedLight, packedOverlay);
|
||||
leg4.render(matrixStack, buffer, packedLight, packedOverlay);
|
||||
}
|
||||
|
||||
public void setRotationAngle(ModelRenderer modelRenderer, float x, float y, float z) {
|
||||
modelRenderer.rotateAngleX = x;
|
||||
modelRenderer.rotateAngleY = y;
|
||||
modelRenderer.rotateAngleZ = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRotationAngles(T entityIn, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) { }
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.technovision.tutorial.client.entity.render;
|
||||
|
||||
import com.technovision.tutorial.Tutorial;
|
||||
import com.technovision.tutorial.client.entity.model.HogModel;
|
||||
import com.technovision.tutorial.entities.HogEntity;
|
||||
import net.minecraft.client.renderer.entity.EntityRendererManager;
|
||||
import net.minecraft.client.renderer.entity.MobRenderer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
public class HogRenderer extends MobRenderer<HogEntity, HogModel<HogEntity>> {
|
||||
|
||||
protected static final ResourceLocation TEXTURE = new ResourceLocation(Tutorial.MOD_ID, "textures/entity/hog.png");
|
||||
|
||||
public HogRenderer(EntityRendererManager renderManagerIn) {
|
||||
super(renderManagerIn, new com.technovision.tutorial.client.entity.model.HogModel<>(), 0.7f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getEntityTexture(HogEntity entity) {
|
||||
return TEXTURE;
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
package com.technovision.tutorial.entities;
|
||||
|
||||
import com.technovision.tutorial.init.ModEntityType;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.entity.*;
|
||||
import net.minecraft.entity.ai.attributes.AttributeModifierMap;
|
||||
import net.minecraft.entity.ai.attributes.Attributes;
|
||||
import net.minecraft.entity.ai.goal.*;
|
||||
import net.minecraft.entity.passive.AnimalEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.Items;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
import net.minecraft.util.DamageSource;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.SoundEvents;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
|
||||
public class HogEntity extends AnimalEntity {
|
||||
|
||||
private static final Ingredient TEMPTATION_ITEMS = Ingredient.fromItems(Items.CARROT, Items.POTATO, Items.BEETROOT);
|
||||
|
||||
private EatGrassGoal eatGrassGoal;
|
||||
private int goatTimer;
|
||||
|
||||
public HogEntity(EntityType<? extends AnimalEntity> type, World worldIn) {
|
||||
super(type, worldIn);
|
||||
}
|
||||
|
||||
public static AttributeModifierMap.MutableAttribute setCustomAttributes() {
|
||||
//func_233666_p_ ---> registerAttributes()
|
||||
//func_233815_a_ ---> createMutableAttribute()
|
||||
return MobEntity.func_233666_p_().func_233815_a_(Attributes.MAX_HEALTH, 10.0D)
|
||||
.func_233815_a_(Attributes.MOVEMENT_SPEED, 0.25D);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AgeableEntity createChild(AgeableEntity ageable) {
|
||||
return ModEntityType.GOAT.get().create(this.world);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerGoals() {
|
||||
super.registerGoals();
|
||||
this.eatGrassGoal = new EatGrassGoal(this);
|
||||
this.goalSelector.addGoal(0, new SwimGoal(this));
|
||||
this.goalSelector.addGoal(1, new PanicGoal(this, 1.25D));
|
||||
this.goalSelector.addGoal(2, new BreedGoal(this, 1.0D));
|
||||
this.goalSelector.addGoal(3, new TemptGoal(this, 1.1D, TEMPTATION_ITEMS, false));
|
||||
this.goalSelector.addGoal(4, new FollowParentGoal(this, 1.1D));
|
||||
this.goalSelector.addGoal(5, this.eatGrassGoal);
|
||||
this.goalSelector.addGoal(6, new WaterAvoidingRandomWalkingGoal(this, 1.0D));
|
||||
this.goalSelector.addGoal(7, new LookAtGoal(this, PlayerEntity.class, 6.0F));
|
||||
this.goalSelector.addGoal(8, new LookRandomlyGoal(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SoundEvent getAmbientSound() { return SoundEvents.ENTITY_PIG_AMBIENT; }
|
||||
|
||||
@Override
|
||||
protected SoundEvent getDeathSound() { return SoundEvents.ENTITY_PIG_DEATH; }
|
||||
|
||||
@Override
|
||||
protected SoundEvent getHurtSound(DamageSource damageSourceIn) {
|
||||
return SoundEvents.ENTITY_PIG_HURT;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void playStepSound(BlockPos pos, BlockState blockIn) {
|
||||
this.playSound(SoundEvents.ENTITY_PIG_STEP, 0.15F, 1.0F);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateAITasks() {
|
||||
this.goatTimer = this.eatGrassGoal.getEatingGrassTimer();
|
||||
super.updateAITasks();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void livingTick() {
|
||||
if (this.world.isRemote) {
|
||||
this.goatTimer = Math.max(0, this.goatTimer - 1);
|
||||
}
|
||||
super.livingTick();
|
||||
}
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public void handleStatusUpdate(byte id) {
|
||||
if (id == 10) {
|
||||
this.goatTimer = 40;
|
||||
} else {
|
||||
super.handleStatusUpdate(id);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.technovision.tutorial.init;
|
||||
|
||||
import com.technovision.tutorial.Tutorial;
|
||||
import com.technovision.tutorial.entities.HogEntity;
|
||||
import net.minecraft.entity.EntityClassification;
|
||||
import net.minecraft.entity.EntityType;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.fml.RegistryObject;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
public class ModEntityType {
|
||||
|
||||
public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITIES, Tutorial.MOD_ID);
|
||||
|
||||
// Entity Types
|
||||
public static final RegistryObject<EntityType<HogEntity>> GOAT = ENTITY_TYPES.register("goat",
|
||||
() -> EntityType.Builder.create(HogEntity::new, EntityClassification.CREATURE)
|
||||
.size(1.0f, 1.0f) // Hitbox
|
||||
.build(new ResourceLocation(Tutorial.MOD_ID, "goat").toString()));
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.technovision.tutorial.util;
|
||||
|
||||
import com.technovision.tutorial.Tutorial;
|
||||
import com.technovision.tutorial.client.entity.render.HogRenderer;
|
||||
import com.technovision.tutorial.init.ModEntityType;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.client.registry.RenderingRegistry;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||
|
||||
@Mod.EventBusSubscriber(modid = Tutorial.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
|
||||
public class ClientEventBusSubscriber {
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onClientSetup(FMLClientSetupEvent event) {
|
||||
RenderingRegistry.registerEntityRenderingHandler(ModEntityType.GOAT.get(), HogRenderer::new);
|
||||
}
|
||||
}
|
BIN
src/main/resources/assets/tutorial/textures/entity/hog.png
Normal file
BIN
src/main/resources/assets/tutorial/textures/entity/hog.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.4 KiB |
Loading…
Reference in New Issue
Block a user