Skip to content

Commit

Permalink
Remove 'com.syntaxphoenix.syntaxapi.version' depenedency
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Dec 23, 2023
1 parent 88bb2b6 commit ca39291
Show file tree
Hide file tree
Showing 8 changed files with 449 additions and 73 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,6 @@
<artifactId>command</artifactId>
<version>2.0.11</version>
</dependency>
<dependency>
<groupId>com.syntaxphoenix.syntaxapi</groupId>
<artifactId>version</artifactId>
<version>2.0.10</version>
</dependency>
<dependency>
<groupId>com.syntaxphoenix.syntaxapi</groupId>
<artifactId>reflection</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
Expand All @@ -21,13 +22,15 @@
import javax.tools.Diagnostic.Kind;
import javax.tools.JavaFileObject;

import org.bukkit.Bukkit;
import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.source.EnumConstantSource;
import org.jboss.forge.roaster.model.source.JavaEnumSource;

import com.syntaxphoenix.syntaxapi.version.DefaultVersion;
import com.syntaxphoenix.syntaxapi.version.Version;
import com.syntaxphoenix.syntaxapi.version.VersionAnalyzer;
import com.syntaxphoenix.spigot.smoothtimber.version.Version;
import com.syntaxphoenix.spigot.smoothtimber.version.VersionManager;
import com.syntaxphoenix.spigot.smoothtimber.version.VersionManager.VersionState;
import com.syntaxphoenix.spigot.smoothtimber.version.manager.VersionChanger;

