-
Notifications
You must be signed in to change notification settings - Fork 17
/
BlockUtil.java
83 lines (72 loc) · 2.7 KB
/
BlockUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.griefcraft.util;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Bed;
import org.bukkit.block.data.type.Chest;
import org.bukkit.util.Vector;
public class BlockUtil {
/**
* Look for a double chest adjacent to a chest
*
* @param block
* @return
*/
public static Block findAdjacentDoubleChest(Block block) {
BlockData blockData = block.getBlockData();
if (!(blockData instanceof Chest)) {
return null;
}
Chest chestData = (Chest) blockData;
if (chestData.getType() != Chest.Type.SINGLE) {
BlockFace chestFace = chestData.getFacing();
// we have to rotate is to get the adjacent chest
// west, right -> south
// west, left -> north
if (chestFace == BlockFace.WEST) {
chestFace = BlockFace.NORTH;
} else if (chestFace == BlockFace.NORTH) {
chestFace = BlockFace.EAST;
} else if (chestFace == BlockFace.EAST) {
chestFace = BlockFace.SOUTH;
} else if (chestFace == BlockFace.SOUTH) {
chestFace = BlockFace.WEST;
}
if (chestData.getType() == Chest.Type.RIGHT) {
chestFace = chestFace.getOppositeFace();
}
Block face = block.getRelative(chestFace);
// They're placing it beside a chest, check if it's already
// protected
if (face.getType() == block.getType()) {
return face;
}
}
return null;
}
public static Block findAdjacentBedPart(Block block) {
BlockData blockData = block.getBlockData();
if (!(blockData instanceof Bed)) {
return null;
}
Bed bedData = (Bed) blockData;
BlockFace bedFace = bedData.getFacing();
if (bedData.getPart() == Bed.Part.HEAD) {
bedFace = bedFace.getOppositeFace();
}
Block relativeBlock = block.getRelative(bedFace);
if (relativeBlock.getType() == block.getType()) {
return relativeBlock;
}
return null;
}
public static float getRelativeHitCoordinatesForBlockFace(Vector relativeHitPosition, BlockFace direction) {
return switch (direction) {
case NORTH -> (float) (1.0D - relativeHitPosition.getX());
case SOUTH -> (float) (relativeHitPosition.getX());
case WEST -> (float) (relativeHitPosition.getZ());
case EAST -> (float) (1.0D - relativeHitPosition.getZ());
default -> throw new IncompatibleClassChangeError();
};
}
}