Skip to content

Commit

Permalink
refactor: use optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
Zepalesque committed Jan 3, 2025
1 parent df79ead commit fbf4a4b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.debug=false


# Version
mod_version=1.2.06
mod_version=1.2.07

# Mod
mod_id=zenith
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private BlockState calculateOther(RandomSource random, WorldGenLevel level, Bloc
return this.entries.get(index).calculate(random, level, pos);
}

public record Entry(ExtendableStateList list, Map<ResourceKey<Biome>, SimpleWeightedRandomList<BlockState>> byBiome, SimpleWeightedRandomList<BlockState> fallback) {
public record Entry(ExtendableStateList list, Optional<Map<ResourceKey<Biome>, SimpleWeightedRandomList<BlockState>>> byBiome, Optional<SimpleWeightedRandomList<BlockState>> fallback) {

private static boolean canCreate = false;

Expand All @@ -60,7 +60,7 @@ public record Entry(ExtendableStateList list, Map<ResourceKey<Biome>, SimpleWeig
if (!canCreate) throw new IllegalStateException("Use ExtendableStateList$Entry#create please!");
}

public static Entry create(ExtendableStateList list, Map<ResourceKey<Biome>, SimpleWeightedRandomList<BlockState>> byBiome, SimpleWeightedRandomList<BlockState> fallback) {
public static Entry create(ExtendableStateList list, Optional<Map<ResourceKey<Biome>, SimpleWeightedRandomList<BlockState>>> byBiome, Optional<SimpleWeightedRandomList<BlockState>> fallback) {
canCreate = true;
Entry e = new Entry(list, byBiome, fallback);
list.entries.add(e);
Expand All @@ -70,8 +70,8 @@ public static Entry create(ExtendableStateList list, Map<ResourceKey<Biome>, Sim

public static Codec<Entry> CODEC = RecordCodecBuilder.create(builder -> builder.group(
StateLists.STATE_LIST_REGISTRY.byNameCodec().fieldOf("parent_state_list").forGetter(Entry::list),
Codec.unboundedMap(ResourceKey.codec(Registries.BIOME), SimpleWeightedRandomList.wrappedCodec(BlockState.CODEC)).fieldOf("by_biome").forGetter(Entry::byBiome),
SimpleWeightedRandomList.wrappedCodec(BlockState.CODEC).fieldOf("fallback").forGetter(Entry::fallback)
Codec.unboundedMap(ResourceKey.codec(Registries.BIOME), SimpleWeightedRandomList.wrappedCodec(BlockState.CODEC)).optionalFieldOf("by_biome").forGetter(Entry::byBiome),
SimpleWeightedRandomList.wrappedCodec(BlockState.CODEC).optionalFieldOf("fallback").forGetter(Entry::fallback)
).apply(builder, Entry::create));


Expand All @@ -80,11 +80,11 @@ public BlockState calculate(RandomSource random, WorldGenLevel level, BlockPos p
Optional<ResourceKey<Biome>> optional = biome.unwrapKey();
if (optional.isPresent()) {
ResourceKey<Biome> key = optional.get();
if (this.byBiome.containsKey(key)) {
return this.byBiome.get(key).getRandomValue(random).orElseThrow(IllegalStateException::new);
if (this.byBiome.isPresent() && this.byBiome.get().containsKey(key)) {
return this.byBiome.get().get(key).getRandomValue(random).orElseThrow(IllegalStateException::new);
}
}
return fallback.getRandomValue(random).orElseThrow(IllegalStateException::new);
return this.fallback.isEmpty() ? Blocks.AIR.defaultBlockState() : this.fallback.get().getRandomValue(random).orElseThrow(IllegalStateException::new);
}

}
Expand Down

0 comments on commit fbf4a4b

Please sign in to comment.