public final class MCVersionEnumGenerator extends AbstractProcessor {

Expand Down Expand Up @@ -58,7 +61,6 @@ public boolean process(final Set<? extends TypeElement> annotations, final Round
final HashMap<String, ArrayList<Version>> map = new HashMap<>();
final ArrayList<String> types = new ArrayList<>();

final VersionAnalyzer analyzer = new DefaultVersion().getAnalyzer();
for (final Element element : roundEnv.getElementsAnnotatedWith(SupportedVersions.class)) {
if (element.getKind() != ElementKind.CLASS) {
continue;
Expand All @@ -72,12 +74,12 @@ public boolean process(final Set<? extends TypeElement> annotations, final Round
final ArrayList<Version> versions = new ArrayList<>();
map.put(typeName, versions);
for (final String supported : versionsSupported.value()) {
final Version version = analyzer.analyze(supported);
if (version.getMajor() == 0) {
final Version version = Version.fromString(supported);
if (version.major() == 0) {
messager.printMessage(Kind.WARNING, format("Failed to parse version '%s' of type '%s'", supported, typeName), element);
continue;
}
versions.add(analyzer.analyze(supported));
versions.add(version);
}
}

Expand All @@ -94,21 +96,37 @@ public boolean process(final Set<? extends TypeElement> annotations, final Round
final JavaEnumSource source = Roaster.create(JavaEnumSource.class);
source.setName("MCVersion");
source.setPackage("com.syntaxphoenix.spigot.smoothtimber.version.manager.gen");
source.addImport("com.syntaxphoenix.spigot.smoothtimber.version.manager.VersionChanger");
source.addImport("com.syntaxphoenix.syntaxapi.version.DefaultVersion");
source.addImport("com.syntaxphoenix.syntaxapi.version.Version");
source.addImport("com.syntaxphoenix.syntaxapi.version.VersionAnalyzer");
source.addImport("com.syntaxphoenix.syntaxapi.version.VersionManager");
source.addImport("com.syntaxphoenix.syntaxapi.version.VersionState");
source.addImport("java.util.ArrayList");
source.addImport("org.bukkit.Bukkit");
source.addNestedType(join("\n", "private static interface ChangerBuilder {", " public VersionChanger create();", "}"));
source.addNestedType(join("\n", "public static final class MCVersionResult {", " private final MCVersion version;",
" private final VersionState state;", " private MCVersionResult(MCVersion version, VersionState state) {",
" this.version = version;", " this.state = state;", " }", " public MCVersion version() { return version; }",
" public VersionState state() { return state; }", "}"));
source.addMethod(join("\n", "MCVersion(DefaultVersion[] versions, ChangerBuilder builder) {", " this.builder = builder;",
" versionManager.setAll(VersionState.SUPPORTED, versions);", "}")).setConstructor(true);
source.addImport(Version.class);
source.addImport(VersionManager.class);
source.addImport(VersionState.class);
source.addImport(VersionChanger.class);
source.addImport(ArrayList.class);
source.addImport(Collections.class);
source.addImport(List.class);
source.addImport(Bukkit.class);
source.addNestedType(join("\n", new String[] {
"private static interface ChangerBuilder {",
" public VersionChanger create();",
"}"
}));
source.addNestedType(join("\n", new String[] {
"public static final class MCVersionResult {",
" private final MCVersion version;",
" private final VersionState state;",
" private MCVersionResult(MCVersion version, VersionState state) {",
" this.version = version;",
" this.state = state;",
" }",
" public MCVersion version() { return version; }",
" public VersionState state() { return state; }",
"}"
}));
source.addMethod(join("\n", new String[] {
"MCVersion(Version[] versions, ChangerBuilder builder) {",
" this.builder = builder;",
" versionManager.set(VersionState.SUPPORTED, versions);",
"}"
})).setConstructor(true);
for (int index = 0; index < types.size(); index++) {
final String type = types.get(index);
final ArrayList<Version> versions = map.get(type);
Expand All @@ -119,50 +137,95 @@ public boolean process(final Set<? extends TypeElement> annotations, final Round
final String versionName = getVersionFromName(typeName);
final EnumConstantSource enumSource = source.addEnumConstant(versionName);

final StringBuilder versionArray = new StringBuilder("new DefaultVersion[] {");
final StringBuilder versionArray = new StringBuilder("new Version[] {");
for (final Version version : versions) {
versionArray.append("new DefaultVersion(");
versionArray.append(version.getMajor()).append(", ").append(version.getMinor()).append(", ").append(version.getPatch());
versionArray.append("new Version(");
versionArray.append(version.major()).append(", ").append(version.minor()).append(", ").append(version.patch());
versionArray.append("),");
}
enumSource.setConstructorArguments(versionArray.append("}").toString(), format("() -> new %s()", typeName));
}
source.addField("private static final VersionAnalyzer ANALYZER = new DefaultVersion().getAnalyzer();");
source.addField("private static final MCVersion[] ALL_VERSIONS = MCVersion.values();");
source.addField("private static DefaultVersion minecraftVersion;");
source.addField("private static Version minecraftVersion;");
source.addField("private static MCVersionResult coreVersionResult;");

source.addField(
"private final VersionManager versionManager = new VersionManager(VersionState.NOT_COMPATIBLE, VersionState.NOT_TESTED, VersionState.NOT_COMPATIBLE);");
"private final VersionManager versionManager = new VersionManager();");
source.addField("private final ChangerBuilder builder;");

source.addMethod(join("\n", "public VersionChanger create() {", " return builder.create();", "}"));

source.addMethod(
join("\n", "public VersionState getState(DefaultVersion version) {", " return versionManager.getState(version);", "}"));

source.addMethod(join("\n", "public ArrayList<DefaultVersion> getKnownSupported() {",
" return versionManager.getVersions(VersionState.SUPPORTED);", "}"));

source.addMethod(join("\n", "public static ArrayList<DefaultVersion> getAllKnownSupported() {",
" ArrayList<DefaultVersion> list = new ArrayList<>();", " for (MCVersion version : ALL_VERSIONS) {",
" list.addAll(version.getKnownSupported());", " }", " return list;", "}"));
source.addMethod(join("\n", "public static DefaultVersion getMinecraftVersion() {", " if (minecraftVersion != null) {",
" return minecraftVersion;", " }", " String versionStr = Bukkit.getVersion().split(\" \")[2].replace(\")\", \"\");",
" Version version = ANALYZER.analyze(versionStr);",
" if (version == null || version.getMajor() == 0 || !(version instanceof DefaultVersion)) {",
" throw new IllegalStateException(String.format(\"Minecraft version is invalid: '%s'\", versionStr));", " }",
" return (minecraftVersion = (DefaultVersion) version);", "}"));
source.addMethod(join("\n", "public static MCVersion getCoreVersion() {", " return getCoreVersionResult().version();", "}"));
source.addMethod(join("\n", "public static MCVersionResult getCoreVersionResult() {", " if (coreVersionResult != null) {",
" return coreVersionResult;", " }", " return (coreVersionResult = detectCoreVersion(getMinecraftVersion()));", "}"));
source.addMethod(join("\n", "public static MCVersionResult detectCoreVersion(DefaultVersion version) {",
" if (ALL_VERSIONS.length == 0) {", " throw new IllegalStateException(\"No core version available\");", " }",
" MCVersion selected = ALL_VERSIONS[0];", " VersionState selectedState = VersionState.NOT_COMPATIBLE;",
" for (MCVersion current : ALL_VERSIONS) {", " VersionState state = current.getState(version);",
" if (state == VersionState.NOT_COMPATIBLE) {", " break;", " }", " selected = current;",
" if ((selectedState = state) == VersionState.SUPPORTED || state == VersionState.NOT_SUPPORTED) {", " break;",
" }", " }", " return new MCVersionResult(selected, selectedState);", "}"));
source.addMethod(join("\n", new String[] {
"public VersionChanger create() {",
" return builder.create();",
"}"
}));

source.addMethod(join("\n", new String[] {
"public VersionState getState(Version version) {",
" return versionManager.state(version);",
"}"
}));

source.addMethod(join("\n", new String[] {
"public List<Version> getKnownSupported() {",
" return versionManager.get(VersionState.SUPPORTED);",
"}"
}));

source.addMethod(join("\n", new String[] {
"public static List<Version> getAllKnownSupported() {",
" ArrayList<Version> list = new ArrayList<>();",
" for (MCVersion version : ALL_VERSIONS) {",
" list.addAll(version.getKnownSupported());",
" }",
" return Collections.unmodifiableList(list);",
"}"
}));
source.addMethod(join("\n", new String[] {
"public static Version getMinecraftVersion() {",
" if (minecraftVersion != null) {",
" return minecraftVersion;",
" }",
" String versionStr = Bukkit.getVersion().split(\" \")[2].replace(\")\", \"\");",
" Version version = Version.fromString(versionStr);",
" if (version == null || version.major() == 0) {",
" throw new IllegalStateException(String.format(\"Minecraft version is invalid: '%s'\", versionStr));",
" }",
" return (minecraftVersion = version);",
"}"
}));
source.addMethod(join("\n", new String[] {
"public static MCVersion getCoreVersion() {",
" return getCoreVersionResult().version();",
"}"
}));
source.addMethod(join("\n", new String[] {
"public static MCVersionResult getCoreVersionResult() {",
" if (coreVersionResult != null) {",
" return coreVersionResult;",
" }",
" return (coreVersionResult = detectCoreVersion(getMinecraftVersion()));",
"}"
}));
source.addMethod(join("\n", new String[] {
"public static MCVersionResult detectCoreVersion(Version version) {",
" if (ALL_VERSIONS.length == 0) {",
" throw new IllegalStateException(\"No core version available\");",
" }",
" MCVersion selected = ALL_VERSIONS[0];",
" VersionState selectedState = VersionState.UNSUPPORTED;",
" for (MCVersion current : ALL_VERSIONS) {",
" VersionState state = current.getState(version);",
" if (state == VersionState.INCOMPATIBLE) {",
" break;",
" }",
" selected = current;",
" if ((selectedState = state) == VersionState.SUPPORTED || state == VersionState.UNSUPPORTED) {",
" break;",
" }",
" }",
" return new MCVersionResult(selected, selectedState);",
"}"
}));

try {
final JavaFileObject file = processingEnv.getFiler().createSourceFile(source.getPackage() + '.' + source.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class BlockyLog extends CompatibilityAddon {

@Override
public void onEnable(final PluginPackage pluginPackage, final SmoothTimber smoothTimber) throws Exception {
switch (pluginPackage.getVersion().getMajor()) {
switch (pluginPackage.getVersion().major()) {
case 1:
resolver = new BlockyLogResolver_v1_x();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.syntaxphoenix.spigot.smoothtimber.compatibility.jobsreborn.adapter.AdapterLegacy4_17;
import com.syntaxphoenix.spigot.smoothtimber.utilities.Container;
import com.syntaxphoenix.spigot.smoothtimber.utilities.plugin.PluginPackage;
import com.syntaxphoenix.syntaxapi.version.Version;
import com.syntaxphoenix.spigot.smoothtimber.version.Version;

public class JobsReborn extends CompatibilityAddon {

Expand All @@ -30,7 +30,7 @@ public void onEnable(final PluginPackage pluginPackage, final SmoothTimber smoot

private JobAdapter getAdapter(final PluginPackage pluginPackage) {
final Version version = pluginPackage.getVersion();
if (version.getMajor() < 4 || version.getMajor() == 4 && version.getMinor() <= 16) {
if (version.major() < 4 || version.major() == 4 && version.minor() <= 16) {
return new AdapterLegacy4_16();
}
return new AdapterLegacy4_17();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import org.bukkit.plugin.Plugin;

import com.syntaxphoenix.spigot.smoothtimber.version.Version;
import com.syntaxphoenix.syntaxapi.reflection.ReflectCache;
import com.syntaxphoenix.syntaxapi.version.DefaultVersion;
import com.syntaxphoenix.syntaxapi.version.Version;
import com.syntaxphoenix.syntaxapi.version.VersionAnalyzer;

public class PluginPackage {

public static final VersionAnalyzer ANALYZER = new DefaultVersion().getAnalyzer();

private ReflectCache cache = new ReflectCache();

private Version version;
Expand Down Expand Up @@ -38,7 +34,7 @@ final void update(final Plugin plugin) {
this.plugin = plugin;
this.name = plugin.getName();
this.versionRaw = plugin.getDescription().getVersion();
this.version = ANALYZER.analyze(versionRaw.contains("[") ? versionRaw.split("\\[")[0] : versionRaw);
this.version = Version.fromString(versionRaw.contains("[") ? versionRaw.split("\\[")[0] : versionRaw);
}

/*
Expand Down
Loading

0 comments on commit ca39291

Please sign in to comment.