3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-11-19 22:40:18 +01:00

Attempt to clean up some code

Dieser Commit ist enthalten in:
davchoo 2022-07-07 05:06:28 -04:00
Ursprung c2a6e8ce70
Commit f553dee3a5
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: A0168C8E45799B7D
7 geänderte Dateien mit 35 neuen und 59 gelöschten Zeilen

Datei anzeigen

@ -100,10 +100,11 @@ public class BlockRegistryPopulator {
GeyserImpl.getInstance().getLogger().debug("Registered " + customBlocks.size() + " custom blocks."); GeyserImpl.getInstance().getLogger().debug("Registered " + customBlocks.size() + " custom blocks.");
} }
private static void generateCustomBlockStates(List<NbtMap> blockStates, List<CustomBlockState> customExtBlockStates, CustomBlockData customBlock, int stateVersion) { private static void generateCustomBlockStates(CustomBlockData customBlock, List<NbtMap> blockStates, List<CustomBlockState> customExtBlockStates, int stateVersion) {
String name = CUSTOM_PREFIX + customBlock.name();
if (customBlock.properties().isEmpty()) { if (customBlock.properties().isEmpty()) {
blockStates.add(NbtMap.builder() blockStates.add(NbtMap.builder()
.putString("name", CUSTOM_PREFIX + customBlock.name()) .putString("name", name)
.putInt("version", stateVersion) .putInt("version", stateVersion)
.putCompound("states", NbtMap.EMPTY) .putCompound("states", NbtMap.EMPTY)
.build()); .build());
@ -121,7 +122,6 @@ public class BlockRegistryPopulator {
statesBuilder.put(property.name(), property.values().get(permIndex % property.values().size())); statesBuilder.put(property.name(), property.values().get(permIndex % property.values().size()));
permIndex /= property.values().size(); permIndex /= property.values().size();
} }
String name = CUSTOM_PREFIX + customBlock.name();
NbtMap states = statesBuilder.build(); NbtMap states = statesBuilder.build();
blockStates.add(NbtMap.builder() blockStates.add(NbtMap.builder()
@ -171,17 +171,6 @@ public class BlockRegistryPopulator {
return new BlockPropertyData(CUSTOM_PREFIX + customBlock.name(), propertyTag); return new BlockPropertyData(CUSTOM_PREFIX + customBlock.name(), propertyTag);
} }
private static final long FNV1_64_OFFSET_BASIS = 0xcbf29ce484222325L;
private static final long FNV1_64_PRIME = 1099511628211L;
private static long fnv164(String str) {
long hash = FNV1_64_OFFSET_BASIS;
for (byte b : str.getBytes(StandardCharsets.UTF_8)) {
hash *= FNV1_64_PRIME;
hash ^= b;
}
return hash;
}
private static void registerBedrockBlocks() { private static void registerBedrockBlocks() {
for (Map.Entry<ObjectIntPair<String>, BiFunction<String, NbtMapBuilder, String>> palette : BLOCK_MAPPERS.entrySet()) { for (Map.Entry<ObjectIntPair<String>, BiFunction<String, NbtMapBuilder, String>> palette : BLOCK_MAPPERS.entrySet()) {
NbtList<NbtMap> blocksTag; NbtList<NbtMap> blocksTag;
@ -203,19 +192,13 @@ public class BlockRegistryPopulator {
if (BlockRegistries.CUSTOM_BLOCKS.get().length != 0) { if (BlockRegistries.CUSTOM_BLOCKS.get().length != 0) {
for (CustomBlockData customBlock : BlockRegistries.CUSTOM_BLOCKS.get()) { for (CustomBlockData customBlock : BlockRegistries.CUSTOM_BLOCKS.get()) {
customBlockProperties.add(generateBlockPropertyData(customBlock)); customBlockProperties.add(generateBlockPropertyData(customBlock));
generateCustomBlockStates(customBlockStates, customExtBlockStates, customBlock, stateVersion); generateCustomBlockStates(customBlock, customBlockStates, customExtBlockStates, stateVersion);
} }
blockStates.addAll(customBlockStates); blockStates.addAll(customBlockStates);
GeyserImpl.getInstance().getLogger().debug("Added " + customBlockStates.size() + " custom block states to v" + palette.getKey().valueInt() + " palette."); GeyserImpl.getInstance().getLogger().debug("Added " + customBlockStates.size() + " custom block states to v" + palette.getKey().valueInt() + " palette.");
if (palette.getKey().valueInt() > 486) { // The palette is sorted by the FNV1 64-bit hash of the name
// Version 1.18.30 and above sort the palette by the FNV1 64 bit hash of the name blockStates.sort((a, b) -> Long.compareUnsigned(fnv164(a.getString("name")), fnv164(b.getString("name"))));
blockStates.sort((a, b) -> Long.compareUnsigned(fnv164(a.getString("name")), fnv164(b.getString("name"))));
} else {
// Version below 1.18.30 sort the palette alphabetically by name
blockStates.sort(Comparator.comparing(state -> state.getString("name")));
}
} }
// New since 1.16.100 - find the block runtime ID by the order given to us in the block palette, // New since 1.16.100 - find the block runtime ID by the order given to us in the block palette,
@ -602,4 +585,16 @@ public class BlockRegistryPopulator {
tagBuilder.put("states", statesBuilder.build()); tagBuilder.put("states", statesBuilder.build());
return tagBuilder.build(); return tagBuilder.build();
} }
private static final long FNV1_64_OFFSET_BASIS = 0xcbf29ce484222325L;
private static final long FNV1_64_PRIME = 1099511628211L;
private static long fnv164(String str) {
long hash = FNV1_64_OFFSET_BASIS;
for (byte b : str.getBytes(StandardCharsets.UTF_8)) {
hash *= FNV1_64_PRIME;
hash ^= b;
}
return hash;
}
} }

Datei anzeigen

@ -646,6 +646,7 @@ public class ItemRegistryPopulator {
entries.put(identifier, new StartGamePacket.ItemEntry(identifier, (short) customProtocolId)); entries.put(identifier, new StartGamePacket.ItemEntry(identifier, (short) customProtocolId));
customBlockItemIds.put(customBlock, customProtocolId); customBlockItemIds.put(customBlock, customProtocolId);
customIdMappings.put(customProtocolId, identifier);
} }
} }

Datei anzeigen

@ -53,7 +53,6 @@ import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.geyser.util.FileUtils; import org.geysermc.geyser.util.FileUtils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -180,7 +179,7 @@ public abstract class ItemTranslator {
translateCustomItem(nbt, builder, bedrockItem); translateCustomItem(nbt, builder, bedrockItem);
if (bedrockItem == session.getItemMappings().getStoredItems().playerHead()) { if (bedrockItem == session.getItemMappings().getStoredItems().playerHead()) {
translatePlayerHead(session, nbt, builder, bedrockItem); translatePlayerHead(session, nbt, builder);
} }
if (nbt != null) { if (nbt != null) {
@ -551,39 +550,19 @@ public abstract class ItemTranslator {
} }
} }
private static void translatePlayerHead(GeyserSession session, CompoundTag nbt, ItemData.Builder builder, ItemMapping mapping) { private static void translatePlayerHead(GeyserSession session, CompoundTag nbt, ItemData.Builder builder) {
if (nbt != null && nbt.contains("SkullOwner")) { if (nbt != null && nbt.contains("SkullOwner")) {
String skinHash = null; if (!(nbt.get("SkullOwner") instanceof CompoundTag skullOwner)) {
Tag skullOwner = nbt.get("SkullOwner");
if (skullOwner instanceof StringTag) {
// It's a username give up d: // It's a username give up d:
return; return;
} else if (skullOwner instanceof CompoundTag) {
Tag properties = ((CompoundTag) skullOwner).get("Properties");
if (properties instanceof CompoundTag) {
Tag textures = ((CompoundTag) properties).get("textures");
if (textures instanceof ListTag) {
List<Tag> textureTags = ((ListTag) textures).getValue();
if (!textureTags.isEmpty()) {
Tag skinTag = textureTags.get(0);
if (skinTag instanceof CompoundTag) {
Tag valueTag = ((CompoundTag) skinTag).get("Value");
String encodedJson = (String) valueTag.getValue();
try {
SkinManager.GameProfileData data = SkinManager.GameProfileData.loadFromJson(encodedJson);
skinHash = data.skinUrl().substring(data.skinUrl().lastIndexOf('/') + 1);
} catch (IOException e) {
session.getGeyser().getLogger().debug("Not sure how to handle skull head item display. " + nbt + " " + e.getMessage());
return;
}
}
}
}
}
} }
if (skinHash == null) { SkinManager.GameProfileData data = SkinManager.GameProfileData.from(skullOwner);
if (data == null) {
session.getGeyser().getLogger().debug("Not sure how to handle skull head item display. " + nbt); session.getGeyser().getLogger().debug("Not sure how to handle skull head item display. " + nbt);
return;
} }
String skinHash = data.skinUrl().substring(data.skinUrl().lastIndexOf('/') + 1);
GeyserConvertSkullInventoryEvent skullInventoryEvent = new GeyserConvertSkullInventoryEvent(skinHash); GeyserConvertSkullInventoryEvent skullInventoryEvent = new GeyserConvertSkullInventoryEvent(skinHash);
GeyserImpl.getInstance().getEventBus().fire(skullInventoryEvent); GeyserImpl.getInstance().getEventBus().fire(skullInventoryEvent);

Datei anzeigen

@ -74,10 +74,9 @@ public class SkullBlockEntityTranslator extends BlockEntityTranslator implements
return CompletableFuture.completedFuture(null); return CompletableFuture.completedFuture(null);
} }
public static int translateSkull(GeyserSession session, CompoundTag tag, int posX, int posY, int posZ, int blockState) { public static int translateSkull(GeyserSession session, CompoundTag tag, Vector3i blockPosition, int blockState) {
try { try {
String texturesProperty = getTextures(tag).get(); String texturesProperty = getTextures(tag).get();
Vector3i blockPosition = Vector3i.from(posX, posY, posZ);
if (texturesProperty == null) { if (texturesProperty == null) {
session.getGeyser().getLogger().debug("Custom skull with invalid SkullOwner tag: " + blockPosition + " " + tag); session.getGeyser().getLogger().debug("Custom skull with invalid SkullOwner tag: " + blockPosition + " " + tag);
return -1; return -1;

Datei anzeigen

@ -53,6 +53,7 @@ import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.registry.type.ItemMapping; import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings; import org.geysermc.geyser.registry.type.ItemMappings;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.SkullCache;
import org.geysermc.geyser.translator.protocol.PacketTranslator; import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator; import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.util.BlockUtils; import org.geysermc.geyser.util.BlockUtils;
@ -507,9 +508,10 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
int javaBlockState = session.getGeyser().getWorldManager().getBlockAt(session, blockPos); int javaBlockState = session.getGeyser().getWorldManager().getBlockAt(session, blockPos);
int bedrockId = session.getBlockMappings().getBedrockBlockId(javaBlockState); int bedrockId = session.getBlockMappings().getBedrockBlockId(javaBlockState);
if (BlockStateValues.getSkullVariant(javaBlockState) == 3) { if (BlockStateValues.getSkullVariant(javaBlockState) == 3) {
int customRuntimeId = session.getSkullCache().updateSkull(blockPos, javaBlockState); // The changed block was a player skull so check if a custom block was defined for this skull
if (customRuntimeId != -1) { SkullCache.Skull skull = session.getSkullCache().getSkulls().get(blockPos);
bedrockId = customRuntimeId; if (skull != null && skull.getCustomRuntimeId() != -1) {
bedrockId = skull.getCustomRuntimeId();
} }
} }

Datei anzeigen

@ -66,7 +66,7 @@ public class JavaBlockEntityDataTranslator extends PacketTranslator<ClientboundB
// Check for custom skulls. // Check for custom skulls.
boolean customSkull = false; boolean customSkull = false;
if (session.getPreferencesCache().showCustomSkulls() && packet.getNbt() != null && packet.getNbt().contains("SkullOwner")) { if (session.getPreferencesCache().showCustomSkulls() && packet.getNbt() != null && packet.getNbt().contains("SkullOwner")) {
int runtimeId = SkullBlockEntityTranslator.translateSkull(session, packet.getNbt(), position.getX(), position.getY(), position.getZ(), blockState); int runtimeId = SkullBlockEntityTranslator.translateSkull(session, packet.getNbt(), position, blockState);
if (runtimeId != -1) { if (runtimeId != -1) {
customSkull = true; customSkull = true;
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket(); UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();

Datei anzeigen

@ -274,7 +274,7 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
// Check for custom skulls // Check for custom skulls
if (session.getPreferencesCache().showCustomSkulls() && type == BlockEntityType.SKULL && tag != null && tag.contains("SkullOwner")) { if (session.getPreferencesCache().showCustomSkulls() && type == BlockEntityType.SKULL && tag != null && tag.contains("SkullOwner")) {
int runtimeId = SkullBlockEntityTranslator.translateSkull(session, tag, x + chunkBlockX, y, z + chunkBlockZ, blockState); int runtimeId = SkullBlockEntityTranslator.translateSkull(session, tag, Vector3i.from(x + chunkBlockX, y, z + chunkBlockZ), blockState);
if (runtimeId != -1) { if (runtimeId != -1) {
int bedrockSectionY = (y >> 4) - (bedrockDimension.minY() >> 4); int bedrockSectionY = (y >> 4) - (bedrockDimension.minY() >> 4);
GeyserChunkSection bedrockSection = sections[bedrockSectionY]; GeyserChunkSection bedrockSection = sections[bedrockSectionY];