Skip to content

Commit

Permalink
Merge pull request #150 from Andre601/feature/improve-versions-file-h…
Browse files Browse the repository at this point in the history
…andling

Improve versions.json file handling
  • Loading branch information
Andre601 authored Apr 2, 2022
2 parents 5aeae10 + 2aabaa4 commit 58e4e72
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 119 deletions.
5 changes: 0 additions & 5 deletions bungeecord/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@
<artifactId>configurate-yaml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>configurate-gson</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bungeecord</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void onLogin(PreLoginEvent event){
plugin.getProxyLogger().infoFormat(
"Denied login for Player %s with MC version %s (Protocol version: %d)",
event.getConnection().getName(),
plugin.getProtocolVersionResolver().getFriendlyName(userProtocol),
plugin.getProtocolVersionResolver().getVersions().getFriendlyName(userProtocol),
userProtocol
);
}
Expand Down
9 changes: 5 additions & 4 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@

<dependencies>
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>configurate-yaml</artifactId>
<version>4.1.2</version>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>configurate-gson</artifactId>
<artifactId>configurate-yaml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public class CommandPermissions{
public static final String COMMAND_HELP = COMMAND_BASE + "help";
// oneversionremake.command.reload
public static final String COMMAND_RELOAD = COMMAND_BASE + "reload";
// oneversionremake.command.refresh
public static final String COMMAND_REFRESH = COMMAND_BASE + "refresh";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.andre601.oneversionremake.core.interfaces.PluginCore;
import com.andre601.oneversionremake.core.interfaces.ProxyLogger;
import com.andre601.oneversionremake.core.proxy.ProtocolVersionResolver;
import com.andre601.oneversionremake.core.proxy.VersionsFile;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -32,7 +33,7 @@

public class OneVersionRemake{

private static final String DEF_VERSIONS_URL = "https://www.andre601.ch/oneversionremake/versions.json";
public static final String DEF_VERSIONS_URL = "https://www.andre601.ch/oneversionremake/protocol_versions.json";

private final PluginCore pluginCore;
private final ConfigHandler configHandler;
Expand Down Expand Up @@ -85,7 +86,7 @@ public Map<String, Map<String, Integer>> getPieMap(){

List<Integer> versions = getConfigHandler().getIntList("Protocol", "Versions");
if(versions.isEmpty()){
String unknown = getProtocolVersionResolver().getFriendlyName(0);
String unknown = getProtocolVersionResolver().getVersions().getFriendlyName(0);

Map<String, Integer> entry = new HashMap<>();

Expand All @@ -96,8 +97,8 @@ public Map<String, Map<String, Integer>> getPieMap(){
}

for(int version : versions){
String major = getProtocolVersionResolver().getMajor(version);
String name = getProtocolVersionResolver().getFriendlyName(version);
String major = getProtocolVersionResolver().getVersions().getMajor(version);
String name = getProtocolVersionResolver().getVersions().getFriendlyName(version);

Map<String, Integer> entry = new HashMap<>();
entry.put(name, 1);
Expand Down Expand Up @@ -141,22 +142,36 @@ private void load(){
return;
}

if(!getProtocolVersionResolver().hasFile() || getConfigHandler().getBoolean(true, "Settings", "UpdateVersions")){
String url = getConfigHandler().getString(DEF_VERSIONS_URL, "Settings", "VersionsUrl");

if(getProtocolVersionResolver().loadFile(url)){
getProxyLogger().info("Updated versions.json!");
String url = getConfigHandler().getString(DEF_VERSIONS_URL, "Settings", "VersionsUrl");

if(getProtocolVersionResolver().isFileMissing()){
getProxyLogger().info("Creating versions.json...");
getProtocolVersionResolver().createFile(url).whenComplete((versions, throwable) -> {
if(handleErrors(versions, throwable))
return;

getProxyLogger().info("Successfully created versions.json!");
enable();
}else{
getProxyLogger().warn("Unable to update versions.json! Check previous lines for warnings and errors.");
}
});
}else
if(getConfigHandler().getBoolean(true, "Settings", "UpdateVersions")){
getProxyLogger().info("Checking for updated versions.json...");
getProtocolVersionResolver().updateFile(url).whenComplete((versions, throwable) -> {
if(handleErrors(versions, throwable))
return;

getProxyLogger().info("Update-check complete!");
enable();
});
}else{
if(getProtocolVersionResolver().setupConfigurate()){
getProxyLogger().info("Loading versions.json...");
getProtocolVersionResolver().loadFile().whenComplete(((versions, throwable) -> {
if(handleErrors(versions, throwable))
return;

getProxyLogger().info("Loaded versions.json!");
enable();
}else{
getProxyLogger().warn("Unable to load versions.json! Check previous lines for errors and warnings.");
}
}));
}
}

Expand All @@ -169,7 +184,7 @@ private void enable(){
versionsSet = false;
}else{
getProxyLogger().info("Loaded the following Protocol Version(s):");
getProxyLogger().info(getProtocolVersionResolver().getFriendlyNames(protocols, false));
getProxyLogger().info(getProtocolVersionResolver().getVersions().getFriendlyNames(protocols, false));

versionsSet = true;
}
Expand All @@ -190,7 +205,7 @@ private void enable(){
getProxyLogger().info("No Protocol Versions set. Skipping Metrics initialization...");
}

getProxyLogger().info("OneVersionRemake is ready!");
getProxyLogger().info("OneVersionRemake ready!");
}

private void printBanner(){
Expand Down Expand Up @@ -231,4 +246,19 @@ private void loadVersion(){
version = "UNKNOWN";
}
}

private boolean handleErrors(VersionsFile versionsFile, Throwable throwable){
if(throwable != null || versionsFile == null){
if(throwable != null){
getProxyLogger().warn("Encountered an Exception while handling versions.json");
getProxyLogger().warnFormat("Error Message: %s", throwable.getMessage());
}else{
getProxyLogger().warn("Handling of versions.json was non-successful!");
getProxyLogger().warn("Please check previous entries for any errors.");
}
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public String toString(String text, List<Integer> serverProtocols, int userProto
}

private String parse(String text, List<Integer> serverProtocols, int userProtocol, boolean majorOnly){
return text.replace("{version}", core.getProtocolVersionResolver().getFriendlyNames(serverProtocols, majorOnly))
.replace("{userVersion}", core.getProtocolVersionResolver().getFriendlyName(userProtocol));
return text.replace("{version}", core.getProtocolVersionResolver().getVersions().getFriendlyNames(serverProtocols, majorOnly))
.replace("{userVersion}", core.getProtocolVersionResolver().getVersions().getFriendlyName(userProtocol));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,26 @@ public void handle(CmdSender sender, String[] args){
sender.sendMsg();
sender.sendMsg(NamedTextColor.AQUA, "/ovr reload");
sender.sendMsg(NamedTextColor.GRAY, "Reloads the configuration file.");
sender.sendMsg();
sender.sendMsg(NamedTextColor.AQUA, "/ovr refresh");
sender.sendMsg(NamedTextColor.GRAY, "Updates the versions.json file.");
}else
if(args[0].equalsIgnoreCase("reload")){
if(!sender.hasPermission(CommandPermissions.COMMAND_RELOAD)){
sender.sendMsg(NamedTextColor.RED, "Insufficient permissions!");
return;
}

sender.sendMsg(NamedTextColor.GRAY, "Reloading config.yml...");

if(core.reloadConfig()){
List<Integer> protocols = core.getConfigHandler().getIntList("Protocol", "Versions");

sender.sendMsg(NamedTextColor.AQUA, "Loaded following Minecraft version(s):");
if(protocols.isEmpty()){
sender.sendMsg(NamedTextColor.RED, "None");
}else{
sender.sendMsg(NamedTextColor.GRAY, core.getProtocolVersionResolver().getFriendlyNames(protocols, false));
sender.sendMsg(NamedTextColor.GRAY, core.getProtocolVersionResolver().getVersions().getFriendlyNames(protocols, false));
}

sender.sendMsg();
Expand All @@ -70,6 +75,30 @@ public void handle(CmdSender sender, String[] args){
sender.sendMsg(NamedTextColor.RED, "There was an issue while reloading the configuration file!");
sender.sendMsg(NamedTextColor.RED, "Please check the Console of the Proxy for any errors and warnings.");
}
}else
if(args[0].equalsIgnoreCase("refresh")){
if(!sender.hasPermission(CommandPermissions.COMMAND_REFRESH)){
sender.sendMsg(NamedTextColor.RED, "Insufficient permissions!");
return;
}

sender.sendMsg(NamedTextColor.GRAY, "Updating versions.json...");

core.getProtocolVersionResolver()
.updateFile(core.getConfigHandler().getString(OneVersionRemake.DEF_VERSIONS_URL, "Settings", "VersionsUrl"))
.whenComplete((versions, throwable) -> {
if(versions == null || throwable != null){
if(throwable != null){
sender.sendMsg(NamedTextColor.RED, "Encountered an Exception while performing the update.");
}else{
sender.sendMsg(NamedTextColor.RED, "Update was not successful! Check console for any errors!");
}

return;
}

sender.sendMsg(NamedTextColor.GREEN, "Successfully updated versions.json!");
});
}else{
sender.sendMsg(NamedTextColor.RED, "Unknown argument \"%s\".", args[0]);
sender.sendMsg(NamedTextColor.RED, "Run \"/ovr help\" for all commands.");
Expand Down
Loading

0 comments on commit 58e4e72

Please sign in to comment.