Skip to content

Commit

Permalink
server: Replaced all math/rand with math/rand/v2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Dec 29, 2024
1 parent 4719c63 commit cc7e4ee
Show file tree
Hide file tree
Showing 78 changed files with 180 additions and 172 deletions.
6 changes: 3 additions & 3 deletions server/block/beetroot_seeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
)

// BeetrootSeeds are a crop that can be harvested to craft soup or red dye.
Expand Down Expand Up @@ -53,7 +53,7 @@ func (b BeetrootSeeds) BreakInfo() BreakInfo {
if b.Growth < 7 {
return []item.Stack{item.NewStack(b, 1)}
}
return []item.Stack{item.NewStack(item.Beetroot{}, 1), item.NewStack(b, rand.Intn(4)+1)}
return []item.Stack{item.NewStack(item.Beetroot{}, 1), item.NewStack(b, rand.IntN(4)+1)}
})
}

Expand All @@ -71,7 +71,7 @@ func (b BeetrootSeeds) EncodeItem() (name string, meta int16) {
func (b BeetrootSeeds) RandomTick(pos cube.Pos, tx *world.Tx, r *rand.Rand) {
if tx.Light(pos) < 8 {
breakBlock(b, pos, tx)
} else if b.Growth < 7 && r.Intn(3) > 0 && r.Float64() <= b.CalculateGrowthChance(pos, tx) {
} else if b.Growth < 7 && r.IntN(3) > 0 && r.Float64() <= b.CalculateGrowthChance(pos, tx) {
b.Growth++
tx.SetBlock(pos, b, nil)
}
Expand Down
4 changes: 2 additions & 2 deletions server/block/blackstone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package block
import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"math/rand"
"math/rand/v2"
)

// Blackstone is a naturally generating block in the nether that can be used to craft stone tools, brewing stands and
Expand All @@ -22,7 +22,7 @@ func (b Blackstone) BreakInfo() BreakInfo {
if b.Type == GildedBlackstone() {
drops = func(item.Tool, []item.Enchantment) []item.Stack {
if rand.Float64() < 0.1 {
return []item.Stack{item.NewStack(item.GoldNugget{}, rand.Intn(4)+2)}
return []item.Stack{item.NewStack(item.GoldNugget{}, rand.IntN(4)+2)}
}
return []item.Stack{item.NewStack(b, 1)}
}
Expand Down
2 changes: 1 addition & 1 deletion server/block/blast_furnace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
"time"
)

Expand Down
2 changes: 1 addition & 1 deletion server/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
"time"
)

Expand Down
4 changes: 2 additions & 2 deletions server/block/break_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/particle"
"math"
"math/rand"
"math/rand/v2"
"slices"
"time"
)
Expand Down Expand Up @@ -133,7 +133,7 @@ type XPDropRange [2]int
func (r XPDropRange) RandomValue() int {
diff := r[1] - r[0]
// Add one because it's a [r[0], r[1]] interval.
return rand.Intn(diff+1) + r[0]
return rand.IntN(diff+1) + r[0]
}

// pickaxeEffective is a convenience function for blocks that are effectively mined with a pickaxe.
Expand Down
2 changes: 1 addition & 1 deletion server/block/cactus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
)

// Cactus is a plant block that generates naturally in dry areas and causes damage.
Expand Down
2 changes: 1 addition & 1 deletion server/block/campfire.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
"strconv"
"time"
)
Expand Down
6 changes: 3 additions & 3 deletions server/block/carrot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
"time"
)

Expand Down Expand Up @@ -41,7 +41,7 @@ func (c Carrot) BoneMeal(pos cube.Pos, tx *world.Tx) bool {
if c.Growth == 7 {
return false
}
c.Growth = min(c.Growth+rand.Intn(4)+2, 7)
c.Growth = min(c.Growth+rand.IntN(4)+2, 7)
tx.SetBlock(pos, c, nil)
return true
}
Expand All @@ -67,7 +67,7 @@ func (c Carrot) BreakInfo() BreakInfo {
if c.Growth < 7 {
return []item.Stack{item.NewStack(c, 1)}
}
return []item.Stack{item.NewStack(c, rand.Intn(4)+2)}
return []item.Stack{item.NewStack(c, rand.IntN(4)+2)}
})
}

