123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- 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;
- }
- }
|