Skip to content

Commit

Permalink
Goblin traders will now eat apples if health is low
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCrayfish committed Feb 15, 2020
1 parent 0b6371f commit f982e9b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
Expand Up @@ -2,10 +2,9 @@

import com.mrcrayfish.goblintraders.client.renderer.entity.model.GoblinTraderModel;
import com.mrcrayfish.goblintraders.entity.AbstractGoblinEntity;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.client.renderer.entity.MobRenderer;
import net.minecraft.client.renderer.entity.model.BipedModel;
import net.minecraft.client.renderer.entity.layers.HeldItemLayer;
import net.minecraft.util.ResourceLocation;

/**
Expand All @@ -16,6 +15,7 @@ public class GoblinTraderRenderer extends MobRenderer<AbstractGoblinEntity, Gobl
public GoblinTraderRenderer(EntityRendererManager renderManagerIn)
{
super(renderManagerIn, new GoblinTraderModel(), 0.5F);
this.addLayer(new HeldItemLayer<>(this));
}

@Override
Expand Down
@@ -1,16 +1,20 @@
package com.mrcrayfish.goblintraders.client.renderer.entity.model;

import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mrcrayfish.goblintraders.entity.AbstractGoblinEntity;
import net.minecraft.client.renderer.entity.model.IHasArm;
import net.minecraft.client.renderer.entity.model.IHasHead;
import net.minecraft.client.renderer.entity.model.SegmentedModel;
import net.minecraft.client.renderer.model.ModelRenderer;
import net.minecraft.util.Hand;
import net.minecraft.util.HandSide;
import net.minecraft.util.math.MathHelper;

/**
* Author: MrCrayfish
*/
public class GoblinTraderModel extends SegmentedModel<AbstractGoblinEntity>
public class GoblinTraderModel extends SegmentedModel<AbstractGoblinEntity> implements IHasArm, IHasHead
{
public ModelRenderer head;
public ModelRenderer hood;
Expand Down Expand Up @@ -125,5 +129,25 @@ public void render(AbstractGoblinEntity entity, float limbSwing, float limbSwing
arm.rotateAngleY += this.body.rotateAngleY * 2.0F;
arm.rotateAngleZ += MathHelper.sin(this.swingProgress * (float)Math.PI) * -0.4F;
}

if(entity.isHandActive())
{
this.rightArm.rotateAngleX = (float) Math.toRadians(-90F + 5F * Math.sin(ageInTicks));
this.leftArm.rotateAngleX = (float) Math.toRadians(-90F + 5F * Math.sin(ageInTicks));
}
}

@Override
public void func_225599_a_(HandSide handSide, MatrixStack matrixStack)
{
this.rightArm.setAnglesAndRotation(matrixStack);
matrixStack.translate(0.25, -0.15, 0.25);
matrixStack.scale(0.75F, 0.75F, 0.75F);
}

@Override
public ModelRenderer getModelHead()
{
return this.head;
}
}
@@ -1,9 +1,7 @@
package com.mrcrayfish.goblintraders.entity;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.mrcrayfish.goblintraders.entity.ai.goal.AttackRevengeTargetGoal;
import com.mrcrayfish.goblintraders.entity.ai.goal.FollowPotentialCustomerGoal;
import com.mrcrayfish.goblintraders.entity.ai.goal.*;
import com.mrcrayfish.goblintraders.entity.ai.goal.LookAtCustomerGoal;
import com.mrcrayfish.goblintraders.entity.ai.goal.TradeWithPlayerGoal;
import net.minecraft.entity.CreatureEntity;
Expand Down Expand Up @@ -58,13 +56,38 @@ protected void registerGoals()
this.goalSelector.addGoal(2, new LookAtCustomerGoal(this));
this.goalSelector.addGoal(3, new AttackRevengeTargetGoal(this));
this.goalSelector.addGoal(4, new FollowPotentialCustomerGoal(this));
this.goalSelector.addGoal(5, new EatAppleGoal(this));
this.goalSelector.addGoal(5, new WaterAvoidingRandomWalkingGoal(this, 0.4D));
this.goalSelector.addGoal(7, new LookAtWithoutMovingGoal(this, PlayerEntity.class, 4.0F, 1.0F));
this.goalSelector.addGoal(8, new LookAtGoal(this, MobEntity.class, 8.0F));
}

public abstract ResourceLocation getTexture();

@Override
protected void onItemUseFinish()
{
if(!this.activeItemStack.equals(this.getHeldItem(this.getActiveHand())))
{
this.stopActiveHand();
}
else
{
if(!this.activeItemStack.isEmpty() && this.isHandActive())
{
this.func_226293_b_(this.activeItemStack, 16);
ItemStack copy = this.activeItemStack.copy();
if(copy.getItem() == Items.APPLE && copy.getItem().getFood() != null)
{
this.setHealth(this.getHealth() + copy.getItem().getFood().getHealing());
}
ItemStack stack = net.minecraftforge.event.ForgeEventFactory.onItemUseFinish(this, copy, getItemInUseCount(), this.activeItemStack.onItemUseFinish(this.world, this));
this.setHeldItem(this.getActiveHand(), stack);
this.resetActiveHand();
}
}
}

@Override
public void livingTick()
{
Expand Down
@@ -0,0 +1,21 @@
package com.mrcrayfish.goblintraders.entity.ai.goal;

import com.mrcrayfish.goblintraders.entity.AbstractGoblinEntity;
import net.minecraft.entity.ai.goal.Goal;
import net.minecraft.entity.ai.goal.UseItemGoal;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;

import java.util.EnumSet;

/**
* Author: MrCrayfish
*/
public class EatAppleGoal extends UseItemGoal<AbstractGoblinEntity>
{
public EatAppleGoal(AbstractGoblinEntity entity)
{
super(entity, new ItemStack(Items.APPLE), null, entity1 -> entity1.getHealth() < entity1.getMaxHealth() && entity1.getRNG().nextInt(40) == 0); //TODO change sound to a burp
this.setMutexFlags(EnumSet.of(Goal.Flag.MOVE));
}
}
Expand Up @@ -24,7 +24,7 @@ public class ModEntities
{
private static final List<EntityType<?>> ENTITY_TYPES = new ArrayList<>();

public static final EntityType<GoblinTraderEntity> GOBLIN_TRADER = build(new ResourceLocation(Reference.MOD_ID, "goblin_trader"), GoblinTraderEntity::new, 0.75F, 1.0F);
public static final EntityType<GoblinTraderEntity> GOBLIN_TRADER = build(new ResourceLocation(Reference.MOD_ID, "goblin_trader"), GoblinTraderEntity::new, 0.5F, 1.0F);

private static <T extends Entity> EntityType<T> build(ResourceLocation id, Function<World, T> function, float width, float height)
{
Expand Down

0 comments on commit f982e9b

Please sign in to comment.