Skip to content

Commit

Permalink
Resolved merge conflicts and replaced SneakingActivatable with Item.U…
Browse files Browse the repository at this point in the history
…seOnBlock.
  • Loading branch information
Sandertv committed Nov 22, 2024
1 parent 2fed28a commit cc071b6
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 222 deletions.
9 changes: 0 additions & 9 deletions server/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ type Activatable interface {
Activate(pos cube.Pos, clickedFace cube.Face, tx *world.Tx, u item.User, ctx *item.UseContext) bool
}

// SneakingActivatable represents a block that may be activated by a viewer of the world while sneaking. When
// activated, the block will execute some specific logic.
type SneakingActivatable interface {
// SneakingActivate activates the block at a specific block position while sneaking. The face clicked is
// passed, as well as the world in which the block was activated and the viewer that activated it.
// SneakingActivate returns a bool indicating if activating the block was used successfully.
SneakingActivate(pos cube.Pos, clickedFace cube.Face, w *world.World, u item.User, ctx *item.UseContext) bool
}

// Pickable represents a block that may give a different item then the block itself when picked.
type Pickable interface {
// Pick returns the item that is picked when the block is picked.
Expand Down
28 changes: 11 additions & 17 deletions server/block/brewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newBrewer() *brewer {
}

// InsertItem ...
func (b *brewer) InsertItem(h Hopper, pos cube.Pos, w *world.World) bool {
func (b *brewer) InsertItem(h Hopper, pos cube.Pos, tx *world.Tx) bool {
for sourceSlot, sourceStack := range h.inventory.Slots() {
var slot int

Expand Down Expand Up @@ -79,7 +79,7 @@ func (b *brewer) InsertItem(h Hopper, pos cube.Pos, w *world.World) bool {
}

stack := sourceStack.Grow(-sourceStack.Count() + 1)
it, _ := b.Inventory(w, pos).Item(slot)
it, _ := b.Inventory(tx, pos).Item(slot)

if !sourceStack.Comparable(it) {
// The items are not the same.
Expand All @@ -93,7 +93,7 @@ func (b *brewer) InsertItem(h Hopper, pos cube.Pos, w *world.World) bool {
stack = it.Grow(1)
}

_ = b.Inventory(w, pos).SetItem(slot, stack)
_ = b.Inventory(tx, pos).SetItem(slot, stack)
_ = h.inventory.SetItem(sourceSlot, sourceStack.Grow(-1))
return true

Expand All @@ -102,23 +102,17 @@ func (b *brewer) InsertItem(h Hopper, pos cube.Pos, w *world.World) bool {
}

// ExtractItem ...
func (b *brewer) ExtractItem(h Hopper, pos cube.Pos, w *world.World) bool {
func (b *brewer) ExtractItem(h Hopper, pos cube.Pos, tx *world.Tx) bool {
for sourceSlot, sourceStack := range b.inventory.Slots() {
if sourceStack.Empty() {
continue
}

if sourceSlot == 0 || sourceSlot == 4 {
if sourceStack.Empty() || sourceSlot == 0 || sourceSlot == 4 {
continue
}

_, err := h.inventory.AddItem(sourceStack.Grow(-sourceStack.Count() + 1))
if err != nil {
// The hopper is full.
continue
}

_ = b.Inventory(w, pos).SetItem(sourceSlot, sourceStack.Grow(-1))
_ = b.Inventory(tx, pos).SetItem(sourceSlot, sourceStack.Grow(-1))
return true
}
return false
Expand All @@ -139,20 +133,20 @@ func (b *brewer) Fuel() (fuel, maxFuel int32) {
}

// Inventory returns the inventory of the brewer.
func (b *brewer) Inventory(*world.World, cube.Pos) *inventory.Inventory {
func (b *brewer) Inventory(*world.Tx, cube.Pos) *inventory.Inventory {
return b.inventory
}

// AddViewer adds a viewer to the brewer, so that it is updated whenever the inventory of the brewer is changed.
func (b *brewer) AddViewer(v ContainerViewer, _ *world.World, _ cube.Pos) {
func (b *brewer) AddViewer(v ContainerViewer, _ *world.Tx, _ cube.Pos) {
b.mu.Lock()
defer b.mu.Unlock()
b.viewers[v] = struct{}{}
}

// RemoveViewer removes a viewer from the brewer, so that slot updates in the inventory are no longer sent to
// it.
func (b *brewer) RemoveViewer(v ContainerViewer, _ *world.World, _ cube.Pos) {
func (b *brewer) RemoveViewer(v ContainerViewer, _ *world.Tx, _ cube.Pos) {
b.mu.Lock()
defer b.mu.Unlock()
delete(b.viewers, v)
Expand All @@ -174,7 +168,7 @@ func (b *brewer) setFuel(fuel, maxFuel int32) {

// tickBrewing ticks the brewer, ensuring the necessary items exist in the brewer, and then processing all inputted
// items for the necessary duration.
func (b *brewer) tickBrewing(block string, pos cube.Pos, w *world.World) {
func (b *brewer) tickBrewing(block string, pos cube.Pos, tx *world.Tx) {
b.mu.Lock()

// Get each item in the brewer. We don't need to validate errors here since we know the bounds of the brewer.
Expand Down Expand Up @@ -229,7 +223,7 @@ func (b *brewer) tickBrewing(block string, pos cube.Pos, w *world.World) {

// Reduce the ingredient by one.
defer b.inventory.SetItem(0, ingredient.Grow(-1))
w.PlaySound(pos.Vec3Centre(), sound.PotionBrewed{})
tx.PlaySound(pos.Vec3Centre(), sound.PotionBrewed{})

// Decrement the fuel, and reset the duration.
b.fuelAmount--
Expand Down
24 changes: 12 additions & 12 deletions server/block/brewing_stand.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func (b BrewingStand) Model() world.BlockModel {
}

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

// Tick is called to check if the brewing stand should update and start or stop brewing.
func (b BrewingStand) Tick(_ int64, pos cube.Pos, w *world.World) {
func (b BrewingStand) Tick(_ int64, pos cube.Pos, tx *world.Tx) {
// Get each item in the brewing stand. We don't need to validate errors here since we know the bounds of the stand.
left, _ := b.inventory.Item(1)
middle, _ := b.inventory.Item(2)
Expand All @@ -51,32 +51,32 @@ func (b BrewingStand) Tick(_ int64, pos cube.Pos, w *world.World) {
displayLeft, displayMiddle, displayRight := b.LeftSlot, b.MiddleSlot, b.RightSlot
b.LeftSlot, b.MiddleSlot, b.RightSlot = !left.Empty(), !middle.Empty(), !right.Empty()
if b.LeftSlot != displayLeft || b.MiddleSlot != displayMiddle || b.RightSlot != displayRight {
w.SetBlock(pos, b, nil)
tx.SetBlock(pos, b, nil)
}

// Tick brewing.
b.tickBrewing("brewing_stand", pos, w)
b.tickBrewing("brewing_stand", pos, tx)
}

// Activate ...
func (b BrewingStand) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User, _ *item.UseContext) bool {
func (b BrewingStand) Activate(pos cube.Pos, _ cube.Face, tx *world.Tx, u item.User, _ *item.UseContext) bool {
if opener, ok := u.(ContainerOpener); ok {
opener.OpenBlockContainer(pos)
opener.OpenBlockContainer(pos, tx)
return true
}
return false
}

// UseOnBlock ...
func (b BrewingStand) 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 BrewingStand) UseOnBlock(pos cube.Pos, face cube.Face, _ 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 = NewBrewingStand()
place(w, pos, b, user, ctx)
place(tx, pos, b, user, ctx)
return placed(ctx)
}

Expand Down Expand Up @@ -114,9 +114,9 @@ func (b BrewingStand) DecodeNBT(data map[string]any) any {

// BreakInfo ...
func (b BrewingStand) BreakInfo() BreakInfo {
return newBreakInfo(0.5, alwaysHarvestable, pickaxeEffective, 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.Vec3Centre())
return newBreakInfo(0.5, alwaysHarvestable, pickaxeEffective, 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.Vec3Centre())
}
})
}
Expand Down
47 changes: 21 additions & 26 deletions server/block/copper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
)
Expand All @@ -21,6 +22,17 @@ type Copper struct {
Waxed bool
}

func (c Copper) Strip() (world.Block, world.Sound, bool) {
if c.Waxed {
c.Waxed = false
return c, sound.WaxRemoved{}, true
} else if ot, ok := c.Oxidation.Decrease(); ok {
c.Oxidation = ot
return c, sound.CopperScraped{}, true
}
return c, nil, false
}

// BreakInfo ...
func (c Copper) BreakInfo() BreakInfo {
return newBreakInfo(3, func(t item.Tool) bool {
Expand All @@ -30,11 +42,9 @@ func (c Copper) BreakInfo() BreakInfo {

// Wax waxes the copper block to stop it from oxidising further.
func (c Copper) Wax(cube.Pos, mgl64.Vec3) (world.Block, bool) {
if c.Waxed {
return c, false
}
before := c.Waxed
c.Waxed = true
return c, true
return c, before != true
}

func (c Copper) CanOxidate() bool {
Expand All @@ -45,40 +55,25 @@ func (c Copper) OxidationLevel() OxidationType {
return c.Oxidation
}

func (c Copper) WithOxidationLevel(o OxidationType) Oxidizable {
func (c Copper) WithOxidationLevel(o OxidationType) Oxidisable {
c.Oxidation = o
return c
}

func (c Copper) Activate(pos cube.Pos, _ cube.Face, w *world.World, user item.User, _ *item.UseContext) bool {
var ok bool
c.Oxidation, c.Waxed, ok = activateOxidizable(pos, w, user, c.Oxidation, c.Waxed)
if ok {
w.SetBlock(pos, c, nil)
return true
}
return false
}

func (c Copper) SneakingActivate(pos cube.Pos, face cube.Face, w *world.World, user item.User, ctx *item.UseContext) bool {
// Sneaking should still trigger axe functionality.
return c.Activate(pos, face, w, user, ctx)
}

func (c Copper) RandomTick(pos cube.Pos, w *world.World, r *rand.Rand) {
attemptOxidation(pos, w, r, c)
func (c Copper) RandomTick(pos cube.Pos, tx *world.Tx, r *rand.Rand) {
attemptOxidation(pos, tx, r, c)
}

// EncodeItem ...
func (c Copper) EncodeItem() (name string, meta int16) {
if c.Type == NormalCopper() && c.Oxidation == NormalOxidation() && !c.Waxed {
if c.Type == NormalCopper() && c.Oxidation == UnoxidisedOxidation() && !c.Waxed {
return "minecraft:copper_block", 0
}
name = "copper"
if c.Type != NormalCopper() {
name = c.Type.String() + "_" + name
}
if c.Oxidation != NormalOxidation() {
if c.Oxidation != UnoxidisedOxidation() {
name = c.Oxidation.String() + "_" + name
}
if c.Waxed {
Expand All @@ -89,14 +84,14 @@ func (c Copper) EncodeItem() (name string, meta int16) {

// EncodeBlock ...
func (c Copper) EncodeBlock() (string, map[string]any) {
if c.Type == NormalCopper() && c.Oxidation == NormalOxidation() && !c.Waxed {
if c.Type == NormalCopper() && c.Oxidation == UnoxidisedOxidation() && !c.Waxed {
return "minecraft:copper_block", nil
}
name := "copper"
if c.Type != NormalCopper() {
name = c.Type.String() + "_" + name
}
if c.Oxidation != NormalOxidation() {
if c.Oxidation != UnoxidisedOxidation() {
name = c.Oxidation.String() + "_" + name
}
if c.Waxed {
Expand Down
Loading

0 comments on commit cc071b6

Please sign in to comment.