CompatibilityLayer framework

This commit is contained in:
nossr50
2020-04-24 20:21:21 -07:00
parent f82ad99c82
commit 5984230bf3
37 changed files with 1292 additions and 123 deletions

View File

@ -0,0 +1,23 @@
package com.gmail.nossr50.util.platform;
import com.gmail.nossr50.util.compat.CompatibilityManager;
/**
*
* These classes are a band-aid solution for adding NMS support into 2.1.XXX
* In 2.2 we are switching to modules and that will clean things up significantly
*
*/
public abstract class AbstractPlatform implements Platform{
protected final CompatibilityManager compatibilityManager;
protected final MinecraftGameVersion minecraftGameVersion;
protected final ServerSoftwareType serverSoftwareType;
public AbstractPlatform(MinecraftGameVersion minecraftGameVersion, ServerSoftwareType serverSoftwareType, CompatibilityManager compatibilityManager) {
this.minecraftGameVersion = minecraftGameVersion;
this.serverSoftwareType = serverSoftwareType;
this.compatibilityManager = compatibilityManager;
}
}

View File

@ -0,0 +1,32 @@
package com.gmail.nossr50.util.platform;
import com.gmail.nossr50.util.compat.CompatibilityManager;
import org.jetbrains.annotations.NotNull;
/**
*
* These classes are a band-aid solution for adding NMS support into 2.1.XXX
* In 2.2 we are switching to modules and that will clean things up significantly
*
*/
public class BukkitPlatform extends AbstractPlatform {
public BukkitPlatform(MinecraftGameVersion minecraftGameVersion) {
super(minecraftGameVersion, ServerSoftwareType.CRAFT_BUKKIT, new CompatibilityManager(minecraftGameVersion));
}
@Override
public @NotNull ServerSoftwareType getServerSoftwareType() {
return super.serverSoftwareType;
}
@Override
public @NotNull CompatibilityManager getCompatibilityManager() {
return compatibilityManager;
}
@Override
public @NotNull MinecraftGameVersion getGameVersion() {
return super.minecraftGameVersion;
}
}

View File

@ -0,0 +1,113 @@
package com.gmail.nossr50.util.platform;
import com.gmail.nossr50.util.platform.version.SimpleNumericVersion;
import com.gmail.nossr50.util.platform.version.Versioned;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
/**
*
* These classes are a band-aid solution for adding NMS support into 2.1.XXX
* In 2.2 we are switching to modules and that will clean things up significantly
*
*/
public abstract class MajorMinorPatchVersion implements Versioned {
@NotNull
private final SimpleNumericVersion majorVersion;
@NotNull
private final SimpleNumericVersion minorVersion;
@NotNull
private final SimpleNumericVersion patchVersion;
public MajorMinorPatchVersion(@NotNull SimpleNumericVersion majorVersion, @NotNull SimpleNumericVersion minorVersion) {
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
this.patchVersion = new SimpleNumericVersion(0);
}
public MajorMinorPatchVersion(@NotNull SimpleNumericVersion majorVersion, @NotNull SimpleNumericVersion minorVersion, @Nullable SimpleNumericVersion patchVersion) {
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
if(patchVersion == null) {
this.patchVersion = new SimpleNumericVersion(0);
} else {
this.patchVersion = patchVersion;
}
}
public MajorMinorPatchVersion(int majorVerNumber, int minorVerNumber, int patchVerNumber) {
this.majorVersion = new SimpleNumericVersion(majorVerNumber);
this.minorVersion = new SimpleNumericVersion(minorVerNumber);
this.patchVersion = new SimpleNumericVersion(patchVerNumber);
}
public MajorMinorPatchVersion(int majorVerNumber, int minorVerNumber) {
this.majorVersion = new SimpleNumericVersion(majorVerNumber);
this.minorVersion = new SimpleNumericVersion(minorVerNumber);
this.patchVersion = new SimpleNumericVersion(0);
}
/**
* Get the major version string
* @return the major version string
*/
public @NotNull SimpleNumericVersion getMajorVersion() {
return majorVersion;
}
/**
* Get the minor version string
* @return the minor version string
*/
public @NotNull SimpleNumericVersion getMinorVersion() {
return minorVersion;
}
/**
* Get the patch version string
* @return patch version string or null if patch numeric value is less than or equal to 0
*/
public @NotNull SimpleNumericVersion getPatchVersion() {
return patchVersion;
}
@Override
public String getVersionStr() {
if(isPatch()) {
return majorVersion.getVersionString() + "."
+ minorVersion + "."
+ patchVersion;
} else {
return majorVersion.getVersionString() + "."
+ minorVersion.getVersionString();
}
}
/**
* Whether or not this version of Minecraft is a patch
* a patch version value above 0 will indicate that this is a patch
* @return true if this version is a patch
*/
public boolean isPatch() {
return patchVersion.asInt() > 0;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MajorMinorPatchVersion that = (MajorMinorPatchVersion) o;
return majorVersion.equals(that.majorVersion) &&
minorVersion.equals(that.minorVersion) &&
patchVersion.equals(that.patchVersion);
}
@Override
public int hashCode() {
return Objects.hash(majorVersion, minorVersion, patchVersion);
}
}

View File

@ -0,0 +1,30 @@
package com.gmail.nossr50.util.platform;
import com.gmail.nossr50.util.platform.version.SimpleNumericVersion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
*
* These classes are a band-aid solution for adding NMS support into 2.1.XXX
* In 2.2 we are switching to modules and that will clean things up significantly
*
*/
public class MinecraftGameVersion extends MajorMinorPatchVersion {
public MinecraftGameVersion(@NotNull SimpleNumericVersion majorVersion, @NotNull SimpleNumericVersion minorVersion) {
super(majorVersion, minorVersion);
}
public MinecraftGameVersion(@NotNull SimpleNumericVersion majorVersion, @NotNull SimpleNumericVersion minorVersion, @Nullable SimpleNumericVersion patchVersion) {
super(majorVersion, minorVersion, patchVersion);
}
public MinecraftGameVersion(int majorVerNumber, int minorVerNumber, int patchVerNumber) {
super(majorVerNumber, minorVerNumber, patchVerNumber);
}
public MinecraftGameVersion(int majorVerNumber, int minorVerNumber) {
super(majorVerNumber, minorVerNumber);
}
}

View File

@ -0,0 +1,32 @@
package com.gmail.nossr50.util.platform;
import com.gmail.nossr50.util.compat.CompatibilityManager;
import org.jetbrains.annotations.NotNull;
/**
*
* These classes are a band-aid solution for adding NMS support into 2.1.XXX
* In 2.2 we are switching to modules and that will clean things up significantly
*
*/
public interface Platform {
/**
* Target {@link ServerSoftwareType} for this {@link Platform}
* @return the {@link ServerSoftwareType} for this {@link Platform}
*/
@NotNull ServerSoftwareType getServerSoftwareType();
/**
* Get the {@link CompatibilityManager} for this {@link Platform}
* @return the {@link CompatibilityManager} for this platform
*/
@NotNull CompatibilityManager getCompatibilityManager();
/**
* The target game version of this {@link Platform}
* @return the target {@link MinecraftGameVersion} of this {@link Platform}
*/
@NotNull MinecraftGameVersion getGameVersion();
}

View File

@ -0,0 +1,45 @@
package com.gmail.nossr50.util.platform;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
*
* These classes are a band-aid solution for adding NMS support into 2.1.XXX
* In 2.2 we are switching to modules and that will clean things up significantly
*
*/
public class PlatformBuilder {
private MinecraftGameVersion minecraftGameVersion;
private ServerSoftwareType serverSoftwareType;
public PlatformBuilder() {
}
public PlatformBuilder setMinecraftGameVersion(@NotNull MinecraftGameVersion minecraftGameVersion) {
this.minecraftGameVersion = minecraftGameVersion;
return this;
}
public PlatformBuilder setSoftwareType(@NotNull ServerSoftwareType softwareType) {
this.serverSoftwareType = softwareType;
return this;
}
public @Nullable Platform build() {
switch(serverSoftwareType) {
case PAPER:
case SPIGOT:
case CRAFT_BUKKIT:
return createBukkitPlatform();
default:
return null;
}
}
private BukkitPlatform createBukkitPlatform() {
return new BukkitPlatform(minecraftGameVersion);
}
}

View File

