Merge pull request #1 from Minestom/master

Merge remote-tracking branch 'origin/master'
This commit is contained in:
iamceph 2020-12-17 15:32:46 +01:00 committed by GitHub
commit 636b527cc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
90 changed files with 923 additions and 806 deletions

View File

@ -32,7 +32,7 @@ public final class ChatColor {
public static final ChatColor BLACK = fromRGB((byte) 0, (byte) 0, (byte) 0, 0, "black"); public static final ChatColor BLACK = fromRGB((byte) 0, (byte) 0, (byte) 0, 0, "black");
public static final ChatColor DARK_BLUE = fromRGB((byte) 0, (byte) 0, (byte) 170, 1, "dark_blue"); public static final ChatColor DARK_BLUE = fromRGB((byte) 0, (byte) 0, (byte) 170, 1, "dark_blue");
public static final ChatColor DARK_GREEN = fromRGB((byte) 0, (byte) 170, (byte) 0, 2, "dark_green"); public static final ChatColor DARK_GREEN = fromRGB((byte) 0, (byte) 170, (byte) 0, 2, "dark_green");
public static final ChatColor DARK_CYAN = fromRGB((byte) 0, (byte) 170, (byte) 170, 3, "dark_cyan"); public static final ChatColor DARK_CYAN = fromRGB((byte) 0, (byte) 170, (byte) 170, 3, "dark_aqua");
public static final ChatColor DARK_RED = fromRGB((byte) 170, (byte) 0, (byte) 0, 4, "dark_red"); public static final ChatColor DARK_RED = fromRGB((byte) 170, (byte) 0, (byte) 0, 4, "dark_red");
public static final ChatColor PURPLE = fromRGB((byte) 170, (byte) 0, (byte) 170, 5, "dark_purple"); public static final ChatColor PURPLE = fromRGB((byte) 170, (byte) 0, (byte) 170, 5, "dark_purple");
public static final ChatColor GOLD = fromRGB((byte) 255, (byte) 170, (byte) 0, 6, "gold"); public static final ChatColor GOLD = fromRGB((byte) 255, (byte) 170, (byte) 0, 6, "gold");

View File

@ -202,27 +202,29 @@ public final class CommandManager {
} }
// Process the command // Process the command
try {
{
// Check for rich-command // Check for rich-command
this.dispatcher.execute(sender, command); final boolean result = this.dispatcher.execute(sender, command);
return true; if (result) {
} catch (NullPointerException e) { return true;
// Check for legacy-command } else {
final String[] splitCommand = command.split(" "); // Check for legacy-command
final String commandName = splitCommand[0]; final String[] splitCommand = command.split(" ");
final CommandProcessor commandProcessor = commandProcessorMap.get(commandName.toLowerCase()); final String commandName = splitCommand[0];
if (commandProcessor == null) { final CommandProcessor commandProcessor = commandProcessorMap.get(commandName.toLowerCase());
if (unknownCommandCallback != null) { if (commandProcessor == null) {
this.unknownCommandCallback.apply(sender, command); if (unknownCommandCallback != null) {
this.unknownCommandCallback.apply(sender, command);
}
return false;
} }
return false;
// Execute the legacy-command
final String[] args = command.substring(command.indexOf(" ") + 1).split(" ");
return commandProcessor.process(sender, commandName, args);
} }
// Execute the legacy-command
final String[] args = command.substring(command.indexOf(" ") + 1).split(" ");
return commandProcessor.process(sender, commandName, args);
} }
} }

View File

@ -148,7 +148,7 @@ public final class Arguments {
public Object getObject(@NotNull String id) { public Object getObject(@NotNull String id) {
return args.computeIfAbsent(id, s -> { return args.computeIfAbsent(id, s -> {
throw new NullPointerException( throw new NullPointerException(
"The argument with the id " + id + " has no value assigned, be sure to check your arguments id, your syntax, and that you do not change the argument id dynamically."); "The argument with the id '" + id + "' has no value assigned, be sure to check your arguments id, your syntax, and that you do not change the argument id dynamically.");
}); });
} }

View File

@ -75,9 +75,19 @@ public class CommandDispatcher {
return findCommandResult(command, args); return findCommandResult(command, args);
} }
public void execute(@NotNull CommandSender source, @NotNull String commandString) { /**
* Check if the command exists, and execute it.
*
* @param source the command source
* @param commandString the command with the argument(s)
* @return true if the command executed successfully, false if the command doesn't exist
*/
public boolean execute(@NotNull CommandSender source, @NotNull String commandString) {
CommandResult result = parse(commandString); CommandResult result = parse(commandString);
result.execute(source, commandString); if (result != null) {
result.execute(source, commandString);
}
return result != null;
} }
@NotNull @NotNull

View File

@ -515,10 +515,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
property.instance = instances[i]; property.instance = instances[i];
property.attribute = instances[i].getAttribute(); property.attribute = instances[i].getAttribute();
if (getEntityType() == EntityType.PLAYER && instances[i].getAttribute().equals(Attributes.MOVEMENT_SPEED)) property.value = value;
property.value = ((Player) this).getWalkingSpeed();
else
property.value = value;
properties[i] = property; properties[i] = property;
} }

View File

