Skip to content

Commit b871891

Browse files
Refactor and add modular service implementations.
Renamed interfaces to remove "I" prefix for consistency in naming. Added `GameHelper` and `ModLoaderHelper` implementations for both Fabric and NeoForge platforms. Updated service loading to include the newly added helpers.
1 parent 775bdf2 commit b871891

21 files changed

+368
-14
lines changed

common/src/main/java/me/pandamods/pandalib/platform/Services.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@
1313
package me.pandamods.pandalib.platform;
1414

1515
import com.mojang.logging.LogUtils;
16-
import me.pandamods.pandalib.platform.services.INetworkHelper;
17-
import me.pandamods.pandalib.platform.services.IRegistrationHelper;
16+
import me.pandamods.pandalib.platform.services.GameHelper;
17+
import me.pandamods.pandalib.platform.services.ModLoaderHelper;
18+
import me.pandamods.pandalib.platform.services.NetworkHelper;
19+
import me.pandamods.pandalib.platform.services.RegistrationHelper;
1820
import org.slf4j.Logger;
1921

2022
import java.util.ServiceLoader;
2123

2224
public class Services {
2325
private static final Logger LOGGER = LogUtils.getLogger();
2426

25-
public static final INetworkHelper NETWORK = load(INetworkHelper.class);
26-
public static final IRegistrationHelper REGISTRATION = load(IRegistrationHelper.class);
27+
public static final NetworkHelper NETWORK = load(NetworkHelper.class);
28+
public static final RegistrationHelper REGISTRATION = load(RegistrationHelper.class);
29+
public static final GameHelper GAME = load(GameHelper.class);
30+
public static final ModLoaderHelper MOD_LOADER = load(ModLoaderHelper.class);
2731

2832
private static <T> T load(Class<T> serviceClass) {
2933
final T loadedService = ServiceLoader.load(serviceClass).findFirst().orElseThrow(() ->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2024 Oliver Froberg (The Panda Oliver)
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* any later version.
8+
*
9+
* You should have received a copy of the GNU Lesser General Public License
10+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
11+
*/
12+
13+
package me.pandamods.pandalib.platform.services;
14+
15+
import java.nio.file.Path;
16+
17+
public interface GameHelper {
18+
boolean isDevelopmentEnvironment();
19+
boolean isProductionEnvironment();
20+
21+
boolean isClient();
22+
boolean isServer();
23+
24+
Path getGameDir();
25+
Path getConfigDir();
26+
Path getModDir();
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2024 Oliver Froberg (The Panda Oliver)
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* any later version.
8+
*
9+
* You should have received a copy of the GNU Lesser General Public License
10+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
11+
*/
12+
13+
package me.pandamods.pandalib.platform.services;
14+
15+
import java.util.List;
16+
17+
public interface ModLoaderHelper {
18+
boolean isModLoaded(String modId);
19+
Mod getMod(String modId);
20+
List<Mod> getMods();
21+
List<String> getModIds();
22+
23+
interface Mod {
24+
String getId();
25+
String getDisplayName();
26+
String getDescription();
27+
List<String> getAuthors();
28+
String getVersion();
29+
}
30+
}

common/src/main/java/me/pandamods/pandalib/platform/services/INetworkHelper.java renamed to common/src/main/java/me/pandamods/pandalib/platform/services/NetworkHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
1717
import net.minecraft.server.level.ServerPlayer;
1818

19-
public interface INetworkHelper extends NetworkRegistry {
19+
public interface NetworkHelper extends NetworkRegistry {
2020
<T extends CustomPacketPayload> void sendToServer(T payload);
2121

2222
<T extends CustomPacketPayload> void sendToPlayer(ServerPlayer player, T payload);

common/src/main/java/me/pandamods/pandalib/platform/services/IRegistrationHelper.java renamed to common/src/main/java/me/pandamods/pandalib/platform/services/RegistrationHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616

1717
import java.util.function.Supplier;
1818

19-
public interface IRegistrationHelper {
19+
public interface RegistrationHelper {
2020
<T> void register(DeferredObject<? extends T> deferredObject, Supplier<? extends T> supplier);
2121
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2024 Oliver Froberg (The Panda Oliver)
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* any later version.
8+
*
9+
* You should have received a copy of the GNU Lesser General Public License
10+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
11+
*/
12+
13+
package me.pandamods.pandalib.fabric.platform;
14+
15+
import me.pandamods.pandalib.platform.services.GameHelper;
16+
import net.fabricmc.api.EnvType;
17+
import net.fabricmc.loader.api.FabricLoader;
18+
19+
import java.nio.file.Path;
20+
21+
public class GameHelperImpl implements GameHelper {
22+
@Override
23+
public boolean isDevelopmentEnvironment() {
24+
return FabricLoader.getInstance().isDevelopmentEnvironment();
25+
}
26+
27+
@Override
28+
public boolean isProductionEnvironment() {
29+
return !FabricLoader.getInstance().isDevelopmentEnvironment();
30+
}
31+
32+
@Override
33+
public boolean isClient() {
34+
return FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT;
35+
}
36+
37+
@Override
38+
public boolean isServer() {
39+
return FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER;
40+
}
41+
42+
@Override
43+
public Path getGameDir() {
44+
return FabricLoader.getInstance().getGameDir().toAbsolutePath().normalize();
45+
}
46+
47+
@Override
48+
public Path getConfigDir() {
49+
return FabricLoader.getInstance().getConfigDir().toAbsolutePath().normalize();
50+
}
51+
52+
@Override
53+
public Path getModDir() {
54+
return getGameDir().resolve("mods");
55+
}
56+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (C) 2024 Oliver Froberg (The Panda Oliver)
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* any later version.
8+
*
9+
* You should have received a copy of the GNU Lesser General Public License
10+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
11+
*/
12+
13+
package me.pandamods.pandalib.fabric.platform;
14+
15+
import dev.architectury.event.events.client.ClientLifecycleEvent;
16+
import me.pandamods.pandalib.platform.services.ModLoaderHelper;
17+
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
18+
import net.fabricmc.loader.api.FabricLoader;
19+
import net.fabricmc.loader.api.ModContainer;
20+
import net.fabricmc.loader.api.metadata.ModMetadata;
21+
import net.fabricmc.loader.api.metadata.Person;
22+
import net.fabricmc.loader.impl.FabricLoaderImpl;
23+
24+
import java.util.Collections;
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
public class ModLoaderHelperImpl implements ModLoaderHelper {
30+
private final Map<String, Mod> mods = new HashMap<>();
31+
32+
@Override
33+
public boolean isModLoaded(String modId) {
34+
return FabricLoader.getInstance().isModLoaded(modId);
35+
}
36+
37+
@Override
38+
public Mod getMod(String modId) {
39+
return mods.computeIfAbsent(modId, ModImpl::new);
40+
}
41+
42+
@Override
43+
public List<Mod> getMods() {
44+
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
45+
getMod(mod.getMetadata().getId());
46+
}
47+
48+
return List.copyOf(mods.values());
49+
}
50+
51+
@Override
52+
public List<String> getModIds() {
53+
return FabricLoader.getInstance().getAllMods().stream().map(ModContainer::getMetadata).map(ModMetadata::getId).toList();
54+
}
55+
56+
private static class ModImpl implements Mod {
57+
private final ModContainer container;
58+
private final ModMetadata metadata;
59+
60+
public ModImpl(String modId) {
61+
this.container = FabricLoader.getInstance().getModContainer(modId).orElseThrow();
62+
this.metadata = this.container.getMetadata();
63+
}
64+
65+
@Override
66+
public String getId() {
67+
return metadata.getId();
68+
}
69+
70+
@Override
71+
public String getDisplayName() {
72+
return metadata.getName();
73+
}
74+
75+
@Override
76+
public String getDescription() {
77+
return metadata.getDescription();
78+
}
79+
80+
@Override
81+
public List<String> getAuthors() {
82+
return metadata.getAuthors().stream().map(Person::getName).toList();
83+
}
84+
85+
@Override
86+
public String getVersion() {
87+
return metadata.getVersion().getFriendlyString();
88+
}
89+
}
90+
}

fabric/src/main/java/me/pandamods/pandalib/fabric/platform/NetworkHelperImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import me.pandamods.pandalib.fabric.PandaLibFabric;
1818
import me.pandamods.pandalib.networking.NetworkContext;
1919
import me.pandamods.pandalib.networking.NetworkReceiver;
20-
import me.pandamods.pandalib.platform.services.INetworkHelper;
20+
import me.pandamods.pandalib.platform.services.NetworkHelper;
2121
import net.fabricmc.api.EnvType;
2222
import net.fabricmc.api.Environment;
2323
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
@@ -28,7 +28,7 @@
2828
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
2929
import net.minecraft.server.level.ServerPlayer;
3030

31-
public class NetworkHelperImpl implements INetworkHelper {
31+
public class NetworkHelperImpl implements NetworkHelper {
3232
@Override
3333
@Environment(EnvType.CLIENT)
3434
public <T extends CustomPacketPayload> void registerClientReceiver(CustomPacketPayload.Type<T> type,

fabric/src/main/java/me/pandamods/pandalib/fabric/platform/RegistrationHelperImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
package me.pandamods.pandalib.fabric.platform;
1414

15-
import me.pandamods.pandalib.platform.services.IRegistrationHelper;
15+
import me.pandamods.pandalib.platform.services.RegistrationHelper;
1616
import me.pandamods.pandalib.registry.DeferredObject;
1717
import net.minecraft.core.Registry;
1818

1919
import java.util.function.Supplier;
2020

21-
public class RegistrationHelperImpl implements IRegistrationHelper {
21+
public class RegistrationHelperImpl implements RegistrationHelper {
2222
@Override
2323
@SuppressWarnings("unchecked")
2424
public <T> void register(DeferredObject<? extends T> deferredObject, Supplier<? extends T> supplier) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
me.pandamods.pandalib.fabric.platform.GameHelperImpl

0 commit comments

Comments
 (0)