From 40c38da37f2081fb9b3c956cf1bd896a8bb99881 Mon Sep 17 00:00:00 2001 From: TechnoVisionDev Date: Thu, 23 Jul 2020 21:42:21 -0700 Subject: [PATCH] Custom Entity --- .../com/technovision/tutorial/Tutorial.java | 11 +- .../client/entity/model/HogModel.java | 80 ++++++++++++++ .../client/entity/render/HogRenderer.java | 22 ++++ .../tutorial/entities/HogEntity.java | 98 ++++++++++++++++++ .../tutorial/init/ModEntityType.java | 21 ++++ .../util/ClientEventBusSubscriber.java | 19 ++++ .../assets/tutorial/textures/entity/hog.png | Bin 0 -> 2421 bytes 7 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/technovision/tutorial/client/entity/model/HogModel.java create mode 100644 src/main/java/com/technovision/tutorial/client/entity/render/HogRenderer.java create mode 100644 src/main/java/com/technovision/tutorial/entities/HogEntity.java create mode 100644 src/main/java/com/technovision/tutorial/init/ModEntityType.java create mode 100644 src/main/java/com/technovision/tutorial/util/ClientEventBusSubscriber.java create mode 100644 src/main/resources/assets/tutorial/textures/entity/hog.png diff --git a/src/main/java/com/technovision/tutorial/Tutorial.java b/src/main/java/com/technovision/tutorial/Tutorial.java index c562513..1007335 100644 --- a/src/main/java/com/technovision/tutorial/Tutorial.java +++ b/src/main/java/com/technovision/tutorial/Tutorial.java @@ -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) { } diff --git a/src/main/java/com/technovision/tutorial/client/entity/model/HogModel.java b/src/main/java/com/technovision/tutorial/client/entity/model/HogModel.java new file mode 100644 index 0000000..65d1ff9 --- /dev/null +++ b/src/main/java/com/technovision/tutorial/client/entity/model/HogModel.java @@ -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 extends EntityModel { + + 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) { } +} diff --git a/src/main/java/com/technovision/tutorial/client/entity/render/HogRenderer.java b/src/main/java/com/technovision/tutorial/client/entity/render/HogRenderer.java new file mode 100644 index 0000000..4636b2a --- /dev/null +++ b/src/main/java/com/technovision/tutorial/client/entity/render/HogRenderer.java @@ -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> { + + 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; + } +} diff --git a/src/main/java/com/technovision/tutorial/entities/HogEntity.java b/src/main/java/com/technovision/tutorial/entities/HogEntity.java new file mode 100644 index 0000000..9f604b8 --- /dev/null +++ b/src/main/java/com/technovision/tutorial/entities/HogEntity.java @@ -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 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); + } + + } +} diff --git a/src/main/java/com/technovision/tutorial/init/ModEntityType.java b/src/main/java/com/technovision/tutorial/init/ModEntityType.java new file mode 100644 index 0000000..143f4fd --- /dev/null +++ b/src/main/java/com/technovision/tutorial/init/ModEntityType.java @@ -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> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITIES, Tutorial.MOD_ID); + + // Entity Types + public static final RegistryObject> 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())); +} diff --git a/src/main/java/com/technovision/tutorial/util/ClientEventBusSubscriber.java b/src/main/java/com/technovision/tutorial/util/ClientEventBusSubscriber.java new file mode 100644 index 0000000..09053b9 --- /dev/null +++ b/src/main/java/com/technovision/tutorial/util/ClientEventBusSubscriber.java @@ -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); + } +} diff --git a/src/main/resources/assets/tutorial/textures/entity/hog.png b/src/main/resources/assets/tutorial/textures/entity/hog.png new file mode 100644 index 0000000000000000000000000000000000000000..5a17067d6e31ab9206e49ae2dfc7e5efec71bb82 GIT binary patch literal 2421 zcmV-*35xcKP)6Q-m$p6{?DI3O^`t@Q+cliuI6Ka7oL!o9 zy9AqlfTvEs?Vf+_OC~_g%k{wpaeZ(>99@y(=!#SVe5z+!oa&iY0%<~=T{kPvuA4Q9 z008~;@TPcrcvE@Z_j3^VnUqX`niqmT00vM90N@bBgmCajFbM&?{6lgO_?eVUfSMNo zKGB`xM0YBefd`kQIJhKD3JRYe?T_b2`^)Q26gl|gd0YU?L7r>({A*t_0cu_x)B)s$ zzBzHBZ%zpmbxgif#Q^;8v0o<0vm<@+>`31v7eGJlo`3C2CP2*#Kw#pX5CX7ETRP*? zmd;70o@Jevd)&w5LkPdu?)lfgWCDEQ<@D+qaeDQPxY*wj7yCQPO`1I&DfV=vIQDZP zTqp5!b?dyix^-S@%1^FJadOpIBFLLSUglK+?m8}_YB@-|5Y7a&uskJLZKNOip{@V<{2(B* zT?69)c!-PirilU$01iG#CO{@YYX!w_pIHbUq9_Q!fPVe95GEcGky47z&d!L4NryiH z9AZR7bar+|N+}{@67cl1V^$#mO@Fu;yBZW|!Z=99l>ks9{n&*;0K_hVcDarVu!-k~ z$93lI=ebVs$NSVX0WtwvE0`i0q+84j}`=G0@R!Ubb!gl z#M6NZV3Qp2HX`0O9Rw2e@gAE!5P^PJj?$ljcS6d-d&~oq%l&2#l{(nuJ?7HrVWjEi|g6rN{!4)ENko(Yf%&{_d-Os*3db_oPICIHen2%Px56F>g+i=fR392AX7 zr5&fV0i5NST}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