mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-10 13:49:04 +01:00
Deprecate unsafe casting methods
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
0f2ad33e52
commit
3fa7a0ba4b
@ -36,18 +36,24 @@ public interface CommandSender extends PermissionHandler, Audience, TagHandler {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets if the sender is a {@link Player}.
|
* Gets if the sender is a {@link Player}.
|
||||||
|
* <p>
|
||||||
|
* Consider using {@code instanceof} instead.
|
||||||
*
|
*
|
||||||
* @return true if 'this' is a player, false otherwise
|
* @return true if 'this' is a player, false otherwise
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
default boolean isPlayer() {
|
default boolean isPlayer() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets if the sender is a {@link ConsoleSender}.
|
* Gets if the sender is a {@link ConsoleSender}.
|
||||||
|
* <p>
|
||||||
|
* Consider using {@code instanceof} instead.
|
||||||
*
|
*
|
||||||
* @return true if 'this' is the console, false otherwise
|
* @return true if 'this' is the console, false otherwise
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
default boolean isConsole() {
|
default boolean isConsole() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -59,6 +65,7 @@ public interface CommandSender extends PermissionHandler, Audience, TagHandler {
|
|||||||
* @throws ClassCastException if 'this' is not a player
|
* @throws ClassCastException if 'this' is not a player
|
||||||
* @see #isPlayer()
|
* @see #isPlayer()
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
default Player asPlayer() {
|
default Player asPlayer() {
|
||||||
throw new ClassCastException("CommandSender is not a Player");
|
throw new ClassCastException("CommandSender is not a Player");
|
||||||
}
|
}
|
||||||
@ -70,6 +77,7 @@ public interface CommandSender extends PermissionHandler, Audience, TagHandler {
|
|||||||
* @throws ClassCastException if 'this' is not a console sender
|
* @throws ClassCastException if 'this' is not a console sender
|
||||||
* @see #isConsole()
|
* @see #isConsole()
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
default ConsoleSender asConsole() {
|
default ConsoleSender asConsole() {
|
||||||
throw new ClassCastException("CommandSender is not the ConsoleSender");
|
throw new ClassCastException("CommandSender is not the ConsoleSender");
|
||||||
}
|
}
|
||||||
|
@ -3,20 +3,23 @@ package net.minestom.server.command.builder.condition;
|
|||||||
|
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.minestom.server.command.CommandSender;
|
import net.minestom.server.command.CommandSender;
|
||||||
|
import net.minestom.server.command.ConsoleSender;
|
||||||
|
import net.minestom.server.entity.Player;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Common command conditions
|
* Common command conditions
|
||||||
*/
|
*/
|
||||||
public class Conditions {
|
public class Conditions {
|
||||||
public static boolean playerOnly(CommandSender sender, String commandString) {
|
public static boolean playerOnly(CommandSender sender, String commandString) {
|
||||||
if (!sender.isPlayer()) {
|
if (!(sender instanceof Player)) {
|
||||||
sender.sendMessage(Component.text("The command is only available for players"));
|
sender.sendMessage(Component.text("The command is only available for players"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean consoleOnly(CommandSender sender, String commandString) {
|
public static boolean consoleOnly(CommandSender sender, String commandString) {
|
||||||
if (!sender.isConsole()) {
|
if (!(sender instanceof ConsoleSender)) {
|
||||||
sender.sendMessage(Component.text("The command is only available form the console"));
|
sender.sendMessage(Component.text("The command is only available form the console"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -242,12 +242,8 @@ public class EntityFinder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull List<@NotNull Entity> find(@NotNull CommandSender sender) {
|
public @NotNull List<@NotNull Entity> find(@NotNull CommandSender sender) {
|
||||||
if (sender.isPlayer()) {
|
return sender instanceof Player player ?
|
||||||
Player player = sender.asPlayer();
|
find(player.getInstance(), player) : find(null, null);
|
||||||
return find(player.getInstance(), player);
|
|
||||||
} else {
|
|
||||||
return find(null, null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -260,37 +256,27 @@ public class EntityFinder {
|
|||||||
public @Nullable Player findFirstPlayer(@Nullable Instance instance, @Nullable Entity self) {
|
public @Nullable Player findFirstPlayer(@Nullable Instance instance, @Nullable Entity self) {
|
||||||
final List<Entity> entities = find(instance, self);
|
final List<Entity> entities = find(instance, self);
|
||||||
for (Entity entity : entities) {
|
for (Entity entity : entities) {
|
||||||
if (entity instanceof Player) {
|
if (entity instanceof Player player) {
|
||||||
return (Player) entity;
|
return player;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable Player findFirstPlayer(@NotNull CommandSender sender) {
|
public @Nullable Player findFirstPlayer(@NotNull CommandSender sender) {
|
||||||
if (sender.isPlayer()) {
|
return sender instanceof Player player ?
|
||||||
final Player player = sender.asPlayer();
|
findFirstPlayer(player.getInstance(), player) :
|
||||||
return findFirstPlayer(player.getInstance(), player);
|
findFirstPlayer(null, null);
|
||||||
} else {
|
|
||||||
return findFirstPlayer(null, null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable Entity findFirstEntity(@Nullable Instance instance, @Nullable Entity self) {
|
public @Nullable Entity findFirstEntity(@Nullable Instance instance, @Nullable Entity self) {
|
||||||
final List<Entity> entities = find(instance, self);
|
final List<Entity> entities = find(instance, self);
|
||||||
for (Entity entity : entities) {
|
return entities.isEmpty() ? null : entities.get(0);
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable Entity findFirstEntity(@NotNull CommandSender sender) {
|
public @Nullable Entity findFirstEntity(@NotNull CommandSender sender) {
|
||||||
if (sender.isPlayer()) {
|
return sender instanceof Player player ?
|
||||||
final Player player = sender.asPlayer();
|
findFirstEntity(player.getInstance(), player) : findFirstEntity(null, null);
|
||||||
return findFirstEntity(player.getInstance(), player);
|
|
||||||
} else {
|
|
||||||
return findFirstEntity(null, null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum TargetSelector {
|
public enum TargetSelector {
|
||||||
|
@ -19,7 +19,7 @@ public class BookCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void execute(CommandSender sender, CommandContext context) {
|
private void execute(CommandSender sender, CommandContext context) {
|
||||||
Player player = sender.asPlayer();
|
Player player = (Player) sender;
|
||||||
|
|
||||||
player.openBook(Book.builder()
|
player.openBook(Book.builder()
|
||||||
.author(Component.text(player.getUsername()))
|
.author(Component.text(player.getUsername()))
|
||||||
|
@ -15,7 +15,7 @@ public class DimensionCommand extends Command {
|
|||||||
setCondition(Conditions::playerOnly);
|
setCondition(Conditions::playerOnly);
|
||||||
|
|
||||||
addSyntax((sender, context) -> {
|
addSyntax((sender, context) -> {
|
||||||
final Player player = sender.asPlayer();
|
final Player player = (Player) sender;
|
||||||
final Instance instance = player.getInstance();
|
final Instance instance = player.getInstance();
|
||||||
final var instances = MinecraftServer.getInstanceManager().getInstances().stream().filter(instance1 -> !instance1.equals(instance)).toList();
|
final var instances = MinecraftServer.getInstanceManager().getInstances().stream().filter(instance1 -> !instance1.equals(instance)).toList();
|
||||||
if (instances.isEmpty()) {
|
if (instances.isEmpty()) {
|
||||||
|
@ -8,7 +8,8 @@ import net.minestom.server.entity.Player;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import static net.minestom.server.command.builder.arguments.ArgumentType.*;
|
import static net.minestom.server.command.builder.arguments.ArgumentType.Float;
|
||||||
|
import static net.minestom.server.command.builder.arguments.ArgumentType.Literal;
|
||||||
|
|
||||||
public class FindCommand extends Command {
|
public class FindCommand extends Command {
|
||||||
public FindCommand() {
|
public FindCommand() {
|
||||||
@ -22,7 +23,7 @@ public class FindCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void executorEntity(CommandSender sender, CommandContext context) {
|
private void executorEntity(CommandSender sender, CommandContext context) {
|
||||||
Player player = sender.asPlayer();
|
Player player = (Player) sender;
|
||||||
float range = context.get("range");
|
float range = context.get("range");
|
||||||
|
|
||||||
Collection<Entity> entities = player.getInstance().getNearbyEntities(player.getPosition(), range);
|
Collection<Entity> entities = player.getInstance().getNearbyEntities(player.getPosition(), range);
|
||||||
|
@ -48,13 +48,13 @@ public class GamemodeCommand extends Command {
|
|||||||
//Command Syntax for /gamemode <gamemode>
|
//Command Syntax for /gamemode <gamemode>
|
||||||
addSyntax((sender, context) -> {
|
addSyntax((sender, context) -> {
|
||||||
//Limit execution to players only
|
//Limit execution to players only
|
||||||
if (!sender.isPlayer()) {
|
if (!(sender instanceof Player p)) {
|
||||||
sender.sendMessage(Component.text("Please run this command in-game.", NamedTextColor.RED));
|
sender.sendMessage(Component.text("Please run this command in-game.", NamedTextColor.RED));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check permission, this could be replaced with hasPermission
|
//Check permission, this could be replaced with hasPermission
|
||||||
if (sender.asPlayer().getPermissionLevel() < 2) {
|
if (p.getPermissionLevel() < 2) {
|
||||||
sender.sendMessage(Component.text("You don't have permission to use this command.", NamedTextColor.RED));
|
sender.sendMessage(Component.text("You don't have permission to use this command.", NamedTextColor.RED));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -62,14 +62,14 @@ public class GamemodeCommand extends Command {
|
|||||||
GameMode mode = context.get(gamemode);
|
GameMode mode = context.get(gamemode);
|
||||||
|
|
||||||
//Set the gamemode for the sender
|
//Set the gamemode for the sender
|
||||||
executeSelf(sender.asPlayer(), mode);
|
executeSelf(p, mode);
|
||||||
}, gamemode);
|
}, gamemode);
|
||||||
|
|
||||||
//Command Syntax for /gamemode <gamemode> [targets]
|
//Command Syntax for /gamemode <gamemode> [targets]
|
||||||
addSyntax((sender, context) -> {
|
addSyntax((sender, context) -> {
|
||||||
//Check permission for players only
|
//Check permission for players only
|
||||||
//This allows the console to use this syntax too
|
//This allows the console to use this syntax too
|
||||||
if (sender.isPlayer() && sender.asPlayer().getPermissionLevel() < 2) {
|
if (sender instanceof Player p && p.getPermissionLevel() < 2) {
|
||||||
sender.sendMessage(Component.text("You don't have permission to use this command.", NamedTextColor.RED));
|
sender.sendMessage(Component.text("You don't have permission to use this command.", NamedTextColor.RED));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -89,15 +89,15 @@ public class GamemodeCommand extends Command {
|
|||||||
private void executeOthers(CommandSender sender, GameMode mode, List<Entity> entities) {
|
private void executeOthers(CommandSender sender, GameMode mode, List<Entity> entities) {
|
||||||
if (entities.size() == 0) {
|
if (entities.size() == 0) {
|
||||||
//If there are no players that could be modified, display an error message
|
//If there are no players that could be modified, display an error message
|
||||||
if (sender.isPlayer()) sender.sendMessage(Component.translatable("argument.entity.notfound.player", NamedTextColor.RED), MessageType.SYSTEM);
|
if (sender instanceof Player)
|
||||||
|
sender.sendMessage(Component.translatable("argument.entity.notfound.player", NamedTextColor.RED), MessageType.SYSTEM);
|
||||||
else sender.sendMessage(Component.text("No player was found", NamedTextColor.RED), MessageType.SYSTEM);
|
else sender.sendMessage(Component.text("No player was found", NamedTextColor.RED), MessageType.SYSTEM);
|
||||||
} else for (Entity entity : entities) {
|
} else for (Entity entity : entities) {
|
||||||
if (entity instanceof Player) {
|
if (entity instanceof Player p) {
|
||||||
Player p = (Player) entity;
|
|
||||||
if (p == sender) {
|
if (p == sender) {
|
||||||
//If the player is the same as the sender, call
|
//If the player is the same as the sender, call
|
||||||
//executeSelf to display one message instead of two
|
//executeSelf to display one message instead of two
|
||||||
executeSelf(sender.asPlayer(), mode);
|
executeSelf((Player) sender, mode);
|
||||||
} else {
|
} else {
|
||||||
p.setGameMode(mode);
|
p.setGameMode(mode);
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package demo.commands;
|
package demo.commands;
|
||||||
|
|
||||||
import net.minestom.server.command.CommandSender;
|
import net.minestom.server.command.CommandSender;
|
||||||
|
import net.minestom.server.entity.Player;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@ -11,8 +12,7 @@ public class LegacyCommand extends net.minestom.server.command.builder.SimpleCom
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean process(@NotNull CommandSender sender, @NotNull String command, @NotNull String[] args) {
|
public boolean process(@NotNull CommandSender sender, @NotNull String command, @NotNull String[] args) {
|
||||||
if (!sender.isPlayer())
|
if (!(sender instanceof Player)) return false;
|
||||||
return false;
|
|
||||||
|
|
||||||
System.gc();
|
System.gc();
|
||||||
sender.sendMessage("Explicit GC");
|
sender.sendMessage("Explicit GC");
|
||||||
|
@ -16,7 +16,7 @@ public class SetBlockCommand extends Command {
|
|||||||
final ArgumentBlockState block = BlockState("block");
|
final ArgumentBlockState block = BlockState("block");
|
||||||
|
|
||||||
addSyntax((sender, context) -> {
|
addSyntax((sender, context) -> {
|
||||||
final Player player = sender.asPlayer();
|
final Player player = (Player) sender;
|
||||||
player.getInstance().setBlock(context.get(position).from(player), context.get(block));
|
player.getInstance().setBlock(context.get(position).from(player), context.get(block));
|
||||||
}, position, block);
|
}, position, block);
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,9 @@ import net.minestom.server.command.builder.arguments.Argument;
|
|||||||
import net.minestom.server.command.builder.arguments.ArgumentEnum;
|
import net.minestom.server.command.builder.arguments.ArgumentEnum;
|
||||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
import net.minestom.server.command.builder.arguments.ArgumentType;
|
||||||
import net.minestom.server.command.builder.arguments.minecraft.registry.ArgumentEntityType;
|
import net.minestom.server.command.builder.arguments.minecraft.registry.ArgumentEntityType;
|
||||||
import net.minestom.server.command.builder.arguments.relative.ArgumentRelativeVec3;
|
|
||||||
import net.minestom.server.command.builder.condition.Conditions;
|
import net.minestom.server.command.builder.condition.Conditions;
|
||||||
import net.minestom.server.coordinate.Vec;
|
import net.minestom.server.coordinate.Vec;
|
||||||
import net.minestom.server.entity.Entity;
|
import net.minestom.server.entity.*;
|
||||||
import net.minestom.server.entity.EntityCreature;
|
|
||||||
import net.minestom.server.entity.EntityType;
|
|
||||||
import net.minestom.server.entity.LivingEntity;
|
|
||||||
import net.minestom.server.utils.location.RelativeVec;
|
import net.minestom.server.utils.location.RelativeVec;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@ -35,7 +31,7 @@ public class SummonCommand extends Command {
|
|||||||
));
|
));
|
||||||
entityClass = ArgumentType.Enum("class", EntityClass.class)
|
entityClass = ArgumentType.Enum("class", EntityClass.class)
|
||||||
.setFormat(ArgumentEnum.Format.LOWER_CASED)
|
.setFormat(ArgumentEnum.Format.LOWER_CASED)
|
||||||
.setDefaultValue(EntityClass.CREATURE);;
|
.setDefaultValue(EntityClass.CREATURE);
|
||||||
addSyntax(this::execute, entity, pos, entityClass);
|
addSyntax(this::execute, entity, pos, entityClass);
|
||||||
setDefaultExecutor((sender, context) -> sender.sendMessage("Usage: /summon <type> <x> <y> <z> <class>"));
|
setDefaultExecutor((sender, context) -> sender.sendMessage("Usage: /summon <type> <x> <y> <z> <class>"));
|
||||||
}
|
}
|
||||||
@ -43,7 +39,7 @@ public class SummonCommand extends Command {
|
|||||||
private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) {
|
private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) {
|
||||||
final Entity entity = commandContext.get(entityClass).instantiate(commandContext.get(this.entity));
|
final Entity entity = commandContext.get(entityClass).instantiate(commandContext.get(this.entity));
|
||||||
//noinspection ConstantConditions - One couldn't possibly execute a command without being in an instance
|
//noinspection ConstantConditions - One couldn't possibly execute a command without being in an instance
|
||||||
entity.setInstance(commandSender.asPlayer().getInstance(), commandContext.get(pos).fromSender(commandSender));
|
entity.setInstance(((Player) commandSender).getInstance(), commandContext.get(pos).fromSender(commandSender));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
|
@ -7,7 +7,6 @@ import net.minestom.server.command.builder.Command;
|
|||||||
import net.minestom.server.command.builder.CommandContext;
|
import net.minestom.server.command.builder.CommandContext;
|
||||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
import net.minestom.server.command.builder.arguments.ArgumentType;
|
||||||
import net.minestom.server.coordinate.Pos;
|
import net.minestom.server.coordinate.Pos;
|
||||||
import net.minestom.server.coordinate.Vec;
|
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.utils.location.RelativeVec;
|
import net.minestom.server.utils.location.RelativeVec;
|
||||||
|
|
||||||
@ -28,15 +27,14 @@ public class TeleportCommand extends Command {
|
|||||||
private void onPlayerTeleport(CommandSender sender, CommandContext context) {
|
private void onPlayerTeleport(CommandSender sender, CommandContext context) {
|
||||||
final String playerName = context.get("player");
|
final String playerName = context.get("player");
|
||||||
Player pl = MinecraftServer.getConnectionManager().getPlayer(playerName);
|
Player pl = MinecraftServer.getConnectionManager().getPlayer(playerName);
|
||||||
if (pl != null && sender.isPlayer()) {
|
if (sender instanceof Player player) {
|
||||||
Player player = (Player) sender;
|
|
||||||
player.teleport(pl.getPosition());
|
player.teleport(pl.getPosition());
|
||||||
}
|
}
|
||||||
sender.sendMessage(Component.text("Teleported to player " + playerName));
|
sender.sendMessage(Component.text("Teleported to player " + playerName));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPositionTeleport(CommandSender sender, CommandContext context) {
|
private void onPositionTeleport(CommandSender sender, CommandContext context) {
|
||||||
final Player player = sender.asPlayer();
|
final Player player = (Player) sender;
|
||||||
|
|
||||||
final RelativeVec relativeVec = context.get("pos");
|
final RelativeVec relativeVec = context.get("pos");
|
||||||
final Pos position = player.getPosition().withCoord(relativeVec.from(player));
|
final Pos position = player.getPosition().withCoord(relativeVec.from(player));
|
||||||
|
@ -21,7 +21,7 @@ public class TitleCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleTitle(CommandSender source, CommandContext context) {
|
private void handleTitle(CommandSender source, CommandContext context) {
|
||||||
Player player = source.asPlayer();
|
Player player = (Player) source;
|
||||||
String titleContent = context.get("content");
|
String titleContent = context.get("content");
|
||||||
|
|
||||||
player.showTitle(Title.title(Component.text(titleContent), Component.empty(), Title.DEFAULT_TIMES));
|
player.showTitle(Title.title(Component.text(titleContent), Component.empty(), Title.DEFAULT_TIMES));
|
||||||
|
Loading…
Reference in New Issue
Block a user