Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.jayhill.xlife.event;
- import com.jayhill.xlife.XLife;
- import com.jayhill.xlife.common.capability.health.*;
- import com.jayhill.xlife.common.capability.stats.*;
- import com.jayhill.xlife.common.capability.time.*;
- import com.jayhill.xlife.common.util.HealthBookUtils;
- import com.jayhill.xlife.common.util.HealthTextUtils;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.EntityType;
- import net.minecraft.entity.LivingEntity;
- import net.minecraft.entity.ai.attributes.Attributes;
- import net.minecraft.entity.effect.LightningBoltEntity;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.entity.player.ServerPlayerEntity;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.Items;
- import net.minecraft.nbt.ListNBT;
- import net.minecraft.util.DamageSource;
- import net.minecraft.util.math.vector.Vector3d;
- import net.minecraft.util.text.TranslationTextComponent;
- import net.minecraft.world.GameType;
- import net.minecraftforge.common.util.LazyOptional;
- import net.minecraftforge.event.AttachCapabilitiesEvent;
- import net.minecraftforge.event.TickEvent;
- import net.minecraftforge.event.entity.living.LivingDeathEvent;
- import net.minecraftforge.event.entity.player.PlayerEvent.*;
- import net.minecraftforge.event.entity.player.PlayerInteractEvent;
- import net.minecraftforge.eventbus.api.SubscribeEvent;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.UUID;
- import static net.minecraft.util.text.TextFormatting.GRAY;
- /** Handles every event in X Life. */
- public class LifeEvents {
- public static List<Runnable> message = new ArrayList<>();
- private static String cause;
- private int tickCount;
- private int getTickCount() {
- return this.tickCount;
- }
- private void setTickCount(int count) {
- this.tickCount = count;
- }
- @SubscribeEvent
- public void onAttach(AttachCapabilitiesEvent<Entity> event) {
- if (event.getObject() instanceof PlayerEntity) {
- // Health
- HealthCapabilityProvider healthProvider = new HealthCapabilityProvider();
- event.addCapability(XLife.getId("health"), healthProvider);
- // Statistics
- StatsCapabilityProvider statsProvider = new StatsCapabilityProvider();
- event.addCapability(XLife.getId("stats"), statsProvider);
- // Time
- TimeCapabilityProvider timeProvider = new TimeCapabilityProvider();
- event.addCapability(XLife.getId("time"), timeProvider);
- }
- }
- @SubscribeEvent
- public void onJoin(PlayerLoggedInEvent event) {
- PlayerEntity player = event.getPlayer();
- player.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(health -> {
- if (health.getMaxHealth() == 0) {
- health.setMaxHealth(2.0F);
- player.setHealth(health.getMaxHealth());
- player.getAttribute(Attributes.MAX_HEALTH).setBaseValue(health.getMaxHealth());
- }
- });
- }
- @SubscribeEvent
- public void onDeath(LivingDeathEvent event) {
- DamageSource source = event.getSource();
- Entity sourceEntity = event.getSource().getEntity();
- if (event.getEntity() instanceof PlayerEntity) {
- PlayerEntity player = (PlayerEntity) event.getEntity();
- player.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(health -> player.getCapability(StatsCapability.STATS_CAPABILITY).ifPresent(stats -> player.getCapability(TimeCapability.TIME_CAPABILITY).ifPresent(time -> {
- int hearts = (int) ((health.getMaxHealth() - 2) / 2);
- if (health.getMaxHealth() >= 2.0F && health.getMaxHealth() <= 22.0F) {
- // Sets players new max health.
- health.setMaxHealth(health.getMaxHealth() + 2.0F);
- // Sets players cause of death.
- 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 = "Wither";
- } 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";
- }
- }
- // Sets players death statistics.
- String[] causeArray = stats.getCause();
- causeArray[hearts - 1] = cause;
- String[] messageArray = stats.getMessage();
- messageArray[hearts - 1] = player.getCombatTracker().getDeathMessage().getString();
- String[] timeArray = stats.getTime();
- if (time.getStoredTime() == 1) {
- timeArray[hearts - 1] = "1 second";
- } else if (time.getStoredTime() < 60) {
- timeArray[hearts - 1] = time.getStoredTime() + " seconds";
- } else if (time.getStoredTime() >= 60 && time.getStoredTime() < 120) {
- timeArray[hearts - 1] = "1 minute";
- } else if (time.getStoredTime() >= 120 && time.getStoredTime() < 3600) {
- timeArray[hearts - 1] = time.getStoredTime() / 60 + " minutes";
- } else if (time.getStoredTime() >= 3600 && time.getStoredTime() < 7200) {
- timeArray[hearts - 1] = "1 hour";
- } else {
- timeArray[hearts - 1] = time.getStoredTime() / 3600 + " hours";
- }
- stats.setCause(causeArray);
- stats.setMessage(messageArray);
- stats.setTime(timeArray);
- time.setStoredTime(0);
- // Sends a message for players remaining lives.
- HealthTextUtils.getRemainingLives(player);
- }
- if (health.getMaxHealth() == 22.0F) {
- LightningBoltEntity lightingEntity = EntityType.LIGHTNING_BOLT.create(player.level);
- if (lightingEntity != null) {
- lightingEntity.moveTo(Vector3d.atBottomCenterOf(player.blockPosition()));
- lightingEntity.setVisualOnly(true);
- player.level.addFreshEntity(lightingEntity);
- }
- }
- })));
- }
- }
- @SubscribeEvent
- public void onRespawn(PlayerRespawnEvent event) {
- PlayerEntity player = event.getPlayer();
- if (player.getServer() != null) {
- player.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(health -> player.getCapability(StatsCapability.STATS_CAPABILITY).ifPresent(stats -> {
- int lives = (int) (10 - ((health.getMaxHealth() - 2) / 2));
- int hearts = (int) (health.getMaxHealth() / 2) - 2;
- String[] timeArray = stats.getTime();
- if (health.getMaxHealth() >= 2.0F && health.getMaxHealth() <= 22.0F) {
- // Sets players new max health.
- player.setHealth(health.getMaxHealth());
- player.getAttribute(Attributes.MAX_HEALTH).setBaseValue(health.getMaxHealth());
- if (hearts < 10) {
- if (lives >= 2) {
- player.getServer().getCommands().performCommand(player.getServer().createCommandSourceStack().withSuppressedOutput(), "/title " + player.getScoreboardName() + " title \"" + lives + " lives remain\"");
- } else if (lives == 1) {
- player.getServer().getCommands().performCommand(player.getServer().createCommandSourceStack().withSuppressedOutput(), "/title " + player.getScoreboardName() + " title \"1 life remains\"");
- } else {
- player.getServer().getCommands().performCommand(player.getServer().createCommandSourceStack().withSuppressedOutput(), "/title " + player.getScoreboardName() + " title \"Eliminated\"");
- }
- player.getServer().getCommands().performCommand(player.getServer().createCommandSourceStack().withSuppressedOutput(), "/title " + player.getScoreboardName() + " subtitle \"You lasted " + timeArray[hearts] + "\"");
- }
- }
- if (health.getMaxHealth() == 22.0F) {
- player.setGameMode(GameType.SPECTATOR);
- }
- }));
- }
- }
- @SubscribeEvent
- public void onFormat(TabListNameFormat event) {
- PlayerEntity player = event.getPlayer();
- player.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(health -> {
- if (!player.isSpectator()) {
- if (health.getMaxHealth() <= 20.0F) {
- event.setDisplayName(new TranslationTextComponent(player.getScoreboardName() + " " + GRAY + "(" + HealthTextUtils.setHearts(health.getMaxHealth(), player.getHealth()) + GRAY + ")"));
- } else {
- event.setDisplayName(new TranslationTextComponent(player.getScoreboardName()));
- }
- }
- });
- }
- @SubscribeEvent
- public void onTick(TickEvent.PlayerTickEvent event) {
- PlayerEntity player = event.player;
- if (player.getServer() != null) {
- player.getCapability(TimeCapability.TIME_CAPABILITY).ifPresent(time -> {
- if (event.side.isServer()) {
- if (event.phase == TickEvent.Phase.START) {
- for (Runnable runnable : message) runnable.run(); {
- message.clear();
- }
- } else if (event.phase == TickEvent.Phase.END) {
- if (this.tickCount == 20) {
- time.setStoredTime(time.getStoredTime() + 1);
- this.setTickCount(0);
- } else {
- if (player.isAlive() && player.getServer().getPlayerList().getPlayer(player.getUUID()) != null) {
- this.setTickCount(this.getTickCount() + 1);
- }
- }
- }
- // Updates players name in tablist.
- for (ServerPlayerEntity players : player.getServer().getPlayerList().getPlayers()) {
- players.refreshTabListName();
- }
- }
- });
- }
- }
- @SubscribeEvent
- public void onUseBook(PlayerInteractEvent.RightClickItem event) {
- PlayerEntity player = event.getPlayer();
- ItemStack stack = event.getItemStack();
- ListNBT pages = new ListNBT();
- if (stack.getItem() == Items.WRITTEN_BOOK) {
- if (stack.getTag() != null && stack.getTag().hasUUID("owner")) {
- if (!player.level.isClientSide) {
- UUID uuid = stack.getTag().getUUID("owner");
- ServerPlayerEntity players = player.getServer().getPlayerList().getPlayer(uuid);
- if (players != null) {
- players.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(health -> {
- players.getCapability(TimeCapability.TIME_CAPABILITY).ifPresent(time -> {
- stack.getTag().put("pages", pages);
- stack.getTag().remove("resolved");
- if (health.getMaxHealth() >= 2.0F && health.getMaxHealth() <= 6.0F) {
- pages.add(HealthBookUtils.setPages(players, 1));
- } else if (health.getMaxHealth() >= 8.0F && health.getMaxHealth() <= 12.0F) {
- pages.add(HealthBookUtils.setPages(players, 1));
- pages.add(HealthBookUtils.setPages(players, 2));
- } else if (health.getMaxHealth() >= 14.0F && health.getMaxHealth() <= 18.0F) {
- pages.add(HealthBookUtils.setPages(players, 1));
- pages.add(HealthBookUtils.setPages(players, 2));
- pages.add(HealthBookUtils.setPages(players, 3));
- } else {
- pages.add(HealthBookUtils.setPages(players, 1));
- pages.add(HealthBookUtils.setPages(players, 2));
- pages.add(HealthBookUtils.setPages(players, 3));
- pages.add(HealthBookUtils.setPages(players, 4));
- }
- });
- });
- }
- }
- }
- }
- }
- @SubscribeEvent
- public void onClone(Clone event) {
- PlayerEntity player = event.getPlayer();
- // Health
- LazyOptional<IHealthCapability> healthCapability = event.getOriginal().getCapability(HealthCapability.HEALTH_CAPABILITY);
- healthCapability.ifPresent(oldStore -> player.getCapability(HealthCapability.HEALTH_CAPABILITY).ifPresent(newStore -> newStore.onClone((DefaultHealthCapability) oldStore)));
- // Statistics
- LazyOptional<IStatsCapability> statsCapability = event.getOriginal().getCapability(StatsCapability.STATS_CAPABILITY);
- statsCapability.ifPresent(oldStore -> player.getCapability(StatsCapability.STATS_CAPABILITY).ifPresent(newStore -> newStore.onClone((DefaultStatsCapability) oldStore)));
- // Time
- LazyOptional<ITimeCapability> timeCapability = event.getOriginal().getCapability(TimeCapability.TIME_CAPABILITY);
- timeCapability.ifPresent(oldStore -> player.getCapability(TimeCapability.TIME_CAPABILITY).ifPresent(newStore -> newStore.onClone((DefaultTimeCapability) oldStore)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement