Skip to content

Commit

Permalink
@A for give command + clear command (#2172)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherko231 authored Feb 26, 2024
1 parent 0389439 commit 0cff906
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/main/java/cn/nukkit/command/SimpleCommandMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private void setDefaultCommands() {
this.register("nukkit", new SaveOffCommand("save-off"));
this.register("nukkit", new SaveCommand("save-all"));
this.register("nukkit", new GiveCommand("give"));
this.register("nukkit", new ClearCommand("clear"));
this.register("nukkit", new EffectCommand("effect"));
this.register("nukkit", new EnchantCommand("enchant"));
this.register("nukkit", new ParticleCommand("particle"));
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/cn/nukkit/command/defaults/ClearCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cn.nukkit.command.defaults;

import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandEnum;
import cn.nukkit.command.data.CommandParamType;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.item.Item;
import cn.nukkit.lang.TranslationContainer;
import cn.nukkit.utils.TextFormat;

import java.util.ArrayList;
import java.util.List;

/**
* Created on 2024/26/2 by Sherko231.
* Package cn.nukkit.command.defaults in project Nukkit .
*/
public class ClearCommand extends VanillaCommand {
public ClearCommand(String name) {
super(name, "%nukkit.command.clear.description", "%nukkit.command.clear.usage");
this.setPermission("nukkit.command.clear");
this.commandParameters.clear();
this.commandParameters.put("default", new CommandParameter[]{});
this.commandParameters.put("target", new CommandParameter[]{
CommandParameter.newType("player", CommandParamType.TARGET),
});
}

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!this.testPermission(sender)) {
return false;
}

if (args.length > 1 || (!(sender instanceof Player) && args.length < 1)) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return false;
}

if (args.length == 0) {
Player player = (Player) sender;
player.getInventory().clearAll();
return false;
}

List<Player> targets = new ArrayList<>();
if (args[0].equals("@a")) {
targets.addAll(Server.getInstance().getOnlinePlayers().values());
}
else {
Player target = sender.getServer().getPlayer(args[0].replace("@s", sender.getName()));
if (target != null) {
targets.add(target);
}
else {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
return false;
}
}

for (Player player : targets) {
player.getInventory().clearAll();
Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.clear.success", player.getName()));
}

return false;
}
}
52 changes: 33 additions & 19 deletions src/main/java/cn/nukkit/command/defaults/GiveCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.nukkit.command.defaults;

import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandEnum;
Expand All @@ -10,6 +11,10 @@
import cn.nukkit.lang.TranslationContainer;
import cn.nukkit.utils.TextFormat;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* Created on 2015/12/9 by xtypr.
* Package cn.nukkit.command.defaults in project Nukkit .
Expand Down Expand Up @@ -42,23 +47,35 @@ public GiveCommand(String name) {
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!this.testPermission(sender)) {
return true;
return false;
}

if (args.length < 2) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return false;
}

return true;
List<Player> targets = new ArrayList<>();
if (args[0].equals("@a")) {
targets.addAll(Server.getInstance().getOnlinePlayers().values());
}
else {
Player target = sender.getServer().getPlayer(args[0].replace("@s", sender.getName()));
if (target != null) {
targets.add(target);
}
else {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
return false;
}
}

Player player = sender.getServer().getPlayer(args[0].replace("@s", sender.getName()));
Item item;

try {
item = Item.fromString(args[1]);
} catch (Exception e) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return true;
return false;
}

try {
Expand All @@ -67,22 +84,19 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
item.setCount(item.getMaxStackSize());
}

if (player != null) {
if (item.getId() == 0) {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.give.item.notFound", args[1]));
return true;
}
player.getInventory().addItem(item.clone());
} else {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
if (item.getId() == 0) {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.give.item.notFound", args[1]));
return false;
}

return true;
for (Player player : targets) {
player.getInventory().addItem(item.clone());
Command.broadcastCommandMessage(sender, new TranslationContainer(
"%commands.give.success",
item.getName() + " (" + item.getId() + ":" + item.getDamage() + ")",
String.valueOf(item.getCount()),
player.getName()));
}
Command.broadcastCommandMessage(sender, new TranslationContainer(
"%commands.give.success",
item.getName() + " (" + item.getId() + ":" + item.getDamage() + ")",
String.valueOf(item.getCount()),
player.getName()));
return true;
}
}
1 change: 1 addition & 0 deletions src/main/java/cn/nukkit/permission/DefaultPermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static void registerCorePermissions() {
registerPermission(new Permission(ROOT + ".command.tell", "Allows the user to privately message another player", Permission.DEFAULT_TRUE), commands);
registerPermission(new Permission(ROOT + ".command.say", "Allows the user to talk as the console", Permission.DEFAULT_OP), commands);
registerPermission(new Permission(ROOT + ".command.give", "Allows the user to give items to players", Permission.DEFAULT_OP), commands);
registerPermission(new Permission(ROOT + ".command.clear", "Allows the user to clear items from players", Permission.DEFAULT_OP), commands);
registerPermission(new Permission(ROOT + ".command.effect", "Allows the user to give/take potion effects", Permission.DEFAULT_OP), commands);
registerPermission(new Permission(ROOT + ".command.particle", "Allows the user to create particle effects", Permission.DEFAULT_OP), commands);
registerPermission(new Permission(ROOT + ".command.teleport", "Allows the user to teleport players", Permission.DEFAULT_OP), commands);
Expand Down

0 comments on commit 0cff906

Please sign in to comment.