Skip to content

Commit

Permalink
Difficulty Lock + Lang Config Update
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jul 4, 2024
1 parent 9a60008 commit b231e9e
Show file tree
Hide file tree
Showing 16 changed files with 444 additions and 20 deletions.
9 changes: 7 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//version: 1718246243
//version: 1719793363
/*
* DO NOT CHANGE THIS FILE!
* Also, you may replace this file at any time if there is an update available.
Expand Down Expand Up @@ -69,6 +69,7 @@ propertyDefaultIfUnset("includeMCVersionJar", false)
propertyDefaultIfUnset("autoUpdateBuildScript", false)
propertyDefaultIfUnset("modArchivesBaseName", project.modId)
propertyDefaultIfUnsetWithEnvVar("developmentEnvironmentUserName", "Developer", "DEV_USERNAME")
propertyDefaultIfUnset("additionalJavaArguments", "")
propertyDefaultIfUnset("enableJava17RunTasks", false)
propertyDefaultIfUnset("generateGradleTokenClass", "")
propertyDefaultIfUnset("gradleTokenModId", "")
Expand Down Expand Up @@ -119,7 +120,7 @@ if (!getFile(targetPackageJava).exists() && !getFile(targetPackageScala).exists(

if (apiPackage) {
final String endApiPath = modGroupPath + '/' + apiPackagePath
if (useSrcApiPath) {
if (useSrcApiPath.toBoolean()) {
targetPackageJava = 'src/api/java/' + endApiPath
targetPackageScala = 'src/api/scala/' + endApiPath
targetPackageKotlin = 'src/api/kotlin/' + endApiPath
Expand Down Expand Up @@ -382,6 +383,10 @@ minecraft {
])
}

if (additionalJavaArguments.size() != 0) {
extraRunJvmArguments.addAll(additionalJavaArguments.split(';'))
}

if (enableJava17RunTasks.toBoolean()) {
lwjgl3Version = "3.3.2"
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/config/LabsConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nomiceu.nomilabs.config;

import net.minecraft.world.EnumDifficulty;
import net.minecraftforge.common.config.Config;

import com.cleanroommc.configanytime.ConfigAnytime;
Expand Down Expand Up @@ -387,6 +388,11 @@ public static class Advanced {
@Config.Name("minecraft window overrides")
public final WindowOverrides windowOverrides = new WindowOverrides();

@Config.Comment({ "Overrides for the Minecraft Difficulty." })
@Config.LangKey("config.nomilabs.advanced.difficulty")
@Config.Name("minecraft difficulty overrides")
public final DifficultyOverrides difficultyOverrides = new DifficultyOverrides();

@Config.Comment({ "List of Regex Patterns to ignore if they are included in the ITEM missing registry list.",
"Do not change unless you know what you are doing!",
"This can be very inefficient with lots of patterns and lots of missing registries. Try to condense it into one pattern!",
Expand Down Expand Up @@ -582,6 +588,35 @@ public static class ControlMenuTooltipSettings {
@Config.LangKey("config.nomilabs.advanced.controls_tooltips.show_class")
public boolean showClass = false;
}

public static class DifficultyOverrides {

@Config.Comment({ "Whether to Override Difficulty in Normal Mode.",
"[default: false]" })
@Config.LangKey("config.nomilabs.advanced.difficulty.enable_normal")
@Config.RequiresWorldRestart
public boolean overrideDifficultyNormal = false;

@Config.Comment({ "Whether to Override Difficulty in Expert Mode.",
"[default: false]" })
@Config.LangKey("config.nomilabs.advanced.difficulty.enable_expert")
@Config.RequiresWorldRestart
public boolean overrideDifficultyExpert = false;

@Config.Comment({
"Difficulty (Locked) Override in Normal Mode. Does Not Apply if overrideDifficultyNormal is set to false!",
"[default: NORMAL]" })
@Config.LangKey("config.nomilabs.advanced.difficulty.normal_override")
@Config.RequiresWorldRestart
public EnumDifficulty difficultyNormal = EnumDifficulty.NORMAL;

@Config.Comment({
"Difficulty (Locked) Override in Expert Mode. Does Not Apply if overrideDifficultyExpert is set to false!",
"[default: NORMAL]" })
@Config.LangKey("config.nomilabs.advanced.difficulty.expert_override")
@Config.RequiresWorldRestart
public EnumDifficulty difficultyExpert = EnumDifficulty.NORMAL;
}
}

static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.nomiceu.nomilabs;
package com.nomiceu.nomilabs.config;

import net.minecraftforge.common.config.Config;

import com.cleanroommc.configanytime.ConfigAnytime;
import com.nomiceu.nomilabs.LabsValues;

@SuppressWarnings({ "CanBeFinal", "unused" })
@Config(modid = LabsValues.LABS_MODID, name = LabsValues.LABS_MODID + "-version")
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/event/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.RegistryEvent.MissingMappings;
import net.minecraftforge.event.entity.living.LivingEquipmentChangeEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Mod;
Expand Down Expand Up @@ -44,6 +45,7 @@
import com.nomiceu.nomilabs.remap.LabsRemappers;
import com.nomiceu.nomilabs.remap.Remapper;
import com.nomiceu.nomilabs.remap.datafixer.DataFixerHandler;
import com.nomiceu.nomilabs.util.LabsDifficultyHelper;
import com.nomiceu.nomilabs.util.LabsModeHelper;
import com.nomiceu.nomilabs.util.LabsNames;

Expand Down Expand Up @@ -141,6 +143,16 @@ public static void registerRecipes(RegistryEvent.Register<IRecipe> event) {
// com.nomiceu.nomilabs.recipe.LabsTestRecipes.initRecipes();
}

@SubscribeEvent
public static void onWorldLoad(WorldEvent.Load event) {
var toLock = LabsDifficultyHelper.getLockedDifficulty();
if (toLock == null) return;

// Lock Difficulty
event.getWorld().getWorldInfo().setDifficulty(toLock);
event.getWorld().getWorldInfo().setDifficultyLocked(true);
}

@SubscribeEvent
public static void onEquipmentChangeEvent(LivingEquipmentChangeEvent event) {
ItemExcitationCoil.onEquipmentChange(event);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.nomiceu.nomilabs.mixin;

import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandDifficulty;
import net.minecraft.command.ICommandSender;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.EnumDifficulty;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.nomiceu.nomilabs.mixinhelper.DifficultySettableServer;
import com.nomiceu.nomilabs.util.LabsDifficultyHelper;
import com.nomiceu.nomilabs.util.LabsModeHelper;

/**
* Prevents Changing Difficulty when Labs Locked, and saves changed difficulties to server.properties on Dedicated
* Servers.
*/
@Mixin(CommandDifficulty.class)
public abstract class CommandDifficultyMixin extends CommandBase {

@Inject(method = "execute", at = @At("HEAD"), cancellable = true)
public void executeIfNotLocked(MinecraftServer server, ICommandSender sender, String[] args, CallbackInfo ci) {
var locked = LabsDifficultyHelper.getLockedDifficulty();
if (locked == null) return;

notifyCommandListener(sender, this, "command.nomilabs.difficulty.labs_locked_1",
new TextComponentTranslation(locked.getTranslationKey())
.setStyle(new Style().setColor(TextFormatting.DARK_AQUA)));
notifyCommandListener(sender, this, "command.nomilabs.difficulty.labs_locked_2",
new TextComponentString(LabsModeHelper.getFormattedMode())
.setStyle(new Style().setColor(TextFormatting.GOLD)));
ci.cancel();
}

@Redirect(method = "execute",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/server/MinecraftServer;setDifficultyForAllWorlds(Lnet/minecraft/world/EnumDifficulty;)V"))
public void setDifficultyAndSave(MinecraftServer instance, EnumDifficulty newDifficulty) {
if (instance instanceof DifficultySettableServer diff)
diff.setDifficultyForAllWorldsAndSave(newDifficulty);
else
instance.setDifficultyForAllWorlds(newDifficulty);
}
}
87 changes: 87 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/mixin/DedicatedServerMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.nomiceu.nomilabs.mixin;

import java.io.File;
import java.net.Proxy;

import net.minecraft.server.MinecraftServer;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraft.server.dedicated.PropertyManager;
import net.minecraft.server.management.PlayerProfileCache;
import net.minecraft.util.datafix.DataFixer;
import net.minecraft.world.EnumDifficulty;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import com.mojang.authlib.GameProfileRepository;
import com.mojang.authlib.minecraft.MinecraftSessionService;
import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService;
import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.mixinhelper.DifficultySettableServer;
import com.nomiceu.nomilabs.util.LabsDifficultyHelper;
import com.nomiceu.nomilabs.util.LabsModeHelper;
import com.nomiceu.nomilabs.util.LabsTranslate;

/**
* Gets the Locked Difficulty on Servers, Locking the Difficulty there as well.
* <p>
* This is because servers select difficulty from the 'server.properties' file.
* <p>
* Also overrides the difficulty in server.properties for consistency.
* <p>
* Allows for support of calling `setDifficultyForAllWorldsAndSave`, changing difficulty and saving to
* `server.properties` on the fly.
*/
@Mixin(DedicatedServer.class)
public abstract class DedicatedServerMixin extends MinecraftServer implements DifficultySettableServer {

/**
* Mandatory Ignored Constructor
*/
public DedicatedServerMixin(File anvilFileIn, Proxy proxyIn, DataFixer dataFixerIn,
YggdrasilAuthenticationService authServiceIn, MinecraftSessionService sessionServiceIn,
GameProfileRepository profileRepoIn, PlayerProfileCache profileCacheIn) {
super(anvilFileIn, proxyIn, dataFixerIn, authServiceIn, sessionServiceIn, profileRepoIn, profileCacheIn);
}

@Shadow
private PropertyManager settings;

@Inject(method = "getDifficulty", at = @At("HEAD"), cancellable = true)
public void getLockedDifficulty(CallbackInfoReturnable<EnumDifficulty> cir) {
var locked = LabsDifficultyHelper.getLockedDifficulty();
if (locked != null) cir.setReturnValue(locked);
}

@SuppressWarnings("LoggingSimilarMessage")
@Inject(method = "init", at = @At("RETURN"))
public void changeInitDifficulty(CallbackInfoReturnable<EnumDifficulty> cir) {
var locked = LabsDifficultyHelper.getLockedDifficulty();
var savedDifficulty = this.settings.getIntProperty("difficulty", 1);
if (locked != null && savedDifficulty != locked.getId()) {
NomiLabs.LOGGER.warn("===============================================");
NomiLabs.LOGGER.warn("============ LABS DIFFICULTY LOCK: ============");
NomiLabs.LOGGER.warn("-----------------------------------------------");

NomiLabs.LOGGER.warn("Server Difficulty was set to {}, it has now been overrided to {}!",
LabsTranslate.translate(EnumDifficulty.byId(savedDifficulty).getTranslationKey()),
LabsTranslate.translate(locked.getTranslationKey()));

NomiLabs.LOGGER.warn("This is because you are on {} mode!", LabsModeHelper.getFormattedMode());

NomiLabs.LOGGER.warn("-----------------------------------------------");
NomiLabs.LOGGER.warn("===============================================");

}
}

@Override
public void setDifficultyForAllWorldsAndSave(EnumDifficulty difficulty) {
setDifficultyForAllWorlds(difficulty);
settings.setProperty("difficulty", difficulty.getId());
settings.saveProperties();
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/mixin/GuiOptionsMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.nomiceu.nomilabs.mixin;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiOptions;
import net.minecraft.client.gui.GuiScreen;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.nomiceu.nomilabs.network.LabsDifficultyChangeMessage;
import com.nomiceu.nomilabs.network.LabsNetworkHandler;

/**
* Fixes Difficulty Button not setting difficulty for all worlds and saving to server.properties.
*/
@Mixin(GuiOptions.class)
public abstract class GuiOptionsMixin extends GuiScreen {

@Inject(method = "actionPerformed", at = @At("HEAD"))
public void handleProperDifficultyChange(GuiButton button, CallbackInfo ci) {
if (!button.enabled || button.id != 108) return;

// Set difficulty properly, but do not cancel
// Let operation set difficulty of world as well, to provide instantaneous change
LabsNetworkHandler.NETWORK_HANDLER
.sendToServer(new LabsDifficultyChangeMessage(mc.world.getDifficulty().getId() + 1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.nomiceu.nomilabs.mixin;

import java.io.File;
import java.net.Proxy;

import net.minecraft.server.MinecraftServer;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.server.management.PlayerProfileCache;
import net.minecraft.util.datafix.DataFixer;
import net.minecraft.world.EnumDifficulty;

import org.spongepowered.asm.mixin.Mixin;

import com.mojang.authlib.GameProfileRepository;
import com.mojang.authlib.minecraft.MinecraftSessionService;
import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService;
import com.nomiceu.nomilabs.mixinhelper.DifficultySettableServer;

/**
* Allows for support of calling `setDifficultyForAllWorldsAndSave`.
*/
@Mixin(IntegratedServer.class)
public abstract class IntegratedServerMixin extends MinecraftServer implements DifficultySettableServer {

/**
* Default Ignored Constructor
*/
public IntegratedServerMixin(File anvilFileIn, Proxy proxyIn, DataFixer dataFixerIn,
YggdrasilAuthenticationService authServiceIn, MinecraftSessionService sessionServiceIn,
GameProfileRepository profileRepoIn, PlayerProfileCache profileCacheIn) {
super(anvilFileIn, proxyIn, dataFixerIn, authServiceIn, sessionServiceIn, profileRepoIn, profileCacheIn);
}

@Override
public void setDifficultyForAllWorldsAndSave(EnumDifficulty difficulty) {
setDifficultyForAllWorlds(difficulty);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/nomiceu/nomilabs/mixin/MinecraftMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.nomiceu.nomilabs.LabsVersionConfig;
import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.config.LabsConfig;
import com.nomiceu.nomilabs.config.LabsVersionConfig;
import com.nomiceu.nomilabs.util.LabsModeHelper;

/**
Expand All @@ -41,7 +41,7 @@ private void setCustomTitle(String title) {
if (LabsConfig.advanced.windowOverrides.windowTitleOverride.isEmpty()) Display.setTitle(title);
else Display.setTitle(LabsConfig.advanced.windowOverrides.windowTitleOverride
.replace("{version}", LabsVersionConfig.formattedVersion)
.replace("{mode}", LabsModeHelper.getFormattedModePre()));
.replace("{mode}", LabsModeHelper.getFormattedMode()));
}

@Inject(method = "setWindowIcon", at = @At("HEAD"), cancellable = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nomiceu.nomilabs.mixinhelper;

import net.minecraft.world.EnumDifficulty;

public interface DifficultySettableServer {

void setDifficultyForAllWorldsAndSave(EnumDifficulty difficulty);
}
Loading

0 comments on commit b231e9e

Please sign in to comment.