mirror of
https://github.com/trafficlunar/options-profiles.git
synced 2026-06-28 14:44:10 +00:00
port to 26.1 part 1
This commit is contained in:
parent
b64f9b6741
commit
3a17ce9f60
33 changed files with 581 additions and 344 deletions
|
|
@ -1,18 +1,21 @@
|
|||
package net.trafficlunar.optionsprofiles;
|
||||
|
||||
import dev.architectury.event.events.common.CommandRegistrationEvent;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.trafficlunar.optionsprofiles.gui.ProfilesScreen;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class Commands {
|
||||
public static void init() {
|
||||
CommandRegistrationEvent.EVENT.register(((dispatcher, buildContext, selection) -> dispatcher.register(
|
||||
net.minecraft.commands.Commands
|
||||
.literal("optionsprofiles")
|
||||
.executes(context -> {
|
||||
Minecraft.getInstance().execute(() -> Minecraft.getInstance().setScreen(new ProfilesScreen(null)));
|
||||
return 1;
|
||||
})
|
||||
)));
|
||||
private static final LiteralArgumentBuilder<CommandSourceStack> PROFILE_COMMAND = net.minecraft.commands.Commands
|
||||
.literal("optionsprofiles")
|
||||
.executes(context -> {
|
||||
Minecraft.getInstance().execute(() -> Minecraft.getInstance().setScreen(new ProfilesScreen(null)));
|
||||
return 1;
|
||||
});
|
||||
|
||||
public static void registerCommands(Consumer<LiteralArgumentBuilder<CommandSourceStack>> consumer) {
|
||||
consumer.accept(PROFILE_COMMAND);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,22 @@
|
|||
package net.trafficlunar.optionsprofiles;
|
||||
|
||||
import com.mojang.blaze3d.platform.InputConstants;
|
||||
import dev.architectury.event.events.client.ClientTickEvent;
|
||||
import dev.architectury.registry.client.keymappings.KeyMappingRegistry;
|
||||
import net.minecraft.client.KeyMapping;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.trafficlunar.optionsprofiles.profiles.ProfileConfiguration;
|
||||
import net.trafficlunar.optionsprofiles.profiles.Profiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Keybinds {
|
||||
private static final KeyMapping[] PROFILE_KEYMAPPINGS = new KeyMapping[3];
|
||||
|
||||
public static void init() {
|
||||
KeyMapping.Category category = KeyMapping.Category.register(ResourceLocation.fromNamespaceAndPath(OptionsProfilesMod.MOD_ID, "keys"));
|
||||
public static void registerKeybinds(Consumer<KeyMapping> consumer) {
|
||||
KeyMapping.Category category = KeyMapping.Category.register(Identifier.fromNamespaceAndPath(OptionsProfilesMod.MOD_ID, "keys"));
|
||||
|
||||
for (int i = 0; i < PROFILE_KEYMAPPINGS.length; i++) {
|
||||
PROFILE_KEYMAPPINGS[i] = new KeyMapping(
|
||||
|
|
@ -27,16 +25,16 @@ public class Keybinds {
|
|||
-1,
|
||||
category
|
||||
);
|
||||
KeyMappingRegistry.register(PROFILE_KEYMAPPINGS[i]);
|
||||
consumer.accept(PROFILE_KEYMAPPINGS[i]);
|
||||
}
|
||||
}
|
||||
|
||||
ClientTickEvent.CLIENT_POST.register(minecraft -> {
|
||||
for (int i = 0; i < PROFILE_KEYMAPPINGS.length; i++) {
|
||||
while (PROFILE_KEYMAPPINGS[i].consumeClick()) {
|
||||
loadProfilesByKeybind(i + 1);
|
||||
}
|
||||
public static void tick() {
|
||||
for (int i = 0; i < PROFILE_KEYMAPPINGS.length; i++) {
|
||||
while (PROFILE_KEYMAPPINGS[i].consumeClick()) {
|
||||
loadProfilesByKeybind(i + 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadProfilesByKeybind(int keybindIndex) {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
package net.trafficlunar.optionsprofiles;
|
||||
|
||||
import dev.architectury.event.events.client.ClientLifecycleEvent;
|
||||
import dev.architectury.event.events.client.ClientPlayerEvent;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.trafficlunar.optionsprofiles.profiles.ProfileConfiguration;
|
||||
import net.trafficlunar.optionsprofiles.profiles.Profiles;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
|
@ -37,38 +36,6 @@ public class OptionsProfilesMod {
|
|||
|
||||
// Load mod config
|
||||
CONFIG = OptionsProfilesModConfiguration.load();
|
||||
|
||||
// Load profiles marked to load on startup
|
||||
ClientLifecycleEvent.CLIENT_STARTED.register(client -> {
|
||||
try (Stream<Path> paths = Files.list(Profiles.PROFILES_DIRECTORY)) {
|
||||
paths.filter(Files::isDirectory)
|
||||
.forEach(path -> {
|
||||
String profileName = path.getFileName().toString();
|
||||
|
||||
// This gets the configuration but also creates the configuration file if it is not there
|
||||
ProfileConfiguration profileConfiguration = ProfileConfiguration.get(profileName);
|
||||
if (profileConfiguration.shouldLoadOnStartup()) {
|
||||
Profiles.loadProfile(profileName);
|
||||
OptionsProfilesMod.LOGGER.info("[Profile '{}']: Loaded on startup", profileName);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
OptionsProfilesMod.LOGGER.error("An error occurred when initializing", e);
|
||||
}
|
||||
});
|
||||
|
||||
// Load profiles marked to load on server join
|
||||
ClientPlayerEvent.CLIENT_PLAYER_JOIN.register((LocalPlayer player) -> {
|
||||
handleClientPlayerEvent(player, false);
|
||||
});
|
||||
|
||||
// Load profiles marked to load on server leave
|
||||
ClientPlayerEvent.CLIENT_PLAYER_QUIT.register((LocalPlayer player) -> {
|
||||
handleClientPlayerEvent(player, true);
|
||||
});
|
||||
|
||||
Keybinds.init();
|
||||
Commands.init();
|
||||
}
|
||||
|
||||
public static OptionsProfilesModConfiguration config() {
|
||||
|
|
@ -79,7 +46,25 @@ public class OptionsProfilesMod {
|
|||
}
|
||||
}
|
||||
|
||||
private static void handleClientPlayerEvent(LocalPlayer player, boolean isOnLeave) {
|
||||
public static void handleClientLoad() {
|
||||
try (Stream<Path> paths = Files.list(Profiles.PROFILES_DIRECTORY)) {
|
||||
paths.filter(Files::isDirectory)
|
||||
.forEach(path -> {
|
||||
String profileName = path.getFileName().toString();
|
||||
|
||||
// This gets the configuration but also creates the configuration file if it is not there
|
||||
ProfileConfiguration profileConfiguration = ProfileConfiguration.get(profileName);
|
||||
if (profileConfiguration.shouldLoadOnStartup()) {
|
||||
Profiles.loadProfile(profileName);
|
||||
OptionsProfilesMod.LOGGER.info("[Profile '{}']: Loaded on startup", profileName);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
OptionsProfilesMod.LOGGER.error("An error occurred when initializing", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void handleClientPlayerEvent(LocalPlayer player, boolean isOnLeave) {
|
||||
if (player == null) return;
|
||||
Connection connection = player.connection.getConnection();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,16 +1,12 @@
|
|||
package net.trafficlunar.optionsprofiles.gui;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.minecraft.client.gui.screens.options.controls.KeyBindsList;
|
||||
import net.minecraft.client.gui.GuiGraphicsExtractor;
|
||||
import net.trafficlunar.optionsprofiles.OptionsProfilesMod;
|
||||
import net.trafficlunar.optionsprofiles.profiles.ProfileConfiguration;
|
||||
import net.trafficlunar.optionsprofiles.profiles.Profiles;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.gui.GuiGraphics;
|
||||
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
|
||||
import net.minecraft.client.gui.components.CycleButton;
|
||||
import net.minecraft.client.gui.components.EditBox;
|
||||
|
|
@ -137,14 +133,15 @@ public class OptionsToggleList extends ContainerObjectSelectionList<OptionsToggl
|
|||
}
|
||||
}
|
||||
|
||||
public void renderContent(GuiGraphics guiGraphics, int mouseX, int mouseY, boolean hovered, float tickDelta) {
|
||||
@Override
|
||||
public void extractContent(GuiGraphicsExtractor guiGraphics, int mouseX, int mouseY, boolean hovered, float tickDelta) {
|
||||
int posX = OptionsToggleList.this.scrollBarX() - this.toggleButton.getWidth() - 10;
|
||||
int posY = this.getContentY() - 2;
|
||||
|
||||
this.toggleButton.setPosition(posX, posY);
|
||||
this.toggleButton.render(guiGraphics, mouseX, mouseY, tickDelta);
|
||||
this.toggleButton.extractRenderState(guiGraphics, mouseX, mouseY, tickDelta);
|
||||
|
||||
guiGraphics.drawString(OptionsToggleList.this.minecraft.font, this.optionKey, this.getContentX(), this.getContentYMiddle() - 4, -1);
|
||||
guiGraphics.centeredText(OptionsToggleList.this.minecraft.font, this.optionKey, this.getContentX(), this.getContentYMiddle() - 4, -1);
|
||||
}
|
||||
|
||||
public List<? extends GuiEventListener> children() {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package net.trafficlunar.optionsprofiles.gui;
|
||||
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.client.gui.GuiGraphics;
|
||||
import net.minecraft.client.gui.components.Button;
|
||||
import net.minecraft.client.gui.components.EditBox;
|
||||
import net.minecraft.client.gui.components.StringWidget;
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
package net.trafficlunar.optionsprofiles.gui;
|
||||
|
||||
import net.minecraft.client.gui.GuiGraphicsExtractor;
|
||||
import net.trafficlunar.optionsprofiles.OptionsProfilesMod;
|
||||
import net.trafficlunar.optionsprofiles.profiles.ProfileConfiguration;
|
||||
import net.trafficlunar.optionsprofiles.profiles.Profiles;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.gui.GuiGraphics;
|
||||
import net.minecraft.client.gui.components.Button;
|
||||
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
|
||||
import net.minecraft.client.gui.components.events.GuiEventListener;
|
||||
|
|
@ -93,17 +91,18 @@ public class ProfilesList extends ContainerObjectSelectionList<ProfilesList.Prof
|
|||
.build();
|
||||
}
|
||||
|
||||
public void renderContent(GuiGraphics guiGraphics, int mouseX, int mouseY, boolean hovered, float tickDelta) {
|
||||
@Override
|
||||
public void extractContent(GuiGraphicsExtractor guiGraphics, int mouseX, int mouseY, boolean hovered, float tickDelta) {
|
||||
int posX = ProfilesList.this.scrollBarX() - this.loadButton.getWidth() - 10;
|
||||
int posY = this.getContentY() - 2;
|
||||
|
||||
this.editButton.setPosition(posX - this.editButton.getWidth(), posY);
|
||||
this.editButton.render(guiGraphics, mouseX, mouseY, tickDelta);
|
||||
this.editButton.extractRenderState(guiGraphics, mouseX, mouseY, tickDelta);
|
||||
|
||||
this.loadButton.setPosition(posX, posY);
|
||||
this.loadButton.render(guiGraphics, mouseX, mouseY, tickDelta);
|
||||
this.loadButton.extractRenderState(guiGraphics, mouseX, mouseY, tickDelta);
|
||||
|
||||
guiGraphics.drawString(ProfilesList.this.minecraft.font, this.profileName, this.getContentX(), this.getContentYMiddle() - 4, -1);
|
||||
guiGraphics.centeredText(ProfilesList.this.minecraft.font, this.profileName, this.getContentX(), this.getContentYMiddle() - 4, -1);
|
||||
}
|
||||
|
||||
public List<? extends GuiEventListener> children() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
package net.trafficlunar.optionsprofiles.platform;
|
||||
|
||||
import net.trafficlunar.optionsprofiles.OptionsProfilesMod;
|
||||
import net.trafficlunar.optionsprofiles.platform.services.IPlatformHelper;
|
||||
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
// Service loaders are a built-in Java feature that allow us to locate implementations of an interface that vary from one
|
||||
// environment to another. In the context of MultiLoader we use this feature to access a mock API in the common code that
|
||||
// is swapped out for the platform specific implementation at runtime.
|
||||
public class Services {
|
||||
|
||||
// In this example we provide a platform helper which provides information about what platform the mod is running on.
|
||||
// For example this can be used to check if the code is running on NeoForge vs Fabric, or to ask the modloader if another
|
||||
// mod is loaded.
|
||||
public static final IPlatformHelper PLATFORM = load(IPlatformHelper.class);
|
||||
|
||||
// This code is used to load a service for the current environment. Your implementation of the service must be defined
|
||||
// manually by including a text file in META-INF/services named with the fully qualified class name of the service.
|
||||
// Inside the file you should write the fully qualified class name of the implementation to load for the platform. For
|
||||
// example our file on Forge points to ForgePlatformHelper while Fabric points to FabricPlatformHelper.
|
||||
public static <T> T load(Class<T> clazz) {
|
||||
|
||||
final T loadedService = ServiceLoader.load(clazz, Services.class.getClassLoader())
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new NullPointerException("Failed to load service for " + clazz.getName()));
|
||||
OptionsProfilesMod.LOGGER.debug("Loaded {} for service {}", loadedService, clazz);
|
||||
return loadedService;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package net.trafficlunar.optionsprofiles.platform.services;
|
||||
|
||||
public interface IPlatformHelper {
|
||||
String getPlatformName();
|
||||
|
||||
boolean isModLoaded(String modId);
|
||||
}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package net.trafficlunar.optionsprofiles.profiles;
|
||||
|
||||
import dev.architectury.platform.Platform;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.trafficlunar.optionsprofiles.OptionsProfilesMod;
|
||||
import net.trafficlunar.optionsprofiles.platform.Services;
|
||||
import net.trafficlunar.optionsprofiles.profiles.loaders.DistantHorizonsLoader;
|
||||
import net.trafficlunar.optionsprofiles.profiles.loaders.IrisLoader;
|
||||
import net.trafficlunar.optionsprofiles.profiles.loaders.SodiumExtraLoader;
|
||||
|
|
@ -88,13 +88,13 @@ public class Profiles {
|
|||
|
||||
copyOptionFile(profile, OPTIONS_FILE);
|
||||
copyOptionFile(profile, OPTIFINE_OPTIONS_FILE);
|
||||
if (Platform.isModLoaded("sodium"))
|
||||
if (Services.PLATFORM.isModLoaded("sodium"))
|
||||
copyOptionFile(profile, SODIUM_OPTIONS_FILE);
|
||||
if (Platform.isModLoaded("sodium-extra"))
|
||||
if (Services.PLATFORM.isModLoaded("sodium-extra"))
|
||||
copyOptionFile(profile, SODIUM_EXTRA_OPTIONS_FILE);
|
||||
if (Platform.isModLoaded("iris"))
|
||||
if (Services.PLATFORM.isModLoaded("iris"))
|
||||
copyOptionFile(profile, IRIS_OPTIONS_FILE);
|
||||
if (Platform.isModLoaded("distanthorizons"))
|
||||
if (Services.PLATFORM.isModLoaded("distanthorizons"))
|
||||
copyOptionFile(profile, DISTANT_HORIZONS_OPTIONS_FILE);
|
||||
|
||||
if (!overwriting) {
|
||||
|
|
@ -255,13 +255,13 @@ public class Profiles {
|
|||
loadOptionFile(profileName, OPTIONS_FILE);
|
||||
loadOptionFile(profileName, OPTIFINE_OPTIONS_FILE);
|
||||
|
||||
if (Platform.isModLoaded("sodium"))
|
||||
if (Services.PLATFORM.isModLoaded("sodium"))
|
||||
loadOptionFile(profileName, SODIUM_OPTIONS_FILE, SodiumLoader::load);
|
||||
if (Platform.isModLoaded("sodium-extra"))
|
||||
if (Services.PLATFORM.isModLoaded("sodium-extra"))
|
||||
loadOptionFile(profileName, SODIUM_EXTRA_OPTIONS_FILE, SodiumExtraLoader::load);
|
||||
if (Platform.isModLoaded("iris"))
|
||||
if (Services.PLATFORM.isModLoaded("iris"))
|
||||
loadOptionFile(profileName, IRIS_OPTIONS_FILE, IrisLoader::load);
|
||||
if (Platform.isModLoaded("distanthorizons")) {
|
||||
if (Services.PLATFORM.isModLoaded("distanthorizons")) {
|
||||
loadOptionFile(profileName, DISTANT_HORIZONS_OPTIONS_FILE); // Overwrite / load original Disant Horizons option file
|
||||
loadOptionFile(profileName, DISTANT_HORIZONS_OPTIONS_FILE, DistantHorizonsLoader::load); // Tell Distant Horizons mod to reload configuration
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import me.flashyreese.mods.sodiumextra.client.SodiumExtraClientMod;
|
|||
import me.flashyreese.mods.sodiumextra.client.gui.FogTypeConfig;
|
||||
import me.flashyreese.mods.sodiumextra.client.gui.SodiumExtraGameOptions;
|
||||
import me.flashyreese.mods.sodiumextra.common.util.ResourceLocationSerializer;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.level.material.FogType;
|
||||
import net.trafficlunar.optionsprofiles.OptionsProfilesMod;
|
||||
|
|
@ -19,7 +20,7 @@ import java.util.Map;
|
|||
public class SodiumExtraLoader {
|
||||
public static void load(Path file) {
|
||||
try (FileReader reader = new FileReader(file.toFile())) {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(ResourceLocation.class, new ResourceLocationSerializer()).create();
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(Identifier.class, new IdentifierSerializer()).create();
|
||||
Configuration configuration = gson.fromJson(reader, Configuration.class);
|
||||
|
||||
apply(configuration);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package net.trafficlunar.optionsprofiles.profiles.loaders;
|
||||
|
||||
import me.flashyreese.mods.sodiumextra.client.config.SodiumExtraGameOptions;
|
||||
import net.caffeinemc.mods.sodium.client.render.chunk.DeferMode;
|
||||
import net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.QuadSplittingMode;
|
||||
import net.trafficlunar.optionsprofiles.OptionsProfilesMod;
|
||||
|
|
@ -25,6 +26,7 @@ public class SodiumLoader {
|
|||
}
|
||||
|
||||
private static void apply(Configuration configuration) {
|
||||
SodiumExtraGameOptions
|
||||
SodiumClientMod.options().quality.weatherQuality = SodiumGameOptions.WeatherQuality.valueOf(configuration.quality.weather_quality);
|
||||
SodiumClientMod.options().quality.leavesQuality = SodiumGameOptions.LeavesQuality.valueOf(configuration.quality.leaves_quality);
|
||||
SodiumClientMod.options().quality.enableVignette = configuration.quality.enable_vignette;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
{
|
||||
"required": true,
|
||||
"minVersion": "0.8",
|
||||
"package": "net.trafficlunar.optionsprofiles.mixin",
|
||||
"compatibilityLevel": "JAVA_17",
|
||||
"minVersion": "0.8",
|
||||
"client": [
|
||||
"MixinOptionsScreen"
|
||||
],
|
||||
"mixins": [
|
||||
],
|
||||
"mixins": [],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
}
|
||||
|
|
|
|||
6
common/src/main/resources/pack.mcmeta
Normal file
6
common/src/main/resources/pack.mcmeta
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"pack": {
|
||||
"description": "${mod_name}",
|
||||
"pack_format": 8
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue