ServerEvents.loaded(event => {
  if (event.server.persistentData.gameRules) return

  event.server.gameRules.set("reducedDebugInfo", true)
  event.server.gameRules.set("announceAdvancements", false)
  event.server.gameRules.set("doInsomnia", false)
  event.server.gameRules.set("showDeathMessages", false)
  event.server.gameRules.set("mobGriefing", false)
  event.server.gameRules.set("doTraderSpawning", false)

  event.server.runCommandSilent(`forceload add 31 32 0 63`)
  event.server.runCommandSilent(`team add Guards`)
  event.server.runCommandSilent(`team modify Guards friendlyFire false`)

  event.server.persistentData.gameRules = true

});

EntityEvents.spawned(event => {
  let entity = event.entity;
  if (!entity.isPlayer()) return;

  let maxMana = entity.getAttribute("irons_spellbooks:max_mana");
  let rollCount = entity.getAttribute("combatroll:count");

  if (maxMana && maxMana.baseValue > 0) {
    maxMana.baseValue = 0;
  }

  if (rollCount && rollCount.baseValue <= 1) {
    rollCount.baseValue = 3;
  }
});

const anchorPos = { x: 0, y: -58, z: 28 };

BlockEvents.rightClicked(event => {
  const { block, item } = event;
  const pos = block?.pos;

  if (!block || block.id !== 'minecraft:respawn_anchor') return;
  if (pos.x !== anchorPos.x || pos.y !== anchorPos.y || pos.z !== anchorPos.z) return;

  if (!item || item.id !== 'minecraft:glass_bottle') {
    event.cancel();
    return;
  }

  event.server.runCommandSilent(`setblock 0 -58 28 minecraft:respawn_anchor[charges=4]`);
});