if (jediPlayer.isOnline(target)) return;
JediPlayer jediTarget = new JediPlayer(target);
if (target.isAllowFlying()) target.setFlying(false);
target.setAllowFlying(!target.isAllowFlying());
jediTarget.sendMessage(target.isAllowFlying() ?
Component.text("Режим полета был включен.").color(NamedTextColor.GREEN) :
Component.text("Режим полета был выключен.").color(NamedTextColor.RED));
jediPlayer.sendMessage(target.isAllowFlying() ?
Component.text("Вы включили режим полета для ", NamedTextColor.GREEN)
.append(jediTarget.getPlayer().getName().color(NamedTextColor.GREEN))
.append(Component.text(".", NamedTextColor.GREEN)) :
Component.text("Вы выключили режим полета для ", NamedTextColor.RED)
.append(jediTarget.getPlayer().getName().color(NamedTextColor.RED))
.append(Component.text(".", NamedTextColor.RED)));
}, ArgumentType.String("target"));
На данный момент команда выглядит так, в чем мои проблемы:
Первая проблема, это то, что таргета я беру по нику и когда прописываю команду то мне не предлагает ники игроков на сервере, как это можно сделать? Аргумент должен быть String
И вторая проблема, if (jediPlayer.isOnline(target)) return; public boolean isOnline(T target) {
if (target == null) {
sendMessage(Component.text("Игрок не найден.", NamedTextColor.RED));
return true;
}
return false;
}
if (target.isAllowFlying()) target.setFlying(false); эта проверка напоминает о том, что если он будет null, то выбросит исключение, хотя он не будет null, т.к. это на себя заберёт проверка if (jediPlayer.isOnline(target)) return; реши проблемы
Assistant
To address your two issues:
1. Making the Command Suggest Online Player Names
To have the command suggest the usernames of online players while still using a String argument type, you can add a suggestion callback to the argument. This way, when the command is being typed, it will display a list of online player names for auto-completion.
Here's how you can modify your command argument to include a suggestion provider:
This modification ensures that when the command is typed, it will suggest the usernames of all online players, enhancing the user experience.
2. Preventing Null Pointer Exceptions with Proper Null Checks
The warning about target potentially being null is due to the compiler not being able to guarantee that target is non-null after your isOnline check. While your isOnline method checks for null internally, the compiler can't infer that target is non-null afterward because it's not explicitly checked in the method where target is used.
To resolve this, you should add an explicit null check in your command handler before proceeding to use target. Here's how you can adjust your code:
java
Player target = MinecraftServer.getConnectionManager().getOnlinePlayerByUsername(context.get("target"));
if (target == null) {
jediPlayer.sendMessage(Component.text("Игрок не найден.", NamedTextColor.RED));
return;
}
// Now that target is confirmed not null, proceed with your logic
if (target.isAllowFlying()) target.setFlying(false);
target.setAllowFlying(!target.isAllowFlying());
JediPlayer jediTarget = new JediPlayer(target);
jediTarget.sendMessage(target.isAllowFlying() ?
Component.text("Режим полета был включен.").color(NamedTextColor.GREEN) :
Component.text("Режим полета был выключен.").color(NamedTextColor.RED));
jediPlayer.sendMessage(target.isAllowFlying() ?
Component.text("Вы включили режим полета для ", NamedTextColor.GREEN)
.append(jediTarget.getPlayer().getName().color(NamedTextColor.GREEN))
.append(Component.text(".", NamedTextColor.GREEN)) :
Component.text("Вы выключили режим полета для ", NamedTextColor.RED)
.append(jediTarget.getPlayer().getName().color(NamedTextColor.RED))
.append(Component.text(".", NamedTextColor.RED)));
By adding the explicit if (target == null) check, you ensure that target is non-null when you use it later in your code. This satisfies the compiler's concern about potential NullPointerExceptions.
Rewriting the isOnline Method (Optional)
Alternatively, you could adjust your isOnline method to better reflect its purpose and improve clarity:
java
public boolean isOnline(Player target) {
if (target == null) {
sendMessage(Component.text("Игрок не найден.", NamedTextColor.RED));
return false;
}
return true;
}
And then in your command handler:
java
if (!jediPlayer.isOnline(target)) return;
This change makes the method name isOnline align with its return value (returning true when the player is online/not null), and ensures that when you proceed after the check, target is non-null.
Summary
Suggestion of Player Names: Use a suggestion callback with your String argument to provide online player names for auto-completion.
Null Checks: Add an explicit null check for target in your command handler to prevent potential NullPointerExceptions and satisfy the compiler.
By implementing these changes, your command should work as intended, providing player name suggestions and ensuring that null values are properly handled.