Add tag api to CommandSender

This commit is contained in:
TheMode 2021-06-22 13:30:47 +02:00
parent 43c2b48b68
commit 0f02be4b2b
3 changed files with 32 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import net.kyori.adventure.text.Component;
import net.minestom.server.chat.JsonMessage; import net.minestom.server.chat.JsonMessage;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.permission.PermissionHandler; import net.minestom.server.permission.PermissionHandler;
import net.minestom.server.tag.TagHandler;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -12,7 +13,7 @@ import org.jetbrains.annotations.NotNull;
* <p> * <p>
* Main implementations are {@link Player} and {@link ConsoleSender}. * Main implementations are {@link Player} and {@link ConsoleSender}.
*/ */
public interface CommandSender extends PermissionHandler, Audience { public interface CommandSender extends PermissionHandler, Audience, TagHandler {
/** /**
* Sends a raw string message. * Sends a raw string message.
@ -28,7 +29,7 @@ public interface CommandSender extends PermissionHandler, Audience {
* *
* @param messages the messages to send * @param messages the messages to send
*/ */
default void sendMessage(@NotNull String @NotNull[] messages) { default void sendMessage(@NotNull String @NotNull [] messages) {
for (String message : messages) { for (String message : messages) {
sendMessage(message); sendMessage(message);
} }
@ -39,9 +40,8 @@ public interface CommandSender extends PermissionHandler, Audience {
* If this is not a {@link Player}, only the content of the message will be sent as a string. * If this is not a {@link Player}, only the content of the message will be sent as a string.
* *
* @param text The {@link JsonMessage} to send. * @param text The {@link JsonMessage} to send.
*
* @deprecated Use {@link #sendMessage(Component)} * @deprecated Use {@link #sendMessage(Component)}
* */ */
@Deprecated @Deprecated
default void sendMessage(@NotNull JsonMessage text) { default void sendMessage(@NotNull JsonMessage text) {
this.sendMessage(text.asComponent()); this.sendMessage(text.asComponent());

View File

@ -5,7 +5,10 @@ import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.minestom.server.permission.Permission; import net.minestom.server.permission.Permission;
import net.minestom.server.tag.Tag;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -20,6 +23,7 @@ public class ConsoleSender implements CommandSender {
private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleSender.class); private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleSender.class);
private final Set<Permission> permissions = new CopyOnWriteArraySet<>(); private final Set<Permission> permissions = new CopyOnWriteArraySet<>();
private final NBTCompound nbtCompound = new NBTCompound();
@Override @Override
public void sendMessage(@NotNull String message) { public void sendMessage(@NotNull String message) {
@ -47,4 +51,14 @@ public class ConsoleSender implements CommandSender {
public ConsoleSender asConsole() { public ConsoleSender asConsole() {
return this; return this;
} }
@Override
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
return tag.read(nbtCompound);
}
@Override
public <T> void setTag(@NotNull Tag<T> tag, @Nullable T value) {
tag.write(nbtCompound, value);
}
} }

View File

@ -3,7 +3,10 @@ package net.minestom.server.command;
import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.CommandContext;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.minestom.server.permission.Permission; import net.minestom.server.permission.Permission;
import net.minestom.server.tag.Tag;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
@ -19,10 +22,21 @@ import java.util.Set;
public class ServerSender implements CommandSender { public class ServerSender implements CommandSender {
private final Set<Permission> permissions = Collections.unmodifiableSet(new HashSet<>()); private final Set<Permission> permissions = Collections.unmodifiableSet(new HashSet<>());
private final NBTCompound nbtCompound = new NBTCompound();
@NotNull @NotNull
@Override @Override
public Set<Permission> getAllPermissions() { public Set<Permission> getAllPermissions() {
return permissions; return permissions;
} }
@Override
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
return tag.read(nbtCompound);
}
@Override
public <T> void setTag(@NotNull Tag<T> tag, @Nullable T value) {
tag.write(nbtCompound, value);
}
} }