Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.21.10-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/net/tfminecraft/archaeo/museum/MuseumListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.tfminecraft.archaeo.site.SiteRepository;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Lectern;
import org.bukkit.block.Shelf;
Expand Down Expand Up @@ -200,7 +201,7 @@ private static List<ItemStack> displayedOnBlock(BlockState state, Vector click)
}
return stacks;
}
int slot = shelf.getSlot(click);
int slot = shelfSlot(((org.bukkit.block.data.type.Shelf) shelf.getBlockData()).getFacing(), click);
if (slot >= 0) {
stacks.add(shelf.getInventory().getItem(slot));
}
Expand All @@ -209,6 +210,27 @@ private static List<ItemStack> displayedOnBlock(BlockState state, Vector click)
return stacks;
}

// Paper 1.21.10 has no Shelf#getSlot. Match vanilla's three equal columns,
// ordered left to right when looking at the shelf's front.
static int shelfSlot(BlockFace facing, Vector click) {
if (click == null || !(click.getX() >= 0 && click.getX() <= 1
&& click.getY() >= 0 && click.getY() <= 1
&& click.getZ() >= 0 && click.getZ() <= 1)) {
return -1;
}
float horizontal = (float) switch (facing) {
case NORTH -> 1 - click.getX();
case SOUTH -> click.getX();
case WEST -> click.getZ();
case EAST -> 1 - click.getZ();
default -> -1;
};
if (horizontal < 0) {
return -1;
}
return Math.min(2, (int) Math.floor(horizontal * 16.0f / (16.0f / 3)));
}

/**
* @param entity frame, display, armor stand, or furniture root
* @return stacks that entity is showing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.tfminecraft.archaeo.museum;

import org.bukkit.block.BlockFace;
import org.bukkit.util.Vector;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class MuseumListenerTest {
@Test
public void selectsAllThreeSlotsFromEachFacing() {
for (int slot = 0; slot < 3; slot++) {
double horizontal = (slot + 0.5) / 3;
assertEquals(slot, MuseumListener.shelfSlot(BlockFace.NORTH, new Vector(1 - horizontal, 0.5, 0)));
assertEquals(slot, MuseumListener.shelfSlot(BlockFace.SOUTH, new Vector(horizontal, 0.5, 1)));
assertEquals(slot, MuseumListener.shelfSlot(BlockFace.WEST, new Vector(0, 0.5, horizontal)));
assertEquals(slot, MuseumListener.shelfSlot(BlockFace.EAST, new Vector(1, 0.5, 1 - horizontal)));
}
}

@Test
public void keepsEdgesInsideTheInventoryAndSplitsAtThirds() {
double[] positions = {0, 1.0 / 3 - 0.001, 1.0 / 3, 2.0 / 3 - 0.001, 2.0 / 3, 1};
int[] slots = {0, 0, 1, 1, 2, 2};
for (int i = 0; i < positions.length; i++) {
assertEquals(slots[i], MuseumListener.shelfSlot(BlockFace.SOUTH, new Vector(positions[i], 0, 1)));
}
}

@Test
public void rejectsPositionsOutsideTheBlock() {
assertEquals(-1, MuseumListener.shelfSlot(BlockFace.SOUTH, null));
assertEquals(-1, MuseumListener.shelfSlot(BlockFace.UP, new Vector(0.5, 0.5, 0.5)));
for (double invalid : new double[] {-0.01, 1.01, Double.NaN, Double.POSITIVE_INFINITY}) {
assertEquals(-1, MuseumListener.shelfSlot(BlockFace.SOUTH, new Vector(invalid, 0.5, 0.5)));
assertEquals(-1, MuseumListener.shelfSlot(BlockFace.SOUTH, new Vector(0.5, invalid, 0.5)));
assertEquals(-1, MuseumListener.shelfSlot(BlockFace.SOUTH, new Vector(0.5, 0.5, invalid)));
}
}
}
Loading