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

176 lines
5.9 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-11 12:09:22 +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
*/
2015-09-11 12:09:22 +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-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
*/
2015-09-11 12:09:22 +02:00
public static PlotId fromString(final String string)
{
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-11 12:09:22 +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-11 12:09:22 +02: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
2015-09-11 12:09:22 +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-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
@Override
2015-09-11 12:09:22 +02:00
public String toString()
{
return x + ";" + y;
2014-11-05 04:42:08 +01:00
}
2015-09-11 12:09:22 +02:00
public static PlotId unpair(int hash)
{
if (hash >= 0)
{
if ((hash % 2) == 0)
{
2015-03-26 11:44:31 +01:00
// + +
hash /= 2;
2015-09-11 12:09:22 +02:00
final int i = (int) (Math.abs(-1 + Math.sqrt(1 + (8 * hash))) / 2);
final int idx = hash - ((i * (1 + i)) / 2);
final int idy = ((i * (3 + i)) / 2) - hash;
2015-03-26 11:44:31 +01:00
return new PlotId(idx, idy);
}
2015-09-11 12:09:22 +02:00
else
{
2015-03-26 11:44:31 +01:00
// + -
hash -= 1;
hash /= 2;
2015-09-11 12:09:22 +02:00
final int i = (int) (Math.abs(-1 + Math.sqrt(1 + (8 * hash))) / 2);
final int idx = hash - ((i * (1 + i)) / 2);
final int idy = ((i * (3 + i)) / 2) - hash;
2015-03-26 11:44:31 +01:00
return new PlotId(idx, -idy);
}
}
2015-09-11 12:09:22 +02:00
else
{
if ((hash % 2) == 0)
{
2015-03-26 11:44:31 +01:00
// - +
hash /= -2;
2015-09-11 12:09:22 +02:00
final int i = (int) (Math.abs(-1 + Math.sqrt(1 + (8 * hash))) / 2);
final int idx = hash - ((i * (1 + i)) / 2);
final int idy = ((i * (3 + i)) / 2) - hash;
2015-03-26 11:44:31 +01:00
return new PlotId(-idx, idy);
}
2015-09-11 12:09:22 +02:00
else
{
2015-03-26 11:44:31 +01:00
// - -
hash += 1;
hash /= -2;
2015-09-11 12:09:22 +02:00
final int i = (int) (Math.abs(-1 + Math.sqrt(1 + (8 * hash))) / 2);
final int idx = hash - ((i * (1 + i)) / 2);
final int idy = ((i * (3 + i)) / 2) - hash;
2015-03-26 11:44:31 +01:00
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-09-11 12:09:22 +02:00
public void recalculateHash()
{
hash = 0;
hashCode();
}
2015-09-11 12:09:22 +02:00
2014-11-05 04:42:08 +01:00
@Override
2015-09-11 12:09:22 +02:00
public int hashCode()
{
if (hash == 0)
{
if (x >= 0)
{
if (y >= 0)
{
2015-04-27 11:07:42 +02:00
hash = (x * x) + (3 * x) + (2 * x * y) + y + (y * y);
2015-09-11 12:09:22 +02:00
}
else
{
2015-04-27 11:07:42 +02:00
final int y1 = -y;
hash = (x * x) + (3 * x) + (2 * x * y1) + y1 + (y1 * y1) + 1;
}
2015-09-11 12:09:22 +02:00
}
else
{
2015-04-27 11:07:42 +02:00
final int x1 = -x;
2015-09-11 12:09:22 +02:00
if (y >= 0)
{
2015-04-27 11:07:42 +02:00
hash = -((x1 * x1) + (3 * x1) + (2 * x1 * y) + y + (y * y));
2015-09-11 12:09:22 +02:00
}
else
{
2015-04-27 11:07:42 +02:00
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
}