Expand Down
6 changes: 3 additions & 3 deletions server/block/cocoa_bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
)

// CocoaBean is a crop block found in jungle biomes.
Expand Down Expand Up @@ -79,7 +79,7 @@ func (c CocoaBean) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, tx *wo

// RandomTick ...
func (c CocoaBean) RandomTick(pos cube.Pos, tx *world.Tx, r *rand.Rand) {
if c.Age < 2 && r.Intn(5) == 0 {
if c.Age < 2 && r.IntN(5) == 0 {
c.Age++
tx.SetBlock(pos, c, nil)
}
Expand All @@ -89,7 +89,7 @@ func (c CocoaBean) RandomTick(pos cube.Pos, tx *world.Tx, r *rand.Rand) {
func (c CocoaBean) BreakInfo() BreakInfo {
return newBreakInfo(0.2, alwaysHarvestable, axeEffective, func(item.Tool, []item.Enchantment) []item.Stack {
if c.Age == 2 {
return []item.Stack{item.NewStack(c, rand.Intn(2)+2)}
return []item.Stack{item.NewStack(c, rand.IntN(2)+2)}
}
return []item.Stack{item.NewStack(c, 1)}
}).withBlastResistance(15)
Expand Down
2 changes: 1 addition & 1 deletion server/block/composter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/particle"
"github.com/df-mc/dragonfly/server/world/sound"
"math/rand"
"math/rand/v2"
"time"
)

Expand Down
2 changes: 1 addition & 1 deletion server/block/copper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
)

// Copper is a solid block commonly found in deserts and beaches underneath sand.
Expand Down
2 changes: 1 addition & 1 deletion server/block/copper_door.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
)

// CopperDoor is a block that can be used as an openable 1x2 barrier.
Expand Down
2 changes: 1 addition & 1 deletion server/block/copper_grate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
)

// CopperGrate is a solid block commonly found in deserts and beaches underneath sand.
Expand Down
4 changes: 2 additions & 2 deletions server/block/copper_ore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package block

import (
"github.com/df-mc/dragonfly/server/item"
"math/rand"
"math/rand/v2"
)

// CopperOre is a rare mineral block found underground.
Expand All @@ -18,7 +18,7 @@ type CopperOre struct {
func (c CopperOre) BreakInfo() BreakInfo {
return newBreakInfo(c.Type.Hardness(), func(t item.Tool) bool {
return t.ToolType() == item.TypePickaxe && t.HarvestLevel() >= item.ToolTierStone.HarvestLevel
}, pickaxeEffective, silkTouchDrop(item.NewStack(item.RawCopper{}, rand.Intn(4)+2), item.NewStack(c, 1))).withBlastResistance(9)
}, pickaxeEffective, silkTouchDrop(item.NewStack(item.RawCopper{}, rand.IntN(4)+2), item.NewStack(c, 1))).withBlastResistance(9)
}

// SmeltInfo ...
Expand Down
2 changes: 1 addition & 1 deletion server/block/copper_trapdoor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math"
"math/rand"
"math/rand/v2"
)

// CopperTrapdoor is a block that can be used as an openable 1x1 barrier.
Expand Down
2 changes: 1 addition & 1 deletion server/block/coral.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
"time"
)

Expand Down
2 changes: 1 addition & 1 deletion server/block/coral_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package block
import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/world"
"math/rand"
"math/rand/v2"
"time"
)

Expand Down
4 changes: 2 additions & 2 deletions server/block/deadbush.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
)

