Skip to content

Commit

Permalink
Fixes breaking rooted dirt exploit (#2371)
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed May 19, 2024
1 parent 50276cb commit 8aba736
Show file tree
Hide file tree
Showing 25 changed files with 351 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,20 @@ public void onPlayerInteract(final PlayerInteractEvent e)
Player p = e.getPlayer();
Location l = e.getClickedBlock().getLocation();
Material m = e.getClickedBlock().getType();
// Check for berry picking
if (e.getAction() == Action.RIGHT_CLICK_BLOCK && (e.getClickedBlock().getType() == Material.CAVE_VINES || e.getClickedBlock().getType() == Material.CAVE_VINES_PLANT)) {
if (!((CaveVinesPlant) e.getClickedBlock().getBlockData()).isBerries()) {
return;
// Right click handling
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
Material clickedType = e.getClickedBlock().getType();
switch (clickedType) {
case CAVE_VINES, CAVE_VINES_PLANT -> {
if (((CaveVinesPlant) e.getClickedBlock().getBlockData()).isBerries()) {
this.checkIsland(e, p, l, Flags.HARVEST);
}
this.checkIsland(e, p, l, Flags.HARVEST);
return;
}
if (e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getClickedBlock().getType() == Material.SWEET_BERRY_BUSH) {
this.checkIsland(e, p, l, Flags.HARVEST);
}
case SWEET_BERRY_BUSH -> this.checkIsland(e, p, l, Flags.HARVEST);
case ROOTED_DIRT -> this.checkIsland(e, p, l, Flags.BREAK_BLOCKS);
default -> { // Do nothing
}
}
return;
}
// Only handle hitting things
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public Set<Island> getOwnedIslands(@NonNull World world, @NonNull UUID uniqueId)
*/
@Nullable
public Island getIsland(@NonNull World world, @NonNull UUID uuid) {
return islandCache.get(world, uuid);
return islandCache.getIsland(world, uuid);
}

/**
Expand Down Expand Up @@ -1723,14 +1723,14 @@ public void setPrimaryIsland(UUID uuid, Island i) {
}

/**
* Convenience method. See {@link IslandCache#get(World, UUID)}
* Convenience method. See {@link IslandCache#getIsland(World, UUID)}
*
* @param world world
* @param uuid player's UUID
* @return Island of player or null if there isn't one
*/
public Island getPrimaryIsland(World world, UUID uuid) {
return this.getIslandCache().get(world, uuid);
return this.getIslandCache().getIsland(world, uuid);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void deleteIslandFromCache(@NonNull String uniqueId) {
* @return island or null if none
*/
@Nullable
public Island get(@NonNull World world, @NonNull UUID uuid) {
public Island getIsland(@NonNull World world, @NonNull UUID uuid) {
List<Island> islands = getIslands(world, uuid);
if (islands.isEmpty()) {
return null;
Expand Down
36 changes: 18 additions & 18 deletions src/test/java/world/bentobox/bentobox/TestBentoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ public void setUp() throws Exception {
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
when(offlinePlayer.getName()).thenReturn("tastybento");

when(player.hasPermission(anyString())).thenReturn(true);
when(mockPlayer.hasPermission(anyString())).thenReturn(true);

when(location.getWorld()).thenReturn(world);
when(ownerOfIsland.getLocation()).thenReturn(location);
when(visitorToIsland.getLocation()).thenReturn(location);
when(location.clone()).thenReturn(location);

when(player.getUniqueId()).thenReturn(MEMBER_UUID);
when(mockPlayer.getUniqueId()).thenReturn(MEMBER_UUID);
when(ownerOfIsland.getUniqueId()).thenReturn(uuid);
when(visitorToIsland.getUniqueId()).thenReturn(VISITOR_UUID);

Expand Down Expand Up @@ -151,36 +151,36 @@ public void testCommandAPI() {
String[] args = {""};
// Results are alphabetically sorted
when(Util.tabLimit(any(), any())).thenCallRealMethod();
assertEquals(Arrays.asList("help", "sub1","sub2"), testCommand.tabComplete(player, "test", args));
assertEquals(Arrays.asList("help", "sub1","sub2"), testCommand.tabComplete(mockPlayer, "test", args));
assertNotSame(Arrays.asList("help", "sub1","sub2"), testCommand.tabComplete(sender, "test", args));
args[0] = "su";
assertEquals(Arrays.asList("sub1","sub2"), testCommand.tabComplete(player, "test", args));
assertEquals(Arrays.asList("sub1","sub2"), testCommand.tabComplete(mockPlayer, "test", args));
args[0] = "d";
assertNotSame(Arrays.asList("help", "sub1","sub2"), testCommand.tabComplete(player, "test", args));
assertNotSame(Arrays.asList("help", "sub1","sub2"), testCommand.tabComplete(mockPlayer, "test", args));
args[0] = "sub1";
assertEquals(Collections.emptyList(), testCommand.tabComplete(player, "test", args));
assertEquals(Collections.emptyList(), testCommand.tabComplete(mockPlayer, "test", args));
String[] args2 = {"sub2",""};
assertEquals(Arrays.asList("help", "subsub"), testCommand.tabComplete(player, "test", args2));
assertEquals(Arrays.asList("help", "subsub"), testCommand.tabComplete(mockPlayer, "test", args2));
args2[1] = "s";
assertEquals(Collections.singletonList("subsub"), testCommand.tabComplete(player, "test", args2));
assertEquals(Collections.singletonList("subsub"), testCommand.tabComplete(mockPlayer, "test", args2));
String[] args3 = {"sub2","subsub", ""};
assertEquals(Arrays.asList("help", "subsubsub"), testCommand.tabComplete(player, "test", args3));
assertEquals(Arrays.asList("help", "subsubsub"), testCommand.tabComplete(mockPlayer, "test", args3));
// Test for overridden tabcomplete
assertEquals(Arrays.asList("Ben", "Bill", "Florian", "Ted", "help"),
testCommand.tabComplete(player, "test", new String[] {"sub2", "subsub", "subsubsub", ""}));
testCommand.tabComplete(mockPlayer, "test", new String[] {"sub2", "subsub", "subsubsub", ""}));
// Test for partial word
assertEquals(Arrays.asList("Ben", "Bill"),
testCommand.tabComplete(player, "test", new String[] {"sub2", "subsub", "subsubsub", "b"}));
testCommand.tabComplete(mockPlayer, "test", new String[] {"sub2", "subsub", "subsubsub", "b"}));

// Test command arguments
CompositeCommand argCmd = new Test3ArgsCommand();
argCmd.setOnlyPlayer(true);
argCmd.setPermission("default.permission");
assertTrue(argCmd.execute(player, "args", new String[]{"give", "100", "ben"}));
assertFalse(testCommand.execute(player, "test", new String[] {"sub2", "subsub", "subsubsub"}));
assertFalse(testCommand.execute(player, "test", new String[] {"sub2", "subsub", "subsubsub", "ben"}));
assertFalse(testCommand.execute(player, "test", new String[] {"sub2", "subsub", "subsubsub", "ben", "100"}));
assertTrue(testCommand.execute(player, "test", new String[] {"sub2", "subsub", "subsubsub", "ben", "100", "today"}));
assertTrue(argCmd.execute(mockPlayer, "args", new String[]{"give", "100", "ben"}));
assertFalse(testCommand.execute(mockPlayer, "test", new String[] {"sub2", "subsub", "subsubsub"}));
assertFalse(testCommand.execute(mockPlayer, "test", new String[] {"sub2", "subsub", "subsubsub", "ben"}));
assertFalse(testCommand.execute(mockPlayer, "test", new String[] {"sub2", "subsub", "subsubsub", "ben", "100"}));
assertTrue(testCommand.execute(mockPlayer, "test", new String[] {"sub2", "subsub", "subsubsub", "ben", "100", "today"}));

// Usage tests
assertEquals("/test", testCommand.getUsage());
Expand Down Expand Up @@ -421,8 +421,8 @@ public void testEventProtection() {
Assert.assertTrue(fl.checkIsland(e, ownerOfIsland, location, Flags.BREAK_BLOCKS, true));

// checking events - member
Event e2 = new BlockBreakEvent(block, player);
Assert.assertTrue(fl.checkIsland(e2, player, location, Flags.BREAK_BLOCKS, true));
Event e2 = new BlockBreakEvent(block, mockPlayer);
Assert.assertTrue(fl.checkIsland(e2, mockPlayer, location, Flags.BREAK_BLOCKS, true));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public abstract class AbstractCommonSetup {
protected UUID uuid = UUID.randomUUID();

@Mock
protected Player player;
protected Player mockPlayer;
@Mock
protected PluginManager pim;
@Mock
Expand Down Expand Up @@ -116,15 +116,15 @@ public void setUp() throws Exception {
when(pm.getPlayer(any(UUID.class))).thenReturn(players);

// Player
when(player.getUniqueId()).thenReturn(uuid);
when(player.getLocation()).thenReturn(location);
when(player.getWorld()).thenReturn(world);
when(player.getName()).thenReturn("tastybento");
when(player.getInventory()).thenReturn(inv);
when(mockPlayer.getUniqueId()).thenReturn(uuid);
when(mockPlayer.getLocation()).thenReturn(location);
when(mockPlayer.getWorld()).thenReturn(world);
when(mockPlayer.getName()).thenReturn("tastybento");
when(mockPlayer.getInventory()).thenReturn(inv);

User.setPlugin(plugin);
User.clearUsers();
User.getInstance(player);
User.getInstance(mockPlayer);

// IWM
when(plugin.getIWM()).thenReturn(iwm);
Expand All @@ -150,7 +150,7 @@ public void setUp() throws Exception {

// Enable reporting from Flags class
MetadataValue mdv = new FixedMetadataValue(plugin, "_why_debug");
when(player.getMetadata(anyString())).thenReturn(Collections.singletonList(mdv));
when(mockPlayer.getMetadata(anyString())).thenReturn(Collections.singletonList(mdv));

// Locales & Placeholders
LocalesManager lm = mock(LocalesManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void setUp() throws Exception {
hand = EquipmentSlot.HAND;
// Nothing in hand right now
when(item.getType()).thenReturn(Material.AIR);
when(player.getInventory()).thenReturn(inv);
when(mockPlayer.getInventory()).thenReturn(inv);
when(inv.getItemInMainHand()).thenReturn(item);
when(inv.getItemInOffHand()).thenReturn(new ItemStack(Material.BUCKET));

Expand All @@ -161,7 +161,7 @@ public void setUp() throws Exception {
@Test
public void testOnPlayerInteractItemFrameNotAllowed() {
when(clickedBlock.getType()).thenReturn(Material.ITEM_FRAME);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
bil.onPlayerInteract(e);
assertEquals(Event.Result.DENY, e.useInteractedBlock());
verify(notifier).notify(any(), eq("protection.protected"));
Expand All @@ -175,7 +175,7 @@ public void testOnPlayerInteractItemFrameNotAllowedOtherFlagsOkay() {
when(island.isAllowed(any(), eq(Flags.BREAK_BLOCKS))).thenReturn(true);
when(island.isAllowed(any(), eq(Flags.PLACE_BLOCKS))).thenReturn(true);
when(clickedBlock.getType()).thenReturn(Material.ITEM_FRAME);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
bil.onPlayerInteract(e);
assertEquals(Event.Result.DENY, e.useInteractedBlock());
verify(notifier).notify(any(), eq("protection.protected"));
Expand All @@ -188,7 +188,7 @@ public void testOnPlayerInteractItemFrameNotAllowedOtherFlagsOkay() {
public void testOnPlayerInteractNothingInHandPotsNotAllowed() {
Arrays.stream(Material.values()).filter(m -> m.name().startsWith("POTTED")).forEach(bm -> {
when(clickedBlock.getType()).thenReturn(bm);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
bil.onPlayerInteract(e);
assertEquals("Failure " + bm, Event.Result.DENY, e.useInteractedBlock());
});
Expand All @@ -208,7 +208,7 @@ public void testOnPlayerInteractNothingInHandNotAllowed() {
when(clickedBlock.getState()).thenReturn(sign);
for (Material bm : clickedBlocks.keySet()) {
when(clickedBlock.getType()).thenReturn(bm);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
bil.onPlayerInteract(e);
assertEquals("Failure " + bm, Event.Result.DENY, e.useInteractedBlock());
if (clickedBlocks.get(bm).getType().equals(Type.PROTECTION)) {
Expand All @@ -235,7 +235,7 @@ public void testOnPlayerInteractNothingInHandAllowed() {
clickedBlocks.get(bm).setSetting(world, true);
}
when(clickedBlock.getType()).thenReturn(bm);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
bil.onPlayerInteract(e);
assertNotEquals("Failure " + bm, Event.Result.DENY, e.useInteractedBlock());
verify(notifier, never()).notify(any(), eq("protection.protected"));
Expand All @@ -250,7 +250,7 @@ public void testOnPlayerInteractNothingInHandAllowed() {
public void testOnPlayerInteractSpawnEggInHandNotAllowed() {
when(clickedBlock.getType()).thenReturn(Material.SPAWNER);
when(item.getType()).thenReturn(Material.BLAZE_SPAWN_EGG);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
bil.onPlayerInteract(e);
assertEquals(Event.Result.DENY, e.useInteractedBlock());
assertEquals(Event.Result.DENY, e.useItemInHand());
Expand All @@ -265,7 +265,7 @@ public void testOnPlayerInteractSpawnEggInHandAllowed() {
when(island.isAllowed(any(), any())).thenReturn(true);
when(clickedBlock.getType()).thenReturn(Material.SPAWNER);
when(item.getType()).thenReturn(Material.BLAZE_SPAWN_EGG);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
bil.onPlayerInteract(e);
assertNotEquals(Event.Result.DENY, e.useInteractedBlock());
assertNotEquals(Event.Result.DENY, e.useItemInHand());
Expand All @@ -281,7 +281,7 @@ public void testOnPlayerInteractSpawnEggInHandOnItemFrameNotAllowed() {
when(island.isAllowed(any(), eq(Flags.PLACE_BLOCKS))).thenReturn(true);
when(clickedBlock.getType()).thenReturn(Material.ITEM_FRAME);
when(item.getType()).thenReturn(Material.BLAZE_SPAWN_EGG);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
bil.onPlayerInteract(e);
assertEquals(Event.Result.DENY, e.useInteractedBlock());
assertEquals(Event.Result.DENY, e.useItemInHand());
Expand Down

0 comments on commit 8aba736

Please sign in to comment.