Skip to content

Commit

Permalink
Merge pull request #11 from LostLuma/1.7
Browse files Browse the repository at this point in the history
Upstream Sync for Minecraft 1.7
  • Loading branch information
SpaceWalkerRS authored Jun 16, 2024
2 parents f83ccae + 4e89c05 commit 226ec8e
Show file tree
Hide file tree
Showing 31 changed files with 1,588 additions and 555 deletions.
45 changes: 32 additions & 13 deletions src/main/java/com/terraformersmc/modmenu/ModMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
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.ModMenuConfig.GameMenuButtonStyle;
import com.terraformersmc.modmenu.config.ModMenuConfig.TitleMenuButtonStyle;
import com.terraformersmc.modmenu.config.ModMenuConfigManager;
import com.terraformersmc.modmenu.event.ModMenuEventHandler;
import com.terraformersmc.modmenu.util.ModrinthUtil;
import com.terraformersmc.modmenu.util.TranslationUtil;
import com.terraformersmc.modmenu.util.UpdateCheckerUtil;
import com.terraformersmc.modmenu.util.mod.Mod;
import com.terraformersmc.modmenu.util.mod.fabric.FabricDummyParentMod;
import com.terraformersmc.modmenu.util.mod.fabric.FabricMod;
Expand All @@ -33,7 +34,7 @@

public class ModMenu implements ClientModInitializer {
public static final String MOD_ID = "modmenu";
public static final String GITHUB_REF = "TerraformersMC/ModMenu";
public static final String GITHUB_REF = "OrnitheMC/ModMenu";
public static final Logger LOGGER = LogManager.getLogger("Mod Menu");
public static final Gson GSON = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setPrettyPrinting().create();
public static final Gson GSON_MINIFIED = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
Expand All @@ -42,17 +43,19 @@ public class ModMenu implements ClientModInitializer {
public static final Map<String, Mod> ROOT_MODS = new HashMap<>();
public static final LinkedListMultimap<Mod, Mod> PARENT_MAP = LinkedListMultimap.create();

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

private static int cachedDisplayedModCount = -1;
public static boolean runningQuilt = FabricLoader.getInstance().isModLoaded("quilt_loader");
public static boolean devEnvironment = FabricLoader.getInstance().isDevelopmentEnvironment();

public static Screen getConfigScreen(String modid, Screen menuScreen) {
if(!delayedScreenFactoryProviders.isEmpty()) {
delayedScreenFactoryProviders.forEach(map -> map.forEach(configScreenFactories::putIfAbsent));
delayedScreenFactoryProviders.clear();
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;
Expand All @@ -68,13 +71,18 @@ public static Screen getConfigScreen(String modid, Screen menuScreen) {
public void initClient() {
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());
delayedScreenFactoryProviders.add(api.getProvidedConfigScreenFactories());
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);
Expand All @@ -91,10 +99,17 @@ public void initClient() {
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);
}

ModrinthUtil.checkForUpdates();
checkForUpdates();

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

Expand Down Expand Up @@ -122,6 +137,10 @@ public static void clearModCountCache() {
cachedDisplayedModCount = -1;
}

public static void checkForUpdates() {
UpdateCheckerUtil.checkForUpdates();
}

public static boolean areModUpdatesAvailable() {
if (!ModMenuConfig.UPDATE_CHECKER.getValue()) {
return false;
Expand All @@ -136,7 +155,7 @@ public static boolean areModUpdatesAvailable() {
continue;
}

if (mod.getModrinthData() != null || mod.getChildHasUpdate()) {
if (mod.hasUpdate() || mod.getChildHasUpdate()) {
return true; // At least one currently visible mod has an update
}
}
Expand All @@ -148,9 +167,9 @@ public static String getDisplayedModCount() {
if (cachedDisplayedModCount == -1) {
// listen, if you have >= 2^32 mods then that's on you
cachedDisplayedModCount = Math.toIntExact(MODS.values().stream().filter(mod ->
(ModMenuConfig.COUNT_CHILDREN.getValue() || mod.getParent() == null) &&
(ModMenuConfig.COUNT_LIBRARIES.getValue() || !mod.getBadges().contains(Mod.Badge.LIBRARY)) &&
(ModMenuConfig.COUNT_HIDDEN_MODS.getValue() || !mod.isHidden())
(ModMenuConfig.COUNT_CHILDREN.getValue() || mod.getParent() == null) &&
(ModMenuConfig.COUNT_LIBRARIES.getValue() || !mod.getBadges().contains(Mod.Badge.LIBRARY)) &&
(ModMenuConfig.COUNT_HIDDEN_MODS.getValue() || !mod.isHidden())
).count());
}
return NumberFormat.getInstance().format(cachedDisplayedModCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import com.google.common.collect.ImmutableMap;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import com.terraformersmc.modmenu.api.UpdateChecker;
import com.terraformersmc.modmenu.gui.ModMenuOptionsScreen;
import com.terraformersmc.modmenu.util.mod.fabric.FabricLoaderUpdateChecker;
import com.terraformersmc.modmenu.util.mod.quilt.QuiltLoaderUpdateChecker;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.options.OptionsScreen;

import java.util.Map;

public class ModMenuModMenuCompat implements ModMenuApi {

@Override
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return ModMenuOptionsScreen::new;
Expand All @@ -20,4 +23,13 @@ public ConfigScreenFactory<?> getModConfigScreenFactory() {
public Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {
return ImmutableMap.of("minecraft", parent -> new OptionsScreen(parent, Minecraft.getInstance().options));
}

@Override
public Map<String, UpdateChecker> getProvidedUpdateCheckers() {
if (ModMenu.runningQuilt) {
return ImmutableMap.of("quilt_loader", new QuiltLoaderUpdateChecker());
} else {
return ImmutableMap.of("fabricloader", new FabricLoaderUpdateChecker());
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/terraformersmc/modmenu/api/ModMenuApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,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 @@ -59,6 +69,16 @@ default Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {
return ImmutableMap.of();
}

/**
* 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 ImmutableMap.of();
}

/**
* Used to mark mods with a badge indicating that they are
* provided by a modpack.
Expand Down
19 changes: 19 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,19 @@
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();
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/terraformersmc/modmenu/api/UpdateChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.terraformersmc.modmenu.api;

public interface UpdateChecker {
/**
* Gets called when ModMenu is checking for updates.
* This is done in a separate thread, so this call can/should be blocking.
*
* <p>Your update checker should aim to return an update on the same or a more stable channel than the user's preference which you can get via {@link UpdateChannel#getUserPreference()}.</p>
*
* @return The update info
*/
UpdateInfo checkForUpdates();
}
29 changes: 29 additions & 0 deletions src/main/java/com/terraformersmc/modmenu/api/UpdateInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.terraformersmc.modmenu.api;

import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;

public interface UpdateInfo {
/**
* @return If an update for the mod is available.
*/
boolean isUpdateAvailable();

/**
* @return The message that is getting displayed when an update is available or <code>null</code> to let ModMenu handle displaying the message.
*/
@Nullable
default Text getUpdateMessage() {
return null;
}

/**
* @return The URL to the mod download.
*/
String getDownloadLink();

/**
* @return The update channel this update is available for.
*/
UpdateChannel getUpdateChannel();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.terraformersmc.modmenu.config;

import com.google.gson.annotations.SerializedName;
import com.terraformersmc.modmenu.api.UpdateChannel;
import com.terraformersmc.modmenu.config.option.BooleanConfigOption;
import com.terraformersmc.modmenu.config.option.ConfigOption;
import com.terraformersmc.modmenu.config.option.EnumConfigOption;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class ModMenuConfig {
public static final StringSetConfigOption DISABLE_UPDATE_CHECKER = new StringSetConfigOption("disable_update_checker", new HashSet<>());
public static final BooleanConfigOption UPDATE_CHECKER = new BooleanConfigOption("update_checker", true);
public static final BooleanConfigOption BUTTON_UPDATE_BADGE = new BooleanConfigOption("button_update_badge", true);
public static final EnumConfigOption<UpdateChannel> UPDATE_CHANNEL = new EnumConfigOption<>("update_channel", UpdateChannel.RELEASE);
public static final BooleanConfigOption QUICK_CONFIGURE = new BooleanConfigOption("quick_configure", true);

public static ConfigOption[] asOptions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.terraformersmc.modmenu.gui;

import com.terraformersmc.modmenu.ModMenu;
import com.terraformersmc.modmenu.config.ModMenuConfig;
import com.terraformersmc.modmenu.config.ModMenuConfigManager;
import com.terraformersmc.modmenu.gui.widget.ConfigOptionListWidget;
Expand Down Expand Up @@ -56,6 +57,7 @@ public void mouseClicked(int mouseX, int mouseY, int button) {
public void buttonClicked(ButtonWidget button) {
switch (button.id) {
case DONE:
ModMenu.checkForUpdates();
ModMenuConfigManager.save();
ModMenuOptionsScreen.this.minecraft.openScreen(ModMenuOptionsScreen.this.previous);
break;
Expand All @@ -64,6 +66,7 @@ public void buttonClicked(ButtonWidget button) {

@Override
public void removed() {
ModMenu.checkForUpdates();
ModMenuConfigManager.save();
}
}
Loading

0 comments on commit 226ec8e

Please sign in to comment.