Skip to content

Commit

Permalink
Resolve text components for dummy players
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Nov 16, 2024
1 parent c41046f commit 9625363
Showing 1 changed file with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.client.resources.PlayerSkin;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.world.entity.Display;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -107,10 +111,14 @@ public void readAdditionalSaveData(CompoundTag compound) {
super.readAdditionalSaveData(compound);

if (compound.contains("name_prefix", Tag.TAG_STRING)) {
this.entityData.set(PREFIX, Optional.ofNullable(Component.Serializer.fromJson(compound.getString("name_prefix"), registryAccess())));
entityData.set(PREFIX, parseAndResolveComponent(compound.getString("name_prefix")));
} else {
entityData.set(PREFIX, Optional.empty());
}
if (compound.contains("name_suffix", Tag.TAG_STRING)) {
this.entityData.set(SUFFIX, Optional.ofNullable(Component.Serializer.fromJson(compound.getString("name_suffix"), registryAccess())));
entityData.set(SUFFIX, parseAndResolveComponent(compound.getString("name_suffix")));
} else {
entityData.set(SUFFIX, Optional.empty());
}

if (compound.contains("profile")) {
Expand All @@ -122,6 +130,22 @@ public void readAdditionalSaveData(CompoundTag compound) {
}
}

private Optional<Component> parseAndResolveComponent(String json) {
try {
Component component = Component.Serializer.fromJson(json, registryAccess());
if (component != null) {
CommandSourceStack source = createCommandSourceStack().withPermission(Commands.LEVEL_GAMEMASTERS);
Component resolvedComponent = ComponentUtils.updateForEntity(source, component, this, 0);
return Optional.of(resolvedComponent);
} else {
return Optional.of(Component.empty());
}
} catch (Exception e) {
LOGGER.warn("Failed to parse dummy entity text {}", json, e);
}
return Optional.empty();
}

public void setAndFillProfile(ResolvableProfile profile) {
// Only update the profile (and thus the texture) if it has changed in some way
// Avoids unnecessary texture reloads on the client when changing pose/name
Expand Down

0 comments on commit 9625363

Please sign in to comment.