-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45362df
commit df79ead
Showing
6 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ org.gradle.debug=false | |
|
||
|
||
# Version | ||
mod_version=1.2.05 | ||
mod_version=1.2.06 | ||
|
||
# Mod | ||
mod_id=zenith | ||
|
91 changes: 91 additions & 0 deletions
91
src/main/java/net/zepalesque/zenith/api/extendablestate/ExtendableStateList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package net.zepalesque.zenith.api.extendablestate; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.core.registries.Registries; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.util.RandomSource; | ||
import net.minecraft.util.random.SimpleWeightedRandomList; | ||
import net.minecraft.world.level.WorldGenLevel; | ||
import net.minecraft.world.level.biome.Biome; | ||
import net.minecraft.world.level.block.Blocks; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.zepalesque.zenith.core.registry.StateLists; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class ExtendableStateList { | ||
private final int weightForDefaults, weightForOthers, totalWeight; | ||
private final SimpleWeightedRandomList<BlockState> defaults; | ||
private final List<Entry> entries = new ArrayList<>(); | ||
|
||
public ExtendableStateList(int weightForDefaults, int weightForOthers, SimpleWeightedRandomList<BlockState> defaults) { | ||
this.weightForDefaults = weightForDefaults; | ||
this.weightForOthers = weightForOthers; | ||
this.defaults = defaults; | ||
|
||
this.totalWeight = weightForDefaults + weightForOthers; | ||
} | ||
|
||
public BlockState calculate(RandomSource random, WorldGenLevel level, BlockPos pos) { | ||
if (this.totalWeight == 0) return Blocks.AIR.defaultBlockState(); | ||
|
||
int i = random.nextInt(totalWeight); | ||
if (i < this.weightForDefaults) return calculateDefaults(random, level, pos); | ||
else return calculateOther(random, level, pos); | ||
|
||
} | ||
|
||
private BlockState calculateDefaults(RandomSource random, WorldGenLevel level, BlockPos pos) { | ||
return this.defaults.getRandomValue(random).orElseThrow(IllegalStateException::new); | ||
} | ||
|
||
private BlockState calculateOther(RandomSource random, WorldGenLevel level, BlockPos pos) { | ||
int length = this.entries.size(); | ||
int index = random.nextInt(length); | ||
return this.entries.get(index).calculate(random, level, pos); | ||
} | ||
|
||
public record Entry(ExtendableStateList list, Map<ResourceKey<Biome>, SimpleWeightedRandomList<BlockState>> byBiome, SimpleWeightedRandomList<BlockState> fallback) { | ||
|
||
private static boolean canCreate = false; | ||
|
||
@Deprecated | ||
public Entry { | ||
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) { | ||
canCreate = true; | ||
Entry e = new Entry(list, byBiome, fallback); | ||
list.entries.add(e); | ||
canCreate = false; | ||
return e; | ||
} | ||
|
||
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) | ||
).apply(builder, Entry::create)); | ||
|
||
|
||
public BlockState calculate(RandomSource random, WorldGenLevel level, BlockPos pos) { | ||
Holder<Biome> biome = level.getBiome(pos); | ||
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); | ||
} | ||
} | ||
return fallback.getRandomValue(random).orElseThrow(IllegalStateException::new); | ||
} | ||
|
||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...ain/java/net/zepalesque/zenith/api/world/feature/gen/ExtendableStateListBlockFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package net.zepalesque.zenith.api.world.feature.gen; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.level.WorldGenLevel; | ||
import net.minecraft.world.level.block.DoublePlantBlock; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate; | ||
import net.minecraft.world.level.levelgen.feature.Feature; | ||
import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext; | ||
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration; | ||
import net.zepalesque.zenith.api.extendablestate.ExtendableStateList; | ||
import net.zepalesque.zenith.core.registry.StateLists; | ||
|
||
import java.util.Optional; | ||
|
||
public class ExtendableStateListBlockFeature extends Feature<ExtendableStateListBlockFeature.Config> { | ||
|
||
public ExtendableStateListBlockFeature(Codec<Config> codec) { | ||
super(codec); | ||
} | ||
|
||
public boolean place(FeaturePlaceContext<ExtendableStateListBlockFeature.Config> context) { | ||
ExtendableStateListBlockFeature.Config config = context.config(); | ||
WorldGenLevel level = context.level(); | ||
BlockPos pos = context.origin(); | ||
Optional<BlockPredicate> predicate = context.config().predicate(); | ||
BlockState state = config.list().calculate(context.random(), context.level(), pos); | ||
if ((predicate.isEmpty() || predicate.get().test(level, pos)) && state.canSurvive(level, pos)) { | ||
if (state.getBlock() instanceof DoublePlantBlock) { | ||
if (!level.isEmptyBlock(pos.above())) { | ||
return false; | ||
} | ||
|
||
DoublePlantBlock.placeAt(level, state, pos, 2); | ||
} else { | ||
level.setBlock(pos, state, 2); | ||
} | ||
|
||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
|
||
public record Config(ExtendableStateList list, Optional<BlockPredicate> predicate) implements FeatureConfiguration { | ||
public static final Codec<Config> CODEC = RecordCodecBuilder.create((config) -> config.group( | ||
StateLists.STATE_LIST_REGISTRY.byNameCodec().fieldOf("to_place").forGetter(Config::list), | ||
BlockPredicate.CODEC.optionalFieldOf("predicate").forGetter(Config::predicate) | ||
).apply(config, Config::new)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/net/zepalesque/zenith/core/registry/StateLists.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package net.zepalesque.zenith.core.registry; | ||
|
||
import net.minecraft.core.Registry; | ||
import net.neoforged.neoforge.registries.RegistryBuilder; | ||
import net.zepalesque.zenith.api.extendablestate.ExtendableStateList; | ||
import net.zepalesque.zenith.core.Zenith; | ||
|
||
public class StateLists { | ||
// public static final DeferredRegister<ExtendableStateList> STATE_LISTS = DeferredRegister.create(Zenith.Keys.EXTENDABLE_STATE_LIST, Zenith.MODID); | ||
public static final Registry<ExtendableStateList> STATE_LIST_REGISTRY = new RegistryBuilder<>(Zenith.Keys.EXTENDABLE_STATE_LIST).sync(true).create(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters