Skip to content

Commit

Permalink
version 2.9.14-snapshot-1
Browse files Browse the repository at this point in the history
added multi-version support for RPs
  • Loading branch information
EriolEandur committed Sep 18, 2024
1 parent fd7679e commit 2f944d9
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 94 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<includes>
<include>plugin.yml</include>
<include>config.yml</include>
<include>protocolVersions.yml</include>
<include>items.json</include>
</includes>
</resource>
Expand All @@ -48,6 +49,10 @@
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/repository/public/</url>
</repository>
<repository>
<id>viaversion-repo</id>
<url>https://repo.viaversion.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
Expand Down Expand Up @@ -103,6 +108,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.viaversion</groupId>
<artifactId>viaversion-api</artifactId>
<version>[5.0.0,6.0.0)</version>
<scope>provided</scope>
</dependency>
</dependencies>
<name>MCME-Architect</name>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ public boolean onCommand(CommandSender cs, Command cmd, String c, String[] args)
}
if(args.length>0 && args[0].equalsIgnoreCase("server")
&& PluginData.hasPermission(cs, Permission.RESOURCE_PACK_ADMIN)) {
if(args.length>2) {
RpReleaseUtil.setServerResourcePack(cs, args[1], args[2], success -> {
if(args.length>3) {
RpReleaseUtil.setServerResourcePack(cs, args[1], args[2], args[3], success -> {
if (success) {
PluginData.getMessageUtil().sendInfoMessage(cs, "Server resource pack " + args[1]
+ " set to version: " + args[2]);
+ " set to version: " + args[2] + " for MC: "+ args[3]);
} else {
PluginData.getMessageUtil().sendErrorMessage(cs, "Error while setting server resource pack!");
}
});
} else {
PluginData.getMessageUtil().sendErrorMessage(cs, "Command syntax: /rp server <rpName> <version>");
PluginData.getMessageUtil().sendErrorMessage(cs, "Command syntax: /rp server <rpName> <rpVersion> <requiredMcVersion>");
}
return true;
}
Expand All @@ -84,7 +84,8 @@ public boolean onCommand(CommandSender cs, Command cmd, String c, String[] args)
@Override
public void run() {
String rpName = RpManager.matchRpName(args[1]);
if(RpManager.refreshSHA(cs, rpName)) {
boolean allVersions = args.length > 2 && args[2].equals("all");
if(RpManager.refreshSHA(cs, rpName, allVersions)) {
PluginData.getMessageUtil().sendInfoMessage(cs, "SHA recalculated for rp: "+rpName);
} else {
PluginData.getMessageUtil().sendErrorMessage(cs, "Error while recalculating SHA for rp: "+args[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.mcmiddleearth.connect.log.Log;
import com.mcmiddleearth.pluginutil.message.FancyMessage;
import com.mcmiddleearth.pluginutil.message.MessageType;
import com.viaversion.viaversion.api.Via;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -66,24 +67,26 @@ public void onPlayerPreLogin(AsyncPlayerPreLoginEvent event) {
@EventHandler
public void onPlayerConnect(PlayerConnectEvent event) {
Player player = event.getPlayer();
Logger.getGlobal().info("PlayerConnectEvent: "+player.getName()+" "+ event.getReason().name());
/*Logger.getGlobal().info("PlayerConnectEvent: "+player.getName()+" "+ event.getReason().name());
int version = player.getProtocolVersion();
String snapshot = "";
if(version > 0x40000000) {
snapshot = "Snapshot ";
version = version - 0x40000000;
}
Logger.getGlobal().info("Protocol Version: "+snapshot + version);
Logger.getGlobal().info("Bukkit Protocol Version: "+snapshot + version);
Logger.getGlobal().info("ViaVersion Protocol Version: "+Via.getAPI().getPlayerProtocolVersion(player.getUniqueId()).getVersion());
Logger.getGlobal().info("Sodium client: "+RpManager.isSodiumClient(player));
Logger.getGlobal().info("Incomming plugin channels:");
Bukkit.getMessenger().getIncomingChannels().forEach(channel->Logger.getGlobal().info(channel));
Bukkit.getMessenger().getIncomingChannels().forEach(channel->Logger.getGlobal().info(channel));*/
if(event.getReason().equals(PlayerConnectEvent.ConnectReason.JOIN_PROXY)) {
new BukkitRunnable() {
int counter = 11;
@Override
public void run() {
if(RpManager.hasPlayerDataLoaded(player) || counter==0) {
RpPlayerData data = RpManager.getPlayerData(player);
data.setProtocolVersion(Via.getAPI().getPlayerProtocolVersion(player.getUniqueId()).getVersion());
if(RpManager.isSodiumClient(player)) {
data.setClient("sodium");
}
Expand Down Expand Up @@ -126,7 +129,7 @@ public void run() {
Logger.getLogger(ArchitectPlugin.class.getName()).log(Level.WARNING,"Could not get player rp settings from the database");
}
}
}.runTaskTimer(ArchitectPlugin.getPluginInstance(),0,20);
}.runTaskTimer(ArchitectPlugin.getPluginInstance(),30,20);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import com.mcmiddleearth.architect.ArchitectPlugin;
import com.mcmiddleearth.architect.PluginData;
import com.mcmiddleearth.architect.serverResoucePack.RegionEditConversation.RegionEditConversationFactory;
import com.mcmiddleearth.connect.log.Log;
import com.mcmiddleearth.util.DevUtil;
import com.mcmiddleearth.util.ResourceUtil;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
Expand All @@ -46,6 +48,7 @@
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
*
Expand All @@ -55,21 +58,36 @@ public class RpManager {

private static final File playerFile = new File(ArchitectPlugin.getPluginInstance().getDataFolder(),"/playerRpData.dat");
private static final File regionFolder = new File(ArchitectPlugin.getPluginInstance().getDataFolder(),"regions");


private static final File versionFile = new File(ArchitectPlugin.getPluginInstance().getDataFolder(), "protocolVersions.yml");
private static final Map<String, Integer> protocolVersions = new HashMap<>();

private static final String rpDatabaseConfig = "rpSettingsDatabase";

private static final Map<String, RpRegion> regions = new HashMap<>();

private static final Map<UUID,RpPlayerData> playerRpData = new HashMap<>();

private static final Map<UUID, String> sodiumClients = new HashMap<>();

private static final RpDatabaseConnector dbConnector = new RpDatabaseConnector(ArchitectPlugin.getPluginInstance().getConfig().getConfigurationSection(rpDatabaseConfig));

private static final RegionEditConversationFactory regionEditConversationFactory
= new RegionEditConversationFactory(ArchitectPlugin.getPluginInstance());

public static void init() {
if(!versionFile.exists()) {
ResourceUtil.saveResourceToFile(versionFile.getName(),versionFile);
}
YamlConfiguration versionConfig = new YamlConfiguration();
try {
versionConfig.load(versionFile);
for(String version: versionConfig.getKeys(false)) {
protocolVersions.put(version, versionConfig.getInt(version));
}
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
if(!regionFolder.exists()) {
regionFolder.mkdir();
}
Expand Down Expand Up @@ -145,6 +163,7 @@ public static RpPlayerData getPlayerData(Player player) {
RpPlayerData data = playerRpData.get(player.getUniqueId());
if(data == null) {
data = new RpPlayerData();
data.setProtocolVersion(player.getProtocolVersion());
playerRpData.put(player.getUniqueId(), data);
}
return data;
Expand Down Expand Up @@ -261,6 +280,32 @@ private static ConfigurationSection searchInVariantSection(ConfigurationSection
if (variantSection != null) {
if (variantSection.contains("url")) {
return variantSection;
} else {
String versionResult = "0.1";
int protocolResult = 0;
String versionMin = "unknown";
int protocolMin = Integer.MAX_VALUE;
for(String version: variantSection.getKeys(false)) {
//Logger.getGlobal().info("Version: "+version);
Integer protocolVersion = protocolVersions.get(version);
if(protocolVersion==null) {
return null;
}
//Logger.getGlobal().info("Protocol: "+protocolVersion);
if(protocolResult < protocolVersion && protocolVersion <= data.getProtocolVersion()) {
versionResult = version;
protocolResult = protocolVersion;
}
if(protocolMin > protocolVersion) {
protocolMin = protocolVersion;
versionMin = version;
}
}
if(protocolResult > 0) {
return variantSection.getConfigurationSection(versionResult);
} else {
return variantSection.getConfigurationSection(versionMin);
}
}
}
return null;
Expand Down Expand Up @@ -306,7 +351,7 @@ public static boolean setRp(String rpName, Player player, boolean force) {
return false;
}

public static byte[] getSHAForUrl(String url) {
/*public static byte[] getSHAForUrl(String url) {
for(String rpName: getRpConfig().getKeys(false)) {
ConfigurationSection clientSection = getRpConfig().getConfigurationSection(rpName);
for (String clientName : clientSection.getKeys(false)) {
Expand All @@ -323,7 +368,7 @@ public static byte[] getSHAForUrl(String url) {
}
}
return new byte[20];
}
}*/

public static String getRpForUrl(String url) {
for(String rpName: getRpConfig().getKeys(false)) {
Expand All @@ -338,8 +383,17 @@ public static String getRpForUrl(String url) {
for (String varKey : pxSection.getKeys(false)) {
//Logger.getGlobal().info("Variant: "+varKey);
ConfigurationSection varSection = pxSection.getConfigurationSection(varKey);
if (varSection.getString("url").equals(url)) {
return rpName;
if (varSection.contains("url")) {
if(varSection.getString("url").equals(url)) {
return rpName;
}
} else {
for(String versionKey: varSection.getKeys(false)) {
ConfigurationSection versionSection = varSection.getConfigurationSection(versionKey);
if (versionSection.getString("url").equals(url)) {
return rpName;
}
}
}
}
}
Expand All @@ -356,39 +410,60 @@ public static ConfigurationSection getRpConfig() {
return ArchitectPlugin.getPluginInstance().getConfig().getConfigurationSection("ServerResourcePacks");
}

public static boolean refreshSHA(CommandSender cs, String rp) {
public static boolean refreshSHA(CommandSender cs, String rp, boolean allVersions) {
ConfigurationSection config = getRpConfig().getConfigurationSection(rp);
if(config!=null) {
for(String clientKey: config.getKeys(false)) {
//Logger.getGlobal().info("ClientKey: "+clientKey);
ConfigurationSection clientSection = config.getConfigurationSection(clientKey);
for(String resolutionKey: clientSection.getKeys(false)) {
//Logger.getGlobal().info("ResolutionKey: "+resolutionKey);
ConfigurationSection resolutionSection = clientSection.getConfigurationSection(resolutionKey);
for (String variantKey : resolutionSection.getKeys(false)) {
//Logger.getGlobal().info("VariantKey: "+variantKey);
try {
ConfigurationSection variantSection = resolutionSection.getConfigurationSection(variantKey);
URL url = new URL(variantSection.getString("url"));
InputStream fis = url.openStream();
MessageDigest sha1 = MessageDigest.getInstance("SHA1");

byte[] data = new byte[1024];
int read = 0;
long time = System.currentTimeMillis();
while ((read = fis.read(data)) != -1) {
sha1.update(data, 0, read);
if (System.currentTimeMillis() - time > 5000) {
time = System.currentTimeMillis();
PluginData.getMessageUtil().sendInfoMessage(cs, "calculating ...");
List<ConfigurationSection> sections = new LinkedList<>();
if (variantSection.contains("url")) {
//Logger.getGlobal().info("DirectURL: "+variantSection.getString("url"));
sections.add(variantSection);
} else {
if (allVersions) {
//Logger.getGlobal().info("All versions");
sections.addAll(variantSection.getKeys(false).stream()
.map(variantSection::getConfigurationSection).collect(Collectors.toSet()));
} else {
//Logger.getGlobal().info("Latest versions");
sections.add(variantSection
.getConfigurationSection(getLatestVersion(variantSection.getKeys(false))));
}
}
byte[] hashBytes = sha1.digest();
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
//for(ConfigurationSection section: sections) Logger.getGlobal().info("URL: "+section.getString("url"));
for (ConfigurationSection section : sections) {
URL url = new URL(section.getString("url"));
InputStream fis = url.openStream();
MessageDigest sha1 = MessageDigest.getInstance("SHA1");

byte[] data = new byte[1024];
int read = 0;
long time = System.currentTimeMillis();
while ((read = fis.read(data)) != -1) {
sha1.update(data, 0, read);
if (System.currentTimeMillis() - time > 5000) {
time = System.currentTimeMillis();
PluginData.getMessageUtil().sendInfoMessage(cs, "calculating ...");
}
}
byte[] hashBytes = sha1.digest();
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
}
String hashString = sb.toString();
section.set("sha", hashString);
ArchitectPlugin.getPluginInstance().saveConfig();
}
String hashString = sb.toString();
variantSection.set("sha", hashString);
ArchitectPlugin.getPluginInstance().saveConfig();
} catch (IOException | NoSuchAlgorithmException ex) {
} catch(IOException | NoSuchAlgorithmException ex){
Logger.getLogger(RpManager.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
Expand All @@ -399,6 +474,22 @@ public static boolean refreshSHA(CommandSender cs, String rp) {
}
return false;
}

private static String getLatestVersion(Collection<String> versions) {
int latestProtocol = 0;
String latestVersion = "";
for(String version : versions) {
//protocolVersions.forEach((key, protocol) -> Logger.getGlobal().info(key+" "+protocol));
//Logger.getGlobal().info("getLatestVersion: version "+ version+ " protcolVersions: "+protocolVersions.size());
int protocolVersion = protocolVersions.get(version);
if(protocolVersion > latestProtocol) {
latestProtocol = protocolVersion;
latestVersion = version;
}
}
//Logger.getGlobal().info("LatestVersion: "+latestVersion);
return latestVersion;
}

public static String getResolutionKey(int px) {
return px+"px";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class RpPlayerData implements Serializable {

private final long serialVerionsUID = 1;

private boolean autoRp = true;
private String variant = "light";
private String client = "vanilla";
Expand All @@ -37,6 +37,8 @@ public class RpPlayerData implements Serializable {
private transient PlayerResourcePackStatusEvent.Status currentRpStatus
= PlayerResourcePackStatusEvent.Status.DECLINED;

private transient int protocolVersion;

public boolean isAutoRp() {
return autoRp;
}
Expand Down Expand Up @@ -92,4 +94,12 @@ public PlayerResourcePackStatusEvent.Status getCurrentRpStatus() {
public void setCurrentRpStatus(PlayerResourcePackStatusEvent.Status currentRpStatus) {
this.currentRpStatus = currentRpStatus;
}

public void setProtocolVersion(int protocolVersion) {
this.protocolVersion = protocolVersion;
}

public int getProtocolVersion() {
return protocolVersion;
}
}
Loading

0 comments on commit 2f944d9

Please sign in to comment.