mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-28 20:18:10 +01:00
Use the Validator interface when possible for readability
This commit is contained in:
parent
b2097a338d
commit
1d30e6e953
@ -12,7 +12,7 @@ public class ArgumentBoolean extends Argument<Boolean> {
|
|||||||
public static final int NOT_BOOLEAN_ERROR = 1;
|
public static final int NOT_BOOLEAN_ERROR = 1;
|
||||||
|
|
||||||
public ArgumentBoolean(String id) {
|
public ArgumentBoolean(String id) {
|
||||||
super(id, false);
|
super(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -16,7 +16,7 @@ public abstract class ArgumentNumber<T extends Number> extends Argument<T> {
|
|||||||
protected T min, max;
|
protected T min, max;
|
||||||
|
|
||||||
public ArgumentNumber(String id) {
|
public ArgumentNumber(String id) {
|
||||||
super(id, false);
|
super(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@ -7,6 +7,7 @@ import net.minestom.server.listener.manager.PacketConsumer;
|
|||||||
import net.minestom.server.network.packet.client.login.LoginStartPacket;
|
import net.minestom.server.network.packet.client.login.LoginStartPacket;
|
||||||
import net.minestom.server.network.packet.server.play.ChatMessagePacket;
|
import net.minestom.server.network.packet.server.play.ChatMessagePacket;
|
||||||
import net.minestom.server.network.player.PlayerConnection;
|
import net.minestom.server.network.player.PlayerConnection;
|
||||||
|
import net.minestom.server.utils.callback.validator.PlayerValidator;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@ -14,7 +15,6 @@ import java.util.*;
|
|||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages the connected clients.
|
* Manages the connected clients.
|
||||||
@ -93,7 +93,7 @@ public final class ConnectionManager {
|
|||||||
* @param jsonMessage the message to send, probably a {@link net.minestom.server.chat.ColoredText} or {@link net.minestom.server.chat.RichMessage}
|
* @param jsonMessage the message to send, probably a {@link net.minestom.server.chat.ColoredText} or {@link net.minestom.server.chat.RichMessage}
|
||||||
* @param condition the condition to receive the message
|
* @param condition the condition to receive the message
|
||||||
*/
|
*/
|
||||||
public void broadcastMessage(@NotNull JsonMessage jsonMessage, @Nullable Function<Player, Boolean> condition) {
|
public void broadcastMessage(@NotNull JsonMessage jsonMessage, @Nullable PlayerValidator condition) {
|
||||||
final Collection<Player> recipients = getRecipients(condition);
|
final Collection<Player> recipients = getRecipients(condition);
|
||||||
|
|
||||||
if (!recipients.isEmpty()) {
|
if (!recipients.isEmpty()) {
|
||||||
@ -117,7 +117,7 @@ public final class ConnectionManager {
|
|||||||
PacketWriterUtils.writeAndSend(recipients, chatMessagePacket);
|
PacketWriterUtils.writeAndSend(recipients, chatMessagePacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Collection<Player> getRecipients(@Nullable Function<Player, Boolean> condition) {
|
private Collection<Player> getRecipients(@Nullable PlayerValidator condition) {
|
||||||
Collection<Player> recipients;
|
Collection<Player> recipients;
|
||||||
|
|
||||||
// Get the recipients
|
// Get the recipients
|
||||||
@ -126,7 +126,7 @@ public final class ConnectionManager {
|
|||||||
} else {
|
} else {
|
||||||
recipients = new ArrayList<>();
|
recipients = new ArrayList<>();
|
||||||
getOnlinePlayers().forEach(player -> {
|
getOnlinePlayers().forEach(player -> {
|
||||||
final boolean result = condition.apply(player);
|
final boolean result = condition.isValid(player);
|
||||||
if (result)
|
if (result)
|
||||||
recipients.add(player);
|
recipients.add(player);
|
||||||
});
|
});
|
||||||
|
@ -6,6 +6,7 @@ import net.minestom.server.instance.Chunk;
|
|||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.instance.InstanceContainer;
|
import net.minestom.server.instance.InstanceContainer;
|
||||||
import net.minestom.server.instance.SharedInstance;
|
import net.minestom.server.instance.SharedInstance;
|
||||||
|
import net.minestom.server.utils.callback.validator.EntityValidator;
|
||||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||||
import net.minestom.server.utils.thread.MinestomThread;
|
import net.minestom.server.utils.thread.MinestomThread;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@ -16,7 +17,6 @@ import java.util.Set;
|
|||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to link chunks into multiple groups.
|
* Used to link chunks into multiple groups.
|
||||||
@ -216,12 +216,12 @@ public abstract class ThreadProvider {
|
|||||||
* @param condition the condition which confirm if the update happens or not
|
* @param condition the condition which confirm if the update happens or not
|
||||||
*/
|
*/
|
||||||
protected void conditionalEntityUpdate(@NotNull Instance instance, @NotNull Chunk chunk, long time,
|
protected void conditionalEntityUpdate(@NotNull Instance instance, @NotNull Chunk chunk, long time,
|
||||||
@Nullable Function<Entity, Boolean> condition) {
|
@Nullable EntityValidator condition) {
|
||||||
final Set<Entity> entities = instance.getChunkEntities(chunk);
|
final Set<Entity> entities = instance.getChunkEntities(chunk);
|
||||||
|
|
||||||
if (!entities.isEmpty()) {
|
if (!entities.isEmpty()) {
|
||||||
for (Entity entity : entities) {
|
for (Entity entity : entities) {
|
||||||
if (condition != null && !condition.apply(entity))
|
if (condition != null && !condition.isValid(entity))
|
||||||
continue;
|
continue;
|
||||||
entity.tick(time);
|
entity.tick(time);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
package net.minestom.server.utils.callback.validator;
|
||||||
|
|
||||||
|
import net.minestom.server.entity.Entity;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface EntityValidator extends Validator<Entity> {
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package net.minestom.server.utils.callback.validator;
|
||||||
|
|
||||||
|
import net.minestom.server.entity.Player;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface PlayerValidator extends Validator<Player> {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user