Guest User

Untitled

a guest
Jun 18th, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.10 KB | None | 0 0
  1. package com.crescentine.trajanstanks.entity.tanks.basetank;
  2.  
  3. import com.crescentine.trajanscore.TankModClient;
  4. import com.crescentine.trajanstanks.entity.shell.ShellEntity;
  5. import com.crescentine.trajanstanks.item.TankModItems;
  6. import net.minecraft.ChatFormatting;
  7. import net.minecraft.Util;
  8. import net.minecraft.core.particles.ParticleTypes;
  9. import net.minecraft.nbt.CompoundTag;
  10. import net.minecraft.network.chat.TextComponent;
  11. import net.minecraft.server.level.ServerLevel;
  12. import net.minecraft.sounds.SoundEvent;
  13. import net.minecraft.sounds.SoundEvents;
  14. import net.minecraft.sounds.SoundSource;
  15. import net.minecraft.world.DifficultyInstance;
  16. import net.minecraft.world.InteractionHand;
  17. import net.minecraft.world.InteractionResult;
  18. import net.minecraft.world.damagesource.DamageSource;
  19. import net.minecraft.world.entity.*;
  20. import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
  21. import net.minecraft.world.entity.ai.attributes.Attributes;
  22. import net.minecraft.world.entity.animal.Pig;
  23. import net.minecraft.world.entity.player.Player;
  24. import net.minecraft.world.item.ItemStack;
  25. import net.minecraft.world.item.Items;
  26. import net.minecraft.world.level.Level;
  27. import net.minecraft.world.level.ServerLevelAccessor;
  28. import net.minecraft.world.phys.Vec3;
  29. import org.jetbrains.annotations.Nullable;
  30. import software.bernie.geckolib3.core.IAnimatable;
  31. import software.bernie.geckolib3.core.PlayState;
  32. import software.bernie.geckolib3.core.controller.AnimationController;
  33. import software.bernie.geckolib3.core.event.predicate.AnimationEvent;
  34. import software.bernie.geckolib3.core.manager.AnimationData;
  35. import software.bernie.geckolib3.core.manager.AnimationFactory;
  36.  
  37. public class BaseTankEntity extends Pig implements IAnimatable {
  38. private final AnimationFactory factory = new AnimationFactory(this);
  39. public double healAmount = 0;
  40. public double speed = 0;
  41. public int shootingCooldown = 60;
  42. public int time;
  43. public double armor = 0;
  44. static int shellsUsed = 1;
  45. public double health = 0;
  46. public double maxFuel = 12000.00;
  47. public double fuel = 0;
  48. public String tankName;
  49.  
  50. public BaseTankEntity(EntityType<?> entityType, Level world) {
  51. super((EntityType<? extends Pig>) entityType, world);
  52. }
  53.  
  54. public static AttributeSupplier.Builder createAttributes() {
  55. return Pig.createLivingAttributes()
  56. .add(Attributes.MAX_HEALTH, 250.0)
  57. .add(Attributes.KNOCKBACK_RESISTANCE, 10.0D)
  58. .add(Attributes.FOLLOW_RANGE, 0.0D);
  59. }
  60. @Override
  61. public InteractionResult interactAt(Player player, Vec3 hitPos, InteractionHand hand) {
  62. ItemStack itemstack = player.getItemInHand(hand);
  63. if (itemstack.is(Items.IRON_BLOCK)) {
  64. this.heal((float) healAmount);
  65. itemstack.shrink(1);
  66. double d0 = this.random.nextGaussian() * 0.03D;
  67. double d1 = this.random.nextGaussian() * 0.03D;
  68. double d2 = this.random.nextGaussian() * 0.03D;
  69. this.level.addParticle(ParticleTypes.FLAME, this.getRandomX(1.0D), this.getRandomY() + 0.5D, this.getRandomZ(1.0D), d0, d1, d2);
  70. return InteractionResult.SUCCESS;
  71. }
  72. if (itemstack.is(Items.COAL) && fuel < maxFuel) {
  73. fuel += 80;
  74. itemstack.shrink(1);
  75. double d0 = this.random.nextGaussian() * 0.03D;
  76. double d1 = this.random.nextGaussian() * 0.03D;
  77. double d2 = this.random.nextGaussian() * 0.03D;
  78. this.level.addParticle(ParticleTypes.LARGE_SMOKE, this.getRandomX(1.0D), this.getRandomY() + 0.5D, this.getRandomZ(1.0D), d0, d1, d2);
  79. return InteractionResult.SUCCESS;
  80.  
  81. }
  82. player.startRiding(this, true);
  83. return InteractionResult.FAIL;
  84. }
  85.  
  86. @Override
  87. protected boolean canAddPassenger(Entity p_20354_) {
  88. return !this.isVehicle();
  89. }
  90.  
  91. @Override
  92. public void thunderHit(ServerLevel p_29473_, LightningBolt p_29474_) {
  93. }
  94. @Override
  95. public boolean rideableUnderWater() {
  96. return false;
  97. }
  98.  
  99. @Override
  100. protected int calculateFallDamage(float fallDistance, float damageMultiplier) {
  101. return 0;
  102. }
  103.  
  104. @Override
  105. public int getMaxFallDistance() {
  106. return 30;
  107. }
  108.  
  109. @Override
  110. public boolean canBeControlledByRider() {
  111. return true;
  112. }
  113.  
  114. @Override
  115. protected boolean isImmobile() {
  116. return true;
  117. }
  118.  
  119. @Override
  120. public float getSteeringSpeed() {
  121. if (TankModClient.startMoving.isDown() && fuel > 0) {
  122. return (float) speed;
  123. }
  124. return 0.0f;
  125. }
  126.  
  127. @Override
  128. public SpawnGroupData finalizeSpawn(ServerLevelAccessor p_146746_, DifficultyInstance p_146747_, MobSpawnType p_146748_, @Nullable SpawnGroupData p_146749_, @Nullable CompoundTag p_146750_) {
  129. this.getAttribute(Attributes.MAX_HEALTH).setBaseValue((float) health);
  130. this.getAttribute(Attributes.ARMOR).setBaseValue(armor);
  131. this.setHealth((float) health);
  132. return super.finalizeSpawn(p_146746_, p_146747_, p_146748_, p_146749_, p_146750_);
  133. }
  134.  
  135. @Override
  136. public boolean requiresCustomPersistence() {
  137. return true;
  138. }
  139.  
  140. @Override
  141. public ItemStack getItemBySlot(EquipmentSlot slot) {
  142. return ItemStack.EMPTY;
  143. }
  144.  
  145. @Override
  146. protected void registerGoals() {
  147. }
  148.  
  149. @Override
  150. public boolean startRiding(Entity p_21396_, boolean p_21397_) {
  151. this.time = 0;
  152. return super.startRiding(p_21396_, p_21397_);
  153. }
  154.  
  155. @Override
  156. public boolean isBaby() {
  157. return false;
  158. }
  159.  
  160. @Override
  161. public Pig getBreedOffspring(ServerLevel p_149001_, AgeableMob p_149002_) {
  162. return null;
  163. }
  164.  
  165. @Override
  166. public void addAdditionalSaveData(CompoundTag pCompound) {
  167. pCompound.putDouble(tankName + ".fuel", fuel);
  168. super.addAdditionalSaveData(pCompound);
  169. }
  170.  
  171. @Override
  172. public void readAdditionalSaveData(CompoundTag pCompound) {
  173. super.readAdditionalSaveData(pCompound);
  174. fuel = pCompound.getDouble(tankName + ".fuel");
  175. }
  176.  
  177. @Override
  178. protected SoundEvent getAmbientSound() {
  179. return SoundEvents.MINECART_RIDING;
  180. }
  181.  
  182. @Override
  183. protected SoundEvent getDeathSound() {
  184. return SoundEvents.GENERIC_EXPLODE;
  185. }
  186.  
  187. @Override
  188. protected SoundEvent getHurtSound(DamageSource source) {
  189. return SoundEvents.ARMOR_EQUIP_IRON;
  190. }
  191.  
  192. @Override
  193. protected SoundEvent getSwimSplashSound() {
  194. return SoundEvents.PLAYER_SPLASH;
  195. }
  196.  
  197. @Override
  198. protected SoundEvent getSwimSound() {
  199. return SoundEvents.GENERIC_SWIM;
  200. }
  201.  
  202. @Override
  203. public void tick() {
  204. super.tick();
  205. if (time < shootingCooldown) time++;
  206. if (isVehicle() && fuel > 0) fuel--;
  207. }
  208.  
  209. public boolean shoot(Player player, BaseTankEntity tank, Level world) {
  210. ItemStack itemStack = ItemStack.EMPTY;
  211. Player playerEntity = (Player) player;
  212. BaseTankEntity tankEntity = (BaseTankEntity) tank;
  213. for (int i = 0; i < playerEntity.getInventory().getContainerSize(); ++i) {
  214. ItemStack stack = playerEntity.getInventory().getItem(i);
  215. if (stack.getItem() == TankModItems.SHELL_ITEM.get() && stack.getCount() >= shellsUsed) {
  216. itemStack = stack;
  217. break;
  218. }
  219. }
  220.  
  221. if (time < shootingCooldown) {
  222. player.sendMessage(new TextComponent("Please wait " + (shootingCooldown - time) / 20 + " s !").withStyle(ChatFormatting.AQUA, ChatFormatting.BOLD), Util.NIL_UUID);
  223. world.playSound(null, player.blockPosition(), SoundEvents.DISPENSER_FAIL, SoundSource.BLOCKS, 1.0f, 1.0f);
  224. return false;
  225. }
  226. if (itemStack.isEmpty()) {
  227. player.sendMessage(new TextComponent("You don't have any ammo!").withStyle(ChatFormatting.RED, ChatFormatting.BOLD), Util.NIL_UUID);
  228. world.playSound(null, player.blockPosition(), SoundEvents.DISPENSER_FAIL, SoundSource.BLOCKS, 1.0f, 1.0f);
  229. return false;
  230. }
  231. if (fuel < 1) {
  232. player.sendMessage(new TextComponent("You don't have any fuel!").withStyle(ChatFormatting.RED, ChatFormatting.BOLD), Util.NIL_UUID);
  233. world.playSound(null, player.blockPosition(), SoundEvents.DISPENSER_FAIL, SoundSource.BLOCKS, 1.0f, 1.0f);
  234. return false;
  235. }
  236. if (!itemStack.isEmpty()) {
  237. ShellEntity shellEntity = new ShellEntity(tankEntity, world);
  238. shellEntity.shootFromRotation(tankEntity, tankEntity.getXRot(), tankEntity.getYRot(), 0.0F, 2.0F, 0F);
  239. world.addFreshEntity(shellEntity);
  240. itemStack.shrink(shellsUsed);
  241. }
  242. time = 0;
  243. return true;
  244. }
  245.  
  246. public boolean fuelLeft(Player player) {
  247. if (fuel < 1200 && fuel > 1) {
  248. player.sendMessage(new TextComponent("Low fuel! Amount of time before fuel runs out " + fuel / 20 + " seconds or " + String.format("%.2f", fuel/1200) + " minutes ").withStyle(ChatFormatting.RED, ChatFormatting.BOLD), Util.NIL_UUID);
  249. return true;
  250. }
  251. if (fuel > 1200) {
  252. player.sendMessage(new TextComponent("The amount of fuel remaining: " + fuel / 20 + " seconds, or " + String.format("%.2f", fuel/1200) + " minutes ").withStyle(ChatFormatting.BLUE, ChatFormatting.BOLD), Util.NIL_UUID);
  253. return true;
  254. }
  255. if (fuel < 1) {
  256. player.sendMessage(new TextComponent("No fuel remaining!").withStyle(ChatFormatting.RED, ChatFormatting.BOLD), Util.NIL_UUID);
  257. }
  258. return false;
  259. }
  260. @Override
  261. public AnimationFactory getFactory() {
  262. return this.factory;
  263. }
  264. protected <E extends IAnimatable> PlayState predicate(AnimationEvent<E> event) {
  265. return PlayState.STOP;
  266. }
  267. @Override
  268. public void registerControllers(AnimationData animationData) {
  269. animationData.addAnimationController(new AnimationController<>(this, "controller", 0, this::predicate));
  270. }
  271. }
  272.  
Add Comment
Please, Sign In to add comment