mirror of
https://github.com/GeyserMC/Geyser.git
synced 2025-04-17 19:12:14 +02:00
Merge 7c029886cd
into 23a462d60c
This commit is contained in:
commit
e8440c66c3
2 changed files with 89 additions and 15 deletions
core/src/main/java/org/geysermc/geyser/registry/populator
|
@ -45,7 +45,6 @@ import org.geysermc.geyser.registry.type.GeyserMappingItem;
|
|||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
@ -59,7 +58,8 @@ public class CreativeItemRegistryPopulator {
|
|||
(identifier, data) -> identifier.equals("minecraft:empty_map") && data == 2
|
||||
);
|
||||
|
||||
static List<CreativeItemGroup> readCreativeItemGroups(ItemRegistryPopulator.PaletteVersion palette, List<CreativeItemData> creativeItemData) {
|
||||
static void readCreativeItemGroups(ItemRegistryPopulator.PaletteVersion palette, List<CreativeItemData> creativeItemData,
|
||||
List<CreativeItemGroup> creativeItemGroups, Map<String, Integer> groupIndexMap, Map<CreativeItemCategory, Integer> lastCreativeItemGroup) {
|
||||
GeyserBootstrap bootstrap = GeyserImpl.getInstance().getBootstrap();
|
||||
|
||||
JsonNode creativeItemEntries;
|
||||
|
@ -69,7 +69,6 @@ public class CreativeItemRegistryPopulator {
|
|||
throw new AssertionError("Unable to load creative item groups", e);
|
||||
}
|
||||
|
||||
List<CreativeItemGroup> creativeItemGroups = new ArrayList<>();
|
||||
for (JsonNode creativeItemEntry : creativeItemEntries) {
|
||||
CreativeItemCategory category = CreativeItemCategory.valueOf(creativeItemEntry.get("category").asText().toUpperCase(Locale.ROOT));
|
||||
String name = creativeItemEntry.get("name").asText();
|
||||
|
@ -88,10 +87,29 @@ public class CreativeItemRegistryPopulator {
|
|||
.orElseThrow();
|
||||
}
|
||||
|
||||
if (!name.isEmpty()) {
|
||||
groupIndexMap.put(name, creativeItemGroups.size());
|
||||
}
|
||||
|
||||
creativeItemGroups.add(new CreativeItemGroup(category, name, itemData));
|
||||
}
|
||||
|
||||
return creativeItemGroups;
|
||||
CreativeItemCategory category = null;
|
||||
for (int i = 0; i < creativeItemGroups.size(); i++) {
|
||||
CreativeItemGroup creativeItemGroup = creativeItemGroups.get(i);
|
||||
if (category == null) {
|
||||
category = creativeItemGroup.getCategory();
|
||||
}
|
||||
|
||||
if (creativeItemGroup.getCategory() != category) {
|
||||
lastCreativeItemGroup.put(category, i--);
|
||||
category = creativeItemGroup.getCategory();
|
||||
}
|
||||
|
||||
if (i == creativeItemGroups.size() - 1) {
|
||||
lastCreativeItemGroup.put(creativeItemGroup.getCategory(), i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void populate(ItemRegistryPopulator.PaletteVersion palette, Map<String, ItemDefinition> definitions, Map<String, GeyserMappingItem> items, BiConsumer<ItemData.Builder, Integer> itemConsumer) {
|
||||
|
|
|
@ -51,6 +51,7 @@ import org.cloudburstmc.protocol.bedrock.codec.v786.Bedrock_v786;
|
|||
import org.cloudburstmc.protocol.bedrock.data.definitions.BlockDefinition;
|
||||
import org.cloudburstmc.protocol.bedrock.data.definitions.ItemDefinition;
|
||||
import org.cloudburstmc.protocol.bedrock.data.definitions.SimpleItemDefinition;
|
||||
import org.cloudburstmc.protocol.bedrock.data.inventory.CreativeItemCategory;
|
||||
import org.cloudburstmc.protocol.bedrock.data.inventory.CreativeItemData;
|
||||
import org.cloudburstmc.protocol.bedrock.data.inventory.CreativeItemGroup;
|
||||
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
|
||||
|
@ -244,11 +245,11 @@ public class ItemRegistryPopulator {
|
|||
}
|
||||
});
|
||||
|
||||
List<CreativeItemGroup> creativeItemGroups;
|
||||
if (GameProtocol.isPreCreativeInventoryRewrite(palette.protocolVersion)) {
|
||||
creativeItemGroups = new ArrayList<>();
|
||||
} else {
|
||||
creativeItemGroups = CreativeItemRegistryPopulator.readCreativeItemGroups(palette, creativeItems);
|
||||
List<CreativeItemGroup> creativeItemGroups = new ObjectArrayList<>();
|
||||
Map<String, Integer> creativeGroupIds = new Object2IntOpenHashMap<>();
|
||||
Map<CreativeItemCategory, Integer> lastCreativeGroupIds = new Object2IntOpenHashMap<>();
|
||||
if (!GameProtocol.isPreCreativeInventoryRewrite(palette.protocolVersion)) {
|
||||
CreativeItemRegistryPopulator.readCreativeItemGroups(palette, creativeItems, creativeItemGroups, creativeGroupIds, lastCreativeGroupIds);
|
||||
}
|
||||
|
||||
BlockMappings blockMappings = BlockRegistries.BLOCKS.forVersion(palette.protocolVersion());
|
||||
|
@ -433,18 +434,26 @@ public class ItemRegistryPopulator {
|
|||
customBlockItemDefinitions.put(customBlockData, definition);
|
||||
customIdMappings.put(customProtocolId, bedrockIdentifier);
|
||||
|
||||
CreativeItemCategory category = customBlockData.creativeCategory() == null ? CreativeItemCategory.UNDEFINED :
|
||||
CreativeItemCategory.values()[customBlockData.creativeCategory().ordinal()];
|
||||
|
||||
CreativeItemData newData = new CreativeItemData(itemData.getItem().toBuilder()
|
||||
.definition(definition)
|
||||
.blockDefinition(bedrockBlock)
|
||||
.netId(itemData.getNetId())
|
||||
.count(1)
|
||||
.build(), itemData.getNetId(), 0);
|
||||
.build(), itemData.getNetId(), getCreativeIndex(
|
||||
customBlockData.creativeGroup(),
|
||||
category,
|
||||
creativeGroupIds,
|
||||
lastCreativeGroupIds, creativeItemGroups)
|
||||
);
|
||||
|
||||
creativeItems.set(j, newData);
|
||||
} else {
|
||||
CreativeItemData creativeItemData = new CreativeItemData(itemData.getItem().toBuilder()
|
||||
.blockDefinition(bedrockBlock)
|
||||
.build(), itemData.getNetId(), 0);
|
||||
.build(), itemData.getNetId(), itemData.getGroupId());
|
||||
|
||||
creativeItems.set(j, creativeItemData);
|
||||
}
|
||||
|
@ -500,7 +509,13 @@ public class ItemRegistryPopulator {
|
|||
.definition(customMapping.itemDefinition())
|
||||
.blockDefinition(null)
|
||||
.count(1)
|
||||
.build(), creativeNetId.get(), customItem.creativeCategory().getAsInt());
|
||||
.build(), creativeNetId.get(), getCreativeIndex(
|
||||
customItem.creativeGroup(),
|
||||
customItem.creativeCategory().isPresent() ? CreativeItemCategory.values()[customItem.creativeCategory().getAsInt()] : CreativeItemCategory.ITEMS,
|
||||
creativeGroupIds,
|
||||
lastCreativeGroupIds,
|
||||
creativeItemGroups)
|
||||
);
|
||||
creativeItems.add(creativeItemData);
|
||||
}
|
||||
|
||||
|
@ -586,7 +601,7 @@ public class ItemRegistryPopulator {
|
|||
.netId(creativeNetId.incrementAndGet())
|
||||
.definition(definition)
|
||||
.count(1)
|
||||
.build(), creativeNetId.get(), 99)); // todo do not hardcode!
|
||||
.build(), creativeNetId.get(), getCreativeIndex("itemGroup.name.minecart", CreativeItemCategory.ITEMS, creativeGroupIds, lastCreativeGroupIds, creativeItemGroups)));
|
||||
|
||||
// Register any completely custom items given to us
|
||||
IntSet registeredJavaIds = new IntOpenHashSet(); // Used to check for duplicate item java ids
|
||||
|
@ -616,7 +631,12 @@ public class ItemRegistryPopulator {
|
|||
.definition(registration.mapping().getBedrockDefinition())
|
||||
.netId(creativeNetId.incrementAndGet())
|
||||
.count(1)
|
||||
.build(), creativeNetId.get(), customItem.creativeCategory().getAsInt());
|
||||
.build(), creativeNetId.get(), getCreativeIndex(
|
||||
customItem.creativeGroup(),
|
||||
customItem.creativeCategory().isPresent() ? CreativeItemCategory.values()[customItem.creativeCategory().getAsInt()] : CreativeItemCategory.ITEMS,
|
||||
creativeGroupIds,
|
||||
lastCreativeGroupIds, creativeItemGroups)
|
||||
);
|
||||
|
||||
creativeItems.add(creativeItemData);
|
||||
}
|
||||
|
@ -648,12 +668,20 @@ public class ItemRegistryPopulator {
|
|||
GeyserBedrockBlock bedrockBlock = blockMappings.getCustomBlockStateDefinitions().getOrDefault(customBlock.defaultBlockState(), null);
|
||||
|
||||
if (bedrockBlock != null && customBlock.includedInCreativeInventory()) {
|
||||
CreativeItemCategory category = customBlock.creativeCategory() == null ? CreativeItemCategory.UNDEFINED :
|
||||
CreativeItemCategory.values()[customBlock.creativeCategory().ordinal()];
|
||||
|
||||
CreativeItemData creativeItemData = new CreativeItemData(ItemData.builder()
|
||||
.definition(definition)
|
||||
.blockDefinition(bedrockBlock)
|
||||
.netId(creativeNetId.incrementAndGet())
|
||||
.count(1)
|
||||
.build(), creativeNetId.get(), customBlock.creativeCategory().id());
|
||||
.build(), creativeNetId.get(), getCreativeIndex(
|
||||
customBlock.creativeGroup(),
|
||||
category,
|
||||
creativeGroupIds,
|
||||
lastCreativeGroupIds, creativeItemGroups)
|
||||
);
|
||||
creativeItems.add(creativeItemData);
|
||||
}
|
||||
}
|
||||
|
@ -717,4 +745,32 @@ public class ItemRegistryPopulator {
|
|||
builder.putCompound("components", componentBuilder.build());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
public static int getCreativeIndex(String creativeGroup, CreativeItemCategory creativeItemCategory, Map<String, Integer> groupIndexes, Map<CreativeItemCategory, Integer> fallBacks, List<CreativeItemGroup> creativeItemGroups) {
|
||||
if (fallBacks.isEmpty()) {
|
||||
// Not post rewrite, this index wont be used either way
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (creativeGroup != null) {
|
||||
if (groupIndexes.containsKey(creativeGroup)) {
|
||||
return groupIndexes.get(creativeGroup);
|
||||
}
|
||||
}
|
||||
|
||||
if (creativeItemCategory != null) {
|
||||
if (fallBacks.containsKey(creativeItemCategory)) {
|
||||
return fallBacks.get(creativeItemCategory);
|
||||
}
|
||||
|
||||
// Must "register" this category first
|
||||
// Sending a creative item group with no items in it crashes the client. Fun.
|
||||
creativeItemGroups.add(new CreativeItemGroup(creativeItemCategory, "", ItemData.AIR));
|
||||
fallBacks.put(creativeItemCategory, creativeItemGroups.size() - 1);
|
||||
|
||||
return fallBacks.get(creativeItemCategory);
|
||||
}
|
||||
|
||||
return fallBacks.get(CreativeItemCategory.UNDEFINED);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue