var CUNICLEAN_INTERVAL_TICKS = 20 * 60 * 10;
var CUNICLEAN_WARN_ONE_MINUTE = 20 * 60 * 9;
var CUNICLEAN_WARN_TEN_SECONDS = CUNICLEAN_INTERVAL_TICKS - 20 * 10;
var CUNICLEAN_TICK = 0;
var CUNICLEAN_DIMENSIONS = [
  'minecraft:overworld',
  'minecraft:the_nether',
  'minecraft:the_end',
  'cuniculum:spawn_void'
];
var CUNICLEAN_JUNK_ITEMS = [
  'minecraft:white_carpet',
  'minecraft:orange_carpet',
  'minecraft:magenta_carpet',
  'minecraft:light_blue_carpet',
  'minecraft:yellow_carpet',
  'minecraft:lime_carpet',
  'minecraft:pink_carpet',
  'minecraft:gray_carpet',
  'minecraft:light_gray_carpet',
  'minecraft:cyan_carpet',
  'minecraft:purple_carpet',
  'minecraft:blue_carpet',
  'minecraft:brown_carpet',
  'minecraft:green_carpet',
  'minecraft:red_carpet',
  'minecraft:black_carpet',
  'minecraft:dirt',
  'minecraft:coarse_dirt',
  'minecraft:rooted_dirt',
  'minecraft:cobblestone',
  'minecraft:cobbled_deepslate',
  'minecraft:netherrack',
  'minecraft:gravel',
  'minecraft:sand',
  'minecraft:red_sand',
  'minecraft:granite',
  'minecraft:diorite',
  'minecraft:andesite',
  'minecraft:tuff',
  'minecraft:deepslate',
  'minecraft:stone',
  'minecraft:oak_leaves',
  'minecraft:spruce_leaves',
  'minecraft:birch_leaves',
  'minecraft:jungle_leaves',
  'minecraft:acacia_leaves',
  'minecraft:dark_oak_leaves',
  'minecraft:mangrove_leaves',
  'minecraft:cherry_leaves',
  'minecraft:rotten_flesh',
  'minecraft:bone',
  'minecraft:string',
  'minecraft:gunpowder',
  'minecraft:spider_eye'
];

function cunicleanTellAll(server, text, color) {
  if (!server) {
    return;
  }
  server.runCommandSilent('tellraw @a ' + JSON.stringify({
    text: '[CUNICULUM] ' + text,
    color: color || 'gold',
    italic: false
  }));
}

function cunicleanClearItems(server) {
  if (!server) {
    return;
  }
  for (var i = 0; i < CUNICLEAN_DIMENSIONS.length; i++) {
    for (var j = 0; j < CUNICLEAN_JUNK_ITEMS.length; j++) {
      server.runCommandSilent('execute in ' + CUNICLEAN_DIMENSIONS[i] + ' run kill @e[type=minecraft:item,nbt={Item:{id:"' + CUNICLEAN_JUNK_ITEMS[j] + '"}}]');
    }
  }
  cunicleanTellAll(server, 'Se limpio basura comun tirada del mundo. Armas, armaduras, minerales, reliquias y mochilas se conservan.', 'green');
}

ServerEvents.tick(function (event) {
  var server = event.server;
  if (!server) {
    return;
  }

  CUNICLEAN_TICK++;
  if (CUNICLEAN_TICK === CUNICLEAN_WARN_ONE_MINUTE) {
    cunicleanTellAll(server, 'Limpieza de objetos tirados en 60 segundos.', 'yellow');
  } else if (CUNICLEAN_TICK === CUNICLEAN_WARN_TEN_SECONDS) {
    cunicleanTellAll(server, 'Limpieza de objetos tirados en 10 segundos.', 'red');
  } else if (CUNICLEAN_TICK >= CUNICLEAN_INTERVAL_TICKS) {
    cunicleanClearItems(server);
    CUNICLEAN_TICK = 0;
  }
});
