From 5224eecdd19ef8950060ac9c6081f2900433ac92 Mon Sep 17 00:00:00 2001 From: themode Date: Wed, 16 Dec 2020 03:21:59 +0100 Subject: [PATCH] Event unification (type-specific event, CancellableEvent is now an interface) --- .../net/minestom/server/entity/Player.java | 2 +- .../server/event/CancellableEvent.java | 16 +++------- .../minestom/server/event/EntityEvent.java | 23 +++++++++++++ .../minestom/server/event/InstanceEvent.java | 23 +++++++++++++ .../minestom/server/event/InventoryEvent.java | 23 +++++++++++++ .../minestom/server/event/PlayerEvent.java | 23 +++++++++++++ .../event/entity/EntityAttackEvent.java | 15 ++------- .../event/entity/EntityDamageEvent.java | 26 +++++++++------ .../server/event/entity/EntityDeathEvent.java | 17 ++-------- .../server/event/entity/EntityFireEvent.java | 30 ++++++++--------- .../event/entity/EntityItemMergeEvent.java | 24 ++++++++++---- .../server/event/entity/EntitySpawnEvent.java | 8 ++--- .../server/event/entity/EntityTickEvent.java | 12 ++----- .../event/entity/EntityVelocityEvent.java | 19 +++++++++-- .../server/event/handler/EventHandler.java | 4 ++- .../instance/AddEntityToInstanceEvent.java | 24 +++++++------- .../instance/InstanceChunkLoadEvent.java | 17 ++-------- .../instance/InstanceChunkUnloadEvent.java | 17 ++-------- .../event/instance/InstanceTickEvent.java | 17 ++-------- .../RemoveEntityFromInstanceEvent.java | 24 +++++++------- .../event/inventory/InventoryClickEvent.java | 19 +++-------- .../event/inventory/InventoryCloseEvent.java | 19 +++-------- .../event/inventory/InventoryOpenEvent.java | 21 +++++++++--- .../inventory/InventoryPreClickEvent.java | 31 ++++++++++-------- .../server/event/item/ItemDropEvent.java | 15 ++++++++- .../event/item/PickupExperienceEvent.java | 15 ++++++++- .../server/event/item/PickupItemEvent.java | 15 ++++++++- .../event/player/AdvancementTabEvent.java | 17 ++-------- .../event/player/PlayerAddItemStackEvent.java | 32 ++++++++++--------- .../event/player/PlayerBlockBreakEvent.java | 29 +++++++++-------- .../player/PlayerBlockInteractEvent.java | 28 ++++++++-------- .../event/player/PlayerBlockPlaceEvent.java | 28 ++++++++-------- .../player/PlayerChangeHeldSlotEvent.java | 30 +++++++++-------- .../server/event/player/PlayerChatEvent.java | 30 +++++++++-------- .../event/player/PlayerChunkLoadEvent.java | 17 ++-------- .../event/player/PlayerChunkUnloadEvent.java | 17 ++-------- .../event/player/PlayerCommandEvent.java | 30 +++++++++-------- .../event/player/PlayerDisconnectEvent.java | 18 ++--------- .../server/event/player/PlayerEatEvent.java | 17 ++-------- .../player/PlayerEntityInteractEvent.java | 17 ++-------- .../player/PlayerHandAnimationEvent.java | 30 +++++++++-------- .../player/PlayerItemAnimationEvent.java | 29 +++++++++-------- .../server/event/player/PlayerLoginEvent.java | 17 ++-------- .../server/event/player/PlayerMoveEvent.java | 30 +++++++++-------- .../player/PlayerPluginMessageEvent.java | 17 ++-------- .../event/player/PlayerPreEatEvent.java | 28 ++++++++-------- .../event/player/PlayerPreLoginEvent.java | 17 ++-------- .../player/PlayerResourcePackStatusEvent.java | 17 ++-------- .../event/player/PlayerRespawnEvent.java | 17 ++-------- .../event/player/PlayerSetItemStackEvent.java | 27 ++++++++-------- .../player/PlayerSettingsChangeEvent.java | 9 +++--- .../event/player/PlayerSkinInitEvent.java | 17 ++-------- .../server/event/player/PlayerSpawnEvent.java | 21 ++++++------ .../event/player/PlayerStartDiggingEvent.java | 28 ++++++++-------- .../event/player/PlayerStartFlyingEvent.java | 19 ++--------- .../event/player/PlayerStopFlyingEvent.java | 18 ++--------- .../event/player/PlayerSwapItemEvent.java | 28 ++++++++-------- .../server/event/player/PlayerTickEvent.java | 18 ++--------- .../event/player/PlayerUseItemEvent.java | 28 ++++++++-------- .../player/PlayerUseItemOnBlockEvent.java | 17 ++-------- .../inventory/InventoryClickHandler.java | 2 +- .../click/InventoryClickProcessor.java | 4 +-- .../server/listener/ChatMessageListener.java | 2 +- .../server/listener/WindowListener.java | 2 +- src/test/java/demo/PlayerInit.java | 23 +++++++++---- 65 files changed, 614 insertions(+), 660 deletions(-) create mode 100644 src/main/java/net/minestom/server/event/EntityEvent.java create mode 100644 src/main/java/net/minestom/server/event/InstanceEvent.java create mode 100644 src/main/java/net/minestom/server/event/InventoryEvent.java create mode 100644 src/main/java/net/minestom/server/event/PlayerEvent.java diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 1f47344cc..a7a5473ac 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -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, () -> { diff --git a/src/main/java/net/minestom/server/event/CancellableEvent.java b/src/main/java/net/minestom/server/event/CancellableEvent.java index c0d0e20f5..e04d9ea5b 100644 --- a/src/main/java/net/minestom/server/event/CancellableEvent.java +++ b/src/main/java/net/minestom/server/event/CancellableEvent.java @@ -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); } diff --git a/src/main/java/net/minestom/server/event/EntityEvent.java b/src/main/java/net/minestom/server/event/EntityEvent.java new file mode 100644 index 000000000..49485c1e2 --- /dev/null +++ b/src/main/java/net/minestom/server/event/EntityEvent.java @@ -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; + } +} diff --git a/src/main/java/net/minestom/server/event/InstanceEvent.java b/src/main/java/net/minestom/server/event/InstanceEvent.java new file mode 100644 index 000000000..fc7ba05da --- /dev/null +++ b/src/main/java/net/minestom/server/event/InstanceEvent.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/net/minestom/server/event/InventoryEvent.java b/src/main/java/net/minestom/server/event/InventoryEvent.java new file mode 100644 index 000000000..ada529220 --- /dev/null +++ b/src/main/java/net/minestom/server/event/InventoryEvent.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/net/minestom/server/event/PlayerEvent.java b/src/main/java/net/minestom/server/event/PlayerEvent.java new file mode 100644 index 000000000..21185ebbe --- /dev/null +++ b/src/main/java/net/minestom/server/event/PlayerEvent.java @@ -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; + } +} diff --git a/src/main/java/net/minestom/server/event/entity/EntityAttackEvent.java b/src/main/java/net/minestom/server/event/entity/EntityAttackEvent.java index f23f527f1..13b2a8ebc 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityAttackEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityAttackEvent.java @@ -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 */ diff --git a/src/main/java/net/minestom/server/event/entity/EntityDamageEvent.java b/src/main/java/net/minestom/server/event/entity/EntityDamageEvent.java index 256632fd4..69e10ffc4 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityDamageEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityDamageEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/entity/EntityDeathEvent.java b/src/main/java/net/minestom/server/event/entity/EntityDeathEvent.java index cb18c8981..7bb3c7b7a 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityDeathEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityDeathEvent.java @@ -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); } } diff --git a/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java b/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java index 17b760710..8a1355af3 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityFireEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/entity/EntityItemMergeEvent.java b/src/main/java/net/minestom/server/event/entity/EntityItemMergeEvent.java index cb234c3cc..09a155e84 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityItemMergeEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityItemMergeEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/entity/EntitySpawnEvent.java b/src/main/java/net/minestom/server/event/entity/EntitySpawnEvent.java index fc2c69836..a53b66ee4 100644 --- a/src/main/java/net/minestom/server/event/entity/EntitySpawnEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntitySpawnEvent.java @@ -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; } diff --git a/src/main/java/net/minestom/server/event/entity/EntityTickEvent.java b/src/main/java/net/minestom/server/event/entity/EntityTickEvent.java index 5abc288fe..0e81c3313 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityTickEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityTickEvent.java @@ -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; - } } diff --git a/src/main/java/net/minestom/server/event/entity/EntityVelocityEvent.java b/src/main/java/net/minestom/server/event/entity/EntityVelocityEvent.java index fa09f1176..ae0024f99 100644 --- a/src/main/java/net/minestom/server/event/entity/EntityVelocityEvent.java +++ b/src/main/java/net/minestom/server/event/entity/EntityVelocityEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/handler/EventHandler.java b/src/main/java/net/minestom/server/event/handler/EventHandler.java index 25fbe91eb..eaec31f87 100644 --- a/src/main/java/net/minestom/server/event/handler/EventHandler.java +++ b/src/main/java/net/minestom/server/event/handler/EventHandler.java @@ -122,7 +122,9 @@ public interface EventHandler { * @param the event type * @see #callEvent(Class, Event) */ - default void callCancellableEvent(@NotNull Class eventClass, @NotNull E event, @NotNull Runnable successCallback) { + default void callCancellableEvent(@NotNull Class eventClass, + @NotNull E event, + @NotNull Runnable successCallback) { callEvent(eventClass, event); if (!event.isCancelled()) { successCallback.run(); diff --git a/src/main/java/net/minestom/server/event/instance/AddEntityToInstanceEvent.java b/src/main/java/net/minestom/server/event/instance/AddEntityToInstanceEvent.java index 21da230b0..861d6f891 100644 --- a/src/main/java/net/minestom/server/event/instance/AddEntityToInstanceEvent.java +++ b/src/main/java/net/minestom/server/event/instance/AddEntityToInstanceEvent.java @@ -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; } } diff --git a/src/main/java/net/minestom/server/event/instance/InstanceChunkLoadEvent.java b/src/main/java/net/minestom/server/event/instance/InstanceChunkLoadEvent.java index 3fe385778..c102d75b1 100644 --- a/src/main/java/net/minestom/server/event/instance/InstanceChunkLoadEvent.java +++ b/src/main/java/net/minestom/server/event/instance/InstanceChunkLoadEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/instance/InstanceChunkUnloadEvent.java b/src/main/java/net/minestom/server/event/instance/InstanceChunkUnloadEvent.java index f354d67ee..306aaf107 100644 --- a/src/main/java/net/minestom/server/event/instance/InstanceChunkUnloadEvent.java +++ b/src/main/java/net/minestom/server/event/instance/InstanceChunkUnloadEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/instance/InstanceTickEvent.java b/src/main/java/net/minestom/server/event/instance/InstanceTickEvent.java index 26b67b619..eef4c67a6 100644 --- a/src/main/java/net/minestom/server/event/instance/InstanceTickEvent.java +++ b/src/main/java/net/minestom/server/event/instance/InstanceTickEvent.java @@ -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; - } } \ No newline at end of file diff --git a/src/main/java/net/minestom/server/event/instance/RemoveEntityFromInstanceEvent.java b/src/main/java/net/minestom/server/event/instance/RemoveEntityFromInstanceEvent.java index 55f83bc4f..fe180ffea 100644 --- a/src/main/java/net/minestom/server/event/instance/RemoveEntityFromInstanceEvent.java +++ b/src/main/java/net/minestom/server/event/instance/RemoveEntityFromInstanceEvent.java @@ -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; } } diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java index f3c3ce4a6..b7e9a89bb 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryCloseEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryCloseEvent.java index 81b94df32..881d64e07 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryCloseEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryCloseEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java index 574aa4a1f..6b02eb717 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java @@ -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; *

* 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; + } } diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java index 54f42bc89..ca4733b38 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/item/ItemDropEvent.java b/src/main/java/net/minestom/server/event/item/ItemDropEvent.java index d6bcee6e6..4ec4591ab 100644 --- a/src/main/java/net/minestom/server/event/item/ItemDropEvent.java +++ b/src/main/java/net/minestom/server/event/item/ItemDropEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/item/PickupExperienceEvent.java b/src/main/java/net/minestom/server/event/item/PickupExperienceEvent.java index 9bb212a02..a1fa19188 100644 --- a/src/main/java/net/minestom/server/event/item/PickupExperienceEvent.java +++ b/src/main/java/net/minestom/server/event/item/PickupExperienceEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/item/PickupItemEvent.java b/src/main/java/net/minestom/server/event/item/PickupItemEvent.java index 48e44bc16..b4ce28234 100644 --- a/src/main/java/net/minestom/server/event/item/PickupItemEvent.java +++ b/src/main/java/net/minestom/server/event/item/PickupItemEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/AdvancementTabEvent.java b/src/main/java/net/minestom/server/event/player/AdvancementTabEvent.java index ea725ac49..6270b9887 100644 --- a/src/main/java/net/minestom/server/event/player/AdvancementTabEvent.java +++ b/src/main/java/net/minestom/server/event/player/AdvancementTabEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/player/PlayerAddItemStackEvent.java b/src/main/java/net/minestom/server/event/player/PlayerAddItemStackEvent.java index 48d181eef..3adbb1e6f 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerAddItemStackEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerAddItemStackEvent.java @@ -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; } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerBlockBreakEvent.java b/src/main/java/net/minestom/server/event/player/PlayerBlockBreakEvent.java index f4918f2d2..c9dee8793 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerBlockBreakEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerBlockBreakEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerBlockInteractEvent.java b/src/main/java/net/minestom/server/event/player/PlayerBlockInteractEvent.java index 233a03901..da1075a34 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerBlockInteractEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerBlockInteractEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerBlockPlaceEvent.java b/src/main/java/net/minestom/server/event/player/PlayerBlockPlaceEvent.java index 5db541ece..49594df1c 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerBlockPlaceEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerBlockPlaceEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerChangeHeldSlotEvent.java b/src/main/java/net/minestom/server/event/player/PlayerChangeHeldSlotEvent.java index 4ef36cfec..d3c0a0c48 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerChangeHeldSlotEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerChangeHeldSlotEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerChatEvent.java b/src/main/java/net/minestom/server/event/player/PlayerChatEvent.java index 4f87a08cc..66af6d837 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerChatEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerChatEvent.java @@ -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 recipients; private String message; private Function chatFormat; - public PlayerChatEvent(@NotNull Player sender, @NotNull Collection recipients, @NotNull String message) { - this.sender = sender; + private boolean cancelled; + + public PlayerChatEvent(@NotNull Player player, @NotNull Collection 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. *

@@ -88,4 +80,14 @@ public class PlayerChatEvent extends CancellableEvent { public Function getChatFormatFunction() { return chatFormat; } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerChunkLoadEvent.java b/src/main/java/net/minestom/server/event/player/PlayerChunkLoadEvent.java index bb66cfb14..b312ae85d 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerChunkLoadEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerChunkLoadEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/player/PlayerChunkUnloadEvent.java b/src/main/java/net/minestom/server/event/player/PlayerChunkUnloadEvent.java index 137fb7f99..e8ab23be5 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerChunkUnloadEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerChunkUnloadEvent.java @@ -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; *

* 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. * diff --git a/src/main/java/net/minestom/server/event/player/PlayerCommandEvent.java b/src/main/java/net/minestom/server/event/player/PlayerCommandEvent.java index 0309c67ea..6e977c067 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerCommandEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerCommandEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerDisconnectEvent.java b/src/main/java/net/minestom/server/event/player/PlayerDisconnectEvent.java index 5f8778c04..93335cbb3 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerDisconnectEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerDisconnectEvent.java @@ -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); } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerEatEvent.java b/src/main/java/net/minestom/server/event/player/PlayerEatEvent.java index 516017f53..527b4c56f 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerEatEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerEatEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/player/PlayerEntityInteractEvent.java b/src/main/java/net/minestom/server/event/player/PlayerEntityInteractEvent.java index 385e7c6a0..d5c8da5e9 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerEntityInteractEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerEntityInteractEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/player/PlayerHandAnimationEvent.java b/src/main/java/net/minestom/server/event/player/PlayerHandAnimationEvent.java index 67ee657af..7d9ba9d21 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerHandAnimationEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerHandAnimationEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerItemAnimationEvent.java b/src/main/java/net/minestom/server/event/player/PlayerItemAnimationEvent.java index cf4f9792b..335108736 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerItemAnimationEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerItemAnimationEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerLoginEvent.java b/src/main/java/net/minestom/server/event/player/PlayerLoginEvent.java index 57bc94523..817e8a94c 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerLoginEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerLoginEvent.java @@ -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; *

* 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); } /** diff --git a/src/main/java/net/minestom/server/event/player/PlayerMoveEvent.java b/src/main/java/net/minestom/server/event/player/PlayerMoveEvent.java index 1ec41a584..2190a7dbe 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerMoveEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerMoveEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerPluginMessageEvent.java b/src/main/java/net/minestom/server/event/player/PlayerPluginMessageEvent.java index 3f7a74e68..6f14dd180 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerPluginMessageEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerPluginMessageEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/player/PlayerPreEatEvent.java b/src/main/java/net/minestom/server/event/player/PlayerPreEatEvent.java index a2c06c4bb..a4f61f447 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerPreEatEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerPreEatEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java b/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java index 1db457ab3..dfcb4d03f 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/player/PlayerResourcePackStatusEvent.java b/src/main/java/net/minestom/server/event/player/PlayerResourcePackStatusEvent.java index 2ad6e5068..1d367e846 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerResourcePackStatusEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerResourcePackStatusEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/player/PlayerRespawnEvent.java b/src/main/java/net/minestom/server/event/player/PlayerRespawnEvent.java index 00039316c..598a9d729 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerRespawnEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerRespawnEvent.java @@ -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. *

diff --git a/src/main/java/net/minestom/server/event/player/PlayerSetItemStackEvent.java b/src/main/java/net/minestom/server/event/player/PlayerSetItemStackEvent.java index fd1cbe7d8..f0e9e0574 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerSetItemStackEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerSetItemStackEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerSettingsChangeEvent.java b/src/main/java/net/minestom/server/event/player/PlayerSettingsChangeEvent.java index 357c4cb94..d876c4d4d 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerSettingsChangeEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerSettingsChangeEvent.java @@ -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; } diff --git a/src/main/java/net/minestom/server/event/player/PlayerSkinInitEvent.java b/src/main/java/net/minestom/server/event/player/PlayerSkinInitEvent.java index 7cdf377f8..d8cb40644 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerSkinInitEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerSkinInitEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/event/player/PlayerSpawnEvent.java b/src/main/java/net/minestom/server/event/player/PlayerSpawnEvent.java index 4d27ddf4f..6c4f393a2 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerSpawnEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerSpawnEvent.java @@ -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. - *

- * 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; } /** diff --git a/src/main/java/net/minestom/server/event/player/PlayerStartDiggingEvent.java b/src/main/java/net/minestom/server/event/player/PlayerStartDiggingEvent.java index 60e107820..a8b00c515 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerStartDiggingEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerStartDiggingEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerStartFlyingEvent.java b/src/main/java/net/minestom/server/event/player/PlayerStartFlyingEvent.java index 074de112f..aefdf9ee0 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerStartFlyingEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerStartFlyingEvent.java @@ -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; - } - } diff --git a/src/main/java/net/minestom/server/event/player/PlayerStopFlyingEvent.java b/src/main/java/net/minestom/server/event/player/PlayerStopFlyingEvent.java index 84d2b3f9d..027de80c9 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerStopFlyingEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerStopFlyingEvent.java @@ -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); } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerSwapItemEvent.java b/src/main/java/net/minestom/server/event/player/PlayerSwapItemEvent.java index 3785874ec..2aff53cb0 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerSwapItemEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerSwapItemEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerTickEvent.java b/src/main/java/net/minestom/server/event/player/PlayerTickEvent.java index 88c5f5d63..1872fa2da 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerTickEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerTickEvent.java @@ -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); } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerUseItemEvent.java b/src/main/java/net/minestom/server/event/player/PlayerUseItemEvent.java index 07389079a..d32567e45 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerUseItemEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerUseItemEvent.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/event/player/PlayerUseItemOnBlockEvent.java b/src/main/java/net/minestom/server/event/player/PlayerUseItemOnBlockEvent.java index 6bec50bf5..88dbcfb32 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerUseItemOnBlockEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerUseItemOnBlockEvent.java @@ -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. * diff --git a/src/main/java/net/minestom/server/inventory/InventoryClickHandler.java b/src/main/java/net/minestom/server/inventory/InventoryClickHandler.java index 0cbef79e0..03c588a7a 100644 --- a/src/main/java/net/minestom/server/inventory/InventoryClickHandler.java +++ b/src/main/java/net/minestom/server/inventory/InventoryClickHandler.java @@ -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); } diff --git a/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java b/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java index b140a1e83..6c4caa498 100644 --- a/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java +++ b/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java @@ -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); } diff --git a/src/main/java/net/minestom/server/listener/ChatMessageListener.java b/src/main/java/net/minestom/server/listener/ChatMessageListener.java index 395deda3f..da341207d 100644 --- a/src/main/java/net/minestom/server/listener/ChatMessageListener.java +++ b/src/main/java/net/minestom/server/listener/ChatMessageListener.java @@ -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)); diff --git a/src/main/java/net/minestom/server/listener/WindowListener.java b/src/main/java/net/minestom/server/listener/WindowListener.java index 39cdd441d..c007c6a06 100644 --- a/src/main/java/net/minestom/server/listener/WindowListener.java +++ b/src/main/java/net/minestom/server/listener/WindowListener.java @@ -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(); diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index dfd66805b..73d870828 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -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)); });