-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Difficulty Lock + Lang Config Update
- Loading branch information
1 parent
9a60008
commit b231e9e
Showing
16 changed files
with
444 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...m/nomiceu/nomilabs/LabsVersionConfig.java → ...eu/nomilabs/config/LabsVersionConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/com/nomiceu/nomilabs/mixin/CommandDifficultyMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
87
src/main/java/com/nomiceu/nomilabs/mixin/DedicatedServerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/main/java/com/nomiceu/nomilabs/mixin/GuiOptionsMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/nomiceu/nomilabs/mixin/IntegratedServerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/nomiceu/nomilabs/mixinhelper/DifficultySettableServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.