jayhillx

LifeEvents

Feb 18th, 2023 (edited)
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. package com.xlife.common.event;
  2.  
  3. import com.xlife.XLife;
  4. import com.xlife.common.capability.ILifeInformation;
  5. import com.xlife.common.capability.LifeCapability;
  6. import com.xlife.common.capability.LifeCapabilityProvider;
  7. import com.xlife.common.capability.StoredLifeInformation;
  8. import net.minecraft.entity.Entity;
  9. import net.minecraft.entity.LivingEntity;
  10. import net.minecraft.entity.player.PlayerEntity;
  11. import net.minecraft.entity.player.ServerPlayerEntity;
  12. import net.minecraft.stats.Stats;
  13. import net.minecraft.util.DamageSource;
  14. import net.minecraftforge.common.util.LazyOptional;
  15. import net.minecraftforge.event.AttachCapabilitiesEvent;
  16. import net.minecraftforge.event.entity.living.LivingDeathEvent;
  17. import net.minecraftforge.event.entity.player.PlayerEvent;
  18. import net.minecraftforge.eventbus.api.SubscribeEvent;
  19.  
  20. import java.util.ArrayList;
  21. import java.util.List;
  22.  
  23. /**
  24.  * join
  25.  * logout
  26.  * death
  27.  * respawn
  28.  * tick
  29.  */
  30. public class LifeEvents {
  31.  
  32.     @SubscribeEvent
  33.     public void onJoin(PlayerEvent.PlayerLoggedInEvent event) {
  34.         PlayerEntity player = event.getPlayer();
  35.  
  36.         player.getCapability(LifeCapability.LIFE_CAPABILITY).ifPresent((info) -> {
  37.  
  38.         });
  39.     }
  40.  
  41.     @SubscribeEvent
  42.     public void onDeath(LivingDeathEvent event) {
  43.         DamageSource source = event.getSource();
  44.         Entity sourceEntity = event.getSource().getTrueSource();
  45.  
  46.         if (event.getEntity() instanceof PlayerEntity) {
  47.             PlayerEntity player = (PlayerEntity)event.getEntity();
  48.  
  49.             player.getCapability(LifeCapability.LIFE_CAPABILITY).ifPresent((info) -> {
  50.                 String cause;
  51.  
  52.                 if (info.getRemainingLives() >= 1 && info.getRemainingLives() <= 10) {
  53.                     if (sourceEntity instanceof LivingEntity) {
  54.                         cause = sourceEntity.getName().getString();
  55.                     } else {
  56.                         if (source == DamageSource.IN_FIRE) {
  57.                             cause = "Flames";
  58.                         } else if (source == DamageSource.LIGHTNING_BOLT) {
  59.                             cause = "Lightning";
  60.                         } else if (source == DamageSource.ON_FIRE) {
  61.                             cause = "Burning";
  62.                         } else if (source == DamageSource.LAVA) {
  63.                             cause = "Lava";
  64.                         } else if (source == DamageSource.HOT_FLOOR) {
  65.                             cause = "Magma";
  66.                         } else if (source == DamageSource.IN_WALL) {
  67.                             cause = "Suffocation";
  68.                         } else if (source == DamageSource.CRAMMING) {
  69.                             cause = "Cramming";
  70.                         } else if (source == DamageSource.DROWN) {
  71.                             cause = "Drowning";
  72.                         } else if (source == DamageSource.STARVE) {
  73.                             cause = "Starvation";
  74.                         } else if (source == DamageSource.CACTUS) {
  75.                             cause = "Cactus";
  76.                         } else if (source == DamageSource.FALL) {
  77.                             cause = "Falling";
  78.                         } else if (source == DamageSource.FLY_INTO_WALL) {
  79.                             cause = "Kinetic Energy";
  80.                         } else if (source == DamageSource.OUT_OF_WORLD) {
  81.                             cause = "Void";
  82.                         } else if (source == DamageSource.MAGIC) {
  83.                             cause = "Magic";
  84.                         } else if (source == DamageSource.WITHER) {
  85.                             cause = "Withered";
  86.                         } else if (source == DamageSource.ANVIL) {
  87.                             cause = "Falling Anvil";
  88.                         } else if (source == DamageSource.FALLING_BLOCK) {
  89.                             cause = "Falling Block";
  90.                         } else if (source == DamageSource.DRAGON_BREATH) {
  91.                             cause = "Dragon Breath";
  92.                         } else if (source == DamageSource.SWEET_BERRY_BUSH) {
  93.                             cause = "Berry Bush";
  94.                         } else if (source.isExplosion()) {
  95.                             cause = "Explosion";
  96.                         } else {
  97.                             cause = "Something";
  98.                         }
  99.                     }
  100.  
  101.                     // add life to lives list.
  102.                     List<String> life = new ArrayList<>(3);
  103.                     life.add(0, cause);
  104.                     life.add(1, player.getCombatTracker().getDeathMessage().getFormattedText());
  105.                     info.getPastLives().add(life);
  106.                 }
  107.             });
  108.         }
  109.     }
  110.  
  111.     @SubscribeEvent
  112.     public void onRespawn(PlayerEvent.PlayerRespawnEvent event) {
  113.         PlayerEntity player = event.getPlayer();
  114.  
  115.         player.getCapability(LifeCapability.LIFE_CAPABILITY).ifPresent((info) -> {
  116.            
  117.         });
  118.     }
  119.  
  120.     @SubscribeEvent
  121.     public void attachCapability(AttachCapabilitiesEvent<Entity> event) {
  122.         if (event.getObject() instanceof PlayerEntity) {
  123.             event.addCapability(XLife.modLoc("info"), new LifeCapabilityProvider());
  124.         }
  125.     }
  126.  
  127.     @SubscribeEvent
  128.     public void cloneCapability(PlayerEvent.Clone event) {
  129.         LazyOptional<ILifeInformation> capability = event.getOriginal().getCapability(LifeCapability.LIFE_CAPABILITY);
  130.  
  131.         capability.ifPresent(oldStore -> event.getPlayer().getCapability(LifeCapability.LIFE_CAPABILITY).ifPresent(newStore -> newStore.clonePlayerInfo((StoredLifeInformation) oldStore)));
  132.     }
  133.  
  134. }
Add Comment
Please, Sign In to add comment