Skip to content

Commit

Permalink
requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xNatsuri committed Aug 18, 2024
1 parent fab899b commit d989880
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
30 changes: 25 additions & 5 deletions server/block/hopper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ type Hopper struct {

// LastTick is the last world tick that the hopper was ticked.
LastTick int64
// TransferCooldown is the duration until the hopper can transfer items again.
// TransferCooldown is the duration in ticks until the hopper can transfer items again.
TransferCooldown int64
// CollectCooldown is the duration until the hopper can collect items again.
// CollectCooldown is the duration in ticks until the hopper can collect items again.
CollectCooldown int64

inventory *inventory.Inventory
Expand Down Expand Up @@ -77,7 +77,7 @@ func (h Hopper) BreakInfo() BreakInfo {
}

// Inventory returns the inventory of the hopper.
func (h Hopper) Inventory(w *world.World, pos cube.Pos) *inventory.Inventory {
func (h Hopper) Inventory(*world.World, cube.Pos) *inventory.Inventory {
return h.inventory
}

Expand Down Expand Up @@ -134,10 +134,17 @@ func (h Hopper) Tick(currentTick int64, pos cube.Pos, w *world.World) {
h.CollectCooldown--
h.LastTick = currentTick

if !h.Powered && h.TransferCooldown < 0 {
if !h.Powered && h.TransferCooldown <= 0 {
inserted := h.insertItem(pos, w)
extracted := h.extractItem(pos, w)
if inserted || extracted {
if h.Facing == cube.FaceDown {
if hopper, ok := w.Block(pos.Side(h.Facing)).(Hopper); ok {
hopper.TransferCooldown = 8
w.SetBlock(pos.Side(h.Facing), hopper, nil)
}
}

h.TransferCooldown = 8
}
}
Expand All @@ -157,6 +164,12 @@ func (h Hopper) insertItem(pos cube.Pos, w *world.World) bool {
return e.InsertItem(h, pos.Side(h.Facing), w)
}

if hopper, ok := dest.(Hopper); ok {
if hopper.TransferCooldown > 0 {
return false
}
}

if container, ok := dest.(Container); ok {
for sourceSlot, sourceStack := range h.inventory.Slots() {
if sourceStack.Empty() {
Expand Down Expand Up @@ -191,6 +204,12 @@ func (h Hopper) extractItem(pos cube.Pos, w *world.World) bool {
return e.ExtractItem(h, pos, w)
}

if hopper, ok := origin.(Hopper); ok {
if hopper.TransferCooldown > 0 {
return false
}
}

if containerOrigin, ok := origin.(Container); ok {
for slot, stack := range containerOrigin.Inventory(w, originPos).Slots() {
if stack.Empty() {
Expand All @@ -201,8 +220,9 @@ func (h Hopper) extractItem(pos cube.Pos, w *world.World) bool {
_, err := h.inventory.AddItem(stack.Grow(-stack.Count() + 1))
if err != nil {
// The hopper is full.
return false
continue
}

_ = containerOrigin.Inventory(w, originPos).SetItem(slot, stack.Grow(-1))
return true
}
Expand Down
3 changes: 2 additions & 1 deletion server/block/smelter.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *smelter) ExtractItem(h Hopper, pos cube.Pos, w *world.World) bool {

if sourceSlot == 1 {
fuel, ok := sourceStack.Item().(item.Fuel)
if !ok || fuel.FuelInfo().Duration.Seconds() != 0 {
if ok && fuel.FuelInfo().Duration.Seconds() != 0 {
continue
}
}
Expand All @@ -104,6 +104,7 @@ func (s *smelter) ExtractItem(h Hopper, pos cube.Pos, w *world.World) bool {
// The hopper is full.
continue
}

_ = s.Inventory(w, pos).SetItem(sourceSlot, sourceStack.Grow(-1))
return true
}
Expand Down
4 changes: 2 additions & 2 deletions server/entity/item_behaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func (i *ItemBehaviour) Tick(e *Ent) *Movement {

bl, ok := w.Block(blockPos).(block.Hopper)
if ok && !bl.Powered && bl.CollectCooldown <= 0 {
addedCount, err := bl.Inventory(w, pos).AddItem(i.i)
addedCount, err := bl.Inventory(w, blockPos).AddItem(i.i)
if err != nil {
if addedCount == 0 {
return i.passive.Tick(e)
}

// This is only reached if part of the item stack was collected into the hopper.
w.AddEntity(NewItem(i.Item().Grow(addedCount-i.i.Count()), pos.Vec3Centre()))
w.AddEntity(NewItem(i.Item().Grow(-addedCount), pos.Vec3Centre()))
}

_ = e.Close()
Expand Down

0 comments on commit d989880

Please sign in to comment.