Skip to content

Commit

Permalink
Fix custom skulls (#5338)
Browse files Browse the repository at this point in the history
  • Loading branch information
onebeastchris authored Feb 12, 2025
1 parent b0bf867 commit d5b5712
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.geysermc.geyser.item.components;

import lombok.Getter;
import org.checkerframework.checker.nullness.qual.NonNull;

@Getter
public enum Rarity {
Expand All @@ -44,8 +45,7 @@ public enum Rarity {

private static final Rarity[] VALUES = values();

public static Rarity fromId(int id) {
public static @NonNull Rarity fromId(Integer id) {
return VALUES.length > id ? VALUES[id] : VALUES[0];
}

}
5 changes: 0 additions & 5 deletions core/src/main/java/org/geysermc/geyser/item/type/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.inventory.item.BedrockEnchantment;
import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.item.components.Rarity;
import org.geysermc.geyser.item.enchantment.Enchantment;
import org.geysermc.geyser.level.block.type.Block;
import org.geysermc.geyser.registry.Registries;
Expand Down Expand Up @@ -95,10 +94,6 @@ public int defaultMaxStackSize() {
return baseComponents.getOrDefault(DataComponentType.MAX_STACK_SIZE, 1);
}

public Rarity defaultRarity() {
return Rarity.fromId(baseComponents.getOrDefault(DataComponentType.RARITY, 0));
}

/**
* Returns an unmodifiable {@link DataComponents} view containing known data components.
* Optionally, additional components can be provided to replace (or add to)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@

package org.geysermc.geyser.item.type;

import org.geysermc.mcprotocollib.auth.GameProfile;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.item.components.Rarity;
import org.geysermc.geyser.level.block.type.Block;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.text.ChatColor;
import org.geysermc.geyser.text.MinecraftLocale;
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
import org.geysermc.mcprotocollib.auth.GameProfile;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;

Expand All @@ -44,24 +45,21 @@ public PlayerHeadItem(Builder builder, Block block, Block... otherBlocks) {
public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) {
super.translateComponentsToBedrock(session, components, builder);

// TODO verify
// Also - ChatColor.YELLOW + ChatColor.ITALIC + MessageTranslator.convertMessageLenient(nameTag.getValue(), session.locale())) this code existed if a custom name was already present.
// But I think we would always overwrite that because translateDisplayProperties runs after this method.
String customName = builder.getCustomName();
if (customName == null) {
GameProfile profile = components.get(DataComponentType.PROFILE);
if (profile != null) {
String name = profile.getName();
if (name != null) {
// Add correct name of player skull
String displayName = ChatColor.RESET + ChatColor.YELLOW +
MinecraftLocale.getLocaleString("block.minecraft.player_head.named", session.locale()).replace("%s", name);
builder.setCustomName(displayName);
} else {
// No name found so default to "Player Head"
builder.setCustomName(ChatColor.RESET + ChatColor.YELLOW +
MinecraftLocale.getLocaleString("block.minecraft.player_head", session.locale()));
}
// Use the correct color, determined by the rarity of the item
char rarity = Rarity.fromId(components.get(DataComponentType.RARITY)).getColor();

GameProfile profile = components.get(DataComponentType.PROFILE);
if (profile != null) {
String name = profile.getName();
if (name != null) {
// Add correct name of player skull
String displayName = ChatColor.RESET + ChatColor.ESCAPE + rarity +
MinecraftLocale.getLocaleString("block.minecraft.player_head.named", session.locale()).replace("%s", name);
builder.setCustomName(displayName);
} else {
// No name found so default to "Player Head"
builder.setCustomName(ChatColor.RESET + ChatColor.ESCAPE + rarity +
MinecraftLocale.getLocaleString("block.minecraft.player_head", session.locale()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
import org.geysermc.geyser.inventory.item.StoredItemMappings;
import org.geysermc.geyser.item.GeyserCustomMappingData;
import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.item.components.Rarity;
import org.geysermc.geyser.item.type.BlockItem;
import org.geysermc.geyser.item.type.Item;
import org.geysermc.geyser.level.block.property.Properties;
Expand Down Expand Up @@ -491,9 +490,8 @@ public static void populate() {
mappingBuilder = mappingBuilder.toolType(mappingItem.getToolType().intern());
}

if (javaOnlyItems.contains(javaItem) || javaItem.defaultRarity() != Rarity.COMMON) {
if (javaOnlyItems.contains(javaItem)) {
// These items don't exist on Bedrock, so set up a variable that indicates they should have custom names
// Or, ensure that we are translating these at all times to account for rarity colouring
mappingBuilder = mappingBuilder.translationString((javaItem instanceof BlockItem ? "block." : "item.") + entry.getKey().replace(":", "."));
GeyserImpl.getInstance().getLogger().debug("Adding " + entry.getKey() + " as an item that needs to be translated.");
}
Expand Down Expand Up @@ -665,8 +663,7 @@ public static void populate() {
int customProtocolId = nextFreeBedrockId++;
String identifier = customBlock.identifier();

// TODO verify
final ItemDefinition definition = new SimpleItemDefinition(identifier, customProtocolId, ItemVersion.DATA_DRIVEN, false, null);
final ItemDefinition definition = new SimpleItemDefinition(identifier, customProtocolId, ItemVersion.NONE, false, null);
registry.put(customProtocolId, definition);
customBlockItemDefinitions.put(customBlock, definition);
customIdMappings.put(customProtocolId, identifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public class SkullCache {

private Vector3f lastPlayerPosition;

private long lastCleanup = System.currentTimeMillis();

public SkullCache(GeyserSession session) {
this.session = session;
this.maxVisibleSkulls = session.getGeyser().getConfig().getMaxVisibleCustomSkulls();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static ItemData translateToBedrock(GeyserSession session, ItemStack stack
// Translate item-specific components
javaItem.translateComponentsToBedrock(session, components, nbtBuilder);

Rarity rarity = Rarity.fromId(components.getOrDefault(DataComponentType.RARITY, 0));
Rarity rarity = Rarity.fromId(components.get(DataComponentType.RARITY));
String customName = getCustomName(session, customComponents, bedrockItem, rarity.getColor(), false, false);
if (customName != null) {
PotionContents potionContents = components.get(DataComponentType.POTION_CONTENTS);
Expand Down Expand Up @@ -611,6 +611,7 @@ private static void translateCustomBlock(CustomBlockData customBlockData, Geyser
}

if (textures == null || textures.isEmpty()) {
// TODO the java client looks up the texture properties here and updates the item
return null;
}

Expand Down

0 comments on commit d5b5712

Please sign in to comment.