Add a chat message to quickly edit a hologram after modifying its lines

This commit is contained in:
filoghost 2019-05-30 14:54:31 +02:00
parent 0f0d709abe
commit c0da957d6c
13 changed files with 96 additions and 37 deletions

View File

@ -20,6 +20,7 @@ public enum ConfigNode {
SPACE_BETWEEN_LINES("space-between-lines", 0.02), SPACE_BETWEEN_LINES("space-between-lines", 0.02),
PRECISE_HOLOGRAM_MOVEMENT("precise-hologram-movement", true), PRECISE_HOLOGRAM_MOVEMENT("precise-hologram-movement", true),
QUICK_EDIT_COMMANDS("quick-edit-commands", true),
IMAGES_SYMBOL("images.symbol", "[x]"), IMAGES_SYMBOL("images.symbol", "[x]"),
TRANSPARENCY_SPACE("images.transparency.space", " [|] "), TRANSPARENCY_SPACE("images.transparency.space", " [|] "),
TRANSPARENCY_COLOR("images.transparency.color", "&7"), TRANSPARENCY_COLOR("images.transparency.color", "&7"),

View File

@ -39,6 +39,7 @@ public class Configuration {
public static double spaceBetweenLines; public static double spaceBetweenLines;
public static boolean preciseHologramMovement; public static boolean preciseHologramMovement;
public static boolean quickEditCommands;
public static String imageSymbol; public static String imageSymbol;
public static String transparencySymbol; public static String transparencySymbol;
public static boolean updateNotification; public static boolean updateNotification;
@ -129,6 +130,7 @@ public class Configuration {
spaceBetweenLines = config.getDouble(ConfigNode.SPACE_BETWEEN_LINES.getPath()); spaceBetweenLines = config.getDouble(ConfigNode.SPACE_BETWEEN_LINES.getPath());
preciseHologramMovement = config.getBoolean(ConfigNode.PRECISE_HOLOGRAM_MOVEMENT.getPath()); preciseHologramMovement = config.getBoolean(ConfigNode.PRECISE_HOLOGRAM_MOVEMENT.getPath());
quickEditCommands = config.getBoolean(ConfigNode.QUICK_EDIT_COMMANDS.getPath());
updateNotification = config.getBoolean(ConfigNode.UPDATE_NOTIFICATION.getPath()); updateNotification = config.getBoolean(ConfigNode.UPDATE_NOTIFICATION.getPath());

View File

@ -60,7 +60,7 @@ public class HolographicDisplays extends JavaPlugin {
private static MainListener mainListener; private static MainListener mainListener;
// The command handler, just in case a plugin wants to register more commands. // The command handler, just in case a plugin wants to register more commands.
private HologramsCommandHandler commandHandler; private static HologramsCommandHandler commandHandler;
// The new version found by the updater, null if there is no new version. // The new version found by the updater, null if there is no new version.
private static String newVersion; private static String newVersion;
@ -201,7 +201,7 @@ public class HolographicDisplays extends JavaPlugin {
return mainListener; return mainListener;
} }
public HologramsCommandHandler getCommandHandler() { public static HologramsCommandHandler getCommandHandler() {
return commandHandler; return commandHandler;
} }

View File

@ -21,7 +21,7 @@ public class Strings {
public static final String BASE_PERM = "holograms."; public static final String BASE_PERM = "holograms.";
public static final String TIP_PREFIX = "" + ChatColor.YELLOW + ChatColor.BOLD + "TIP" + Colors.SECONDARY_SHADOW + " "; public static final String TIP_PREFIX = "" + ChatColor.YELLOW + ChatColor.BOLD + "TIP:" + Colors.SECONDARY_SHADOW + " ";
public static String formatTitle(String input) { public static String formatTitle(String input) {

View File

@ -17,6 +17,7 @@ package com.gmail.filoghost.holographicdisplays.commands.main;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -49,14 +50,16 @@ import com.gmail.filoghost.holographicdisplays.util.Utils;
public class HologramsCommandHandler implements CommandExecutor { public class HologramsCommandHandler implements CommandExecutor {
private List<HologramSubCommand> subCommands; private List<HologramSubCommand> subCommands;
private Map<Class<? extends HologramSubCommand>, HologramSubCommand> subCommandsByClass;
public HologramsCommandHandler() { public HologramsCommandHandler() {
subCommands = Utils.newList(); subCommands = Utils.newList();
subCommandsByClass = Utils.newMap();
registerSubCommand(new AddlineCommand()); registerSubCommand(new AddlineCommand());
registerSubCommand(new CreateCommand()); registerSubCommand(new CreateCommand());
registerSubCommand(new DeleteCommand()); registerSubCommand(new DeleteCommand());
registerSubCommand(new EditCommand(this)); registerSubCommand(new EditCommand());
registerSubCommand(new ListCommand()); registerSubCommand(new ListCommand());
registerSubCommand(new NearCommand()); registerSubCommand(new NearCommand());
registerSubCommand(new TeleportCommand()); registerSubCommand(new TeleportCommand());
@ -72,17 +75,22 @@ public class HologramsCommandHandler implements CommandExecutor {
registerSubCommand(new ReadimageCommand()); registerSubCommand(new ReadimageCommand());
registerSubCommand(new InfoCommand()); registerSubCommand(new InfoCommand());
registerSubCommand(new HelpCommand(this)); registerSubCommand(new HelpCommand());
} }
public void registerSubCommand(HologramSubCommand subCommand) { public void registerSubCommand(HologramSubCommand subCommand) {
subCommands.add(subCommand); subCommands.add(subCommand);
subCommandsByClass.put(subCommand.getClass(), subCommand);
} }
public List<HologramSubCommand> getSubCommands() { public List<HologramSubCommand> getSubCommands() {
return new ArrayList<HologramSubCommand>(subCommands); return new ArrayList<HologramSubCommand>(subCommands);
} }
public HologramSubCommand getSubCommand(Class<? extends HologramSubCommand> subCommandClass) {
return subCommandsByClass.get(subCommandClass);
}
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

View File

@ -73,8 +73,10 @@ public class AddlineCommand extends HologramSubCommand {
HologramDatabase.saveHologram(hologram); HologramDatabase.saveHologram(hologram);
HologramDatabase.trySaveToDisk(); HologramDatabase.trySaveToDisk();
sender.sendMessage(Colors.PRIMARY + "Line added!");
Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram));
sender.sendMessage(Colors.PRIMARY + "Line added!");
EditCommand.sendQuickEditCommands(sender, label, hologram.getName());
} }
@Override @Override

View File

@ -20,15 +20,17 @@ import java.util.List;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.filoghost.holographicdisplays.HolographicDisplays;
import com.gmail.filoghost.holographicdisplays.commands.Colors; import com.gmail.filoghost.holographicdisplays.commands.Colors;
import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; import com.gmail.filoghost.holographicdisplays.commands.CommandValidator;
import com.gmail.filoghost.holographicdisplays.commands.Strings; import com.gmail.filoghost.holographicdisplays.commands.Strings;
import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.HologramsCommandHandler; import com.gmail.filoghost.holographicdisplays.disk.Configuration;
import com.gmail.filoghost.holographicdisplays.exception.CommandException; import com.gmail.filoghost.holographicdisplays.exception.CommandException;
import com.gmail.filoghost.holographicdisplays.object.NamedHologram; import com.gmail.filoghost.holographicdisplays.object.NamedHologram;
import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager;
import com.gmail.filoghost.holographicdisplays.util.Utils; import com.gmail.filoghost.holographicdisplays.util.Utils;
import com.google.common.collect.Lists;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ClickEvent;
@ -39,12 +41,17 @@ import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention;
public class EditCommand extends HologramSubCommand { public class EditCommand extends HologramSubCommand {
private HologramsCommandHandler mainCommandHandler; private static final List<QuickCommandInfo> QUICK_EDIT_COMMANDS = Lists.newArrayList(
new QuickCommandInfo("Add", AddlineCommand.class),
public EditCommand(HologramsCommandHandler mainCommandHandler) { new QuickCommandInfo("Remove", RemovelineCommand.class),
new QuickCommandInfo("Set", SetlineCommand.class),
new QuickCommandInfo("Insert", InsertlineCommand.class),
new QuickCommandInfo("View", InfoCommand.class)
);
public EditCommand() {
super("edit"); super("edit");
setPermission(Strings.BASE_PERM + "edit"); setPermission(Strings.BASE_PERM + "edit");
this.mainCommandHandler = mainCommandHandler;
} }
@Override @Override
@ -66,7 +73,7 @@ public class EditCommand extends HologramSubCommand {
sender.sendMessage(""); sender.sendMessage("");
sender.sendMessage(Strings.formatTitle("How to edit the hologram '" + name + "'")); sender.sendMessage(Strings.formatTitle("How to edit the hologram '" + name + "'"));
for (HologramSubCommand subCommand : mainCommandHandler.getSubCommands()) { for (HologramSubCommand subCommand : HolographicDisplays.getCommandHandler().getSubCommands()) {
if (subCommand.getType() == SubCommandType.EDIT_LINES) { if (subCommand.getType() == SubCommandType.EDIT_LINES) {
String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments().replace("<hologramName>", hologram.getName()).replace("<hologram>", hologram.getName()) : ""); String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments().replace("<hologramName>", hologram.getName()).replace("<hologram>", hologram.getName()) : "");
@ -95,17 +102,37 @@ public class EditCommand extends HologramSubCommand {
} }
} }
public static void sendQuickEditCommands(Player player, String hologramName) { public static void sendQuickEditCommands(CommandSender sender, String label, String hologramName) {
player.spigot().sendMessage(new ComponentBuilder("[Quick Edit]").color(ChatColor.YELLOW).bold(true) if (!Configuration.quickEditCommands) {
.append(" ", FormatRetention.NONE) return;
.append("[Add Line]").event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/")) }
.append("hover").color(ChatColor.WHITE).italic(true).underlined(true) if (!(sender instanceof Player)) {
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(ChatColor.LIGHT_PURPLE + "Hover on the commands to get info about them."))) return;
.append(" or ", FormatRetention.NONE).color(ChatColor.GRAY) }
.append("click").color(ChatColor.WHITE).italic(true).underlined(true)
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(ChatColor.LIGHT_PURPLE + "Click on the commands to insert them in the chat."))) ComponentBuilder message = new ComponentBuilder("EDIT LINES:").color(ChatColor.GRAY).bold(true).append(" ", FormatRetention.NONE);
.append(" on the commands!", FormatRetention.NONE).color(ChatColor.GRAY)
.create()); for (QuickCommandInfo quickEditCommand : QUICK_EDIT_COMMANDS) {
HologramSubCommand subCommand = HolographicDisplays.getCommandHandler().getSubCommand(quickEditCommand.commandClass);
// Assume first argument is always "<hologram>" and remove it
String arguments = subCommand.getPossibleArguments();
if (arguments.contains(" ")) {
arguments = arguments.substring(arguments.indexOf(" ") + 1);
} else {
arguments = "";
}
String usage = "/" + label + " " + subCommand.getName() + " " + hologramName + " ";
message.append("[" + quickEditCommand.chatName + "]").color(ChatColor.DARK_AQUA)
.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, usage))
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(
ChatColor.GRAY + "Click to insert in chat the highlighted part of the command:\n" +
ChatColor.YELLOW + usage + ChatColor.DARK_GRAY + arguments)));
message.append(" ", FormatRetention.NONE);
}
((Player) sender).spigot().sendMessage(message.create());
} }
@Override @Override
@ -117,5 +144,19 @@ public class EditCommand extends HologramSubCommand {
public SubCommandType getType() { public SubCommandType getType() {
return SubCommandType.GENERIC; return SubCommandType.GENERIC;
} }
private static class QuickCommandInfo {
private final String chatName;
private final Class<? extends HologramSubCommand> commandClass;
public QuickCommandInfo(String chatName, Class<? extends HologramSubCommand> command) {
this.chatName = chatName;
this.commandClass = command;
}
}
} }

