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; 022import org.checkerframework.checker.nullness.qual.Nullable; 023 024/** 025 * (x,y,z) or (x,z) representation for PlotSquared (hence the "Plot" prefix) 026 */ 027public final class PlotLoc { 028 029 private final int x; 030 private final int y; 031 private final int z; 032 033 /** 034 * Initialize a new {@link PlotLoc} and set the Y value to {@code -1} 035 * 036 * @param x X value 037 * @param z Z value 038 */ 039 public PlotLoc(final int x, final int z) { 040 this(x, -1, z); 041 } 042 043 public PlotLoc(int x, int y, int z) { 044 this.x = x; 045 this.y = y; 046 this.z = z; 047 } 048 049 public static @Nullable PlotLoc fromString(final String input) { 050 if (input == null || "side".equalsIgnoreCase(input)) { 051 return null; 052 } else if (StringMan.isEqualIgnoreCaseToAny(input, "center", "middle", "centre")) { 053 return new PlotLoc(Integer.MAX_VALUE, Integer.MAX_VALUE); 054 } else { 055 try { 056 String[] split = input.split(","); 057 if (split.length == 2) { 058 return new PlotLoc(Integer.parseInt(split[0]), Integer.parseInt(split[1])); 059 } else if (split.length == 3) { 060 return new PlotLoc(Integer.parseInt(split[0]), Integer.parseInt(split[1]), 061 Integer.parseInt(split[2]) 062 ); 063 } else { 064 throw new IllegalArgumentException( 065 String.format("Unable to deserialize: %s", input)); 066 } 067 } catch (NumberFormatException ignored) { 068 return null; 069 } 070 } 071 } 072 073 public int getX() { 074 return this.x; 075 } 076 077 public int getY() { 078 return this.y; 079 } 080 081 public int getZ() { 082 return this.z; 083 } 084 085 @Override 086 public int hashCode() { 087 final int prime = 31; 088 int result = 1; 089 result = (prime * result) + this.getX(); 090 result = (prime * result) + this.getY(); 091 result = (prime * result) + this.getZ(); 092 return result; 093 } 094 095 @Override 096 public String toString() { 097 if (this.getY() == -1) { 098 return String.format("%d,%d", x, z); 099 } 100 return String.format("%d,%d,%d", x, y, z); 101 } 102 103 @Override 104 public boolean equals(final Object obj) { 105 if (this == obj) { 106 return true; 107 } 108 if (obj == null || getClass() != obj.getClass()) { 109 return false; 110 } 111 final PlotLoc other = (PlotLoc) obj; 112 return (this.getX() == other.getX()) && (this.getY() == 113 other.getY()) && (this.getZ() == other.getZ()); 114 } 115 116}