Skip to content

Commit

Permalink
Fix registries that rely on order
Browse files Browse the repository at this point in the history
Vanilla no longer allows to register items to arbitrary
ids but some of our registries rely on this due to the
way vanilla stores this data and has no natural registries
for them.

Fixing this by registering the items in the order we want
them to be present in and then verying their ids match the
ones we expected afterwards.

While this is very brittle, it is the most straight forward
way to ensure we stay in sync with vanilla elements
without more complex changes.
  • Loading branch information
aromaa committed Nov 14, 2024
1 parent 64bec57 commit 9b83c93
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,16 @@ public <T> Registry<T> createRegistry(final RegistryType<T> type, final @Nullabl
if (defaultValues != null) {
final MappedRegistry<T> mr = (MappedRegistry<T>) registry;
defaultValues.forEach((vk, vi, vv) -> {
if (vi.isPresent()) {
mr.register(
net.minecraft.resources.ResourceKey.create(key, (ResourceLocation) (Object) vk),
vv,
RegistrationInfo.BUILT_IN
);
} else {
mr.register(
net.minecraft.resources.ResourceKey.create(key, (ResourceLocation) (Object) vk),
vv,
RegistrationInfo.BUILT_IN
);
}
mr.register(
net.minecraft.resources.ResourceKey.create(key, (ResourceLocation) (Object) vk),
vv,
RegistrationInfo.BUILT_IN
);
vi.ifPresent(id -> {
if (mr.getId(vv) != id) {
throw new IllegalStateException("Registry entry " + vk + " was expected to have id of " + id + " but was instead " + mr.getId(vv));
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
import org.spongepowered.api.registry.RegistryKey;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public final class RegistryLoader<T> extends InitialRegistryData<T> {

private final Map<ResourceKey, T> values = new HashMap<>();
private final Map<ResourceKey, T> values = new LinkedHashMap<>();
private @MonotonicNonNull Map<ResourceKey, Integer> ids;

private RegistryLoader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ private static RegistryLoader<Criterion> criterion() {

private static RegistryLoader<FireworkShape> fireworkShape() {
return RegistryLoader.of(l -> {
l.addWithId(FireworkExplosion.Shape.BURST.getId(), FireworkShapes.BURST, () -> (FireworkShape) (Object) FireworkExplosion.Shape.BURST);
l.addWithId(FireworkExplosion.Shape.CREEPER.getId(), FireworkShapes.CREEPER, () -> (FireworkShape) (Object) FireworkExplosion.Shape.CREEPER);
l.addWithId(FireworkExplosion.Shape.LARGE_BALL.getId(), FireworkShapes.LARGE_BALL, () -> (FireworkShape) (Object) FireworkExplosion.Shape.LARGE_BALL);
l.addWithId(FireworkExplosion.Shape.SMALL_BALL.getId(), FireworkShapes.SMALL_BALL, () -> (FireworkShape) (Object) FireworkExplosion.Shape.SMALL_BALL);
l.addWithId(FireworkExplosion.Shape.LARGE_BALL.getId(), FireworkShapes.LARGE_BALL, () -> (FireworkShape) (Object) FireworkExplosion.Shape.LARGE_BALL);
l.addWithId(FireworkExplosion.Shape.STAR.getId(), FireworkShapes.STAR, () -> (FireworkShape) (Object) FireworkExplosion.Shape.STAR);
l.addWithId(FireworkExplosion.Shape.CREEPER.getId(), FireworkShapes.CREEPER, () -> (FireworkShape) (Object) FireworkExplosion.Shape.CREEPER);
l.addWithId(FireworkExplosion.Shape.BURST.getId(), FireworkShapes.BURST, () -> (FireworkShape) (Object) FireworkExplosion.Shape.BURST);
});
}

Expand Down

0 comments on commit 9b83c93

Please sign in to comment.