mirror of
https://github.com/GeyserMC/Geyser.git
synced 2025-12-19 14:59:27 +00:00
Clean up some things related to item translation and UUID parsing
This commit is contained in:
@@ -73,9 +73,9 @@ import java.util.function.Function;
|
|||||||
* <p>Be sure to update this class for Java updates!</p>
|
* <p>Be sure to update this class for Java updates!</p>
|
||||||
*/
|
*/
|
||||||
// Lots of unchecked casting happens here. It should all be handled properly.
|
// Lots of unchecked casting happens here. It should all be handled properly.
|
||||||
// TODO only log somethings once (like was done in vault translator)
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public class ItemStackParser {
|
// TODO only log some things once (like was done in vault translator)
|
||||||
|
public final class ItemStackParser {
|
||||||
private static final Map<DataComponentType<?>, DataComponentParser<?, ?>> PARSERS = new Reference2ObjectOpenHashMap<>();
|
private static final Map<DataComponentType<?>, DataComponentParser<?, ?>> PARSERS = new Reference2ObjectOpenHashMap<>();
|
||||||
|
|
||||||
// We need the rawClass parameter here because the Raw type can't be inferred from the parser alone
|
// We need the rawClass parameter here because the Raw type can't be inferred from the parser alone
|
||||||
@@ -256,13 +256,15 @@ public class ItemStackParser {
|
|||||||
* <ul>
|
* <ul>
|
||||||
* <li>{@link ItemStackParser#parseItemStack(GeyserSession, NbtMap)}</li>
|
* <li>{@link ItemStackParser#parseItemStack(GeyserSession, NbtMap)}</li>
|
||||||
* <li>{@link ItemTranslator#translateToBedrock(GeyserSession, ItemStack)}</li>
|
* <li>{@link ItemTranslator#translateToBedrock(GeyserSession, ItemStack)}</li>
|
||||||
* <li>{@link BedrockItemBuilder#itemDataToNbt(ItemData)}</li>
|
* <li>{@link BedrockItemBuilder#createItemNbt(ItemData)}</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
public static NbtMapBuilder javaItemStackToBedrock(GeyserSession session, @Nullable NbtMap map) {
|
public static NbtMapBuilder javaItemStackToBedrock(GeyserSession session, @Nullable NbtMap map) {
|
||||||
return BedrockItemBuilder.itemDataToNbt(ItemTranslator.translateToBedrock(session, parseItemStack(session, map)));
|
return BedrockItemBuilder.createItemNbt(ItemTranslator.translateToBedrock(session, parseItemStack(session, map)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ItemStackParser() {}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
private interface DataComponentParser<Raw, Parsed> {
|
private interface DataComponentParser<Raw, Parsed> {
|
||||||
|
|
||||||
|
|||||||
@@ -52,11 +52,8 @@ public class CrossbowItem extends Item {
|
|||||||
if (chargedProjectiles != null && !chargedProjectiles.isEmpty()) {
|
if (chargedProjectiles != null && !chargedProjectiles.isEmpty()) {
|
||||||
ItemStack javaProjectile = chargedProjectiles.get(0);
|
ItemStack javaProjectile = chargedProjectiles.get(0);
|
||||||
|
|
||||||
ItemMapping projectileMapping = session.getItemMappings().getMapping(javaProjectile.getId());
|
|
||||||
ItemData itemData = ItemTranslator.translateToBedrock(session, javaProjectile);
|
ItemData itemData = ItemTranslator.translateToBedrock(session, javaProjectile);
|
||||||
|
NbtMapBuilder newProjectile = BedrockItemBuilder.createItemNbt(itemData);
|
||||||
NbtMapBuilder newProjectile = BedrockItemBuilder.createItemNbt(projectileMapping, itemData.getCount(), itemData.getDamage());
|
|
||||||
|
|
||||||
builder.putCompound("chargedItem", newProjectile.build());
|
builder.putCompound("chargedItem", newProjectile.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -146,7 +146,7 @@ public final class BedrockItemBuilder {
|
|||||||
/**
|
/**
|
||||||
* Creates item NBT to nest within NBT with name, count, damage, and tag set.
|
* Creates item NBT to nest within NBT with name, count, damage, and tag set.
|
||||||
*/
|
*/
|
||||||
public static NbtMapBuilder itemDataToNbt(ItemData data) {
|
public static NbtMapBuilder createItemNbt(ItemData data) {
|
||||||
NbtMapBuilder builder = BedrockItemBuilder.createItemNbt(data.getDefinition().getIdentifier(), data.getCount(), data.getDamage());
|
NbtMapBuilder builder = BedrockItemBuilder.createItemNbt(data.getDefinition().getIdentifier(), data.getCount(), data.getDamage());
|
||||||
if (data.getTag() != null) {
|
if (data.getTag() != null) {
|
||||||
builder.putCompound("tag", data.getTag());
|
builder.putCompound("tag", data.getTag());
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import org.geysermc.geyser.level.block.type.BlockState;
|
|||||||
import org.geysermc.geyser.session.GeyserSession;
|
import org.geysermc.geyser.session.GeyserSession;
|
||||||
import org.geysermc.geyser.session.cache.SkullCache;
|
import org.geysermc.geyser.session.cache.SkullCache;
|
||||||
import org.geysermc.geyser.skin.SkinManager;
|
import org.geysermc.geyser.skin.SkinManager;
|
||||||
|
import org.geysermc.geyser.util.EntityUtils;
|
||||||
import org.geysermc.mcprotocollib.auth.GameProfile;
|
import org.geysermc.mcprotocollib.auth.GameProfile;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.ResolvableProfile;
|
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.ResolvableProfile;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityType;
|
import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityType;
|
||||||
@@ -62,15 +63,6 @@ public class SkullBlockEntityTranslator extends BlockEntityTranslator implements
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static UUID parseUUID(int[] uuid) {
|
|
||||||
if (uuid != null && uuid.length == 4) {
|
|
||||||
// thank u viaversion
|
|
||||||
return new UUID((long) uuid[0] << 32 | ((long) uuid[1] & 0xFFFFFFFFL),
|
|
||||||
(long) uuid[2] << 32 | ((long) uuid[3] & 0xFFFFFFFFL));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<GameProfile.Property> parseProperties(List<NbtMap> properties) {
|
private static List<GameProfile.Property> parseProperties(List<NbtMap> properties) {
|
||||||
if (properties == null) {
|
if (properties == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -86,7 +78,7 @@ public class SkullBlockEntityTranslator extends BlockEntityTranslator implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ResolvableProfile parseResolvableProfile(NbtMap profile) {
|
public static ResolvableProfile parseResolvableProfile(NbtMap profile) {
|
||||||
UUID uuid = parseUUID(profile.getIntArray("id", null));
|
UUID uuid = EntityUtils.uuidFromIntArray(profile.getIntArray("id", null));
|
||||||
String name = profile.getString("name", null);
|
String name = profile.getString("name", null);
|
||||||
List<GameProfile.Property> properties = parseProperties(profile.getList("properties", NbtType.COMPOUND, null));
|
List<GameProfile.Property> properties = parseProperties(profile.getList("properties", NbtType.COMPOUND, null));
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import org.geysermc.geyser.entity.type.player.PlayerEntity;
|
|||||||
import org.geysermc.geyser.item.parser.ItemStackParser;
|
import org.geysermc.geyser.item.parser.ItemStackParser;
|
||||||
import org.geysermc.geyser.level.block.type.BlockState;
|
import org.geysermc.geyser.level.block.type.BlockState;
|
||||||
import org.geysermc.geyser.session.GeyserSession;
|
import org.geysermc.geyser.session.GeyserSession;
|
||||||
|
import org.geysermc.geyser.util.EntityUtils;
|
||||||
import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityType;
|
import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityType;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -61,7 +62,7 @@ public class VaultBlockEntityTranslator extends BlockEntityTranslator {
|
|||||||
List<int[]> connectedPlayers = sharedData.getList("connected_players", NbtType.INT_ARRAY);
|
List<int[]> connectedPlayers = sharedData.getList("connected_players", NbtType.INT_ARRAY);
|
||||||
LongList bedrockPlayers = new LongArrayList(connectedPlayers.size());
|
LongList bedrockPlayers = new LongArrayList(connectedPlayers.size());
|
||||||
for (int[] player : connectedPlayers) {
|
for (int[] player : connectedPlayers) {
|
||||||
UUID uuid = uuidFromIntArray(player);
|
UUID uuid = EntityUtils.uuidFromIntArray(player);
|
||||||
if (uuid.equals(session.getPlayerEntity().getUuid())) {
|
if (uuid.equals(session.getPlayerEntity().getUuid())) {
|
||||||
bedrockPlayers.add(session.getPlayerEntity().getGeyserId());
|
bedrockPlayers.add(session.getPlayerEntity().getGeyserId());
|
||||||
} else {
|
} else {
|
||||||
@@ -77,10 +78,4 @@ public class VaultBlockEntityTranslator extends BlockEntityTranslator {
|
|||||||
// if it is not sent over the network
|
// if it is not sent over the network
|
||||||
bedrockNbt.putFloat("connected_particle_range", (float) sharedData.getDouble("connected_particles_range", 4.5d));
|
bedrockNbt.putFloat("connected_particle_range", (float) sharedData.getDouble("connected_particles_range", 4.5d));
|
||||||
}
|
}
|
||||||
|
|
||||||
// From ViaVersion! thank u!!
|
|
||||||
// TODO code dup
|
|
||||||
private static UUID uuidFromIntArray(int[] parts) {
|
|
||||||
return new UUID((long) parts[0] << 32 | (parts[1] & 0xFFFFFFFFL), (long) parts[2] << 32 | (parts[3] & 0xFFFFFFFFL));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ import org.geysermc.mcprotocollib.protocol.data.game.entity.type.EntityType;
|
|||||||
import org.geysermc.mcprotocollib.protocol.data.game.item.component.Equippable;
|
import org.geysermc.mcprotocollib.protocol.data.game.item.component.Equippable;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public final class EntityUtils {
|
public final class EntityUtils {
|
||||||
/**
|
/**
|
||||||
@@ -361,6 +362,16 @@ public final class EntityUtils {
|
|||||||
return session.getTagCache().is(holderSet, entity);
|
return session.getTagCache().is(holderSet, entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// From ViaVersion! thank u!!
|
||||||
|
public static UUID uuidFromIntArray(int[] uuid) {
|
||||||
|
if (uuid != null && uuid.length == 4) {
|
||||||
|
// thank u viaversion
|
||||||
|
return new UUID((long) uuid[0] << 32 | ((long) uuid[1] & 0xFFFFFFFFL),
|
||||||
|
(long) uuid[2] << 32 | ((long) uuid[3] & 0xFFFFFFFFL));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private EntityUtils() {
|
private EntityUtils() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user