Skip to content

Commit c099f1a

Browse files
authored
Adapt modifications of GameProfile PropertyMap to the new immutable design (#4248)
* Adapt modifications of GameProfile PropertyMap to the new immutable design * Add forgotten param 'final'
1 parent e90cb4a commit c099f1a

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

src/main/java/org/spongepowered/common/bridge/world/entity/player/PlayerBridge.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package org.spongepowered.common.bridge.world.entity.player;
2626

27+
import com.mojang.authlib.GameProfile;
2728
import net.kyori.adventure.bossbar.BossBar;
2829

2930
public interface PlayerBridge {
@@ -43,4 +44,6 @@ public interface PlayerBridge {
4344
void bridge$addActiveBossBar(BossBar bar);
4445

4546
void bridge$removeActiveBossBar(BossBar bar);
47+
48+
void bridge$setGameProfile(GameProfile profile);
4649
}

src/main/java/org/spongepowered/common/data/provider/entity/ServerPlayerData.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
*/
2525
package org.spongepowered.common.data.provider.entity;
2626

27+
import com.google.common.collect.HashMultimap;
28+
import com.mojang.authlib.GameProfile;
2729
import com.mojang.authlib.properties.Property;
30+
import com.mojang.authlib.properties.PropertyMap;
2831
import net.minecraft.network.protocol.game.ClientboundChangeDifficultyPacket;
2932
import net.minecraft.network.protocol.game.ClientboundPlayerAbilitiesPacket;
3033
import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket;
@@ -48,6 +51,7 @@
4851
import org.spongepowered.common.accessor.stats.StatsCounterAccessor;
4952
import org.spongepowered.common.bridge.server.level.ServerPlayerBridge;
5053
import org.spongepowered.common.bridge.server.level.ServerPlayerEntityHealthScaleBridge;
54+
import org.spongepowered.common.bridge.world.entity.player.PlayerBridge;
5155
import org.spongepowered.common.data.SpongeDataManager;
5256
import org.spongepowered.common.data.provider.DataProviderRegistrator;
5357
import org.spongepowered.common.profile.SpongeProfileProperty;
@@ -81,7 +85,11 @@ public static void register(final DataProviderRegistrator registrator) {
8185
return new SpongeProfileProperty(properties.iterator().next());
8286
})
8387
.set((h ,v) -> {
84-
h.getGameProfile().properties().replaceValues(ProfileProperty.TEXTURES, Collections.singletonList(((SpongeProfileProperty)v).asProperty()));
88+
final var profile = h.getGameProfile();
89+
final var mutableProperties = HashMultimap.create(profile.properties());
90+
mutableProperties.replaceValues(ProfileProperty.TEXTURES, Collections.singletonList(((SpongeProfileProperty)v).asProperty()));
91+
92+
((PlayerBridge) h).bridge$setGameProfile(new GameProfile(profile.id(), profile.name(), new PropertyMap(mutableProperties)));
8593
ServerPlayerData.resendProfile(h);
8694
})
8795
.create(Keys.SPECTATOR_TARGET)

src/main/java/org/spongepowered/common/entity/living/human/HumanEntity.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
*/
2525
package org.spongepowered.common.entity.living.human;
2626

27+
import com.google.common.collect.HashMultimap;
28+
import com.google.common.collect.Multimap;
2729
import com.mojang.authlib.GameProfile;
2830
import com.mojang.authlib.properties.Property;
31+
import com.mojang.authlib.properties.PropertyMap;
2932
import com.mojang.authlib.yggdrasil.ProfileResult;
3033
import com.mojang.serialization.DataResult;
3134
import net.kyori.adventure.text.Component;
@@ -103,6 +106,7 @@
103106
import java.util.Optional;
104107
import java.util.OptionalInt;
105108
import java.util.UUID;
109+
import java.util.function.Consumer;
106110
import java.util.stream.Stream;
107111

108112
public final class HumanEntity extends PathfinderMob implements TeamMember, RangedAttackMob {
@@ -331,7 +335,9 @@ public boolean getOrLoadSkin(final UUID minecraftAccount) {
331335
return false;
332336
}
333337

334-
this.fakeProfile.partialProfile().properties().replaceValues(ProfileProperty.TEXTURES, profile.properties().get(ProfileProperty.TEXTURES));
338+
this.modifyProperties(props -> {
339+
props.replaceValues(ProfileProperty.TEXTURES, profile.properties().get(ProfileProperty.TEXTURES));
340+
});
335341
if (this.isAliveAndInWorld()) {
336342
this.respawnOnClient();
337343
}
@@ -349,8 +355,11 @@ public boolean getOrLoadSkin(final String minecraftAccount) {
349355
return false;
350356
}
351357

352-
this.fakeProfile.partialProfile().properties().clear();
353-
this.fakeProfile.partialProfile().properties().putAll(profile.properties());
358+
this.modifyProperties(props -> {
359+
props.clear();
360+
props.putAll(profile.properties());
361+
});
362+
354363
if (this.isAliveAndInWorld()) {
355364
this.respawnOnClient();
356365
}
@@ -381,10 +390,10 @@ public SpongeProfileProperty getSkinProperty() {
381390
}
382391

383392
public void setSkinProperty(final ProfileProperty property) {
384-
this.fakeProfile.partialProfile().properties()
385-
.replaceValues(
386-
ProfileProperty.TEXTURES,
387-
Collections.singletonList(((SpongeProfileProperty) property).asProperty()));
393+
this.modifyProperties(props -> props.replaceValues(
394+
ProfileProperty.TEXTURES,
395+
Collections.singletonList(((SpongeProfileProperty) property).asProperty())
396+
));
388397

389398
if (this.isAliveAndInWorld()) {
390399
this.respawnOnClient();
@@ -403,6 +412,17 @@ private void respawnOnClient() {
403412
)));
404413
}
405414

415+
private void modifyProperties(final Consumer<Multimap<String, Property>> fn) {
416+
final var mutable = HashMultimap.create(this.fakeProfile.partialProfile().properties());
417+
fn.accept(mutable);
418+
419+
this.fakeProfile = ResolvableProfile.createResolved(new GameProfile(
420+
this.fakeProfile.partialProfile().id(),
421+
this.fakeProfile.partialProfile().name(),
422+
new PropertyMap(mutable)
423+
));
424+
}
425+
406426
/**
407427
* Can the fake profile be removed from the tab list immediately (i.e. as
408428
* soon as the human has spawned).

src/mixins/java/org/spongepowered/common/mixin/core/world/entity/player/PlayerMixin.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.spongepowered.api.world.server.ServerWorld;
5757
import org.spongepowered.asm.mixin.Final;
5858
import org.spongepowered.asm.mixin.Mixin;
59+
import org.spongepowered.asm.mixin.Mutable;
5960
import org.spongepowered.asm.mixin.Shadow;
6061
import org.spongepowered.asm.mixin.injection.At;
6162
import org.spongepowered.asm.mixin.injection.Inject;
@@ -92,7 +93,7 @@ public abstract class PlayerMixin extends AvatarMixin implements PlayerBridge, G
9293
@Shadow public float experienceProgress;
9394
@Shadow public AbstractContainerMenu containerMenu;
9495
@Shadow @Final public InventoryMenu inventoryMenu;
95-
@Shadow @Final private GameProfile gameProfile;
96+
@Shadow @Final @Mutable private GameProfile gameProfile;
9697
@Shadow public abstract boolean shadow$isSpectator();
9798
@Shadow protected abstract int shadow$getPermissionLevel();
9899
@Shadow public abstract int shadow$getXpNeededForNextLevel();
@@ -127,6 +128,11 @@ public abstract class PlayerMixin extends AvatarMixin implements PlayerBridge, G
127128
return this.gameProfile;
128129
}
129130

131+
@Override
132+
public void bridge$setGameProfile(final GameProfile profile) {
133+
this.gameProfile = profile;
134+
}
135+
130136
@Override
131137
public int bridge$getExperienceSinceLevel() {
132138
return this.totalExperience - ExperienceHolderUtil.xpAtLevel(this.experienceLevel);

src/mixins/java/org/spongepowered/common/mixin/ipforward/server/network/ServerLoginPacketListenerImplMixin_IpForward.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
package org.spongepowered.common.mixin.ipforward.server.network;
2626

2727

28+
import com.google.common.collect.HashMultimap;
2829
import com.mojang.authlib.GameProfile;
2930
import com.mojang.authlib.properties.Property;
31+
import com.mojang.authlib.properties.PropertyMap;
3032
import net.minecraft.network.Connection;
3133
import net.minecraft.network.protocol.login.ServerboundHelloPacket;
3234
import net.minecraft.server.MinecraftServer;
@@ -92,9 +94,13 @@ public abstract class ServerLoginPacketListenerImplMixin_IpForward {
9294
$$0 = new GameProfile(uuid, $$0.name());
9395

9496
if (((ConnectionBridge_IpForward) this.connection).bungeeBridge$getSpoofedProfile() != null) {
97+
final var mutableProperties = HashMultimap.create($$0.properties());
98+
9599
for (final Property property : ((ConnectionBridge_IpForward) this.connection).bungeeBridge$getSpoofedProfile()) {
96-
$$0.properties().put(property.name(), property);
100+
mutableProperties.put(property.name(), property);
97101
}
102+
103+
$$0 = new GameProfile($$0.id(), $$0.name(), new PropertyMap(mutableProperties));
98104
}
99105
}
100106

0 commit comments

Comments
 (0)