Skip to content

Commit a011e91

Browse files
committed
Fixed selection of floating fake images
- Updated SelectFakeItemFrameListener class
1 parent d7e3c65 commit a011e91

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

src/main/java/io/josemmo/bukkit/plugin/interaction/SelectFakeItemFrameListener.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
import io.josemmo.bukkit.plugin.YamipaPlugin;
1111
import io.josemmo.bukkit.plugin.renderer.FakeImage;
1212
import io.josemmo.bukkit.plugin.renderer.FakeItemFrame;
13+
import io.josemmo.bukkit.plugin.renderer.ImageRenderer;
1314
import io.josemmo.bukkit.plugin.utils.Internals;
14-
import org.bukkit.Location;
1515
import org.bukkit.block.Block;
1616
import org.bukkit.block.BlockFace;
1717
import org.bukkit.entity.Player;
1818
import org.bukkit.plugin.Plugin;
19+
import org.bukkit.util.BlockIterator;
1920
import org.jetbrains.annotations.NotNull;
2021
import org.jetbrains.annotations.Nullable;
21-
import java.util.List;
2222

2323
public abstract class SelectFakeItemFrameListener implements PacketListener {
2424
private static final int MAX_BLOCK_DISTANCE = 5;
@@ -29,26 +29,27 @@ public abstract class SelectFakeItemFrameListener implements PacketListener {
2929
* @return Fake image instance or <code>null</code> if not found
3030
*/
3131
public static @Nullable FakeImage getFakeImage(@NotNull Player player) {
32-
// Get target block in range
33-
List<Block> lastTwoTargetBlocks = player.getLastTwoTargetBlocks(null, MAX_BLOCK_DISTANCE);
34-
if (lastTwoTargetBlocks.size() != 2) {
35-
return null;
36-
}
37-
Block targetBlock = lastTwoTargetBlocks.get(1);
38-
if (!targetBlock.getType().isSolid()) {
39-
return null;
40-
}
41-
42-
// Get target block face
43-
Block adjacentBlock = lastTwoTargetBlocks.get(0);
44-
BlockFace targetBlockFace = targetBlock.getFace(adjacentBlock);
45-
if (targetBlockFace == null) {
46-
return null;
32+
ImageRenderer renderer = YamipaPlugin.getInstance().getRenderer();
33+
34+
// Get the closest fake image within player's line of sight
35+
BlockIterator iterator = new BlockIterator(player, MAX_BLOCK_DISTANCE);
36+
Block previousBlock = null;
37+
while (iterator.hasNext()) {
38+
Block currentBlock = iterator.next();
39+
if (previousBlock != null) {
40+
BlockFace currentBlockFace = currentBlock.getFace(previousBlock);
41+
if (currentBlockFace != null) {
42+
FakeImage image = renderer.getImage(currentBlock.getLocation(), currentBlockFace);
43+
if (image != null) {
44+
return image;
45+
}
46+
}
47+
}
48+
previousBlock = currentBlock;
4749
}
4850

49-
// Get fake image instance
50-
Location targetBlockLocation = targetBlock.getLocation();
51-
return YamipaPlugin.getInstance().getRenderer().getImage(targetBlockLocation, targetBlockFace);
51+
// No fake image found
52+
return null;
5253
}
5354

5455
/**

0 commit comments

Comments
 (0)