sprobuj
name: 'SimplePlugin'
version: ${project.version}
main: 'xyz.yooniks.simple.SimplePlugin'
authors: ['yooniks']
description: 'Prosty plugin'
website: 'www.youtube.com/c/Enchanted3'
commands:
service:
aliases: ['services', 'uslugi']
services:
name: "vip"
message-on-buy: "&6{PLAYER} &7has just bought vip!"
package xyz.yooniks.simple;
import lombok.Getter;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.yooniks.simple.command.ServiceCommand;
import xyz.yooniks.simple.service.Service;
import xyz.yooniks.simple.service.ServiceManager;
import java.util.List;
import java.util.stream.Collectors;
public final class SimplePlugin extends JavaPlugin {
@Getter
private ServiceManager serviceManager;
@Override
public void onEnable() {
this.saveDefaultConfig();
final List<Service> services = this.getConfig().getConfigurationSection("services").getKeys(false)
.stream()
.map(id -> new Service(
this.getConfig().getString("services." + id + ".name"),
this.getConfig().getString("services." + id + ".message-on-buy")))
.collect(Collectors.toList());
this.serviceManager = new ServiceManager(services);
this.getCommand("service").setExecutor(new ServiceCommand(this));
}
@Override
public void onDisable() {
this.serviceManager = null;
}
}
package xyz.yooniks.simple.service;
import lombok.Getter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Getter
public class ServiceManager {
private final Map<String, Service> services = new LinkedHashMap<>();
public ServiceManager(List<Service> services) {
services.forEach(
service -> this.services.put(service.getName(), service));
}
}
package xyz.yooniks.simple.service;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class Service {
private final String name, messageOnBuy;
}
package xyz.yooniks.simple.command;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import xyz.yooniks.simple.SimplePlugin;
import xyz.yooniks.simple.service.Service;
@AllArgsConstructor
public class ServiceCommand implements CommandExecutor {
private final SimplePlugin plugin;
@Override
public boolean onCommand(
CommandSender cs, Command command, String label, String[] args) {
if (!cs.hasPermission("is.command")) {
cs.sendMessage(ChatColor.RED + "No permission!");
return true;
}
if (args.length < 2) {
cs.sendMessage(ChatColor.RED + "Correctly usage: /service [player_nickname] [service]");
cs.sendMessage(ChatColor.GREEN + "List of services:");
this.plugin.getServiceManager().getServices().values()
.stream()
.map(service -> ChatColor.YELLOW + service.getName())
.forEach(cs::sendMessage);
return true;
}
final String playerName = args[0];
final String serviceById = args[1];
final Service service = this.plugin.getServiceManager().getServices().get(serviceById);
if (service == null) {
cs.sendMessage(ChatColor.RED + "This service does not exists! List of services: /service");
return true;
}
this.plugin.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&',
StringUtils.replace(service.getMessageOnBuy(), "{PLAYER}", playerName)));
//add commands -.-
return false;
}
}