View File

@ -19,11 +19,11 @@ import java.util.List;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.filoghost.holographicdisplays.HolographicDisplays;
import com.gmail.filoghost.holographicdisplays.commands.Colors; import com.gmail.filoghost.holographicdisplays.commands.Colors;
import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; import com.gmail.filoghost.holographicdisplays.commands.CommandValidator;
import com.gmail.filoghost.holographicdisplays.commands.Strings; import com.gmail.filoghost.holographicdisplays.commands.Strings;
import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.HologramsCommandHandler;
import com.gmail.filoghost.holographicdisplays.exception.CommandException; import com.gmail.filoghost.holographicdisplays.exception.CommandException;
import com.gmail.filoghost.holographicdisplays.util.Utils; import com.gmail.filoghost.holographicdisplays.util.Utils;
@ -35,13 +35,10 @@ import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention; import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention;
public class HelpCommand extends HologramSubCommand { public class HelpCommand extends HologramSubCommand {
private HologramsCommandHandler mainCommandHandler;
public HelpCommand(HologramsCommandHandler mainCommandHandler) { public HelpCommand() {
super("help"); super("help");
setPermission(Strings.BASE_PERM + "help"); setPermission(Strings.BASE_PERM + "help");
this.mainCommandHandler = mainCommandHandler;
} }
@Override @Override
@ -59,7 +56,7 @@ public class HelpCommand extends HologramSubCommand {
public void execute(CommandSender sender, String label, String[] args) throws CommandException { public void execute(CommandSender sender, String label, String[] args) throws CommandException {
sender.sendMessage(""); sender.sendMessage("");
sender.sendMessage(Strings.formatTitle("Holographic Displays Commands")); sender.sendMessage(Strings.formatTitle("Holographic Displays Commands"));
for (HologramSubCommand subCommand : mainCommandHandler.getSubCommands()) { for (HologramSubCommand subCommand : HolographicDisplays.getCommandHandler().getSubCommands()) {
if (subCommand.getType() == SubCommandType.GENERIC) { if (subCommand.getType() == SubCommandType.GENERIC) {
String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments() : ""); String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments() : "");
@ -90,12 +87,12 @@ public class HelpCommand extends HologramSubCommand {
public static void sendHoverTip(Player player) { public static void sendHoverTip(Player player) {
player.sendMessage(""); player.sendMessage("");
player.spigot().sendMessage(new ComponentBuilder("TIP").color(ChatColor.YELLOW).bold(true) player.spigot().sendMessage(new ComponentBuilder("TIP:").color(ChatColor.YELLOW).bold(true)
.append(" Try to ", FormatRetention.NONE).color(ChatColor.GRAY) .append(" Try to ", FormatRetention.NONE).color(ChatColor.GRAY)
.append("hover").color(ChatColor.WHITE).italic(true).underlined(true) .append("hover").color(ChatColor.WHITE).underlined(true)
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(ChatColor.LIGHT_PURPLE + "Hover on the commands to get info about them."))) .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(ChatColor.LIGHT_PURPLE + "Hover on the commands to get info about them.")))
.append(" or ", FormatRetention.NONE).color(ChatColor.GRAY) .append(" or ", FormatRetention.NONE).color(ChatColor.GRAY)
.append("click").color(ChatColor.WHITE).italic(true).underlined(true) .append("click").color(ChatColor.WHITE).underlined(true)
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(ChatColor.LIGHT_PURPLE + "Click on the commands to insert them in the chat."))) .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(ChatColor.LIGHT_PURPLE + "Click on the commands to insert them in the chat.")))
.append(" on the commands!", FormatRetention.NONE).color(ChatColor.GRAY) .append(" on the commands!", FormatRetention.NONE).color(ChatColor.GRAY)
.create()); .create());

View File

@ -60,6 +60,7 @@ public class InfoCommand extends HologramSubCommand {
for (CraftHologramLine line : hologram.getLinesUnsafe()) { for (CraftHologramLine line : hologram.getLinesUnsafe()) {
sender.sendMessage(Colors.SECONDARY + Colors.BOLD + (++index) + Colors.SECONDARY_SHADOW + ". " + Colors.SECONDARY + (line instanceof CraftTextLine ? ((CraftTextLine) line).getText() : HologramDatabase.saveLineToString(line))); sender.sendMessage(Colors.SECONDARY + Colors.BOLD + (++index) + Colors.SECONDARY_SHADOW + ". " + Colors.SECONDARY + (line instanceof CraftTextLine ? ((CraftTextLine) line).getText() : HologramDatabase.saveLineToString(line)));
} }
EditCommand.sendQuickEditCommands(sender, label, hologram.getName());
} }
@Override @Override

View File

@ -66,15 +66,17 @@ public class InsertlineCommand extends HologramSubCommand {
HologramDatabase.saveHologram(hologram); HologramDatabase.saveHologram(hologram);
HologramDatabase.trySaveToDisk(); HologramDatabase.trySaveToDisk();
Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram));
if (insertAfter == 0) { if (insertAfter == 0) {
sender.sendMessage(Colors.PRIMARY + "Line inserted before line n.1!"); sender.sendMessage(Colors.PRIMARY + "Line inserted before line n.1!");
} else if (insertAfter == oldLinesAmount) { } else if (insertAfter == oldLinesAmount) {
sender.sendMessage(Colors.PRIMARY + "Line appended at the end!"); sender.sendMessage(Colors.PRIMARY + "Line appended at the end!");
sender.sendMessage(Strings.TIP_PREFIX + "Next time use /" + label + " addline to add a line at the end."); sender.sendMessage(Strings.TIP_PREFIX + "Next time use \"/" + label + " addline\" to add a line at the end.");
} else { } else {
sender.sendMessage(Colors.PRIMARY + "Line inserted between lines " + insertAfter + " and " + (insertAfter + 1) + "!"); sender.sendMessage(Colors.PRIMARY + "Line inserted between lines " + insertAfter + " and " + (insertAfter + 1) + "!");
} }
Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); EditCommand.sendQuickEditCommands(sender, label, hologram.getName());
} }
@Override @Override

View File

@ -65,8 +65,10 @@ public class RemovelineCommand extends HologramSubCommand {
HologramDatabase.saveHologram(hologram); HologramDatabase.saveHologram(hologram);
HologramDatabase.trySaveToDisk(); HologramDatabase.trySaveToDisk();
sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " removed!");
Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram));
sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " removed!");
EditCommand.sendQuickEditCommands(sender, label, hologram.getName());
} }
@Override @Override

View File

@ -79,9 +79,11 @@ public class SetlineCommand extends HologramSubCommand {
HologramDatabase.saveHologram(hologram); HologramDatabase.saveHologram(hologram);
HologramDatabase.trySaveToDisk(); HologramDatabase.trySaveToDisk();
sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " changed!");
Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram));
sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " changed!");
EditCommand.sendQuickEditCommands(sender, label, hologram.getName());
} }
@Override @Override

View File

@ -5,6 +5,7 @@
#. #.
space-between-lines: 0.02 space-between-lines: 0.02
precise-hologram-movement: true precise-hologram-movement: true
quick-edit-commands: true
images: images:
symbol: '[x]' symbol: '[x]'
transparency: transparency: