Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Dec 23, 2023
1 parent da7b9c3 commit 88bb2b6
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;
import javax.tools.JavaFileObject;

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

import javax.tools.JavaFileObject;

import com.syntaxphoenix.syntaxapi.version.DefaultVersion;
import com.syntaxphoenix.syntaxapi.version.Version;
import com.syntaxphoenix.syntaxapi.version.VersionAnalyzer;
Expand All @@ -52,47 +51,47 @@ public Set<String> getSupportedAnnotationTypes() {
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
return false;
}
HashMap<String, ArrayList<Version>> map = new HashMap<>();
ArrayList<String> types = new ArrayList<>();
VersionAnalyzer analyzer = new DefaultVersion().getAnalyzer();
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;
}
String typeName = element.asType().toString();
final String typeName = element.asType().toString();
if (types.contains(typeName)) {
continue;
}
SupportedVersions versionsSupported = element.getAnnotation(SupportedVersions.class);
final SupportedVersions versionsSupported = element.getAnnotation(SupportedVersions.class);
types.add(typeName);
ArrayList<Version> versions = new ArrayList<>();
final ArrayList<Version> versions = new ArrayList<>();
map.put(typeName, versions);
for (String supported : versionsSupported.value()) {
Version version = analyzer.analyze(supported);
for (final String supported : versionsSupported.value()) {
final Version version = analyzer.analyze(supported);
if (version.getMajor() == 0) {
messager.printMessage(Kind.WARNING, format("Failed to parse version '%s' of type '%s'", supported, typeName), element);
continue;
}
versions.add(analyzer.analyze(supported));
}
}

