diff --git a/common/src/main/java/me/filoghost/holographicdisplays/common/Utils.java b/common/src/main/java/me/filoghost/holographicdisplays/common/Utils.java index 9b49f969..35973e07 100644 --- a/common/src/main/java/me/filoghost/holographicdisplays/common/Utils.java +++ b/common/src/main/java/me/filoghost/holographicdisplays/common/Utils.java @@ -6,6 +6,7 @@ package me.filoghost.holographicdisplays.common; import me.filoghost.fcommons.Preconditions; +import me.filoghost.fcommons.Strings; import java.util.ArrayList; import java.util.List; @@ -84,13 +85,18 @@ public class Utils { return false; } - - public static String uncapitalize(String str) { - if (str == null || str.isEmpty()) { - return str; - } - - return Character.toLowerCase(str.charAt(0)) + str.substring(1); + + public static String formatExceptionMessage(Throwable t) { + return formatExceptionMessage(t.getMessage()); } + public static String formatExceptionMessage(String message) { + message = Strings.capitalizeFirst(message); + char lastChar = message.charAt(message.length() - 1); + if (Character.isLetterOrDigit(lastChar)) { + message = message + "."; + } + return message; + } + } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandManager.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandManager.java index 42b969aa..dd542357 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandManager.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandManager.java @@ -30,6 +30,7 @@ import me.filoghost.holographicdisplays.commands.subs.ReloadCommand; import me.filoghost.holographicdisplays.commands.subs.RemovelineCommand; import me.filoghost.holographicdisplays.commands.subs.SetlineCommand; import me.filoghost.holographicdisplays.commands.subs.TeleportCommand; +import me.filoghost.holographicdisplays.common.Utils; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -133,6 +134,11 @@ public class HologramCommandManager extends SubCommandManager { + context.getSubLabel() + " " + context.getSubCommand().getUsageArgs()); } + @Override + protected void sendExecutionErrorMessage(CommandContext context, String errorMessage) { + context.getSender().sendMessage(Colors.ERROR + Utils.formatExceptionMessage(errorMessage)); + } + @Override protected void handleUnexpectedException(CommandContext context, Throwable t) { Bukkit.getLogger().log(Level.SEVERE, "Unhandled exception while executing /" + context.getRootLabel(), t); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java index ce655c65..13d4bb8d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java @@ -8,8 +8,9 @@ package me.filoghost.holographicdisplays.commands; import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.HolographicDisplays; +import me.filoghost.holographicdisplays.common.Utils; import me.filoghost.holographicdisplays.disk.HologramLineParser; -import me.filoghost.holographicdisplays.exception.HologramLineParseException; +import me.filoghost.holographicdisplays.disk.HologramLineParseException; import me.filoghost.holographicdisplays.object.NamedHologram; import me.filoghost.holographicdisplays.object.NamedHologramManager; import me.filoghost.holographicdisplays.object.line.CraftHologramLine; @@ -24,10 +25,10 @@ public class HologramCommandValidate { try { return HologramLineParser.parseLine(hologram, serializedLine, validateMaterial); } catch (HologramLineParseException e) { - throw new CommandException(e.getMessage()); + throw new CommandException(Utils.formatExceptionMessage(e)); } } - + public static NamedHologram getNamedHologram(String hologramName) throws CommandException { NamedHologram hologram = NamedHologramManager.getHologram(hologramName); CommandValidate.notNull(hologram, "Cannot find a hologram named \"" + hologramName + "\"."); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadimageCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadimageCommand.java index 47e7abd3..5a8be068 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadimageCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadimageCommand.java @@ -14,12 +14,12 @@ import me.filoghost.holographicdisplays.commands.HologramCommandValidate; import me.filoghost.holographicdisplays.commands.Messages; import me.filoghost.holographicdisplays.disk.HologramDatabase; import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import me.filoghost.holographicdisplays.exception.TooWideException; -import me.filoghost.holographicdisplays.exception.UnreadableImageException; +import me.filoghost.holographicdisplays.image.ImageReader; +import me.filoghost.holographicdisplays.image.ImageTooWideException; +import me.filoghost.holographicdisplays.image.ImageReadException; import me.filoghost.holographicdisplays.image.ImageMessage; import me.filoghost.holographicdisplays.object.NamedHologram; import me.filoghost.holographicdisplays.object.line.CraftTextLine; -import me.filoghost.holographicdisplays.util.FileUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -91,7 +91,7 @@ public class ReadimageCommand extends LineEditingCommand { if (fileName.startsWith("http://") || fileName.startsWith("https://")) { isUrl = true; - image = FileUtils.readImage(new URL(fileName)); + image = ImageReader.readImage(new URL(fileName)); } else { if (fileName.matches(".*[a-zA-Z0-9\\-]+\\.[a-zA-Z0-9\\-]{1,4}\\/.+")) { @@ -99,7 +99,7 @@ public class ReadimageCommand extends LineEditingCommand { } Path targetImage = HologramCommandValidate.getUserReadableFile(fileName); - image = FileUtils.readImage(targetImage); + image = ImageReader.readImage(targetImage); } if (!append) { @@ -132,9 +132,9 @@ public class ReadimageCommand extends LineEditingCommand { } catch (MalformedURLException e) { throw new CommandException("The provided URL was not valid."); - } catch (TooWideException e) { + } catch (ImageTooWideException e) { throw new CommandException("The image is too large. Max width allowed is " + ImageMessage.MAX_WIDTH + " pixels."); - } catch (UnreadableImageException e) { + } catch (ImageReadException e) { throw new CommandException("The plugin was unable to read the image. Be sure that the format is supported."); } catch (IOException e) { e.printStackTrace(); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadtextCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadtextCommand.java index 5139c13d..c0c8ec10 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadtextCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadtextCommand.java @@ -11,13 +11,13 @@ import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.holographicdisplays.Colors; import me.filoghost.holographicdisplays.commands.HologramCommandValidate; import me.filoghost.holographicdisplays.commands.Messages; -import me.filoghost.holographicdisplays.common.Utils; import me.filoghost.holographicdisplays.disk.HologramDatabase; +import me.filoghost.holographicdisplays.disk.HologramLineParseException; import me.filoghost.holographicdisplays.disk.HologramLineParser; import me.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import me.filoghost.holographicdisplays.exception.HologramLineParseException; import me.filoghost.holographicdisplays.object.NamedHologram; import me.filoghost.holographicdisplays.object.line.CraftHologramLine; +import me.filoghost.holographicdisplays.util.FileUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -72,7 +72,7 @@ public class ReadtextCommand extends LineEditingCommand { CraftHologramLine line = HologramLineParser.parseLine(hologram, serializedLines.get(i), true); linesToAdd.add(line); } catch (HologramLineParseException e) { - throw new CommandException("Error at line " + (i + 1) + ": " + Utils.uncapitalize(e.getMessage())); + throw new CommandException("Error at line " + (i + 1) + ": " + e.getMessage()); } } @@ -83,10 +83,8 @@ public class ReadtextCommand extends LineEditingCommand { HologramDatabase.saveHologram(hologram); HologramDatabase.trySaveToDisk(); - if (fileName.contains(".")) { - if (isImageExtension(fileName.substring(fileName.lastIndexOf('.') + 1))) { - Messages.sendWarning(sender, "The read file has an image's extension. If it is an image, you should use /" + context.getRootLabel() + " readimage."); - } + if (isImageExtension(FileUtils.getExtension(fileName))) { + Messages.sendWarning(sender, "The read file has an image's extension. If it is an image, you should use /" + context.getRootLabel() + " readimage."); } sender.sendMessage(Colors.PRIMARY + "The lines were pasted into the hologram!"); @@ -97,8 +95,8 @@ public class ReadtextCommand extends LineEditingCommand { } } - private boolean isImageExtension(String input) { - return Arrays.asList("jpg", "png", "jpeg", "gif").contains(input.toLowerCase()); + private boolean isImageExtension(String extension) { + return Arrays.asList("jpg", "png", "jpeg", "gif").contains(extension.toLowerCase()); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReloadCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReloadCommand.java index ce47eb4f..0bfefd2b 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReloadCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReloadCommand.java @@ -15,12 +15,9 @@ import me.filoghost.holographicdisplays.commands.Messages; import me.filoghost.holographicdisplays.common.Utils; import me.filoghost.holographicdisplays.disk.Configuration; import me.filoghost.holographicdisplays.disk.HologramDatabase; +import me.filoghost.holographicdisplays.disk.HologramLoadException; import me.filoghost.holographicdisplays.disk.UnicodeSymbols; import me.filoghost.holographicdisplays.event.HolographicDisplaysReloadEvent; -import me.filoghost.holographicdisplays.exception.HologramLineParseException; -import me.filoghost.holographicdisplays.exception.HologramNotFoundException; -import me.filoghost.holographicdisplays.exception.InvalidFormatException; -import me.filoghost.holographicdisplays.exception.WorldNotFoundException; import me.filoghost.holographicdisplays.object.CraftHologram; import me.filoghost.holographicdisplays.object.NamedHologram; import me.filoghost.holographicdisplays.object.NamedHologramManager; @@ -64,14 +61,8 @@ public class ReloadCommand extends HologramSubCommand { try { NamedHologram singleHologramEntity = HologramDatabase.loadHologram(singleSavedHologram); NamedHologramManager.addHologram(singleHologramEntity); - } catch (HologramNotFoundException e) { - Messages.sendWarning(sender, "Hologram '" + singleSavedHologram + "' not found, skipping it."); - } catch (InvalidFormatException e) { - Messages.sendWarning(sender, "Hologram '" + singleSavedHologram + "' has an invalid location format."); - } catch (HologramLineParseException e) { - Messages.sendWarning(sender, "Hologram '" + singleSavedHologram + "' has an invalid line: " + Utils.uncapitalize(e.getMessage())); - } catch (WorldNotFoundException e) { - Messages.sendWarning(sender, "Hologram '" + singleSavedHologram + "' was in the world '" + e.getMessage() + "' but it wasn't loaded."); + } catch (HologramLoadException e) { + Messages.sendWarning(sender, Utils.formatExceptionMessage(e)); } } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramDatabase.java b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramDatabase.java index 1f1ccab0..0bdf66e5 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramDatabase.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramDatabase.java @@ -6,10 +6,6 @@ package me.filoghost.holographicdisplays.disk; import me.filoghost.fcommons.logging.Log; -import me.filoghost.holographicdisplays.exception.HologramLineParseException; -import me.filoghost.holographicdisplays.exception.HologramNotFoundException; -import me.filoghost.holographicdisplays.exception.InvalidFormatException; -import me.filoghost.holographicdisplays.exception.WorldNotFoundException; import me.filoghost.holographicdisplays.object.NamedHologram; import me.filoghost.holographicdisplays.object.line.CraftHologramLine; import org.bukkit.Location; @@ -40,26 +36,30 @@ public class HologramDatabase { config = YamlConfiguration.loadConfiguration(file); } - public static NamedHologram loadHologram(String name) throws HologramNotFoundException, InvalidFormatException, WorldNotFoundException, HologramLineParseException { + public static NamedHologram loadHologram(String name) throws HologramNotFoundException, LocationFormatException, LocationWorldNotLoadedException, HologramLineParseException { ConfigurationSection configSection = config.getConfigurationSection(name); if (configSection == null) { - throw new HologramNotFoundException(); + throw new HologramNotFoundException("hologram \"" + name + "\" not found, skipping it"); } List lines = configSection.getStringList("lines"); String locationString = configSection.getString("location"); if (lines == null || locationString == null || lines.size() == 0) { - throw new HologramNotFoundException(); + throw new HologramNotFoundException("hologram \"" + name + "\" was found, but it contained no lines"); } - Location loc = LocationSerializer.locationFromString(locationString); + Location loc = LocationSerializer.locationFromString(name, locationString); NamedHologram hologram = new NamedHologram(loc, name); for (String line : lines) { - hologram.getLinesUnsafe().add(HologramLineParser.parseLine(hologram, line, false)); + try { + hologram.getLinesUnsafe().add(HologramLineParser.parseLine(hologram, line, false)); + } catch (HologramLineParseException e) { + throw new HologramLineParseException("hologram \"" + hologram.getName() + "\" has an invalid line: " + e.getMessage(), e.getCause()); + } } return hologram; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/HologramLineParseException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLineParseException.java similarity index 71% rename from plugin/src/main/java/me/filoghost/holographicdisplays/exception/HologramLineParseException.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLineParseException.java index ef35981d..864a273b 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/HologramLineParseException.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLineParseException.java @@ -3,9 +3,9 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.exception; +package me.filoghost.holographicdisplays.disk; -public class HologramLineParseException extends Exception { +public class HologramLineParseException extends HologramLoadException { public HologramLineParseException(String message) { super(message); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLineParser.java b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLineParser.java index e65040ff..777c97ef 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLineParser.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLineParser.java @@ -7,8 +7,6 @@ package me.filoghost.holographicdisplays.disk; import me.filoghost.fcommons.MaterialsHelper; import me.filoghost.fcommons.Strings; -import me.filoghost.holographicdisplays.common.Utils; -import me.filoghost.holographicdisplays.exception.HologramLineParseException; import me.filoghost.holographicdisplays.nbt.parser.MojangsonParseException; import me.filoghost.holographicdisplays.nbt.parser.MojangsonParser; import me.filoghost.holographicdisplays.object.NamedHologram; @@ -16,7 +14,6 @@ import me.filoghost.holographicdisplays.object.line.CraftHologramLine; import me.filoghost.holographicdisplays.object.line.CraftItemLine; import me.filoghost.holographicdisplays.object.line.CraftTextLine; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; @@ -72,7 +69,7 @@ public class HologramLineParser { try { dataValue = (short) Integer.parseInt(materialAndDataValue[1]); } catch (NumberFormatException e) { - throw new HologramLineParseException("Data value \"" + materialAndDataValue[1] + "\" is not a valid number."); + throw new HologramLineParseException("data value \"" + materialAndDataValue[1] + "\" is not a valid number"); } materialName = materialAndDataValue[0]; } else { @@ -82,7 +79,7 @@ public class HologramLineParser { Material material = MaterialsHelper.matchMaterial(materialName); if (material == null) { if (checkMaterialValidity) { - throw new HologramLineParseException("\"" + materialName + "\" is not a valid material."); + throw new HologramLineParseException("\"" + materialName + "\" is not a valid material"); } material = Material.BEDROCK; } @@ -95,9 +92,9 @@ public class HologramLineParser { MojangsonParser.parse(nbtString); Bukkit.getUnsafe().modifyItemStack(itemStack, nbtString); } catch (MojangsonParseException e) { - throw new HologramLineParseException("Invalid NBT data, " + Utils.uncapitalize(ChatColor.stripColor(e.getMessage()))); + throw new HologramLineParseException("invalid NBT data, " + e.getMessage()); } catch (Throwable t) { - throw new HologramLineParseException("Unexpected exception while parsing NBT data.", t); + throw new HologramLineParseException("unexpected exception while parsing NBT data", t); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLoadException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLoadException.java new file mode 100644 index 00000000..cca33f3f --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLoadException.java @@ -0,0 +1,18 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.disk; + +public class HologramLoadException extends Exception { + + public HologramLoadException(String message) { + super(message); + } + + public HologramLoadException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramNotFoundException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramNotFoundException.java new file mode 100644 index 00000000..b54a04d3 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramNotFoundException.java @@ -0,0 +1,14 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.disk; + +public class HologramNotFoundException extends HologramLoadException { + + public HologramNotFoundException(String message) { + super(message); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/LocationFormatException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/LocationFormatException.java new file mode 100644 index 00000000..072d3714 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/LocationFormatException.java @@ -0,0 +1,14 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.disk; + +public class LocationFormatException extends HologramLoadException { + + public LocationFormatException(String message) { + super(message); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/LocationSerializer.java b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/LocationSerializer.java index cd843c6d..2c9ba861 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/LocationSerializer.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/LocationSerializer.java @@ -5,8 +5,7 @@ */ package me.filoghost.holographicdisplays.disk; -import me.filoghost.holographicdisplays.exception.InvalidFormatException; -import me.filoghost.holographicdisplays.exception.WorldNotFoundException; +import me.filoghost.fcommons.Strings; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; @@ -19,31 +18,35 @@ public class LocationSerializer { private static DecimalFormat numberFormat = new DecimalFormat("0.000", DecimalFormatSymbols.getInstance(Locale.ROOT)); - public static Location locationFromString(String input) throws WorldNotFoundException, InvalidFormatException { - if (input == null) { - throw new InvalidFormatException(); + public static Location locationFromString(String hologramName, String serializedLocation) throws LocationWorldNotLoadedException, LocationFormatException { + if (serializedLocation == null) { + throw new LocationFormatException("hologram \"" + hologramName + "\" doesn't have a location set"); } - - String[] parts = input.split(","); - + + String[] parts = Strings.splitAndTrim(serializedLocation, ","); + if (parts.length != 4) { - throw new InvalidFormatException(); + throw new LocationFormatException("hologram \"" + hologramName + "\" has an invalid location format:" + + " it must be \"world, x, y, z\""); } try { - double x = Double.parseDouble(parts[1].replace(" ", "")); - double y = Double.parseDouble(parts[2].replace(" ", "")); - double z = Double.parseDouble(parts[3].replace(" ", "")); - - World world = Bukkit.getWorld(parts[0].trim()); + String worldName = parts[0]; + double x = Double.parseDouble(parts[1]); + double y = Double.parseDouble(parts[2]); + double z = Double.parseDouble(parts[3]); + + World world = Bukkit.getWorld(worldName); if (world == null) { - throw new WorldNotFoundException(parts[0].trim()); + throw new LocationWorldNotLoadedException("hologram \"" + hologramName + "\"" + + " was in the world \"" + worldName + "\" but it wasn't loaded"); } return new Location(world, x, y, z); } catch (NumberFormatException ex) { - throw new InvalidFormatException(); + throw new LocationFormatException("hologram \"" + hologramName + "\"" + + " has an invalid location format: invalid number"); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/LocationWorldNotLoadedException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/LocationWorldNotLoadedException.java new file mode 100644 index 00000000..c6877d72 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/LocationWorldNotLoadedException.java @@ -0,0 +1,14 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.disk; + +public class LocationWorldNotLoadedException extends HologramLoadException { + + public LocationWorldNotLoadedException(String message) { + super(message); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/HologramNotFoundException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/exception/HologramNotFoundException.java deleted file mode 100644 index 6cff933c..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/HologramNotFoundException.java +++ /dev/null @@ -1,10 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.exception; - -public class HologramNotFoundException extends Exception { - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/InvalidFormatException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/exception/InvalidFormatException.java deleted file mode 100644 index 4f7a3c6b..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/InvalidFormatException.java +++ /dev/null @@ -1,10 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.exception; - -public class InvalidFormatException extends Exception { - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/TooWideException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/exception/TooWideException.java deleted file mode 100644 index 3ebc149d..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/TooWideException.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.exception; - -public class TooWideException extends Exception { - - private int width; - - public TooWideException(int width) { - this.width = width; - } - - public int getWidth() { - return width; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/UnreadableImageException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/exception/UnreadableImageException.java deleted file mode 100644 index 32416a39..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/UnreadableImageException.java +++ /dev/null @@ -1,10 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.exception; - -public class UnreadableImageException extends Exception { - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/WorldNotFoundException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/exception/WorldNotFoundException.java deleted file mode 100644 index 15f0fddb..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/exception/WorldNotFoundException.java +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.exception; - -public class WorldNotFoundException extends Exception { - - public WorldNotFoundException(String message) { - super(message); - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageMessage.java b/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageMessage.java index f391368c..eaccf24a 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageMessage.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageMessage.java @@ -6,7 +6,6 @@ package me.filoghost.holographicdisplays.image; import me.filoghost.holographicdisplays.disk.Configuration; -import me.filoghost.holographicdisplays.exception.TooWideException; import org.bukkit.ChatColor; import java.awt.*; @@ -49,12 +48,12 @@ public class ImageMessage { private String[] lines; - public ImageMessage(BufferedImage image, int width) throws TooWideException { + public ImageMessage(BufferedImage image, int width) throws ImageTooWideException { ChatColor[][] chatColors = toChatColorArray(image, width); lines = toImgMessage(chatColors); } - private ChatColor[][] toChatColorArray(BufferedImage image, int width) throws TooWideException { + private ChatColor[][] toChatColorArray(BufferedImage image, int width) throws ImageTooWideException { double ratio = (double) image.getHeight() / image.getWidth(); int height = (int) ((width) * ratio); if (height == 0) { @@ -62,7 +61,7 @@ public class ImageMessage { } if (width > MAX_WIDTH) { - throw new TooWideException(width); + throw new ImageTooWideException(); } BufferedImage resized = resizeImage(image, width, height); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageReadException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageReadException.java new file mode 100644 index 00000000..917909d0 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageReadException.java @@ -0,0 +1,10 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.image; + +public class ImageReadException extends Exception { + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageReader.java b/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageReader.java new file mode 100644 index 00000000..5ab636a9 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageReader.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.image; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; + +public class ImageReader { + + public static BufferedImage readImage(Path file) throws ImageReadException, IOException { + BufferedImage image = ImageIO.read(Files.newInputStream(file)); + + if (image == null) { + throw new ImageReadException(); + } + + return image; + } + + public static BufferedImage readImage(URL url) throws ImageReadException, IOException { + BufferedImage image = ImageIO.read(url); + + if (image == null) { + throw new ImageReadException(); + } + + return image; + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageTooWideException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageTooWideException.java new file mode 100644 index 00000000..dfd2cead --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageTooWideException.java @@ -0,0 +1,10 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.image; + +public class ImageTooWideException extends Exception { + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/nbt/parser/MojangsonParser.java b/plugin/src/main/java/me/filoghost/holographicdisplays/nbt/parser/MojangsonParser.java index 4279272d..2912205d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/nbt/parser/MojangsonParser.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/nbt/parser/MojangsonParser.java @@ -59,7 +59,7 @@ public final class MojangsonParser { private String parseCompoundKey() throws MojangsonParseException { skipWhitespace(); if (!hasNext()) { - throw parseException("Expected key"); + throw parseException("expected key"); } return currentChar() == '"' ? parseQuotedString() : parseSimpleString(); } @@ -70,7 +70,7 @@ public final class MojangsonParser { return new NBTString(parseQuotedString()); String str = parseSimpleString(); if (str.isEmpty()) - throw parseException("Expected value"); + throw parseException("expected value"); return parseLiteral(str); } @@ -118,7 +118,7 @@ public final class MojangsonParser { char c = nextChar(); if (escape) { if ((c != '\\') && (c != '"')) { - throw parseException("Invalid escape of '" + c + "'"); + throw parseException("invalid escape of '" + c + "'"); } escape = false; } else { @@ -138,7 +138,7 @@ public final class MojangsonParser { builder.append(c); } } - throw parseException("Missing termination quote"); + throw parseException("missing termination quote"); } private String parseSimpleString() { @@ -152,7 +152,7 @@ public final class MojangsonParser { private NBTTag parseAnything() throws MojangsonParseException { skipWhitespace(); if (!hasNext()) - throw parseException("Expected value"); + throw parseException("expected value"); int c = currentChar(); if (c == '{') @@ -179,7 +179,7 @@ public final class MojangsonParser { while ((hasNext()) && (currentChar() != '}')) { String str = parseCompoundKey(); if (str.isEmpty()) { - throw parseException("Expected non-empty key"); + throw parseException("expected non-empty key"); } expectChar(':'); @@ -188,7 +188,7 @@ public final class MojangsonParser { break; } if (!hasNext()) { - throw parseException("Expected key"); + throw parseException("expected key"); } } expectChar('}'); @@ -201,7 +201,7 @@ public final class MojangsonParser { skipWhitespace(); if (!hasNext()) { - throw parseException("Expected value"); + throw parseException("expected value"); } NBTList list = new NBTList(); NBTType listType = null; @@ -213,14 +213,14 @@ public final class MojangsonParser { if (listType == null) { listType = elementType; } else if (elementType != listType) { - throw parseException("Unable to insert " + elementType + " into ListTag of type " + listType); + throw parseException("unable to insert " + elementType + " into ListTag of type " + listType); } list.add(element); if (!advanceToNextArrayElement()) { break; } if (!hasNext()) { - throw parseException("Expected value"); + throw parseException("expected value"); } } expectChar(']'); @@ -236,7 +236,7 @@ public final class MojangsonParser { skipWhitespace(); if (!hasNext()) { - throw parseException("Expected value"); + throw parseException("expected value"); } if (arrayType == 'B') return new NBTByteArray(parseNumArray(NBTType.BYTE_ARRAY, NBTType.BYTE)); @@ -244,7 +244,7 @@ public final class MojangsonParser { return new NBTLongArray(parseNumArray(NBTType.LONG_ARRAY, NBTType.LONG)); else if (arrayType == 'I') return new NBTIntArray(parseNumArray(NBTType.INT_ARRAY, NBTType.INT)); - throw parseException("Invalid array type '" + arrayType + "' found"); + throw parseException("invalid array type '" + arrayType + "' found"); } private Number[] parseNumArray(NBTType arrayType, NBTType primType) throws MojangsonParseException { @@ -254,7 +254,7 @@ public final class MojangsonParser { NBTType elementType = element.getType(); if (elementType != primType) { - throw parseException("Unable to insert " + elementType + " into " + arrayType); + throw parseException("unable to insert " + elementType + " into " + arrayType); } if (primType == NBTType.BYTE) { result.add(((NBTByte) element).getValue()); @@ -267,7 +267,7 @@ public final class MojangsonParser { break; } if (!hasNext()) { - throw parseException("Expected value"); + throw parseException("expected value"); } } expectChar(']'); @@ -347,7 +347,7 @@ public final class MojangsonParser { this.index += 1; return; } - throw new MojangsonParseException("Expected '" + c + "' but got '" + (hasNext ? Character.valueOf(currentChar()) : "") + "'", this.str, this.index + 1); + throw new MojangsonParseException("expected '" + c + "' but got '" + (hasNext ? Character.valueOf(currentChar()) : "") + "'", this.str, this.index + 1); } /** @@ -359,7 +359,7 @@ public final class MojangsonParser { skipWhitespace(); if (hasNext()) { this.index++; - throw parseException("Trailing data found"); + throw parseException("trailing data found"); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/RelativePlaceholder.java b/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/RelativePlaceholder.java index 24eb6226..4dbbbdb8 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/RelativePlaceholder.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/placeholder/RelativePlaceholder.java @@ -30,7 +30,7 @@ public abstract class RelativePlaceholder { public static void register(RelativePlaceholder relativePlaceholder) { for (RelativePlaceholder existingPlaceholder : registry) { if (existingPlaceholder.getTextPlaceholder().equals(relativePlaceholder.getTextPlaceholder())) { - throw new IllegalArgumentException("Relative placeholder already registered."); + throw new IllegalArgumentException("Relative placeholder already registered"); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/task/StartupLoadHologramsTask.java b/plugin/src/main/java/me/filoghost/holographicdisplays/task/StartupLoadHologramsTask.java index 9ae973e2..503cb8f6 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/task/StartupLoadHologramsTask.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/task/StartupLoadHologramsTask.java @@ -6,11 +6,9 @@ package me.filoghost.holographicdisplays.task; import me.filoghost.fcommons.logging.Log; +import me.filoghost.holographicdisplays.common.Utils; import me.filoghost.holographicdisplays.disk.HologramDatabase; -import me.filoghost.holographicdisplays.exception.HologramLineParseException; -import me.filoghost.holographicdisplays.exception.HologramNotFoundException; -import me.filoghost.holographicdisplays.exception.InvalidFormatException; -import me.filoghost.holographicdisplays.exception.WorldNotFoundException; +import me.filoghost.holographicdisplays.disk.HologramLoadException; import me.filoghost.holographicdisplays.object.NamedHologram; import me.filoghost.holographicdisplays.object.NamedHologramManager; @@ -27,16 +25,11 @@ public class StartupLoadHologramsTask implements Runnable { NamedHologram namedHologram = HologramDatabase.loadHologram(hologramName); NamedHologramManager.addHologram(namedHologram); namedHologram.refreshAll(); - } catch (HologramNotFoundException e) { - Log.warning("Hologram '" + hologramName + "' not found, skipping it."); - } catch (InvalidFormatException e) { - Log.warning("Hologram '" + hologramName + "' has an invalid location format."); - } catch (WorldNotFoundException e) { - Log.warning("Hologram '" + hologramName + "' was in the world '" + e.getMessage() + "' but it wasn't loaded."); - } catch (HologramLineParseException e) { - Log.warning("Hologram '" + hologramName + "' has an invalid line: " + e.getMessage()); + } catch (HologramLoadException e) { + Log.warning(Utils.formatExceptionMessage(e)); } catch (Exception e) { - Log.warning("Unhandled exception while loading the hologram '" + hologramName + "'. Please contact the developer.", e); + Log.warning("Unexpected exception while loading the hologram \"" + hologramName + "\"." + + " Please contact the developer.", e); } } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/util/FileUtils.java b/plugin/src/main/java/me/filoghost/holographicdisplays/util/FileUtils.java index 7025ed5f..24415bd8 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/util/FileUtils.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/util/FileUtils.java @@ -5,36 +5,18 @@ */ package me.filoghost.holographicdisplays.util; -import me.filoghost.holographicdisplays.exception.UnreadableImageException; - -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; -import java.io.IOException; -import java.net.URL; -import java.nio.file.Files; import java.nio.file.Path; public class FileUtils { - public static BufferedImage readImage(Path file) throws UnreadableImageException, IOException { - BufferedImage image = ImageIO.read(Files.newInputStream(file)); - - if (image == null) { - throw new UnreadableImageException(); + public static String getExtension(String fileName) { + int lastFullStop = fileName.lastIndexOf('.'); + if (lastFullStop >= 0) { + return fileName.substring(lastFullStop + 1); + } else { + return ""; } - - return image; - } - - public static BufferedImage readImage(URL url) throws UnreadableImageException, IOException { - BufferedImage image = ImageIO.read(url); - - if (image == null) { - throw new UnreadableImageException(); - } - - return image; } public static boolean isInsideDirectory(Path file, Path directory) {