123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- package me.km.entities;
- import me.km.Server;
- import me.km.utils.ReflectionUtils;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.CreatureEntity;
- import net.minecraft.entity.EntitySize;
- import net.minecraft.entity.EntityType;
- import net.minecraft.entity.Pose;
- import net.minecraft.entity.ai.attributes.AttributeModifierMap;
- import net.minecraft.entity.ai.attributes.Attributes;
- import net.minecraft.entity.ai.goal.LookAtGoal;
- import net.minecraft.entity.ai.goal.LookRandomlyGoal;
- import net.minecraft.entity.ai.goal.SwimGoal;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.nbt.CompoundNBT;
- import net.minecraft.network.datasync.DataParameter;
- import net.minecraft.network.datasync.DataSerializers;
- import net.minecraft.network.datasync.EntityDataManager;
- 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.world.World;
- import net.minecraftforge.api.distmarker.Dist;
- import net.minecraftforge.api.distmarker.OnlyIn;
- import net.minecraft.util.SoundEvents;
- public class EntityHuman extends CreatureEntity {
- private static final DataParameter<String> SKIN_NAME = EntityDataManager.createKey(EntityHuman.class, DataSerializers.STRING);
- private static final DataParameter<Float> SCALE = EntityDataManager.createKey(EntityHuman.class, DataSerializers.FLOAT);
- private static final DataParameter<Boolean> SLIM = EntityDataManager.createKey(EntityHuman.class, DataSerializers.BOOLEAN);
- private static final DataParameter<Boolean> STATUE = EntityDataManager.createKey(EntityHuman.class, DataSerializers.BOOLEAN);
- private final float originalWidth;
- private final float originalHeight;
- private EntitySize size;
- public EntityHuman(EntityType<EntityHuman> type, World w) {
- super(type, w);
- this.originalWidth = getWidth();
- this.originalHeight = getHeight();
- size = ModEntities.HUMAN.getSize();
- }
- @Override
- protected void registerData() {
- super.registerData();
- this.getDataManager().register(SKIN_NAME, "Steve");
- this.getDataManager().register(SCALE, 1.0f);
- this.getDataManager().register(SLIM, false);
- this.getDataManager().register(STATUE, false);
- }
- public boolean isStatue() {
- return this.getDataManager().get(STATUE);
- }
- public void setStatue(boolean statue) {
- this.getDataManager().set(STATUE, statue);
- if(!world.isRemote) {
- ReflectionUtils.getGoals(goalSelector).clear();
- ReflectionUtils.getGoals(targetSelector).clear();
- registerGoals();
- }
- }
- public boolean isSlim() {
- return this.getDataManager().get(SLIM);
- }
- public void setSlim(boolean slim) {
- this.getDataManager().set(SLIM, slim);
- }
- public final float getScale() {
- return this.getDataManager().get(SCALE);
- }
- public final void setScale(float scale) {
- this.getDataManager().set(SCALE, scale);
- updateSize(scale);
- }
- private void updateSize(float scale) {
- this.setSize(originalWidth * scale, originalHeight * scale);
- }
- public void setSkinName(String name) {
- this.getDataManager().set(SKIN_NAME, name);
- }
- public String getSkinName() {
- return this.getDataManager().get(SKIN_NAME);
- }
- @OnlyIn(Dist.CLIENT)
- public ResourceLocation getTexture() {
- if(isStatue()) {
- return HumanSkinLoader.GREY_INSTANCE.getTexture(getSkinName());
- }
- return HumanSkinLoader.INSTANCE.getTexture(getSkinName());
- }
- @Override
- public void readAdditional(CompoundNBT com) {
- super.readAdditional(com);
- if(com.contains("HumanName")) {
- setSkinName(com.getString("HumanName"));
- }
- if(com.contains("Scale")) {
- setScale(com.getFloat("Scale"));
- }
- if(com.contains("Slim")) {
- setSlim(com.getBoolean("Slim"));
- }
- if(com.contains("Statue")) {
- setStatue(com.getBoolean("Statue"));
- }
- }
- @Override
- public void writeAdditional(CompoundNBT com) {
- super.writeAdditional(com);
- com.putString("HumanName", getSkinName());
- com.putFloat("Scale", getScale());
- com.putBoolean("Slim", isSlim());
- com.putBoolean("Statue", isStatue());
- }
- @Override
- public void addVelocity(double x, double y, double z) {
- }
- @Override
- public void applyKnockback(float strength, double ratioX, double ratioZ) {
- }
- @Override
- public void applyEntityCollision(Entity entityIn) {
- }
- @Override
- public boolean attackEntityFrom(DamageSource source, float amount) {
- if(source == DamageSource.OUT_OF_WORLD) {
- this.remove();
- return false;
- }
- if(!world.isRemote) {
- Server.scriptEvents.onHumanHurt(source.getTrueSource(), this);
- }
- return false;
- }
- @Override
- protected void registerGoals() {
- if(isStatue()) {
- return;
- }
- this.goalSelector.addGoal(0, new SwimGoal(this));
- this.goalSelector.addGoal(1, new LookAtGoal(this, PlayerEntity.class, 6.0F));
- this.goalSelector.addGoal(2, new LookRandomlyGoal(this));
- }
- public static AttributeModifierMap.MutableAttribute getAttributes() {
- return AttributeModifierMap.createMutableAttribute()
- .createMutableAttribute(Attributes.ATTACK_DAMAGE, 1)
- .createMutableAttribute(Attributes.MOVEMENT_SPEED, 0.1)
- .createMutableAttribute(Attributes.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.getAttribute(Attributes.MOVEMENT_SPEED).getValue();
- }
- @Override
- protected SoundEvent getFallSound(int heightIn) {
- return heightIn > 4 ? SoundEvents.ENTITY_PLAYER_BIG_FALL : SoundEvents.ENTITY_PLAYER_SMALL_FALL;
- }
- @OnlyIn(Dist.CLIENT)
- @Override
- public boolean getAlwaysRenderNameTagForRender() {
- return true;
- }
- @Override
- public ITextComponent getDisplayName() {
- return getName();
- }
- @Override
- public float getEyeHeight(Pose p_213307_1_) {
- float f = getDefaultEyeHeight();
- if(getHeight() == 1.65F) {
- f -= 0.08F;
- }
- f *= getScale();
- return f;
- }
- public float getDefaultEyeHeight() {
- return 1.62F;
- }
- @Override
- public boolean canDespawn(double distanceToClosestPlayer) {
- return false;
- }
- @Override
- public EntitySize getSize(Pose poseIn) {
- return size;
- }
- private void setSize(float w, float h) {
- size = EntitySize.flexible(w, h);
- recalculateSize();
- }
- @Override
- public void notifyDataManagerChange(DataParameter<?> key) {
- super.notifyDataManagerChange(key);
- if(SCALE.equals(key)) {
- updateSize(getScale());
- }
- }
- }
|