types.sort((t1, t2) -> {
ArrayList<Version> v1 = map.get(t1);
ArrayList<Version> v2 = map.get(t2);
int comp = Boolean.compare(v1.isEmpty(), v2.isEmpty());
final ArrayList<Version> v1 = map.get(t1);
final ArrayList<Version> v2 = map.get(t2);
final int comp = Boolean.compare(v1.isEmpty(), v2.isEmpty());
if (comp != 0) {
return comp;
}
return v1.get(0).compareTo(v2.get(0));
});
JavaEnumSource source = Roaster.create(JavaEnumSource.class);

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");
Expand All @@ -103,41 +102,25 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
source.addImport("com.syntaxphoenix.syntaxapi.version.VersionState");
source.addImport("java.util.ArrayList");
source.addImport("org.bukkit.Bukkit");
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(DefaultVersion[] versions, ChangerBuilder builder) {",
" this.builder = builder;",
" versionManager.setAll(VersionState.SUPPORTED, versions);",
"}"
})).setConstructor(true);
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);
for (int index = 0; index < types.size(); index++) {
String type = types.get(index);
ArrayList<Version> versions = map.get(type);
final String type = types.get(index);
final ArrayList<Version> versions = map.get(type);

source.addImport(type);

String typeName = getNameFromType(type);
String versionName = getVersionFromName(typeName);
EnumConstantSource enumSource = source.addEnumConstant(versionName);
StringBuilder versionArray = new StringBuilder("new DefaultVersion[] {");
for (Version version : versions) {
final String typeName = getNameFromType(type);
final String versionName = getVersionFromName(typeName);
final EnumConstantSource enumSource = source.addEnumConstant(versionName);

final StringBuilder versionArray = new StringBuilder("new DefaultVersion[] {");
for (final Version version : versions) {
versionArray.append("new DefaultVersion(");
versionArray.append(version.getMajor()).append(", ").append(version.getMinor()).append(", ").append(version.getPatch());
versionArray.append("),");
Expand All @@ -149,104 +132,59 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
source.addField("private static DefaultVersion minecraftVersion;");
source.addField("private static MCVersionResult coreVersionResult;");

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

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

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

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

source.addMethod(join("\n", new String[] {
"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", new String[] {
"public static DefaultVersion getMinecraftVersion() {",
" if (minecraftVersion != null) {",
" return minecraftVersion;",
" }",
" String versionStr = Bukkit.getVersion().split(\" \")[2].replace(\")\", \"\");",

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", 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(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);",
"}"
}));

" 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);", "}"));

try {
JavaFileObject file = processingEnv.getFiler().createSourceFile(source.getPackage() + '.' + source.getName());
final JavaFileObject file = processingEnv.getFiler().createSourceFile(source.getPackage() + '.' + source.getName());
try (Writer writer = file.openWriter()) {
writer.write(source.toString());
}
} catch (IOException e) {
} catch (final IOException e) {
}

return false;
}

/*
* Helper
*/
private String getNameFromType(String typeName) {
String[] parts = typeName.split("\\.");

private String getNameFromType(final String typeName) {
final String[] parts = typeName.split("\\.");
return parts[parts.length - 1];
}
private String getVersionFromName(String changerName) {

private String getVersionFromName(final String changerName) {
return changerName.split("x", 2)[0] + 'x';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Retention(SOURCE)
@Target(TYPE)
public @interface SupportedVersions {

public String[] value() default {};

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void execute(final MinecraftInfo info, final Arguments arguments) {
return;
}

if ((!sender.hasPermission("smoothtimber.toggle") && !sender.hasPermission("smoothtimber.*"))) {
if (!sender.hasPermission("smoothtimber.toggle") && !sender.hasPermission("smoothtimber.*")) {
sender.sendMessage(Message.GLOBAL_PREFIX.colored() + ' ' + Message.COMMAND_MISSING222PERMISSION.colored(new String[] {
"%permission%",
"smoothtimber.toggle"
Expand All @@ -41,7 +41,7 @@ public void execute(final MinecraftInfo info, final Arguments arguments) {

int time = -1;

if ((arguments.count() != 0) && (arguments.getType(1) == ArgumentType.INTEGER || arguments.getType(1) == ArgumentType.BYTE
if (arguments.count() != 0 && (arguments.getType(1) == ArgumentType.INTEGER || arguments.getType(1) == ArgumentType.BYTE
|| arguments.getType(1) == ArgumentType.SHORT)) {
time = arguments.get(1).asNumeric().asNumber().intValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import java.util.Map;
import java.util.Optional;

import com.syntaxphoenix.spigot.smoothtimber.compatibility.factionsuuid.FactionsUUID;
import org.bukkit.plugin.java.JavaPlugin;

import com.syntaxphoenix.spigot.smoothtimber.SmoothTimber;
import com.syntaxphoenix.spigot.smoothtimber.compatibility.blockylog.BlockyLog;
import com.syntaxphoenix.spigot.smoothtimber.compatibility.coreprotect.CoreProtect;
import com.syntaxphoenix.spigot.smoothtimber.compatibility.factionsuuid.FactionsUUID;
import com.syntaxphoenix.spigot.smoothtimber.compatibility.griefprevention.GriefPrevention;
import com.syntaxphoenix.spigot.smoothtimber.compatibility.jobsreborn.JobsReborn;
import com.syntaxphoenix.spigot.smoothtimber.compatibility.lands.Lands;
Expand Down Expand Up @@ -103,7 +103,7 @@ public static class CompatAddon<E extends CompatibilityAddon> {

private final Class<E> owner;
private final Reflect reflect;

private final String name;

private CompatibilityAddon instance;
Expand Down Expand Up @@ -132,20 +132,23 @@ void start(final PluginPackage pluginPackage) {
}
instance = (CompatibilityAddon) reflect.init();
try {
PluginUtils.sendConsoleMessage(true, "&7Trying to enable compatibility addon '&3" + name + "&7' for plugin '&3" + pluginPackage.getName() + "&7'...");
PluginUtils.sendConsoleMessage(true,
"&7Trying to enable compatibility addon '&3" + name + "&7' for plugin '&3" + pluginPackage.getName() + "&7'...");
instance.onEnable(pluginPackage, JavaPlugin.getPlugin(SmoothTimber.class));
if (instance.hasConfig()) {
PluginUtils.sendConsoleMessage(true, "&7Trying to load config of compatibility addon '&3" + name + "&7' for plugin '&3" + pluginPackage.getName() + "&7'...");
PluginUtils.sendConsoleMessage(true, "&7Trying to load config of compatibility addon '&3" + name + "&7' for plugin '&3"
+ pluginPackage.getName() + "&7'...");
instance.getConfig().reload();
}
PluginUtils.sendConsoleMessage(true, "&7Successfully enabled compatbility addon '&3" + name + "&7' for plugin '&3" + pluginPackage.getName() + "&7'...");
PluginUtils.sendConsoleMessage(true,
"&7Successfully enabled compatbility addon '&3" + name + "&7' for plugin '&3" + pluginPackage.getName() + "&7'...");
} catch (final IncompatiblePluginException pluginException) {
instance = null;
PluginUtils.sendConsoleMessage(true, Exceptions.stackTraceToString(pluginException));
} catch (final Exception exception) {
instance = null;
PluginUtils.sendConsoleMessage(true, "&7Failed to enable compatibility addon '&3" + name
+ "&7' for plugin '&3" + pluginPackage.getName() + "&7'!");
PluginUtils.sendConsoleMessage(true,
"&7Failed to enable compatibility addon '&3" + name + "&7' for plugin '&3" + pluginPackage.getName() + "&7'!");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public class IncompatiblePluginException extends RuntimeException {
* message. The cause is not initialized, and may subsequently be initialized by
* a call to {@link #initCause}.
*/
public IncompatiblePluginException() {
}
public IncompatiblePluginException() {}

/**
* Constructs a new IncompatiblePluginException with the specified detail
Expand Down
Loading

0 comments on commit 88bb2b6

Please sign in to comment.