// DeadBush is a transparent block in the form of an aesthetic plant.
Expand Down Expand Up @@ -55,7 +55,7 @@ func (d DeadBush) BreakInfo() BreakInfo {
if t.ToolType() == item.TypeShears {
return []item.Stack{item.NewStack(d, 1)}
}
if amount := rand.Intn(3); amount != 0 {
if amount := rand.IntN(3); amount != 0 {
return []item.Stack{item.NewStack(item.Stick{}, amount)}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion server/block/double_tall_grass.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
)

// DoubleTallGrass is a two-block high variety of grass.
Expand Down
4 changes: 2 additions & 2 deletions server/block/dragon_egg.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package block

import (
"math/rand"
"math/rand/v2"

"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/item"
Expand Down Expand Up @@ -30,7 +30,7 @@ func (d DragonEgg) SideClosed(cube.Pos, cube.Pos, *world.Tx) bool {
// teleport ...
func (d DragonEgg) teleport(pos cube.Pos, tx *world.Tx) {
for i := 0; i < 1000; i++ {
newPos := pos.Add(cube.Pos{rand.Intn(31) - 15, max(tx.Range()[0]-pos.Y(), min(tx.Range()[1]-pos.Y(), rand.Intn(15)-7)), rand.Intn(31) - 15})
newPos := pos.Add(cube.Pos{rand.IntN(31) - 15, max(tx.Range()[0]-pos.Y(), min(tx.Range()[1]-pos.Y(), rand.IntN(15)-7)), rand.IntN(31) - 15})

if _, ok := tx.Block(newPos).(Air); ok {
tx.SetBlock(newPos, d, nil)
Expand Down
17 changes: 10 additions & 7 deletions server/block/explosion.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math"
"math/rand"
"math/rand/v2"
"time"
)

Expand All @@ -18,8 +18,10 @@ import (
type ExplosionConfig struct {
// Size is the size of the explosion, it is effectively the radius which entities/blocks will be affected within.
Size float64
// Rand is the source to use for the explosion "randomness".
Rand rand.Source
// RandSource is the source to use for the explosion "randomness". If set
// to nil, RandSource defaults to a `rand.PCG`source seeded with
// `time.Now().UnixNano()`.
RandSource rand.Source
// SpawnFire will cause the explosion to randomly start fires in 1/3 of all destroyed air blocks that are
// above opaque blocks.
SpawnFire bool
Expand Down Expand Up @@ -74,8 +76,9 @@ func (c ExplosionConfig) Explode(tx *world.Tx, explosionPos mgl64.Vec3) {
if c.Particle == nil {
c.Particle = particle.HugeExplosion{}
}
if c.Rand == nil {
c.Rand = rand.NewSource(time.Now().UnixNano())
if c.RandSource == nil {
t := uint64(time.Now().UnixNano())
c.RandSource = rand.NewPCG(t, t)
}
if c.Size == 0 {
c.Size = 4
Expand All @@ -84,7 +87,7 @@ func (c ExplosionConfig) Explode(tx *world.Tx, explosionPos mgl64.Vec3) {
c.ItemDropChance = 1.0 / c.Size
}

r, d := rand.New(c.Rand), c.Size*2
r, d := rand.New(c.RandSource), c.Size*2
box := cube.Box(
math.Floor(explosionPos[0]-d-1),
math.Floor(explosionPos[1]-d-1),
Expand Down Expand Up @@ -164,7 +167,7 @@ func (c ExplosionConfig) Explode(tx *world.Tx, explosionPos mgl64.Vec3) {
}
if c.SpawnFire {
for _, pos := range affectedBlocks {
if r.Intn(3) == 0 {
if r.IntN(3) == 0 {
if _, ok := tx.Block(pos).(Air); ok && tx.Block(pos.Side(cube.FaceDown)).Model().FaceSolid(pos, cube.FaceUp, tx) {
Fire{}.Start(tx, pos)
}
Expand Down
2 changes: 1 addition & 1 deletion server/block/farmland.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/event"
"github.com/df-mc/dragonfly/server/world"
"math/rand"
"math/rand/v2"
)

// Farmland is a block that crops are grown on. Farmland is created by interacting with a grass or dirt block using a
Expand Down
2 changes: 1 addition & 1 deletion server/block/fern.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"math/rand/v2"
)

// Fern is a transparent plant block which can be used to obtain seeds and as decoration.
Expand Down
Loading

0 comments on commit cc7e4ee

Please sign in to comment.