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
|
|
|
|
|
|
|
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;
|
2015-02-23 02:32:27 +01: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
|
|
|
*/
|
|
|
|
public PlotId(final int x, final int y) {
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-16 10:48:18 +01:00
|
|
|
/**
|
|
|
|
* Get a Plot Id based on a string
|
|
|
|
*
|
2014-12-18 03:15:11 +01:00
|
|
|
* @param string to create id from
|
|
|
|
*
|
2014-11-16 10:48:18 +01:00
|
|
|
* @return null if the string is invalid
|
|
|
|
*/
|
|
|
|
public static PlotId fromString(final String string) {
|
|
|
|
int x, y;
|
2014-12-16 06:03:20 +01:00
|
|
|
final String[] parts = string.split(";");
|
|
|
|
if (parts.length < 2) {
|
2014-11-16 10:48:18 +01:00
|
|
|
return null;
|
2014-12-16 06:03:20 +01:00
|
|
|
}
|
2014-11-16 10:48:18 +01:00
|
|
|
try {
|
|
|
|
x = Integer.parseInt(parts[0]);
|
|
|
|
y = Integer.parseInt(parts[1]);
|
2014-12-18 03:15:11 +01:00
|
|
|
} catch (final Exception e) {
|
2014-11-16 10:48:18 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new PlotId(x, y);
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
@Override
|
|
|
|
public boolean equals(final Object obj) {
|
|
|
|
if (this == obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (obj == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (getClass() != obj.getClass()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final PlotId other = (PlotId) obj;
|
2014-11-11 03:23:45 +01:00
|
|
|
return ((this.x.equals(other.x)) && (this.y.equals(other.y)));
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return this.x + ";" + this.y;
|
|
|
|
}
|
2015-03-26 11:44:31 +01:00
|
|
|
|
2015-04-06 14:16:24 +02:00
|
|
|
public static PlotId unpair(int hash) {
|
2015-03-26 11:44:31 +01:00
|
|
|
if (hash >= 0) {
|
|
|
|
if (hash % 2 == 0) {
|
|
|
|
// + +
|
|
|
|
hash /= 2;
|
|
|
|
int i = (int) (Math.abs(-1 + Math.sqrt(1 + 8 * hash)) / 2);
|
|
|
|
int idx = hash - ((i * (1 + i)) / 2);
|
|
|
|
int idy = ((i * (3 + i)) / 2) - hash;
|
|
|
|
return new PlotId(idx, idy);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// + -
|
|
|
|
hash -= 1;
|
|
|
|
hash /= 2;
|
|
|
|
int i = (int) (Math.abs(-1 + Math.sqrt(1 + 8 * hash)) / 2);
|
|
|
|
int idx = hash - ((i * (1 + i)) / 2);
|
|
|
|
int idy = ((i * (3 + i)) / 2) - hash;
|
|
|
|
return new PlotId(idx, -idy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (hash % 2 == 0) {
|
|
|
|
// - +
|
|
|
|
hash /= -2;
|
|
|
|
int i = (int) (Math.abs(-1 + Math.sqrt(1 + 8 * hash)) / 2);
|
|
|
|
int idx = hash - ((i * (1 + i)) / 2);
|
|
|
|
int idy = ((i * (3 + i)) / 2) - hash;
|
|
|
|
return new PlotId(-idx, idy);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// - -
|
|
|
|
hash += 1;
|
|
|
|
hash /= -2;
|
|
|
|
int i = (int) (Math.abs(-1 + Math.sqrt(1 + 8 * hash)) / 2);
|
|
|
|
int idx = hash - ((i * (1 + i)) / 2);
|
|
|
|
int idy = ((i * (3 + i)) / 2) - hash;
|
|
|
|
return new PlotId(-idx, -idy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-04-27 11:07:42 +02:00
|
|
|
private int hash;
|
|
|
|
|
2015-06-08 18:01:09 +02:00
|
|
|
public void recalculateHash() {
|
|
|
|
this.hash = 0;
|
|
|
|
hashCode();
|
|
|
|
}
|
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2015-04-27 11:07:42 +02:00
|
|
|
if (hash == 0) {
|
|
|
|
if (x >= 0) {
|
|
|
|
if (y >= 0) {
|
|
|
|
hash = (x * x) + (3 * x) + (2 * x * y) + y + (y * y);
|
|
|
|
} else {
|
|
|
|
final int y1 = -y;
|
|
|
|
hash = (x * x) + (3 * x) + (2 * x * y1) + y1 + (y1 * y1) + 1;
|
|
|
|
}
|
2014-12-18 03:15:11 +01:00
|
|
|
} else {
|
2015-04-27 11:07:42 +02:00
|
|
|
final int x1 = -x;
|
|
|
|
if (y >= 0) {
|
|
|
|
hash = -((x1 * x1) + (3 * x1) + (2 * x1 * y) + y + (y * y));
|
|
|
|
} else {
|
|
|
|
final int y1 = -y;
|
|
|
|
hash = -((x1 * x1) + (3 * x1) + (2 * x1 * y1) + y1 + (y1 * y1) + 1);
|
|
|
|
}
|
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
|
|
|
}
|