From be2b69e30cf2a5af57b71f804af831a268e52a69 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sat, 11 Jun 2022 10:05:01 +0000 Subject: [PATCH] 0.3 --- pom.xml | 2 +- .../minecon724/realweather/ConfigUtils.java | 6 ++-- .../minecon724/realweather/GetStateTask.java | 31 +++++++++++++------ .../java/pl/minecon724/realweather/RW.java | 7 +++-- .../minecon724/realweather/WeatherState.java | 2 +- src/main/resources/config.yml | 2 -- 6 files changed, 32 insertions(+), 18 deletions(-) diff --git a/pom.xml b/pom.xml index 1bf810d..1147d22 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 pl.minecon724 realweather - 0.2.0 + 0.3.0 17 diff --git a/src/main/java/pl/minecon724/realweather/ConfigUtils.java b/src/main/java/pl/minecon724/realweather/ConfigUtils.java index 571e206..44aea5d 100644 --- a/src/main/java/pl/minecon724/realweather/ConfigUtils.java +++ b/src/main/java/pl/minecon724/realweather/ConfigUtils.java @@ -1,9 +1,11 @@ package pl.minecon724.realweather; +import net.md_5.bungee.api.ChatColor; + public class ConfigUtils { - public static String parsePlaceholders(String key, String value, Object[] data) { + public static String parsePlaceholders(String key, String value, String[] data) { if (key.equals("messages.actionbarInfo")) { - value = value.replaceAll('%weather_full%', data[0]).replaceAll('%weather%', data[1]); + value = value.replaceAll("%weather_full%", data[0] + " " + data[1]).replaceAll("%weather%", data[1]); } return value; } diff --git a/src/main/java/pl/minecon724/realweather/GetStateTask.java b/src/main/java/pl/minecon724/realweather/GetStateTask.java index b81d1fe..c4556ea 100644 --- a/src/main/java/pl/minecon724/realweather/GetStateTask.java +++ b/src/main/java/pl/minecon724/realweather/GetStateTask.java @@ -9,6 +9,9 @@ import com.maxmind.geoip2.exception.AddressNotFoundException; import com.maxmind.geoip2.model.CityResponse; import com.maxmind.geoip2.record.Location; +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; + import org.bukkit.Bukkit; import org.bukkit.WeatherType; import org.bukkit.World; @@ -27,11 +30,11 @@ public class GetStateTask extends BukkitRunnable { List worlds; Logger logger; WebServiceClient client; - boolean broadcast; boolean debug, debugDox; double scaleLat, scaleLon; int onExceed; boolean actionbar; + String actionbarInfo; MapUtils mapUtils = new MapUtils(); @@ -39,10 +42,11 @@ public class GetStateTask extends BukkitRunnable { Provider provider, String source, double pointLatitude, double pointLongitude, List worlds, Logger logger, - WebServiceClient client, boolean broadcast, + WebServiceClient client, boolean debug, boolean debugDox, double scaleLat, double scaleLon, - int onExceed, boolean actionbar + int onExceed, boolean actionbar, + String actionbarInfo ) { this.provider = provider; this.source = source; @@ -51,13 +55,13 @@ public class GetStateTask extends BukkitRunnable { this.worlds = worlds; this.logger = logger; this.client = client; - this.broadcast = broadcast; this.debug = debug; this.debugDox = debugDox; this.scaleLat = scaleLat; this.scaleLon = scaleLon; this.onExceed = onExceed; this.actionbar = actionbar; + this.actionbarInfo = actionbarInfo; } // That's a lot of variables @@ -75,8 +79,9 @@ public class GetStateTask extends BukkitRunnable { world.setStorm(state.simple == ConditionSimple.CLEAR ? false : true); } if (actionbar) { + String[] data = {state.level.name(), state.condition.name()}; for (Player p : Bukkit.getOnlinePlayers()) { - + p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ConfigUtils.parsePlaceholders("messages.actionbarInfo", actionbarInfo, data))); } } } else if (source.equals("player")) { @@ -100,7 +105,12 @@ public class GetStateTask extends BukkitRunnable { "Provider returned state %s %s for %s", state.condition.name(), state.level.name(), p.getName() )); p.setPlayerWeather(state.simple == ConditionSimple.CLEAR ? WeatherType.CLEAR : WeatherType.DOWNFALL); - if (broadcast) p.sendMessage( String.format("%s %s in %s", state.level.name(), state.condition.name(), city.getCity().getName()) ); + if (actionbar) { + String[] data = {state.level.name(), state.condition.name()}; + p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText( + ConfigUtils.parsePlaceholders("messages.actionbarInfo", actionbarInfo, data)) + ); + } } } catch (AddressNotFoundException e) { logger.warning(String.format("%s's IP address (%s) is not a public IP address, therefore we can't retrieve their location.", curr.getName(), playerIp.toString())); @@ -121,9 +131,12 @@ public class GetStateTask extends BukkitRunnable { if (debug) logger.info( String.format( "Provider returned state %s %s for %f, %f", state.condition.name(), state.level.name(), lat, lon )); - if (broadcast) p.sendMessage( String.format("%s %s in %f, %f", - state.level.name(), state.condition.name(), lat, lon - ) ); + if (actionbar) { + String[] data = {state.level.name(), state.condition.name()}; + p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText( + ConfigUtils.parsePlaceholders("messages.actionbarInfo", actionbarInfo, data)) + ); + } } } } diff --git a/src/main/java/pl/minecon724/realweather/RW.java b/src/main/java/pl/minecon724/realweather/RW.java index f28b6d3..542b58e 100644 --- a/src/main/java/pl/minecon724/realweather/RW.java +++ b/src/main/java/pl/minecon724/realweather/RW.java @@ -27,6 +27,7 @@ public class RW extends JavaPlugin { ConfigurationSection weatherSec = config.getConfigurationSection("weather"); ConfigurationSection providerSec = config.getConfigurationSection("provider"); ConfigurationSection settingsSec = config.getConfigurationSection("settings"); + ConfigurationSection messagesSec = config.getConfigurationSection("messages"); String source = weatherSec.getString("source"); ConfigurationSection point = weatherSec.getConfigurationSection("point"); @@ -64,20 +65,20 @@ public class RW extends JavaPlugin { double scale_lon = map.getDouble("scale_lon"); int on_exceed = map.getInt("on_exceed"); - boolean broadcast = settingsSec.getBoolean("broadcast"); boolean debug = settingsSec.getBoolean("debug"); boolean debugDox = settingsSec.getBoolean("debugDox"); new GetStateTask( provider, source, pointLatitude, pointLongitude, worlds, this.getLogger(), - client, broadcast, debug, debugDox, scale_lat, scale_lon, on_exceed + client, debug, debugDox, scale_lat, scale_lon, on_exceed, + settingsSec.getBoolean("actionbar"), ConfigUtils.color(messagesSec.getString("actionbarInfo")) ).runTaskTimerAsynchronously(this, settingsSec.getLong("timeBeforeInitialRun"), settingsSec.getLong("timeBetweenRecheck") ); Metrics metrics = new Metrics(this, 15020); - metrics.addCustomChart(new SimplePie("source_type", () -> { + metrics.addCustomChart(new Metrics.SimplePie("source_type", () -> { return source; })); diff --git a/src/main/java/pl/minecon724/realweather/WeatherState.java b/src/main/java/pl/minecon724/realweather/WeatherState.java index bfd6128..3e58177 100644 --- a/src/main/java/pl/minecon724/realweather/WeatherState.java +++ b/src/main/java/pl/minecon724/realweather/WeatherState.java @@ -17,7 +17,7 @@ public class WeatherState { Condition condition; ConditionLevel level; ConditionSimple simple; - + // Constructor public State(Condition condition, diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e04329d..839a90f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -47,8 +47,6 @@ settings: # 20 is one second # Shouldn't affect performance timeBetweenRecheck: 600 - # Whether to broadcast messages about weather updates - broadcast: false # Whether to display an actionbar containing info actionbar: true