Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Big update #9

Open
wants to merge 1 commit into
base: 1.8.9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ publishing {
}
} else {
maven {
maven {
name 'Repsy'
credentials(PasswordCredentials)
url 'https://repsy.io/mvn/enderzombi102/mc'
}
name 'Repsy'
credentials(PasswordCredentials)
url 'https://repsy.io/mvn/enderzombi102/mc'
}
}
}
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
14 changes: 7 additions & 7 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -202,11 +202,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
207 changes: 207 additions & 0 deletions src/main/java/com/terraformersmc/modmenu/ModMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package com.terraformersmc.modmenu;

import com.google.common.collect.LinkedListMultimap;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import com.terraformersmc.modmenu.api.UpdateChecker;
import com.terraformersmc.modmenu.config.ModMenuConfig;
import com.terraformersmc.modmenu.config.ModMenuConfigManager;
import com.terraformersmc.modmenu.event.ModMenuEventHandler;
import com.terraformersmc.modmenu.util.EnumToLowerCaseJsonConverter;
import com.terraformersmc.modmenu.util.ModMenuScreenTexts;
import com.terraformersmc.modmenu.util.mod.Mod;
import com.terraformersmc.modmenu.util.mod.fabric.FabricDummyParentMod;
import com.terraformersmc.modmenu.util.mod.fabric.FabricMod;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.text.NumberFormat;
import java.util.*;

import static com.terraformersmc.modmenu.util.TranslationUtil.hasTranslation;

