// Seguridad global: los jugadores conservan dano por caida; los mobs no.
function cunisafetyEntityTypeId(entity) {
  if (!entity) {
    return '';
  }
  try { if (entity.type) { return String(entity.type); } } catch (ignoredType) { }
  try { if (entity.typeId) { return String(entity.typeId); } } catch (ignoredTypeId) { }
  try { if (entity.getType) { return String(entity.getType()); } } catch (ignoredGetType) { }
  return '';
}

function cunisafetyIsPlayer(entity) {
  if (!entity) {
    return false;
  }
  try { if (entity.isPlayer && entity.isPlayer()) { return true; } } catch (ignoredIsPlayer) { }
  try { if (entity.player) { return true; } } catch (ignoredPlayer) { }
  return cunisafetyEntityTypeId(entity) === 'minecraft:player';
}

function cunisafetyDamageSourceText(event) {
  var source = null;
  var values = [];
  try { source = event.source; } catch (ignoredSource) { }
  try { if (!source && event.getSource) { source = event.getSource(); } } catch (ignoredGetSource) { }
  if (source) {
    try { if (source.is && source.is('minecraft:fall')) { values.push('minecraft:fall'); } } catch (ignoredIsFall) { }
    try { if (source.is && source.is('#minecraft:is_fall')) { values.push('minecraft:fall'); } } catch (ignoredIsFallTag) { }
    try { values.push(source); } catch (ignoredValue) { }
    try { values.push(source.type); } catch (ignoredTypeField) { }
    try { if (source.type) { values.push(source.type()); } } catch (ignoredTypeCall) { }
    try { values.push(source.msgId); } catch (ignoredMsgField) { }
    try { if (source.getMsgId) { values.push(source.getMsgId()); } } catch (ignoredMsgCall) { }
    try { if (source.getType) { values.push(source.getType()); } } catch (ignoredGetType) { }
    try { if (source.type && source.type().msgId) { values.push(source.type().msgId()); } } catch (ignoredNestedMsg) { }
    try { if (source.getType && source.getType().msgId) { values.push(source.getType().msgId()); } } catch (ignoredNestedGetMsg) { }
  }
  return values.join(' ').toLowerCase();
}

function cunisafetyIsFallDamage(event) {
  var text = cunisafetyDamageSourceText(event);
  return text.indexOf('minecraft:fall') >= 0
    || text.indexOf('damage_type:fall') >= 0
    || text.indexOf('msgid=fall') >= 0
    || text === 'fall'
    || /\bfall\b/.test(text);
}

EntityEvents.beforeHurt(function (event) {
  var entity = event.entity;
  if (!entity || cunisafetyIsPlayer(entity)) {
    return;
  }
  if (cunisafetyIsFallDamage(event)) {
    try { event.cancel(); } catch (ignoredCancel) { }
  }
});
