|
| 1 | +package com.mairwunnx.projectessentials.home |
| 2 | + |
| 3 | +import com.mairwunnx.projectessentials.core.api.v1.commands.back.BackLocationAPI |
| 4 | +import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationAPI |
| 5 | +import com.mairwunnx.projectessentials.core.api.v1.extensions.playSound |
| 6 | +import com.mairwunnx.projectessentials.home.configurations.HomeConfiguration |
| 7 | +import com.mairwunnx.projectessentials.home.configurations.HomeConfigurationModel |
| 8 | +import com.mairwunnx.projectessentials.home.configurations.HomeSettingsConfiguration |
| 9 | +import net.minecraft.client.Minecraft |
| 10 | +import net.minecraft.entity.player.ServerPlayerEntity |
| 11 | +import net.minecraft.particles.ParticleTypes |
| 12 | +import net.minecraft.util.SoundEvents |
| 13 | +import net.minecraft.world.dimension.DimensionType |
| 14 | +import net.minecraftforge.api.distmarker.Dist |
| 15 | +import net.minecraftforge.fml.DistExecutor |
| 16 | +import kotlin.random.Random |
| 17 | + |
| 18 | +val homeConfiguration by lazy { |
| 19 | + ConfigurationAPI.getConfigurationByName<HomeConfiguration>("home").take() |
| 20 | +} |
| 21 | + |
| 22 | +val homeSettingsConfiguration by lazy { |
| 23 | + ConfigurationAPI.getConfigurationByName<HomeSettingsConfiguration>("home-settings").take() |
| 24 | +} |
| 25 | + |
| 26 | +fun teleportToHome(player: ServerPlayerEntity, home: HomeConfigurationModel.User.Home) { |
| 27 | + BackLocationAPI.commit(player) |
| 28 | + val world = player.server.getWorld( |
| 29 | + DimensionType.getById(home.dimensionId) ?: DimensionType.OVERWORLD |
| 30 | + ) |
| 31 | + player.teleport(world, home.xPos + 0.5, home.yPos + 0.5, home.zPos + 0.5, home.yaw, home.pitch) |
| 32 | + if (homeSettingsConfiguration.playSoundOnTeleport) { |
| 33 | + player.playSound(player, SoundEvents.ENTITY_ENDERMAN_TELEPORT) |
| 34 | + } |
| 35 | + if (homeSettingsConfiguration.showEffectOnTeleport) { |
| 36 | + DistExecutor.runWhenOn(Dist.CLIENT) { |
| 37 | + Runnable { |
| 38 | + for (i in 0..200) { |
| 39 | + Minecraft.getInstance().world.addParticle( |
| 40 | + ParticleTypes.PORTAL, |
| 41 | + player.positionVec.x + (random.nextDouble() - 0.5) * player.width.toDouble(), |
| 42 | + player.positionVec.y + random.nextDouble() * player.height.toDouble() - 0.25, |
| 43 | + player.positionVec.z + (random.nextDouble() - 0.5) * player.width.toDouble(), |
| 44 | + (random.nextDouble() - 0.5) * 2.0, |
| 45 | + -random.nextDouble(), |
| 46 | + (random.nextDouble() - 0.5) * 2.0 |
| 47 | + ) |
| 48 | + } |
| 49 | + if (player.world.isRemote) spawnServerParticles(player) |
| 50 | + } |
| 51 | + } |
| 52 | + DistExecutor.runWhenOn(Dist.DEDICATED_SERVER) { Runnable { spawnServerParticles(player) } } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +private val random = Random |
| 57 | +private fun spawnServerParticles(player: ServerPlayerEntity) { |
| 58 | + for (i in 0..200) { |
| 59 | + player.serverWorld.spawnParticle( |
| 60 | + ParticleTypes.PORTAL, |
| 61 | + player.posX + (random.nextDouble() - 0.5) * player.width.toDouble(), |
| 62 | + player.posY + random.nextDouble() * player.height.toDouble() - 0.25, |
| 63 | + player.posZ + (random.nextDouble() - 0.5) * player.width.toDouble(), |
| 64 | + 1, |
| 65 | + -0.006, -0.006, 0.0, |
| 66 | + (random.nextDouble() - 0.5) * 2.0 |
| 67 | + ) |
| 68 | + } |
| 69 | +} |
0 commit comments