mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2024-12-19 23:37:33 +01:00
Add copy command, better error reporting, small fix.
This commit is contained in:
parent
2f17ff632b
commit
07f722accc
@ -5,8 +5,6 @@ import org.bukkit.command.CommandSender;
|
||||
|
||||
public class Strings {
|
||||
|
||||
public static final String NO_SUCH_HOLOGRAM = ChatColor.RED + "A hologram with that name doesn't exist.";
|
||||
|
||||
public static final String BASE_PERM = "holograms.";
|
||||
|
||||
public static final String TIP_PREFIX = "" + ChatColor.YELLOW + ChatColor.BOLD + "TIP" + Colors.SECONDARY_SHADOW + " ";
|
||||
@ -16,6 +14,10 @@ public class Strings {
|
||||
return "" + Colors.PRIMARY_SHADOW + ChatColor.BOLD + "----- " + input + Colors.PRIMARY_SHADOW + ChatColor.BOLD + " -----";
|
||||
}
|
||||
|
||||
public static String noSuchHologram(String name) {
|
||||
return ChatColor.RED + "Cannot find a hologram named \"" + name + "\".";
|
||||
}
|
||||
|
||||
public static void sendWarning(CommandSender recipient, String warning) {
|
||||
recipient.sendMessage(ChatColor.RED + "( " + ChatColor.DARK_RED + ChatColor.BOLD + "!" + ChatColor.RED + " ) " + Colors.SECONDARY_SHADOW + warning);
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import com.gmail.filoghost.holographicdisplays.commands.Colors;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.Strings;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.main.subs.AddlineCommand;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.main.subs.AlignCommand;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.main.subs.CopyCommand;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.main.subs.CreateCommand;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.main.subs.DeleteCommand;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.main.subs.EditCommand;
|
||||
@ -47,6 +48,7 @@ public class HologramsCommandHandler implements CommandExecutor {
|
||||
registerSubCommand(new TeleportCommand());
|
||||
registerSubCommand(new MovehereCommand());
|
||||
registerSubCommand(new AlignCommand());
|
||||
registerSubCommand(new CopyCommand());
|
||||
registerSubCommand(new ReloadCommand());
|
||||
registerSubCommand(new FixCommand());
|
||||
|
||||
|
@ -37,7 +37,7 @@ public class AddlineCommand extends HologramSubCommand {
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase());
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM);
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase()));
|
||||
|
||||
hologram.getLinesUnsafe().add(HologramDatabase.readLineFromString(Utils.join(args, " ", 1, args.length), hologram));
|
||||
hologram.refreshAll();
|
||||
|
@ -37,8 +37,8 @@ public class AlignCommand extends HologramSubCommand {
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(args[1].toLowerCase());
|
||||
NamedHologram referenceHologram = NamedHologramManager.getHologram(args[2].toLowerCase());
|
||||
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM + " (hologram to align)");
|
||||
CommandValidator.notNull(referenceHologram, Strings.NO_SUCH_HOLOGRAM + " (reference hologram)");
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(args[1].toLowerCase()));
|
||||
CommandValidator.notNull(referenceHologram, Strings.noSuchHologram(args[2].toLowerCase()));
|
||||
|
||||
CommandValidator.isTrue(hologram != referenceHologram, "The hologram must not be the same!");
|
||||
|
||||
|
@ -0,0 +1,69 @@
|
||||
package com.gmail.filoghost.holographicdisplays.commands.main.subs;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.gmail.filoghost.holographicdisplays.commands.Colors;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.CommandValidator;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.Strings;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand;
|
||||
import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase;
|
||||
import com.gmail.filoghost.holographicdisplays.exception.CommandException;
|
||||
import com.gmail.filoghost.holographicdisplays.object.NamedHologram;
|
||||
import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager;
|
||||
import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine;
|
||||
|
||||
public class CopyCommand extends HologramSubCommand {
|
||||
|
||||
public CopyCommand() {
|
||||
super("copy");
|
||||
setPermission(Strings.BASE_PERM + "copy");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPossibleArguments() {
|
||||
return "<hologramToCopy> <intoHologram>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinimumArguments() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
|
||||
NamedHologram hologramToCopy = NamedHologramManager.getHologram(args[0].toLowerCase());
|
||||
NamedHologram intoHologram = NamedHologramManager.getHologram(args[1].toLowerCase());
|
||||
|
||||
CommandValidator.notNull(hologramToCopy, Strings.noSuchHologram(args[0].toLowerCase()));
|
||||
CommandValidator.notNull(intoHologram, Strings.noSuchHologram(args[1].toLowerCase()));
|
||||
|
||||
intoHologram.clearLines();
|
||||
for (CraftHologramLine line : hologramToCopy.getLinesUnsafe()) {
|
||||
String lineString = HologramDatabase.saveLineToString(line);
|
||||
intoHologram.getLinesUnsafe().add(HologramDatabase.readLineFromString(lineString, intoHologram));
|
||||
}
|
||||
|
||||
intoHologram.refreshAll();
|
||||
|
||||
HologramDatabase.saveHologram(intoHologram);
|
||||
HologramDatabase.trySaveToDisk();
|
||||
|
||||
sender.sendMessage(Colors.PRIMARY + "Hologram \"" + hologramToCopy.getName() + "\" copied into hologram \"" + intoHologram.getName() + "\"!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTutorial() {
|
||||
return Arrays.asList(
|
||||
"Copies the contents of a hologram into another one.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SubCommandType getType() {
|
||||
return SubCommandType.GENERIC;
|
||||
}
|
||||
|
||||
}
|
@ -14,16 +14,12 @@ import com.gmail.filoghost.holographicdisplays.commands.Strings;
|
||||
import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand;
|
||||
import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase;
|
||||
import com.gmail.filoghost.holographicdisplays.exception.CommandException;
|
||||
import com.gmail.filoghost.holographicdisplays.exception.InvalidCharactersException;
|
||||
import com.gmail.filoghost.holographicdisplays.object.NamedHologram;
|
||||
import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Utils;
|
||||
|
||||
public class CreateCommand extends HologramSubCommand {
|
||||
|
||||
private static final String VALID_HOLOGRAM_NAME_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
|
||||
|
||||
|
||||
public CreateCommand() {
|
||||
super("create");
|
||||
setPermission(Strings.BASE_PERM + "create");
|
||||
@ -43,14 +39,12 @@ public class CreateCommand extends HologramSubCommand {
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
Player player = CommandValidator.getPlayerSender(sender);
|
||||
String name = args[0];
|
||||
String name = args[0].toLowerCase();
|
||||
|
||||
if (!name.matches("[a-zA-Z_\\-]+")) {
|
||||
if (!name.matches("[a-zA-Z0-9_\\-]+")) {
|
||||
throw new CommandException("The name must contain only alphanumeric chars, underscores and hyphens.");
|
||||
}
|
||||
|
||||
name = name.toLowerCase();
|
||||
|
||||
CommandValidator.isTrue(!NamedHologramManager.isExistingHologram(name), "A hologram with that name already exists.");
|
||||
|
||||
Location spawnLoc = player.getLocation();
|
||||
@ -88,15 +82,6 @@ public class CreateCommand extends HologramSubCommand {
|
||||
}
|
||||
}
|
||||
|
||||
public static String validateName(String name) throws InvalidCharactersException {
|
||||
for (char c : name.toCharArray()) {
|
||||
if (VALID_HOLOGRAM_NAME_CHARS.indexOf(c) < 0) {
|
||||
throw new InvalidCharactersException(Character.toString(c));
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTutorial() {
|
||||
return Arrays.asList(
|
||||
|
@ -36,7 +36,7 @@ public class DeleteCommand extends HologramSubCommand {
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase());
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM);
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase()));
|
||||
|
||||
hologram.delete();
|
||||
NamedHologramManager.removeHologram(hologram);
|
||||
|
@ -43,7 +43,7 @@ public class EditCommand extends HologramSubCommand {
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
String name = args[0].toLowerCase();
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(name);
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM);
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(name));
|
||||
|
||||
sender.sendMessage("");
|
||||
sender.sendMessage(Strings.formatTitle("How to edit the hologram '" + name + "'"));
|
||||
|
@ -40,7 +40,7 @@ public class FixCommand extends HologramSubCommand {
|
||||
CommandValidator.isTrue(!HolographicDisplays.is1_8(), "This command is no longer necessary in 1.8+. The holograms already use the correct ambient light.");
|
||||
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase());
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM);
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase()));
|
||||
|
||||
if (args.length <= 1) {
|
||||
sender.sendMessage(Colors.PRIMARY + "This command will put a glowstone 16 blocks above the hologram to fix the lightning.");
|
||||
|
@ -39,7 +39,7 @@ public class InsertlineCommand extends HologramSubCommand {
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase());
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM);
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase()));
|
||||
|
||||
int insertAfter = CommandValidator.getInteger(args[1]);
|
||||
int oldLinesAmount = hologram.size();
|
||||
|
@ -40,7 +40,7 @@ public class MovehereCommand extends HologramSubCommand {
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
Player player = CommandValidator.getPlayerSender(sender);
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase());
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM);
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase()));
|
||||
|
||||
hologram.teleport(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());
|
||||
hologram.despawnEntities();
|
||||
|
@ -66,7 +66,7 @@ public class ReadimageCommand extends HologramSubCommand {
|
||||
args = newArgs.toArray(new String[0]);
|
||||
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase());
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM);
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase()));
|
||||
|
||||
int width = CommandValidator.getInteger(args[2]);
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class ReadtextCommand extends HologramSubCommand {
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase());
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM);
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase()));
|
||||
|
||||
try {
|
||||
List<String> lines = FileUtils.readLines(new File(HolographicDisplays.getInstance().getDataFolder(), args[1]));
|
||||
|
@ -37,7 +37,7 @@ public class RemovelineCommand extends HologramSubCommand {
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase());
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM);
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase()));
|
||||
|
||||
int lineNumber = CommandValidator.getInteger(args[1]);
|
||||
|
||||
|
@ -38,7 +38,7 @@ public class SetlineCommand extends HologramSubCommand {
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase());
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM);
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase()));
|
||||
|
||||
|
||||
int lineNumber = CommandValidator.getInteger(args[1]);
|
||||
|
@ -38,7 +38,7 @@ public class TeleportCommand extends HologramSubCommand {
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
Player player = CommandValidator.getPlayerSender(sender);
|
||||
NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase());
|
||||
CommandValidator.notNull(hologram, Strings.NO_SUCH_HOLOGRAM);
|
||||
CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase()));
|
||||
|
||||
Location loc = hologram.getLocation();
|
||||
loc.setPitch(90);
|
||||
|
@ -96,6 +96,19 @@ public class HologramDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
public static String saveLineToString(CraftHologramLine line) {
|
||||
if (line instanceof CraftTextLine) {
|
||||
return StringConverter.toSaveableFormat(((CraftTextLine) line).getText());
|
||||
|
||||
} else if (line instanceof CraftItemLine) {
|
||||
CraftItemLine itemLine = (CraftItemLine) line;
|
||||
return "ICON: " + itemLine.getItemStack().getType().toString().replace("_", " ").toLowerCase() + (itemLine.getItemStack().getDurability() != 0 ? String.valueOf(itemLine.getItemStack().getDurability()) : "");
|
||||
} else {
|
||||
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteHologram(String name) {
|
||||
config.set(name, null);
|
||||
}
|
||||
@ -109,13 +122,7 @@ public class HologramDatabase {
|
||||
|
||||
for (CraftHologramLine line : hologram.getLinesUnsafe()) {
|
||||
|
||||
if (line instanceof CraftTextLine) {
|
||||
lines.add(StringConverter.toSaveableFormat(((CraftTextLine) line).getText()));
|
||||
|
||||
} else if (line instanceof CraftItemLine) {
|
||||
CraftItemLine itemLine = (CraftItemLine) line;
|
||||
lines.add("ICON: " + itemLine.getItemStack().getType().toString().replace("_", " ").toLowerCase() + (itemLine.getItemStack().getDurability() != 0 ? String.valueOf(itemLine.getItemStack().getDurability()) : ""));
|
||||
}
|
||||
lines.add(saveLineToString(line));
|
||||
}
|
||||
|
||||
configSection.set("lines", lines);
|
||||
|
Loading…
Reference in New Issue
Block a user