EntityHuman.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package me.km.entities;
  2. import me.km.Server;
  3. import me.km.utils.ReflectionUtils;
  4. import net.minecraft.entity.Entity;
  5. import net.minecraft.entity.CreatureEntity;
  6. import net.minecraft.entity.EntitySize;
  7. import net.minecraft.entity.EntityType;
  8. import net.minecraft.entity.Pose;
  9. import net.minecraft.entity.ai.attributes.AttributeModifierMap;
  10. import net.minecraft.entity.ai.attributes.Attributes;
  11. import net.minecraft.entity.ai.goal.LookAtGoal;
  12. import net.minecraft.entity.ai.goal.LookRandomlyGoal;
  13. import net.minecraft.entity.ai.goal.SwimGoal;
  14. import net.minecraft.entity.monster.MonsterEntity;
  15. import net.minecraft.entity.player.PlayerEntity;
  16. import net.minecraft.nbt.CompoundNBT;
  17. import net.minecraft.network.datasync.DataParameter;
  18. import net.minecraft.network.datasync.DataSerializers;
  19. import net.minecraft.network.datasync.EntityDataManager;
  20. import net.minecraft.util.DamageSource;
  21. import net.minecraft.util.ResourceLocation;
  22. import net.minecraft.util.SoundCategory;
  23. import net.minecraft.util.SoundEvent;
  24. import net.minecraft.util.text.ITextComponent;
  25. import net.minecraft.world.World;
  26. import net.minecraftforge.api.distmarker.Dist;
  27. import net.minecraftforge.api.distmarker.OnlyIn;
  28. import net.minecraft.util.SoundEvents;
  29. public class EntityHuman extends CreatureEntity {
  30. private static final DataParameter<String> SKIN_NAME = EntityDataManager.createKey(EntityHuman.class, DataSerializers.STRING);
  31. private static final DataParameter<Float> SCALE = EntityDataManager.createKey(EntityHuman.class, DataSerializers.FLOAT);
  32. private static final DataParameter<Boolean> SLIM = EntityDataManager.createKey(EntityHuman.class, DataSerializers.BOOLEAN);
  33. private static final DataParameter<Boolean> STATUE = EntityDataManager.createKey(EntityHuman.class, DataSerializers.BOOLEAN);
  34. private final float originalWidth;
  35. private final float originalHeight;
  36. private EntitySize size;
  37. public EntityHuman(EntityType<EntityHuman> type, World w) {
  38. super(type, w);
  39. this.originalWidth = getWidth();
  40. this.originalHeight = getHeight();
  41. size = ModEntities.HUMAN.getSize();
  42. }
  43. @Override
  44. protected void registerData() {
  45. super.registerData();
  46. this.getDataManager().register(SKIN_NAME, "Steve");
  47. this.getDataManager().register(SCALE, 1.0f);
  48. this.getDataManager().register(SLIM, false);
  49. this.getDataManager().register(STATUE, false);
  50. }
  51. public boolean isStatue() {
  52. return this.getDataManager().get(STATUE);
  53. }
  54. public void setStatue(boolean statue) {
  55. this.getDataManager().set(STATUE, statue);
  56. if(!world.isRemote) {
  57. ReflectionUtils.getGoals(goalSelector).clear();
  58. ReflectionUtils.getGoals(targetSelector).clear();
  59. registerGoals();
  60. }
  61. }
  62. public boolean isSlim() {
  63. return this.getDataManager().get(SLIM);
  64. }
  65. public void setSlim(boolean slim) {
  66. this.getDataManager().set(SLIM, slim);
  67. }
  68. public final float getScale() {
  69. return this.getDataManager().get(SCALE);
  70. }
  71. public final void setScale(float scale) {
  72. this.getDataManager().set(SCALE, scale);
  73. updateSize(scale);
  74. }
  75. private void updateSize(float scale) {
  76. this.setSize(originalWidth * scale, originalHeight * scale);
  77. }
  78. public void setSkinName(String name) {
  79. this.getDataManager().set(SKIN_NAME, name);
  80. }
  81. public String getSkinName() {
  82. return this.getDataManager().get(SKIN_NAME);
  83. }
  84. @OnlyIn(Dist.CLIENT)
  85. public ResourceLocation getTexture() {
  86. if(isStatue()) {
  87. return HumanSkinLoader.GREY_INSTANCE.getTexture(getSkinName());
  88. }
  89. return HumanSkinLoader.INSTANCE.getTexture(getSkinName());
  90. }
  91. @Override
  92. public void readAdditional(CompoundNBT com) {
  93. super.readAdditional(com);
  94. if(com.contains("HumanName")) {
  95. setSkinName(com.getString("HumanName"));
  96. }
  97. if(com.contains("Scale")) {
  98. setScale(com.getFloat("Scale"));
  99. }
  100. if(com.contains("Slim")) {
  101. setSlim(com.getBoolean("Slim"));
  102. }
  103. if(com.contains("Statue")) {
  104. setStatue(com.getBoolean("Statue"));
  105. }
  106. }
  107. @Override
  108. public void writeAdditional(CompoundNBT com) {
  109. super.writeAdditional(com);
  110. com.putString("HumanName", getSkinName());
  111. com.putFloat("Scale", getScale());
  112. com.putBoolean("Slim", isSlim());
  113. com.putBoolean("Statue", isStatue());
  114. }
  115. @Override
  116. public void addVelocity(double x, double y, double z) {
  117. }
  118. @Override
  119. public void applyKnockback(float strength, double ratioX, double ratioZ) {
  120. }
  121. @Override
  122. public void applyEntityCollision(Entity entityIn) {
  123. }
  124. @Override
  125. public boolean attackEntityFrom(DamageSource source, float amount) {
  126. if(source == DamageSource.OUT_OF_WORLD) {
  127. this.remove();
  128. return false;
  129. }
  130. if(!world.isRemote) {
  131. Server.scriptEvents.onHumanHurt(source.getTrueSource(), this);
  132. }
  133. return false;
  134. }
  135. @Override
  136. protected void registerGoals() {
  137. if(isStatue()) {
  138. return;
  139. }
  140. this.goalSelector.addGoal(0, new SwimGoal(this));
  141. this.goalSelector.addGoal(1, new LookAtGoal(this, PlayerEntity.class, 6.0F));
  142. this.goalSelector.addGoal(2, new LookRandomlyGoal(this));
  143. }
  144. public static AttributeModifierMap.MutableAttribute getAttributes() {
  145. return MonsterEntity.func_234295_eP_()
  146. .createMutableAttribute(Attributes.MAX_HEALTH, 20.0)
  147. .createMutableAttribute(Attributes.ATTACK_DAMAGE, 1)
  148. .createMutableAttribute(Attributes.MOVEMENT_SPEED, 0.1)
  149. .createMutableAttribute(Attributes.ATTACK_SPEED);
  150. }
  151. @Override
  152. protected SoundEvent getSwimSound() {
  153. return SoundEvents.ENTITY_PLAYER_SWIM;
  154. }
  155. @Override
  156. protected SoundEvent getSplashSound() {
  157. return SoundEvents.ENTITY_PLAYER_SPLASH;
  158. }
  159. @Override
  160. public SoundCategory getSoundCategory() {
  161. return SoundCategory.NEUTRAL;
  162. }
  163. @Override
  164. protected SoundEvent getHurtSound(DamageSource ds) {
  165. if(ds == DamageSource.ON_FIRE) {
  166. return SoundEvents.ENTITY_PLAYER_HURT_ON_FIRE;
  167. } else {
  168. return ds == DamageSource.DROWN ? SoundEvents.ENTITY_PLAYER_HURT_DROWN : SoundEvents.ENTITY_PLAYER_HURT;
  169. }
  170. }
  171. @Override
  172. protected SoundEvent getDeathSound() {
  173. return SoundEvents.ENTITY_PLAYER_DEATH;
  174. }
  175. @Override
  176. public double getYOffset() {
  177. return -0.35D;
  178. }
  179. @Override
  180. public float getAIMoveSpeed() {
  181. return (float) this.getAttribute(Attributes.MOVEMENT_SPEED).getValue();
  182. }
  183. @Override
  184. protected SoundEvent getFallSound(int heightIn) {
  185. return heightIn > 4 ? SoundEvents.ENTITY_PLAYER_BIG_FALL : SoundEvents.ENTITY_PLAYER_SMALL_FALL;
  186. }
  187. @OnlyIn(Dist.CLIENT)
  188. @Override
  189. public boolean getAlwaysRenderNameTagForRender() {
  190. return true;
  191. }
  192. @Override
  193. public ITextComponent getDisplayName() {
  194. return getName();
  195. }
  196. @Override
  197. public float getEyeHeight(Pose p_213307_1_) {
  198. float f = getDefaultEyeHeight();
  199. if(getHeight() == 1.65F) {
  200. f -= 0.08F;
  201. }
  202. f *= getScale();
  203. return f;
  204. }
  205. public float getDefaultEyeHeight() {
  206. return 1.62F;
  207. }
  208. @Override
  209. public boolean canDespawn(double distanceToClosestPlayer) {
  210. return false;
  211. }
  212. @Override
  213. public EntitySize getSize(Pose poseIn) {
  214. return size;
  215. }
  216. private void setSize(float w, float h) {
  217. size = EntitySize.flexible(w, h);
  218. recalculateSize();
  219. }
  220. @Override
  221. public void notifyDataManagerChange(DataParameter<?> key) {
  222. super.notifyDataManagerChange(key);
  223. if(SCALE.equals(key)) {
  224. updateSize(getScale());
  225. }
  226. }
  227. }