|
| 1 | +/* |
| 2 | + * This file is part of RocketPlaceholders, licensed under the MIT License. |
| 3 | + * |
| 4 | + * Copyright (c) Lorenzo0111 |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package me.lorenzo0111.rocketplaceholders.command.subcommands; |
| 26 | + |
| 27 | +import me.lorenzo0111.rocketplaceholders.command.RocketPlaceholdersCommand; |
| 28 | +import me.lorenzo0111.rocketplaceholders.command.SubCommand; |
| 29 | +import me.lorenzo0111.rocketplaceholders.creator.Placeholder; |
| 30 | +import me.lorenzo0111.rocketplaceholders.utilities.JavaScriptParser; |
| 31 | +import org.bukkit.Bukkit; |
| 32 | +import org.bukkit.ChatColor; |
| 33 | +import org.bukkit.command.CommandSender; |
| 34 | + |
| 35 | +import javax.script.ScriptException; |
| 36 | + |
| 37 | +public class SetJSCommand extends SubCommand { |
| 38 | + private final JavaScriptParser<String> engine; |
| 39 | + |
| 40 | + public SetJSCommand(RocketPlaceholdersCommand command) { |
| 41 | + super(command); |
| 42 | + this.engine = new JavaScriptParser<>(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getName() { |
| 47 | + return "setjs"; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void perform(CommandSender sender, String[] args) { |
| 52 | + if (!sender.hasPermission("rocketplaceholders.command.setjs")) { |
| 53 | + this.sendPermissionsError(sender); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (args.length < 3) { |
| 58 | + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getCommand().getPlugin().getConfig().getString("prefix") + "&r &7Try to use &8/rocketplaceholders setjs (Placeholder) (Javascript Expression)!")); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + Placeholder placeholder = this.getCommand().getPlaceholdersManager().getStorageManager().get(args[1]); |
| 63 | + |
| 64 | + if (placeholder == null) { |
| 65 | + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getCommand().getPlugin().getConfig().getString("prefix") + "&r &7This placeholder does not exists!")); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + StringBuilder builder = new StringBuilder(); |
| 70 | + for (int i = 2; i < args.length; i++) { |
| 71 | + builder.append(args[i]).append(" "); |
| 72 | + } |
| 73 | + |
| 74 | + builder.deleteCharAt(builder.length() - 1); |
| 75 | + engine.bind("Player", sender); |
| 76 | + engine.bind("Server", Bukkit.getServer()); |
| 77 | + engine.bind("Placeholder", placeholder); |
| 78 | + try { |
| 79 | + String text = engine.parse(builder.toString()); |
| 80 | + if (text != null) { |
| 81 | + placeholder.setText(text); |
| 82 | + } |
| 83 | + } catch (ScriptException e) { |
| 84 | + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getCommand().getPlugin().getConfig().getString("prefix") + "&r &7Error while parsing the javascript expression!")); |
| 85 | + this.getCommand().getPlugin().debug(e.getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments