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:
Ursprung
c2a6e8ce70
Commit
f553dee3a5
@ -100,10 +100,11 @@ public class BlockRegistryPopulator {
|
||||
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()) {
|
||||
blockStates.add(NbtMap.builder()
|
||||
.putString("name", CUSTOM_PREFIX + customBlock.name())
|
||||
.putString("name", name)
|
||||
.putInt("version", stateVersion)
|
||||
.putCompound("states", NbtMap.EMPTY)
|
||||
.build());
|
||||
@ -121,7 +122,6 @@ public class BlockRegistryPopulator {
|
||||
statesBuilder.put(property.name(), property.values().get(permIndex % property.values().size()));
|
||||
permIndex /= property.values().size();
|
||||
}
|
||||
String name = CUSTOM_PREFIX + customBlock.name();
|
||||
NbtMap states = statesBuilder.build();
|
||||
|
||||
blockStates.add(NbtMap.builder()
|
||||
@ -171,17 +171,6 @@ public class BlockRegistryPopulator {
|
||||
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() {
|
||||
for (Map.Entry<ObjectIntPair<String>, BiFunction<String, NbtMapBuilder, String>> palette : BLOCK_MAPPERS.entrySet()) {
|
||||
NbtList<NbtMap> blocksTag;
|
||||
@ -203,19 +192,13 @@ public class BlockRegistryPopulator {
|
||||
if (BlockRegistries.CUSTOM_BLOCKS.get().length != 0) {
|
||||
for (CustomBlockData customBlock : BlockRegistries.CUSTOM_BLOCKS.get()) {
|
||||
customBlockProperties.add(generateBlockPropertyData(customBlock));
|
||||
generateCustomBlockStates(customBlockStates, customExtBlockStates, customBlock, stateVersion);
|
||||
generateCustomBlockStates(customBlock, customBlockStates, customExtBlockStates, stateVersion);
|
||||
}
|
||||
blockStates.addAll(customBlockStates);
|
||||
GeyserImpl.getInstance().getLogger().debug("Added " + customBlockStates.size() + " custom block states to v" + palette.getKey().valueInt() + " palette.");
|
||||
|
||||
if (palette.getKey().valueInt() > 486) {
|
||||
// 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"))));
|
||||
} else {
|
||||
// Version below 1.18.30 sort the palette alphabetically by name
|
||||
blockStates.sort(Comparator.comparing(state -> state.getString("name")));
|
||||
}
|
||||
|
||||
// The palette is sorted by the FNV1 64-bit hash of the name
|
||||
blockStates.sort((a, b) -> Long.compareUnsigned(fnv164(a.getString("name")), fnv164(b.getString("name"))));
|
||||
}
|
||||
|
||||
// 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());
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -646,6 +646,7 @@ public class ItemRegistryPopulator {
|
||||
|
||||
entries.put(identifier, new StartGamePacket.ItemEntry(identifier, (short) customProtocolId));
|
||||
customBlockItemIds.put(customBlock, customProtocolId);
|
||||
customIdMappings.put(customProtocolId, identifier);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,7 +53,6 @@ import org.geysermc.geyser.translator.text.MessageTranslator;
|
||||
import org.geysermc.geyser.util.FileUtils;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@ -180,7 +179,7 @@ public abstract class ItemTranslator {
|
||||
translateCustomItem(nbt, builder, bedrockItem);
|
||||
|
||||
if (bedrockItem == session.getItemMappings().getStoredItems().playerHead()) {
|
||||
translatePlayerHead(session, nbt, builder, bedrockItem);
|
||||
translatePlayerHead(session, nbt, builder);
|
||||
}
|
||||
|
||||
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")) {
|
||||
String skinHash = null;
|
||||
Tag skullOwner = nbt.get("SkullOwner");
|
||||
if (skullOwner instanceof StringTag) {
|
||||
if (!(nbt.get("SkullOwner") instanceof CompoundTag skullOwner)) {
|
||||
// It's a username give up d:
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
String skinHash = data.skinUrl().substring(data.skinUrl().lastIndexOf('/') + 1);
|
||||
GeyserConvertSkullInventoryEvent skullInventoryEvent = new GeyserConvertSkullInventoryEvent(skinHash);
|
||||
GeyserImpl.getInstance().getEventBus().fire(skullInventoryEvent);
|
||||
|
||||
|
@ -74,10 +74,9 @@ public class SkullBlockEntityTranslator extends BlockEntityTranslator implements
|
||||
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 {
|
||||
String texturesProperty = getTextures(tag).get();
|
||||
Vector3i blockPosition = Vector3i.from(posX, posY, posZ);
|
||||
if (texturesProperty == null) {
|
||||
session.getGeyser().getLogger().debug("Custom skull with invalid SkullOwner tag: " + blockPosition + " " + tag);
|
||||
return -1;
|
||||
|
@ -53,6 +53,7 @@ import org.geysermc.geyser.registry.BlockRegistries;
|
||||
import org.geysermc.geyser.registry.type.ItemMapping;
|
||||
import org.geysermc.geyser.registry.type.ItemMappings;
|
||||
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.Translator;
|
||||
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 bedrockId = session.getBlockMappings().getBedrockBlockId(javaBlockState);
|
||||
if (BlockStateValues.getSkullVariant(javaBlockState) == 3) {
|
||||
int customRuntimeId = session.getSkullCache().updateSkull(blockPos, javaBlockState);
|
||||
if (customRuntimeId != -1) {
|
||||
bedrockId = customRuntimeId;
|
||||
// The changed block was a player skull so check if a custom block was defined for this skull
|
||||
SkullCache.Skull skull = session.getSkullCache().getSkulls().get(blockPos);
|
||||
if (skull != null && skull.getCustomRuntimeId() != -1) {
|
||||
bedrockId = skull.getCustomRuntimeId();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,7 +66,7 @@ public class JavaBlockEntityDataTranslator extends PacketTranslator<ClientboundB
|
||||
// Check for custom skulls.
|
||||
boolean customSkull = false;
|
||||
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) {
|
||||
customSkull = true;
|
||||
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
|
||||
|
@ -274,7 +274,7 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
|
||||
|
||||
// Check for custom skulls
|
||||
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) {
|
||||
int bedrockSectionY = (y >> 4) - (bedrockDimension.minY() >> 4);
|
||||
GeyserChunkSection bedrockSection = sections[bedrockSectionY];
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren