5 Commits

Author SHA1 Message Date
Jack Fitch
84f3050820 fix time one minute behind 2022-10-28 16:20:45 -04:00
Jack Fitch
829462243c update README 2022-10-28 16:20:28 -04:00
Jack Fitch
2858457303 suppress deprecation warnings 2022-10-28 16:07:11 -04:00
Jack Fitch
1d2513f8b7 add option to change sync intervals 2022-10-28 16:04:31 -04:00
Jack Fitch
6558d82f5c add support for Minecraft 1.7+ 2022-10-28 15:46:02 -04:00
5 changed files with 42 additions and 22 deletions

View File

@@ -1,16 +1,18 @@
# RealTimeWeather [![Build Status](https://app.travis-ci.com/Jack1424/RealTimeWeather.svg?branch=master)](https://app.travis-ci.com/Jack1424/RealTimeWeather)
A lightweight Minecraft Java server plugin that allows you to sync your server's time and weather with the real world
A lightweight Minecraft Java server plugin that allows you to sync your server's time and weather with the real world.
**Current Features:**
- Lightweight
- Supports all Minecraft versions from 1.7+
- Constant time syncing
- Weather syncing (rain and thunder)
**Upcoming Features:**
- [ ] Use WeatherPlusAPI (more weather states)
- [ ] Support for more Minecraft verions
- [X] Support for more Minecraft versions
- [ ] Wiki
## Contributions
I'm open to any help/ideas that you have. Just open an issue or a pull request and I'll be sure to look at it as soon as I can. Builds with `gradlew shadowJar`.
I'm open to any help/ideas that you have. Just open an issue or a pull request, and I'll be sure to look at it as soon as I can. Builds with `gradlew shadowJar`.
License: GPL-3.0

View File

