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.damage.DamageType;
import net.minestom.server.entity.property.Attribute; import net.minestom.server.entity.property.Attribute;
import net.minestom.server.entity.vehicle.PlayerVehicleInformation; 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.ItemDropEvent;
import net.minestom.server.event.item.ItemUpdateStateEvent; import net.minestom.server.event.item.ItemUpdateStateEvent;
import net.minestom.server.event.item.PickupExperienceEvent; import net.minestom.server.event.item.PickupExperienceEvent;
@ -873,12 +874,22 @@ public class Player extends LivingEntity {
return Collections.unmodifiableSet(bossBars); 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) if (inventory == null)
throw new IllegalArgumentException("Inventory cannot be null, use Player#closeInventory() to close current"); throw new IllegalArgumentException("Inventory cannot be null, use Player#closeInventory() to close current");
InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(this, inventory);
callCancellableEvent(InventoryOpenEvent.class, inventoryOpenEvent, () -> {
if (getOpenInventory() != null) { if (getOpenInventory() != null) {
getOpenInventory().removeViewer(this); closeInventory();
} }
OpenWindowPacket openWindowPacket = new OpenWindowPacket(); OpenWindowPacket openWindowPacket = new OpenWindowPacket();
@ -888,6 +899,10 @@ public class Player extends LivingEntity {
playerConnection.sendPacket(openWindowPacket); playerConnection.sendPacket(openWindowPacket);
inventory.addViewer(this); inventory.addViewer(this);
refreshOpenInventory(inventory); refreshOpenInventory(inventory);
});
return !inventoryOpenEvent.isCancelled();
} }
public void closeInventory() { 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;
}
}