Skip to content

Commit

Permalink
Refactored a ton of blocks/items to use world.Tx instead of world.World.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Oct 16, 2024
1 parent e3f2187 commit a13eb38
Show file tree
Hide file tree
Showing 171 changed files with 1,401 additions and 1,402 deletions.
16 changes: 8 additions & 8 deletions server/block/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (a Anvil) BreakInfo() BreakInfo {
}

// Activate ...
func (Anvil) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User, _ *item.UseContext) bool {
func (Anvil) Activate(pos cube.Pos, clickedFace cube.Face, tx *world.Tx, u item.User, ctx *item.UseContext) bool {
if opener, ok := u.(ContainerOpener); ok {
opener.OpenBlockContainer(pos)
return true
Expand All @@ -40,19 +40,19 @@ func (Anvil) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User, _
}

// UseOnBlock ...
func (a Anvil) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) (used bool) {
pos, _, used = firstReplaceable(w, pos, face, a)
func (a Anvil) UseOnBlock(pos cube.Pos, face cube.Face, clickPos mgl64.Vec3, tx *world.Tx, user item.User, ctx *item.UseContext) (used bool) {
pos, _, used = firstReplaceable(tx, pos, face, a)
if !used {
return
}
a.Facing = user.Rotation().Direction().RotateLeft()
place(w, pos, a, user, ctx)
place(tx, pos, a, user, ctx)
return placed(ctx)
}

// NeighbourUpdateTick ...
func (a Anvil) NeighbourUpdateTick(pos, _ cube.Pos, w *world.World) {
a.fall(a, pos, w)
func (a Anvil) NeighbourUpdateTick(pos, changedNeighbour cube.Pos, tx *world.Tx) {
a.fall(a, pos, tx)
}

// Damage returns the damage per block fallen of the anvil and the maximum damage the anvil can deal.
Expand All @@ -75,8 +75,8 @@ func (a Anvil) Break() world.Block {
}

// Landed is called when a falling anvil hits the ground, used to, for example, play a sound.
func (Anvil) Landed(w *world.World, pos cube.Pos) {
w.PlaySound(pos.Vec3Centre(), sound.AnvilLand{})
func (Anvil) Landed(tx *world.Tx, pos cube.Pos) {
tx.PlaySound(pos.Vec3Centre(), sound.AnvilLand{})
}

// EncodeItem ...
Expand Down
26 changes: 13 additions & 13 deletions server/block/banner.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,36 @@ func (Banner) FuelInfo() item.FuelInfo {
}

// UseOnBlock ...
func (b Banner) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) (used bool) {
pos, face, used = firstReplaceable(w, pos, face, b)
func (b Banner) UseOnBlock(pos cube.Pos, face cube.Face, clickPos mgl64.Vec3, tx *world.Tx, user item.User, ctx *item.UseContext) (used bool) {
pos, face, used = firstReplaceable(tx, pos, face, b)
if !used || face == cube.FaceDown {
return false
}

if face == cube.FaceUp {
b.Attach = StandingAttachment(user.Rotation().Orientation().Opposite())
place(w, pos, b, user, ctx)
place(tx, pos, b, user, ctx)
return
}
b.Attach = WallAttachment(face.Direction())
place(w, pos, b, user, ctx)
place(tx, pos, b, user, ctx)
return placed(ctx)
}

// NeighbourUpdateTick ...
func (b Banner) NeighbourUpdateTick(pos, _ cube.Pos, w *world.World) {
func (b Banner) NeighbourUpdateTick(pos, changedNeighbour cube.Pos, tx *world.Tx) {
if b.Attach.hanging {
if _, ok := w.Block(pos.Side(b.Attach.facing.Opposite().Face())).(Air); ok {
w.SetBlock(pos, nil, nil)
w.AddParticle(pos.Vec3Centre(), particle.BlockBreak{Block: b})
dropItem(w, item.NewStack(b, 1), pos.Vec3Centre())
if _, ok := tx.Block(pos.Side(b.Attach.facing.Opposite().Face())).(Air); ok {
tx.SetBlock(pos, nil, nil)
tx.AddParticle(pos.Vec3Centre(), particle.BlockBreak{Block: b})
dropItem(tx, item.NewStack(b, 1), pos.Vec3Centre())
}
return
}
if _, ok := w.Block(pos.Side(cube.FaceDown)).(Air); ok {
w.SetBlock(pos, nil, nil)
w.AddParticle(pos.Vec3Centre(), particle.BlockBreak{Block: b})
dropItem(w, item.NewStack(b, 1), pos.Vec3Centre())
if _, ok := tx.Block(pos.Side(cube.FaceDown)).(Air); ok {
tx.SetBlock(pos, nil, nil)
tx.AddParticle(pos.Vec3Centre(), particle.BlockBreak{Block: b})
dropItem(tx, item.NewStack(b, 1), pos.Vec3Centre())
}
}

Expand Down
36 changes: 18 additions & 18 deletions server/block/barrel.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewBarrel() Barrel {
}

// Inventory returns the inventory of the barrel. The size of the inventory will be 27.
func (b Barrel) Inventory(*world.World, cube.Pos) *inventory.Inventory {
func (b Barrel) Inventory(*world.Tx, cube.Pos) *inventory.Inventory {
return b.inventory
}

Expand All @@ -62,45 +62,45 @@ func (b Barrel) WithName(a ...any) world.Item {
}

// open opens the barrel, displaying the animation and playing a sound.
func (b Barrel) open(w *world.World, pos cube.Pos) {
func (b Barrel) open(tx *world.Tx, pos cube.Pos) {
b.Open = true
w.PlaySound(pos.Vec3Centre(), sound.BarrelOpen{})
w.SetBlock(pos, b, nil)
tx.PlaySound(pos.Vec3Centre(), sound.BarrelOpen{})
tx.SetBlock(pos, b, nil)
}

// close closes the barrel, displaying the animation and playing a sound.
func (b Barrel) close(w *world.World, pos cube.Pos) {
func (b Barrel) close(tx *world.Tx, pos cube.Pos) {
b.Open = false
w.PlaySound(pos.Vec3Centre(), sound.BarrelClose{})
w.SetBlock(pos, b, nil)
tx.PlaySound(pos.Vec3Centre(), sound.BarrelClose{})
tx.SetBlock(pos, b, nil)
}

// AddViewer adds a viewer to the barrel, so that it is updated whenever the inventory of the barrel is changed.
func (b Barrel) AddViewer(v ContainerViewer, w *world.World, pos cube.Pos) {
func (b Barrel) AddViewer(v ContainerViewer, tx *world.Tx, pos cube.Pos) {
b.viewerMu.Lock()
defer b.viewerMu.Unlock()
if len(b.viewers) == 0 {
b.open(w, pos)
b.open(tx, pos)
}
b.viewers[v] = struct{}{}
}

// RemoveViewer removes a viewer from the barrel, so that slot updates in the inventory are no longer sent to
// it.
func (b Barrel) RemoveViewer(v ContainerViewer, w *world.World, pos cube.Pos) {
func (b Barrel) RemoveViewer(v ContainerViewer, tx *world.Tx, pos cube.Pos) {
b.viewerMu.Lock()
defer b.viewerMu.Unlock()
if len(b.viewers) == 0 {
return
}
delete(b.viewers, v)
if len(b.viewers) == 0 {
b.close(w, pos)
b.close(tx, pos)
}
}

// Activate ...
func (b Barrel) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User, _ *item.UseContext) bool {
func (b Barrel) Activate(pos cube.Pos, clickedFace cube.Face, tx *world.Tx, u item.User, ctx *item.UseContext) bool {
if opener, ok := u.(ContainerOpener); ok {
opener.OpenBlockContainer(pos)
return true
Expand All @@ -109,24 +109,24 @@ func (b Barrel) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User,
}

// UseOnBlock ...
func (b Barrel) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) (used bool) {
pos, _, used = firstReplaceable(w, pos, face, b)
func (b Barrel) UseOnBlock(pos cube.Pos, face cube.Face, clickPos mgl64.Vec3, tx *world.Tx, user item.User, ctx *item.UseContext) (used bool) {
pos, _, used = firstReplaceable(tx, pos, face, b)
if !used {
return
}
//noinspection GoAssignmentToReceiver
b = NewBarrel()
b.Facing = calculateFace(user, pos)

place(w, pos, b, user, ctx)
place(tx, pos, b, user, ctx)
return placed(ctx)
}

// BreakInfo ...
func (b Barrel) BreakInfo() BreakInfo {
return newBreakInfo(2.5, alwaysHarvestable, axeEffective, oneOf(b)).withBreakHandler(func(pos cube.Pos, w *world.World, u item.User) {
for _, i := range b.Inventory(w, pos).Clear() {
dropItem(w, i, pos.Vec3())
return newBreakInfo(2.5, alwaysHarvestable, axeEffective, oneOf(b)).withBreakHandler(func(pos cube.Pos, tx *world.Tx, u item.User) {
for _, i := range b.Inventory(tx, pos).Clear() {
dropItem(tx, i, pos.Vec3())
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion server/block/barrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Barrier struct {
}

// SideClosed ...
func (Barrier) SideClosed(cube.Pos, cube.Pos, *world.World) bool {
func (Barrier) SideClosed(cube.Pos, cube.Pos, *world.Tx) bool {
return false
}

Expand Down
6 changes: 3 additions & 3 deletions server/block/basalt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ type Basalt struct {
}

// UseOnBlock ...
func (b Basalt) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) (used bool) {
pos, face, used = firstReplaceable(w, pos, face, b)
func (b Basalt) UseOnBlock(pos cube.Pos, face cube.Face, clickPos mgl64.Vec3, tx *world.Tx, user item.User, ctx *item.UseContext) (used bool) {
pos, face, used = firstReplaceable(tx, pos, face, b)
if !used {
return
}
b.Axis = face.Axis()

place(w, pos, b, user, ctx)
place(tx, pos, b, user, ctx)
return placed(ctx)
}

Expand Down
28 changes: 14 additions & 14 deletions server/block/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (b Beacon) BreakInfo() BreakInfo {
}

// Activate manages the opening of a beacon by activating it.
func (b Beacon) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User, _ *item.UseContext) bool {
func (b Beacon) Activate(pos cube.Pos, clickedFace cube.Face, tx *world.Tx, u item.User, ctx *item.UseContext) bool {
if opener, ok := u.(ContainerOpener); ok {
opener.OpenBlockContainer(pos)
return true
Expand Down Expand Up @@ -75,7 +75,7 @@ func (b Beacon) EncodeNBT() map[string]any {
}

// SideClosed ...
func (b Beacon) SideClosed(cube.Pos, cube.Pos, *world.World) bool {
func (b Beacon) SideClosed(cube.Pos, cube.Pos, *world.Tx) bool {
return false
}

Expand All @@ -91,32 +91,32 @@ func (b Beacon) Level() int {

// Tick recalculates level, recalculates the active state of the beacon, and powers players,
// once every 80 ticks (4 seconds).
func (b Beacon) Tick(currentTick int64, pos cube.Pos, w *world.World) {
func (b Beacon) Tick(currentTick int64, pos cube.Pos, tx *world.Tx) {
if currentTick%80 == 0 {
before := b.level
// Recalculating pyramid level and powering up players in range once every 4 seconds.
b.level = b.recalculateLevel(pos, w)
b.level = b.recalculateLevel(pos, tx)
if before != b.level {
w.SetBlock(pos, b, nil)
tx.SetBlock(pos, b, nil)
}
if b.level == 0 {
return
}
if !b.obstructed(pos, w) {
b.broadcastBeaconEffects(pos, w)
if !b.obstructed(pos, tx) {
b.broadcastBeaconEffects(pos, tx)
}
}
}

// recalculateLevel recalculates the level of the beacon's pyramid and returns it. The level can be 0-4.
func (b Beacon) recalculateLevel(pos cube.Pos, w *world.World) int {
func (b Beacon) recalculateLevel(pos cube.Pos, tx *world.Tx) int {
var lvl int
iter := 1
// This loop goes over all 4 possible pyramid levels.
for y := pos.Y() - 1; y >= pos.Y()-4; y-- {
for x := pos.X() - iter; x <= pos.X()+iter; x++ {
for z := pos.Z() - iter; z <= pos.Z()+iter; z++ {
if s, ok := w.Block(cube.Pos{x, y, z}).(BeaconSource); !ok || !s.PowersBeacon() {
if s, ok := tx.Block(cube.Pos{x, y, z}).(BeaconSource); !ok || !s.PowersBeacon() {
return lvl
}
}
Expand All @@ -128,19 +128,19 @@ func (b Beacon) recalculateLevel(pos cube.Pos, w *world.World) int {
}

// obstructed determines whether the beacon is currently obstructed.
func (b Beacon) obstructed(pos cube.Pos, w *world.World) bool {
func (b Beacon) obstructed(pos cube.Pos, tx *world.Tx) bool {
// Fast obstructed light calculation.
if w.SkyLight(pos.Side(cube.FaceUp)) == 15 {
if tx.Skylight(pos.Side(cube.FaceUp)) == 15 {
return false
}
// Slow obstructed light calculation, if the fast way out didn't suffice.
return w.HighestLightBlocker(pos.X(), pos.Z()) > pos[1]
return tx.HighestLightBlocker(pos.X(), pos.Z()) > pos[1]
}

// broadcastBeaconEffects determines the entities in range which could receive the beacon's powers, and
// determines the powers (effects) that these entities could get. Afterwards, the entities in range that are
// beaconAffected get their according effect(s).
func (b Beacon) broadcastBeaconEffects(pos cube.Pos, w *world.World) {
func (b Beacon) broadcastBeaconEffects(pos cube.Pos, tx *world.Tx) {
seconds := 9 + b.level*2
if b.level == 4 {
seconds--
Expand Down Expand Up @@ -184,7 +184,7 @@ func (b Beacon) broadcastBeaconEffects(pos cube.Pos, w *world.World) {

// Finding entities in range.
r := 10 + (b.level * 10)
entitiesInRange := w.EntitiesWithin(cube.Box(
entitiesInRange := tx.EntitiesWithin(cube.Box(
float64(pos.X()-r), -math.MaxFloat64, float64(pos.Z()-r),
float64(pos.X()+r), math.MaxFloat64, float64(pos.Z()+r),
), nil)
Expand Down
24 changes: 12 additions & 12 deletions server/block/beetroot_seeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@ func (BeetrootSeeds) SameCrop(c Crop) bool {
}

// BoneMeal ...
func (b BeetrootSeeds) BoneMeal(pos cube.Pos, w *world.World) bool {
func (b BeetrootSeeds) BoneMeal(pos cube.Pos, tx *world.Tx) bool {
if b.Growth == 7 {
return false
}
if rand.Float64() < 0.75 {
b.Growth++
w.SetBlock(pos, b, nil)
tx.SetBlock(pos, b, nil)
return true
}
return false
}

// UseOnBlock ...
func (b BeetrootSeeds) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) bool {
pos, _, used := firstReplaceable(w, pos, face, b)
func (b BeetrootSeeds) UseOnBlock(pos cube.Pos, face cube.Face, clickPos mgl64.Vec3, tx *world.Tx, user item.User, ctx *item.UseContext) bool {
pos, _, used := firstReplaceable(tx, pos, face, b)
if !used {
return false
}

if _, ok := w.Block(pos.Side(cube.FaceDown)).(Farmland); !ok {
if _, ok := tx.Block(pos.Side(cube.FaceDown)).(Farmland); !ok {
return false
}

place(w, pos, b, user, ctx)
place(tx, pos, b, user, ctx)
return placed(ctx)
}

Expand All @@ -69,13 +69,13 @@ func (b BeetrootSeeds) EncodeItem() (name string, meta int16) {
}

// RandomTick ...
func (b BeetrootSeeds) RandomTick(pos cube.Pos, w *world.World, r *rand.Rand) {
if w.Light(pos) < 8 {
w.SetBlock(pos, nil, nil)
w.AddParticle(pos.Vec3Centre(), particle.BlockBreak{Block: b})
} else if b.Growth < 7 && r.Intn(3) > 0 && r.Float64() <= b.CalculateGrowthChance(pos, w) {
func (b BeetrootSeeds) RandomTick(pos cube.Pos, tx *world.Tx, r *rand.Rand) {
if tx.Light(pos) < 8 {
tx.SetBlock(pos, nil, nil)
tx.AddParticle(pos.Vec3Centre(), particle.BlockBreak{Block: b})
} else if b.Growth < 7 && r.Intn(3) > 0 && r.Float64() <= b.CalculateGrowthChance(pos, tx) {
b.Growth++
w.SetBlock(pos, b, nil)
tx.SetBlock(pos, b, nil)
}
}

Expand Down
Loading

0 comments on commit a13eb38

Please sign in to comment.