summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/command/location/util/LocationUtils.java
blob: e0fc033f54390827776f7681cef272df2307c352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package net.tylermurphy.hideAndSeek.command.location.util;

import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.game.Game;
import net.tylermurphy.hideAndSeek.game.util.Status;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.Nullable;

import java.util.function.Consumer;

import static net.tylermurphy.hideAndSeek.configuration.Config.*;
import static net.tylermurphy.hideAndSeek.configuration.Localization.message;

public class LocationUtils {

    /**
     * Provides a vector for a player
     * @param player the player to create the vector for
     * @return the vector
     */
    private static @Nullable Vector vector(Player player) {
        if (Main.getInstance().getGame().getStatus() != Status.STANDBY) {
            player.sendMessage(errorPrefix + message("GAME_INPROGRESS"));
            return null;
        }

        if (player.getLocation().getBlockX() == 0 || player.getLocation().getBlockZ() == 0 || player.getLocation().getBlockY() == 0){
            player.sendMessage(errorPrefix + message("NOT_AT_ZERO"));
            return null;
        }

        Location loc = player.getLocation();
        return new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
    }

    public static void setLocation(Player player, Locations place, @Nullable Consumer<Vector> consumer) {
        Vector vec = vector(player);

        World world = player.getLocation().getWorld();
        if(world == null) {
            throw new RuntimeException("Unable to get world: " + spawnWorld);
        }

        consumer.accept(vec);

        player.sendMessage(messagePrefix + message(place.message()));
        addToConfig(place.path("x"), vec.getX());
        addToConfig(place.path("y"), vec.getY());
        addToConfig(place.path("z"), vec.getZ());
        addToConfig(place.path("world"), player.getLocation().getWorld().getName());
        saveConfig();
    }

}