events.listen('entity.death', function (event) {
    if(event.entity.isPlayer()) {
      console.info('Player Died...');
      event.server.scheduleInTicks(5, event, schedule => {
        var players = schedule.data.server.players.filter( (entity) => {
          return entity.profile.id == schedule.data.entity.profile.id;
        });
        if(!players.isEmpty()){
          if(players.getFirst().isAlive()){
            console.info('Player Is alive. Running Commands.');
            schedule.data.server.runCommandSilent('effect give ' + players.getFirst().name + ' minecraft:resistance 60 4 true');
            schedule.data.server.runCommandSilent('effect give ' + players.getFirst().name + ' quark:resilience 60 5 true');
			schedule.data.server.runCommandSilent('effect give ' + players.getFirst().name + ' minecraft:invisibility 60 5 true');
			schedule.data.server.runCommandSilent('tellraw ' + players.getFirst().name + ' ["",{"text":"You have been granted ","color":"yellow"},{"text":"Death Protection","color":"dark_red"},{"text":" for a short time!","color":"yellow"}]');
            console.info('Player has been given effects.');
			schedule.data.server.runCommandSilent('tellraw ' + players.getFirst().name + ' ["",{"text":"A ","color":"green"},{"text":"Totem Of Holding","color":"yellow"},{"text":" has appeared at your ","color":"green"},{"text":"death point","color":"red"},{"text":".","color":"green"},{"text":"\n"},{"text":"Use ","color":"green"},{"text":"Satan\'s Contract","color":"red"},{"text":" or do ","color":"green"},{"text":"/back","color":"red"},{"text":" if available.","color":"green"}]');
            console.info('Player has been informed.');
          }else{
            console.info('Player was not alive to receive effects. Rescheudling...');
            schedule.reschedule();
          }
        }else{
          console.info('Player UUID could not be found in player list. Rescheduling...');
          console.info("UUID ("+schedule.data.entity.profile.id+")");
          schedule.reschedule();
        }
      });
    }
});



