2021-06-05 21:12:33 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2021-06-05 21:12:33 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.util;
|
2021-06-05 21:12:33 +00:00
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonSyntaxException;
|
|
|
|
import org.geysermc.floodgate.news.NewsItem;
|
|
|
|
import org.geysermc.floodgate.news.NewsItemAction;
|
2021-07-05 12:24:35 +00:00
|
|
|
import org.geysermc.floodgate.news.data.AnnouncementData;
|
2021-06-05 21:12:33 +00:00
|
|
|
import org.geysermc.floodgate.news.data.BuildSpecificData;
|
|
|
|
import org.geysermc.floodgate.news.data.CheckAfterData;
|
2022-06-08 12:09:14 +00:00
|
|
|
import org.geysermc.geyser.Constants;
|
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
|
|
|
import org.geysermc.geyser.GeyserLogger;
|
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.text.ChatColor;
|
2021-06-05 21:12:33 +00:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
public class NewsHandler {
|
|
|
|
private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
|
2021-11-20 21:34:30 +00:00
|
|
|
private final GeyserLogger logger = GeyserImpl.getInstance().getLogger();
|
2021-06-05 21:12:33 +00:00
|
|
|
private final Gson gson = new Gson();
|
|
|
|
|
|
|
|
private final Map<Integer, NewsItem> activeNewsItems = new HashMap<>();
|
|
|
|
private final String branch;
|
|
|
|
private final int build;
|
|
|
|
|
2021-06-08 11:21:22 +00:00
|
|
|
private boolean firstCheck = true;
|
2021-06-05 21:12:33 +00:00
|
|
|
|
|
|
|
public NewsHandler(String branch, int build) {
|
|
|
|
this.branch = branch;
|
|
|
|
this.build = build;
|
|
|
|
|
|
|
|
executorService.scheduleWithFixedDelay(this::checkNews, 0, 30, TimeUnit.MINUTES);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void schedule(long delayMs) {
|
|
|
|
executorService.schedule(this::checkNews, delayMs, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkNews() {
|
|
|
|
try {
|
2021-11-07 14:41:08 +00:00
|
|
|
String body = WebUtils.getBody(Constants.NEWS_OVERVIEW_URL + Constants.NEWS_PROJECT_NAME);
|
2021-06-05 21:12:33 +00:00
|
|
|
JsonArray array = gson.fromJson(body, JsonArray.class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (JsonElement newsItemElement : array) {
|
|
|
|
NewsItem newsItem = NewsItem.readItem(newsItemElement.getAsJsonObject());
|
|
|
|
if (newsItem != null) {
|
|
|
|
addNews(newsItem);
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 11:21:22 +00:00
|
|
|
firstCheck = false;
|
2021-06-05 21:12:33 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
if (logger.isDebug()) {
|
|
|
|
logger.error("Error while reading news item", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (JsonSyntaxException ignored) {}
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public void handleNews(GeyserSession session, NewsItemAction action) {
|
2021-06-05 21:12:33 +00:00
|
|
|
for (NewsItem news : getActiveNews(action)) {
|
|
|
|
handleNewsItem(session, news, action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
private void handleNewsItem(GeyserSession session, NewsItem news, NewsItemAction action) {
|
2021-06-05 21:12:33 +00:00
|
|
|
switch (action) {
|
|
|
|
case ON_SERVER_STARTED:
|
2021-06-08 11:21:22 +00:00
|
|
|
if (!firstCheck) {
|
2021-06-05 21:12:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case BROADCAST_TO_CONSOLE:
|
|
|
|
logger.info(news.getMessage());
|
|
|
|
break;
|
|
|
|
case ON_OPERATOR_JOIN:
|
|
|
|
//todo doesn't work, it's called before we know the op level.
|
|
|
|
// if (session != null && session.getOpPermissionLevel() >= 2) {
|
|
|
|
// session.sendMessage(ChatColor.GREEN + news.getMessage());
|
|
|
|
// }
|
|
|
|
break;
|
|
|
|
case BROADCAST_TO_OPERATORS:
|
2021-11-22 19:52:26 +00:00
|
|
|
for (GeyserSession player : GeyserImpl.getInstance().getSessionManager().getSessions().values()) {
|
2021-06-05 21:12:33 +00:00
|
|
|
if (player.getOpPermissionLevel() >= 2) {
|
|
|
|
session.sendMessage(ChatColor.GREEN + news.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<NewsItem> getActiveNews() {
|
|
|
|
return activeNewsItems.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<NewsItem> getActiveNews(NewsItemAction action) {
|
|
|
|
List<NewsItem> news = new ArrayList<>();
|
|
|
|
for (NewsItem item : getActiveNews()) {
|
|
|
|
if (item.getActions().contains(action)) {
|
|
|
|
news.add(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return news;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addNews(NewsItem item) {
|
|
|
|
if (activeNewsItems.containsKey(item.getId())) {
|
|
|
|
if (!item.isActive()) {
|
|
|
|
activeNewsItems.remove(item.getId());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-08 10:53:00 +00:00
|
|
|
if (!item.isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-05 21:12:33 +00:00
|
|
|
switch (item.getType()) {
|
2021-07-05 12:24:35 +00:00
|
|
|
case ANNOUNCEMENT:
|
|
|
|
if (!item.getDataAs(AnnouncementData.class).isAffected(Constants.NEWS_PROJECT_NAME)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2021-06-05 21:12:33 +00:00
|
|
|
case BUILD_SPECIFIC:
|
|
|
|
if (!item.getDataAs(BuildSpecificData.class).isAffected(branch, build)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CHECK_AFTER:
|
|
|
|
long checkAfter = item.getDataAs(CheckAfterData.class).getCheckAfter();
|
|
|
|
long delayMs = System.currentTimeMillis() - checkAfter;
|
|
|
|
schedule(delayMs > 0 ? delayMs : 0);
|
|
|
|
break;
|
2021-07-05 12:24:35 +00:00
|
|
|
case CONFIG_SPECIFIC:
|
|
|
|
//todo implement
|
|
|
|
break;
|
2021-06-05 21:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
activeNewsItems.put(item.getId(), item);
|
|
|
|
activateNews(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void activateNews(NewsItem item) {
|
|
|
|
for (NewsItemAction action : item.getActions()) {
|
|
|
|
handleNewsItem(null, item, action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown() {
|
|
|
|
executorService.shutdown();
|
|
|
|
}
|
|
|
|
}
|