Refactor exceptions

This commit is contained in:
filoghost 2021-02-07 16:18:16 +01:00
parent d71df75036
commit 40e1967d20
27 changed files with 223 additions and 194 deletions

View File

@ -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;
@ -85,12 +86,17 @@ public class Utils {
}
public static String uncapitalize(String str) {
if (str == null || str.isEmpty()) {
return str;
public static String formatExceptionMessage(Throwable t) {
return formatExceptionMessage(t.getMessage());
}
return Character.toLowerCase(str.charAt(0)) + str.substring(1);
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;
}
}

View File

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

View File

@ -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,7 +25,7 @@ public class HologramCommandValidate {
try {
return HologramLineParser.parseLine(hologram, serializedLine, validateMaterial);
} catch (HologramLineParseException e) {
throw new CommandException(e.getMessage());
throw new CommandException(Utils.formatExceptionMessage(e));
}
}

View File

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

View File

@ -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,11 +83,9 @@ public class ReadtextCommand extends LineEditingCommand {
HologramDatabase.saveHologram(hologram);
HologramDatabase.trySaveToDisk();
if (fileName.contains(".")) {
if (isImageExtension(fileName.substring(fileName.lastIndexOf('.') + 1))) {
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!");
Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(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());
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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(" ", ""));
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(parts[0].trim());
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");
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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()) : "<End of string>") + "'", this.str, this.index + 1);
throw new MojangsonParseException("expected '" + c + "' but got '" + (hasNext ? Character.valueOf(currentChar()) : "<End of string>") + "'", 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");
}
}

View File

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

View File

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

View File

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