Applied-Energistics-2-tiler.../src/api/java/appeng/api/util/WorldCoord.java

188 lines
4.2 KiB
Java
Raw Normal View History

2014-12-13 01:19:51 +01:00
/*
* The MIT License (MIT)
2015-02-03 12:04:13 +01:00
*
2014-12-13 01:19:51 +01:00
* Copyright (c) 2013 AlgorithmX2
2015-02-03 12:04:13 +01:00
*
2014-12-13 01:19:51 +01:00
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
2015-02-03 12:04:13 +01:00
*
2014-12-13 01:19:51 +01:00
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
2015-02-03 12:04:13 +01:00
*
2014-12-13 01:19:51 +01:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package appeng.api.util;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
/**
* Represents a relative coordinate, either relative to another object, or
* relative to the origin of a dimension.
*/
public class WorldCoord
{
public int x;
public int y;
public int z;
2015-09-30 14:24:40 +02:00
public WorldCoord( final TileEntity s )
{
2015-06-16 02:44:59 +02:00
this( s.getPos() );
}
2015-09-30 14:24:40 +02:00
public WorldCoord( final int _x, final int _y, final int _z )
{
this.x = _x;
this.y = _y;
this.z = _z;
}
public WorldCoord( final BlockPos pos )
{
2015-09-30 14:26:54 +02:00
this.x = pos.getX();
this.y = pos.getY();
this.z = pos.getZ();
2015-06-16 02:44:59 +02:00
}
2015-09-30 14:24:40 +02:00
public WorldCoord subtract( final AEPartLocation direction, final int length )
2015-06-16 02:44:59 +02:00
{
this.x -= direction.xOffset * length;
this.y -= direction.yOffset * length;
this.z -= direction.zOffset * length;
return this;
}
2015-09-30 14:24:40 +02:00
public WorldCoord add( final int _x, final int _y, final int _z )
{
2015-01-01 21:17:00 +01:00
this.x += _x;
this.y += _y;
this.z += _z;
return this;
}
2015-09-30 14:24:40 +02:00
public WorldCoord subtract( final int _x, final int _y, final int _z )
{
2015-01-01 21:17:00 +01:00
this.x -= _x;
this.y -= _y;
this.z -= _z;
return this;
}
2015-09-30 14:24:40 +02:00
public WorldCoord multiple( final int _x, final int _y, final int _z )
{
2015-01-01 21:17:00 +01:00
this.x *= _x;
this.y *= _y;
this.z *= _z;
return this;
}
2015-09-30 14:24:40 +02:00
public WorldCoord divide( final int _x, final int _y, final int _z )
{
2015-01-01 21:17:00 +01:00
this.x /= _x;
this.y /= _y;
this.z /= _z;
return this;
}
/**
* Will Return NULL if it's at some diagonal!
*/
2015-09-30 14:24:40 +02:00
public AEPartLocation directionTo( final WorldCoord loc )
{
2015-09-30 14:24:40 +02:00
final int ox = this.x - loc.x;
final int oy = this.y - loc.y;
final int oz = this.z - loc.z;
2015-09-30 14:24:40 +02:00
final int xlen = Math.abs( ox );
final int ylen = Math.abs( oy );
final int zlen = Math.abs( oz );
2015-06-16 02:44:59 +02:00
if( loc.isEqual( this.copy().add( AEPartLocation.EAST, xlen ) ) )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return AEPartLocation.EAST;
2015-04-29 02:30:53 +02:00
}
2015-06-16 02:44:59 +02:00
if( loc.isEqual( this.copy().add( AEPartLocation.WEST, xlen ) ) )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return AEPartLocation.WEST;
2015-04-29 02:30:53 +02:00
}
2015-06-16 02:44:59 +02:00
if( loc.isEqual( this.copy().add( AEPartLocation.NORTH, zlen ) ) )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return AEPartLocation.NORTH;
2015-04-29 02:30:53 +02:00
}
2015-06-16 02:44:59 +02:00
if( loc.isEqual( this.copy().add( AEPartLocation.SOUTH, zlen ) ) )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return AEPartLocation.SOUTH;
2015-04-29 02:30:53 +02:00
}
2015-06-16 02:44:59 +02:00
if( loc.isEqual( this.copy().add( AEPartLocation.UP, ylen ) ) )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return AEPartLocation.UP;
2015-04-29 02:30:53 +02:00
}
2015-06-16 02:44:59 +02:00
if( loc.isEqual( this.copy().add( AEPartLocation.DOWN, ylen ) ) )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return AEPartLocation.DOWN;
2015-04-29 02:30:53 +02:00
}
return null;
}
2015-09-30 14:24:40 +02:00
public boolean isEqual( final WorldCoord c )
{
2015-01-01 21:17:00 +01:00
return this.x == c.x && this.y == c.y && this.z == c.z;
}
2015-09-30 14:24:40 +02:00
public WorldCoord add( final AEPartLocation direction, final int length )
{
2015-06-16 02:44:59 +02:00
this.x += direction.xOffset * length;
this.y += direction.yOffset * length;
this.z += direction.zOffset * length;
return this;
}
public WorldCoord copy()
{
2015-01-01 21:17:00 +01:00
return new WorldCoord( this.x, this.y, this.z );
}
@Override
public int hashCode()
{
return ( this.y << 24 ) ^ this.x ^ this.z;
}
@Override
2015-09-30 14:24:40 +02:00
public boolean equals( final Object obj )
{
return obj instanceof WorldCoord && this.isEqual( (WorldCoord) obj );
}
2015-06-16 02:44:59 +02:00
public BlockPos getPos()
{
2015-09-30 14:26:54 +02:00
return new BlockPos( this.x, this.y, this.z );
2015-06-16 02:44:59 +02:00
}
2016-01-01 01:48:15 +01:00
@Override
public String toString()
{
return "x=" + this.x + ", y=" + this.y + ", z=" + this.z;
}
}