public class ModMenu implements ClientModInitializer {
public static final String MOD_ID = "modmenu";
public static final Logger LOGGER = LogManager.getLogger("Mod Menu");
public static final Gson GSON;
public static final Gson GSON_MINIFIED;

static {
GsonBuilder builder = new GsonBuilder().registerTypeHierarchyAdapter(Enum.class, new EnumToLowerCaseJsonConverter())
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
GSON = builder.setPrettyPrinting().create();
GSON_MINIFIED = builder.create();
}

public static final Map<String, Mod> MODS = new HashMap<>();
public static final Map<String, Mod> ROOT_MODS = new HashMap<>();
public static final LinkedListMultimap<Mod, Mod> PARENT_MAP = LinkedListMultimap.create();

private static final Map<String, ConfigScreenFactory<?>> configScreenFactories = new HashMap<>();
private static final List<ModMenuApi> apiImplementations = new ArrayList<>();

private static int cachedDisplayedModCount = -1;

public static Screen getConfigScreen(String modid, Screen menuScreen) {
for (ModMenuApi api : apiImplementations) {
Map<String, ConfigScreenFactory<?>> factoryProviders = api.getProvidedConfigScreenFactories();
if (!factoryProviders.isEmpty()) {
factoryProviders.forEach(configScreenFactories::putIfAbsent);
}
}
if (ModMenuConfig.HIDDEN_CONFIGS.getValue().contains(modid)) {
return null;
}
ConfigScreenFactory<?> factory = configScreenFactories.get(modid);
if (factory != null) {
return factory.create(menuScreen);
}
return null;
}

@Override
public void onInitializeClient() {
ModMenuConfigManager.initializeConfig();
Set<String> modpackMods = new HashSet<>();
Map<String, UpdateChecker> updateCheckers = new HashMap<>();
Map<String, UpdateChecker> providedUpdateCheckers = new HashMap<>();

FabricLoader.getInstance().getEntrypointContainers("modmenu", ModMenuApi.class).forEach(entrypoint -> {
ModMetadata metadata = entrypoint.getProvider().getMetadata();
String modId = metadata.getId();
try {
ModMenuApi api = entrypoint.getEntrypoint();
configScreenFactories.put(modId, api.getModConfigScreenFactory());
apiImplementations.add(api);
updateCheckers.put(modId, api.getUpdateChecker());
providedUpdateCheckers.putAll(api.getProvidedUpdateCheckers());
api.attachModpackBadges(modpackMods::add);
} catch (Throwable e) {
LOGGER.error("Mod {} provides a broken implementation of ModMenuApi", modId, e);
}
});

// Fill mods map
for (ModContainer modContainer : FabricLoader.getInstance().getAllMods()) {
Mod mod = new FabricMod(modContainer, modpackMods);

UpdateChecker updateChecker = updateCheckers.get(mod.getId());

if (updateChecker == null) {
updateChecker = providedUpdateCheckers.get(mod.getId());
}

MODS.put(mod.getId(), mod);
mod.setUpdateChecker(updateChecker);
}

Map<String, Mod> dummyParents = new HashMap<>();

// Initialize parent map
for (Mod mod : MODS.values()) {
String parentId = mod.getParent();
if (parentId != null) {
Mod parent = MODS.getOrDefault(parentId, dummyParents.get(parentId));
if (parent == null) {
if (mod instanceof FabricMod) {
parent = new FabricDummyParentMod((FabricMod) mod, parentId);
dummyParents.put(parentId, parent);
}
}
if (parent != null) {
PARENT_MAP.put(parent, mod);
}
} else {
ROOT_MODS.put(mod.getId(), mod);
}
}
MODS.putAll(dummyParents);
ModMenuEventHandler.register();
}

public static void clearModCountCache() {
cachedDisplayedModCount = -1;
}

public static boolean areModUpdatesAvailable() {
if (!ModMenuConfig.UPDATE_CHECKER.getValue()) {
return false;
}

for (Mod mod : MODS.values()) {
if (mod.isHidden()) {
continue;
}

if (!ModMenuConfig.SHOW_LIBRARIES.getValue() && mod.getBadges().contains(Mod.Badge.LIBRARY)) {
continue;
}

if (mod.hasUpdate() || mod.getChildHasUpdate()) {
return true; // At least one currently visible mod has an update
}
}

return false;
}

public static String getDisplayedModCount() {
if (cachedDisplayedModCount == -1) {
boolean includeChildren = ModMenuConfig.COUNT_CHILDREN.getValue();
boolean includeLibraries = ModMenuConfig.COUNT_LIBRARIES.getValue();
boolean includeHidden = ModMenuConfig.COUNT_HIDDEN_MODS.getValue();

// listen, if you have >= 2^32 mods then that's on you
cachedDisplayedModCount = Math.toIntExact(MODS.values().stream().filter(mod -> {
boolean isChild = mod.getParent() != null;
if (!includeChildren && isChild) {
return false;
}
boolean isLibrary = mod.getBadges().contains(Mod.Badge.LIBRARY);
if (!includeLibraries && isLibrary) {
return false;
}
return includeHidden || !mod.isHidden();
}).count());
}
return NumberFormat.getInstance().format(cachedDisplayedModCount);
}

public static Text createModsButtonText(boolean title) {
ModMenuConfig.TitleMenuButtonStyle titleStyle = ModMenuConfig.MODS_BUTTON_STYLE.getValue();
ModMenuConfig.GameMenuButtonStyle gameMenuStyle = ModMenuConfig.GAME_MENU_BUTTON_STYLE.getValue();
boolean isIcon = title ?
titleStyle == ModMenuConfig.TitleMenuButtonStyle.ICON :
gameMenuStyle == ModMenuConfig.GameMenuButtonStyle.ICON;
boolean isShort = title ?
titleStyle == ModMenuConfig.TitleMenuButtonStyle.SHRINK :
gameMenuStyle == ModMenuConfig.GameMenuButtonStyle.REPLACE;
Text modsText = ModMenuScreenTexts.TITLE.copy();
if (ModMenuConfig.MOD_COUNT_LOCATION.getValue().isOnModsButton() && !isIcon) {
String count = ModMenu.getDisplayedModCount();
if (isShort) {
modsText.append(new LiteralText(" ")).append(new TranslatableText("modmenu.loaded.short", count));
} else {
String specificKey = "modmenu.loaded." + count;
String key = hasTranslation(specificKey) ? specificKey : "modmenu.loaded";
if (ModMenuConfig.EASTER_EGGS.getValue() && hasTranslation(specificKey + ".secret")) {
key = specificKey + ".secret";
}
modsText.append(new LiteralText(" ")).append(new TranslatableText(key, count));
}
}
return modsText;
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.github.prospector.modmenu;
package com.terraformersmc.modmenu;

import com.google.common.collect.ImmutableMap;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import io.github.prospector.modmenu.gui.ModMenuOptionsScreen;
import com.terraformersmc.modmenu.gui.ModMenuOptionsScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.SettingsScreen;

import java.util.HashMap;
import java.util.Map;

public class ModMenuModMenuCompat implements ModMenuApi {
Expand All @@ -17,8 +17,6 @@ public ConfigScreenFactory<?> getModConfigScreenFactory() {

@Override
public Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {
return ImmutableMap.of(
"minecraft", parent -> new SettingsScreen( parent, MinecraftClient.getInstance().options )
);
return new HashMap<String, ConfigScreenFactory<?>>() {{ put("minecraft", parent -> new SettingsScreen(parent, MinecraftClient.getInstance().options)); }};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import net.minecraft.client.gui.screen.Screen;

@SuppressWarnings("deprecation")
@FunctionalInterface
public interface ConfigScreenFactory<S extends Screen> extends io.github.prospector.modmenu.api.ConfigScreenFactory<S> {
public interface ConfigScreenFactory<S extends Screen> {
S create(Screen parent);
}
43 changes: 37 additions & 6 deletions src/main/java/com/terraformersmc/modmenu/api/ModMenuApi.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.terraformersmc.modmenu.api;

import com.google.common.collect.ImmutableMap;
import io.github.prospector.modmenu.ModMenu;
import io.github.prospector.modmenu.gui.ModsScreen;
import com.terraformersmc.modmenu.ModMenu;
import com.terraformersmc.modmenu.gui.ModsScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;

import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;

public interface ModMenuApi extends io.github.prospector.modmenu.util.ModMenuApiMarker {
public interface ModMenuApi {

/**
* Used for creating a {@link Screen} instance of the Mod Menu
Expand All @@ -28,7 +29,7 @@ static Screen createModsScreen(Screen previous) {
* @return The text that would be displayed on a Mods button
*/
static Text createModsButtonText() {
return ModMenu.createModsButtonText();
return ModMenu.createModsButtonText(true);
}

/**
Expand All @@ -42,6 +43,16 @@ default ConfigScreenFactory<?> getModConfigScreenFactory() {
return screen -> null;
}

/**
* Used for mods that have their own update checking logic.
* By returning your own {@link UpdateChecker} instance, you can override ModMenus built-in update checking logic.
*
* @return An {@link UpdateChecker} or <code>null</code> if ModMenu should handle update checking.
*/
default UpdateChecker getUpdateChecker() {
return null;
}

/**
* Used to provide config screen factories for other mods. This takes second
* priority to a mod's own config screen factory provider. For example, if
Expand All @@ -55,6 +66,26 @@ default ConfigScreenFactory<?> getModConfigScreenFactory() {
* @return a map of mod ids to screen factories.
*/
default Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {
return ImmutableMap.of();
return Collections.emptyMap();
}

/**
* Used to provide update checkers for other mods. A mod registering its own
* update checker will take priority over any provided ones should both exist.
*
* @return a map of mod ids to update checkers.
*/
default Map<String, UpdateChecker> getProvidedUpdateCheckers() {
return Collections.emptyMap();
}

/**
* Used to mark mods with a badge indicating that they are
* provided by a modpack.
* <p>
* Builtin mods such as `minecraft` cannot be marked as
* provided by a modpack.
*/
default void attachModpackBadges(Consumer<String> consumer) {
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/terraformersmc/modmenu/api/UpdateChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.terraformersmc.modmenu.api;

import com.terraformersmc.modmenu.config.ModMenuConfig;

/**
* Supported update channels, in ascending order by stability.
*/
public enum UpdateChannel {
ALPHA, BETA, RELEASE;

/**
* @return the user's preferred update channel.
*/
public static UpdateChannel getUserPreference() {
return ModMenuConfig.UPDATE_CHANNEL.getValue();
}
}
Loading