001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.location;
020
021public enum Direction {
022    ALL(-1, "all"),
023    NORTH(0, "north"),
024    EAST(1, "east"),
025    SOUTH(2, "south"),
026    WEST(
027            3,
028            "west"
029    ),
030    NORTHEAST(4, "northeast"),
031    SOUTHEAST(5, "southeast"),
032    SOUTHWEST(
033            6,
034            "southwest"
035    ),
036    NORTHWEST(7, "northwest"),
037    ;
038
039
040    private final int index;
041    private final String name;
042
043    Direction(int index, String name) {
044
045        this.index = index;
046        this.name = name;
047    }
048
049    public static Direction getFromIndex(int index) {
050        for (Direction value : values()) {
051            if (value.getIndex() == index) {
052                return value;
053            }
054        }
055        return NORTH;
056    }
057
058    /**
059     * {@return the opposite direction}
060     * If this is {@link Direction#ALL}, then {@link Direction#ALL} is returned.
061     * @since 7.2.0
062     */
063    public Direction opposite() {
064        return switch (this) {
065            case ALL -> ALL;
066            case NORTH -> SOUTH;
067            case EAST -> WEST;
068            case SOUTH -> NORTH;
069            case WEST -> EAST;
070            case NORTHEAST -> SOUTHWEST;
071            case SOUTHEAST -> NORTHWEST;
072            case SOUTHWEST -> NORTHEAST;
073            case NORTHWEST -> SOUTHEAST;
074        };
075    }
076
077    public int getIndex() {
078        return index;
079    }
080
081    public String getName() {
082        return name;
083    }
084}