EntityHuman.java 7.8 KB

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