Skip to content

Commit

Permalink
Add Platform API
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrix-Shen <[email protected]>
  • Loading branch information
Hendrix-Shen committed Feb 23, 2024
1 parent 1aa1e7c commit 29f0179
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ default String getPlatformName() {

DistType getCurrentDistType();

boolean matchesDist(DistType side);
boolean matchesDist(DistType distType);

boolean isModLoaded(String modIdentifier);

boolean isModExist(String modIdentifier);

boolean isDevelopmentEnvironment();

default String getModName(String modIdentifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public ValueContainer<DependencyCheckResult> check() {
Objects.requireNonNull(this.value,
"Dependency type is set to MOD_ID mode and requires mod id as value");

if (!platform.isModLoaded(this.value) && !this.optional) {
if (!platform.isModExist(this.value) && !this.optional) {
return ValueContainer.of(new DependencyCheckResult(false,
I18n.tr("magiclib.dependency.result.mod_id.require", this.value,
this.versionPredicates.isEmpty() ? "[*]" : this.versionPredicates)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,20 @@ public DistType getCurrentDistType() {
}

@Override
public boolean matchesDist(DistType side) {
return this.getCurrentDistType().matches(side);
public boolean matchesDist(DistType distType) {
return this.getCurrentDistType().matches(distType);
}

@Override
public boolean isModLoaded(String modIdentifier) {
return FabricLoader.getInstance().isModLoaded(modIdentifier);
}

@Override
public boolean isModExist(String modIdentifier) {
return FabricLoader.getInstance().isModLoaded(modIdentifier);
}

@Override
public boolean isDevelopmentEnvironment() {
return FabricLoader.getInstance().isDevelopmentEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,24 @@ public DistType getCurrentDistType() {
}

@Override
public boolean matchesDist(DistType side) {
return this.getCurrentDistType().matches(side);
public boolean matchesDist(DistType distType) {
return this.getCurrentDistType().matches(distType);
}

@Override
public boolean isModLoaded(String modIdentifier) {
return ModList.get().isLoaded(modIdentifier);
}

@Override
public boolean isModExist(String modIdentifier) {
return ValueContainer.ofNullable(ForgeModList.getInstance().getMods())
.orElse(ForgeLoadingModList.getInstance().getMods())
.orElseThrow(() -> new IllegalStateException("Access ModList too early!"))
.stream()
.anyMatch(modInfo -> modInfo.getModId().equals(modIdentifier));
}

@Override
public boolean isDevelopmentEnvironment() {
return !FMLLoader.isProduction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class NeoForgePlatformImpl implements Platform {
@Getter(lazy = true)
private static final Platform instance = new NeoForgePlatformImpl();
public static final ImmutableBiMap<DistType, Dist> sideTypeMappings = ImmutableBiMap.of(
public static final ImmutableBiMap<DistType, Dist> distTypeMappings = ImmutableBiMap.of(
DistType.CLIENT, Dist.CLIENT,
DistType.SERVER, Dist.DEDICATED_SERVER
);
Expand Down Expand Up @@ -61,15 +61,24 @@ public DistType getCurrentDistType() {
}

@Override
public boolean matchesDist(DistType side) {
return this.getCurrentDistType().matches(side);
public boolean matchesDist(DistType distType) {
return this.getCurrentDistType().matches(distType);
}

@Override
public boolean isModLoaded(String modIdentifier) {
return ModList.get().isLoaded(modIdentifier);
}

@Override
public boolean isModExist(String modIdentifier) {
return ValueContainer.ofNullable(NeoForgeModList.getInstance().getMods())
.orElse(NeoForgeLoadingModList.getInstance().getMods())
.orElseThrow(() -> new IllegalStateException("Access ModList too early!"))
.stream()
.anyMatch(iModInfo -> iModInfo.getModId().equals(modIdentifier));
}

@Override
public boolean isDevelopmentEnvironment() {
return !FMLLoader.isProduction();
Expand Down Expand Up @@ -112,10 +121,10 @@ public Dist getCurrentEnvType() {
}

public DistType getDistType(Dist envType) {
return NeoForgePlatformImpl.sideTypeMappings.inverse().get(envType);
return NeoForgePlatformImpl.distTypeMappings.inverse().get(envType);
}

public Dist getDist(DistType sideType) {
return NeoForgePlatformImpl.sideTypeMappings.get(sideType);
return NeoForgePlatformImpl.distTypeMappings.get(sideType);
}
}

0 comments on commit 29f0179

Please sign in to comment.