Raxen/src/main/java/me/unurled/raxen/commands/admin/CustomModelDataCommand.java
unurled b24ddfba27 gq to me
change of domain name
2022-06-09 23:27:04 +02:00

97 lines
3.8 KiB
Java

package me.unurled.raxen.commands.admin;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static me.unurled.raxen.utils.Items.customModelData;
import static me.unurled.raxen.utils.Utils.*;
public class CustomModelDataCommand implements TabExecutor {
/**
* change custom model data of an item
* @param sender Source of the command
* @param command Command which was executed
* @param label Alias of the command which was used
* @param args Passed command arguments
* @return
*/
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player)) {
errorConsoleSender(sender);
return true;
}
Player p = (Player) sender;
if (!(p.hasPermission("raxen.custommodeldata.cmd"))) {
p.sendMessage(noPerms());
return true;
}
ItemStack it = p.getInventory().getItemInMainHand();
if (args.length == 0) {
p.sendMessage(colorComp("<gold>Usage: /custommodeldata <model-data-value> <item-name></gold>"));
return true;
}
if (!(isInt(args[0]))) {
Material material = Material.getMaterial(args[1]);
if (material != null) {
it = customModelData(new ItemStack(material), Integer.parseInt(args[0]));
p.getInventory().setItemInMainHand(it);
p.updateInventory();
} else {
p.sendMessage(colorComp("<red>Please specify a valid Item Name or a valid Integer.</red>"));
}
} else {
if (it != null && it.getType() != Material.AIR) { // check if player has an item in hand
// has item in hand applying the custom model data
it = customModelData(it, Integer.parseInt(args[0]));
p.getInventory().setItemInMainHand(it);
p.updateInventory();
} else {
p.sendMessage(colorComp("<red>Get an item in hand please or specify an item</red>"));
p.sendMessage(colorComp("<gold>Usage: /custommodeldata <model-data-value> <item-name></gold>"));
}
}
return true;
}
/**
* @param sender Source of the command. For players tab-completing a
* command inside of a command block, this will be the player, not
* the command block.
* @param command Command which was executed
* @param label Alias of the command which was used
* @param args The arguments passed to the command, including final
* partial argument to be completed
* @return
*/
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
final List<String> completions = new ArrayList<>();
List<String> SUB_COMMANDS= new ArrayList<>();
if (!(isInt(args[0]))) {
return Arrays.asList( "§c" + args[0]);
}
if (args.length == 2) {
for (Material mat : Material.values()) {
SUB_COMMANDS.add(mat.name());
}
StringUtil.copyPartialMatches(args[1], SUB_COMMANDS, completions);
Collections.sort(completions);
}
return completions;
}
}