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
021import com.plotsquared.core.util.StringMan;
022
023public class BlockLoc {
024
025    public static final BlockLoc ZERO = new BlockLoc(0, 0, 0);
026    public static final BlockLoc MINY = new BlockLoc(0, Integer.MIN_VALUE, 0);
027    private static final BlockLoc MIDDLE = new BlockLoc(Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
028
029    private final int x;
030    private final int y;
031    private final int z;
032
033    private final float yaw;
034    private final float pitch;
035
036    public BlockLoc(int x, int y, int z) {
037        this(x, y, z, 0f, 0f);
038    }
039
040    public BlockLoc(int x, int y, int z, float yaw, float pitch) {
041        this.x = x;
042        this.y = y;
043        this.z = z;
044
045        this.yaw = yaw;
046        this.pitch = pitch;
047    }
048
049    public static BlockLoc fromString(String string) {
050        if (string == null || "side".equalsIgnoreCase(string)) {
051            return null;
052        } else if (StringMan.isEqualIgnoreCaseToAny(string, "center", "middle", "centre")) {
053            return MIDDLE;
054        } else {
055            String[] parts = string.split(",");
056
057            float yaw;
058            float pitch;
059            if (parts.length == 5) {
060                yaw = Float.parseFloat(parts[3]);
061                pitch = Float.parseFloat(parts[4]);
062            } else if (parts.length == 3) {
063                yaw = 0;
064                pitch = 0;
065            } else {
066                return ZERO;
067            }
068            int x = Integer.parseInt(parts[0]);
069            int y = Integer.parseInt(parts[1]);
070            int z = Integer.parseInt(parts[2]);
071
072            return new BlockLoc(x, y, z, yaw, pitch);
073        }
074    }
075
076    @Override
077    public int hashCode() {
078        int prime = 31;
079        int result = 1;
080        result = prime * result + this.getX();
081        result = prime * result + this.getY();
082        result = prime * result + this.getZ();
083        return result;
084    }
085
086    @Override
087    public boolean equals(Object obj) {
088        if (this == obj) {
089            return true;
090        }
091        if (obj == null) {
092            return this.getX() == 0 && this.getY() == 0 && this.getZ() == 0;
093        }
094        if (getClass() != obj.getClass()) {
095            return false;
096        }
097        BlockLoc other = (BlockLoc) obj;
098        return this.getX() == other.getX() && this.getY() == other.getY() && this.getZ() == other
099                .getZ() && this.getYaw() == other.getYaw() && this.getPitch() == other.getPitch();
100    }
101
102    @Override
103    public String toString() {
104        if (this.getX() == 0 && this.getY() == 0 && this.getZ() == 0 && this.getYaw() == 0 && this.getPitch() == 0) {
105            return "";
106        }
107        return this.getX() + "," + this.getY() + ',' + this.getZ() + ',' + this.getYaw() + ','
108                + this.getPitch();
109
110    }
111
112    public int getX() {
113        return x;
114    }
115
116    public int getY() {
117        return y;
118    }
119
120    public int getZ() {
121        return z;
122    }
123
124    public float getYaw() {
125        return yaw;
126    }
127
128    public float getPitch() {
129        return pitch;
130    }
131
132}