Event unification (type-specific event, CancellableEvent is now an interface)

This commit is contained in:
themode 2020-12-16 03:21:59 +01:00
parent 7750934a3f
commit 5224eecdd1
65 changed files with 614 additions and 660 deletions

View File

@ -1852,7 +1852,7 @@ public class Player extends LivingEntity implements CommandSender {
public boolean openInventory(@NotNull Inventory inventory) {
Check.notNull(inventory, "Inventory cannot be null, use Player#closeInventory() to close current");
InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(this, inventory);
InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(inventory, this);
callCancellableEvent(InventoryOpenEvent.class, inventoryOpenEvent, () -> {

View File

@ -3,26 +3,20 @@ package net.minestom.server.event;
/**
* Represents an {@link Event} which can be cancelled.
*/
public class CancellableEvent extends Event {
private boolean cancelled;
public interface CancellableEvent {
/**
* Gets if the {@link Event} should be cancelled or not.
*
* @return true if the {@link Event} should be cancelled
* @return true if the event should be cancelled
*/
public boolean isCancelled() {
return cancelled;
}
boolean isCancelled();
/**
* Marks the {@link Event} as cancelled or not.
*
* @param cancel true if the {@link Event} should be cancelled, false otherwise
* @param cancel true if the event should be cancelled, false otherwise
*/
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
void setCancelled(boolean cancel);
}

View File

@ -0,0 +1,23 @@
package net.minestom.server.event;
import net.minestom.server.entity.Entity;
import org.jetbrains.annotations.NotNull;
public class EntityEvent extends Event {
protected final Entity entity;
public EntityEvent(@NotNull Entity entity) {
this.entity = entity;
}
/**
* Gets the entity of this event.
*
* @return the entity
*/
@NotNull
public Entity getEntity() {
return entity;
}
}

View File

@ -0,0 +1,23 @@
package net.minestom.server.event;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
public class InstanceEvent extends Event {
protected final Instance instance;
public InstanceEvent(@NotNull Instance instance) {
this.instance = instance;
}
/**
* Gets the instance.
*
* @return instance
*/
@NotNull
public Instance getInstance() {
return instance;
}
}

View File

@ -0,0 +1,23 @@
package net.minestom.server.event;
import net.minestom.server.inventory.Inventory;
import org.jetbrains.annotations.Nullable;
public class InventoryEvent extends Event {
protected Inventory inventory;
public InventoryEvent(@Nullable Inventory inventory) {
this.inventory = inventory;
}
/**
* Gets the inventory.
*
* @return the inventory, null if this is a player's inventory
*/
@Nullable
public Inventory getInventory() {
return inventory;
}
}

View File

@ -0,0 +1,23 @@
package net.minestom.server.event;
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull;
public class PlayerEvent extends Event {
protected final Player player;
public PlayerEvent(@NotNull Player player) {
this.player = player;
}
/**
* Gets the player.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
}

View File

@ -1,31 +1,22 @@
package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.Event;
import net.minestom.server.event.EntityEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player does a left click on an entity or with
* {@link net.minestom.server.entity.EntityCreature#attack(Entity)}.
*/
public class EntityAttackEvent extends Event {
public class EntityAttackEvent extends EntityEvent {
private final Entity source;
private final Entity target;
public EntityAttackEvent(@NotNull Entity source, @NotNull Entity target) {
this.source = source;
super(source);
this.target = target;
}
/**
* @return the source of the attack
*/
@NotNull
public Entity getSource() {
return source;
}
/**
* @return the target of the attack
*/

View File

@ -3,31 +3,29 @@ package net.minestom.server.event.entity;
import net.minestom.server.entity.LivingEntity;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.EntityEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called with {@link LivingEntity#damage(DamageType, float)}.
*/
public class EntityDamageEvent extends CancellableEvent {
public class EntityDamageEvent extends EntityEvent implements CancellableEvent {
private final LivingEntity entity;
private final DamageType damageType;
private float damage;
private boolean cancelled;
public EntityDamageEvent(@NotNull LivingEntity entity, @NotNull DamageType damageType, float damage) {
this.entity = entity;
super(entity);
this.damageType = damageType;
this.damage = damage;
}
/**
* Gets the damaged entity.
*
* @return the damaged entity
*/
@NotNull
@Override
public LivingEntity getEntity() {
return entity;
return (LivingEntity) entity;
}
/**
@ -57,4 +55,14 @@ public class EntityDamageEvent extends CancellableEvent {
public void setDamage(float damage) {
this.damage = damage;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancelled;
}
}

View File

@ -1,25 +1,14 @@
package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.Event;
import net.minestom.server.event.EntityEvent;
import org.jetbrains.annotations.NotNull;
public class EntityDeathEvent extends Event {
public class EntityDeathEvent extends EntityEvent {
private final Entity entity;
// TODO cause
public EntityDeathEvent(@NotNull Entity entity) {
this.entity = entity;
}
/**
* Get the killed entity,
*
* @return the entity that died
*/
@NotNull
public Entity getEntity() {
return entity;
super(entity);
}
}

View File

@ -2,28 +2,19 @@ package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.EntityEvent;
import net.minestom.server.utils.time.TimeUnit;
import org.jetbrains.annotations.NotNull;
public class EntityFireEvent extends CancellableEvent {
public class EntityFireEvent extends EntityEvent implements CancellableEvent {
private final Entity entity;
private int duration;
private TimeUnit timeUnit;
public EntityFireEvent(Entity entity, int duration, TimeUnit timeUnit) {
this.entity = entity;
setFireTime(duration, timeUnit);
}
private boolean cancelled;
/**
* Gets the entity who got in fire.
*
* @return the entity
*/
@NotNull
public Entity getEntity() {
return entity;
public EntityFireEvent(Entity entity, int duration, TimeUnit timeUnit) {
super(entity);
setFireTime(duration, timeUnit);
}
public long getFireTime(TimeUnit timeUnit) {
@ -43,4 +34,13 @@ public class EntityFireEvent extends CancellableEvent {
this.timeUnit = timeUnit;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,21 +2,22 @@ package net.minestom.server.event.entity;
import net.minestom.server.entity.ItemEntity;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.EntityEvent;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
/**
* Called when two {@link ItemEntity} are merging their {@link ItemStack} together to form a sole entity.
*/
public class EntityItemMergeEvent extends CancellableEvent {
public class EntityItemMergeEvent extends EntityEvent implements CancellableEvent {
private final ItemEntity source;
private final ItemEntity merged;
private ItemStack result;
private boolean cancelled;
public EntityItemMergeEvent(@NotNull ItemEntity source, @NotNull ItemEntity merged, @NotNull ItemStack result) {
this.source = source;
super(source);
this.merged = merged;
this.result = result;
}
@ -29,8 +30,9 @@ public class EntityItemMergeEvent extends CancellableEvent {
* @return the source ItemEntity
*/
@NotNull
public ItemEntity getSource() {
return source;
@Override
public ItemEntity getEntity() {
return (ItemEntity) entity;
}
/**
@ -63,4 +65,14 @@ public class EntityItemMergeEvent extends CancellableEvent {
public void setResult(@NotNull ItemStack result) {
this.result = result;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,20 +1,19 @@
package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.Event;
import net.minestom.server.event.EntityEvent;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
/**
* Called when a new instance is set for an entity.
*/
public class EntitySpawnEvent extends Event {
public class EntitySpawnEvent extends EntityEvent {
private final Entity entity;
private final Instance spawnInstance;
public EntitySpawnEvent(@NotNull Entity entity, @NotNull Instance spawnInstance) {
this.entity = entity;
super(entity);
this.spawnInstance = spawnInstance;
}
@ -24,6 +23,7 @@ public class EntitySpawnEvent extends Event {
* @return the entity
*/
@NotNull
@Override
public Entity getEntity() {
return entity;
}

View File

@ -1,23 +1,17 @@
package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.Event;
import net.minestom.server.event.EntityEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when an entity ticks itself.
* Same event instance used for all tick events for the same entity.
*/
public class EntityTickEvent extends Event {
private final Entity entity;
public class EntityTickEvent extends EntityEvent {
public EntityTickEvent(@NotNull Entity entity) {
this.entity = entity;
super(entity);
}
@NotNull
public Entity getEntity() {
return entity;
}
}

View File

@ -2,19 +2,21 @@ package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.EntityEvent;
import net.minestom.server.utils.Vector;
import org.jetbrains.annotations.NotNull;
/**
* Called when a velocity is applied to an entity using {@link Entity#setVelocity(Vector)}.
*/
public class EntityVelocityEvent extends CancellableEvent {
public class EntityVelocityEvent extends EntityEvent implements CancellableEvent {
private final Entity entity;
private Vector velocity;
private boolean cancelled;
public EntityVelocityEvent(@NotNull Entity entity, @NotNull Vector velocity) {
this.entity = entity;
super(entity);
this.velocity = velocity;
}
@ -24,6 +26,7 @@ public class EntityVelocityEvent extends CancellableEvent {
* @return the entity
*/
@NotNull
@Override
public Entity getEntity() {
return entity;
}
@ -46,4 +49,14 @@ public class EntityVelocityEvent extends CancellableEvent {
public void setVelocity(@NotNull Vector velocity) {
this.velocity = velocity;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -122,7 +122,9 @@ public interface EventHandler {
* @param <E> the event type
* @see #callEvent(Class, Event)
*/
default <E extends CancellableEvent> void callCancellableEvent(@NotNull Class<E> eventClass, @NotNull E event, @NotNull Runnable successCallback) {
default <E extends Event & CancellableEvent> void callCancellableEvent(@NotNull Class<E> eventClass,
@NotNull E event,
@NotNull Runnable successCallback) {
callEvent(eventClass, event);
if (!event.isCancelled()) {
successCallback.run();

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.instance;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.InstanceEvent;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
@ -9,13 +10,14 @@ import org.jetbrains.annotations.NotNull;
* Called by an Instance when an entity is added to it.
* Can be used attach data.
*/
public class AddEntityToInstanceEvent extends CancellableEvent {
public class AddEntityToInstanceEvent extends InstanceEvent implements CancellableEvent {
private final Instance instance;
private final Entity entity;
private boolean cancelled;
public AddEntityToInstanceEvent(@NotNull Instance instance, @NotNull Entity entity) {
this.instance = instance;
super(instance);
this.entity = entity;
}
@ -29,13 +31,13 @@ public class AddEntityToInstanceEvent extends CancellableEvent {
return entity;
}
/**
* Instance in which the entity is being added
*
* @return instance in which the entity is being added
*/
@NotNull
public Instance getInstance() {
return instance;
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,33 +1,22 @@
package net.minestom.server.event.instance;
import net.minestom.server.event.Event;
import net.minestom.server.event.InstanceEvent;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
/**
* Called when a chunk in an instance is loaded.
*/
public class InstanceChunkLoadEvent extends Event {
public class InstanceChunkLoadEvent extends InstanceEvent {
private final Instance instance;
private final int chunkX, chunkZ;
public InstanceChunkLoadEvent(@NotNull Instance instance, int chunkX, int chunkZ) {
this.instance = instance;
super(instance);
this.chunkX = chunkX;
this.chunkZ = chunkZ;
}
/**
* Gets the instance where the chunk has been loaded.
*
* @return the instance
*/
@NotNull
public Instance getInstance() {
return instance;
}
/**
* Gets the chunk X.
*

View File

@ -1,33 +1,22 @@
package net.minestom.server.event.instance;
import net.minestom.server.event.Event;
import net.minestom.server.event.InstanceEvent;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
/**
* Called when a chunk in an instance is unloaded.
*/
public class InstanceChunkUnloadEvent extends Event {
public class InstanceChunkUnloadEvent extends InstanceEvent {
private final Instance instance;
private final int chunkX, chunkZ;
public InstanceChunkUnloadEvent(@NotNull Instance instance, int chunkX, int chunkZ) {
this.instance = instance;
super(instance);
this.chunkX = chunkX;
this.chunkZ = chunkZ;
}
/**
* Gets the instance where the chunk has been unloaded.
*
* @return the instance
*/
@NotNull
public Instance getInstance() {
return instance;
}
/**
* Gets the chunk X.
*

View File

@ -1,20 +1,19 @@
package net.minestom.server.event.instance;
import net.minestom.server.event.Event;
import net.minestom.server.event.InstanceEvent;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
/**
* Called when an instance processes a tick.
*/
public class InstanceTickEvent extends Event {
public class InstanceTickEvent extends InstanceEvent {
private final int duration;
private final Instance instance;
public InstanceTickEvent(@NotNull Instance instance, long time, long lastTickAge) {
super(instance);
this.duration = (int) (time - lastTickAge);
this.instance = instance;
}
/**
@ -25,14 +24,4 @@ public class InstanceTickEvent extends Event {
public int getDuration() {
return duration;
}
/**
* Gets the instance of the event.
*
* @return the instance
*/
@NotNull
public Instance getInstance() {
return instance;
}
}

View File

@ -2,19 +2,21 @@ package net.minestom.server.event.instance;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.InstanceEvent;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
/**
* Called by an Instance when an entity is removed from it.
*/
public class RemoveEntityFromInstanceEvent extends CancellableEvent {
public class RemoveEntityFromInstanceEvent extends InstanceEvent implements CancellableEvent {
private final Instance instance;
private final Entity entity;
private boolean cancelled;
public RemoveEntityFromInstanceEvent(@NotNull Instance instance, @NotNull Entity entity) {
this.instance = instance;
super(instance);
this.entity = entity;
}
@ -28,13 +30,13 @@ public class RemoveEntityFromInstanceEvent extends CancellableEvent {
return entity;
}
/**
* Instance from which the entity is being removed.
*
* @return instance from which the entity is being removed
*/
@NotNull
public Instance getInstance() {
return instance;
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,7 +1,7 @@
package net.minestom.server.event.inventory;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.InventoryEvent;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.click.ClickType;
import net.minestom.server.item.ItemStack;
@ -12,20 +12,19 @@ import org.jetbrains.annotations.Nullable;
* Called after {@link InventoryPreClickEvent}, this event cannot be cancelled and items related to the click
* are already moved.
*/
public class InventoryClickEvent extends Event {
public class InventoryClickEvent extends InventoryEvent {
private final Player player;
private final Inventory inventory;
private final int slot;
private final ClickType clickType;
private final ItemStack clickedItem;
private final ItemStack cursorItem;
public InventoryClickEvent(@NotNull Player player, Inventory inventory,
public InventoryClickEvent(@Nullable Inventory inventory, @NotNull Player player,
int slot, @NotNull ClickType clickType,
@NotNull ItemStack clicked, @NotNull ItemStack cursor) {
super(inventory);
this.player = player;
this.inventory = inventory;
this.slot = slot;
this.clickType = clickType;
this.clickedItem = clicked;
@ -42,16 +41,6 @@ public class InventoryClickEvent extends Event {
return player;
}
/**
* Can be null if the clicked inventory is the player one.
*
* @return the inventory where the click happened, null if this is the player's inventory
*/
@Nullable
public Inventory getInventory() {
return inventory;
}
/**
* Gets the clicked slot number.
*

View File

@ -1,7 +1,7 @@
package net.minestom.server.event.inventory;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.InventoryEvent;
import net.minestom.server.inventory.Inventory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -9,15 +9,14 @@ import org.jetbrains.annotations.Nullable;
/**
* Called when an {@link Inventory} is closed by a player.
*/
public class InventoryCloseEvent extends Event {
public class InventoryCloseEvent extends InventoryEvent {
private final Player player;
private final Inventory inventory;
private Inventory newInventory;
public InventoryCloseEvent(@NotNull Player player, @Nullable Inventory inventory) {
public InventoryCloseEvent(@Nullable Inventory inventory, @NotNull Player player) {
super(inventory);
this.player = player;
this.inventory = inventory;
}
/**
@ -30,16 +29,6 @@ public class InventoryCloseEvent extends Event {
return player;
}
/**
* Gets the closed inventory.
*
* @return the closed inventory, null if this is the player inventory
*/
@Nullable
public Inventory getInventory() {
return inventory;
}
/**
* Gets the new inventory to open.
*

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.inventory;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.InventoryEvent;
import net.minestom.server.inventory.Inventory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -11,14 +12,15 @@ import org.jetbrains.annotations.Nullable;
* <p>
* Executed by {@link Player#openInventory(Inventory)}.
*/
public class InventoryOpenEvent extends CancellableEvent {
public class InventoryOpenEvent extends InventoryEvent implements CancellableEvent {
private final Player player;
private Inventory inventory;
public InventoryOpenEvent(@NotNull Player player, @Nullable Inventory inventory) {
private boolean cancelled;
public InventoryOpenEvent(@Nullable Inventory inventory, @NotNull Player player) {
super(inventory);
this.player = player;
this.inventory = inventory;
}
/**
@ -37,6 +39,7 @@ public class InventoryOpenEvent extends CancellableEvent {
* @return the inventory to open, null to just close the current inventory if any
*/
@Nullable
@Override
public Inventory getInventory() {
return inventory;
}
@ -51,4 +54,14 @@ public class InventoryOpenEvent extends CancellableEvent {
public void setInventory(@Nullable Inventory inventory) {
this.inventory = inventory;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.inventory;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.InventoryEvent;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.click.ClickType;
import net.minestom.server.item.ItemStack;
@ -11,20 +12,22 @@ import org.jetbrains.annotations.Nullable;
/**
* Called before {@link InventoryClickEvent}, used to potentially cancel the click.
*/
public class InventoryPreClickEvent extends CancellableEvent {
public class InventoryPreClickEvent extends InventoryEvent implements CancellableEvent {
private final Player player;
private final Inventory inventory;
private final int slot;
private final ClickType clickType;
private ItemStack clickedItem;
private ItemStack cursorItem;
public InventoryPreClickEvent(@NotNull Player player, @Nullable Inventory inventory,
private boolean cancelled;
public InventoryPreClickEvent(@Nullable Inventory inventory,
@NotNull Player player,
int slot, @NotNull ClickType clickType,
@NotNull ItemStack clicked, @NotNull ItemStack cursor) {
super(inventory);
this.player = player;
this.inventory = inventory;
this.slot = slot;
this.clickType = clickType;
this.clickedItem = clicked;
@ -41,16 +44,6 @@ public class InventoryPreClickEvent extends CancellableEvent {
return player;
}
/**
* Can be null if the clicked inventory is the player one.
*
* @return the inventory where the click happened, null if this is the player's inventory
*/
@Nullable
public Inventory getInventory() {
return inventory;
}
/**
* Gets the clicked slot number.
*
@ -107,4 +100,14 @@ public class InventoryPreClickEvent extends CancellableEvent {
public void setCursorItem(@NotNull ItemStack cursorItem) {
this.cursorItem = cursorItem;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,14 +2,17 @@ package net.minestom.server.event.item;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.Event;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
public class ItemDropEvent extends CancellableEvent {
public class ItemDropEvent extends Event implements CancellableEvent {
private final Player player;
private final ItemStack itemStack;
private boolean cancelled;
public ItemDropEvent(@NotNull Player player, @NotNull ItemStack itemStack) {
this.player = player;
this.itemStack = itemStack;
@ -24,4 +27,14 @@ public class ItemDropEvent extends CancellableEvent {
public ItemStack getItemStack() {
return itemStack;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,13 +2,16 @@ package net.minestom.server.event.item;
import net.minestom.server.entity.ExperienceOrb;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.Event;
import org.jetbrains.annotations.NotNull;
public class PickupExperienceEvent extends CancellableEvent {
public class PickupExperienceEvent extends Event implements CancellableEvent {
private final ExperienceOrb experienceOrb;
private short experienceCount;
private boolean cancelled;
public PickupExperienceEvent(@NotNull ExperienceOrb experienceOrb) {
this.experienceOrb = experienceOrb;
this.experienceCount = experienceOrb.getExperienceCount();
@ -26,4 +29,14 @@ public class PickupExperienceEvent extends CancellableEvent {
public void setExperienceCount(short experienceCount) {
this.experienceCount = experienceCount;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,14 +2,17 @@ package net.minestom.server.event.item;
import net.minestom.server.entity.LivingEntity;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.Event;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
public class PickupItemEvent extends CancellableEvent {
public class PickupItemEvent extends Event implements CancellableEvent {
private final LivingEntity livingEntity;
private final ItemStack itemStack;
private boolean cancelled;
public PickupItemEvent(@NotNull LivingEntity livingEntity, @NotNull ItemStack itemStack) {
this.livingEntity = livingEntity;
this.itemStack = itemStack;
@ -24,4 +27,14 @@ public class PickupItemEvent extends CancellableEvent {
public ItemStack getItemStack() {
return itemStack;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,35 +2,24 @@ package net.minestom.server.event.player;
import net.minestom.server.advancements.AdvancementAction;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a {@link Player} opens the advancement screens or switch the tab
* and when he closes the screen.
*/
public class AdvancementTabEvent extends Event {
public class AdvancementTabEvent extends PlayerEvent {
private final Player player;
private final AdvancementAction action;
private final String tabId;
public AdvancementTabEvent(@NotNull Player player, @NotNull AdvancementAction action, @NotNull String tabId) {
this.player = player;
super(player);
this.action = action;
this.tabId = tabId;
}
/**
* Gets the {@link Player} responsible for the event.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the action.
*

View File

@ -2,30 +2,22 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
/**
* Called as a result of {@link net.minestom.server.inventory.PlayerInventory#addItemStack(ItemStack)}.
*/
public class PlayerAddItemStackEvent extends CancellableEvent {
public class PlayerAddItemStackEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private ItemStack itemStack;
public PlayerAddItemStackEvent(@NotNull Player player, @NotNull ItemStack itemStack) {
this.player = player;
this.itemStack = itemStack;
}
private boolean cancelled;
/**
* Gets the player who has an item stack added to his inventory.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
public PlayerAddItemStackEvent(@NotNull Player player, @NotNull ItemStack itemStack) {
super(player);
this.itemStack = itemStack;
}
/**
@ -44,6 +36,16 @@ public class PlayerAddItemStackEvent extends CancellableEvent {
* @param itemStack the new item stack
*/
public void setItemStack(@NotNull ItemStack itemStack) {
this.itemStack =itemStack;
this.itemStack = itemStack;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,14 +2,13 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PlayerBlockBreakEvent extends CancellableEvent {
private final Player player;
public class PlayerBlockBreakEvent extends PlayerEvent implements CancellableEvent {
private final BlockPosition blockPosition;
@ -19,10 +18,12 @@ public class PlayerBlockBreakEvent extends CancellableEvent {
private short resultBlockStateId;
private short resultCustomBlockId;
private boolean cancelled;
public PlayerBlockBreakEvent(@NotNull Player player, @NotNull BlockPosition blockPosition,
short blockStateId, @Nullable CustomBlock customBlock,
short resultBlockStateId, short resultCustomBlockId) {
this.player = player;
super(player);
this.blockPosition = blockPosition;
@ -33,16 +34,6 @@ public class PlayerBlockBreakEvent extends CancellableEvent {
this.resultCustomBlockId = resultCustomBlockId;
}
/**
* Gets the player who breaks the block.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the block position.
*
@ -113,4 +104,14 @@ public class PlayerBlockBreakEvent extends CancellableEvent {
public void setResultCustomBlockId(short resultCustomBlockId) {
this.resultCustomBlockId = resultCustomBlockId;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull;
@ -10,9 +11,8 @@ import org.jetbrains.annotations.NotNull;
* Called when a player interacts with a block (right-click).
* This is also called when a block is placed.
*/
public class PlayerBlockInteractEvent extends CancellableEvent {
public class PlayerBlockInteractEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private final BlockPosition blockPosition;
private final Player.Hand hand;
private final BlockFace blockFace;
@ -23,24 +23,16 @@ public class PlayerBlockInteractEvent extends CancellableEvent {
*/
private boolean blocksItemUse;
private boolean cancelled;
public PlayerBlockInteractEvent(@NotNull Player player,
@NotNull BlockPosition blockPosition, @NotNull Player.Hand hand, @NotNull BlockFace blockFace) {
this.player = player;
super(player);
this.blockPosition = blockPosition;
this.hand = hand;
this.blockFace = blockFace;
}
/**
* Gets the player who interacted with the block.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets if the event should block the item use.
*
@ -83,4 +75,14 @@ public class PlayerBlockInteractEvent extends CancellableEvent {
public BlockFace getBlockFace() {
return blockFace;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -3,6 +3,7 @@ package net.minestom.server.event.player;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.utils.BlockPosition;
@ -11,11 +12,10 @@ import org.jetbrains.annotations.NotNull;
/**
* Called when a player tries placing a block.
*/
public class PlayerBlockPlaceEvent extends CancellableEvent {
public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEvent {
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
private final Player player;
private short blockStateId;
private short customBlockId;
private final BlockPosition blockPosition;
@ -23,9 +23,11 @@ public class PlayerBlockPlaceEvent extends CancellableEvent {
private boolean consumeBlock;
private boolean cancelled;
public PlayerBlockPlaceEvent(@NotNull Player player, short blockStateId, short customBlockId,
@NotNull BlockPosition blockPosition, @NotNull Player.Hand hand) {
this.player = player;
super(player);
this.blockStateId = blockStateId;
this.customBlockId = customBlockId;
this.blockPosition = blockPosition;
@ -102,16 +104,6 @@ public class PlayerBlockPlaceEvent extends CancellableEvent {
this.blockStateId = blockStateId;
}
/**
* Gets the player who is placing the block.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the block position.
*
@ -149,4 +141,14 @@ public class PlayerBlockPlaceEvent extends CancellableEvent {
public boolean doesConsumeBlock() {
return consumeBlock;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
@ -9,24 +10,15 @@ import org.jetbrains.annotations.NotNull;
/**
* Called when a player change his held slot (by pressing 1-9 keys).
*/
public class PlayerChangeHeldSlotEvent extends CancellableEvent {
public class PlayerChangeHeldSlotEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private byte slot;
public PlayerChangeHeldSlotEvent(@NotNull Player player, byte slot) {
this.player = player;
this.slot = slot;
}
private boolean cancelled;
/**
* Gets the player who changed his held slot.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
public PlayerChangeHeldSlotEvent(@NotNull Player player, byte slot) {
super(player);
this.slot = slot;
}
/**
@ -48,4 +40,14 @@ public class PlayerChangeHeldSlotEvent extends CancellableEvent {
Check.argCondition(!MathUtils.isBetween(slot, 0, 8), "The held slot needs to be between 0 and 8");
this.slot = slot;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -3,6 +3,7 @@ package net.minestom.server.event.player;
import net.minestom.server.chat.RichMessage;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -14,15 +15,16 @@ import java.util.function.Function;
* Called every time a {@link Player} write and send something in the chat.
* The event can be cancelled to do not send anything, and the format can be changed.
*/
public class PlayerChatEvent extends CancellableEvent {
public class PlayerChatEvent extends PlayerEvent implements CancellableEvent {
private final Player sender;
private final Collection<Player> recipients;
private String message;
private Function<PlayerChatEvent, RichMessage> chatFormat;
public PlayerChatEvent(@NotNull Player sender, @NotNull Collection<Player> recipients, @NotNull String message) {
this.sender = sender;
private boolean cancelled;
public PlayerChatEvent(@NotNull Player player, @NotNull Collection<Player> recipients, @NotNull String message) {
super(player);
this.recipients = new ArrayList<>(recipients);
this.message = message;
}
@ -36,16 +38,6 @@ public class PlayerChatEvent extends CancellableEvent {
this.chatFormat = chatFormat;
}
/**
* Gets the message sender.
*
* @return the sender
*/
@NotNull
public Player getSender() {
return sender;
}
/**
* Those are the players who will receive the message.
* <p>
@ -88,4 +80,14 @@ public class PlayerChatEvent extends CancellableEvent {
public Function<PlayerChatEvent, RichMessage> getChatFormatFunction() {
return chatFormat;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,33 +1,22 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player receive a new chunk data.
*/
public class PlayerChunkLoadEvent extends Event {
public class PlayerChunkLoadEvent extends PlayerEvent {
private final Player player;
private final int chunkX, chunkZ;
public PlayerChunkLoadEvent(@NotNull Player player, int chunkX, int chunkZ) {
this.player = player;
super(player);
this.chunkX = chunkX;
this.chunkZ = chunkZ;
}
/**
* Gets the player.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the chunk X.
*

View File

@ -1,7 +1,7 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
@ -9,27 +9,16 @@ import org.jetbrains.annotations.NotNull;
* <p>
* Could be used to unload the chunk internally in order to save memory.
*/
public class PlayerChunkUnloadEvent extends Event {
public class PlayerChunkUnloadEvent extends PlayerEvent {
private final Player player;
private final int chunkX, chunkZ;
public PlayerChunkUnloadEvent(@NotNull Player player, int chunkX, int chunkZ) {
this.player = player;
super(player);
this.chunkX = chunkX;
this.chunkZ = chunkZ;
}
/**
* Gets the player.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the chunk X.
*

View File

@ -2,29 +2,21 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called every time a player send a message starting by '/'.
*/
public class PlayerCommandEvent extends CancellableEvent {
public class PlayerCommandEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private String command;
public PlayerCommandEvent(@NotNull Player player, @NotNull String command) {
this.player = player;
this.command = command;
}
private boolean cancelled;
/**
* Gets the player who sent the command.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
public PlayerCommandEvent(@NotNull Player player, @NotNull String command) {
super(player);
this.command = command;
}
/**
@ -45,4 +37,14 @@ public class PlayerCommandEvent extends CancellableEvent {
public void setCommand(@NotNull String command) {
this.command = command;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,27 +1,15 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player disconnect.
*/
public class PlayerDisconnectEvent extends Event {
private final Player player;
public class PlayerDisconnectEvent extends PlayerEvent {
public PlayerDisconnectEvent(@NotNull Player player) {
this.player = player;
}
/**
* Gets the player who is disconnecting.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
super(player);
}
}

View File

@ -1,33 +1,22 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player is finished eating.
*/
public class PlayerEatEvent extends Event {
public class PlayerEatEvent extends PlayerEvent {
private final Player player;
private final ItemStack foodItem;
public PlayerEatEvent(@NotNull Player player, @NotNull ItemStack foodItem) {
this.player = player;
super(player);
this.foodItem = foodItem;
}
/**
* Gets the player who is finished eating.
*
* @return the concerned player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the food item that has been eaten.
*

View File

@ -2,34 +2,23 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a {@link Player} interacts (right-click) with an {@link Entity}.
*/
public class PlayerEntityInteractEvent extends Event {
public class PlayerEntityInteractEvent extends PlayerEvent {
private final Player player;
private final Entity entityTarget;
private final Player.Hand hand;
public PlayerEntityInteractEvent(@NotNull Player player, @NotNull Entity entityTarget, @NotNull Player.Hand hand) {
this.player = player;
super(player);
this.entityTarget = entityTarget;
this.hand = hand;
}
/**
* Gets the {@link Player} who is interacting.
*
* @return the {@link Player}
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the {@link Entity} with who {@link #getPlayer()} is interacting.
*

View File

@ -2,29 +2,21 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when the player swings his hand.
*/
public class PlayerHandAnimationEvent extends CancellableEvent {
public class PlayerHandAnimationEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private final Player.Hand hand;
public PlayerHandAnimationEvent(@NotNull Player player, @NotNull Player.Hand hand) {
this.player = player;
this.hand = hand;
}
private boolean cancelled;
/**
* The player who is swinging his arm.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
public PlayerHandAnimationEvent(@NotNull Player player, @NotNull Player.Hand hand) {
super(player);
this.hand = hand;
}
/**
@ -36,4 +28,14 @@ public class PlayerHandAnimationEvent extends CancellableEvent {
public Player.Hand getHand() {
return hand;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
@ -9,24 +10,15 @@ import org.jetbrains.annotations.NotNull;
*
* @see ItemAnimationType
*/
public class PlayerItemAnimationEvent extends CancellableEvent {
public class PlayerItemAnimationEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private final ItemAnimationType armAnimationType;
public PlayerItemAnimationEvent(@NotNull Player player, @NotNull ItemAnimationType armAnimationType) {
this.player = player;
this.armAnimationType = armAnimationType;
}
private boolean cancelled;
/**
* Gets the {@link Player} who is responsible for the animation.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
public PlayerItemAnimationEvent(@NotNull Player player, @NotNull ItemAnimationType armAnimationType) {
super(player);
this.armAnimationType = armAnimationType;
}
/**
@ -47,4 +39,13 @@ public class PlayerItemAnimationEvent extends CancellableEvent {
EAT
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,7 +1,7 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -16,23 +16,12 @@ import org.jetbrains.annotations.Nullable;
* <p>
* WARNING: defining the spawning instance is MANDATORY.
*/
public class PlayerLoginEvent extends Event {
public class PlayerLoginEvent extends PlayerEvent {
private final Player player;
private Instance spawningInstance;
public PlayerLoginEvent(@NotNull Player player) {
this.player = player;
}
/**
* Gets the player who is logging.
*
* @return the player who is logging
*/
@NotNull
public Player getPlayer() {
return player;
super(player);
}
/**

View File

@ -2,30 +2,22 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.utils.Position;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player is modifying his position.
*/
public class PlayerMoveEvent extends CancellableEvent {
public class PlayerMoveEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private Position newPosition;
public PlayerMoveEvent(@NotNull Player player, @NotNull Position newPosition) {
this.player = player;
this.newPosition = newPosition;
}
private boolean cancelled;
/**
* Gets the player who is moving.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
public PlayerMoveEvent(@NotNull Player player, @NotNull Position newPosition) {
super(player);
this.newPosition = newPosition;
}
/**
@ -46,4 +38,14 @@ public class PlayerMoveEvent extends CancellableEvent {
public void setNewPosition(@NotNull Position newPosition) {
this.newPosition = newPosition;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,34 +1,23 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player send {@link net.minestom.server.network.packet.client.play.ClientPluginMessagePacket}.
*/
public class PlayerPluginMessageEvent extends Event {
public class PlayerPluginMessageEvent extends PlayerEvent {
private final Player player;
private final String identifier;
private final byte[] message;
public PlayerPluginMessageEvent(@NotNull Player player, @NotNull String identifier, @NotNull byte[] message) {
this.player = player;
super(player);
this.identifier = identifier;
this.message = message;
}
/**
* Gets the player who sent the message.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the message identifier.
*

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
@ -10,28 +11,19 @@ import org.jetbrains.annotations.NotNull;
* or to cancel its processing, cancelling the event means that the player will
* continue the animation indefinitely.
*/
public class PlayerPreEatEvent extends CancellableEvent {
public class PlayerPreEatEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private final ItemStack foodItem;
private long eatingTime;
private boolean cancelled;
public PlayerPreEatEvent(@NotNull Player player, @NotNull ItemStack foodItem, long eatingTime) {
this.player = player;
super(player);
this.foodItem = foodItem;
this.eatingTime = eatingTime;
}
/**
* The player who is trying to eat.
*
* @return the concerned player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* The food item which will be eaten.
*
@ -61,4 +53,14 @@ public class PlayerPreEatEvent extends CancellableEvent {
public void setEatingTime(long eatingTime) {
this.eatingTime = eatingTime;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,7 +1,7 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
@ -11,28 +11,17 @@ import java.util.UUID;
* Called before the player initialization, it can be used to kick the player before any connection
* or to change his final username/uuid.
*/
public class PlayerPreLoginEvent extends Event {
public class PlayerPreLoginEvent extends PlayerEvent {
private final Player player;
private String username;
private UUID playerUuid;
public PlayerPreLoginEvent(@NotNull Player player, @NotNull String username, @NotNull UUID playerUuid) {
this.player = player;
super(player);
this.username = username;
this.playerUuid = playerUuid;
}
/**
* Gets the player who is trying to connect.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the player username.
*

View File

@ -1,33 +1,22 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.resourcepack.ResourcePackStatus;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player warns the server of a resource pack status.
*/
public class PlayerResourcePackStatusEvent extends Event {
public class PlayerResourcePackStatusEvent extends PlayerEvent {
private final Player player;
private final ResourcePackStatus status;
public PlayerResourcePackStatusEvent(@NotNull Player player, @NotNull ResourcePackStatus status) {
this.player = player;
super(player);
this.status = status;
}
/**
* Gets the player who send a resource pack status.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the resource pack status.
*

View File

@ -1,7 +1,7 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.utils.Position;
import org.jetbrains.annotations.NotNull;
@ -9,26 +9,15 @@ import org.jetbrains.annotations.NotNull;
* Called when {@link Player#respawn()} is executed (for custom respawn or as a result of
* {@link net.minestom.server.network.packet.client.play.ClientStatusPacket}
*/
public class PlayerRespawnEvent extends Event {
public class PlayerRespawnEvent extends PlayerEvent {
private final Player player;
private Position respawnPosition;
public PlayerRespawnEvent(@NotNull Player player) {
this.player = player;
super(player);
this.respawnPosition = player.getRespawnPoint();
}
/**
* Gets the player who is respawning.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the respawn position.
* <p>

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
@ -9,28 +10,19 @@ import org.jetbrains.annotations.NotNull;
* Called as a result of {@link net.minestom.server.inventory.PlayerInventory#setItemStack(int, ItemStack)}
* and player click in his inventory.
*/
public class PlayerSetItemStackEvent extends CancellableEvent {
public class PlayerSetItemStackEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private int slot;
private ItemStack itemStack;
private boolean cancelled;
public PlayerSetItemStackEvent(@NotNull Player player, int slot, @NotNull ItemStack itemStack) {
this.player = player;
super(player);
this.slot = slot;
this.itemStack = itemStack;
}
/**
* Gets the player who has an item stack set to his inventory.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the slot where the item will be set.
*
@ -68,4 +60,13 @@ public class PlayerSetItemStackEvent extends CancellableEvent {
this.itemStack = itemStack;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,18 +1,16 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called after the player signals the server that his settings has been modified.
*/
public class PlayerSettingsChangeEvent extends Event {
private final Player player;
public class PlayerSettingsChangeEvent extends PlayerEvent {
public PlayerSettingsChangeEvent(@NotNull Player player) {
this.player = player;
super(player);
}
/**
@ -23,6 +21,7 @@ public class PlayerSettingsChangeEvent extends Event {
* @return the player
*/
@NotNull
@Override
public Player getPlayer() {
return player;
}

View File

@ -2,33 +2,22 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.PlayerSkin;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Called at the player connection to initialize his skin.
*/
public class PlayerSkinInitEvent extends Event {
public class PlayerSkinInitEvent extends PlayerEvent {
private final Player player;
private PlayerSkin skin;
public PlayerSkinInitEvent(@NotNull Player player, @Nullable PlayerSkin currentSkin) {
this.player = player;
super(player);
this.skin = currentSkin;
}
/**
* Gets the player whose the skin is getting initialized.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the spawning skin of the player.
*

View File

@ -1,33 +1,32 @@
package net.minestom.server.event.player;
import org.jetbrains.annotations.NotNull;
import net.minestom.server.entity.Player;
import net.minestom.server.event.entity.EntitySpawnEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
/**
* Called when a new instance is set for a player.
*/
public class PlayerSpawnEvent extends EntitySpawnEvent {
public class PlayerSpawnEvent extends PlayerEvent {
private final Instance spawnInstance;
private final boolean firstSpawn;
public PlayerSpawnEvent(@NotNull Player player, @NotNull Instance spawnInstance, boolean firstSpawn) {
super(player, spawnInstance);
super(player);
this.spawnInstance = spawnInstance;
this.firstSpawn = firstSpawn;
}
/**
* Gets the player who spawned.
* <p>
* Shortcut for casting {@link #getEntity()}.
* Gets the entity new instance.
*
* @return
* @return the instance
*/
@NotNull
public Player getPlayer() {
return (Player) getEntity();
public Instance getSpawnInstance() {
return spawnInstance;
}
/**

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull;
@ -13,30 +14,21 @@ import org.jetbrains.annotations.NotNull;
* (could be because of high latency or a modified client) so cancelling {@link PlayerBlockBreakEvent} is also necessary.
* Could be fixed in future Minestom version.
*/
public class PlayerStartDiggingEvent extends CancellableEvent {
public class PlayerStartDiggingEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private final BlockPosition blockPosition;
private final int blockStateId;
private final int customBlockId;
private boolean cancelled;
public PlayerStartDiggingEvent(@NotNull Player player, @NotNull BlockPosition blockPosition, int blockStateId, int customBlockId) {
this.player = player;
super(player);
this.blockPosition = blockPosition;
this.blockStateId = blockStateId;
this.customBlockId = customBlockId;
}
/**
* Gets the {@link Player} who started digging the block.
*
* @return the {@link Player}
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the {@link BlockPosition}.
*
@ -64,4 +56,14 @@ public class PlayerStartDiggingEvent extends CancellableEvent {
public int getCustomBlockId() {
return customBlockId;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,28 +1,15 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player start flying.
*/
public class PlayerStartFlyingEvent extends Event {
private final Player player;
public class PlayerStartFlyingEvent extends PlayerEvent {
public PlayerStartFlyingEvent(@NotNull Player player) {
this.player = player;
super(player);
}
/**
* Gets the player who started flying.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
}

View File

@ -1,27 +1,15 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player stop flying.
*/
public class PlayerStopFlyingEvent extends Event {
private final Player player;
public class PlayerStopFlyingEvent extends PlayerEvent {
public PlayerStopFlyingEvent(@NotNull Player player) {
this.player = player;
}
/**
* Gets the player who stopped flying.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
super(player);
}
}

View File

@ -2,34 +2,26 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player is trying to swap his main and off hand item.
*/
public class PlayerSwapItemEvent extends CancellableEvent {
public class PlayerSwapItemEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private ItemStack mainHandItem;
private ItemStack offHandItem;
private boolean cancelled;
public PlayerSwapItemEvent(@NotNull Player player, @NotNull ItemStack mainHandItem, @NotNull ItemStack offHandItem) {
this.player = player;
super(player);
this.mainHandItem = mainHandItem;
this.offHandItem = offHandItem;
}
/**
* Gets the player who is trying to swap his hands item.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the item which will be in player main hand after the event.
*
@ -67,4 +59,14 @@ public class PlayerSwapItemEvent extends CancellableEvent {
public void setOffHandItem(@NotNull ItemStack offHandItem) {
this.offHandItem = offHandItem;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,27 +1,15 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called at each player tick.
*/
public class PlayerTickEvent extends Event {
private final Player player;
public class PlayerTickEvent extends PlayerEvent {
public PlayerTickEvent(@NotNull Player player) {
this.player = player;
}
/**
* Gets the player.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
super(player);
}
}

View File

@ -2,34 +2,26 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
/**
* Event when an item is used without clicking on a block.
*/
public class PlayerUseItemEvent extends CancellableEvent {
public class PlayerUseItemEvent extends PlayerEvent implements CancellableEvent {
private final Player player;
private final Player.Hand hand;
private final ItemStack itemStack;
private boolean cancelled;
public PlayerUseItemEvent(@NotNull Player player, @NotNull Player.Hand hand, @NotNull ItemStack itemStack) {
this.player = player;
super(player);
this.hand = hand;
this.itemStack = itemStack;
}
/**
* Gets the player who used an item.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets which hand the player used.
*
@ -49,4 +41,14 @@ public class PlayerUseItemEvent extends CancellableEvent {
public ItemStack getItemStack() {
return itemStack;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -1,7 +1,7 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.Event;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.item.ItemStack;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Direction;
@ -10,9 +10,8 @@ import org.jetbrains.annotations.NotNull;
/**
* Used when a player is clicking on a block with an item (but is not a block in item form).
*/
public class PlayerUseItemOnBlockEvent extends Event {
public class PlayerUseItemOnBlockEvent extends PlayerEvent {
private final Player player;
private final Player.Hand hand;
private final ItemStack itemStack;
private final BlockPosition position;
@ -21,23 +20,13 @@ public class PlayerUseItemOnBlockEvent extends Event {
public PlayerUseItemOnBlockEvent(@NotNull Player player, @NotNull Player.Hand hand,
@NotNull ItemStack itemStack,
@NotNull BlockPosition position, @NotNull Direction blockFace) {
this.player = player;
super(player);
this.hand = hand;
this.itemStack = itemStack;
this.position = position;
this.blockFace = blockFace;
}
/**
* Gets the player who used an item while clicking on a block.
*
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
}
/**
* Gets the position of the interacted block.
*

View File

@ -77,7 +77,7 @@ public interface InventoryClickHandler {
default void callClickEvent(@NotNull Player player, Inventory inventory, int slot,
@NotNull ClickType clickType, @NotNull ItemStack clicked, @NotNull ItemStack cursor) {
InventoryClickEvent inventoryClickEvent = new InventoryClickEvent(player, inventory, slot, clickType, clicked, cursor);
InventoryClickEvent inventoryClickEvent = new InventoryClickEvent(inventory, player, slot, clickType, clicked, cursor);
player.callEvent(InventoryClickEvent.class, inventoryClickEvent);
}

View File

@ -516,7 +516,7 @@ public class InventoryClickProcessor {
// PreClickEvent
{
InventoryPreClickEvent inventoryPreClickEvent = new InventoryPreClickEvent(player, inventory, slot, clickType, clicked, cursor);
InventoryPreClickEvent inventoryPreClickEvent = new InventoryPreClickEvent(inventory, player, slot, clickType, clicked, cursor);
player.callEvent(InventoryPreClickEvent.class, inventoryPreClickEvent);
cursor = inventoryPreClickEvent.getCursorItem();
clicked = inventoryPreClickEvent.getClickedItem();
@ -567,7 +567,7 @@ public class InventoryClickProcessor {
private void callClickEvent(@NotNull Player player, @Nullable Inventory inventory, int slot,
@NotNull ClickType clickType, ItemStack clicked, ItemStack cursor) {
InventoryClickEvent inventoryClickEvent = new InventoryClickEvent(player, inventory, slot, clickType, clicked, cursor);
InventoryClickEvent inventoryClickEvent = new InventoryClickEvent(inventory, player, slot, clickType, clicked, cursor);
player.callEvent(InventoryClickEvent.class, inventoryClickEvent);
}

View File

@ -70,7 +70,7 @@ public class ChatMessageListener {
}
private static RichMessage buildDefaultChatMessage(PlayerChatEvent chatEvent) {
final String username = chatEvent.getSender().getUsername();
final String username = chatEvent.getPlayer().getUsername();
final ColoredText usernameText = ColoredText.of(String.format("<%s>", username));

View File

@ -100,7 +100,7 @@ public class WindowListener {
public static void closeWindowListener(ClientCloseWindow packet, Player player) {
// if windowId == 0 then it is player's inventory, meaning that they hadn't been any open inventory packet
InventoryCloseEvent inventoryCloseEvent = new InventoryCloseEvent(player, player.getOpenInventory());
InventoryCloseEvent inventoryCloseEvent = new InventoryCloseEvent(player.getOpenInventory(), player);
player.callEvent(InventoryCloseEvent.class, inventoryCloseEvent);
player.closeInventory();

View File

@ -21,15 +21,16 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.packet.server.play.EffectPacket;
import net.minestom.server.network.packet.server.play.PlayerListHeaderAndFooterPacket;
import net.minestom.server.ping.ResponseDataConsumer;
import net.minestom.server.utils.PacketUtils;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector;
import net.minestom.server.utils.inventory.PlayerInventoryUtils;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.world.DimensionType;
@ -113,7 +114,7 @@ public class PlayerInit {
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {
final Entity source = event.getSource();
final Entity source = event.getEntity();
final Entity entity = event.getTarget();
if (entity instanceof EntityCreature) {
EntityCreature creature = (EntityCreature) entity;
@ -202,17 +203,27 @@ public class PlayerInit {
player.getInventory().addInventoryCondition((p, slot, clickType, inventoryConditionResult) -> {
if (slot == -999)
return;
ItemStack itemStack = p.getInventory().getItemStack(slot);
System.out.println("test " + itemStack.getIdentifier() + " " + itemStack.getData());
//ItemStack itemStack = p.getInventory().getItemStack(slot);
//System.out.println("test " + itemStack.getIdentifier() + " " + itemStack.getData());
});
});
globalEventHandler.addEventCallback(PlayerSpawnEvent.class, event -> {
final Player player = event.getPlayer();
player.setGameMode(GameMode.CREATIVE);
player.setGameMode(GameMode.SURVIVAL);
PlayerInventory inventory = player.getInventory();
ItemStack itemStack = new ItemStack(Material.STONE, (byte) 64);
player.getInventory().addItemStack(itemStack);
inventory.addItemStack(itemStack);
{
ItemStack item = new ItemStack(Material.DIAMOND_CHESTPLATE, (byte) 1);
inventory.setChestplate(item);
item.setDisplayName(ColoredText.of("test"));
inventory.refreshSlot((short) PlayerInventoryUtils.CHESTPLATE_SLOT);
}
//player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 32));
});