Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ua.end433.javascriptminecraft;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.plugin.java.JavaPlugin;
- import javax.script.ScriptEngine;
- import javax.script.ScriptEngineManager;
- import javax.script.ScriptException;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- public class JavaScriptMinecraft extends JavaPlugin {
- private File scriptsDirectory;
- private FileConfiguration messagesConfig;
- private File messagesFile;
- @Override
- public void onEnable() {
- getLogger().info("JavaScriptMinecraft plugin enabled!");
- // Створюємо директорію для скриптів
- scriptsDirectory = new File(getDataFolder(), "scripts");
- if (!scriptsDirectory.exists()) {
- if (scriptsDirectory.mkdirs()) {
- getLogger().info("Scripts directory created: " + scriptsDirectory.getAbsolutePath());
- } else {
- getLogger().warning("Failed to create scripts directory.");
- }
- }
- // Завантажуємо конфігурацію локалізації
- loadMessagesConfig();
- }
- @Override
- public void onDisable() {
- getLogger().info("JavaScriptMinecraft plugin disabled!");
- }
- @Override
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
- if (command.getName().equalsIgnoreCase("jsmc")) {
- if (args.length < 1) {
- sender.sendMessage(getMessage("usage"));
- return true;
- }
- String subCommand = args[0].toLowerCase();
- if (subCommand.equals("script")) {
- if (args.length < 2) {
- sender.sendMessage(getMessage("usage"));
- return true;
- }
- if (!sender.hasPermission("jsmc.run")) {
- sender.sendMessage(getMessage("no_permission"));
- return true;
- }
- String scriptName = args[1];
- File scriptFile = new File(scriptsDirectory, scriptName + ".js");
- if (!scriptFile.exists()) {
- sender.sendMessage(getMessage("script_not_found").replace("{script}", scriptName));
- return true;
- }
- try (FileReader reader = new FileReader(scriptFile)) {
- ScriptEngineManager manager = new ScriptEngineManager();
- ScriptEngine engine = manager.getEngineByName("nashorn");
- // Додаємо sender в контекст скрипту
- engine.put("sender", sender);
- engine.put("server", getServer());
- Object result = engine.eval(reader);
- //sender.sendMessage(getMessage("script_success").replace("{result}", String.valueOf(result)));
- } catch (IOException e) {
- sender.sendMessage(getMessage("script_error_io").replace("{error}", e.getMessage()));
- } catch (ScriptException e) {
- sender.sendMessage(getMessage("script_error_exec").replace("{error}", e.getMessage()));
- }
- } else if (subCommand.equals("reload")) {
- if (!sender.hasPermission("jsmc.reload")) {
- sender.sendMessage(getMessage("no_permission"));
- return true;
- }
- loadMessagesConfig();
- sender.sendMessage(getMessage("reload_success"));
- } else {
- sender.sendMessage(getMessage("usage"));
- }
- return true;
- }
- return false;
- }
- private void loadMessagesConfig() {
- messagesFile = new File(getDataFolder(), "config.yml");
- if (!messagesFile.exists()) {
- saveResource("config.yml", false);
- }
- messagesConfig = YamlConfiguration.loadConfiguration(messagesFile);
- }
- private String getMessage(String key) {
- return messagesConfig.getString(key, "§cMessage not found: " + key);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement