PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotId.java

125 lines
4.4 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-11-16 10:48:18 +01:00
package com.intellectualcrafters.plot.object;
2014-09-22 13:02:14 +02:00
2015-09-13 06:04:31 +02:00
public class PlotId {
2014-11-05 04:42:08 +01:00
/**
* x value
*/
2015-02-15 08:40:55 +01:00
public Integer x;
2014-11-05 04:42:08 +01:00
/**
* y value
*/
2015-02-15 08:40:55 +01:00
public Integer y;
private int hash;
2015-09-13 06:04:31 +02:00
2014-11-05 04:42:08 +01:00
/**
* PlotId class (PlotId x,y values do not correspond to Block locations)
*
2014-12-18 03:15:11 +01:00
* @param x The plot x coordinate
* @param y The plot y coordinate
2014-11-05 04:42:08 +01:00
*/
2015-09-13 06:04:31 +02:00
public PlotId(final int x, final int y) {
2014-11-05 04:42:08 +01:00
this.x = x;
this.y = y;
}
2015-09-13 06:04:31 +02:00
2014-11-16 10:48:18 +01:00
/**
* Get a Plot Id based on a string
*
* @param string to create type from
2014-12-18 03:15:11 +01:00
*
2014-11-16 10:48:18 +01:00
* @return null if the string is invalid
*/
2015-09-13 06:04:31 +02:00
public static PlotId fromString(final String string) {
2016-02-10 19:59:51 +01:00
if (string == null) {
return null;
}
2014-11-16 10:48:18 +01:00
int x, y;
2014-12-16 06:03:20 +01:00
final String[] parts = string.split(";");
2015-09-13 06:04:31 +02:00
if (parts.length < 2) {
return null;
}
try {
2014-11-16 10:48:18 +01:00
x = Integer.parseInt(parts[0]);
y = Integer.parseInt(parts[1]);
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {
2014-11-16 10:48:18 +01:00
return null;
}
return new PlotId(x, y);
}
public static PlotId unpair(int hash) {
return new PlotId(hash >> 16, hash & 0xFFFF);
}
2016-02-10 19:59:51 +01:00
public PlotId getRelative(final int direction) {
switch (direction) {
case 0:
return new PlotId(this.x, this.y - 1);
case 1:
return new PlotId(this.x + 1, this.y);
case 2:
return new PlotId(this.x, this.y + 1);
case 3:
return new PlotId(this.x - 1, this.y);
}
return this;
}
public PlotId getRelative(int x, int y) {
return new PlotId(this.x + x, this.y + y);
}
2014-11-05 04:42:08 +01:00
@Override
2015-09-13 06:04:31 +02:00
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
2014-11-05 04:42:08 +01:00
final PlotId other = (PlotId) obj;
2015-09-11 12:09:22 +02:00
return ((x.equals(other.x)) && (y.equals(other.y)));
2014-11-05 04:42:08 +01:00
}
2015-09-13 06:04:31 +02:00
2014-11-05 04:42:08 +01:00
@Override
2015-09-13 06:04:31 +02:00
public String toString() {
2015-09-11 12:09:22 +02:00
return x + ";" + y;
2014-11-05 04:42:08 +01:00
}
2015-09-13 06:04:31 +02:00
public void recalculateHash() {
2015-09-11 12:09:22 +02:00
hash = 0;
hashCode();
}
2015-09-13 06:04:31 +02:00
2014-11-05 04:42:08 +01:00
@Override
2015-09-13 06:04:31 +02:00
public int hashCode() {
if (hash == 0) {
hash = (x << 16) | (y & 0xFFFF);
2014-11-07 12:15:19 +01:00
}
2015-04-27 11:07:42 +02:00
return hash;
2014-11-05 04:42:08 +01:00
}
2014-09-22 13:02:14 +02:00
}