@ -0,0 +1,130 @@
package com.gmail.nossr50.util.platform;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.compat.CompatibilityManager;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Locale;
import java.util.stream.Collectors;
/**
*
* These classes are a band-aid solution for adding NMS support into 2.1.XXX
* In 2.2 we are switching to modules and that will clean things up significantly
*
*/
public class PlatformManager {
protected Platform platform; //current platform
public PlatformManager() {
init();
}
private void init() {
platform = loadPlatform();
}
public Platform getPlatform() {
return platform;
}
private @Nullable Platform loadPlatform() {
ServerSoftwareType serverSoftwareType = determinePlatformType();
PlatformBuilder platformBuilder = new PlatformBuilder();
MinecraftGameVersion gameVersion = determineGameVersion(Bukkit.getBukkitVersion());
return platformBuilder
.setMinecraftGameVersion(gameVersion)
.setSoftwareType(serverSoftwareType)
.build();
}
//TODO: make this work on things other than bukkit
@Deprecated //Only good for determining bukkit game versions
private @NotNull MinecraftGameVersion determineGameVersion(String platformVersionString) {
int major = 0, minor = 0, patch = 0;
String[] splitVersion = platformVersionString.split("\\.", 3);
mcMMO.p.getLogger().info("Platform String: " + platformVersionString);
//TODO: this is very hacky and probably isn't reliable
//Grab all consecutive digits
major = getSubsequentDigits(splitVersion[0].toCharArray(), 0);
minor = getSubsequentDigits(splitVersion[1].toCharArray(), 0);
//Not all versions of Minecraft have a patch digit
//If the first character isn't a digit it's not a patch number and its some crap we don't care about
if(splitVersion.length > 2 && Character.isDigit(splitVersion[2].toCharArray()[0]))
patch = getSubsequentDigits(splitVersion[2].toCharArray(), 0);
mcMMO.p.getLogger().info("Minecraft version determined to be - "
+ major + "."
+ minor + "."
+ patch);
return new MinecraftGameVersion(major, minor, patch);
}
/**
* Get all consecutive digits in a char array from position
* @param charArray target char array
* @param position starting position
* @return all consecutive digits from position
*/
private int getSubsequentDigits(char[] charArray, int position) {
ArrayList<Character> digitArrayList = new ArrayList<>();
do {
if(Character.isDigit(charArray[position])) {
digitArrayList.add(charArray[position]);
position++;
} else {
break;
}
} while (position < charArray.length);
//Convert List<Character> -> String
String digits = digitArrayList
.stream()
.map(String::valueOf)
.collect(Collectors.joining());
//Copy value
return Integer.parseInt(digits);
}
//TODO: Rewrite this properly once we actually support a not-bukkit platform
private @NotNull ServerSoftwareType determinePlatformType() {
if(Bukkit.getVersion().toLowerCase(Locale.ENGLISH).contains("paper"))
return ServerSoftwareType.PAPER;
else if(Bukkit.getVersion().toLowerCase(Locale.ENGLISH).contains("spigot"))
return ServerSoftwareType.SPIGOT;
else
return ServerSoftwareType.CRAFT_BUKKIT;
}
public ServerSoftwareType getServerSoftware()
{
return platform.getServerSoftwareType();
}
public String getServerSoftwareStr()
{
switch(getServerSoftware())
{
case PAPER:
return "Paper";
case SPIGOT:
return "Spigot";
default:
return "CraftBukkit";
}
}
public @Nullable CompatibilityManager getCompatibilityManager() {
return platform.getCompatibilityManager();
}
}

View File

@ -0,0 +1,9 @@
package com.gmail.nossr50.util.platform;
public enum ServerSoftwareType {
PAPER,
SPIGOT,
CRAFT_BUKKIT,
SPONGE,
OTHER
}

View File

@ -0,0 +1,5 @@
package com.gmail.nossr50.util.platform.version;
public interface NumericVersioned extends Versioned {
int asInt();
}

View File

@ -0,0 +1,22 @@
package com.gmail.nossr50.util.platform.version;
import org.jetbrains.annotations.NotNull;
public class SimpleNumericVersion extends SimpleVersion implements NumericVersioned {
private int versionNumber;
public SimpleNumericVersion(int versionNumber) {
super(String.valueOf(versionNumber));
this.versionNumber = versionNumber;
}
@Override
public int asInt() {
return versionNumber;
}
@Override
public @NotNull String getVersionStr() {
return super.getVersionString();
}
}

View File

@ -0,0 +1,17 @@
package com.gmail.nossr50.util.platform.version;
import org.jetbrains.annotations.NotNull;
public class SimpleVersion {
@NotNull
private final String versionString;
public SimpleVersion(@NotNull String versionString) {
this.versionString = versionString;
}
@NotNull
public String getVersionString() {
return versionString;
}
}

View File

@ -0,0 +1,5 @@
package com.gmail.nossr50.util.platform.version;
public interface Versioned {
String getVersionStr();
}