Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include coin.png as a plugin resource #108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public DisplayProvincesOnBlueMapAction() {

@Override
void reloadAction() {
BufferedImage configIcon = TownyProvincesSettings.getTownCostsIcon();

if (TownyProvincesSettings.getTownCostsIcon() == null) {
throw new RuntimeException("Town Costs Icon URL is not a valid image link");
if (configIcon == null) {
throw new RuntimeException("Town Costs Icon is not a valid image");
}

BlueMapAPI.getInstance().ifPresent(e -> {
Path assetsFolder = e.getWebApp().getWebRoot().resolve("assets");
try (OutputStream out = Files.newOutputStream(assetsFolder.resolve("province.png"), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
BufferedImage configIcon = TownyProvincesSettings.getTownCostsIcon();
BufferedImage resizedIcon = new BufferedImage(
TownyProvincesSettings.getTownCostsIconWidth(),
TownyProvincesSettings.getTownCostsIconHeight(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.dynmap.markers.MarkerSet;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -45,8 +46,10 @@ public DisplayProvincesOnDynmapAction() {

@Override
void reloadAction() {
if (TownyProvincesSettings.getTownCostsIcon() == null) {
throw new RuntimeException("Town Costs Icon URL is not a valid image link");
BufferedImage configIcon = TownyProvincesSettings.getTownCostsIcon();

if (configIcon == null) {
throw new RuntimeException("Town Costs Icon is not a valid image");
}

final MarkerIcon oldMarkerIcon = markerapi.getMarkerIcon("provinces_costs_icon");
Expand All @@ -56,7 +59,7 @@ void reloadAction() {

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
ImageIO.write(TownyProvincesSettings.getTownCostsIcon(), "png", outputStream);
ImageIO.write(configIcon, "png", outputStream);
} catch (IOException ex) {
TownyProvinces.severe("Failed to write BlueMap Marker Icon as png file!");
throw new RuntimeException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.pl3x.map.core.markers.option.Stroke;
import net.pl3x.map.core.world.World;

import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;

Expand Down Expand Up @@ -127,12 +128,14 @@ private SimpleLayer createLayer(String layerKey, String layerName, boolean hideB

@Override
void reloadAction() {
if (TownyProvincesSettings.getTownCostsIcon() == null) {
throw new RuntimeException("Town Costs Icon URL is not a valid image link");
BufferedImage configIcon = TownyProvincesSettings.getTownCostsIcon();

if (configIcon == null) {
throw new RuntimeException("Town Costs Icon is not a valid image");
}

Pl3xMap.api().getIconRegistry().register(new IconImage(
"provinces_costs_icon", TownyProvincesSettings.getTownCostsIcon(), "png"));
"provinces_costs_icon", configIcon, "png"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,14 @@ public enum ConfigNodes {
"# | TOWN COSTS ICON | #",
"# +------------------------------------------------------+ #",
""),
MAP_TOWN_COSTS_ICON_URL(
"map_integration.town_costs_icon.url",
"https://cdn-icons-png.flaticon.com/512/9729/9729309.png", "",
"# Icon for the town costs. This must be a valid image URL.",
MAP_TOWN_COSTS_ICON_PATH(
"map_integration.town_costs_icon.path",
"", "",
"# Custom icon for the town costs. This must be a valid URL, or a filepath in TownyProvinces folder.",
"# Leave this empty to use the included default coin icon.",
"# Default coin icon created by Md Tanvirul Haque - Flaticon",
"# https://www.flaticon.com/free-icon/dollar_9729309"
),
),
MAP_TOWN_COSTS_ICON_HEIGHT(
"map_integration.town_costs_icon.height",
"35",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -256,20 +259,49 @@ public static boolean isBiomeCostAdjustmentsEnabled() {
public static double getBiomeCostAdjustmentsHotLand() { return Settings.getDouble(ConfigNodes.BIOME_COST_ADJUSTMENTS_HOT_LAND); }
public static double getBiomeCostAdjustmentsColdLand() { return Settings.getDouble(ConfigNodes.BIOME_COST_ADJUSTMENTS_COLD_LAND); }

public static @Nullable BufferedImage getTownCostsIcon() {
URL imageURL;
private static BufferedImage getFallbackTownCostsIcon() {
try {
imageURL = new URL(Settings.getString(ConfigNodes.MAP_TOWN_COSTS_ICON_URL));
return ImageIO.read(imageURL);
} catch (MalformedURLException e) {
TownyProvinces.severe("Error: Invalid Town Costs Icon URL in configuration file.");
return null;
InputStream imageStream = TownyProvinces.getPlugin().getResource("coin.png");
if (imageStream == null) {
TownyProvinces.severe("Error: Fallback Town Costs Icon is missing from plugin jar file.");
return null;
}
return ImageIO.read(imageStream);
} catch (IOException e) {
TownyProvinces.severe("Error: Failed to load Town Costs Icon from URL provided in configuration file.");
TownyProvinces.severe("Error: Failed to load fallback Town Costs Icon from plugin jar file.");
return null;
}
}

public static @Nullable BufferedImage getTownCostsIcon() {
String imageString = Settings.getString(ConfigNodes.MAP_TOWN_COSTS_ICON_PATH);
if (imageString.isEmpty()) return getFallbackTownCostsIcon();
if (imageString.startsWith("https://") || imageString.startsWith("http://")) {
URL imageURL;
try {
imageURL = new URL(imageString);
return ImageIO.read(imageURL);
} catch (MalformedURLException e) {
TownyProvinces.severe("Error: Invalid Town Costs Icon URL in configuration file.");
return getFallbackTownCostsIcon();
} catch (IOException e) {
TownyProvinces.severe("Error: Failed to load Town Costs Icon from URL provided in configuration file.");
return getFallbackTownCostsIcon();
}
} else {
try {
Path imagePath = TownyProvinces.getPlugin().getDataFolder().toPath().resolve(imageString);
return ImageIO.read(imagePath.toFile());
} catch (InvalidPathException e) {
TownyProvinces.severe("Error: Invalid Town Costs Icon filepath in configuration file.");
return getFallbackTownCostsIcon();
} catch (IOException e) {
TownyProvinces.severe("Error: Failed to load Town Costs Icon from filepath provided in configuration file.");
return getFallbackTownCostsIcon();
}
}
}

public static int getTownCostsIconWidth() { return Settings.getInt(ConfigNodes.MAP_TOWN_COSTS_ICON_WIDTH); }

public static int getTownCostsIconHeight() { return Settings.getInt(ConfigNodes.MAP_TOWN_COSTS_ICON_HEIGHT); }
Expand Down
Binary file added src/main/resources/coin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.