Skip to content

Commit

Permalink
use randomsource instead of random
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Aug 9, 2023
1 parent 6dfe2d0 commit 4146367
Show file tree
Hide file tree
Showing 41 changed files with 275 additions and 229 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.possible_triangle.skygrid.api

import com.possible_triangle.skygrid.api.xml.elements.WeightedEntry
import net.minecraft.util.RandomSource
import java.util.*
import kotlin.random.Random

class WeightedList<T : WeightedEntry>(pairs: List<T>): Collection<T> {

Expand All @@ -21,7 +21,7 @@ class WeightedList<T : WeightedEntry>(pairs: List<T>): Collection<T> {
map[total] = element
}

fun random(random: Random): T {
fun random(random: RandomSource): T {
if (isEmpty()) throw IllegalArgumentException("WeightedList is empty")
val value = random.nextDouble() * total
return map.higherEntry(value)?.value ?: throw NullPointerException("No entry found")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.possible_triangle.skygrid.api.extensions

import com.possible_triangle.skygrid.api.xml.elements.BlockProvider
import net.minecraft.util.RandomSource
import net.minecraft.world.level.block.Block

fun Collection<BlockProvider>.flat(): List<Pair<Block, Double>> {
Expand All @@ -12,4 +13,9 @@ fun Collection<BlockProvider>.flat(): List<Pair<Block, Double>> {
}
extras + provider.flat().map { it.first to it.second * probability }
}
}

fun <T> Collection<T>.random(random: RandomSource): T {
if (isEmpty()) throw NoSuchElementException("Collection is empty.")
return elementAt(random.nextInt(size))
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.possible_triangle.skygrid.api.world

import kotlin.random.Random
import net.minecraft.util.RandomSource

fun interface Generator<B : IBlockAccess> {
fun generate(random: Random, access: B): Boolean
fun generate(random: RandomSource, access: B): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import com.possible_triangle.skygrid.api.xml.Validating
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import net.minecraft.core.Registry
import net.minecraft.util.RandomSource
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.state.BlockState
import kotlin.random.Random

@Serializable
abstract class BlockProvider : WeightedEntry(), Generator<IBlockAccess>, Validating {
Expand Down Expand Up @@ -43,9 +43,9 @@ abstract class BlockProvider : WeightedEntry(), Generator<IBlockAccess>, Validat
filters: List<FilterOperator>,
): Boolean = parent.internalValidate(blocks, references, filters)

override fun base(random: Random): Block = parent.base(random)
override fun base(random: RandomSource): Block = parent.base(random)

override fun generateBase(random: Random, chunk: IBlockAccess): Boolean = parent.generateBase(random, chunk)
override fun generateBase(random: RandomSource, chunk: IBlockAccess): Boolean = parent.generateBase(random, chunk)
}

stripped.validExtras = emptyList()
Expand All @@ -70,24 +70,24 @@ abstract class BlockProvider : WeightedEntry(), Generator<IBlockAccess>, Validat
return internalValidate(blocks, references, additionalFilters + filters)
}

internal abstract fun base(random: Random): Block
internal abstract fun base(random: RandomSource): Block

private fun getState(random: Random): BlockState {
private fun getState(random: RandomSource): BlockState {
return transformers.fold(base(random).defaultBlockState()) { state, transformer ->
transformer.apply(state, random)
}
}

protected open fun generateBase(random: Random, chunk: IBlockAccess): Boolean {
protected open fun generateBase(random: RandomSource, chunk: IBlockAccess): Boolean {
val state = getState(random)
return chunk.set(state)
}

final override fun generate(random: Random, access: IBlockAccess): Boolean {
final override fun generate(random: RandomSource, access: IBlockAccess): Boolean {
val sharedSeed = random.nextLong()
return generateBase(random, access).apply {
if (this) validExtras.forEach {
it.generate(if (it.shared) Random(sharedSeed) else random, access)
it.generate(if (it.shared) RandomSource.create(sharedSeed) else random, access)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import net.minecraft.core.RegistryAccess
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.NbtOps
import net.minecraft.tags.TagKey
import net.minecraft.util.RandomSource
import net.minecraft.util.random.SimpleWeightedRandomList
import net.minecraft.world.level.SpawnData
import net.minecraft.world.level.block.Block
Expand All @@ -21,7 +22,6 @@ import net.minecraft.world.level.block.EntityBlock
import net.minecraft.world.level.block.entity.BlockEntityType
import nl.adaptivity.xmlutil.serialization.XmlSerialName
import java.util.*
import kotlin.random.Random


@Serializable
Expand All @@ -40,7 +40,7 @@ data class DimensionConfig(
@Transient
lateinit var gap: Optional<SingleBlock>

override fun generate(random: Random, access: IBlockAccess): Boolean {
override fun generate(random: RandomSource, access: IBlockAccess): Boolean {
val generateLoot = loot.isValid()
val fillSpawners = mobs.isValid()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import net.minecraft.core.BlockPos
import net.minecraft.core.Registry
import net.minecraft.util.RandomSource
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.state.BlockState
import kotlin.random.Random

@Serializable
abstract class Extra : Generator<IBlockAccess>, Validating {
Expand All @@ -25,7 +25,7 @@ abstract class Extra : Generator<IBlockAccess>, Validating {

abstract fun internalValidate(blocks: Registry<Block>): Boolean

abstract fun offset(pos: BlockPos, random: Random): BlockPos
abstract fun offset(pos: BlockPos, random: RandomSource): BlockPos

override fun validate(
blocks: Registry<Block>,
Expand All @@ -36,19 +36,20 @@ abstract class Extra : Generator<IBlockAccess>, Validating {
return internalValidate(blocks) && validProviders.isNotEmpty()
}

protected open fun transform(state: BlockState, random: Random): BlockState {
protected open fun transform(state: BlockState, random: RandomSource): BlockState {
return state
}

override fun generate(random: Random, access: IBlockAccess): Boolean {
val providerRandom = Random(random.nextLong())
val offsetRandom = random.nextLong()
override fun generate(random: RandomSource, access: IBlockAccess): Boolean {
val providerRandom = random.fork()
val offsetRandom = random.fork()
val transformRandom = random.fork()

if (random.nextDouble() > probability) return false
val at = offset(BlockPos(0, 0, 0), Random(offsetRandom))
val at = offset(BlockPos(0, 0, 0), offsetRandom)

return validProviders.random(random).generate(providerRandom) { state, pos ->
val transformed = transform(state, Random(offsetRandom))
val transformed = transform(state, transformRandom)
access.set(transformed, pos.offset(at))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.possible_triangle.skygrid.api.xml.elements
import com.possible_triangle.skygrid.api.WeightedList
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlin.random.Random
import net.minecraft.util.RandomSource

@Serializable
data class ListWrapper<T : WeightedEntry>(
Expand All @@ -30,7 +30,7 @@ data class ListWrapper<T : WeightedEntry>(
return weighted.isNotEmpty()
}

fun random(random: Random): T {
fun random(random: RandomSource): T {
return weighted.random(random)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import com.possible_triangle.skygrid.api.world.Generator
import com.possible_triangle.skygrid.api.world.IBlockAccess
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.random.Random
import net.minecraft.util.RandomSource

@Serializable
@SerialName("preset")
data class Preset(val provider: BlockProvider) : Generator<IBlockAccess> {

override fun generate(random: Random, access: IBlockAccess): Boolean {
override fun generate(random: RandomSource, access: IBlockAccess): Boolean {
return provider.generate(random, access, )
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.possible_triangle.skygrid.api.xml.elements

import net.minecraft.util.RandomSource
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.block.state.properties.Property
import kotlin.random.Random

abstract class PropertyTransformer : Transformer() {

abstract val key: String

final override fun apply(state: BlockState, random: Random): BlockState {
final override fun apply(state: BlockState, random: RandomSource): BlockState {
val property = state.properties.find { it.name == key } ?: return state
return transform(state, property, random)
}

protected abstract fun <T : Comparable<T>> transform(
state: BlockState,
property: Property<T>,
random: Random,
random: RandomSource,
): BlockState

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.possible_triangle.skygrid.api.xml.elements

import com.possible_triangle.skygrid.api.world.IBlockAccess
import net.minecraft.util.RandomSource
import net.minecraft.world.level.block.Block
import kotlin.random.Random

abstract class ProxyProvider : BlockProvider() {

abstract fun get(random: Random): BlockProvider
abstract fun get(random: RandomSource): BlockProvider

final override fun base(random: Random): Block {
final override fun base(random: RandomSource): Block {
return get(random).base(random)
}

override fun generateBase(random: Random, chunk: IBlockAccess): Boolean {
override fun generateBase(random: RandomSource, chunk: IBlockAccess): Boolean {
return get(random).generate(random, chunk, )
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.possible_triangle.skygrid.api.xml.elements

import kotlinx.serialization.Serializable
import net.minecraft.util.RandomSource
import net.minecraft.world.level.block.state.BlockState
import kotlin.random.Random

@Serializable
abstract class Transformer {

abstract fun apply(state: BlockState, random: Random): BlockState
abstract fun apply(state: BlockState, random: RandomSource): BlockState

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.possible_triangle.skygrid.api.xml.elements.extras

import com.possible_triangle.skygrid.api.extensions.random
import com.possible_triangle.skygrid.api.xml.elements.BlockProvider
import com.possible_triangle.skygrid.api.xml.elements.Extra
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.core.Direction.*
import net.minecraft.core.Registry
import net.minecraft.util.RandomSource
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.Rotation
import net.minecraft.world.level.block.state.BlockState
import com.possible_triangle.skygrid.api.xml.elements.BlockProvider
import com.possible_triangle.skygrid.api.xml.elements.Extra
import kotlin.random.Random

@Serializable
@SerialName("cardinal")
Expand All @@ -27,22 +28,22 @@ data class Cardinal(
val DIRECTIONS = listOf(SOUTH, WEST, NORTH, EAST, )
}

private fun getDirection(random: Random): Direction {
private fun getDirection(random: RandomSource): Direction {
return DIRECTIONS.random(random)
}

override fun internalValidate(blocks: Registry<Block>): Boolean {
return offset > 0
}

override fun transform(state: BlockState, random: Random): BlockState {
override fun transform(state: BlockState, random: RandomSource): BlockState {
if(!rotate) return state
val direction = getDirection(random)
val rotations = DIRECTIONS.indexOf(direction)
return state.rotate(Rotation.values()[rotations % 4])
}

override fun offset(pos: BlockPos, random: Random): BlockPos {
override fun offset(pos: BlockPos, random: RandomSource): BlockPos {
val direction = getDirection(random)
return pos.relative(direction, offset)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import net.minecraft.core.Registry
import net.minecraft.world.level.block.Block
import com.possible_triangle.skygrid.api.xml.elements.BlockProvider
import com.possible_triangle.skygrid.api.xml.elements.Extra
import kotlin.random.Random
import net.minecraft.util.RandomSource

@Serializable
@SerialName("offset")
Expand All @@ -24,7 +24,7 @@ data class Offset(
return true
}

override fun offset(pos: BlockPos, random: Random): BlockPos {
override fun offset(pos: BlockPos, random: RandomSource): BlockPos {
return pos.offset(x, y, z)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import kotlinx.serialization.Serializable
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.core.Registry
import net.minecraft.util.RandomSource
import net.minecraft.world.level.block.Block
import kotlin.random.Random

@Serializable
@SerialName("side")
Expand All @@ -24,7 +24,7 @@ data class Side(
return Direction.byName(on) != null && offset > 0
}

override fun offset(pos: BlockPos, random: Random): BlockPos {
override fun offset(pos: BlockPos, random: RandomSource): BlockPos {
val direction = Direction.byName(on) ?: throw NullPointerException("Unknown direction '$on'")
return pos.relative(direction, offset)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import net.minecraft.core.Registry
import net.minecraft.util.RandomSource
import net.minecraft.world.level.block.Block
import kotlin.random.Random

@Serializable
@SerialName("list")
Expand All @@ -36,7 +36,7 @@ data class BlockList(
return validChildren.isNotEmpty()
}

override fun get(random: Random): BlockProvider {
override fun get(random: RandomSource): BlockProvider {
return validChildren.random(random)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import net.minecraft.core.Registry
import net.minecraft.util.RandomSource
import net.minecraft.world.level.block.Block
import kotlin.random.Random

@Serializable
@SerialName("fallback")
Expand All @@ -34,7 +34,7 @@ data class Fallback(
return true
}

override fun get(random: Random): BlockProvider {
override fun get(random: RandomSource): BlockProvider {
return provider
}

Expand Down
Loading

0 comments on commit 4146367

Please sign in to comment.