Ensure custom blocks can be represented at any index

This commit is contained in:
Camotoy 2024-06-09 15:06:39 -04:00
parent 8ae1150c80
commit 007ecb4363
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
4 changed files with 26 additions and 10 deletions

View file

@ -30,8 +30,6 @@ import org.geysermc.geyser.item.type.*;
import org.geysermc.geyser.level.block.Blocks;
import org.geysermc.geyser.registry.Registries;
import java.util.Collections;
import static org.geysermc.geyser.item.type.Item.builder;
/**
@ -1378,11 +1376,7 @@ public final class Items {
public static <T extends Item> T register(T item, int id) {
item.setJavaId(id);
// This makes sure that the array is large enough to put the java item at the correct location
if (Registries.JAVA_ITEMS.get().size() <= id) {
Registries.JAVA_ITEMS.get().addAll(Collections.nCopies(id - Registries.JAVA_ITEMS.get().size() + 1, AIR));
}
Registries.JAVA_ITEMS.get().set(id, item);
Registries.JAVA_ITEMS.registerWithAnyIndex(id, item, AIR);
Registries.JAVA_ITEM_IDENTIFIERS.register(item.javaIdentifier(), item);
return item;
}

View file

@ -29,6 +29,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.registry.loader.RegistryLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
@ -94,6 +95,25 @@ public class ListRegistry<M> extends Registry<List<M>> {
return this.mappings.set(index, value);
}
/**
* Registers a new value into this registry with the given index, even if this value would normally be outside
* the range of a list.
*
* @param index the index
* @param value the value
* @param defaultValue the default value to fill empty spaces in the registry with.
* @return a new value into this registry with the given index.
*/
public M registerWithAnyIndex(int index, M value, M defaultValue) {
if (this.frozen) {
throw new IllegalStateException("Registry should not be modified after frozen!");
}
if (this.mappings.size() <= index) {
this.mappings.addAll(Collections.nCopies(index - this.mappings.size() + 1, defaultValue));
}
return this.mappings.set(index, value);
}
/**
* Mark this registry as unsuitable for new additions. The backing list will then be optimized for storage.
*/

View file

@ -127,7 +127,10 @@ public final class Registries {
*/
public static final PacketTranslatorRegistry<Packet> JAVA_PACKET_TRANSLATORS = PacketTranslatorRegistry.create();
public static final SimpleRegistry<List<Item>> JAVA_ITEMS = SimpleRegistry.create(RegistryLoaders.empty(ArrayList::new));
/**
* A registry containing all Java items ordered by their network ID.
*/
public static final ListRegistry<Item> JAVA_ITEMS = ListRegistry.create(RegistryLoaders.empty(ArrayList::new));
public static final SimpleMappedRegistry<String, Item> JAVA_ITEM_IDENTIFIERS = SimpleMappedRegistry.create(RegistryLoaders.empty(Object2ObjectOpenHashMap::new));

View file

@ -213,7 +213,6 @@ public final class BlockRegistryPopulator {
GeyserBedrockBlock[] javaToBedrockBlocks = new GeyserBedrockBlock[JAVA_BLOCKS_SIZE];
GeyserBedrockBlock[] javaToVanillaBedrockBlocks = new GeyserBedrockBlock[JAVA_BLOCKS_SIZE];
//List<String> javaToBedrockIdentifiers = new ArrayList<>(BlockRegistries.JAVA_BLOCKS.get().size());
var javaToBedrockIdentifiers = new Int2ObjectOpenHashMap<String>();
Block lastBlockSeen = null;
@ -456,7 +455,7 @@ public final class BlockRegistryPopulator {
};
block.setJavaId(javaBlockState.stateGroupId());
BlockRegistries.JAVA_BLOCKS.get().add(javaBlockState.stateGroupId(), block); //TODO don't allow duplicates, allow blanks
BlockRegistries.JAVA_BLOCKS.registerWithAnyIndex(javaBlockState.stateGroupId(), block, Blocks.AIR);
BlockRegistries.JAVA_IDENTIFIER_TO_ID.register(javaId, stateRuntimeId);
BlockRegistries.BLOCK_STATES.register(stateRuntimeId, new BlockState(block, stateRuntimeId));
}