Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xlife.common.event;
- import com.xlife.XLife;
- import com.xlife.common.capability.ILifeInformation;
- import com.xlife.common.capability.LifeCapability;
- import com.xlife.common.capability.LifeCapabilityProvider;
- import com.xlife.common.capability.StoredLifeInformation;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.LivingEntity;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.entity.player.ServerPlayerEntity;
- import net.minecraft.stats.Stats;
- import net.minecraft.util.DamageSource;
- import net.minecraftforge.common.util.LazyOptional;
- import net.minecraftforge.event.AttachCapabilitiesEvent;
- import net.minecraftforge.event.entity.living.LivingDeathEvent;
- import net.minecraftforge.event.entity.player.PlayerEvent;
- import net.minecraftforge.eventbus.api.SubscribeEvent;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * join
- * logout
- * death
- * respawn
- * tick
- */
- public class LifeEvents {
- @SubscribeEvent
- public void onJoin(PlayerEvent.PlayerLoggedInEvent event) {
- PlayerEntity player = event.getPlayer();
- player.getCapability(LifeCapability.LIFE_CAPABILITY).ifPresent((info) -> {
- });
- }
- @SubscribeEvent
- public void onDeath(LivingDeathEvent event) {
- DamageSource source = event.getSource();
- Entity sourceEntity = event.getSource().getTrueSource();
- if (event.getEntity() instanceof PlayerEntity) {
- PlayerEntity player = (PlayerEntity)event.getEntity();
- player.getCapability(LifeCapability.LIFE_CAPABILITY).ifPresent((info) -> {
- String cause;
- if (info.getRemainingLives() >= 1 && info.getRemainingLives() <= 10) {
- if (sourceEntity instanceof LivingEntity) {
- cause = sourceEntity.getName().getString();
- } else {
- if (source == DamageSource.IN_FIRE) {
- cause = "Flames";
- } else if (source == DamageSource.LIGHTNING_BOLT) {
- cause = "Lightning";
- } else if (source == DamageSource.ON_FIRE) {
- cause = "Burning";
- } else if (source == DamageSource.LAVA) {
- cause = "Lava";
- } else if (source == DamageSource.HOT_FLOOR) {
- cause = "Magma";
- } else if (source == DamageSource.IN_WALL) {
- cause = "Suffocation";
- } else if (source == DamageSource.CRAMMING) {
- cause = "Cramming";
- } else if (source == DamageSource.DROWN) {
- cause = "Drowning";
- } else if (source == DamageSource.STARVE) {
- cause = "Starvation";
- } else if (source == DamageSource.CACTUS) {
- cause = "Cactus";
- } else if (source == DamageSource.FALL) {
- cause = "Falling";
- } else if (source == DamageSource.FLY_INTO_WALL) {
- cause = "Kinetic Energy";
- } else if (source == DamageSource.OUT_OF_WORLD) {
- cause = "Void";
- } else if (source == DamageSource.MAGIC) {
- cause = "Magic";
- } else if (source == DamageSource.WITHER) {
- cause = "Withered";
- } else if (source == DamageSource.ANVIL) {
- cause = "Falling Anvil";
- } else if (source == DamageSource.FALLING_BLOCK) {
- cause = "Falling Block";
- } else if (source == DamageSource.DRAGON_BREATH) {
- cause = "Dragon Breath";
- } else if (source == DamageSource.SWEET_BERRY_BUSH) {
- cause = "Berry Bush";
- } else if (source.isExplosion()) {
- cause = "Explosion";
- } else {
- cause = "Something";
- }
- }
- // add life to lives list.
- List<String> life = new ArrayList<>(3);
- life.add(0, cause);
- life.add(1, player.getCombatTracker().getDeathMessage().getFormattedText());
- info.getPastLives().add(life);
- }
- });
- }
- }
- @SubscribeEvent
- public void onRespawn(PlayerEvent.PlayerRespawnEvent event) {
- PlayerEntity player = event.getPlayer();
- player.getCapability(LifeCapability.LIFE_CAPABILITY).ifPresent((info) -> {
- });
- }
- @SubscribeEvent
- public void attachCapability(AttachCapabilitiesEvent<Entity> event) {
- if (event.getObject() instanceof PlayerEntity) {
- event.addCapability(XLife.modLoc("info"), new LifeCapabilityProvider());
- }
- }
- @SubscribeEvent
- public void cloneCapability(PlayerEvent.Clone event) {
- LazyOptional<ILifeInformation> capability = event.getOriginal().getCapability(LifeCapability.LIFE_CAPABILITY);
- capability.ifPresent(oldStore -> event.getPlayer().getCapability(LifeCapability.LIFE_CAPABILITY).ifPresent(newStore -> newStore.clonePlayerInfo((StoredLifeInformation) oldStore)));
- }
- }
Add Comment
Please, Sign In to add comment