Files
src
pom.xml
Code
5 405
src/main/java/ru/timzay/tcodoes/Tcodoes.java
package ru.timzay.tcodoes;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class Tcodoes extends JavaPlugin implements CommandExecutor, TabCompleter {
private static final String ADMIN_PERMISSION = "tcodoes.admin";
private static final String CODES_PATH = "codes";
private File codesFile;
private FileConfiguration codes;
@Override
public void onEnable() {
saveDefaultConfig();
codesFile = new File(getDataFolder(), "codes.yml");
if (!codesFile.exists()) {
saveResource("codes.yml", false);
}
codes = YamlConfiguration.loadConfiguration(codesFile);
if (codes.getConfigurationSection(CODES_PATH) == null) {
codes.createSection(CODES_PATH);
saveCodes();
}
PluginCommand codeCommand = getCommand("code");
if (codeCommand == null) {
getLogger().severe("The code command is missing from plugin.yml.");
return;
}
codeCommand.setExecutor(this);
codeCommand.setTabCompleter(this);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
boolean alias = label.equalsIgnoreCase("codelist");
if (alias) {
if (args.length == 0 || (args.length == 1 && args[0].equalsIgnoreCase("list"))) {
return handleList(sender, args.length == 0 ? 0 : 1);
}
send(sender, "usage-list");
return true;
}
if (args.length == 0) {
send(sender, "usage");
return true;
}
String first = args[0].toLowerCase(Locale.ROOT);
if (first.equals("create")) {
return handleCreate(sender, args);
}
if (first.equals("delete")) {
return handleDelete(sender, args);
}
if (first.equals("list")) {
return handleList(sender, args.length);
}
if (args.length != 1) {
send(sender, "usage");
return true;
}
return redeemCode(sender, args[0]);
}
private boolean handleCreate(CommandSender sender, String[] args) {
if (!hasAdminPermission(sender)) {
return true;
}
if (!(sender instanceof Player)) {
send(sender, "player-only");
return true;
}
if (args.length < 3) {
send(sender, "usage-create");
return true;
}
String code = normalize(args[1]);
if (!isValidCode(code)) {
send(sender, "invalid-code");
return true;
}
if (getCodeSection(code) != null) {
send(sender, "already-exists", "{code}", code);
return true;
}
String commandText = String.join(" ", Arrays.copyOfRange(args, 2, args.length));
String[] commandParts = commandText.split("\\|", -1);
List<String> rewardCommands = new ArrayList<>();
for (String commandPart : commandParts) {
String rewardCommand = commandPart.trim();
if (rewardCommand.startsWith("/")) {
rewardCommand = rewardCommand.substring(1).trim();
}
if (rewardCommand.isEmpty()) {
send(sender, "no-rewards");
return true;
}
rewardCommands.add(rewardCommand);
}
ConfigurationSection codeSection = codes.createSection(CODES_PATH + "." + code);
codeSection.set("commands", rewardCommands);
codeSection.set("used", new ArrayList<String>());
if (!saveCodes()) {
codes.set(CODES_PATH + "." + code, null);
send(sender, "save-error");
return true;
}
send(sender, "created", "{code}", code, "{count}", String.valueOf(rewardCommands.size()));
return true;
}
private boolean handleDelete(CommandSender sender, String[] args) {
if (!hasAdminPermission(sender)) {
return true;
}
if (args.length != 2) {
send(sender, "usage-delete");
return true;
}
String code = normalize(args[1]);
if (getCodeSection(code) == null) {
send(sender, "not-found", "{code}", code);
return true;
}
codes.set(CODES_PATH + "." + code, null);
if (!saveCodes()) {
send(sender, "save-error");
return true;
}
send(sender, "deleted", "{code}", code);
return true;
}
private boolean handleList(CommandSender sender, int argumentCount) {
if (!hasAdminPermission(sender)) {
return true;
}
if (argumentCount != 0 && argumentCount != 1) {
send(sender, "usage-list");
return true;
}
List<String> codeNames = getCodeNames();
Collections.sort(codeNames);
if (codeNames.isEmpty()) {
send(sender, "list-empty");
return true;
}
send(sender, "list-header");
for (String codeName : codeNames) {
ConfigurationSection section = getCodeSection(codeName);
int count = section == null ? 0 : section.getStringList("commands").size();
send(sender, "list-entry", "{code}", codeName, "{count}", String.valueOf(count));
}
return true;
}
private boolean redeemCode(CommandSender sender, String input) {
if (!(sender instanceof Player)) {
send(sender, "player-only");
return true;
}
String code = normalize(input);
ConfigurationSection section = getCodeSection(code);
if (section == null) {
send(sender, "not-found", "{code}", code);