2021-02-07 03:37:25 +01:00
|
|
|
package net.knarcraft.stargate;
|
|
|
|
|
|
|
|
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;
|
|
|
|
import org.bukkit.block.data.type.Sign;
|
|
|
|
import org.bukkit.block.data.type.WallSign;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* equals another BlockLocation does not necessarily equal the name 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-07 03:37:25 +01:00
|
|
|
* @param world <p>The world the block exists in</p>
|
|
|
|
* @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>
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies a craftbukkit block
|
|
|
|
* @param block <p>The block to </p>
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a block from a string
|
|
|
|
* @param world <p>The world the block exists in</p>
|
|
|
|
* @param string <p>A comma separated list of z, y and z coordinates as integers</p>
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a new block in a relative position to this block
|
|
|
|
* @param x <p>The x position relative to this block's position</p>
|
|
|
|
* @param y <p>The y position relative to this block's position</p>
|
|
|
|
* @param z <p>The z position relative to this block's position</p>
|
2021-02-07 16:37:42 +01:00
|
|
|
* @return <p>A new block location</p>
|
2021-02-07 03:37:25 +01:00
|
|
|
*/
|
|
|
|
public BlockLocation makeRelative(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
|
|
|
/**
|
|
|
|
* Makes a location relative to the block location
|
|
|
|
* @param x <p>The x position relative to this block's position</p>
|
|
|
|
* @param y <p>The y position relative to this block's position</p>
|
|
|
|
* @param z <p>The z position relative to this block's position</p>
|
|
|
|
* @param rotX <p>The x rotation of the location</p>
|
|
|
|
* @param rotY <p>The y rotation of the location</p>
|
|
|
|
* @return <p>A new location</p>
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public Location makeRelativeLoc(double x, double y, double z, float rotX, float rotY) {
|
2021-02-07 16:58:33 +01:00
|
|
|
Location newLocation = this.clone();
|
2021-02-07 16:37:42 +01:00
|
|
|
newLocation.setYaw(rotX);
|
|
|
|
newLocation.setPitch(rotY);
|
|
|
|
return newLocation.add(x, y, z);
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 16:37:42 +01:00
|
|
|
/**
|
|
|
|
* Makes a block location relative to the current location according to given parameters
|
|
|
|
* @param right <p></p>
|
|
|
|
* @param depth <p>The y position relative to the current position</p>
|
|
|
|
* @param distance <p>The distance away from the previous location to the new location</p>
|
|
|
|
* @param modX <p>x modifier. Defines movement along the x-axis. 0 for no movement</p>
|
|
|
|
* @param modY <p></p>
|
|
|
|
* @param modZ <p>z modifier. Defines movement along the z-axis. 0 for no movement</p>
|
|
|
|
* @return A new location relative to this block location
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public BlockLocation modRelative(int right, int depth, int distance, int modX, int modY, int modZ) {
|
|
|
|
return makeRelative(-right * modX + distance * modZ, -depth * modY, -right * modZ + -distance * modX);
|
|
|
|
}
|
|
|
|
|
2021-02-07 16:37:42 +01:00
|
|
|
/**
|
|
|
|
* Makes a location relative to the current location according to given parameters
|
|
|
|
* @param right <p></p>
|
|
|
|
* @param depth <p>The y position relative to the current position</p>
|
|
|
|
* @param distance <p>The distance away from the previous location to the new location</p>
|
|
|
|
* @param rotX <p>The yaw of the location</p>
|
|
|
|
* @param rotY <p>Unused</p>
|
|
|
|
* @param modX <p>x modifier. Defines movement along the x-axis. 0 for no movement</p>
|
|
|
|
* @param modY <p>Unused</p>
|
|
|
|
* @param modZ <p>z modifier. Defines movement along the z-axis. 0 for no movement</p>
|
|
|
|
* @return A new location relative to this block location
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public Location modRelativeLoc(double right, double depth, double distance, float rotX, float rotY, int modX, int modY, int modZ) {
|
|
|
|
return makeRelativeLoc(0.5 + -right * modX + distance * modZ, depth, 0.5 + -right * modZ + -distance * modX, rotX, 0);
|
|
|
|
}
|
|
|
|
|
2021-02-07 16:37:42 +01:00
|
|
|
/**
|
|
|
|
* Sets the type for the block at this location
|
|
|
|
* @param type <p>The new block material type</p>
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public void setType(Material type) {
|
2021-02-07 16:58:33 +01:00
|
|
|
this.getBlock().setType(type);
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 16:37:42 +01:00
|
|
|
/**
|
|
|
|
* Gets the type for the block at this location
|
|
|
|
* @return <p>The block material type</p>
|
|
|
|
*/
|
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-07 16:37:42 +01:00
|
|
|
/**
|
|
|
|
* Gets the location representing this block location
|
|
|
|
* @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
|
|
|
|
* @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();
|
|
|
|
if (blockData instanceof WallSign) {
|
|
|
|
BlockFace facing = ((WallSign) 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-02-07 03:37:25 +01:00
|
|
|
offsetY = -1;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-07 16:37:42 +01:00
|
|
|
parent = this.makeRelative(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
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (this == obj) return true;
|
|
|
|
if (obj == null) return false;
|
|
|
|
if (getClass() != obj.getClass()) return false;
|
|
|
|
|
|
|
|
BlockLocation blockLocation = (BlockLocation) obj;
|
2021-02-07 16:37:42 +01:00
|
|
|
|
2021-02-07 16:58:33 +01:00
|
|
|
World thisWorld = this.getWorld();
|
|
|
|
World otherWorld = blockLocation.getWorld();
|
|
|
|
boolean worldsEqual = (thisWorld == null && otherWorld == null) || ((thisWorld != null && otherWorld != null)
|
|
|
|
&& thisWorld == otherWorld);
|
|
|
|
|
|
|
|
return blockLocation.getBlockX() == this.getBlockX() && blockLocation.getBlockY() == this.getBlockY() &&
|
|
|
|
blockLocation.getBlockZ() == this.getBlockZ() && worldsEqual;
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|