Moves some classes to the new container package, and improves some code
This commit is contained in:
@ -0,0 +1,82 @@
|
||||
package net.knarcraft.stargate.container;
|
||||
|
||||
import org.bukkit.Axis;
|
||||
import org.bukkit.Material;
|
||||
|
||||
/**
|
||||
* Represents a request for changing a block into another material
|
||||
*/
|
||||
public class BlockChangeRequest {
|
||||
|
||||
private BlockLocation blockLocation;
|
||||
private Material newMaterial;
|
||||
private Axis newAxis;
|
||||
|
||||
/**
|
||||
* Instantiates a new block change request
|
||||
*
|
||||
* @param blockLocation <p>The location of the block to change</p>
|
||||
* @param material <p>The new material to change the block to</p>
|
||||
* @param axis <p>The new axis to orient the block along</p>
|
||||
*/
|
||||
public BlockChangeRequest(BlockLocation blockLocation, Material material, Axis axis) {
|
||||
this.blockLocation = blockLocation;
|
||||
newMaterial = material;
|
||||
newAxis = axis;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the location of the block to change
|
||||
*
|
||||
* @return <p>The location of the block</p>
|
||||
*/
|
||||
public BlockLocation getBlockLocation() {
|
||||
return blockLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the location of the block
|
||||
*
|
||||
* @param blockLocation <p>The new location of the block</p>
|
||||
*/
|
||||
public void setBlockLocation(BlockLocation blockLocation) {
|
||||
this.blockLocation = blockLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the material to change the block into
|
||||
*
|
||||
* @return <p>The material to change the block into</p>
|
||||
*/
|
||||
public Material getMaterial() {
|
||||
return newMaterial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the material to change the block into
|
||||
*
|
||||
* @param material <p>The new material</p>
|
||||
*/
|
||||
public void setMaterial(Material material) {
|
||||
newMaterial = material;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the axis to orient the block along
|
||||
*
|
||||
* @return <p>The axis to orient the block along</p>
|
||||
*/
|
||||
public Axis getAxis() {
|
||||
return newAxis;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the axis to orient the block along
|
||||
*
|
||||
* @param axis <p>The new axis to orient the block along</p>
|
||||
*/
|
||||
public void setAxis(Axis axis) {
|
||||
newAxis = axis;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,223 @@
|
||||
package net.knarcraft.stargate.container;
|
||||
|
||||
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
|
||||
*
|
||||
* <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>
|
||||
*/
|
||||
public class BlockLocation extends Location {
|
||||
|
||||
private BlockLocation parent = null;
|
||||
|
||||
/**
|
||||
* Creates a new block location
|
||||
*
|
||||
* @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) {
|
||||
super(world, x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies a craftbukkit block
|
||||
*
|
||||
* @param block <p>The block to </p>
|
||||
*/
|
||||
public BlockLocation(Block block) {
|
||||
super(block.getWorld(), block.getX(), block.getY(), block.getZ());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
super(world, Integer.parseInt(string.split(",")[0]), Integer.parseInt(string.split(",")[1]),
|
||||
Integer.parseInt(string.split(",")[2]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
* @return <p>A new block location</p>
|
||||
*/
|
||||
public BlockLocation makeRelative(int x, int y, int z) {
|
||||
return (BlockLocation) this.clone().add(x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 yaw <p>The yaw of the location</p>
|
||||
* @param rotY <p>The y rotation of the location</p>
|
||||
* @return <p>A new location</p>
|
||||
*/
|
||||
public Location makeRelativeLoc(double x, double y, double z, float yaw, float rotY) {
|
||||
Location newLocation = this.clone();
|
||||
newLocation.setYaw(yaw);
|
||||
newLocation.setPitch(rotY);
|
||||
return newLocation.add(x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a block location relative to the current origin according to given parameters
|
||||
*
|
||||
* <p>See {@link RelativeBlockVector} to understand better. modX or modZ should always be 0 while the other is 1
|
||||
* or -1.</p>
|
||||
*
|
||||
* @param right <p>The amount of right steps from the top-left origin</p>
|
||||
* @param depth <p>The amount of downward steps from the top-left origin</p>
|
||||
* @param distance <p>The distance outward from the top-left origin</p>
|
||||
* @param modX <p>X modifier. If modX = -1, X will increase as right increases</p>
|
||||
* @param modY <p>Y modifier. modY = 1 for Y decreasing as depth increases</p>
|
||||
* @param modZ <p>Z modifier. If modZ = 1, X will increase as distance increases</p>
|
||||
* @return A new location relative to this block location
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 yaw <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
|
||||
*/
|
||||
public Location modRelativeLoc(double right, double depth, double distance, float yaw, float rotY, int modX, int modY, int modZ) {
|
||||
return makeRelativeLoc(0.5 + -right * modX + distance * modZ, depth, 0.5 + -right * modZ + -distance * modX, yaw, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the type for the block at this location
|
||||
*
|
||||
* @return <p>The block material type</p>
|
||||
*/
|
||||
public Material getType() {
|
||||
return this.getBlock().getType();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the type for the block at this location
|
||||
*
|
||||
* @param type <p>The new block material type</p>
|
||||
*/
|
||||
public void setType(Material type) {
|
||||
this.getBlock().setType(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the location representing this block location
|
||||
*
|
||||
* @return <p>The location representing this block location</p>
|
||||
*/
|
||||
public Location getLocation() {
|
||||
return this.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets this block location's parent block
|
||||
*
|
||||
* @return <p>This block location's parent block</p>
|
||||
*/
|
||||
public Block getParent() {
|
||||
if (parent == null) {
|
||||
findParent();
|
||||
}
|
||||
if (parent == null) {
|
||||
return null;
|
||||
}
|
||||
return parent.getBlock();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
*/
|
||||
private void findParent() {
|
||||
int offsetX = 0;
|
||||
int offsetY = 0;
|
||||
int offsetZ = 0;
|
||||
|
||||
BlockData blockData = getBlock().getBlockData();
|
||||
if (blockData instanceof WallSign) {
|
||||
BlockFace facing = ((WallSign) blockData).getFacing().getOppositeFace();
|
||||
offsetX = facing.getModX();
|
||||
offsetZ = facing.getModZ();
|
||||
} else if (blockData instanceof Sign) {
|
||||
offsetY = -1;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
parent = this.makeRelative(offsetX, offsetY, offsetZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.valueOf(this.getBlockX()) + ',' + this.getBlockY() + ',' + this.getBlockZ();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 18;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null || getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
BlockLocation blockLocation = (BlockLocation) obj;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package net.knarcraft.stargate.container;
|
||||
|
||||
/**
|
||||
* This stores a block location as a vector relative to a position
|
||||
*
|
||||
* <p>A relative block vector stores a vector relative to some origin. The origin in this plugin is usually the
|
||||
* top-left block of a gate (top-left when looking at the side with the sign). The right is therefore the distance
|
||||
* from the top-left corner towards the top-right corner. Depth is the distance from the top-left corner to the
|
||||
* bottom-left corner. Distance is the distance outward from the gate.</p>
|
||||
*/
|
||||
public class RelativeBlockVector {
|
||||
|
||||
private final int right;
|
||||
private final int depth;
|
||||
private final int distance;
|
||||
|
||||
/**
|
||||
* Instantiates a new relative block vector
|
||||
*
|
||||
* @param right <p>The distance to the right relative to the origin</p>
|
||||
* @param depth <p>The distance downward relative to the origin</p>
|
||||
* @param distance <p>The distance outward relative to the origin</p>
|
||||
*/
|
||||
public RelativeBlockVector(int right, int depth, int distance) {
|
||||
this.right = right;
|
||||
this.depth = depth;
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the distance to the right relative to the origin
|
||||
*
|
||||
* @return The distance to the right relative to the origin
|
||||
*/
|
||||
public int getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the distance downward relative to the origin
|
||||
*
|
||||
* @return The distance downward relative to the origin
|
||||
*/
|
||||
public int getDepth() {
|
||||
return depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the distance outward relative to the origin
|
||||
*
|
||||
* @return The distance outward relative to the origin
|
||||
*/
|
||||
public int getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("(right = %d, depth = %d, distance = %d)", right, depth, distance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == this) {
|
||||
return true;
|
||||
}
|
||||
if (other == null || this.getClass() != other.getClass()) {
|
||||
return false;
|
||||
}
|
||||
RelativeBlockVector otherVector = (RelativeBlockVector) other;
|
||||
return this.right == otherVector.right && this.depth == otherVector.depth &&
|
||||
this.distance == otherVector.distance;
|
||||
}
|
||||
|
||||
}
|
43
src/main/java/net/knarcraft/stargate/container/TwoTuple.java
Normal file
43
src/main/java/net/knarcraft/stargate/container/TwoTuple.java
Normal file
@ -0,0 +1,43 @@
|
||||
package net.knarcraft.stargate.container;
|
||||
|
||||
/**
|
||||
* This class allows storing two values of any type
|
||||
*
|
||||
* @param <K> <p>The first type</p>
|
||||
* @param <L> <p>The second type</p>
|
||||
*/
|
||||
public class TwoTuple<K, L> {
|
||||
|
||||
private final K firstValue;
|
||||
private final L secondValue;
|
||||
|
||||
/**
|
||||
* Instantiate a new TwoTuple
|
||||
*
|
||||
* @param firstValue <p>The first value</p>
|
||||
* @param secondValue <p>The second value</p>
|
||||
*/
|
||||
public TwoTuple(K firstValue, L secondValue) {
|
||||
this.firstValue = firstValue;
|
||||
this.secondValue = secondValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first value
|
||||
*
|
||||
* @return <p>The first value</p>
|
||||
*/
|
||||
public K getFirstValue() {
|
||||
return firstValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the second value
|
||||
*
|
||||
* @return <p>The second value</p>
|
||||
*/
|
||||
public L getSecondValue() {
|
||||
return secondValue;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user