// priority: 1

// by blueversal

Platform.mods.kubejs.name = 'Beyond Ocean';

StartupEvents.registry('item', (event) => {
    event.create('aquatica_teleporter')
        .useAnimation('bow')
        .rarity('rare')
        .maxStackSize(1)
        .use((level, player, hand) => true)
        .useDuration(() => 1)
        .finishUsing((itemstack, level, entity) => {
            if (!level.isClientSide() && entity.isPlayer()) {
                let player = entity;
                level.server.runCommandSilent(`execute in aquatica:eternal_deep run tp ${player.username} 0 120 0`);
                level.server.runCommandSilent(`effect give ${player.username} minecraft:water_breathing 120 0 true`);
                level.server.runCommandSilent(`effect give ${player.username} minecraft:night_vision 120 0 true`);
                level.server.runCommandSilent(`eta send ${player.username} 150 <anchor value=MIDDLE><scale value=2.0><bg color=#60000000><color value=#576b6b><glitch intensity=1.0 slices=3 chromatic=true><pulse speed=1.5><neon r=2 intensity=1.5>Aquatica?..</neon></pulse></glitch></color></bg></scale></anchor>`);
                level.server.runCommandSilent(`give ${player.username} kubejs:overworld_teleporter 1`);
                itemstack.shrink(1);
            }
            return itemstack;
        })
        .displayName('Aquatica?..')
        .tooltip('Takes you somewhere unknown...');

    event.create('overworld_teleporter')
        .useAnimation('bow')
        .rarity('uncommon')
        .maxStackSize(1)
        .use((level, player, hand) => true)
        .useDuration(() => 1)
        .finishUsing((itemstack, level, entity) => {
            if (!level.isClientSide() && entity.isPlayer()) {
                let player = entity;
                let spawnPos = player.getRespawnPosition();
                let spawnDim = player.getRespawnDimension().location();
                let targetX, targetY, targetZ, targetDim;
                if (spawnPos != null) {
                    targetX = spawnPos.x;
                    targetY = spawnPos.y;
                    targetZ = spawnPos.z;
                    targetDim = spawnDim;
                } else if (level.spawnPoint != null) {
                    targetX = level.spawnPoint.x;
                    targetY = level.spawnPoint.y;
                    targetZ = level.spawnPoint.z;
                    targetDim = 'minecraft:overworld';
                } else {
                    targetX = 0;
                    targetY = 200;
                    targetZ = 0;
                    targetDim = 'minecraft:overworld';
                }
                level.server.runCommandSilent(`execute in ${targetDim} run tp ${player.username} ${targetX} ${targetY} ${targetZ}`);
                level.server.runCommandSilent(`eta send ${player.username} 100 <anchor value=MIDDLE><scale value=1.5><bg color=#60000000><color value=#00aaff>Back...</color></bg></scale></anchor>`);
                itemstack.shrink(1);
            }
            return itemstack;
        })
        .displayName('Return?')
        .tooltip('Go back...');
});