This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
96 additions
and
5 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
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
88 changes: 88 additions & 0 deletions
88
src/main/java/org/samo_lego/simpleauth/mixin/MixinWorldSaveHandler.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,88 @@ | ||
package org.samo_lego.simpleauth.mixin; | ||
|
||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.nbt.NbtIo; | ||
import net.minecraft.world.WorldSaveHandler; | ||
import org.apache.logging.log4j.Logger; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.ModifyVariable; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
|
||
import static org.samo_lego.simpleauth.SimpleAuth.config; | ||
import static org.samo_lego.simpleauth.SimpleAuth.mojangAccountNamesCache; | ||
|
||
@Mixin(WorldSaveHandler.class) | ||
public class MixinWorldSaveHandler { | ||
|
||
@Final | ||
@Shadow | ||
private File playerDataDir; | ||
|
||
@Unique | ||
private boolean fileExists; | ||
|
||
@Final | ||
@Shadow | ||
private static Logger LOGGER; | ||
|
||
/** | ||
* Saves whether player save file exists. | ||
* | ||
* @param playerEntity | ||
* @param cir | ||
* @param compoundTag | ||
* @param file | ||
*/ | ||
@Inject( | ||
method = "loadPlayerData(Lnet/minecraft/entity/player/PlayerEntity;)Lnet/minecraft/nbt/CompoundTag;", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Ljava/io/File;exists()Z" | ||
), | ||
locals = LocalCapture.CAPTURE_FAILHARD | ||
) | ||
private void fileExists(PlayerEntity playerEntity, CallbackInfoReturnable<CompoundTag> cir, CompoundTag compoundTag, File file) { | ||
// @ModifyVariable cannot capture locals | ||
this.fileExists = file.exists(); | ||
} | ||
|
||
/** | ||
* Loads offline-uuid player data to compoundTag in order to migrate from offline to online. | ||
* | ||
* @param compoundTag null compound tag. | ||
* @param player player who might need migration of datd. | ||
* @return compoundTag containing migrated data. | ||
*/ | ||
@ModifyVariable( | ||
method = "loadPlayerData(Lnet/minecraft/entity/player/PlayerEntity;)Lnet/minecraft/nbt/CompoundTag;", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Ljava/io/File;exists()Z" | ||
) | ||
) | ||
private CompoundTag migratePlayerData(CompoundTag compoundTag, PlayerEntity player) { | ||
// Checking for offline player data only if online doesn't exist yet | ||
if(config.experimental.premiumAutologin && mojangAccountNamesCache.contains(player.getGameProfile().getName()) && !this.fileExists) { | ||
File file = new File(this.playerDataDir, PlayerEntity.getOfflinePlayerUuid(player.getGameProfile().getName()) + ".dat"); | ||
if (file.exists() && file.isFile()) | ||
try { | ||
compoundTag = NbtIo.readCompressed(new FileInputStream(file)); | ||
} | ||
catch (IOException e) { | ||
LOGGER.warn("Failed to load player data for {}", player.getGameProfile().getName()); | ||
} | ||
} | ||
return compoundTag; | ||
} | ||
} |
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