function random(ary){
    return ary[Math.floor(Math.random()*ary.length)];
}

function cursedSummon(event, entityData) {
var pUUID = event.player.getFullNBT().UUID;

  event.server.runCommand(`execute as ${event.player.name} in ${event.player.world.dimension} run summon gateways:small_gateway ${Math.round(event.player.x)} ${Math.round(event.player.y)+1} ${Math.round(event.player.z)} {Motion: [0.0d, 0.0d, 0.0d], max_wave_time: 600, completion_xp: 150, color: "${entityData.colorRing}", active: 0b, Invulnerable: 0b, Air: 300s, OnGround: 0b, PortalCooldown: 0, Rotation: [0.0f, 0.0f], wave: 0b, FallDistance: 0.0f, wave_entities: [L;], queued_stacks: [], CanUpdate: 1b, Fire: -1s, name: "${entityData.gateName}", summoner: ${pUUID}, ticks_active: 0, entity: {Entity: {id: "${entityData.entityName}"}, Weight: 1}}`);
}

var entityList = [
{gateName: 'gateways.small_deathtome_gate', entityName: 'twilightforest:death_tome', colorRing:'white'},
{gateName: 'gateways.small_kobold_gate', entityName: 'twilightforest:kobold', colorRing:'green'},
{gateName: 'gateways.small_looter_gate', entityName: 'farlanders:looter', colorRing:'black'},
{gateName: 'gateways.small_skassassin_gate', entityName: 'mana-and-artifice:skeleton_assassin', colorRing:'white'},
{gateName: 'gateways.small_hzombie_gate', entityName: 'mana-and-artifice:hulking_zombie', colorRing:'green'},
{gateName: 'gateways.small_player_gate', entityName: 'player_mobs:player_mob', colorRing:'green'},

{gateName: 'gateways.small_zombie_gate', entityName: 'minecraft:zombie', colorRing:'green'},
{gateName: 'gateways.small_creeper_gate', entityName: 'minecraft:creeper', colorRing:'green'},
{gateName: 'gateways.small_enderman_gate', entityName: 'minecraft:enderman', colorRing:'green'},
{gateName: 'gateways.small_skeleton_gate', entityName: 'minecraft:skeleton', colorRing:'white'},
{gateName: 'gateways.small_spider_gate', entityName: 'minecraft:spider', colorRing:'red'},
{gateName: 'gateways.small_witch_gate', entityName: 'minecraft:witch', colorRing:'purple'},
{gateName: 'gateways.small_slime_gate', entityName: 'minecraft:slime', colorRing:'green'}
];

onEvent('item.right_click', event => {

  if (event.item.id == 'kubejs:essence_cursed') {
	  if (!event.world.isDaytime()) {

	event.player.playSound('blue_skies:entity.alchemist.prepare_conversion');
	event.server.scheduleInTicks(1, cb => {
	
	cursedSummon(event, random(entityList))
	event.server.runCommandSilent(`execute as ${event.player.name} in ${event.player.world.dimension} run apothboss ${Math.round(event.player.x)} ${Math.round(event.player.y)+6} ${Math.round(event.player.z)}`);
	})
      
    if (!event.player.isCreativeMode()) {
    
    event.item.setCount(event.item.getCount() - 1)
		}
	  }
	else {	
	event.player.tell(text.red("It\'s not dark enough outside..."))
	}
    }
});