Finish factory implementation
This commit is contained in:
parent
eec7b2fcbb
commit
0b753d6a4e
15 changed files with 75 additions and 52 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -10,3 +10,5 @@ out/
|
||||||
|
|
||||||
# Ignore Gradle build output directory
|
# Ignore Gradle build output directory
|
||||||
build
|
build
|
||||||
|
|
||||||
|
run/
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
package org.adrianvictor.lib;
|
package org.adrianvictor.lib;
|
||||||
|
|
||||||
import org.adrianvictor.lib.reflection.VersionMatcher;
|
import org.adrianvictor.lib.versioning.DefaultVersionedServiceFactory;
|
||||||
|
import org.adrianvictor.lib.versioning.VersionMatcher;
|
||||||
|
import org.adrianvictor.lib.versioning.VersionedServiceFactory;
|
||||||
|
import org.adrianvictor.lib.versioning.VersionedServiceRegistrar;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
|
@ -19,14 +20,15 @@ class DummyRegistrarB1_7_3 implements VersionedServiceRegistrar {
|
||||||
@Override
|
@Override
|
||||||
public void register(VersionedServiceFactory factory) {
|
public void register(VersionedServiceFactory factory) {
|
||||||
factory.register(TestService.class, "b1_7_3", TestServiceB1_7_3::new);
|
factory.register(TestService.class, "b1_7_3", TestServiceB1_7_3::new);
|
||||||
factory.register(TestService.class, "b1_8_compat", TestServiceB1_7_3::new); // Register for b1_8_compat
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DummyRegistrarR1_21 implements VersionedServiceRegistrar {
|
class DummyRegistrarR1_21 implements VersionedServiceRegistrar {
|
||||||
@Override
|
@Override
|
||||||
public void register(VersionedServiceFactory factory) {
|
public void register(VersionedServiceFactory factory) {
|
||||||
factory.register(TestService.class, "r1_21", TestServiceR1_21::new);
|
factory.register(TestService.class, "r1_1", TestServiceR1_21::new); // Registers for r1_1 compat
|
||||||
|
factory.register(TestService.class, "r1_16_5", TestServiceR1_21::new); // Registers for r1_16_5 compat
|
||||||
|
factory.register(TestService.class, "r1_21", TestServiceR1_21::new); // Registers for r1_21
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,6 +61,40 @@ public class DefaultVersionedServiceFactoryTest {
|
||||||
assertTrue(service instanceof TestServiceB1_7_3);
|
assertTrue(service instanceof TestServiceB1_7_3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testServiceRegistrationAndRetrievalForR1_1() {
|
||||||
|
// Mock VersionMatcher to return r1_1 suffix for a 1.10.2 server
|
||||||
|
when(mockVersionMatcher.getServerVersion()).thenReturn("1.10.2");
|
||||||
|
when(mockVersionMatcher.getClassSuffix(eq("1.10.2"))).thenReturn("r1_1");
|
||||||
|
|
||||||
|
// Register the dummy service for R1_21 (which covers r1_1 compat)
|
||||||
|
new DummyRegistrarR1_21().register(factory);
|
||||||
|
|
||||||
|
// Retrieve the service
|
||||||
|
TestService service = factory.getService(TestService.class);
|
||||||
|
|
||||||
|
// Assert that the correct compatible version is returned
|
||||||
|
assertNotNull(service);
|
||||||
|
assertTrue(service instanceof TestServiceR1_21);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testServiceRegistrationAndRetrievalForR1_16_5() {
|
||||||
|
// Mock VersionMatcher to return r1_16_5 suffix for a 1.18.1 server
|
||||||
|
when(mockVersionMatcher.getServerVersion()).thenReturn("1.18.1");
|
||||||
|
when(mockVersionMatcher.getClassSuffix(eq("1.18.1"))).thenReturn("r1_16_5");
|
||||||
|
|
||||||
|
// Register the dummy service for R1_21 (which covers r1_16_5 compat)
|
||||||
|
new DummyRegistrarR1_21().register(factory);
|
||||||
|
|
||||||
|
// Retrieve the service
|
||||||
|
TestService service = factory.getService(TestService.class);
|
||||||
|
|
||||||
|
// Assert that the correct compatible version is returned
|
||||||
|
assertNotNull(service);
|
||||||
|
assertTrue(service instanceof TestServiceR1_21);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testServiceRegistrationAndRetrievalForR1_21() {
|
void testServiceRegistrationAndRetrievalForR1_21() {
|
||||||
// Mock VersionMatcher to return r1_21 suffix
|
// Mock VersionMatcher to return r1_21 suffix
|
||||||
|
|
@ -76,23 +112,6 @@ public class DefaultVersionedServiceFactoryTest {
|
||||||
assertTrue(service instanceof TestServiceR1_21);
|
assertTrue(service instanceof TestServiceR1_21);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void testServiceRegistrationAndRetrievalForB1_8_Compat() {
|
|
||||||
// Mock VersionMatcher to return b1_8_compat suffix for a 1.16.5 server
|
|
||||||
when(mockVersionMatcher.getServerVersion()).thenReturn("1.16.5");
|
|
||||||
when(mockVersionMatcher.getClassSuffix(eq("1.16.5"))).thenReturn("b1_8_compat");
|
|
||||||
|
|
||||||
// Register the dummy service for b1_7_3 (which also covers b1_8_compat)
|
|
||||||
new DummyRegistrarB1_7_3().register(factory);
|
|
||||||
|
|
||||||
// Retrieve the service
|
|
||||||
TestService service = factory.getService(TestService.class);
|
|
||||||
|
|
||||||
// Assert that the correct compatible version is returned
|
|
||||||
assertNotNull(service);
|
|
||||||
assertTrue(service instanceof TestServiceB1_7_3);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testNoServiceRegisteredThrowsException() {
|
void testNoServiceRegisteredThrowsException() {
|
||||||
// Mock VersionMatcher to return an unknown suffix
|
// Mock VersionMatcher to return an unknown suffix
|
||||||
|
|
@ -114,7 +133,7 @@ public class DefaultVersionedServiceFactoryTest {
|
||||||
when(mockVersionMatcher.getClassSuffix(eq("1.7.3"))).thenReturn("b1_7_3");
|
when(mockVersionMatcher.getClassSuffix(eq("1.7.3"))).thenReturn("b1_7_3");
|
||||||
|
|
||||||
// Register R1_21 service, but not B1_7_3
|
// Register R1_21 service, but not B1_7_3
|
||||||
new DummyRegistrarR1_21().register(factory);
|
new DummyRegistrarR1_21().register(factory); // R1_21 registrar doesn't register b1_7_3
|
||||||
|
|
||||||
// Attempt to retrieve b1_7_3 service
|
// Attempt to retrieve b1_7_3 service
|
||||||
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->
|
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
plugins {
|
plugins {
|
||||||
java
|
java
|
||||||
id("xyz.jpenilla.run-paper") version "2.3.1"
|
id("xyz.jpenilla.run-paper") version "3.0.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "org.adrianvictor"
|
group = "org.adrianvictor"
|
||||||
|
|
@ -70,7 +70,7 @@ dependencies {
|
||||||
/* ----------------------------------------- */
|
/* ----------------------------------------- */
|
||||||
|
|
||||||
mcVersions.forEach { ver ->
|
mcVersions.forEach { ver ->
|
||||||
tasks.register<Jar>("jar${ver.replace(".", "_").replace("-", "_").replace("/", "_").capitalize()}") {
|
tasks.register<Jar>("jar${ver.replace(".", "_").replace("-", "_").replace("/", "_").replaceFirstChar { it.uppercase() }}") {
|
||||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||||
from(sourceSets["main"].output)
|
from(sourceSets["main"].output)
|
||||||
from(sourceSets[ver].output)
|
from(sourceSets[ver].output)
|
||||||
|
|
@ -127,3 +127,12 @@ java {
|
||||||
tasks.withType<JavaCompile> {
|
tasks.withType<JavaCompile> {
|
||||||
options.encoding = "UTF-8"
|
options.encoding = "UTF-8"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ----------------------------------------- */
|
||||||
|
/* RUN SETTINGS */
|
||||||
|
/* ----------------------------------------- */
|
||||||
|
|
||||||
|
tasks.runServer {
|
||||||
|
minecraftVersion("1.21.1")
|
||||||
|
pluginJars.from(tasks.named("jarR1_21"))
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package org.adrianvictor.lib.impl.b1_7_3;
|
package org.adrianvictor.lib.impl.b1_7_3;
|
||||||
|
|
||||||
import org.adrianvictor.lib.VersionedServiceFactory;
|
import org.adrianvictor.lib.versioning.VersionedServiceFactory;
|
||||||
import org.adrianvictor.lib.VersionedServiceRegistrar;
|
import org.adrianvictor.lib.versioning.VersionedServiceRegistrar;
|
||||||
import org.adrianvictor.lib.configuration.provider.ConfigurationProvider;
|
import org.adrianvictor.lib.configuration.provider.ConfigurationProvider;
|
||||||
import org.adrianvictor.lib.impl.b1_7_3.configuration.Configuration;
|
import org.adrianvictor.lib.impl.b1_7_3.configuration.Configuration;
|
||||||
import org.adrianvictor.lib.text.provider.TextColorProvider;
|
import org.adrianvictor.lib.text.provider.TextColorProvider;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
package org.adrianvictor.lib;
|
package org.adrianvictor.lib;
|
||||||
|
|
||||||
import org.adrianvictor.lib.reflection.VersionMatcher;
|
import org.adrianvictor.lib.versioning.DefaultVersionedServiceFactory;
|
||||||
|
import org.adrianvictor.lib.versioning.VersionMatcher;
|
||||||
|
import org.adrianvictor.lib.versioning.VersionedServiceFactory;
|
||||||
|
import org.adrianvictor.lib.versioning.VersionedServiceRegistrar;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
|
|
@ -12,22 +15,15 @@ public class Main extends JavaPlugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
// Initialize VersionMatcher
|
|
||||||
versionMatcher = new VersionMatcher();
|
versionMatcher = new VersionMatcher();
|
||||||
|
|
||||||
// Initialize DefaultVersionedServiceFactory
|
|
||||||
versionedServiceFactory = new DefaultVersionedServiceFactory(versionMatcher);
|
versionedServiceFactory = new DefaultVersionedServiceFactory(versionMatcher);
|
||||||
|
|
||||||
// Discover and register version-specific implementations using ServiceLoader
|
|
||||||
ServiceLoader<VersionedServiceRegistrar> registrars = ServiceLoader.load(VersionedServiceRegistrar.class);
|
ServiceLoader<VersionedServiceRegistrar> registrars = ServiceLoader.load(VersionedServiceRegistrar.class);
|
||||||
for (VersionedServiceRegistrar registrar : registrars) {
|
for (VersionedServiceRegistrar registrar : registrars) {
|
||||||
registrar.register(versionedServiceFactory);
|
registrar.register(versionedServiceFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Example usage (for demonstration, can be removed later)
|
|
||||||
// ConfigurationProvider configProvider = Configuration.create(versionedServiceFactory);
|
|
||||||
// TextColorProvider textColorProvider = TextColor.create(versionedServiceFactory);
|
|
||||||
|
|
||||||
getLogger().info("tenkumaLib has been enabled!");
|
getLogger().info("tenkumaLib has been enabled!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package org.adrianvictor.lib.configuration;
|
package org.adrianvictor.lib.configuration;
|
||||||
|
|
||||||
import org.adrianvictor.lib.VersionedServiceFactory;
|
import org.adrianvictor.lib.versioning.VersionedServiceFactory;
|
||||||
import org.adrianvictor.lib.configuration.provider.ConfigurationProvider;
|
import org.adrianvictor.lib.configuration.provider.ConfigurationProvider;
|
||||||
|
|
||||||
public class Configuration {
|
public class Configuration {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package org.adrianvictor.lib.text;
|
package org.adrianvictor.lib.text;
|
||||||
|
|
||||||
import org.adrianvictor.lib.VersionedServiceFactory;
|
import org.adrianvictor.lib.versioning.VersionedServiceFactory;
|
||||||
import org.adrianvictor.lib.text.provider.TextColorProvider;
|
import org.adrianvictor.lib.text.provider.TextColorProvider;
|
||||||
|
|
||||||
public class TextColor {
|
public class TextColor {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package org.adrianvictor.lib.text;
|
package org.adrianvictor.lib.text;
|
||||||
|
|
||||||
import org.adrianvictor.lib.VersionedServiceFactory;
|
import org.adrianvictor.lib.versioning.VersionedServiceFactory;
|
||||||
import org.adrianvictor.lib.text.provider.TextColorProvider;
|
import org.adrianvictor.lib.text.provider.TextColorProvider;
|
||||||
|
|
||||||
public class TextColorUtils {
|
public class TextColorUtils {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package org.adrianvictor.lib;
|
package org.adrianvictor.lib.versioning;
|
||||||
|
|
||||||
import org.adrianvictor.lib.reflection.VersionMatcher;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
@ -24,8 +23,7 @@ public class DefaultVersionedServiceFactory implements VersionedServiceFactory {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> T getService(Class<T> serviceType) {
|
public <T> T getService(Class<T> serviceType) {
|
||||||
String serverVersion = versionMatcher.getServerVersion();
|
String serverVersion = versionMatcher.getServerVersion();
|
||||||
// Determine the correct suffix using VersionMatcher (adapted)
|
String versionSuffix = versionMatcher.getClassSuffix(serverVersion);
|
||||||
String versionSuffix = versionMatcher.getClassSuffix(serverVersion); // Removed "release" parameter
|
|
||||||
|
|
||||||
Map<String, Supplier<?>> versionSuppliers = serviceRegistrations.get(serviceType);
|
Map<String, Supplier<?>> versionSuppliers = serviceRegistrations.get(serviceType);
|
||||||
if (versionSuppliers == null || !versionSuppliers.containsKey(versionSuffix)) {
|
if (versionSuppliers == null || !versionSuppliers.containsKey(versionSuffix)) {
|
||||||
|
|
@ -1,15 +1,13 @@
|
||||||
package org.adrianvictor.lib.reflection;
|
package org.adrianvictor.lib.versioning;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
public class VersionMatcher {
|
public class VersionMatcher {
|
||||||
|
|
||||||
|
|
@ -39,7 +37,6 @@ public class VersionMatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort by version (highest minor first for best match within range)
|
|
||||||
applicableEntries.sort(Comparator
|
applicableEntries.sort(Comparator
|
||||||
.comparing(Entry::maxMajor).reversed()
|
.comparing(Entry::maxMajor).reversed()
|
||||||
.thenComparing(Entry::maxMinor).reversed()
|
.thenComparing(Entry::maxMinor).reversed()
|
||||||
|
|
@ -73,13 +70,15 @@ public class VersionMatcher {
|
||||||
private List<Entry> populateEntries() {
|
private List<Entry> populateEntries() {
|
||||||
return List.of(
|
return List.of(
|
||||||
new Entry("^1\\.7\\.3$", "b1_7_3", 1, 7, 1, 7),
|
new Entry("^1\\.7\\.3$", "b1_7_3", 1, 7, 1, 7),
|
||||||
// Covers 1.8 up to 1.16 for compatibility (example for user request)
|
// r1_1 covers versions from 1.1.x up to 1.16.4
|
||||||
new Entry("^1\\.(8|9|10|11|12|13|14|15|16)\\..*$", "b1_8_compat", 1, 8, 1, 16),
|
new Entry("^1\\.(1[0-5]|[1-9])(\\.\\d+)*$", "r1_1", 1, 1, 1, 16),
|
||||||
new Entry("^1\\.21\\..*$", "r1_21", 1, 21, Integer.MAX_VALUE, Integer.MAX_VALUE) // Open-ended for future 1.21.x
|
// r1_16_5 covers versions from 1.16.5 up to 1.20.x
|
||||||
|
new Entry("^1\\.(16\\.[5-9]|1[7-9]|20)(\\.\\d+)*$", "r1_16_5", 1, 16, 1, 20),
|
||||||
|
// r1_21 covers 1.21.x and above
|
||||||
|
new Entry("^1\\.21(\\.\\d+)*$", "r1_21", 1, 21, Integer.MAX_VALUE, Integer.MAX_VALUE)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to parse major and minor version from a string (e.g., "1.16.5" -> [1, 16])
|
|
||||||
private int[] parseVersion(String versionString) {
|
private int[] parseVersion(String versionString) {
|
||||||
Matcher matcher = Pattern.compile("^(\\d+)\\.(\\d+).*").matcher(versionString);
|
Matcher matcher = Pattern.compile("^(\\d+)\\.(\\d+).*").matcher(versionString);
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package org.adrianvictor.lib;
|
package org.adrianvictor.lib.versioning;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package org.adrianvictor.lib;
|
package org.adrianvictor.lib.versioning;
|
||||||
|
|
||||||
public interface VersionedServiceRegistrar {
|
public interface VersionedServiceRegistrar {
|
||||||
void register(VersionedServiceFactory factory);
|
void register(VersionedServiceFactory factory);
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package org.adrianvictor.lib.impl.r1_21;
|
package org.adrianvictor.lib.impl.r1_21;
|
||||||
|
|
||||||
import org.adrianvictor.lib.VersionedServiceFactory;
|
import org.adrianvictor.lib.versioning.VersionedServiceFactory;
|
||||||
import org.adrianvictor.lib.VersionedServiceRegistrar;
|
import org.adrianvictor.lib.versioning.VersionedServiceRegistrar;
|
||||||
import org.adrianvictor.lib.configuration.provider.ConfigurationProvider;
|
import org.adrianvictor.lib.configuration.provider.ConfigurationProvider;
|
||||||
import org.adrianvictor.lib.impl.r1_21.configuration.Configuration;
|
import org.adrianvictor.lib.impl.r1_21.configuration.Configuration;
|
||||||
import org.adrianvictor.lib.text.provider.TextColorProvider;
|
import org.adrianvictor.lib.text.provider.TextColorProvider;
|
||||||
|
|
@ -10,7 +10,7 @@ import org.adrianvictor.lib.impl.r1_21.text.TextColor;
|
||||||
public class R1_21Registrar implements VersionedServiceRegistrar {
|
public class R1_21Registrar implements VersionedServiceRegistrar {
|
||||||
@Override
|
@Override
|
||||||
public void register(VersionedServiceFactory factory) {
|
public void register(VersionedServiceFactory factory) {
|
||||||
factory.register(ConfigurationProvider.class, "r1_21", Configuration::new);
|
factory.register(ConfigurationProvider.class, "r1_1", Configuration::new);
|
||||||
factory.register(TextColorProvider.class, "r1_21", TextColor::new);
|
factory.register(TextColorProvider.class, "r1_16_5", TextColor::new);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue