1
0
mirror of https://github.com/GeyserMC/Geyser.git synced 2025-12-23 00:39:19 +00:00

Added the initial version of news

This commit is contained in:
Tim203
2021-06-05 23:12:33 +02:00
parent e16d83ecc1
commit 50b51f5f57
14 changed files with 735 additions and 20 deletions

View File

@@ -64,6 +64,7 @@ import org.geysermc.floodgate.crypto.AesCipher;
import org.geysermc.floodgate.crypto.AesKeyProducer;
import org.geysermc.floodgate.crypto.Base64Topping;
import org.geysermc.floodgate.crypto.FloodgateCipher;
import org.geysermc.floodgate.news.NewsItemAction;
import org.geysermc.floodgate.time.TimeSyncer;
import org.jetbrains.annotations.Contract;
@@ -111,6 +112,7 @@ public class GeyserConnector {
private TimeSyncer timeSyncer;
private FloodgateCipher cipher;
private FloodgateSkinUploader skinUploader;
private final NewsHandler newsHandler;
private boolean shuttingDown = false;
@@ -213,6 +215,21 @@ public class GeyserConnector {
}
}
String branch = "unknown";
int buildNumber = -1;
try {
Properties gitProperties = new Properties();
gitProperties.load(FileUtils.getResource("git.properties"));
branch = gitProperties.getProperty("git.branch");
String build = gitProperties.getProperty("git.build.number");
if (build != null) {
buildNumber = Integer.parseInt(build);
}
} catch (Exception e) {
logger.error("Failed to read git.properties", e);
}
newsHandler = new NewsHandler(branch, buildNumber);
CooldownUtils.setDefaultShowCooldown(config.getShowCooldown());
DimensionUtils.changeBedrockNetherId(config.isAboveBedrockNetherBuilding()); // Apply End dimension ID workaround to Nether
SkullBlockEntityTranslator.ALLOW_CUSTOM_SKULLS = config.isAllowCustomSkulls();
@@ -326,6 +343,8 @@ public class GeyserConnector {
if (platformType == PlatformType.STANDALONE) {
logger.warning(LanguageUtils.getLocaleStringLog("geyser.core.movement_warn"));
}
newsHandler.handleNews(null, NewsItemAction.ON_SERVER_STARTED);
}
public void shutdown() {
@@ -371,6 +390,7 @@ public class GeyserConnector {
generalThreadPool.shutdown();
bedrockServer.close();
timeSyncer.shutdown();
newsHandler.shutdown();
players.clear();
defaultAuthType = null;
this.getCommandManager().getCommands().clear();

View File

@@ -63,7 +63,7 @@ public final class FloodgateSkinUploader {
public FloodgateSkinUploader(GeyserConnector connector) {
this.logger = connector.getLogger();
this.client = new WebSocketClient(Constants.SKIN_UPLOAD_URI) {
this.client = new WebSocketClient(Constants.GLOBAL_API_WS_URI) {
@Override
public void onOpen(ServerHandshake handshake) {
setConnectionLostTimeout(11);
@@ -99,7 +99,7 @@ public final class FloodgateSkinUploader {
id = node.get("id").asInt();
verifyCode = node.get("verify_code").asText();
break;
case SUBSCRIBERS_COUNT:
case SUBSCRIBER_COUNT:
subscribersCount = node.get("subscribers_count").asInt();
break;
case SKIN_UPLOADED:
@@ -145,6 +145,8 @@ public final class FloodgateSkinUploader {
break;
}
break;
case NEWS_ADDED:
//todo
}
} catch (Exception e) {
logger.error("Error while receiving a message", e);

View File

@@ -27,18 +27,28 @@ package org.geysermc.connector.utils;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public final class Constants {
public static final URI SKIN_UPLOAD_URI;
public static final URI GLOBAL_API_WS_URI;
public static final String NTP_SERVER = "time.cloudflare.com";
public static final Set<String> NEWS_PROJECT_LIST = Collections.unmodifiableSet(
new HashSet<>(Arrays.asList("geyser", "floodgate"))
);
public static final String NEWS_OVERVIEW_URL = "https://api.geysermc.org/v1/news";
static {
URI skinUploadUri = null;
URI wsUri = null;
try {
skinUploadUri = new URI("wss://api.geysermc.org/ws");
wsUri = new URI("wss://api.geysermc.org/ws");
} catch (URISyntaxException e) {
e.printStackTrace();
}
SKIN_UPLOAD_URI = skinUploadUri;
GLOBAL_API_WS_URI = wsUri;
}
}

View File

@@ -0,0 +1,175 @@
/*
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
*
* 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
*/
package org.geysermc.connector.utils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.GeyserLogger;
import org.geysermc.connector.common.ChatColor;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.floodgate.news.NewsItem;
import org.geysermc.floodgate.news.NewsItemAction;
import org.geysermc.floodgate.news.data.BuildSpecificData;
import org.geysermc.floodgate.news.data.CheckAfterData;
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);
private final GeyserLogger logger = GeyserConnector.getInstance().getLogger();
private final Gson gson = new Gson();
private final Map<Integer, NewsItem> activeNewsItems = new HashMap<>();
private final String branch;
private final int build;
private boolean geyserStarted;
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 {
String body = WebUtils.getBody(Constants.NEWS_OVERVIEW_URL);
JsonArray array = gson.fromJson(body, JsonArray.class);
try {
for (JsonElement newsItemElement : array) {
NewsItem newsItem = NewsItem.readItem(newsItemElement.getAsJsonObject());
if (newsItem != null) {
addNews(newsItem);
}
}
} catch (Exception e) {
if (logger.isDebug()) {
logger.error("Error while reading news item", e);
}
}
} catch (JsonSyntaxException ignored) {}
}
public void setGeyserStarted() {
geyserStarted = true;
}
public void handleNews(GeyserSession session, NewsItemAction action) {
for (NewsItem news : getActiveNews(action)) {
handleNewsItem(session, news, action);
}
}
private void handleNewsItem(GeyserSession session, NewsItem news, NewsItemAction action) {
switch (action) {
case ON_SERVER_STARTED:
if (!geyserStarted) {
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:
for (GeyserSession player : GeyserConnector.getInstance().getPlayers()) {
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;
}
if (!Constants.NEWS_PROJECT_LIST.contains(item.getProject())) {
return;
}
switch (item.getType()) {
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;
}
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();
}
}

View File

@@ -45,9 +45,8 @@ public class WebUtils {
* @return Body contents or error message if the request fails
*/
public static String getBody(String reqURL) {
URL url = null;
try {
url = new URL(reqURL);
URL url = new URL(reqURL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Geyser-" + GeyserConnector.getInstance().getPlatformType().toString() + "/" + GeyserConnector.VERSION); // Otherwise Java 8 fails on checking updates