Skip to content

Commit 2bfb9d9

Browse files
committed
Add service loader
1 parent b26e9ee commit 2bfb9d9

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2025 TerminalMC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dev.terminalmc.nocapes.platform;
18+
19+
import dev.terminalmc.nocapes.NoCapes;
20+
import dev.terminalmc.nocapes.platform.services.IPlatformInfo;
21+
22+
import java.util.ServiceLoader;
23+
24+
public class Services {
25+
public static final IPlatformInfo PLATFORM = load(IPlatformInfo.class);
26+
27+
public static <T> T load(Class<T> clazz) {
28+
final T loadedService = ServiceLoader.load(clazz)
29+
.findFirst()
30+
.orElseThrow(() -> new NullPointerException("Failed to load service for " + clazz.getName()));
31+
NoCapes.LOG.debug("Loaded {} for service {}", loadedService, clazz);
32+
return loadedService;
33+
}
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2025 TerminalMC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dev.terminalmc.nocapes.platform.services;
18+
19+
import java.nio.file.Path;
20+
21+
public interface IPlatformInfo {
22+
/**
23+
* @return the configuration directory of the instance.
24+
*/
25+
Path getConfigDir();
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2025 TerminalMC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dev.terminalmc.nocapes.platform;
18+
19+
import dev.terminalmc.nocapes.platform.services.IPlatformInfo;
20+
import net.fabricmc.loader.api.FabricLoader;
21+
22+
import java.nio.file.Path;
23+
24+
public class FabricPlatformInfo implements IPlatformInfo {
25+
@Override
26+
public Path getConfigDir() {
27+
return FabricLoader.getInstance().getConfigDir();
28+
}
29+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dev.terminalmc.nocapes.platform.FabricPlatformInfo
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2025 TerminalMC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dev.terminalmc.nocapes.platform;
18+
19+
import dev.terminalmc.nocapes.platform.services.IPlatformInfo;
20+
import net.neoforged.fml.loading.FMLPaths;
21+
22+
import java.nio.file.Path;
23+
24+
public class NeoForgePlatformInfo implements IPlatformInfo {
25+
@Override
26+
public Path getConfigDir() {
27+
return FMLPaths.CONFIGDIR.get();
28+
}
29+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dev.terminalmc.nocapes.platform.NeoForgePlatformInfo

0 commit comments

Comments
 (0)