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 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_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 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");

View File

@ -202,27 +202,29 @@ public final class CommandManager {
}
// Process the command
try {
{
// Check for rich-command
this.dispatcher.execute(sender, command);
return true;
} catch (NullPointerException e) {
// Check for legacy-command
final String[] splitCommand = command.split(" ");
final String commandName = splitCommand[0];
final CommandProcessor commandProcessor = commandProcessorMap.get(commandName.toLowerCase());
if (commandProcessor == null) {
if (unknownCommandCallback != null) {
this.unknownCommandCallback.apply(sender, command);
final boolean result = this.dispatcher.execute(sender, command);
if (result) {
return true;
} else {
// Check for legacy-command
final String[] splitCommand = command.split(" ");
final String commandName = splitCommand[0];
final CommandProcessor commandProcessor = commandProcessorMap.get(commandName.toLowerCase());
if (commandProcessor == null) {
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) {
return args.computeIfAbsent(id, s -> {
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);
}
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);
result.execute(source, commandString);
if (result != null) {
result.execute(source, commandString);
}
return result != null;
}
@NotNull

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -930,7 +930,6 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
synchronized (entitiesLock) {
Set<Entity> entities = getEntitiesInChunk(chunkIndex);
entities.add(entity);
this.chunkEntities.put(chunkIndex, entities);
this.entities.add(entity);
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());
Set<Entity> entities = getEntitiesInChunk(chunkIndex);
entities.remove(entity);
if (entities.isEmpty()) {
this.chunkEntities.remove(chunkIndex);
} else {
this.chunkEntities.put(chunkIndex, entities);
}
this.entities.remove(entity);
if (entity instanceof Player) {

View File

@ -539,7 +539,7 @@ public class InstanceContainer extends Instance {
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.");
cacheChunk(chunk);

View File

@ -77,7 +77,7 @@ public class MinestomBasicChunkLoader implements IChunkLoader {
// Found, load from result bytes
BinaryReader reader = new BinaryReader(bytes);
// 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)
chunk.readChunk(reader, callback);
return true;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,19 +1,21 @@
package net.minestom.server.item.metadata;
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.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class MapMeta extends ItemMeta {
private int mapId;
private int mapScaleDirection = 1;
private List<MapDecoration> decorations = new ArrayList<>();
private List<MapDecoration> decorations = new CopyOnWriteArrayList<>();
private ChatColor mapColor = ChatColor.NO_COLOR;
public MapMeta() {
@ -198,18 +200,18 @@ public class MapMeta extends ItemMeta {
MapMeta mapMeta = (MapMeta) super.clone();
mapMeta.setMapId(mapId);
mapMeta.setMapScaleDirection(mapScaleDirection);
mapMeta.decorations.addAll(decorations);
mapMeta.decorations = CloneUtils.cloneCopyOnWriteArrayList(decorations);
mapMeta.setMapColor(mapColor);
return mapMeta;
}
public static class MapDecoration {
public static class MapDecoration implements PublicCloneable<MapDecoration> {
private final String id;
private final byte type;
private final byte x, z;
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.type = type;
this.x = x;
@ -262,6 +264,17 @@ public class MapMeta extends ItemMeta {
public double getRotation() {
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.PotionType;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.clone.CloneUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
@ -22,7 +24,9 @@ import java.util.concurrent.CopyOnWriteArrayList;
public class PotionMeta extends ItemMeta {
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 byte red, green, blue;
@ -32,6 +36,7 @@ public class PotionMeta extends ItemMeta {
*
* @return the potion type
*/
@Nullable
public PotionType getPotionType() {
return potionType;
}
@ -41,15 +46,16 @@ public class PotionMeta extends ItemMeta {
*
* @param potionType the new potion type
*/
public void setPotionType(PotionType potionType) {
public void setPotionType(@Nullable PotionType potionType) {
this.potionType = potionType;
}
/**
* Get a list of {@link CustomPotionEffect}.
*
* @return the custom potion effects
* @return the custom potion effect list
*/
@NotNull
public List<CustomPotionEffect> getCustomPotionEffects() {
return customPotionEffects;
}
@ -75,7 +81,8 @@ public class PotionMeta extends ItemMeta {
@Override
public boolean hasNbt() {
return potionType != null;
return potionType != null ||
!customPotionEffects.isEmpty();
}
@Override
@ -155,7 +162,7 @@ public class PotionMeta extends ItemMeta {
public ItemMeta clone() {
PotionMeta potionMeta = (PotionMeta) super.clone();
potionMeta.potionType = potionType;
potionMeta.customPotionEffects.addAll(customPotionEffects);
potionMeta.customPotionEffects = CloneUtils.cloneCopyOnWriteArrayList(customPotionEffects);
potionMeta.hasColor = hasColor;
potionMeta.red = red;

View File

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

View File

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

View File

@ -3,6 +3,7 @@ package net.minestom.server.listener;
import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerMoveEvent;
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.ClientPlayerPositionAndRotationPacket;
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,
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
final float distance = player.getPosition().getDistance(x, y, z);
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
if (!ChunkUtils.isLoaded(player.getInstance(), x, z)) {
if (!ChunkUtils.isLoaded(instance, x, z)) {
player.teleport(player.getPosition());
return;
}
final Position currentPosition = player.getPosition().copy();
final Position currentPosition = player.getPosition().clone();
Position newPosition = new Position(x, y, z, yaw, pitch);
final Position cachedPosition = newPosition.clone();

View File

@ -35,6 +35,11 @@ public class WindowListener {
boolean successful = false;
// prevent click in a non interactive slot (why does it exist?)
if (slot == -1) {
return;
}
switch (mode) {
case 0:
switch (button) {
@ -80,7 +85,10 @@ public class WindowListener {
break;
}
refreshCursorItem(player, inventory);
// Prevent the player from picking a ghost item in cursor
if (!successful) {
refreshCursorItem(player, inventory);
}
WindowConfirmationPacket windowConfirmationPacket = new WindowConfirmationPacket();
windowConfirmationPacket.windowId = windowId;
@ -90,6 +98,22 @@ public class WindowListener {
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 inventory the player open inventory, null if not any (could be player inventory)
@ -109,28 +133,9 @@ public class WindowListener {
return;
}
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = -1;
setSlotPacket.slot = -1;
setSlotPacket.itemStack = cursorItem;
final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem);
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;
final int workerThreadCount = MinecraftServer.getNettyThreadCount();
if (IOUring.isAvailable()) {
boss = new IOUringEventLoopGroup(2);
worker = new IOUringEventLoopGroup(workerThreadCount);
// Find boss/worker event group
{
if (IOUring.isAvailable()) {
boss = new IOUringEventLoopGroup(2);
worker = new IOUringEventLoopGroup(workerThreadCount);
channel = IOUringServerSocketChannel.class;
channel = IOUringServerSocketChannel.class;
LOGGER.info("Using io_uring");
} else if (Epoll.isAvailable()) {
boss = new EpollEventLoopGroup(2);
worker = new EpollEventLoopGroup(workerThreadCount);
LOGGER.info("Using io_uring");
} else if (Epoll.isAvailable()) {
boss = new EpollEventLoopGroup(2);
worker = new EpollEventLoopGroup(workerThreadCount);
channel = EpollServerSocketChannel.class;
channel = EpollServerSocketChannel.class;
LOGGER.info("Using epoll");
} else if (KQueue.isAvailable()) {
boss = new KQueueEventLoopGroup(2);
worker = new KQueueEventLoopGroup(workerThreadCount);
LOGGER.info("Using epoll");
} else if (KQueue.isAvailable()) {
boss = new KQueueEventLoopGroup(2);
worker = new KQueueEventLoopGroup(workerThreadCount);
channel = KQueueServerSocketChannel.class;
channel = KQueueServerSocketChannel.class;
LOGGER.info("Using kqueue");
} else {
boss = new NioEventLoopGroup(2);
worker = new NioEventLoopGroup(workerThreadCount);
LOGGER.info("Using kqueue");
} else {
boss = new NioEventLoopGroup(2);
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()

View File

@ -11,7 +11,18 @@ public class GroupedPacketHandler extends MessageToByteEncoder<FramedPacket> {
protected void encode(ChannelHandlerContext ctx, FramedPacket msg, ByteBuf out) {
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
public float flyingSpeed;
public float walkingSpeed;
public float fieldViewModifier;
@Override
public void write(@NotNull BinaryWriter writer) {
@ -31,7 +31,7 @@ public class PlayerAbilitiesPacket implements ServerPacket {
writer.writeByte(flags);
writer.writeFloat(flyingSpeed);
writer.writeFloat(walkingSpeed);
writer.writeFloat(fieldViewModifier);
}
@Override

View File

@ -23,4 +23,19 @@ public class SetSlotPacket implements ServerPacket {
public int getId() {
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;
import net.minestom.server.MinecraftServer;
import net.minestom.server.gamedata.tags.Tag;
import net.minestom.server.network.packet.server.ServerPacket;
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> entityTags = new LinkedList<>();
private static final TagsPacket REQUIRED_TAGS_PACKET = new TagsPacket();
static {
MinecraftServer.getTagManager().addRequiredTagsToPacket(REQUIRED_TAGS_PACKET);
}
@Override
public void write(@NotNull BinaryWriter writer) {
writeTags(writer, blockTags, name -> Registries.getBlock(name).ordinal());
@ -48,4 +55,15 @@ public class TagsPacket implements ServerPacket {
public int getId() {
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
public ChannelFuture write(@NotNull Object message) {
ChannelFuture channelFuture = channel.write(message);
if (MinecraftServer.shouldProcessNettyErrors()) {
return channel.write(message).addListener(future -> {
return channelFuture.addListener(future -> {
if (!future.isSuccess()) {
future.cause().printStackTrace();
}
});
} else {
return channel.write(message);
return channelFuture;
}
}
@NotNull
public ChannelFuture writeAndFlush(@NotNull Object message) {
ChannelFuture channelFuture = channel.writeAndFlush(message);
if (MinecraftServer.shouldProcessNettyErrors()) {
return channel.writeAndFlush(message).addListener(future -> {
return channelFuture.addListener(future -> {
if (!future.isSuccess()) {
future.cause().printStackTrace();
}
});
} else {
return channel.writeAndFlush(message);
return channelFuture;
}
}

View File

@ -1,9 +1,16 @@
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}.
* <p>
* This is an immutable class.
*/
public class CustomPotionEffect {
public class CustomPotionEffect implements PublicCloneable<CustomPotionEffect> {
private final byte id;
private final byte amplifier;
@ -45,4 +52,28 @@ public class CustomPotionEffect {
public boolean 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
*/
@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.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.packet.server.play.EffectPacket;
import net.minestom.server.network.packet.server.play.PlayerListHeaderAndFooterPacket;
import net.minestom.server.ping.ResponseDataConsumer;
import net.minestom.server.utils.PacketUtils;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector;
import net.minestom.server.utils.inventory.PlayerInventoryUtils;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.world.DimensionType;
@ -113,7 +114,7 @@ public class PlayerInit {
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {
final Entity source = event.getSource();
final Entity source = event.getEntity();
final Entity entity = event.getTarget();
if (entity instanceof EntityCreature) {
EntityCreature creature = (EntityCreature) entity;
@ -202,17 +203,27 @@ public class PlayerInit {
player.getInventory().addInventoryCondition((p, slot, clickType, inventoryConditionResult) -> {
if (slot == -999)
return;
ItemStack itemStack = p.getInventory().getItemStack(slot);
System.out.println("test " + itemStack.getIdentifier() + " " + itemStack.getData());
//ItemStack itemStack = p.getInventory().getItemStack(slot);
//System.out.println("test " + itemStack.getIdentifier() + " " + itemStack.getData());
});
});
globalEventHandler.addEventCallback(PlayerSpawnEvent.class, event -> {
final Player player = event.getPlayer();
player.setGameMode(GameMode.CREATIVE);
player.setGameMode(GameMode.SURVIVAL);
PlayerInventory inventory = player.getInventory();
ItemStack itemStack = new ItemStack(Material.STONE, (byte) 64);
player.getInventory().addItemStack(itemStack);
inventory.addItemStack(itemStack);
{
ItemStack item = new ItemStack(Material.DIAMOND_CHESTPLATE, (byte) 1);
inventory.setChestplate(item);
item.setDisplayName(ColoredText.of("test"));
inventory.refreshSlot((short) PlayerInventoryUtils.CHESTPLATE_SLOT);
}
//player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 32));
});