Skip to content

Commit

Permalink
fix: OptionalArg broken and not allowing to execute a argless command
Browse files Browse the repository at this point in the history
  • Loading branch information
0PandaDEV committed Feb 14, 2025
1 parent 4ef5a3c commit 7cb1624
Show file tree
Hide file tree
Showing 19 changed files with 278 additions and 313 deletions.
12 changes: 7 additions & 5 deletions src/main/java/net/pandadev/nextron/commands/BackCommand.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.pandadev.nextron.commands;

import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.optional.OptionalArg;
import dev.rollczi.litecommands.annotations.permission.Permission;
import net.pandadev.nextron.Main;
import net.pandadev.nextron.apis.SettingsAPI;
Expand All @@ -12,6 +12,8 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Optional;

@Command(name = "back")
@Permission("nextron.back")
public class BackCommand extends HelpBase {
Expand All @@ -21,14 +23,14 @@ public BackCommand() {
}

@Execute
void backCommand(@Context CommandSender sender, @OptionalArg Player target) {
void backCommand(@Context CommandSender sender, @Arg Optional<Player> target) {
Player player = (Player) sender;

if (target == null) {
if (target.isEmpty()) {
teleportBack(player);
} else {
teleportBack(target);
player.sendMessage(Main.getPrefix() + TextAPI.get("back.other.success").replace("%p", target.getName()));
teleportBack(target.get());
player.sendMessage(Main.getPrefix() + TextAPI.get("back.other.success").replace("%p", target.get().getName()));
}
}

Expand Down
23 changes: 11 additions & 12 deletions src/main/java/net/pandadev/nextron/commands/EnderchestCommand.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package net.pandadev.nextron.commands;

import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.optional.OptionalArg;
import dev.rollczi.litecommands.annotations.permission.Permission;
import net.pandadev.nextron.Main;
import org.bukkit.entity.Player;

import java.util.Optional;

@Command(name = "enderchest", aliases = "ec")
@Permission("nextron.enderchest")
public class EnderchestCommand extends HelpBase {
Expand All @@ -17,18 +19,15 @@ public EnderchestCommand() {
}

@Execute
void enderchestCommand(@Context Player player, @OptionalArg Player target) {
if (target == null) {
void enderchestCommand(@Context Player player, @Arg Optional<Player> target) {
if (target.isEmpty()) {
player.openInventory(player.getEnderChest());
return;
}

if (!player.hasPermission("nextron.enderchest.other")) {
player.sendMessage(Main.getNoPerm());
return;
} else {
if (!player.hasPermission("nextron.enderchest.other")) {
player.sendMessage(Main.getNoPerm());
return;
}
player.openInventory(target.get().getEnderChest());
}

player.openInventory(target.getEnderChest());
return;
}
}
33 changes: 15 additions & 18 deletions src/main/java/net/pandadev/nextron/commands/FlyCommand.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package net.pandadev.nextron.commands;

import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.optional.OptionalArg;
import dev.rollczi.litecommands.annotations.permission.Permission;
import net.pandadev.nextron.Main;
import net.pandadev.nextron.apis.SettingsAPI;
import net.pandadev.nextron.languages.TextAPI;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Optional;

@Command(name = "fly")
@Permission("nextron.fly")
public class FlyCommand extends HelpBase {
Expand All @@ -20,15 +22,13 @@ public FlyCommand() {
}

@Execute
void flyCommand(@Context CommandSender sender, @OptionalArg Player target) {
if (target == null) {
if (!(sender instanceof Player)) {
void flyCommand(@Context CommandSender sender, @Arg Optional<Player> target) {
if (target.isEmpty()) {
if (!(sender instanceof Player player)) {
sender.sendMessage(Main.getCommandInstance());
return;
}

Player player = (Player) (sender);

if (player.getAllowFlight()) {
player.setAllowFlight(false);
if (SettingsAPI.allowsFeedback(player)) {
Expand All @@ -40,20 +40,17 @@ void flyCommand(@Context CommandSender sender, @OptionalArg Player target) {
player.sendMessage(Main.getPrefix() + TextAPI.get("fly.on"));
}
}

player.setFallDistance(0.0f);

return;
}

if (target.getAllowFlight()) {
target.setAllowFlight(false);
sender.sendMessage(Main.getPrefix() + TextAPI.get("fly.other.off").replace("%t", target.getName()));
} else {
target.setAllowFlight(true);
sender.sendMessage(Main.getPrefix() + TextAPI.get("fly.other.on").replace("%t", target.getName()));
Player targetPlayer = target.get();
if (targetPlayer.getAllowFlight()) {
targetPlayer.setAllowFlight(false);
sender.sendMessage(Main.getPrefix() + TextAPI.get("fly.other.off").replace("%t", targetPlayer.getName()));
} else {
targetPlayer.setAllowFlight(true);
sender.sendMessage(Main.getPrefix() + TextAPI.get("fly.other.on").replace("%t", targetPlayer.getName()));
}
targetPlayer.setFallDistance(0.0f);
}

target.setFallDistance(0.0f);
}
}
146 changes: 64 additions & 82 deletions src/main/java/net/pandadev/nextron/commands/GamemodeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,141 +4,123 @@
import dev.rollczi.litecommands.annotations.command.RootCommand;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.optional.OptionalArg;
import dev.rollczi.litecommands.annotations.permission.Permission;
import net.pandadev.nextron.Main;
import net.pandadev.nextron.languages.TextAPI;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Optional;

@RootCommand
public class GamemodeCommand extends HelpBase {

public GamemodeCommand() {
super(
"gamemode, Changes the gamemode, /gamemode <gamemode> [player]\n/gm <gamemode> [player]",
"gms, Change the gamemode to survival, /gms [player]",
"gmc, Change the gamemode to creative, /gmc [player]",
"gma, Change the gamemode to adventure, /gma [player]",
"gmsp, Change the gamemode to spectator, /gmsp [player]",
"survival, Change the gamemode to survival, /survival [player]",
"creative, Change the gamemode to creative, /creative [player]",
"adventure, Change the gamemode to adventure, /adventure [player]",
"spectator, Change the gamemode to spectator, /spectator [player]",
"gm0, Change the gamemode to survival, /gm0 [player]",
"gm1, Change the gamemode to creative, /gm1 [player]",
"gm2, Change the gamemode to adventure, /gm2 [player]",
"gm3, Change the gamemode to spectator, /gm3 [player]"
);
super("gamemode, Changes the gamemode, /gamemode <gamemode> [player]\n/gm <gamemode> [player]", "gmc, Change the gamemode to creative, /gmc [player]", "gms, Change the gamemode to survival, /gms [player]", "gma, Change the gamemode to adventure, /gma [player]", "gmsp, Change the gamemode to spectator, /gmsp [player]", "survival, Change the gamemode to survival, /survival [player]", "creative, Change the gamemode to creative, /creative [player]", "adventure, Change the gamemode to adventure, /adventure [player]", "spectator, Change the gamemode to spectator, /spectator [player]", "gm0, Change the gamemode to survival, /gm0 [player]", "gm1, Change the gamemode to creative, /gm1 [player]", "gm2, Change the gamemode to adventure, /gm2 [player]", "gm3, Change the gamemode to spectator, /gm3 [player]");
}


@Execute(name = "gmc", aliases = {"creative", "gm1"})
@Permission("nextron.gamemode.creative")
void creative(@Context CommandSender sender, @OptionalArg Player target) {
if (target == null) {
if (!(sender instanceof Player)) {
sender.sendMessage("§6This command can only be run by a player!");
public void creative(@Context CommandSender sender, @Arg Optional<Player> target) {
if (target.isEmpty()) {
if (!(sender instanceof Player player)) {
sender.sendMessage(Main.getCommandInstance());
return;
}

Player player = (Player) (sender);
player.setGameMode(GameMode.CREATIVE);
player.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.success").replace("%g", GameMode.CREATIVE.toString().toLowerCase()));
return;
player.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.success")
.replace("%g", "creative"));
} else {
Player pTarget = target.get();
pTarget.setGameMode(GameMode.CREATIVE);
sender.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.other.success")
.replace("%t", pTarget.getName())
.replace("%g", "creative"));
}

target.setGameMode(GameMode.CREATIVE);
sender.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.other.success").replace("%t", target.getName()).replace("%g", GameMode.CREATIVE.toString().toLowerCase()));
}

@Execute(name = "gms", aliases = {"survival", "gm0"})
@Permission("nextron.gamemode.survival")
void survival(@Context CommandSender sender, @OptionalArg Player target) {
if (target == null) {
if (!(sender instanceof Player)) {
sender.sendMessage("§6This command can only be run by a player!");
public void survival(@Context CommandSender sender, @Arg Optional<Player> target) {
if (target.isEmpty()) {
if (!(sender instanceof Player player)) {
sender.sendMessage(Main.getCommandInstance());
return;
}

Player player = (Player) (sender);
player.setGameMode(GameMode.SURVIVAL);
player.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.success").replace("%g", GameMode.SURVIVAL.toString().toLowerCase()));
return;
player.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.success")
.replace("%g", "survival"));
} else {
Player pTarget = target.get();
pTarget.setGameMode(GameMode.SURVIVAL);
sender.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.other.success")
.replace("%t", pTarget.getName())
.replace("%g", "survival"));
}

target.setGameMode(GameMode.SURVIVAL);
sender.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.other.success").replace("%t", target.getName()).replace("%g", GameMode.SURVIVAL.toString().toLowerCase()));
}

@Execute(name = "gmsp", aliases = {"spectator", "gm3"})
@Permission("nextron.gamemode.spectator")
void spectator(@Context CommandSender sender, @OptionalArg Player target) {
if (target == null) {
if (!(sender instanceof Player)) {
sender.sendMessage("§6This command can only be run by a player!");
public void spectator(@Context CommandSender sender, @Arg Optional<Player> target) {
if (target.isEmpty()) {
if (!(sender instanceof Player player)) {
sender.sendMessage(Main.getCommandInstance());
return;
}

Player player = (Player) (sender);
player.setGameMode(GameMode.SPECTATOR);
player.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.success").replace("%g", GameMode.SPECTATOR.toString().toLowerCase()));
return;
player.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.success")
.replace("%g", "spectator"));
} else {
Player pTarget = target.get();
pTarget.setGameMode(GameMode.SPECTATOR);
sender.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.other.success")
.replace("%t", pTarget.getName())
.replace("%g", "spectator"));
}

target.setGameMode(GameMode.SPECTATOR);
sender.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.other.success").replace("%t", target.getName()).replace("%g", GameMode.SPECTATOR.toString().toLowerCase()));
}

@Execute(name = "gma", aliases = {"adventure", "gm2"})
@Permission("nextron.gamemode.adventure")
void adventure(@Context CommandSender sender, @OptionalArg Player target) {
if (target == null) {
if (!(sender instanceof Player)) {
sender.sendMessage("§6This command can only be run by a player!");
public void adventure(@Context CommandSender sender, @Arg Optional<Player> target) {
if (target.isEmpty()) {
if (!(sender instanceof Player player)) {
sender.sendMessage(Main.getCommandInstance());
return;
}

Player player = (Player) (sender);

player.setGameMode(GameMode.ADVENTURE);
player.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.success").replace("%g", GameMode.ADVENTURE.toString().toLowerCase()));
return;
player.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.success")
.replace("%g", "adventure"));
} else {
Player pTarget = target.get();
pTarget.setGameMode(GameMode.ADVENTURE);
sender.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.other.success")
.replace("%t", pTarget.getName())
.replace("%g", "adventure"));
}

target.setGameMode(GameMode.ADVENTURE);
sender.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.other.success").replace("%t", target.getName()).replace("%g", GameMode.ADVENTURE.toString().toLowerCase()));
}

@Execute(name = "gamemode", aliases = {"gm"})
@Permission("nextron.gamemode")
void gamemodeCommand(@Context CommandSender sender, @Arg GameMode gamemode, @OptionalArg Player target) {
if (target == null) {
if (!(sender instanceof Player)) {
sender.sendMessage("§6This command can only be run by a player!");
public void gamemode(@Context CommandSender sender, @Arg GameMode gamemode, @Arg Optional<Player> target) {
if (target.isEmpty()) {
if (!(sender instanceof Player player)) {
sender.sendMessage(Main.getCommandInstance());
return;
}

Player player = (Player) (sender);

setGamemode(player, gamemode);
player.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.success").replace("%g", gamemode.toString().toLowerCase()));

player.setGameMode(gamemode);
player.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.success")
.replace("%g", gamemode.toString().toLowerCase()));
} else {
if (!sender.hasPermission("nextron.gamemode.other")) {
sender.sendMessage("§cYou do not have permission to change others' gamemode.");
sender.sendMessage(Main.getNoPerm());
return;
}
setGamemode(target, gamemode);
sender.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.other.success").replace("%t", target.getName()).replace("%g", gamemode.toString().toLowerCase()));
}
}

private void setGamemode(Player player, GameMode gamemode) {
if (player.getGameMode().equals(gamemode)) {
return;
Player pTarget = target.get();
pTarget.setGameMode(gamemode);
sender.sendMessage(Main.getPrefix() + TextAPI.get("gamemode.other.success")
.replace("%t", pTarget.getName())
.replace("%g", gamemode.toString().toLowerCase()));
}
player.setGameMode(gamemode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import dev.rollczi.litecommands.annotations.permission.Permission;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Text;
import net.pandadev.nextron.Main;
import net.pandadev.nextron.languages.TextAPI;
import org.bukkit.command.CommandSender;
Expand All @@ -31,7 +31,7 @@ public void getPositionCommand(@Context CommandSender sender, @Arg Player target
if (player.isOp() && player.hasPermission("nextron.getposition.teleport")) {
TextComponent teleport = new TextComponent("§2[§aTeleport§2]");
teleport.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tp " + player.getName() + " " + target.getName()));
teleport.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(TextAPI.get("getpos.hover") + "§a" + target.getName()).create()));
teleport.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(TextAPI.get("getpos.hover") + "§a" + target.getName())));

player.sendMessage(Main.getPrefix() + TextAPI.get("getpos.success").replace("%p", target.getName()) + " X: §a" + Math.floor(target.getLocation().getX()) + "§7 Y: §a" + Math.floor(target.getLocation().getY()) + "§7 Z: §a" + Math.floor(target.getLocation().getZ()));
player.spigot().sendMessage(ChatMessageType.SYSTEM, teleport);
Expand Down
Loading

0 comments on commit 7cb1624

Please sign in to comment.