mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-16 21:21:22 +01:00
Code review
This commit is contained in:
parent
8a04d7eed9
commit
5db56312f7
@ -5,6 +5,7 @@ import net.minestom.server.event.entity.EntityTickEvent;
|
||||
import net.minestom.server.event.instance.InstanceChunkLoadEvent;
|
||||
import net.minestom.server.event.instance.InstanceTickEvent;
|
||||
import net.minestom.server.event.inventory.InventoryItemChangeEvent;
|
||||
import net.minestom.server.event.inventory.PlayerInventoryItemChangeEvent;
|
||||
import net.minestom.server.event.player.PlayerChunkLoadEvent;
|
||||
import net.minestom.server.event.player.PlayerMoveEvent;
|
||||
import net.minestom.server.event.player.PlayerPacketEvent;
|
||||
@ -25,4 +26,5 @@ public final class GlobalHandles {
|
||||
public static final ListenerHandle<PlayerChunkLoadEvent> PLAYER_CHUNK_LOAD = EventDispatcher.getHandle(PlayerChunkLoadEvent.class);
|
||||
public static final ListenerHandle<InstanceChunkLoadEvent> INSTANCE_CHUNK_LOAD = EventDispatcher.getHandle(InstanceChunkLoadEvent.class);
|
||||
public static final ListenerHandle<InventoryItemChangeEvent> INVENTORY_ITEM_CHANGE_EVENT = EventDispatcher.getHandle(InventoryItemChangeEvent.class);
|
||||
public static final ListenerHandle<PlayerInventoryItemChangeEvent> PLAYER_INVENTORY_ITEM_CHANGE_EVENT = EventDispatcher.getHandle(PlayerInventoryItemChangeEvent.class);
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package net.minestom.server.event.inventory;
|
||||
import net.minestom.server.event.trait.InventoryEvent;
|
||||
import net.minestom.server.inventory.AbstractInventory;
|
||||
import net.minestom.server.inventory.Inventory;
|
||||
import net.minestom.server.inventory.PlayerInventory;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@ -11,20 +10,20 @@ import org.jetbrains.annotations.Nullable;
|
||||
/**
|
||||
* Called when {@link AbstractInventory#safeItemInsert(int, ItemStack)} is being invoked.
|
||||
* This event cannot be cancelled and items related to the change are already moved.
|
||||
*
|
||||
* @see PlayerInventoryItemChangeEvent
|
||||
*/
|
||||
@SuppressWarnings("JavadocReference")
|
||||
public class InventoryItemChangeEvent implements InventoryEvent {
|
||||
|
||||
private final Inventory inventory;
|
||||
private final PlayerInventory playerInventory;
|
||||
private final int slot;
|
||||
private final ItemStack previousItem;
|
||||
private final ItemStack newItem;
|
||||
|
||||
public InventoryItemChangeEvent(@Nullable Inventory inventory, @Nullable PlayerInventory playerInventory,
|
||||
int slot, @NotNull ItemStack previousItem, @NotNull ItemStack newItem) {
|
||||
public InventoryItemChangeEvent(@Nullable Inventory inventory, int slot,
|
||||
@NotNull ItemStack previousItem, @NotNull ItemStack newItem) {
|
||||
this.inventory = inventory;
|
||||
this.playerInventory = playerInventory;
|
||||
this.slot = slot;
|
||||
this.previousItem = previousItem;
|
||||
this.newItem = newItem;
|
||||
@ -57,16 +56,6 @@ public class InventoryItemChangeEvent implements InventoryEvent {
|
||||
return newItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a player inventory in which an event has occurred.
|
||||
* If event happened in a regular (i.e. not player) inventory, the result is null.
|
||||
*
|
||||
* @return null or a player inventory in which an event has occurred.
|
||||
*/
|
||||
public @Nullable PlayerInventory getPlayerInventory() {
|
||||
return playerInventory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Inventory getInventory() {
|
||||
return inventory;
|
||||
|
@ -0,0 +1,32 @@
|
||||
package net.minestom.server.event.inventory;
|
||||
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.trait.PlayerEvent;
|
||||
import net.minestom.server.event.trait.RecursiveEvent;
|
||||
import net.minestom.server.inventory.AbstractInventory;
|
||||
import net.minestom.server.inventory.PlayerInventory;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Called when {@link AbstractInventory#safeItemInsert(int, ItemStack)} is being invoked on a {@link PlayerInventory}.
|
||||
* This event cannot be cancelled and items related to the change are already moved.
|
||||
*
|
||||
* When this event is being called, {@link InventoryItemChangeEvent} listeners will also be triggered, so you can
|
||||
* listen only for an ancestor event and check whether it is an instance of that class.
|
||||
*/
|
||||
@SuppressWarnings("JavadocReference")
|
||||
public class PlayerInventoryItemChangeEvent extends InventoryItemChangeEvent implements PlayerEvent, RecursiveEvent {
|
||||
|
||||
private final Player player;
|
||||
|
||||
public PlayerInventoryItemChangeEvent(@NotNull Player player, int slot, @NotNull ItemStack previousItem, @NotNull ItemStack newItem) {
|
||||
super(null, slot, previousItem, newItem);
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
}
|
@ -65,20 +65,23 @@ public abstract class AbstractInventory implements InventoryClickHandler, TagHan
|
||||
*
|
||||
* @throws IllegalArgumentException if the slot {@code slot} does not exist
|
||||
*/
|
||||
protected synchronized final void safeItemInsert(int slot, @NotNull ItemStack itemStack) {
|
||||
Check.argCondition(
|
||||
!MathUtils.isBetween(slot, 0, getSize()),
|
||||
"The slot {0} does not exist in this inventory",
|
||||
slot
|
||||
);
|
||||
ItemStack previous = itemStacks[slot];
|
||||
UNSAFE_itemInsert(slot, itemStack);
|
||||
GlobalHandles.INVENTORY_ITEM_CHANGE_EVENT.call(getItemChangeEvent(slot, previous, itemStack));
|
||||
protected final void safeItemInsert(int slot, @NotNull ItemStack itemStack) {
|
||||
ItemStack previous;
|
||||
synchronized (this) {
|
||||
Check.argCondition(
|
||||
!MathUtils.isBetween(slot, 0, getSize()),
|
||||
"The slot {0} does not exist in this inventory",
|
||||
slot
|
||||
);
|
||||
previous = itemStacks[slot];
|
||||
UNSAFE_itemInsert(slot, itemStack);
|
||||
}
|
||||
callItemChangeEvent(slot, previous, itemStack);
|
||||
}
|
||||
|
||||
protected abstract void UNSAFE_itemInsert(int slot, @NotNull ItemStack itemStack);
|
||||
|
||||
protected abstract InventoryItemChangeEvent getItemChangeEvent(int slot, @NotNull ItemStack previous, @NotNull ItemStack current);
|
||||
protected abstract void callItemChangeEvent(int slot, @NotNull ItemStack previous, @NotNull ItemStack current);
|
||||
|
||||
public synchronized <T> @NotNull T processItemStack(@NotNull ItemStack itemStack,
|
||||
@NotNull TransactionType type,
|
||||
|
@ -3,6 +3,7 @@ package net.minestom.server.inventory;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minestom.server.Viewable;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.GlobalHandles;
|
||||
import net.minestom.server.event.inventory.InventoryItemChangeEvent;
|
||||
import net.minestom.server.inventory.click.ClickType;
|
||||
import net.minestom.server.inventory.click.InventoryClickResult;
|
||||
@ -217,8 +218,8 @@ public class Inventory extends AbstractInventory implements Viewable {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InventoryItemChangeEvent getItemChangeEvent(int slot, @NotNull ItemStack previous, @NotNull ItemStack current) {
|
||||
return new InventoryItemChangeEvent(this, null, slot, previous, current);
|
||||
protected void callItemChangeEvent(int slot, @NotNull ItemStack previous, @NotNull ItemStack current) {
|
||||
GlobalHandles.INVENTORY_ITEM_CHANGE_EVENT.call(new InventoryItemChangeEvent(this, slot, previous, current));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,7 +3,9 @@ package net.minestom.server.inventory;
|
||||
import net.minestom.server.entity.EquipmentSlot;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.EventDispatcher;
|
||||
import net.minestom.server.event.GlobalHandles;
|
||||
import net.minestom.server.event.inventory.InventoryItemChangeEvent;
|
||||
import net.minestom.server.event.inventory.PlayerInventoryItemChangeEvent;
|
||||
import net.minestom.server.event.item.EntityEquipEvent;
|
||||
import net.minestom.server.inventory.click.ClickType;
|
||||
import net.minestom.server.inventory.click.InventoryClickResult;
|
||||
@ -179,8 +181,8 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InventoryItemChangeEvent getItemChangeEvent(int slot, @NotNull ItemStack previous, @NotNull ItemStack current) {
|
||||
return new InventoryItemChangeEvent(null, this, slot, previous, current);
|
||||
protected void callItemChangeEvent(int slot, @NotNull ItemStack previous, @NotNull ItemStack current) {
|
||||
GlobalHandles.PLAYER_INVENTORY_ITEM_CHANGE_EVENT.call(new PlayerInventoryItemChangeEvent(player, slot, previous, current));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user