Skip to content

Commit

Permalink
Include coin.png as a plugin resource (#108)
Browse files Browse the repository at this point in the history
* Add coin.png to resources

* Add filepath support and fallback functionality for icon

* Default to empty and load coin.png only from resources

* Update comments on config node

* Use string variable instead of getting string again

* Change config node to force change

* Update errors to not say URL explicitly

* Get image once per mapper
  • Loading branch information
lexiccn authored Apr 4, 2024
1 parent 0280a58 commit f4856a9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 22 deletions.
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.

0 comments on commit f4856a9

Please sign in to comment.