Skip to content

Commit

Permalink
Merge branch '1.20.1' into 1.19.4
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed May 2, 2024
2 parents a698399 + 69ca81a commit 6d6a503
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 26 deletions.
3 changes: 3 additions & 0 deletions common/src/main/java/xaeroplus/XaeroPlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import net.lenni0451.lambdaevents.generator.LambdaMetaFactoryGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xaero.common.platform.Services;

import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;

public class XaeroPlus {
public static final Logger LOGGER = LoggerFactory.getLogger("XaeroPlus");
public static final LambdaManager EVENT_BUS = LambdaManager.basic(new LambdaMetaFactoryGenerator());
public static AtomicBoolean initialized = new AtomicBoolean(false);
public static final File configFile = Services.PLATFORM.getConfigDir().resolve("xaeroplus.txt").toFile();
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void createWaypointInject(final String[] args, final WaypointWorld wpw, f

@Inject(method = "saveSettings", at = @At("RETURN"))
public void saveSettings(final CallbackInfo ci) throws IOException {
XaeroPlusModSettingsHooks.saveSettings(this.modMain.getConfigFile(), ALL_MINIMAP_SETTINGS.get());
XaeroPlusModSettingsHooks.saveSettings();
}

@Inject(method = "loadSettingsFile", at = @At("RETURN"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import xaero.map.WorldMap;
import xaero.map.settings.ModOptions;
import xaero.map.settings.ModSettings;
import xaeroplus.settings.XaeroPlusModSettingsHooks;
Expand All @@ -29,7 +28,7 @@ public void initInject(final CallbackInfo ci) {

@Inject(method = "saveSettings", at = @At(value = "RETURN"))
public void saveSettings(final CallbackInfo ci) throws IOException {
XaeroPlusModSettingsHooks.saveSettings(WorldMap.optionsFile, XAERO_PLUS_WORLDMAP_SETTINGS);
XaeroPlusModSettingsHooks.saveSettings();
}

@Inject(method = "loadSettingsFile", at = @At("RETURN"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,89 @@

import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import xaeroplus.Globals;
import xaeroplus.XaeroPlus;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Optional;

public class XaeroPlusModSettingsHooks {
private static int loadCount = 0;

public static void saveSettings(File configFile, List<XaeroPlusSetting> settings) throws IOException {
try (PrintWriter writer = new PrintWriter(new FileWriter(configFile, true))) {
settings.forEach(k -> {
public static void saveSettings() throws IOException {
saveXPSettings();
}

public static synchronized void saveXPSettings() throws IOException {
File tempFile = new File(XaeroPlus.configFile.getAbsolutePath() + ".tmp");
if (tempFile.exists()) tempFile.delete();
try (PrintWriter writer = new PrintWriter(new FileWriter(tempFile, true))) {
XaeroPlusSettingsReflectionHax.ALL_SETTINGS.get().forEach(k -> {
writer.println(k.getSettingName() + ":" +
((k instanceof XaeroPlusBooleanSetting)
? ((XaeroPlusBooleanSetting) k).getValue()
: (k instanceof XaeroPlusFloatSetting)
? ((XaeroPlusFloatSetting) k).getValue()
: (k instanceof XaeroPlusEnumSetting)
? ((XaeroPlusEnumSetting) k).getValueIndex()
: ""));
((k instanceof XaeroPlusBooleanSetting)
? ((XaeroPlusBooleanSetting) k).getValue()
: (k instanceof XaeroPlusFloatSetting)
? ((XaeroPlusFloatSetting) k).getValue()
: (k instanceof XaeroPlusEnumSetting)
? ((XaeroPlusEnumSetting) k).getValueIndex()
: ""));
});
}
Files.move(tempFile.toPath(), XaeroPlus.configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

public static void loadSettings(File file, List<XaeroPlusSetting> settings) throws IOException {
loadCount++;
// load from XP config file
if (loadCount == 1) loadXPSettings();
// load legacy XP settings from xaero config files
loadXPSettingsFromFile(file, settings);
// 1 for minimap, 1 for worldmap
// the reason we need to track load count is on Fabric, WorldMap or Minimap mods could load first
// but we have legacy settings that are stored in their config files
// xaero mods save their settings immediately after loading
// which would overwrite our legacy settings
if (loadCount == 2) Globals.onAllSettingsLoaded();
}

public static synchronized void loadXPSettings() {
try {
if (!XaeroPlus.configFile.exists()) return;
loadXPSettingsFromFile(XaeroPlus.configFile, XaeroPlusSettingsReflectionHax.ALL_SETTINGS.get());
} catch (final Throwable e) {
XaeroPlus.LOGGER.error("Error loading XaeroPlus settings", e);
}
}

public static synchronized void loadXPSettingsFromFile(File file, List<XaeroPlusSetting> settings) throws IOException {
try(BufferedReader reader = new BufferedReader(new FileReader(file))) {
String s;
while ((s = reader.readLine()) != null) {
String[] args = s.split(":");
Optional<XaeroPlusSetting> settingOptional = settings.stream()
.filter(setting -> setting.getSettingName().equalsIgnoreCase(args[0]))
.findFirst();
.filter(setting -> setting.getSettingName().equalsIgnoreCase(args[0]))
.findFirst();
if (settingOptional.isPresent()) {
final XaeroPlusSetting setting = settingOptional.get();
if (setting instanceof XaeroPlusBooleanSetting) {
((XaeroPlusBooleanSetting) setting).setValue(Boolean.parseBoolean(args[1]));
} else if (setting instanceof XaeroPlusFloatSetting) {
((XaeroPlusFloatSetting) setting).setValue(Float.parseFloat(args[1]));
} else if (setting instanceof XaeroPlusEnumSetting) {
((XaeroPlusEnumSetting) setting).setValueIndex((int) Float.parseFloat(args[1]));
if (setting instanceof XaeroPlusBooleanSetting set) {
var value = Boolean.parseBoolean(args[1]);
if (value != set.getValue()) set.setValue(value);
set.setValue(Boolean.parseBoolean(args[1]));
} else if (setting instanceof XaeroPlusFloatSetting set) {
var value = Float.parseFloat(args[1]);
if (value != set.getValue()) set.setValue(value);
} else if (setting instanceof XaeroPlusEnumSetting set) {
var value = (int) Float.parseFloat(args[1]);
if (set.getValueIndex() != value) set.setValueIndex(value);
}
}
}
}

// 1 for minimap, 1 for worldmap
if (loadCount == 2) Globals.onAllSettingsLoaded();
}


public static void getClientBooleanValue(String enumString, List<XaeroPlusSetting> settings, CallbackInfoReturnable<Boolean> cir) {
Optional<XaeroPlusBooleanSetting> settingOptional = settings.stream()
.filter(xaeroPlusSetting -> xaeroPlusSetting.getSettingName().equals(enumString) && xaeroPlusSetting instanceof XaeroPlusBooleanSetting)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* Registry for XaeroPlus-specific settings
*/
public final class XaeroPlusSettingRegistry {

/**
* The order settings are defined here determines the order in the settings GUI's.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package xaeroplus.fabric.mixin.client;

import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.loader.api.FabricLoader;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import xaero.common.PlatformContextFabric;

@Mixin(value = PlatformContextFabric.class, remap = false)
public class MixinPlatformContextFabric {
@Redirect(method = "postLoadClient", at = @At(
value = "INVOKE",
target = "Lnet/fabricmc/fabric/api/resource/ResourceManagerHelper;registerReloadListener(Lnet/fabricmc/fabric/api/resource/IdentifiableResourceReloadListener;)V"
))
public void disableResourceListenerForDevIdkItBreaksRunClient(final ResourceManagerHelper instance, final IdentifiableResourceReloadListener identifiableResourceReloadListener) {
if (!FabricLoader.getInstance().isDevelopmentEnvironment()) {
instance.registerReloadListener(identifiableResourceReloadListener);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package xaeroplus.fabric.mixin.client;

import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.loader.api.FabricLoader;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import xaero.map.WorldMapFabric;

@Mixin(value = WorldMapFabric.class, remap = false)
public class MixinWorldMapFabric {
@Redirect(method = "loadClient", at = @At(
value = "INVOKE",
target = "Lnet/fabricmc/fabric/api/resource/ResourceManagerHelper;registerReloadListener(Lnet/fabricmc/fabric/api/resource/IdentifiableResourceReloadListener;)V"
))
public void disableResourceListenerForDevIdkItBreaksRunClient(final ResourceManagerHelper instance, final IdentifiableResourceReloadListener identifiableResourceReloadListener) {
if (!FabricLoader.getInstance().isDevelopmentEnvironment()) {
instance.registerReloadListener(identifiableResourceReloadListener);
}
}
}
2 changes: 2 additions & 0 deletions fabric/src/main/resources/xaeroplus-fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"plugin": "xaeroplus.fabric.util.compat.XaeroPlusCompatibleMinimapMixinPlugin",
"client": [
"MixinBetterPVP",
"MixinPlatformContextFabric",
"MixinWorldMap",
"MixinWorldMapFabric",
"MixinXaeroMinimap",
"mc.MixinMinecraftClientFabric"
]
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pluginManagement {
}
}
gradle.extra.apply {
set("mod_version", "2.9")
set("mod_version", "2.11")
set("maven_group", "xaeroplus")
set("archives_base_name", "XaeroPlus")
set("minecraft_version", "1.19.4")
Expand Down

0 comments on commit 6d6a503

Please sign in to comment.