Skip to content

Commit bd658d6

Browse files
committed
Added a command to set the text with a javascript expression
1 parent 59329b8 commit bd658d6

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/main/java/me/lorenzo0111/rocketplaceholders/command/RocketPlaceholdersCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public RocketPlaceholdersCommand(RocketPlaceholders plugin, PlaceholdersManager
6464
subcommands.add(new GuiCommand(this));
6565
subcommands.add(new EditorCommand(this));
6666
subcommands.add(new SetCommand(this));
67+
subcommands.add(new SetJSCommand(this));
6768
}
6869

6970
@Override

src/main/java/me/lorenzo0111/rocketplaceholders/command/subcommands/HelpCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void perform(CommandSender sender, String[] args) {
4949
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getCommand().getPlugin().getConfig().getString("prefix") + "&r &8/rocketplaceholders gui » &7Open the gui"));
5050
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getCommand().getPlugin().getConfig().getString("prefix") + "&r &8/rocketplaceholders debug » &7Print debug message"));
5151
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getCommand().getPlugin().getConfig().getString("prefix") + "&r &8/rocketplaceholders test (Placeholder) » &7Try a placeholder"));
52-
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getCommand().getPlugin().getConfig().getString("prefix") + "&r &8/rocketplaceholders set (Placeholder) (New Text) » &7Edit the text of a placeholder"));
52+
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getCommand().getPlugin().getConfig().getString("prefix") + "&r &8/rocketplaceholders set (Placeholder) [--user Username] (New Text) » &7Edit the text of a placeholder"));
53+
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getCommand().getPlugin().getConfig().getString("prefix") + "&r &8/rocketplaceholders setjs (Placeholder) (Javascript Expression) » &7Edit the text of a placeholder"));
5354
}
5455
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)