2021-09-18 21:51:29 +02:00
|
|
|
package net.knarcraft.stargate.container;
|
2021-02-07 03:37:25 +01:00
|
|
|
|
2021-10-08 15:28:12 +02:00
|
|
|
import net.knarcraft.stargate.utility.DirectionHelper;
|
2021-02-07 03:37:25 +01:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.BlockFace;
|
|
|
|
import org.bukkit.block.data.BlockData;
|
2021-10-13 13:35:56 +02:00
|
|
|
import org.bukkit.block.data.Directional;
|
2021-02-07 03:37:25 +01:00
|
|
|
import org.bukkit.block.data.type.Sign;
|
2021-10-08 15:28:12 +02:00
|
|
|
import org.bukkit.util.Vector;
|
2021-02-07 03:37:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class represents a block location
|
2021-02-07 16:37:42 +01:00
|
|
|
*
|
2021-02-09 20:38:50 +01:00
|
|
|
* <p>The BlockLocation class is basically a Location with some extra functionality.
|
|
|
|
* Warning: Because of differences in the equals methods between Location and BlockLocation, a BlockLocation which
|
2021-10-13 13:35:56 +02:00
|
|
|
* equals another BlockLocation does not necessarily equal the same BlockLocation if treated as a Location.</p>
|
2021-02-07 03:37:25 +01:00
|
|
|
*/
|
2021-02-07 16:58:33 +01:00
|
|
|
public class BlockLocation extends Location {
|
2021-02-07 03:37:25 +01:00
|
|
|
|
|
|
|
private BlockLocation parent = null;
|
|
|
|
|
|
|
|
/**
|
2021-02-07 16:37:42 +01:00
|
|
|
* Creates a new block location
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-02-07 03:37:25 +01:00
|
|
|
* @param world <p>The world the block exists in</p>
|
2021-02-20 13:57:04 +01:00
|
|
|
* @param x <p>The x coordinate of the block</p>
|
|
|
|
* @param y <p>The y coordinate of the block</p>
|
|
|
|
* @param z <p>The z coordinate of the block</p>
|
2021-02-07 03:37:25 +01:00
|
|
|
*/
|
|
|
|
public BlockLocation(World world, int x, int y, int z) {
|
2021-02-07 16:58:33 +01:00
|
|
|
super(world, x, y, z);
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-13 13:35:56 +02:00
|
|
|
* Creates a block location from a block
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-10-13 13:35:56 +02:00
|
|
|
* @param block <p>The block to get the location of</p>
|
2021-02-07 03:37:25 +01:00
|
|
|
*/
|
|
|
|
public BlockLocation(Block block) {
|
2021-02-07 16:58:33 +01:00
|
|
|
super(block.getWorld(), block.getX(), block.getY(), block.getZ());
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-13 13:35:56 +02:00
|
|
|
* Gets a block location from a string
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
|
|
|
* @param world <p>The world the block exists in</p>
|
2021-10-13 13:35:56 +02:00
|
|
|
* @param string <p>A comma separated list of x, y and z coordinates as integers</p>
|
2021-02-07 03:37:25 +01:00
|
|
|
*/
|
|
|
|
public BlockLocation(World world, String string) {
|
2021-02-07 16:58:33 +01:00
|
|
|
super(world, Integer.parseInt(string.split(",")[0]), Integer.parseInt(string.split(",")[1]),
|
|
|
|
Integer.parseInt(string.split(",")[2]));
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-13 13:35:56 +02:00
|
|
|
* Creates a new block location in a relative position to this block location
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-10-13 13:35:56 +02:00
|
|
|
* @param x <p>The number of blocks to move in the x-direction</p>
|
|
|
|
* @param y <p>The number of blocks to move in the y-direction</p>
|
|
|
|
* @param z <p>The number of blocks to move in the z-direction</p>
|
2021-02-07 16:37:42 +01:00
|
|
|
* @return <p>A new block location</p>
|
2021-02-07 03:37:25 +01:00
|
|
|
*/
|
2021-10-08 18:23:42 +02:00
|
|
|
public BlockLocation makeRelativeBlockLocation(int x, int y, int z) {
|
2021-02-07 16:58:33 +01:00
|
|
|
return (BlockLocation) this.clone().add(x, y, z);
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 16:37:42 +01:00
|
|
|
/**
|
2021-10-13 13:35:56 +02:00
|
|
|
* Creates a location in a relative position to this block location
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-10-13 13:35:56 +02:00
|
|
|
* @param x <p>The number of blocks to move in the x-direction</p>
|
|
|
|
* @param y <p>The number of blocks to move in the y-direction</p>
|
2021-10-08 18:23:42 +02:00
|
|
|
* @param z <p>The z position relative to this block's position</p>
|
2021-10-13 13:35:56 +02:00
|
|
|
* @param yaw <p>The number of blocks to move in the z-direction</p>
|
2021-02-07 16:37:42 +01:00
|
|
|
* @return <p>A new location</p>
|
|
|
|
*/
|
2021-10-08 18:23:42 +02:00
|
|
|
public Location makeRelativeLocation(double x, double y, double z, float yaw) {
|
2021-02-07 16:58:33 +01:00
|
|
|
Location newLocation = this.clone();
|
2021-09-10 23:38:56 +02:00
|
|
|
newLocation.setYaw(yaw);
|
2021-10-08 18:23:42 +02:00
|
|
|
newLocation.setPitch(0);
|
2021-02-07 16:37:42 +01:00
|
|
|
return newLocation.add(x, y, z);
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
2021-10-08 15:28:12 +02:00
|
|
|
/**
|
|
|
|
* Gets a location relative to this block location
|
|
|
|
*
|
2021-10-08 18:23:42 +02:00
|
|
|
* @param relativeVector <p>The relative block vector describing the relative location</p>
|
2021-10-24 22:48:13 +02:00
|
|
|
* @param yaw <p>The yaw pointing outwards from a portal (in the relative vector's out direction)</p>
|
2021-10-08 15:28:12 +02:00
|
|
|
* @return <p>A location relative to this location</p>
|
|
|
|
*/
|
2021-10-08 18:23:42 +02:00
|
|
|
public BlockLocation getRelativeLocation(RelativeBlockVector relativeVector, double yaw) {
|
|
|
|
Vector realVector = DirectionHelper.getCoordinateVectorFromRelativeVector(relativeVector.getRight(),
|
2021-10-24 22:48:13 +02:00
|
|
|
relativeVector.getDown(), relativeVector.getOut(), yaw);
|
2021-10-08 18:23:42 +02:00
|
|
|
return makeRelativeBlockLocation(realVector.getBlockX(), realVector.getBlockY(), realVector.getBlockZ());
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 16:37:42 +01:00
|
|
|
/**
|
|
|
|
* Makes a location relative to the current location according to given parameters
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-10-24 22:48:13 +02:00
|
|
|
* <p>Out goes in the direction of the yaw. Right goes in the direction of (yaw - 90) degrees.
|
2021-10-13 13:35:56 +02:00
|
|
|
* Depth goes downwards following the -y direction.</p>
|
|
|
|
*
|
|
|
|
* @param right <p>The amount of blocks to go right when looking towards a portal</p>
|
2021-10-24 22:48:13 +02:00
|
|
|
* @param down <p>The amount of blocks to go downwards when looking towards a portal</p>
|
|
|
|
* @param out <p>The amount of blocks to go outwards when looking towards a portal</p>
|
2021-10-08 18:23:42 +02:00
|
|
|
* @param portalYaw <p>The yaw when looking out from the portal</p>
|
2021-02-07 16:37:42 +01:00
|
|
|
* @return A new location relative to this block location
|
|
|
|
*/
|
2021-10-24 22:48:13 +02:00
|
|
|
public Location getRelativeLocation(double right, double down, double out, float portalYaw) {
|
|
|
|
Vector realVector = DirectionHelper.getCoordinateVectorFromRelativeVector(right, down, out, portalYaw);
|
2021-10-08 18:23:42 +02:00
|
|
|
return makeRelativeLocation(0.5 + realVector.getBlockX(), realVector.getBlockY(),
|
2021-10-08 18:59:14 +02:00
|
|
|
0.5 + realVector.getBlockZ(), portalYaw);
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 16:37:42 +01:00
|
|
|
/**
|
2021-10-13 13:35:56 +02:00
|
|
|
* Gets the type of block at this block location
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-10-13 13:35:56 +02:00
|
|
|
* @return <p>The block's material type</p>
|
2021-02-07 16:37:42 +01:00
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public Material getType() {
|
2021-02-07 16:58:33 +01:00
|
|
|
return this.getBlock().getType();
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-20 13:57:04 +01:00
|
|
|
/**
|
2021-10-13 13:35:56 +02:00
|
|
|
* Sets the type of block at this location
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-10-13 13:35:56 +02:00
|
|
|
* @param type <p>The block's new material type</p>
|
2021-02-20 13:57:04 +01:00
|
|
|
*/
|
|
|
|
public void setType(Material type) {
|
|
|
|
this.getBlock().setType(type);
|
|
|
|
}
|
|
|
|
|
2021-02-07 16:37:42 +01:00
|
|
|
/**
|
|
|
|
* Gets the location representing this block location
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-02-07 16:37:42 +01:00
|
|
|
* @return <p>The location representing this block location</p>
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public Location getLocation() {
|
2021-02-07 16:58:33 +01:00
|
|
|
return this.clone();
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 16:37:42 +01:00
|
|
|
/**
|
|
|
|
* Gets this block location's parent block
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-10-13 13:35:56 +02:00
|
|
|
* <p>The parent block is the block the item at this block location is attached to. Usually this is the block a
|
|
|
|
* sign or wall sign is attached to.</p>
|
|
|
|
*
|
2021-02-07 16:37:42 +01:00
|
|
|
* @return <p>This block location's parent block</p>
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public Block getParent() {
|
2021-02-07 16:37:42 +01:00
|
|
|
if (parent == null) {
|
|
|
|
findParent();
|
|
|
|
}
|
|
|
|
if (parent == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-02-07 03:37:25 +01:00
|
|
|
return parent.getBlock();
|
|
|
|
}
|
|
|
|
|
2021-02-07 16:37:42 +01:00
|
|
|
/**
|
|
|
|
* Tries to find the parent block location
|
|
|
|
*
|
|
|
|
* <p>If this block location is a sign, the parent is the block location of the block the sign is connected to.</p>
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
private void findParent() {
|
|
|
|
int offsetX = 0;
|
|
|
|
int offsetY = 0;
|
|
|
|
int offsetZ = 0;
|
|
|
|
|
2021-02-07 16:37:42 +01:00
|
|
|
BlockData blockData = getBlock().getBlockData();
|
2021-10-13 13:35:56 +02:00
|
|
|
if (blockData instanceof Directional) {
|
|
|
|
//Get the offset of the block "behind" this block
|
|
|
|
BlockFace facing = ((Directional) blockData).getFacing().getOppositeFace();
|
2021-02-07 03:37:25 +01:00
|
|
|
offsetX = facing.getModX();
|
|
|
|
offsetZ = facing.getModZ();
|
2021-02-07 16:37:42 +01:00
|
|
|
} else if (blockData instanceof Sign) {
|
2021-10-13 13:35:56 +02:00
|
|
|
//Get offset the block beneath the sign
|
2021-02-07 03:37:25 +01:00
|
|
|
offsetY = -1;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-08 18:23:42 +02:00
|
|
|
parent = this.makeRelativeBlockLocation(offsetX, offsetY, offsetZ);
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2021-02-07 16:58:33 +01:00
|
|
|
return String.valueOf(this.getBlockX()) + ',' + this.getBlockY() + ',' + this.getBlockZ();
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
int result = 18;
|
|
|
|
|
2021-02-07 16:58:33 +01:00
|
|
|
result = result * 27 + this.getBlockX();
|
|
|
|
result = result * 27 + this.getBlockY();
|
|
|
|
result = result * 27 + this.getBlockZ();
|
|
|
|
if (this.getWorld() != null) {
|
|
|
|
result = result * 27 + this.getWorld().getName().hashCode();
|
2021-02-07 16:37:42 +01:00
|
|
|
}
|
2021-02-07 03:37:25 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-10-13 13:35:56 +02:00
|
|
|
public boolean equals(Object object) {
|
|
|
|
if (this == object) {
|
2021-02-22 17:01:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-13 13:35:56 +02:00
|
|
|
if (object == null || getClass() != object.getClass()) {
|
2021-02-22 17:01:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-07 03:37:25 +01:00
|
|
|
|
2021-10-13 13:35:56 +02:00
|
|
|
BlockLocation blockLocation = (BlockLocation) object;
|
2021-02-07 16:37:42 +01:00
|
|
|
|
2021-02-07 16:58:33 +01:00
|
|
|
World thisWorld = this.getWorld();
|
|
|
|
World otherWorld = blockLocation.getWorld();
|
2021-10-13 13:35:56 +02:00
|
|
|
//Check if the worlds of the two locations match
|
2021-02-07 16:58:33 +01:00
|
|
|
boolean worldsEqual = (thisWorld == null && otherWorld == null) || ((thisWorld != null && otherWorld != null)
|
|
|
|
&& thisWorld == otherWorld);
|
|
|
|
|
2021-10-13 13:35:56 +02:00
|
|
|
//As this is a block location, only the block coordinates are compared
|
2021-02-07 16:58:33 +01:00
|
|
|
return blockLocation.getBlockX() == this.getBlockX() && blockLocation.getBlockY() == this.getBlockY() &&
|
|
|
|
blockLocation.getBlockZ() == this.getBlockZ() && worldsEqual;
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|