@@ -4,7 +4,7 @@ plugins {
}
group = 'io.github.Jack1424'
version = '1.1.2'
version = '1.2.0'
repositories {
mavenCentral()
@@ -20,14 +20,14 @@ repositories {
dependencies {
implementation("org.bstats:bstats-bukkit:3.0.0")
compileOnly 'io.papermc.paper:paper-api:1.19.2-R0.1-SNAPSHOT'
implementation("org.bukkit:bukkit:1.13-R0.1-SNAPSHOT")
}
shadowJar {
relocate('org.bstats', 'io.github.jack1424.realtimeweather')
}
def targetJavaVersion = 17
def targetJavaVersion = 11
java {
def javaVersion = JavaVersion.toVersion(targetJavaVersion)
sourceCompatibility = javaVersion

View File

@@ -2,7 +2,6 @@ package io.github.jack1424.realtimeweather;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@@ -23,6 +22,7 @@ import java.time.zone.ZoneRulesException;
import java.util.*;
import java.util.logging.Logger;
@SuppressWarnings("deprecation")
public final class RealTimeWeather extends JavaPlugin implements Listener {
private Logger logger;
private ZoneId timezone;
@@ -62,9 +62,9 @@ public final class RealTimeWeather extends JavaPlugin implements Listener {
debug("Re-enabling normal daylight and weather cycles...");
if (timeEnabled)
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, true);
world.setGameRuleValue("doDaylightCycle", "true");
if (weatherEnabled)
world.setGameRule(GameRule.DO_WEATHER_CYCLE, true);
world.setGameRuleValue("doWeatherCycle", "true");
}
logger.info("Stopping...");
@@ -87,8 +87,11 @@ public final class RealTimeWeather extends JavaPlugin implements Listener {
}
private void setupTime() {
long timeSyncInterval;
try {
timezone = ZoneId.of(Objects.requireNonNull(getConfig().getString("Timezone")));
timeSyncInterval = getConfig().getLong("TimeSyncInterval");
} catch (NullPointerException|ZoneRulesException e) {
logger.severe("Error loading timezone. Check that the values in your configuration file are valid.");
debug(e.getMessage());
@@ -98,30 +101,35 @@ public final class RealTimeWeather extends JavaPlugin implements Listener {
return;
}
debug("Enabling time zone sync (every second)");
debug("Enabling time zone sync...");
debug("Syncing time with " + timezone.toString());
for (World world : getServer().getWorlds())
if (world.getEnvironment().equals(World.Environment.NORMAL))
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
world.setGameRuleValue("doDaylightCycle", "false");
getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> {
if (timeEnabled) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(timezone));
for (World world : getServer().getWorlds())
if (world.getEnvironment().equals(World.Environment.NORMAL))
world.setTime((1000 * cal.get(Calendar.HOUR_OF_DAY)) + (16 * cal.get(Calendar.MINUTE)) - 6000); // TODO: Time is one minute behind
world.setTime((1000 * cal.get(Calendar.HOUR_OF_DAY)) + (16 * (cal.get(Calendar.MINUTE) + 1)) - 6000);
}
}, 0L, 20L); // TODO: Does this really need to update every second?
}, 0L, timeSyncInterval);
}
private void setupWeather() {
long weatherSyncInterval;
String apiKey = getConfig().getString("APIKey");
String zipCode = getConfig().getString("ZipCode");
String countryCode = getConfig().getString("CountryCode");
String lat, lon;
try {
weatherSyncInterval = getConfig().getLong("WeatherSyncInterval");
HttpsURLConnection con = (HttpsURLConnection) new URL(String.format("https://api.openweathermap.org/geo/1.0/zip?zip=%s,%s&appid=%s", zipCode, countryCode, apiKey)).openConnection();
con.setRequestMethod("GET");
con.connect();
@@ -129,15 +137,18 @@ public final class RealTimeWeather extends JavaPlugin implements Listener {
int response = con.getResponseCode();
if (response > 499) {
logger.severe("There was a server error when requesting weather information. Please try again later");
throw new Exception("Server error");
throw new Exception("Server/client error");
}
else if (response > 399) {
String message = "Error when getting weather information: ";
switch (response) {
case 401 -> logger.severe(message + "API key incorrect");
case 404 -> logger.severe(message + "Zip/Country code incorrect");
default -> logger.severe("Unknown error");
}
if (response == 401)
logger.severe(message + "API key incorrect");
else if (response == 404)
logger.severe(message + "Zip/Country code incorrect");
else
logger.severe("Unknown error");
logger.severe("Please check that the values set in the config file are correct");
throw new Exception("Configuration error");
@@ -156,7 +167,7 @@ public final class RealTimeWeather extends JavaPlugin implements Listener {
for (World world : getServer().getWorlds())
if (world.getEnvironment().equals(World.Environment.NORMAL))
world.setGameRule(GameRule.DO_WEATHER_CYCLE, false);
world.setGameRuleValue("doWeatherCycle", "false");
getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> {
debug("Syncing weather...");
@@ -190,7 +201,7 @@ public final class RealTimeWeather extends JavaPlugin implements Listener {
world.setStorm(rain);
world.setThundering(thunder);
}
}, 0L, 6000L);
}, 0L, weatherSyncInterval);
}
private JSONObject makeWeatherRequest(URL url) throws IOException, ParseException {

View File

@@ -4,6 +4,9 @@
################################# TIME SYNC SETTINGS #################################################################
# Set to true to enable time syncing, or false to disable #
SyncTime: false
#
# You can change the time between time syncs from the default (5 seconds) below #
TimeSyncInterval: 100
# #
# Enter the time zone that you want to sync your world(s) with #
# This location CAN be different from your chosen weather location #
@@ -15,6 +18,11 @@ Timezone: 'America/New_York'
################################# WEATHER SYNC SETTINGS ##############################################################
# Set to true to enable weather syncing, or false to disable #
SyncWeather: false
#
# You can change the time between weather syncs from the default (5 minutes) below #
# Due to OpenWeather's restrictions, setting this value below 2400 (2 minutes) will cause problems #
# You can find a handy tick calculator here: https://mapmaking.fr/tick/ #
WeatherSyncInterval: 6000
# #
# In order to access the weather sync feature, create an account at https://openweathermap.org/ #
# Then, create an API key at https://openweathermap.org/appid and copy it #

View File

@@ -2,6 +2,5 @@ name: RealTimeWeather
version: '${version}'
description: Sync your server time and weather with the real world
main: io.github.jack1424.realtimeweather.RealTimeWeather
api-version: 1.19
authors: [ Jack1424 ]
website: https://github.com/Jack1424/RealTimeWeather