Add copy command, better error reporting, small fix.

This commit is contained in:
filoghost 2014-12-30 19:33:25 +01:00
parent 2f17ff632b
commit 07f722accc
17 changed files with 104 additions and 39 deletions

View File

@ -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);
}

View File

@ -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());

View File

@ -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();

View File

@ -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!");

View File

@ -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;
}
}

View File

@ -14,15 +14,11 @@ 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");
@ -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();
@ -87,15 +81,6 @@ public class CreateCommand extends HologramSubCommand {
player.sendMessage(Colors.SECONDARY_SHADOW + "(You were on the ground, the hologram was automatically moved up. If you use /" + label + " movehere " + hologram.getName() + ", the hologram will be moved to your feet)");
}
}
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() {

View File

@ -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);

View File

@ -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 + "'"));

View File

@ -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.");

View File

@ -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();

View File

@ -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();

View File

@ -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]);

View File

@ -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]));

View File

@ -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]);

View File

@ -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]);

View File

@ -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);

View File

@ -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);