@ -3,7 +3,6 @@ package net.minestom.server.entity;
import com.google.common.collect.Queues; import com.google.common.collect.Queues;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.advancements.AdvancementTab; import net.minestom.server.advancements.AdvancementTab;
import net.minestom.server.attribute.Attribute;
import net.minestom.server.attribute.AttributeInstance; import net.minestom.server.attribute.AttributeInstance;
import net.minestom.server.attribute.Attributes; import net.minestom.server.attribute.Attributes;
import net.minestom.server.bossbar.BossBar; import net.minestom.server.bossbar.BossBar;
@ -23,7 +22,6 @@ import net.minestom.server.event.item.ItemDropEvent;
import net.minestom.server.event.item.ItemUpdateStateEvent; import net.minestom.server.event.item.ItemUpdateStateEvent;
import net.minestom.server.event.item.PickupExperienceEvent; import net.minestom.server.event.item.PickupExperienceEvent;
import net.minestom.server.event.player.*; import net.minestom.server.event.player.*;
import net.minestom.server.gamedata.tags.TagManager;
import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.CustomBlock; import net.minestom.server.instance.block.CustomBlock;
@ -184,7 +182,7 @@ public class Player extends LivingEntity implements CommandSender {
private boolean allowFlying; private boolean allowFlying;
private boolean instantBreak; private boolean instantBreak;
private float flyingSpeed = 0.05f; private float flyingSpeed = 0.05f;
private float walkingSpeed = 0.1f; private float fieldViewModifier = 0.1f;
// Statistics // Statistics
private final Map<PlayerStatistic, Integer> statisticValueMap = new Hashtable<>(); private final Map<PlayerStatistic, Integer> statisticValueMap = new Hashtable<>();
@ -195,7 +193,7 @@ public class Player extends LivingEntity implements CommandSender {
// Tick related // Tick related
private final PlayerTickEvent playerTickEvent = new PlayerTickEvent(this); private final PlayerTickEvent playerTickEvent = new PlayerTickEvent(this);
public Player(UUID uuid, String username, PlayerConnection playerConnection) { public Player(@NotNull UUID uuid, @NotNull String username, @NotNull PlayerConnection playerConnection) {
super(EntityType.PLAYER); super(EntityType.PLAYER);
this.uuid = uuid; // Override Entity#uuid defined in the constructor this.uuid = uuid; // Override Entity#uuid defined in the constructor
this.username = username; this.username = username;
@ -216,7 +214,7 @@ public class Player extends LivingEntity implements CommandSender {
this.gameMode = GameMode.SURVIVAL; this.gameMode = GameMode.SURVIVAL;
this.dimensionType = DimensionType.OVERWORLD; this.dimensionType = DimensionType.OVERWORLD;
this.levelFlat = true; this.levelFlat = true;
refreshPosition(0, 0, 0); getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.1f);
// FakePlayer init its connection there // FakePlayer init its connection there
playerConnectionInit(); playerConnectionInit();
@ -299,18 +297,19 @@ public class Player extends LivingEntity implements CommandSender {
} }
// Recipes end // Recipes end
// Send server tags // Tags start
TagsPacket tags = new TagsPacket(); {
TagManager tagManager = MinecraftServer.getTagManager(); TagsPacket tags = TagsPacket.getRequiredTagsPacket();
tagManager.addRequiredTagsToPacket(tags);
UpdateTagListEvent event = new UpdateTagListEvent(tags); UpdateTagListEvent event = new UpdateTagListEvent(tags);
callEvent(UpdateTagListEvent.class, event); callEvent(UpdateTagListEvent.class, event);
getPlayerConnection().sendPacket(tags); this.playerConnection.sendPacket(tags);
}
// Tags end
// Some client update // Some client update
playerConnection.sendPacket(getPropertiesPacket()); // Send default properties this.playerConnection.sendPacket(getPropertiesPacket()); // Send default properties
refreshHealth(); // Heal and send health packet refreshHealth(); // Heal and send health packet
refreshAbilities(); // Send abilities packet refreshAbilities(); // Send abilities packet
getInventory().update(); getInventory().update();
@ -323,14 +322,6 @@ public class Player extends LivingEntity implements CommandSender {
this.playerConnection.setPlayer(this); this.playerConnection.setPlayer(this);
} }
@Override
public float getAttributeValue(@NotNull Attribute attribute) {
if (attribute == Attributes.MOVEMENT_SPEED) {
return walkingSpeed;
}
return super.getAttributeValue(attribute);
}
@Override @Override
public void update(long time) { public void update(long time) {
// Network tick // Network tick
@ -1861,7 +1852,7 @@ public class Player extends LivingEntity implements CommandSender {
public boolean openInventory(@NotNull Inventory inventory) { public boolean openInventory(@NotNull Inventory inventory) {
Check.notNull(inventory, "Inventory cannot be null, use Player#closeInventory() to close current"); 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, () -> { callCancellableEvent(InventoryOpenEvent.class, inventoryOpenEvent, () -> {
@ -2139,12 +2130,12 @@ public class Player extends LivingEntity implements CommandSender {
refreshAbilities(); refreshAbilities();
} }
public float getWalkingSpeed() { public float getFieldViewModifier() {
return walkingSpeed; return fieldViewModifier;
} }
public void setWalkingSpeed(float walkingSpeed) { public void setFieldViewModifier(float fieldViewModifier) {
this.walkingSpeed = walkingSpeed; this.fieldViewModifier = fieldViewModifier;
refreshAbilities(); refreshAbilities();
} }
@ -2170,8 +2161,7 @@ public class Player extends LivingEntity implements CommandSender {
} }
/** /**
* Sends to the player a {@link PlayerAbilitiesPacket} with all the updated fields * Sends to the player a {@link PlayerAbilitiesPacket} with all the updated fields.
* (walkingSpeed set to 0.1).
*/ */
protected void refreshAbilities() { protected void refreshAbilities() {
PlayerAbilitiesPacket playerAbilitiesPacket = new PlayerAbilitiesPacket(); PlayerAbilitiesPacket playerAbilitiesPacket = new PlayerAbilitiesPacket();
@ -2180,7 +2170,7 @@ public class Player extends LivingEntity implements CommandSender {
playerAbilitiesPacket.allowFlying = allowFlying; playerAbilitiesPacket.allowFlying = allowFlying;
playerAbilitiesPacket.instantBreak = instantBreak; playerAbilitiesPacket.instantBreak = instantBreak;
playerAbilitiesPacket.flyingSpeed = flyingSpeed; playerAbilitiesPacket.flyingSpeed = flyingSpeed;
playerAbilitiesPacket.walkingSpeed = 0.1f; playerAbilitiesPacket.fieldViewModifier = fieldViewModifier;
playerConnection.sendPacket(playerAbilitiesPacket); playerConnection.sendPacket(playerAbilitiesPacket);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,33 +1,22 @@
package net.minestom.server.event.instance; 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 net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Called when a chunk in an instance is loaded. * 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; private final int chunkX, chunkZ;
public InstanceChunkLoadEvent(@NotNull Instance instance, int chunkX, int chunkZ) { public InstanceChunkLoadEvent(@NotNull Instance instance, int chunkX, int chunkZ) {
this.instance = instance; super(instance);
this.chunkX = chunkX; this.chunkX = chunkX;
this.chunkZ = chunkZ; 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. * Gets the chunk X.
* *

View File

@ -1,33 +1,22 @@
package net.minestom.server.event.instance; 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 net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Called when a chunk in an instance is unloaded. * 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; private final int chunkX, chunkZ;
public InstanceChunkUnloadEvent(@NotNull Instance instance, int chunkX, int chunkZ) { public InstanceChunkUnloadEvent(@NotNull Instance instance, int chunkX, int chunkZ) {
this.instance = instance; super(instance);
this.chunkX = chunkX; this.chunkX = chunkX;
this.chunkZ = chunkZ; 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. * Gets the chunk X.
* *

View File

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

View File

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

View File

@ -1,7 +1,7 @@
package net.minestom.server.event.inventory; package net.minestom.server.event.inventory;
import net.minestom.server.entity.Player; 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.Inventory;
import net.minestom.server.inventory.click.ClickType; import net.minestom.server.inventory.click.ClickType;
import net.minestom.server.item.ItemStack; 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 * Called after {@link InventoryPreClickEvent}, this event cannot be cancelled and items related to the click
* are already moved. * are already moved.
*/ */
public class InventoryClickEvent extends Event { public class InventoryClickEvent extends InventoryEvent {
private final Player player; private final Player player;
private final Inventory inventory;
private final int slot; private final int slot;
private final ClickType clickType; private final ClickType clickType;
private final ItemStack clickedItem; private final ItemStack clickedItem;
private final ItemStack cursorItem; private final ItemStack cursorItem;
public InventoryClickEvent(@NotNull Player player, Inventory inventory, public InventoryClickEvent(@Nullable Inventory inventory, @NotNull Player player,
int slot, @NotNull ClickType clickType, int slot, @NotNull ClickType clickType,
@NotNull ItemStack clicked, @NotNull ItemStack cursor) { @NotNull ItemStack clicked, @NotNull ItemStack cursor) {
super(inventory);
this.player = player; this.player = player;
this.inventory = inventory;
this.slot = slot; this.slot = slot;
this.clickType = clickType; this.clickType = clickType;
this.clickedItem = clicked; this.clickedItem = clicked;
@ -42,16 +41,6 @@ public class InventoryClickEvent extends Event {
return player; 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. * Gets the clicked slot number.
* *

View File

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

View File

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

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.inventory;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent; import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.InventoryEvent;
import net.minestom.server.inventory.Inventory; import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.click.ClickType; import net.minestom.server.inventory.click.ClickType;
import net.minestom.server.item.ItemStack; 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. * 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 Player player;
private final Inventory inventory;
private final int slot; private final int slot;
private final ClickType clickType; private final ClickType clickType;
private ItemStack clickedItem; private ItemStack clickedItem;
private ItemStack cursorItem; 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, int slot, @NotNull ClickType clickType,
@NotNull ItemStack clicked, @NotNull ItemStack cursor) { @NotNull ItemStack clicked, @NotNull ItemStack cursor) {
super(inventory);
this.player = player; this.player = player;
this.inventory = inventory;
this.slot = slot; this.slot = slot;
this.clickType = clickType; this.clickType = clickType;
this.clickedItem = clicked; this.clickedItem = clicked;
@ -41,16 +44,6 @@ public class InventoryPreClickEvent extends CancellableEvent {
return player; 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. * Gets the clicked slot number.
* *
@ -107,4 +100,14 @@ public class InventoryPreClickEvent extends CancellableEvent {
public void setCursorItem(@NotNull ItemStack cursorItem) { public void setCursorItem(@NotNull ItemStack cursorItem) {
this.cursorItem = cursorItem; this.cursorItem = cursorItem;
} }
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent; import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.validate.Check; import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull; 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). * 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; private byte slot;
public PlayerChangeHeldSlotEvent(@NotNull Player player, byte slot) { private boolean cancelled;
this.player = player;
this.slot = slot;
}
/** public PlayerChangeHeldSlotEvent(@NotNull Player player, byte slot) {
* Gets the player who changed his held slot. super(player);
* this.slot = slot;
* @return the player
*/
@NotNull
public Player getPlayer() {
return player;
} }
/** /**
@ -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"); Check.argCondition(!MathUtils.isBetween(slot, 0, 8), "The held slot needs to be between 0 and 8");
this.slot = slot; this.slot = slot;
} }
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,33 +1,22 @@
package net.minestom.server.event.player; package net.minestom.server.event.player;
import net.minestom.server.entity.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.item.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Called when a player is finished eating. * 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; private final ItemStack foodItem;
public PlayerEatEvent(@NotNull Player player, @NotNull ItemStack foodItem) { public PlayerEatEvent(@NotNull Player player, @NotNull ItemStack foodItem) {
this.player = player; super(player);
this.foodItem = foodItem; 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. * Gets the food item that has been eaten.
* *

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,33 +1,22 @@
package net.minestom.server.event.player; package net.minestom.server.event.player;
import net.minestom.server.entity.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 net.minestom.server.resourcepack.ResourcePackStatus;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Called when a player warns the server of a resource pack status. * 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; private final ResourcePackStatus status;
public PlayerResourcePackStatusEvent(@NotNull Player player, @NotNull ResourcePackStatus status) { public PlayerResourcePackStatusEvent(@NotNull Player player, @NotNull ResourcePackStatus status) {
this.player = player; super(player);
this.status = status; 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. * Gets the resource pack status.
* *

View File

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

View File

@ -2,6 +2,7 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent; import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull; 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)} * Called as a result of {@link net.minestom.server.inventory.PlayerInventory#setItemStack(int, ItemStack)}
* and player click in his inventory. * 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 int slot;
private ItemStack itemStack; private ItemStack itemStack;
private boolean cancelled;
public PlayerSetItemStackEvent(@NotNull Player player, int slot, @NotNull ItemStack itemStack) { public PlayerSetItemStackEvent(@NotNull Player player, int slot, @NotNull ItemStack itemStack) {
this.player = player; super(player);
this.slot = slot; this.slot = slot;
this.itemStack = itemStack; 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. * Gets the slot where the item will be set.
* *
@ -68,4 +60,13 @@ public class PlayerSetItemStackEvent extends CancellableEvent {
this.itemStack = itemStack; this.itemStack = itemStack;
} }
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
} }

View File

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

View File

@ -2,33 +2,22 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.entity.PlayerSkin; 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.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
/** /**
* Called at the player connection to initialize his skin. * 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; private PlayerSkin skin;
public PlayerSkinInitEvent(@NotNull Player player, @Nullable PlayerSkin currentSkin) { public PlayerSkinInitEvent(@NotNull Player player, @Nullable PlayerSkin currentSkin) {
this.player = player; super(player);
this.skin = currentSkin; 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. * Gets the spawning skin of the player.
* *

View File

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

View File

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

View File

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

View File

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

View File

@ -2,34 +2,26 @@ package net.minestom.server.event.player;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent; import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Called when a player is trying to swap his main and off hand item. * 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 mainHandItem;
private ItemStack offHandItem; private ItemStack offHandItem;
private boolean cancelled;
public PlayerSwapItemEvent(@NotNull Player player, @NotNull ItemStack mainHandItem, @NotNull ItemStack offHandItem) { public PlayerSwapItemEvent(@NotNull Player player, @NotNull ItemStack mainHandItem, @NotNull ItemStack offHandItem) {
this.player = player; super(player);
this.mainHandItem = mainHandItem; this.mainHandItem = mainHandItem;
this.offHandItem = offHandItem; 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. * 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) { public void setOffHandItem(@NotNull ItemStack offHandItem) {
this.offHandItem = offHandItem; this.offHandItem = offHandItem;
} }
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
} }

View File

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

View File

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

View File

@ -1,7 +1,7 @@
package net.minestom.server.event.player; package net.minestom.server.event.player;
import net.minestom.server.entity.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.item.ItemStack;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Direction; 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). * 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 Player.Hand hand;
private final ItemStack itemStack; private final ItemStack itemStack;
private final BlockPosition position; private final BlockPosition position;
@ -21,23 +20,13 @@ public class PlayerUseItemOnBlockEvent extends Event {
public PlayerUseItemOnBlockEvent(@NotNull Player player, @NotNull Player.Hand hand, public PlayerUseItemOnBlockEvent(@NotNull Player player, @NotNull Player.Hand hand,
@NotNull ItemStack itemStack, @NotNull ItemStack itemStack,
@NotNull BlockPosition position, @NotNull Direction blockFace) { @NotNull BlockPosition position, @NotNull Direction blockFace) {
this.player = player; super(player);
this.hand = hand; this.hand = hand;
this.itemStack = itemStack; this.itemStack = itemStack;
this.position = position; this.position = position;
this.blockFace = blockFace; 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. * Gets the position of the interacted block.
* *

View File

@ -930,7 +930,6 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
synchronized (entitiesLock) { synchronized (entitiesLock) {
Set<Entity> entities = getEntitiesInChunk(chunkIndex); Set<Entity> entities = getEntitiesInChunk(chunkIndex);
entities.add(entity); entities.add(entity);
this.chunkEntities.put(chunkIndex, entities);
this.entities.add(entity); this.entities.add(entity);
if (entity instanceof Player) { if (entity instanceof Player) {
@ -958,11 +957,6 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
final long chunkIndex = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ()); final long chunkIndex = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
Set<Entity> entities = getEntitiesInChunk(chunkIndex); Set<Entity> entities = getEntitiesInChunk(chunkIndex);
entities.remove(entity); entities.remove(entity);
if (entities.isEmpty()) {
this.chunkEntities.remove(chunkIndex);
} else {
this.chunkEntities.put(chunkIndex, entities);
}
this.entities.remove(entity); this.entities.remove(entity);
if (entity instanceof Player) { if (entity instanceof Player) {

View File

@ -539,7 +539,7 @@ public class InstanceContainer extends Instance {
chunkGenerator.fillBiomes(biomes, chunkX, chunkZ); chunkGenerator.fillBiomes(biomes, chunkX, chunkZ);
} }
final Chunk chunk = chunkSupplier.getChunk(biomes, chunkX, chunkZ); final Chunk chunk = chunkSupplier.createChunk(biomes, chunkX, chunkZ);
Check.notNull(chunk, "Chunks supplied by a ChunkSupplier cannot be null."); Check.notNull(chunk, "Chunks supplied by a ChunkSupplier cannot be null.");
cacheChunk(chunk); cacheChunk(chunk);

View File

@ -77,7 +77,7 @@ public class MinestomBasicChunkLoader implements IChunkLoader {
// Found, load from result bytes // Found, load from result bytes
BinaryReader reader = new BinaryReader(bytes); BinaryReader reader = new BinaryReader(bytes);
// Create the chunk object using the instance's ChunkSupplier to support multiple implementations // Create the chunk object using the instance's ChunkSupplier to support multiple implementations
Chunk chunk = instanceContainer.getChunkSupplier().getChunk(null, chunkX, chunkZ); Chunk chunk = instanceContainer.getChunkSupplier().createChunk(null, chunkX, chunkZ);
// Execute the callback once all blocks are placed (allow for multithreaded implementations) // Execute the callback once all blocks are placed (allow for multithreaded implementations)
chunk.readChunk(reader, callback); chunk.readChunk(reader, callback);
return true; return true;

View File

@ -304,11 +304,13 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
if (!isViewer(player)) if (!isViewer(player))
return; return;
SetSlotPacket setSlotPacket = new SetSlotPacket(); final ItemStack currentCursorItem = cursorPlayersItem.get(player);
setSlotPacket.windowId = -1; final boolean similar = currentCursorItem != null && currentCursorItem.isSimilar(cursorItem);
setSlotPacket.slot = -1;
setSlotPacket.itemStack = cursorItem; if (!similar) {
player.getPlayerConnection().sendPacket(setSlotPacket); final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem);
player.getPlayerConnection().sendPacket(setSlotPacket);
}
this.cursorPlayersItem.put(player, cursorItem); this.cursorPlayersItem.put(player, cursorItem);
} }

View File

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

View File

@ -221,7 +221,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
* @param slot the slot to refresh * @param slot the slot to refresh
*/ */
public void refreshSlot(short slot) { public void refreshSlot(short slot) {
sendSlotRefresh((short) convertToPacketSlot(slot), getItemStack(slot)); final int packetSlot = convertToPacketSlot(slot);
sendSlotRefresh((short) packetSlot, getItemStack(slot));
} }
/** /**
@ -240,12 +241,13 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
* @param cursorItem the new cursor item * @param cursorItem the new cursor item
*/ */
public void setCursorItem(@NotNull ItemStack cursorItem) { public void setCursorItem(@NotNull ItemStack cursorItem) {
final boolean similar = this.cursorItem.isSimilar(cursorItem);
this.cursorItem = cursorItem; this.cursorItem = cursorItem;
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = -1; if (!similar) {
setSlotPacket.slot = -1; final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem);
setSlotPacket.itemStack = cursorItem; player.getPlayerConnection().sendPacket(setSlotPacket);
player.getPlayerConnection().sendPacket(setSlotPacket); }
} }
/** /**
@ -299,8 +301,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
// Refresh slot // Refresh slot
update(); update();
//problem with ghost item when clicking on a slot which has a different internal id // FIXME: replace update() to refreshSlot, currently not possible because our inventory click handling is not exactly the same as what the client expects
//refreshSlot(slot); //refreshSlot((short) slot);
} }
protected void setItemStackInternal(int slot, ItemStack itemStack) { protected void setItemStackInternal(int slot, ItemStack itemStack) {
@ -340,7 +342,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
*/ */
protected void sendSlotRefresh(short slot, ItemStack itemStack) { protected void sendSlotRefresh(short slot, ItemStack itemStack) {
SetSlotPacket setSlotPacket = new SetSlotPacket(); SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = (byte) (MathUtils.isBetween(slot, 35, INVENTORY_SIZE) ? 0 : -2); setSlotPacket.windowId = 0;
setSlotPacket.slot = slot; setSlotPacket.slot = slot;
setSlotPacket.itemStack = itemStack; setSlotPacket.itemStack = itemStack;
player.getPlayerConnection().sendPacket(setSlotPacket); player.getPlayerConnection().sendPacket(setSlotPacket);

View File

@ -491,6 +491,7 @@ public class InventoryClickProcessor {
return clickResult; return clickResult;
} }
@NotNull
private InventoryClickResult startCondition(@NotNull InventoryClickResult clickResult, @Nullable Inventory inventory, private InventoryClickResult startCondition(@NotNull InventoryClickResult clickResult, @Nullable Inventory inventory,
@NotNull Player player, int slot, @NotNull ClickType clickType, @NotNull Player player, int slot, @NotNull ClickType clickType,
ItemStack clicked, ItemStack cursor) { ItemStack clicked, ItemStack cursor) {
@ -516,14 +517,14 @@ public class InventoryClickProcessor {
// PreClickEvent // 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); player.callEvent(InventoryPreClickEvent.class, inventoryPreClickEvent);
cursor = inventoryPreClickEvent.getCursorItem(); cursor = inventoryPreClickEvent.getCursorItem();
clicked = inventoryPreClickEvent.getClickedItem(); clicked = inventoryPreClickEvent.getClickedItem();
clickResult.setCancel(inventoryPreClickEvent.isCancelled());
if (inventoryPreClickEvent.isCancelled()) { if (inventoryPreClickEvent.isCancelled()) {
clickResult.setRefresh(true); clickResult.setRefresh(true);
clickResult.setCancel(true);
} }
} }
@ -542,9 +543,9 @@ public class InventoryClickProcessor {
clickResult.setCursor(cursor); clickResult.setCursor(cursor);
clickResult.setClicked(clicked); clickResult.setClicked(clicked);
clickResult.setCancel(result.isCancel());
if (result.isCancel()) { if (result.isCancel()) {
clickResult.setRefresh(true); clickResult.setRefresh(true);
clickResult.setCancel(true);
} }
} }
@ -559,6 +560,7 @@ public class InventoryClickProcessor {
return clickResult; return clickResult;
} }
@NotNull
private InventoryClickResult startCondition(@Nullable Inventory inventory, @NotNull Player player, int slot, private InventoryClickResult startCondition(@Nullable Inventory inventory, @NotNull Player player, int slot,
@NotNull ClickType clickType, ItemStack clicked, ItemStack cursor) { @NotNull ClickType clickType, ItemStack clicked, ItemStack cursor) {
final InventoryClickResult clickResult = new InventoryClickResult(clicked, cursor); final InventoryClickResult clickResult = new InventoryClickResult(clicked, cursor);
@ -567,7 +569,7 @@ public class InventoryClickProcessor {
private void callClickEvent(@NotNull Player player, @Nullable Inventory inventory, int slot, private void callClickEvent(@NotNull Player player, @Nullable Inventory inventory, int slot,
@NotNull ClickType clickType, ItemStack clicked, ItemStack cursor) { @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); player.callEvent(InventoryClickEvent.class, inventoryClickEvent);
} }

View File

@ -1,6 +1,7 @@
package net.minestom.server.item.metadata; package net.minestom.server.item.metadata;
import net.minestom.server.utils.Position; import net.minestom.server.utils.Position;
import net.minestom.server.utils.clone.CloneUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -95,7 +96,7 @@ public class CompassMeta extends ItemMeta {
CompassMeta compassMeta = (CompassMeta) super.clone(); CompassMeta compassMeta = (CompassMeta) super.clone();
compassMeta.lodestoneTracked = lodestoneTracked; compassMeta.lodestoneTracked = lodestoneTracked;
compassMeta.lodestoneDimension = lodestoneDimension; compassMeta.lodestoneDimension = lodestoneDimension;
compassMeta.lodestonePosition = lodestonePosition != null ? lodestonePosition.clone() : null; compassMeta.lodestonePosition = CloneUtils.optionalClone(lodestonePosition);
return compassMeta; return compassMeta;
} }

View File

@ -4,6 +4,7 @@ import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material; import net.minestom.server.item.Material;
import net.minestom.server.registry.Registries; import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NBTUtils; import net.minestom.server.utils.NBTUtils;
import net.minestom.server.utils.clone.CloneUtils;
import net.minestom.server.utils.validate.Check; import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -183,9 +184,9 @@ public class CrossbowMeta extends ItemMeta {
public ItemMeta clone() { public ItemMeta clone() {
CrossbowMeta crossbowMeta = (CrossbowMeta) super.clone(); CrossbowMeta crossbowMeta = (CrossbowMeta) super.clone();
crossbowMeta.triple = triple; crossbowMeta.triple = triple;
crossbowMeta.projectile1 = projectile1 == null ? null : projectile1.clone(); crossbowMeta.projectile1 = CloneUtils.optionalClone(projectile1);
crossbowMeta.projectile2 = projectile2 == null ? null : projectile2.clone(); crossbowMeta.projectile2 = CloneUtils.optionalClone(projectile2);
crossbowMeta.projectile3 = projectile3 == null ? null : projectile3.clone(); crossbowMeta.projectile3 = CloneUtils.optionalClone(projectile3);
crossbowMeta.charged = charged; crossbowMeta.charged = charged;

View File

@ -1,19 +1,21 @@
package net.minestom.server.item.metadata; package net.minestom.server.item.metadata;
import net.minestom.server.chat.ChatColor; import net.minestom.server.chat.ChatColor;
import net.minestom.server.utils.clone.CloneUtils;
import net.minestom.server.utils.clone.PublicCloneable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList; import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes; import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class MapMeta extends ItemMeta { public class MapMeta extends ItemMeta {
private int mapId; private int mapId;
private int mapScaleDirection = 1; private int mapScaleDirection = 1;
private List<MapDecoration> decorations = new ArrayList<>(); private List<MapDecoration> decorations = new CopyOnWriteArrayList<>();
private ChatColor mapColor = ChatColor.NO_COLOR; private ChatColor mapColor = ChatColor.NO_COLOR;
public MapMeta() { public MapMeta() {
@ -198,18 +200,18 @@ public class MapMeta extends ItemMeta {
MapMeta mapMeta = (MapMeta) super.clone(); MapMeta mapMeta = (MapMeta) super.clone();
mapMeta.setMapId(mapId); mapMeta.setMapId(mapId);
mapMeta.setMapScaleDirection(mapScaleDirection); mapMeta.setMapScaleDirection(mapScaleDirection);
mapMeta.decorations.addAll(decorations); mapMeta.decorations = CloneUtils.cloneCopyOnWriteArrayList(decorations);
mapMeta.setMapColor(mapColor); mapMeta.setMapColor(mapColor);
return mapMeta; return mapMeta;
} }
public static class MapDecoration { public static class MapDecoration implements PublicCloneable<MapDecoration> {
private final String id; private final String id;
private final byte type; private final byte type;
private final byte x, z; private final byte x, z;
private final double rotation; private final double rotation;
public MapDecoration(String id, byte type, byte x, byte z, double rotation) { public MapDecoration(@NotNull String id, byte type, byte x, byte z, double rotation) {
this.id = id; this.id = id;
this.type = type; this.type = type;
this.x = x; this.x = x;
@ -262,6 +264,17 @@ public class MapMeta extends ItemMeta {
public double getRotation() { public double getRotation() {
return rotation; return rotation;
} }
@NotNull
@Override
public MapDecoration clone() {
try {
return (MapDecoration) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new IllegalStateException("Something weird happened");
}
}
} }
} }

View File

@ -4,7 +4,9 @@ import net.minestom.server.chat.ChatColor;
import net.minestom.server.potion.CustomPotionEffect; import net.minestom.server.potion.CustomPotionEffect;
import net.minestom.server.potion.PotionType; import net.minestom.server.potion.PotionType;
import net.minestom.server.registry.Registries; import net.minestom.server.registry.Registries;
import net.minestom.server.utils.clone.CloneUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList; import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes; import org.jglrxavpok.hephaistos.nbt.NBTTypes;
@ -22,7 +24,9 @@ import java.util.concurrent.CopyOnWriteArrayList;
public class PotionMeta extends ItemMeta { public class PotionMeta extends ItemMeta {
private PotionType potionType; private PotionType potionType;
private final List<CustomPotionEffect> customPotionEffects = new CopyOnWriteArrayList<>();
// Not final because of #clone()
private List<CustomPotionEffect> customPotionEffects = new CopyOnWriteArrayList<>();
private boolean hasColor; private boolean hasColor;
private byte red, green, blue; private byte red, green, blue;
@ -32,6 +36,7 @@ public class PotionMeta extends ItemMeta {
* *
* @return the potion type * @return the potion type
*/ */
@Nullable
public PotionType getPotionType() { public PotionType getPotionType() {
return potionType; return potionType;
} }
@ -41,15 +46,16 @@ public class PotionMeta extends ItemMeta {
* *
* @param potionType the new potion type * @param potionType the new potion type
*/ */
public void setPotionType(PotionType potionType) { public void setPotionType(@Nullable PotionType potionType) {
this.potionType = potionType; this.potionType = potionType;
} }
/** /**
* Get a list of {@link CustomPotionEffect}. * Get a list of {@link CustomPotionEffect}.
* *
* @return the custom potion effects * @return the custom potion effect list
*/ */
@NotNull
public List<CustomPotionEffect> getCustomPotionEffects() { public List<CustomPotionEffect> getCustomPotionEffects() {
return customPotionEffects; return customPotionEffects;
} }
@ -75,7 +81,8 @@ public class PotionMeta extends ItemMeta {
@Override @Override
public boolean hasNbt() { public boolean hasNbt() {
return potionType != null; return potionType != null ||
!customPotionEffects.isEmpty();
} }
@Override @Override
@ -155,7 +162,7 @@ public class PotionMeta extends ItemMeta {
public ItemMeta clone() { public ItemMeta clone() {
PotionMeta potionMeta = (PotionMeta) super.clone(); PotionMeta potionMeta = (PotionMeta) super.clone();
potionMeta.potionType = potionType; potionMeta.potionType = potionType;
potionMeta.customPotionEffects.addAll(customPotionEffects); potionMeta.customPotionEffects = CloneUtils.cloneCopyOnWriteArrayList(customPotionEffects);
potionMeta.hasColor = hasColor; potionMeta.hasColor = hasColor;
potionMeta.red = red; potionMeta.red = red;

View File

@ -19,6 +19,7 @@ public class WritableBookMeta extends ItemMeta {
* *
* @return a modifiable {@link ArrayList} containing the book pages * @return a modifiable {@link ArrayList} containing the book pages
*/ */
@NotNull
public ArrayList<String> getPages() { public ArrayList<String> getPages() {
return pages; return pages;
} }
@ -28,7 +29,7 @@ public class WritableBookMeta extends ItemMeta {
* *
* @param pages the pages list * @param pages the pages list
*/ */
public void setPages(ArrayList<String> pages) { public void setPages(@NotNull ArrayList<String> pages) {
this.pages = pages; this.pages = pages;
} }
@ -70,8 +71,7 @@ public class WritableBookMeta extends ItemMeta {
@Override @Override
public ItemMeta clone() { public ItemMeta clone() {
WritableBookMeta writableBookMeta = (WritableBookMeta) super.clone(); WritableBookMeta writableBookMeta = (WritableBookMeta) super.clone();
writableBookMeta.pages.addAll(pages); writableBookMeta.pages = new ArrayList<>(pages);
return writableBookMeta; return writableBookMeta;
} }
} }

View File

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

View File

@ -3,6 +3,7 @@ package net.minestom.server.listener;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerMoveEvent; import net.minestom.server.event.player.PlayerMoveEvent;
import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.network.packet.client.play.ClientPlayerPacket; import net.minestom.server.network.packet.client.play.ClientPlayerPacket;
import net.minestom.server.network.packet.client.play.ClientPlayerPositionAndRotationPacket; import net.minestom.server.network.packet.client.play.ClientPlayerPositionAndRotationPacket;
import net.minestom.server.network.packet.client.play.ClientPlayerPositionPacket; import net.minestom.server.network.packet.client.play.ClientPlayerPositionPacket;
@ -52,6 +53,13 @@ public class PlayerPositionListener {
private static void processMovement(@NotNull Player player, float x, float y, float z, private static void processMovement(@NotNull Player player, float x, float y, float z,
float yaw, float pitch, boolean onGround) { float yaw, float pitch, boolean onGround) {
final Instance instance = player.getInstance();
// Prevent moving before the player spawned, probably a modified client (or high latency?)
if (instance == null) {
return;
}
// Prevent the player from moving during a teleport // Prevent the player from moving during a teleport
final float distance = player.getPosition().getDistance(x, y, z); final float distance = player.getPosition().getDistance(x, y, z);
final int chunkRange = player.getChunkRange() * Chunk.CHUNK_SECTION_SIZE; final int chunkRange = player.getChunkRange() * Chunk.CHUNK_SECTION_SIZE;
@ -60,12 +68,12 @@ public class PlayerPositionListener {
} }
// Try to move in an unloaded chunk, prevent it // Try to move in an unloaded chunk, prevent it
if (!ChunkUtils.isLoaded(player.getInstance(), x, z)) { if (!ChunkUtils.isLoaded(instance, x, z)) {
player.teleport(player.getPosition()); player.teleport(player.getPosition());
return; return;
} }
final Position currentPosition = player.getPosition().copy(); final Position currentPosition = player.getPosition().clone();
Position newPosition = new Position(x, y, z, yaw, pitch); Position newPosition = new Position(x, y, z, yaw, pitch);
final Position cachedPosition = newPosition.clone(); final Position cachedPosition = newPosition.clone();

View File

@ -35,6 +35,11 @@ public class WindowListener {
boolean successful = false; boolean successful = false;
// prevent click in a non interactive slot (why does it exist?)
if (slot == -1) {
return;
}
switch (mode) { switch (mode) {
case 0: case 0:
switch (button) { switch (button) {
@ -80,7 +85,10 @@ public class WindowListener {
break; break;
} }
refreshCursorItem(player, inventory); // Prevent the player from picking a ghost item in cursor
if (!successful) {
refreshCursorItem(player, inventory);
}
WindowConfirmationPacket windowConfirmationPacket = new WindowConfirmationPacket(); WindowConfirmationPacket windowConfirmationPacket = new WindowConfirmationPacket();
windowConfirmationPacket.windowId = windowId; windowConfirmationPacket.windowId = windowId;
@ -90,6 +98,22 @@ public class WindowListener {
player.getPlayerConnection().sendPacket(windowConfirmationPacket); player.getPlayerConnection().sendPacket(windowConfirmationPacket);
} }
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.getOpenInventory(), player);
player.callEvent(InventoryCloseEvent.class, inventoryCloseEvent);
player.closeInventory();
Inventory newInventory = inventoryCloseEvent.getNewInventory();
if (newInventory != null)
player.openInventory(newInventory);
}
public static void windowConfirmationListener(ClientWindowConfirmationPacket packet, Player player) {
// Empty
}
/** /**
* @param player the player to refresh the cursor item * @param player the player to refresh the cursor item
* @param inventory the player open inventory, null if not any (could be player inventory) * @param inventory the player open inventory, null if not any (could be player inventory)
@ -109,28 +133,9 @@ public class WindowListener {
return; return;
} }
SetSlotPacket setSlotPacket = new SetSlotPacket(); final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem);
setSlotPacket.windowId = -1;
setSlotPacket.slot = -1;
setSlotPacket.itemStack = cursorItem;
player.getPlayerConnection().sendPacket(setSlotPacket); player.getPlayerConnection().sendPacket(setSlotPacket);
} }
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());
player.callEvent(InventoryCloseEvent.class, inventoryCloseEvent);
player.closeInventory();
Inventory newInventory = inventoryCloseEvent.getNewInventory();
if (newInventory != null)
player.openInventory(newInventory);
}
public static void windowConfirmationListener(ClientWindowConfirmationPacket packet, Player player) {
// Empty
}
} }

View File

@ -93,34 +93,54 @@ public final class NettyServer {
Class<? extends ServerChannel> channel; Class<? extends ServerChannel> channel;
final int workerThreadCount = MinecraftServer.getNettyThreadCount(); final int workerThreadCount = MinecraftServer.getNettyThreadCount();
if (IOUring.isAvailable()) { // Find boss/worker event group
boss = new IOUringEventLoopGroup(2); {
worker = new IOUringEventLoopGroup(workerThreadCount); if (IOUring.isAvailable()) {
boss = new IOUringEventLoopGroup(2);
worker = new IOUringEventLoopGroup(workerThreadCount);
channel = IOUringServerSocketChannel.class; channel = IOUringServerSocketChannel.class;
LOGGER.info("Using io_uring"); LOGGER.info("Using io_uring");
} else if (Epoll.isAvailable()) { } else if (Epoll.isAvailable()) {
boss = new EpollEventLoopGroup(2); boss = new EpollEventLoopGroup(2);
worker = new EpollEventLoopGroup(workerThreadCount); worker = new EpollEventLoopGroup(workerThreadCount);
channel = EpollServerSocketChannel.class; channel = EpollServerSocketChannel.class;
LOGGER.info("Using epoll"); LOGGER.info("Using epoll");
} else if (KQueue.isAvailable()) { } else if (KQueue.isAvailable()) {
boss = new KQueueEventLoopGroup(2); boss = new KQueueEventLoopGroup(2);
worker = new KQueueEventLoopGroup(workerThreadCount); worker = new KQueueEventLoopGroup(workerThreadCount);
channel = KQueueServerSocketChannel.class; channel = KQueueServerSocketChannel.class;
LOGGER.info("Using kqueue"); LOGGER.info("Using kqueue");
} else { } else {
boss = new NioEventLoopGroup(2); boss = new NioEventLoopGroup(2);
worker = new NioEventLoopGroup(workerThreadCount); worker = new NioEventLoopGroup(workerThreadCount);
channel = NioServerSocketChannel.class; channel = NioServerSocketChannel.class;
LOGGER.info("Using NIO"); LOGGER.info("Using NIO");
}
}
// Add default allocator settings
{
if (System.getProperty("io.netty.allocator.numDirectArenas") == null) {
System.setProperty("io.netty.allocator.numDirectArenas", String.valueOf(workerThreadCount));
}
if (System.getProperty("io.netty.allocator.numHeapArenas") == null) {
System.setProperty("io.netty.allocator.numHeapArenas", String.valueOf(workerThreadCount));
}
if (System.getProperty("io.netty.allocator.maxOrder") == null) {
// The default page size is 8192 bytes, a bit shift of 5 makes it 262KB
// largely enough for this type of server
System.setProperty("io.netty.allocator.maxOrder", "5");
}
} }
bootstrap = new ServerBootstrap() bootstrap = new ServerBootstrap()

View File

@ -11,7 +11,18 @@ public class GroupedPacketHandler extends MessageToByteEncoder<FramedPacket> {
protected void encode(ChannelHandlerContext ctx, FramedPacket msg, ByteBuf out) { protected void encode(ChannelHandlerContext ctx, FramedPacket msg, ByteBuf out) {
final ByteBuf packet = msg.body; final ByteBuf packet = msg.body;
out.writeBytes(packet.retainedSlice()); out.setBytes(0, packet, 0, packet.writerIndex());
out.writerIndex(packet.writerIndex());
}
@Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, FramedPacket msg, boolean preferDirect) {
final int size = msg.body.writerIndex();
if (preferDirect) {
return ctx.alloc().directBuffer(size, size);
} else {
return ctx.alloc().heapBuffer(size, size);
}
} }
} }

View File

@ -15,7 +15,7 @@ public class PlayerAbilitiesPacket implements ServerPacket {
// Options // Options
public float flyingSpeed; public float flyingSpeed;
public float walkingSpeed; public float fieldViewModifier;
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
@ -31,7 +31,7 @@ public class PlayerAbilitiesPacket implements ServerPacket {
writer.writeByte(flags); writer.writeByte(flags);
writer.writeFloat(flyingSpeed); writer.writeFloat(flyingSpeed);
writer.writeFloat(walkingSpeed); writer.writeFloat(fieldViewModifier);
} }
@Override @Override

View File

@ -23,4 +23,19 @@ public class SetSlotPacket implements ServerPacket {
public int getId() { public int getId() {
return ServerPacketIdentifier.SET_SLOT; return ServerPacketIdentifier.SET_SLOT;
} }
/**
* Returns a {@link SetSlotPacket} used to change a player cursor item.
*
* @param cursorItem the cursor item
* @return a set slot packet to change a player cursor item
*/
@NotNull
public static SetSlotPacket createCursorPacket(@NotNull ItemStack cursorItem) {
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = -1;
setSlotPacket.slot = -1;
setSlotPacket.itemStack = cursorItem;
return setSlotPacket;
}
} }

View File

@ -1,5 +1,6 @@
package net.minestom.server.network.packet.server.play; package net.minestom.server.network.packet.server.play;
import net.minestom.server.MinecraftServer;
import net.minestom.server.gamedata.tags.Tag; import net.minestom.server.gamedata.tags.Tag;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.packet.server.ServerPacketIdentifier;
@ -20,6 +21,12 @@ public class TagsPacket implements ServerPacket {
public List<Tag> fluidTags = new LinkedList<>(); public List<Tag> fluidTags = new LinkedList<>();
public List<Tag> entityTags = new LinkedList<>(); public List<Tag> entityTags = new LinkedList<>();
private static final TagsPacket REQUIRED_TAGS_PACKET = new TagsPacket();
static {
MinecraftServer.getTagManager().addRequiredTagsToPacket(REQUIRED_TAGS_PACKET);
}
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writeTags(writer, blockTags, name -> Registries.getBlock(name).ordinal()); writeTags(writer, blockTags, name -> Registries.getBlock(name).ordinal());
@ -48,4 +55,15 @@ public class TagsPacket implements ServerPacket {
public int getId() { public int getId() {
return ServerPacketIdentifier.TAGS; return ServerPacketIdentifier.TAGS;
} }
/**
* Gets the {@link TagsPacket} sent to every {@link net.minestom.server.entity.Player}
* on login.
*
* @return the default tags packet
*/
@NotNull
public static TagsPacket getRequiredTagsPacket() {
return REQUIRED_TAGS_PACKET;
}
} }

View File

@ -145,27 +145,31 @@ public class NettyPlayerConnection extends PlayerConnection {
@NotNull @NotNull
public ChannelFuture write(@NotNull Object message) { public ChannelFuture write(@NotNull Object message) {
ChannelFuture channelFuture = channel.write(message);
if (MinecraftServer.shouldProcessNettyErrors()) { if (MinecraftServer.shouldProcessNettyErrors()) {
return channel.write(message).addListener(future -> { return channelFuture.addListener(future -> {
if (!future.isSuccess()) { if (!future.isSuccess()) {
future.cause().printStackTrace(); future.cause().printStackTrace();
} }
}); });
} else { } else {
return channel.write(message); return channelFuture;
} }
} }
@NotNull @NotNull
public ChannelFuture writeAndFlush(@NotNull Object message) { public ChannelFuture writeAndFlush(@NotNull Object message) {
ChannelFuture channelFuture = channel.writeAndFlush(message);
if (MinecraftServer.shouldProcessNettyErrors()) { if (MinecraftServer.shouldProcessNettyErrors()) {
return channel.writeAndFlush(message).addListener(future -> { return channelFuture.addListener(future -> {
if (!future.isSuccess()) { if (!future.isSuccess()) {
future.cause().printStackTrace(); future.cause().printStackTrace();
} }
}); });
} else { } else {
return channel.writeAndFlush(message); return channelFuture;
} }
} }

View File

@ -1,9 +1,16 @@
package net.minestom.server.potion; package net.minestom.server.potion;
import net.minestom.server.utils.clone.PublicCloneable;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
/** /**
* Represents a custom effect in {@link net.minestom.server.item.metadata.PotionMeta}. * Represents a custom effect in {@link net.minestom.server.item.metadata.PotionMeta}.
* <p>
* This is an immutable class.
*/ */
public class CustomPotionEffect { public class CustomPotionEffect implements PublicCloneable<CustomPotionEffect> {
private final byte id; private final byte id;
private final byte amplifier; private final byte amplifier;
@ -45,4 +52,28 @@ public class CustomPotionEffect {
public boolean showIcon() { public boolean showIcon() {
return showIcon; return showIcon;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CustomPotionEffect that = (CustomPotionEffect) o;
return id == that.id && amplifier == that.amplifier && duration == that.duration && ambient == that.ambient && showParticles == that.showParticles && showIcon == that.showIcon;
}
@Override
public int hashCode() {
return Objects.hash(id, amplifier, duration, ambient, showParticles, showIcon);
}
@NotNull
@Override
public CustomPotionEffect clone() {
try {
return (CustomPotionEffect) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new IllegalStateException("Weird thing happened");
}
}
} }

View File

@ -20,5 +20,5 @@ public interface ChunkSupplier {
* @return a newly {@link Chunk} object, cannot be null * @return a newly {@link Chunk} object, cannot be null
*/ */
@NotNull @NotNull
Chunk getChunk(@Nullable Biome[] biomes, int chunkX, int chunkZ); Chunk createChunk(@Nullable Biome[] biomes, int chunkX, int chunkZ);
} }

View File

@ -0,0 +1,30 @@
package net.minestom.server.utils.clone;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Convenient interface to deep-copy single object or collections.
* <p>
* Most of the methods require object to implement the {@link PublicCloneable} interface.
*/
public final class CloneUtils {
@Nullable
public static <T extends PublicCloneable> T optionalClone(@Nullable T object) {
return object != null ? (T) object.clone() : null;
}
@NotNull
public static <T extends PublicCloneable> CopyOnWriteArrayList cloneCopyOnWriteArrayList(@NotNull List<T> list) {
CopyOnWriteArrayList<T> result = new CopyOnWriteArrayList<>();
for (T element : list) {
result.add((T) element.clone());
}
return result;
}
}

View File

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