Cleanup + comments

This commit is contained in:
themode 2020-10-31 05:09:30 +01:00
parent 2e4a2f57cf
commit c4f36a9cea
18 changed files with 123 additions and 49 deletions

View File

@ -38,7 +38,7 @@ public interface Viewable {
Set<Player> getViewers(); Set<Player> getViewers();
/** /**
* Gets if a player is seeing this viewable object * Gets if a player is seeing this viewable object.
* *
* @param player the player to check * @param player the player to check
* @return true if {@code player} is a viewer, false otherwise * @return true if {@code player} is a viewer, false otherwise
@ -48,10 +48,10 @@ public interface Viewable {
} }
/** /**
* Sends a packet to all viewers * Sends a packet to all viewers.
* <p> * <p>
* It is better than looping through the viewers * It is better than looping through the viewers
* to send a packet since it is here only serialized once * to send a packet since it is here only serialized once.
* *
* @param packet the packet to send to all viewers * @param packet the packet to send to all viewers
*/ */
@ -60,10 +60,10 @@ public interface Viewable {
} }
/** /**
* Sends multiple packets to all viewers * Sends multiple packets to all viewers.
* <p> * <p>
* It is better than looping through the viewers * It is better than looping through the viewers
* to send a packet since it is here only serialized once * to send a packet since it is here only serialized once.
* *
* @param packets the packets to send * @param packets the packets to send
*/ */
@ -74,16 +74,16 @@ public interface Viewable {
} }
/** /**
* Sends a packet to all viewers and the viewable element if it is a player * Sends a packet to all viewers and the viewable element if it is a player.
* <p> * <p>
* If 'this' isn't a player, then {@link #sendPacketToViewers(ServerPacket)} is called instead * If 'this' isn't a player, then {@link #sendPacketToViewers(ServerPacket)} is called instead.
* *
* @param packet the packet to send * @param packet the packet to send
*/ */
default void sendPacketToViewersAndSelf(@NotNull ServerPacket packet) { default void sendPacketToViewersAndSelf(@NotNull ServerPacket packet) {
if (this instanceof Player) { if (this instanceof Player) {
if (getViewers().isEmpty()) { if (getViewers().isEmpty()) {
((Player) this).getPlayerConnection().sendPacket(packet); PacketWriterUtils.writeAndSend((Player) this, packet);
} else { } else {
UNSAFE_sendPacketToViewersAndSelf(packet); UNSAFE_sendPacketToViewersAndSelf(packet);
} }
@ -93,9 +93,9 @@ public interface Viewable {
} }
/** /**
* Sends a packet to all the viewers and 'this' * Sends a packet to all the viewers and 'this'.
* <p> * <p>
* Unsafe because of a cast to {@link Player} without any check beforehand * Unsafe because of a cast to {@link Player} without any check beforehand.
* *
* @param packet the packet to send * @param packet the packet to send
*/ */

View File

@ -4,6 +4,7 @@ import net.minestom.server.entity.Entity;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Position; import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector; import net.minestom.server.utils.Vector;
import org.jetbrains.annotations.NotNull;
/** /**
* See https://wiki.vg/Entity_metadata#Mobs_2 * See https://wiki.vg/Entity_metadata#Mobs_2
@ -21,7 +22,7 @@ public class BoundingBox {
* @param y the height size * @param y the height size
* @param z the depth size * @param z the depth size
*/ */
public BoundingBox(Entity entity, float x, float y, float z) { public BoundingBox(@NotNull Entity entity, float x, float y, float z) {
this.entity = entity; this.entity = entity;
this.x = x; this.x = x;
this.y = y; this.y = y;
@ -34,7 +35,7 @@ public class BoundingBox {
* @param boundingBox the {@link BoundingBox} to check * @param boundingBox the {@link BoundingBox} to check
* @return true if the two {@link BoundingBox} intersect with each other, false otherwise * @return true if the two {@link BoundingBox} intersect with each other, false otherwise
*/ */
public boolean intersect(BoundingBox boundingBox) { public boolean intersect(@NotNull BoundingBox boundingBox) {
return (getMinX() <= boundingBox.getMaxX() && getMaxX() >= boundingBox.getMinX()) && return (getMinX() <= boundingBox.getMaxX() && getMaxX() >= boundingBox.getMinX()) &&
(getMinY() <= boundingBox.getMaxY() && getMaxY() >= boundingBox.getMinY()) && (getMinY() <= boundingBox.getMaxY() && getMaxY() >= boundingBox.getMinY()) &&
(getMinZ() <= boundingBox.getMaxZ() && getMaxZ() >= boundingBox.getMinZ()); (getMinZ() <= boundingBox.getMaxZ() && getMaxZ() >= boundingBox.getMinZ());
@ -46,7 +47,7 @@ public class BoundingBox {
* @param entity the entity to check the bounding box * @param entity the entity to check the bounding box
* @return true if this bounding box intersects with the entity, false otherwise * @return true if this bounding box intersects with the entity, false otherwise
*/ */
public boolean intersect(Entity entity) { public boolean intersect(@NotNull Entity entity) {
return intersect(entity.getBoundingBox()); return intersect(entity.getBoundingBox());
} }
@ -56,7 +57,7 @@ public class BoundingBox {
* @param blockPosition the position to check * @param blockPosition the position to check
* @return true if the bounding box intersects with the position, false otherwise * @return true if the bounding box intersects with the position, false otherwise
*/ */
public boolean intersect(BlockPosition blockPosition) { public boolean intersect(@NotNull BlockPosition blockPosition) {
final float offsetX = 1; final float offsetX = 1;
final float x = blockPosition.getX(); final float x = blockPosition.getX();
@ -90,7 +91,7 @@ public class BoundingBox {
(z >= getMinZ() && z <= getMaxZ()); (z >= getMinZ() && z <= getMaxZ());
} }
public boolean intersect(Position position) { public boolean intersect(@NotNull Position position) {
return intersect(position.getX(), position.getY(), position.getZ()); return intersect(position.getX(), position.getY(), position.getZ());
} }
@ -102,6 +103,7 @@ public class BoundingBox {
* @param z the Z offset * @param z the Z offset
* @return a new {@link BoundingBox} expanded * @return a new {@link BoundingBox} expanded
*/ */
@NotNull
public BoundingBox expand(float x, float y, float z) { public BoundingBox expand(float x, float y, float z) {
return new BoundingBox(entity, this.x + x, this.y + y, this.z + z); return new BoundingBox(entity, this.x + x, this.y + y, this.z + z);
} }
@ -114,6 +116,7 @@ public class BoundingBox {
* @param z the Z offset * @param z the Z offset
* @return a new bounding box contracted * @return a new bounding box contracted
*/ */
@NotNull
public BoundingBox contract(float x, float y, float z) { public BoundingBox contract(float x, float y, float z) {
return new BoundingBox(entity, this.x - x, this.y - y, this.z - z); return new BoundingBox(entity, this.x - x, this.y - y, this.z - z);
} }
@ -204,6 +207,7 @@ public class BoundingBox {
* *
* @return the points at the bottom of the {@link BoundingBox} * @return the points at the bottom of the {@link BoundingBox}
*/ */
@NotNull
public Vector[] getBottomFace() { public Vector[] getBottomFace() {
return new Vector[]{ return new Vector[]{
new Vector(getMinX(), getMinY(), getMinZ()), new Vector(getMinX(), getMinY(), getMinZ()),
@ -218,6 +222,7 @@ public class BoundingBox {
* *
* @return the points at the top of the {@link BoundingBox} * @return the points at the top of the {@link BoundingBox}
*/ */
@NotNull
public Vector[] getTopFace() { public Vector[] getTopFace() {
return new Vector[]{ return new Vector[]{
new Vector(getMinX(), getMaxY(), getMinZ()), new Vector(getMinX(), getMaxY(), getMinZ()),
@ -232,6 +237,7 @@ public class BoundingBox {
* *
* @return the points on the left face of the {@link BoundingBox} * @return the points on the left face of the {@link BoundingBox}
*/ */
@NotNull
public Vector[] getLeftFace() { public Vector[] getLeftFace() {
return new Vector[]{ return new Vector[]{
new Vector(getMinX(), getMinY(), getMinZ()), new Vector(getMinX(), getMinY(), getMinZ()),
@ -246,6 +252,7 @@ public class BoundingBox {
* *
* @return the points on the right face of the {@link BoundingBox} * @return the points on the right face of the {@link BoundingBox}
*/ */
@NotNull
public Vector[] getRightFace() { public Vector[] getRightFace() {
return new Vector[]{ return new Vector[]{
new Vector(getMaxX(), getMinY(), getMinZ()), new Vector(getMaxX(), getMinY(), getMinZ()),
@ -260,6 +267,7 @@ public class BoundingBox {
* *
* @return the points at the front of the {@link BoundingBox} * @return the points at the front of the {@link BoundingBox}
*/ */
@NotNull
public Vector[] getFrontFace() { public Vector[] getFrontFace() {
return new Vector[]{ return new Vector[]{
new Vector(getMinX(), getMinY(), getMinZ()), new Vector(getMinX(), getMinY(), getMinZ()),
@ -274,6 +282,7 @@ public class BoundingBox {
* *
* @return the points at the back of the {@link BoundingBox} * @return the points at the back of the {@link BoundingBox}
*/ */
@NotNull
public Vector[] getBackFace() { public Vector[] getBackFace() {
return new Vector[]{ return new Vector[]{
new Vector(getMinX(), getMinY(), getMaxZ()), new Vector(getMinX(), getMinY(), getMaxZ()),

View File

@ -2,6 +2,7 @@ package net.minestom.server.entity.ai;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityCreature; import net.minestom.server.entity.EntityCreature;
import org.jetbrains.annotations.Nullable;
public abstract class GoalSelector { public abstract class GoalSelector {
@ -19,7 +20,7 @@ public abstract class GoalSelector {
public abstract boolean shouldStart(); public abstract boolean shouldStart();
/** /**
* Start this {@link GoalSelector} * Starts this {@link GoalSelector}.
*/ */
public abstract void start(); public abstract void start();
@ -38,15 +39,16 @@ public abstract class GoalSelector {
public abstract boolean shouldEnd(); public abstract boolean shouldEnd();
/** /**
* End this {@link GoalSelector} * Ends this {@link GoalSelector}.
*/ */
public abstract void end(); public abstract void end();
/** /**
* Find a target based on the entity {@link TargetSelector} * Finds a target based on the entity {@link TargetSelector}.
* *
* @return the target entity, null if not found * @return the target entity, null if not found
*/ */
@Nullable
public Entity findTarget() { public Entity findTarget() {
for (TargetSelector targetSelector : entityCreature.getTargetSelectors()) { for (TargetSelector targetSelector : entityCreature.getTargetSelectors()) {
final Entity entity = targetSelector.findTarget(); final Entity entity = targetSelector.findTarget();

View File

@ -2,6 +2,8 @@ package net.minestom.server.entity.ai;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityCreature; import net.minestom.server.entity.EntityCreature;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* The target selector is called each time the entity receives an "attack" instruction * The target selector is called each time the entity receives an "attack" instruction
@ -11,7 +13,7 @@ public abstract class TargetSelector {
protected final EntityCreature entityCreature; protected final EntityCreature entityCreature;
public TargetSelector(EntityCreature entityCreature) { public TargetSelector(@NotNull EntityCreature entityCreature) {
this.entityCreature = entityCreature; this.entityCreature = entityCreature;
} }
@ -23,6 +25,7 @@ public abstract class TargetSelector {
* *
* @return the target, null if not any * @return the target, null if not any
*/ */
@Nullable
public abstract Entity findTarget(); public abstract Entity findTarget();
/** /**
@ -30,6 +33,7 @@ public abstract class TargetSelector {
* *
* @return the entity * @return the entity
*/ */
@NotNull
public EntityCreature getEntityCreature() { public EntityCreature getEntityCreature() {
return entityCreature; return entityCreature;
} }

View File

@ -4,10 +4,11 @@ import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityCreature; import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.ai.GoalSelector; import net.minestom.server.entity.ai.GoalSelector;
import net.minestom.server.utils.Position; import net.minestom.server.utils.Position;
import org.jetbrains.annotations.NotNull;
public class FollowTargetGoal extends GoalSelector { public class FollowTargetGoal extends GoalSelector {
public FollowTargetGoal(EntityCreature entityCreature) { public FollowTargetGoal(@NotNull EntityCreature entityCreature) {
super(entityCreature); super(entityCreature);
} }

View File

@ -7,6 +7,9 @@ import net.minestom.server.entity.ai.TargetSelector;
import net.minestom.server.utils.Position; import net.minestom.server.utils.Position;
import net.minestom.server.utils.time.CooldownUtils; import net.minestom.server.utils.time.CooldownUtils;
import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* Attacks the entity's target ({@link EntityCreature#getTarget()}) OR the closest entity * Attacks the entity's target ({@link EntityCreature#getTarget()}) OR the closest entity
@ -25,7 +28,7 @@ public class MeleeAttackGoal extends GoalSelector {
* @param delay the delay between each attacks * @param delay the delay between each attacks
* @param timeUnit the unit of the delay * @param timeUnit the unit of the delay
*/ */
public MeleeAttackGoal(EntityCreature entityCreature, int delay, TimeUnit timeUnit) { public MeleeAttackGoal(@NotNull EntityCreature entityCreature, int delay, @NotNull TimeUnit timeUnit) {
super(entityCreature); super(entityCreature);
this.delay = delay; this.delay = delay;
this.timeUnit = timeUnit; this.timeUnit = timeUnit;
@ -38,7 +41,9 @@ public class MeleeAttackGoal extends GoalSelector {
@Override @Override
public void start() { public void start() {
final Position targetPosition = getTarget().getPosition(); final Entity target = getTarget();
Check.notNull(target, "The target is not expected to be null!");
final Position targetPosition = target.getPosition();
entityCreature.setPathTo(targetPosition); entityCreature.setPathTo(targetPosition);
} }
@ -85,6 +90,7 @@ public class MeleeAttackGoal extends GoalSelector {
* *
* @return the target of the entity * @return the target of the entity
*/ */
@Nullable
private Entity getTarget() { private Entity getTarget() {
final Entity target = entityCreature.getTarget(); final Entity target = entityCreature.getTarget();
return target == null ? findTarget() : target; return target == null ? findTarget() : target;

View File

@ -3,6 +3,7 @@ package net.minestom.server.entity.ai.goal;
import net.minestom.server.entity.EntityCreature; import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.ai.GoalSelector; import net.minestom.server.entity.ai.GoalSelector;
import net.minestom.server.utils.Position; import net.minestom.server.utils.Position;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -17,7 +18,7 @@ public class RandomStrollGoal extends GoalSelector {
private long lastStroll; private long lastStroll;
public RandomStrollGoal(EntityCreature entityCreature, int radius) { public RandomStrollGoal(@NotNull EntityCreature entityCreature, int radius) {
super(entityCreature); super(entityCreature);
this.radius = radius; this.radius = radius;
@ -62,6 +63,7 @@ public class RandomStrollGoal extends GoalSelector {
return radius; return radius;
} }
@NotNull
private List<Position> getNearbyBlocks(int radius) { private List<Position> getNearbyBlocks(int radius) {
List<Position> blocks = new ArrayList<>(); List<Position> blocks = new ArrayList<>();
for (int x = -radius; x <= radius; x++) { for (int x = -radius; x <= radius; x++) {

View File

@ -5,6 +5,7 @@ import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.ai.TargetSelector; import net.minestom.server.entity.ai.TargetSelector;
import net.minestom.server.entity.damage.DamageType; import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.entity.damage.EntityDamage; import net.minestom.server.entity.damage.EntityDamage;
import org.jetbrains.annotations.NotNull;
/** /**
* Targets the last damager of this entity. * Targets the last damager of this entity.
@ -13,7 +14,7 @@ public class LastEntityDamagerTarget extends TargetSelector {
private final float range; private final float range;
public LastEntityDamagerTarget(EntityCreature entityCreature, float range) { public LastEntityDamagerTarget(@NotNull EntityCreature entityCreature, float range) {
super(entityCreature); super(entityCreature);
this.range = range; this.range = range;
} }

View File

@ -73,14 +73,14 @@ public class InstanceContainer extends Instance {
private ChunkSupplier chunkSupplier; private ChunkSupplier chunkSupplier;
/** /**
* Create an {@link InstanceContainer} * Creates an {@link InstanceContainer}.
* *
* @param uniqueId the unique id of the instance * @param uniqueId the unique id of the instance
* @param dimensionType the dimension type of the instance * @param dimensionType the dimension type of the instance
* @param storageLocation the {@link StorageLocation} of the instance, * @param storageLocation the {@link StorageLocation} of the instance,
* can be null if you do not wish to save the instance later on * can be null if you do not wish to save the instance later on
*/ */
public InstanceContainer(UUID uniqueId, DimensionType dimensionType, StorageLocation storageLocation) { public InstanceContainer(@NotNull UUID uniqueId, @NotNull DimensionType dimensionType, @Nullable StorageLocation storageLocation) {
super(uniqueId, dimensionType); super(uniqueId, dimensionType);
this.storageLocation = storageLocation; this.storageLocation = storageLocation;
@ -102,7 +102,7 @@ public class InstanceContainer extends Instance {
} }
@Override @Override
public void setBlockStateId(int x, int y, int z, short blockStateId, Data data) { public void setBlockStateId(int x, int y, int z, short blockStateId, @Nullable Data data) {
setBlock(x, y, z, blockStateId, null, data); setBlock(x, y, z, blockStateId, null, data);
} }
@ -114,7 +114,7 @@ public class InstanceContainer extends Instance {
} }
@Override @Override
public void setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId, Data data) { public void setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId, @Nullable Data data) {
final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId); final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
setBlock(x, y, z, blockStateId, customBlock, data); setBlock(x, y, z, blockStateId, customBlock, data);
} }
@ -132,7 +132,8 @@ public class InstanceContainer extends Instance {
* @param customBlock the {@link CustomBlock}, null if none * @param customBlock the {@link CustomBlock}, null if none
* @param data the {@link Data}, null if none * @param data the {@link Data}, null if none
*/ */
private synchronized void setBlock(int x, int y, int z, short blockStateId, CustomBlock customBlock, Data data) { private synchronized void setBlock(int x, int y, int z, short blockStateId,
@Nullable CustomBlock customBlock, @Nullable Data data) {
final Chunk chunk = getChunkAt(x, z); final Chunk chunk = getChunkAt(x, z);
if (ChunkUtils.isLoaded(chunk)) { if (ChunkUtils.isLoaded(chunk)) {
UNSAFE_setBlock(chunk, x, y, z, blockStateId, customBlock, data); UNSAFE_setBlock(chunk, x, y, z, blockStateId, customBlock, data);
@ -160,6 +161,8 @@ public class InstanceContainer extends Instance {
*/ */
private void UNSAFE_setBlock(@NotNull Chunk chunk, int x, int y, int z, short blockStateId, private void UNSAFE_setBlock(@NotNull Chunk chunk, int x, int y, int z, short blockStateId,
@Nullable CustomBlock customBlock, @Nullable Data data) { @Nullable CustomBlock customBlock, @Nullable Data data) {
// Cannot place block in a read-only chunk
if (chunk.isReadOnly()) { if (chunk.isReadOnly()) {
return; return;
} }

View File

@ -2,8 +2,20 @@ package net.minestom.server.listener.manager;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.client.ClientPlayPacket; import net.minestom.server.network.packet.client.ClientPlayPacket;
import org.jetbrains.annotations.NotNull;
/**
* Interface used to add a listener for incoming packets with {@link net.minestom.server.network.ConnectionManager#onPacketReceive(PacketConsumer)}.
*/
@FunctionalInterface @FunctionalInterface
public interface PacketConsumer { public interface PacketConsumer {
void accept(Player player, PacketController packetController, ClientPlayPacket packet);
/**
* Called when a packet is received from the client.
*
* @param player the player who sent the packet
* @param packetController the packet controller, used to cancel or control which listener will be called
* @param packet the received packet
*/
void accept(@NotNull Player player, @NotNull PacketController packetController, @NotNull ClientPlayPacket packet);
} }

View File

@ -1,11 +1,18 @@
package net.minestom.server.listener.manager; package net.minestom.server.listener.manager;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.client.ClientPlayPacket;
import org.jetbrains.annotations.Nullable;
/**
* Used to control the output of a packet in {@link PacketConsumer#accept(Player, PacketController, ClientPlayPacket)}.
*/
public class PacketController { public class PacketController {
private boolean cancel; private boolean cancel;
private PacketListenerConsumer packetListenerConsumer; private PacketListenerConsumer packetListenerConsumer;
protected PacketController(PacketListenerConsumer packetListenerConsumer) { protected PacketController(@Nullable PacketListenerConsumer packetListenerConsumer) {
this.packetListenerConsumer = packetListenerConsumer; this.packetListenerConsumer = packetListenerConsumer;
} }
@ -30,8 +37,9 @@ public class PacketController {
/** /**
* Gets the listener associated with the packet. * Gets the listener associated with the packet.
* *
* @return the packet's listener * @return the packet's listener, null if not present
*/ */
@Nullable
public PacketListenerConsumer getPacketListenerConsumer() { public PacketListenerConsumer getPacketListenerConsumer() {
return packetListenerConsumer; return packetListenerConsumer;
} }
@ -41,9 +49,9 @@ public class PacketController {
* <p> * <p>
* WARNING: this will overwrite the default minestom listener. * WARNING: this will overwrite the default minestom listener.
* *
* @param packetListenerConsumer the new packet listener * @param packetListenerConsumer the new packet listener, can be null
*/ */
public void setPacketListenerConsumer(PacketListenerConsumer packetListenerConsumer) { public void setPacketListenerConsumer(@Nullable PacketListenerConsumer packetListenerConsumer) {
this.packetListenerConsumer = packetListenerConsumer; this.packetListenerConsumer = packetListenerConsumer;
} }
} }

View File

@ -3,6 +3,11 @@ package net.minestom.server.listener.manager;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.client.ClientPlayPacket; import net.minestom.server.network.packet.client.ClientPlayPacket;
/**
* Small convenient interface to use method references with {@link PacketListenerManager#setListener(Class, PacketListenerConsumer)}.
*
* @param <T> the packet type
*/
@FunctionalInterface @FunctionalInterface
public interface PacketListenerConsumer<T extends ClientPlayPacket> { public interface PacketListenerConsumer<T extends ClientPlayPacket> {
void accept(T packet, Player player); void accept(T packet, Player player);

View File

@ -6,11 +6,12 @@ import net.minestom.server.listener.*;
import net.minestom.server.network.ConnectionManager; import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.packet.client.ClientPlayPacket; import net.minestom.server.network.packet.client.ClientPlayPacket;
import net.minestom.server.network.packet.client.play.*; import net.minestom.server.network.packet.client.play.*;
import org.jetbrains.annotations.NotNull;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class PacketListenerManager { public final class PacketListenerManager {
private static final ConnectionManager CONNECTION_MANAGER = MinecraftServer.getConnectionManager(); private static final ConnectionManager CONNECTION_MANAGER = MinecraftServer.getConnectionManager();
@ -48,7 +49,14 @@ public class PacketListenerManager {
setListener(ClientAdvancementTabPacket.class, AdvancementTabListener::listener); setListener(ClientAdvancementTabPacket.class, AdvancementTabListener::listener);
} }
public <T extends ClientPlayPacket> void process(T packet, Player player) { /**
* Processes a packet by getting its {@link PacketListenerConsumer} and calling all the packet listeners.
*
* @param packet the received packet
* @param player the player who sent the packet
* @param <T> the packet type
*/
public <T extends ClientPlayPacket> void process(@NotNull T packet, @NotNull Player player) {
final Class clazz = packet.getClass(); final Class clazz = packet.getClass();
@ -56,7 +64,7 @@ public class PacketListenerManager {
// Listener can be null if none has been set before, call PacketConsumer anyway // Listener can be null if none has been set before, call PacketConsumer anyway
if (packetListenerConsumer == null) { if (packetListenerConsumer == null) {
System.err.println("Packet " + clazz + " does not have any default listener!"); System.err.println("Packet " + clazz + " does not have any default listener! (The issue comes from Minestom)");
} }
@ -79,15 +87,15 @@ public class PacketListenerManager {
} }
/** /**
* Set the listener of a packet * Sets the listener of a packet.
* <p> * <p>
* WARNING: this will overwrite the default minestom listener, this is not reversible * WARNING: this will overwrite the default minestom listener, this is not reversible.
* *
* @param packetClass the class of the packet * @param packetClass the class of the packet
* @param consumer the new packet's listener * @param consumer the new packet's listener
* @param <T> the type of the packet * @param <T> the type of the packet
*/ */
public <T extends ClientPlayPacket> void setListener(Class<T> packetClass, PacketListenerConsumer<T> consumer) { public <T extends ClientPlayPacket> void setListener(@NotNull Class<T> packetClass, @NotNull PacketListenerConsumer<T> consumer) {
this.listeners.put(packetClass, consumer); this.listeners.put(packetClass, consumer);
} }

View File

@ -24,7 +24,7 @@ public final class PacketWriterUtils {
private static final ExecutorService PACKET_WRITER_POOL = new MinestomThread(MinecraftServer.THREAD_COUNT_PACKET_WRITER, MinecraftServer.THREAD_NAME_PACKET_WRITER); private static final ExecutorService PACKET_WRITER_POOL = new MinestomThread(MinecraftServer.THREAD_COUNT_PACKET_WRITER, MinecraftServer.THREAD_NAME_PACKET_WRITER);
/** /**
* Write the {@link ServerPacket} in the writer thread pool. * Writes the {@link ServerPacket} in the writer thread pool.
* <p> * <p>
* WARNING: should not be used if the packet receive order is important * WARNING: should not be used if the packet receive order is important
* *
@ -39,7 +39,7 @@ public final class PacketWriterUtils {
} }
/** /**
* Write a {@link ServerPacket} in the writer thread pool and send it to every players in {@code players}. * Writes a {@link ServerPacket} in the writer thread pool and send it to every players in {@code players}.
* <p> * <p>
* WARNING: should not be used if the packet receive order is important * WARNING: should not be used if the packet receive order is important
* *
@ -65,7 +65,7 @@ public final class PacketWriterUtils {
} }
/** /**
* Write a {@link ServerPacket} and send it to a {@link PlayerConnection}. * Writes a {@link ServerPacket} and send it to a {@link PlayerConnection}.
* <p> * <p>
* WARNING: should not be used if the packet receive order is important * WARNING: should not be used if the packet receive order is important
* *
@ -86,7 +86,7 @@ public final class PacketWriterUtils {
} }
/** /**
* Write a {@link ServerPacket} and send it to a {@link Player}. * Writes a {@link ServerPacket} and send it to a {@link Player}.
* <p> * <p>
* WARNING: should not be used if the packet receive order is important * WARNING: should not be used if the packet receive order is important
* *

View File

@ -3,12 +3,20 @@ package net.minestom.server.network.packet.client;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.listener.manager.PacketListenerManager; import net.minestom.server.listener.manager.PacketListenerManager;
import org.jetbrains.annotations.NotNull;
public abstract class ClientPlayPacket implements ClientPacket { public abstract class ClientPlayPacket implements ClientPacket {
private static final PacketListenerManager PACKET_LISTENER_MANAGER = MinecraftServer.getPacketListenerManager(); private static final PacketListenerManager PACKET_LISTENER_MANAGER = MinecraftServer.getPacketListenerManager();
public void process(Player player) { /**
* Processes the packet for {@code player}.
* <p>
* Called during the player tick and forwarded to the {@link PacketListenerManager}.
*
* @param player the player who sent the packet
*/
public void process(@NotNull Player player) {
PACKET_LISTENER_MANAGER.process(this, player); PACKET_LISTENER_MANAGER.process(this, player);
} }

View File

@ -4,14 +4,14 @@ import java.util.UUID;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
* An utilities class for {@link UUID} * An utilities class for {@link UUID}.
*/ */
public final class UniqueIdUtils { public final class UniqueIdUtils {
public static final Pattern UNIQUE_ID_PATTERN = Pattern.compile("\\b[0-9a-f]{8}\\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\\b[0-9a-f]{12}\\b"); public static final Pattern UNIQUE_ID_PATTERN = Pattern.compile("\\b[0-9a-f]{8}\\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\\b[0-9a-f]{12}\\b");
/** /**
* Checks whether the {@code input} string is an {@link UUID} * Checks whether the {@code input} string is an {@link UUID}.
* *
* @param input The input string to be checked * @param input The input string to be checked
* @return {@code true} if the input an unique identifier, otherwise {@code false} * @return {@code true} if the input an unique identifier, otherwise {@code false}

View File

@ -9,7 +9,7 @@ import java.util.List;
import java.util.Random; import java.util.Random;
/** /**
* Produces a random element from a given set, with weights applied * Produces a random element from a given set, with weights applied.
* *
* @param <E> * @param <E>
*/ */
@ -33,7 +33,7 @@ public class WeightedRandom<E extends WeightedRandomItem> {
} }
/** /**
* Gets a random element from this set * Gets a random element from this set.
* *
* @param rng Random Number Generator to generate random numbers with * @param rng Random Number Generator to generate random numbers with
* @return a random element from this set * @return a random element from this set

View File

@ -1,5 +1,10 @@
package net.minestom.server.world; package net.minestom.server.world;
/**
* Those are all the difficulties which can be displayed in the player menu.
* <p>
* Sets with {@link net.minestom.server.MinecraftServer#setDifficulty(Difficulty)}.
*/
public enum Difficulty { public enum Difficulty {
PEACEFUL((byte) 0), EASY((byte) 1), NORMAL((byte) 2), HARD((byte) 3); PEACEFUL((byte) 0), EASY((byte) 1), NORMAL((byte) 2), HARD((byte) 3);