Skip to content

Commit

Permalink
Backport opRevealImpersonations fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Jul 20, 2024
1 parent 5eced4c commit 7d8d146
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
------------------------------------------------------
Version 2.10.3
------------------------------------------------------
- Fixed impersonator's names appearing as "Faked(Faked)" for operators when the `opRevealImpersonations` gamerule is off
- Toggling the `impersonate:opRevealImpersonations` gamerule now properly updates usernames without needing a re-log

------------------------------------------------------
Version 2.10.2
------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jb_annotations_version = 23.0.0
apiguardian_version = 1.1.2

# Mod Properties
mod_version = 2.10.2
mod_version = 2.10.3
maven_group = org.ladysnake
archives_base_name = impersonate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package io.github.ladysnake.impersonate.impl;

import io.github.ladysnake.impersonate.Impersonator;
import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory;
import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry;
import net.minecraft.server.network.ServerPlayerEntity;
Expand All @@ -27,15 +28,21 @@ public final class ImpersonateGamerules {
register("fakeCapes", GameRuleFactory.createBooleanRule(false, (server, rule) -> {
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
if (rule.get()) {
((PlayerEntityExtensions)player).impersonate_resetCape();
((PlayerEntityExtensions) player).impersonate_resetCape();
} else {
((PlayerEntityExtensions) player).impersonate_disableCape();
}
}
}));

public static final GameRules.Key<GameRules.BooleanRule> OP_REVEAL_IMPERSONATIONS =
register("opRevealImpersonations", GameRuleFactory.createBooleanRule(true));
register("opRevealImpersonations", GameRuleFactory.createBooleanRule(true, (server, rule) -> {
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
if (Impersonator.get(player) instanceof PlayerImpersonator playerImpersonator) {
playerImpersonator.syncChanges(playerImpersonator.getImpersonatedProfile());
}
}
}));

public static final GameRules.Key<GameRules.BooleanRule> LOG_REVEAL_IMPERSONATIONS =
register("logRevealImpersonations", GameRuleFactory.createBooleanRule(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.text.Style;
import net.minecraft.text.TextContent;

import java.util.Objects;
import java.util.Optional;

public class ImpersonateTextContent implements RecipientAwareTextContent {
Expand All @@ -39,8 +40,9 @@ public static TextContent get(PlayerEntity player) {
public static TextContent get(PlayerEntity player, boolean reveal) {
Impersonator impersonator = Impersonator.get(player);
String fakeName = impersonator.getEditedProfile().getName();
String trueText = String.format("%s(%s)", fakeName, player.getGameProfile().getName());
return new ImpersonateTextContent(trueText, fakeName, reveal);
String trueName = player.getGameProfile().getName();
String trueText = String.format("%s(%s)", fakeName, trueName);
return new ImpersonateTextContent(trueText, fakeName, reveal && !Objects.equals(fakeName, trueName));
}

private ImpersonateTextContent(String trueText, String fakedText, boolean revealed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void resolvePlayerListEntries(PlayerListS2CPacket packet, ServerPl
for (PlayerListS2CPacket.Entry entry : packet.getEntries()) {
PlayerEntity playerEntry = player.server.getPlayerManager().getPlayer(entry.profileId());
if (playerEntry != null) {
Impersonator impersonator = Impersonate.IMPERSONATION.get(playerEntry);
Impersonator impersonator = Impersonator.get(playerEntry);
if (impersonator.isImpersonating()) {
// OPs get the true profile with semi-fake display name, others get a complete lie
PlayerListS2CPacketEntryAccessor accessibleEntry = (PlayerListS2CPacketEntryAccessor) (Object) entry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ private void setImpersonatedProfile(@Nullable GameProfile profile) {
this.impersonatedProfile = profile;
this.editedProfile = profile == null ? null : new GameProfile(this.getActualProfile().getId(), this.impersonatedProfile.getName());
this.syncChanges(profile);
Impersonate.IMPERSONATION.sync(this.player);
}
}

private void syncChanges(@Nullable GameProfile profile) {
public void syncChanges(@Nullable GameProfile profile) {
if (this.player instanceof ServerPlayerEntity serverPlayer && serverPlayer.networkHandler != null) {
updatePlayerLists(new PlayerRemoveS2CPacket(List.of(this.player.getUuid())));
this.applyCapeGamerule(serverPlayer, profile);
ServerPlayerSkins.setSkin(serverPlayer, profile == null ? this.getActualProfile() : profile);
updatePlayerLists(PlayerListS2CPacket.entryFromPlayer(List.of(serverPlayer)));
}
Impersonate.IMPERSONATION.sync(this.player);
}

private void applyCapeGamerule(ServerPlayerEntity player, GameProfile impersonatedProfile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.mojang.authlib.GameProfile;
import io.github.ladysnake.impersonate.Impersonate;
import io.github.ladysnake.impersonate.Impersonator;
import net.minecraft.server.command.ListCommand;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
Expand All @@ -42,7 +43,7 @@ private static void fakeNameAndUuid(ServerPlayerEntity player, CallbackInfoRetur
Object[] args = cnt.getArgs();
// Defend against other mods changing the text
if (args.length == 2 && args[0] instanceof Text && args[1] instanceof UUID) {
GameProfile impersonatedProfile = Impersonate.IMPERSONATION.get(player).getImpersonatedProfile();
GameProfile impersonatedProfile = Impersonator.get(player).getImpersonatedProfile();

if (impersonatedProfile != null) {
// Name is already covered by PlayerEntity#getName mixin
Expand Down

0 comments on commit 7d8d146

Please sign in to comment.