Added InventoryOpenEvent

This commit is contained in:
Felix Cravic 2020-05-12 17:12:11 +02:00
parent 4fa1d08fa5
commit a1b427bf83
2 changed files with 50 additions and 11 deletions

View File

@ -10,6 +10,7 @@ import net.minestom.server.effects.Effects;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.entity.property.Attribute;
import net.minestom.server.entity.vehicle.PlayerVehicleInformation;
import net.minestom.server.event.inventory.InventoryOpenEvent;
import net.minestom.server.event.item.ItemDropEvent;
import net.minestom.server.event.item.ItemUpdateStateEvent;
import net.minestom.server.event.item.PickupExperienceEvent;
@ -873,21 +874,35 @@ public class Player extends LivingEntity {
return Collections.unmodifiableSet(bossBars);
}
public void openInventory(Inventory inventory) {
/**
* Open the specified Inventory
*
* @param inventory the inventory to open
* @return true if the inventory has been opened/sent to the player, false otherwise (cancelled by event)
*/
public boolean openInventory(Inventory inventory) {
if (inventory == null)
throw new IllegalArgumentException("Inventory cannot be null, use Player#closeInventory() to close current");
if (getOpenInventory() != null) {
getOpenInventory().removeViewer(this);
}
InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(this, inventory);
OpenWindowPacket openWindowPacket = new OpenWindowPacket();
openWindowPacket.windowId = inventory.getWindowId();
openWindowPacket.windowType = inventory.getInventoryType().getWindowType();
openWindowPacket.title = inventory.getTitle();
playerConnection.sendPacket(openWindowPacket);
inventory.addViewer(this);
refreshOpenInventory(inventory);
callCancellableEvent(InventoryOpenEvent.class, inventoryOpenEvent, () -> {
if (getOpenInventory() != null) {
closeInventory();
}
OpenWindowPacket openWindowPacket = new OpenWindowPacket();
openWindowPacket.windowId = inventory.getWindowId();
openWindowPacket.windowType = inventory.getInventoryType().getWindowType();
openWindowPacket.title = inventory.getTitle();
playerConnection.sendPacket(openWindowPacket);
inventory.addViewer(this);
refreshOpenInventory(inventory);
});
return !inventoryOpenEvent.isCancelled();
}
public void closeInventory() {

View File

@ -0,0 +1,24 @@
package net.minestom.server.event.inventory;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.inventory.Inventory;
public class InventoryOpenEvent extends CancellableEvent {
private Player player;
private Inventory inventory;
public InventoryOpenEvent(Player player, Inventory inventory) {
this.player = player;
this.inventory = inventory;
}
public Player getPlayer() {
return player;
}
public Inventory getInventory() {
return inventory;
}
}