Advertisement
jayhillx

[X Life] LifeEvents

Jan 9th, 2022
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.18 KB | None | 0 0
  1. package com.jayhill.xlife.event;
  2.  
  3. import com.jayhill.xlife.XLife;
  4. import com.jayhill.xlife.common.capability.health.*;
  5. import com.jayhill.xlife.common.capability.stats.*;
  6. import com.jayhill.xlife.common.capability.time.*;
  7. import com.jayhill.xlife.common.util.HealthBookUtils;
  8. import com.jayhill.xlife.common.util.HealthTextUtils;
  9. import net.minecraft.entity.Entity;
  10. import net.minecraft.entity.EntityType;
  11. import net.minecraft.entity.LivingEntity;
  12. import net.minecraft.entity.ai.attributes.Attributes;
  13. import net.minecraft.entity.effect.LightningBoltEntity;
  14. import net.minecraft.entity.player.PlayerEntity;
  15. import net.minecraft.entity.player.ServerPlayerEntity;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.item.Items;
  18. import net.minecraft.nbt.ListNBT;
  19. import net.minecraft.util.DamageSource;
  20. import net.minecraft.util.math.vector.Vector3d;
  21. import net.minecraft.util.text.TranslationTextComponent;
  22. import net.minecraft.world.GameType;
  23. import net.minecraftforge.common.util.LazyOptional;
  24. import net.minecraftforge.event.AttachCapabilitiesEvent;
  25. import net.minecraftforge.event.TickEvent;
  26. import net.minecraftforge.event.entity.living.LivingDeathEvent;
  27. import net.minecraftforge.event.entity.player.PlayerEvent.*;
  28. import net.minecraftforge.event.entity.player.PlayerInteractEvent;
  29. import net.minecraftforge.eventbus.api.SubscribeEvent;
  30.  
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.UUID;
  34.  
  35. import static net.minecraft.util.text.TextFormatting.GRAY;
  36.  
  37. /** Handles every event in X Life. */
  38. public class LifeEvents {
  39.     public static List<Runnable> message = new ArrayList<>();
  40.     private static String cause;
  41.     private int tickCount;
  42.  
  43.     private int getTickCount() {
  44.         return this.tickCount;
  45.     }
  46.  
  47.     private void setTickCount(int count) {
  48.         this.tickCount = count;
  49.     }
  50.  
  51.     @SubscribeEvent
  52.     public void onAttach(AttachCapabilitiesEvent<Entity> event) {
  53.         if (event.getObject() instanceof PlayerEntity) {
  54.             // Health
  55.             HealthCapabilityProvider healthProvider = new HealthCapabilityProvider();
  56.             event.addCapability(XLife.getId("health"), healthProvider);
  57.             // Statistics
  58.             StatsCapabilityProvider statsProvider = new StatsCapabilityProvider();
  59.             event.addCapability(XLife.getId("stats"), statsProvider);
  60.             // Time
  61.             TimeCapabilityProvider timeProvider = new TimeCapabilityProvider();
  62.             event.addCapability(XLife.getId("time"), timeProvider);
  63.         }
  64.     }
  65.  
  66.     @SubscribeEvent
  67.     public void onJoin(PlayerLoggedInEvent event) {
  68.         PlayerEntity player = event.getPlayer();
  69.  
  70.         player.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(health -> {
  71.             if (health.getMaxHealth() == 0) {
  72.                 health.setMaxHealth(2.0F);
  73.  
  74.                 player.setHealth(health.getMaxHealth());
  75.                 player.getAttribute(Attributes.MAX_HEALTH).setBaseValue(health.getMaxHealth());
  76.             }
  77.         });
  78.     }
  79.  
  80.     @SubscribeEvent
  81.     public void onDeath(LivingDeathEvent event) {
  82.         DamageSource source = event.getSource();
  83.         Entity sourceEntity = event.getSource().getEntity();
  84.  
  85.         if (event.getEntity() instanceof PlayerEntity) {
  86.             PlayerEntity player = (PlayerEntity) event.getEntity();
  87.  
  88.             player.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(health -> player.getCapability(StatsCapability.STATS_CAPABILITY).ifPresent(stats -> player.getCapability(TimeCapability.TIME_CAPABILITY).ifPresent(time -> {
  89.                 int hearts = (int) ((health.getMaxHealth() - 2) / 2);
  90.  
  91.                 if (health.getMaxHealth() >= 2.0F && health.getMaxHealth() <= 22.0F) {
  92.                     // Sets players new max health.
  93.                     health.setMaxHealth(health.getMaxHealth() + 2.0F);
  94.  
  95.                     // Sets players cause of death.
  96.                     if (sourceEntity instanceof LivingEntity) {
  97.                         cause = sourceEntity.getName().getString();
  98.                     } else {
  99.                         if (source == DamageSource.IN_FIRE) {
  100.                             cause = "Flames";
  101.                         } else if (source == DamageSource.LIGHTNING_BOLT) {
  102.                             cause = "Lightning";
  103.                         } else if (source == DamageSource.ON_FIRE) {
  104.                             cause = "Burning";
  105.                         } else if (source == DamageSource.LAVA) {
  106.                             cause = "Lava";
  107.                         } else if (source == DamageSource.HOT_FLOOR) {
  108.                             cause = "Magma";
  109.                         } else if (source == DamageSource.IN_WALL) {
  110.                             cause = "Suffocation";
  111.                         } else if (source == DamageSource.CRAMMING) {
  112.                             cause = "Cramming";
  113.                         } else if (source == DamageSource.DROWN) {
  114.                             cause = "Drowning";
  115.                         } else if (source == DamageSource.STARVE) {
  116.                             cause = "Starvation";
  117.                         } else if (source == DamageSource.CACTUS) {
  118.                             cause = "Cactus";
  119.                         } else if (source == DamageSource.FALL) {
  120.                             cause = "Falling";
  121.                         } else if (source == DamageSource.FLY_INTO_WALL) {
  122.                             cause = "Kinetic Energy";
  123.                         } else if (source == DamageSource.OUT_OF_WORLD) {
  124.                             cause = "Void";
  125.                         } else if (source == DamageSource.MAGIC) {
  126.                             cause = "Magic";
  127.                         } else if (source == DamageSource.WITHER) {
  128.                             cause = "Wither";
  129.                         } else if (source == DamageSource.ANVIL) {
  130.                             cause = "Falling Anvil";
  131.                         } else if (source == DamageSource.FALLING_BLOCK) {
  132.                             cause = "Falling Block";
  133.                         } else if (source == DamageSource.DRAGON_BREATH) {
  134.                             cause = "Dragon Breath";
  135.                         } else if (source == DamageSource.SWEET_BERRY_BUSH) {
  136.                             cause = "Berry Bush";
  137.                         } else if (source.isExplosion()) {
  138.                             cause = "Explosion";
  139.                         } else {
  140.                             cause = "Something";
  141.                         }
  142.                     }
  143.  
  144.                     // Sets players death statistics.
  145.                     String[] causeArray = stats.getCause();
  146.                     causeArray[hearts - 1] = cause;
  147.  
  148.                     String[] messageArray = stats.getMessage();
  149.                     messageArray[hearts - 1] = player.getCombatTracker().getDeathMessage().getString();
  150.  
  151.                     String[] timeArray = stats.getTime();
  152.                     if (time.getStoredTime() == 1) {
  153.                         timeArray[hearts - 1] = "1 second";
  154.                     } else if (time.getStoredTime() < 60) {
  155.                         timeArray[hearts - 1] = time.getStoredTime() + " seconds";
  156.                     } else if (time.getStoredTime() >= 60 && time.getStoredTime() < 120) {
  157.                         timeArray[hearts - 1] = "1 minute";
  158.                     } else if (time.getStoredTime() >= 120 && time.getStoredTime() < 3600) {
  159.                         timeArray[hearts - 1] = time.getStoredTime() / 60 + " minutes";
  160.                     } else if (time.getStoredTime() >= 3600 && time.getStoredTime() < 7200) {
  161.                         timeArray[hearts - 1] = "1 hour";
  162.                     } else {
  163.                         timeArray[hearts - 1] = time.getStoredTime() / 3600 + " hours";
  164.                     }
  165.  
  166.                     stats.setCause(causeArray);
  167.                     stats.setMessage(messageArray);
  168.                     stats.setTime(timeArray);
  169.  
  170.                     time.setStoredTime(0);
  171.  
  172.                     // Sends a message for players remaining lives.
  173.                     HealthTextUtils.getRemainingLives(player);
  174.                 }
  175.  
  176.                 if (health.getMaxHealth() == 22.0F) {
  177.                     LightningBoltEntity lightingEntity = EntityType.LIGHTNING_BOLT.create(player.level);
  178.  
  179.                     if (lightingEntity != null) {
  180.                         lightingEntity.moveTo(Vector3d.atBottomCenterOf(player.blockPosition()));
  181.                         lightingEntity.setVisualOnly(true);
  182.                         player.level.addFreshEntity(lightingEntity);
  183.                     }
  184.                 }
  185.             })));
  186.         }
  187.     }
  188.  
  189.     @SubscribeEvent
  190.     public void onRespawn(PlayerRespawnEvent event) {
  191.         PlayerEntity player = event.getPlayer();
  192.  
  193.         if (player.getServer() != null) {
  194.             player.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(health -> player.getCapability(StatsCapability.STATS_CAPABILITY).ifPresent(stats -> {
  195.                 int lives = (int) (10 - ((health.getMaxHealth() - 2) / 2));
  196.                 int hearts = (int) (health.getMaxHealth() / 2) - 2;
  197.  
  198.                 String[] timeArray = stats.getTime();
  199.  
  200.                 if (health.getMaxHealth() >= 2.0F && health.getMaxHealth() <= 22.0F) {
  201.                     // Sets players new max health.
  202.                     player.setHealth(health.getMaxHealth());
  203.                     player.getAttribute(Attributes.MAX_HEALTH).setBaseValue(health.getMaxHealth());
  204.  
  205.                     if (hearts < 10) {
  206.                         if (lives >= 2) {
  207.                             player.getServer().getCommands().performCommand(player.getServer().createCommandSourceStack().withSuppressedOutput(), "/title " + player.getScoreboardName() + " title \"" + lives + " lives remain\"");
  208.                         } else if (lives == 1) {
  209.                             player.getServer().getCommands().performCommand(player.getServer().createCommandSourceStack().withSuppressedOutput(), "/title " + player.getScoreboardName() + " title \"1 life remains\"");
  210.                         } else {
  211.                             player.getServer().getCommands().performCommand(player.getServer().createCommandSourceStack().withSuppressedOutput(), "/title " + player.getScoreboardName() + " title \"Eliminated\"");
  212.                         }
  213.  
  214.                         player.getServer().getCommands().performCommand(player.getServer().createCommandSourceStack().withSuppressedOutput(), "/title " + player.getScoreboardName() + " subtitle \"You lasted " + timeArray[hearts] + "\"");
  215.                     }
  216.                 }
  217.  
  218.                 if (health.getMaxHealth() == 22.0F) {
  219.                     player.setGameMode(GameType.SPECTATOR);
  220.                 }
  221.             }));
  222.         }
  223.     }
  224.  
  225.     @SubscribeEvent
  226.     public void onFormat(TabListNameFormat event) {
  227.         PlayerEntity player = event.getPlayer();
  228.  
  229.         player.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(health -> {
  230.             if (!player.isSpectator()) {
  231.                 if (health.getMaxHealth() <= 20.0F) {
  232.                     event.setDisplayName(new TranslationTextComponent(player.getScoreboardName() + " " + GRAY + "(" + HealthTextUtils.setHearts(health.getMaxHealth(), player.getHealth()) + GRAY + ")"));
  233.                 } else {
  234.                     event.setDisplayName(new TranslationTextComponent(player.getScoreboardName()));
  235.                 }
  236.             }
  237.         });
  238.     }
  239.  
  240.     @SubscribeEvent
  241.     public void onTick(TickEvent.PlayerTickEvent event) {
  242.         PlayerEntity player = event.player;
  243.  
  244.         if (player.getServer() != null) {
  245.             player.getCapability(TimeCapability.TIME_CAPABILITY).ifPresent(time -> {
  246.  
  247.                 if (event.side.isServer()) {
  248.  
  249.                     if (event.phase == TickEvent.Phase.START) {
  250.                         for (Runnable runnable : message) runnable.run(); {
  251.                             message.clear();
  252.                         }
  253.  
  254.                     } else if (event.phase == TickEvent.Phase.END) {
  255.                         if (this.tickCount == 20) {
  256.                             time.setStoredTime(time.getStoredTime() + 1);
  257.                             this.setTickCount(0);
  258.                         } else {
  259.                             if (player.isAlive() && player.getServer().getPlayerList().getPlayer(player.getUUID()) != null) {
  260.                                 this.setTickCount(this.getTickCount() + 1);
  261.                             }
  262.                         }
  263.                     }
  264.  
  265.                     // Updates players name in tablist.
  266.                     for (ServerPlayerEntity players : player.getServer().getPlayerList().getPlayers()) {
  267.                         players.refreshTabListName();
  268.                     }
  269.                 }
  270.             });
  271.         }
  272.     }
  273.  
  274.     @SubscribeEvent
  275.     public void onUseBook(PlayerInteractEvent.RightClickItem event) {
  276.         PlayerEntity player = event.getPlayer();
  277.         ItemStack stack = event.getItemStack();
  278.         ListNBT pages = new ListNBT();
  279.  
  280.         if (stack.getItem() == Items.WRITTEN_BOOK) {
  281.             if (stack.getTag() != null && stack.getTag().hasUUID("owner")) {
  282.  
  283.                 if (!player.level.isClientSide) {
  284.                     UUID uuid = stack.getTag().getUUID("owner");
  285.                     ServerPlayerEntity players = player.getServer().getPlayerList().getPlayer(uuid);
  286.  
  287.                     if (players != null) {
  288.                         players.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(health -> {
  289.                             players.getCapability(TimeCapability.TIME_CAPABILITY).ifPresent(time -> {
  290.  
  291.                                 stack.getTag().put("pages", pages);
  292.                                 stack.getTag().remove("resolved");
  293.  
  294.                                 if (health.getMaxHealth() >= 2.0F && health.getMaxHealth() <= 6.0F) {
  295.                                     pages.add(HealthBookUtils.setPages(players, 1));
  296.                                 } else if (health.getMaxHealth() >= 8.0F && health.getMaxHealth() <= 12.0F) {
  297.                                     pages.add(HealthBookUtils.setPages(players, 1));
  298.                                     pages.add(HealthBookUtils.setPages(players, 2));
  299.                                 } else if (health.getMaxHealth() >= 14.0F && health.getMaxHealth() <= 18.0F) {
  300.                                     pages.add(HealthBookUtils.setPages(players, 1));
  301.                                     pages.add(HealthBookUtils.setPages(players, 2));
  302.                                     pages.add(HealthBookUtils.setPages(players, 3));
  303.                                 } else {
  304.                                     pages.add(HealthBookUtils.setPages(players, 1));
  305.                                     pages.add(HealthBookUtils.setPages(players, 2));
  306.                                     pages.add(HealthBookUtils.setPages(players, 3));
  307.                                     pages.add(HealthBookUtils.setPages(players, 4));
  308.                                 }
  309.                             });
  310.                         });
  311.                     }
  312.                 }
  313.             }
  314.         }
  315.     }
  316.  
  317.     @SubscribeEvent
  318.     public void onClone(Clone event) {
  319.         PlayerEntity player = event.getPlayer();
  320.  
  321.         // Health
  322.         LazyOptional<IHealthCapability> healthCapability = event.getOriginal().getCapability(HealthCapability.HEALTH_CAPABILITY);
  323.         healthCapability.ifPresent(oldStore -> player.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(newStore -> newStore.onClone((DefaultHealthCapability) oldStore)));
  324.         // Statistics
  325.         LazyOptional<IStatsCapability> statsCapability = event.getOriginal().getCapability(StatsCapability.STATS_CAPABILITY);
  326.         statsCapability.ifPresent(oldStore -> player.getCapability(StatsCapability.STATS_CAPABILITY).ifPresent(newStore -> newStore.onClone((DefaultStatsCapability) oldStore)));
  327.         // Time
  328.         LazyOptional<ITimeCapability> timeCapability = event.getOriginal().getCapability(TimeCapability.TIME_CAPABILITY);
  329.         timeCapability.ifPresent(oldStore -> player.getCapability(TimeCapability.TIME_CAPABILITY).ifPresent(newStore -> newStore.onClone((DefaultTimeCapability) oldStore)));
  330.     }
  331.  
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement