Add bounds check to CreativeInventoryListener (#2042)

* Add bounds check to CreativeInventoryListener

* Add test and change bounds check to exclude crafting output slot
This commit is contained in:
GreatWyrm 2024-03-24 22:47:12 -07:00 committed by GitHub
parent ee17c032e7
commit fa0cd40ac1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 0 deletions

View File

@ -18,6 +18,11 @@ public final class CreativeInventoryActionListener {
player.dropItem(item);
return;
}
// Bounds check
// 0 is crafting result inventory slot, ignore attempts to place into it
if (slot < 1 || slot > PlayerInventoryUtils.OFFHAND_SLOT) {
return;
}
// Set item
slot = (short) PlayerInventoryUtils.convertPlayerInventorySlot(slot, PlayerInventoryUtils.OFFSET);
PlayerInventory inventory = player.getInventory();

View File

@ -0,0 +1,42 @@
package net.minestom.server.inventory;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.GameMode;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.listener.CreativeInventoryActionListener;
import net.minestom.server.network.packet.client.play.ClientCreativeInventoryActionPacket;
import net.minestom.server.utils.inventory.PlayerInventoryUtils;
import net.minestom.testing.Env;
import net.minestom.testing.EnvTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
@EnvTest
public class PlayerCreativeSlotTest {
@Test
public void testCreativeSlots(Env env) {
var instance = env.createFlatInstance();
var connection = env.createConnection();
var player = connection.connect(instance, new Pos(0, 42, 0)).join();
assertEquals(instance, player.getInstance());
player.setGameMode(GameMode.CREATIVE);
player.addPacketToQueue(new ClientCreativeInventoryActionPacket((short) PlayerInventoryUtils.OFFHAND_SLOT, ItemStack.of(Material.STICK)));
player.interpretPacketQueue();
assertEquals(Material.STICK, player.getInventory().getItemInOffHand().material());
}
@Test
public void testBoundsCheck(Env env) {
var instance = env.createFlatInstance();
var connection = env.createConnection();
var player = connection.connect(instance, new Pos(0, 42, 0)).join();
player.setGameMode(GameMode.CREATIVE);
assertDoesNotThrow(() -> CreativeInventoryActionListener.listener(new ClientCreativeInventoryActionPacket((short) 76, ItemStack.of(Material.OAK_LOG)), player));
}
}