package me.km.entities; import io.netty.buffer.ByteBuf; import java.nio.charset.StandardCharsets; import me.km.KajetansMod; import me.km.networking.ModPacketHandler; import me.km.snuviscript.ScriptEvents; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityCreature; import net.minecraft.entity.EntityLiving; import net.minecraft.entity.SharedMonsterAttributes; import net.minecraft.entity.ai.EntityAISwimming; import net.minecraft.entity.ai.EntityAIWatchClosest; import net.minecraft.entity.ai.EntityAIWatchClosest2; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.SoundEvents; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.DamageSource; import net.minecraft.util.ResourceLocation; import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundEvent; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraft.world.World; import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class EntityHuman extends EntityCreature implements IEntityAdditionalSpawnData { private final static String STEVE = "Steve"; @SideOnly(Side.CLIENT) private ResourceLocation texture; private String name; private float scale; private byte slim; private float originalWidth; private float originalHeight; public EntityHuman(World w) { super(w); this.name = STEVE; this.scale = 1; this.originalWidth = width; this.originalHeight = height; this.slim = 0; } // ------------------------------------------------------------------------- // size modifier // ------------------------------------------------------------------------- public byte getSlim() { return slim; } public void setSlim(byte slim) { this.slim = slim; } public void setSlim(boolean b) { this.slim = (byte) (b ? 1 : 0); if(!this.world.isRemote) { ModPacketHandler.sendHumanScaleUpdate(this); } } public boolean isSlim() { return this.slim == 1; } public final float getScale() { return scale; } public final void setScale(float scale) { this.scale = scale; this.setSize(originalWidth * scale, originalHeight * scale); if(!this.world.isRemote) { ModPacketHandler.sendHumanScaleUpdate(this); } } // ------------------------------------------------------------------------- // texture stuff // ------------------------------------------------------------------------- @SideOnly(Side.CLIENT) public ResourceLocation getTexture() { return texture; } @SideOnly(Side.CLIENT) public void setTexture(ResourceLocation texture) { this.texture = texture; } @Override public void readEntityFromNBT(NBTTagCompound com) { super.readEntityFromNBT(com); if(com.hasKey("HumanName")) { this.name = com.getString("HumanName"); } if(com.hasKey("Scale")) { this.scale = com.getFloat("Scale"); this.setSize(originalWidth * scale, originalHeight * scale); } if(com.hasKey("Slim")) { this.slim = com.getByte("Slim"); } } @Override public void writeEntityToNBT(NBTTagCompound com) { super.writeEntityToNBT(com); com.setString("HumanName", name); com.setFloat("Scale", scale); com.setByte("Slim", slim); } @Override public void writeSpawnData(ByteBuf buf) { byte[] b = name.getBytes(StandardCharsets.UTF_8); buf.writeInt(b.length); buf.writeBytes(b); buf.writeFloat(scale); buf.writeByte(slim); } @Override public void readSpawnData(ByteBuf buf) { int length = buf.readInt(); name = buf.readBytes(length).toString(StandardCharsets.UTF_8); setTexture(HumanSkinLoader.INSTANCE.getTexture(name, loc -> setTexture(loc))); scale = buf.readFloat(); this.setSize(originalWidth * scale, originalHeight * scale); slim = buf.readByte(); } // ------------------------------------------------------------------------- @Override public void addVelocity(double x, double y, double z) { } @Override public void applyEntityCollision(Entity entityIn) { } @Override public boolean attackEntityFrom(DamageSource source, float amount) { if(source == DamageSource.OUT_OF_WORLD) { this.setDead(); return false; } if(!world.isRemote) { Entity ent = source.getTrueSource(); if(ent instanceof EntityPlayer) { KajetansMod.scripts.getEvent(ScriptEvents.class).onHumanHurt((EntityPlayer) ent, this); } } return false; } @Override protected void initEntityAI() { this.tasks.addTask(0, new EntityAISwimming(this)); this.tasks.addTask(1, new EntityAIWatchClosest2(this, EntityPlayer.class, 3.0F, 1.0F)); this.tasks.addTask(2, new EntityAIWatchClosest(this, EntityLiving.class, 8.0F)); } public void setSkinName(String name) { this.name = name; if(!this.world.isRemote) { ModPacketHandler.sendHumanUpdate(this); } } public String getSkinName() { return name; } @Override protected void applyEntityAttributes() { super.applyEntityAttributes(); this.getAttributeMap().registerAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(1); this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.1d); this.getAttributeMap().registerAttribute(SharedMonsterAttributes.ATTACK_SPEED); } @Override protected SoundEvent getSwimSound() { return SoundEvents.ENTITY_PLAYER_SWIM; } @Override protected SoundEvent getSplashSound() { return SoundEvents.ENTITY_PLAYER_SPLASH; } @Override public SoundCategory getSoundCategory() { return SoundCategory.NEUTRAL; } @Override protected SoundEvent getHurtSound(DamageSource ds) { if (ds == DamageSource.ON_FIRE) { return SoundEvents.ENTITY_PLAYER_HURT_ON_FIRE; } else { return ds == DamageSource.DROWN ? SoundEvents.ENTITY_PLAYER_HURT_DROWN : SoundEvents.ENTITY_PLAYER_HURT; } } @Override protected SoundEvent getDeathSound() { return SoundEvents.ENTITY_PLAYER_DEATH; } @Override public double getYOffset() { return -0.35D; } @Override public float getAIMoveSpeed() { return (float) this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).getAttributeValue(); } @Override protected SoundEvent getFallSound(int heightIn) { return heightIn > 4 ? SoundEvents.ENTITY_PLAYER_BIG_FALL : SoundEvents.ENTITY_PLAYER_SMALL_FALL; } @SideOnly(Side.CLIENT) @Override public boolean getAlwaysRenderNameTagForRender() { return true; } @Override public ITextComponent getDisplayName() { return new TextComponentString(getName()); } @Override public float getEyeHeight() { float f = getDefaultEyeHeight(); if(this.height == 1.65F) { f -= 0.08F; } f *= scale; return f; } public float getDefaultEyeHeight() { return 1.62F; } @Override protected boolean canDespawn() { return false; } }