This commit is contained in:
Minecon724 2022-06-11 10:05:01 +00:00
parent 54625a0bf0
commit be2b69e30c
6 changed files with 32 additions and 18 deletions

View file

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>pl.minecon724</groupId>
<artifactId>realweather</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>

View file

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

View file

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

View file

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

View file

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