diff --git a/server/block/break_info.go b/server/block/break_info.go index b1bd182cb..39500e6eb 100644 --- a/server/block/break_info.go +++ b/server/block/break_info.go @@ -136,6 +136,11 @@ var axeEffective = func(t item.Tool) bool { return t.ToolType() == item.TypeAxe } +// swordEffective is a convenience function for blocks that are effectively mined with a sword. +var swordEffective = func(t item.Tool) bool { + return t.ToolType() == item.TypeSword +} + // shearsEffective is a convenience function for blocks that are effectively mined with shears. var shearsEffective = func(t item.Tool) bool { return t.ToolType() == item.TypeShears diff --git a/server/block/cobweb.go b/server/block/cobweb.go new file mode 100644 index 000000000..075989c29 --- /dev/null +++ b/server/block/cobweb.go @@ -0,0 +1,46 @@ +package block + +import ( + "github.com/df-mc/dragonfly/server/block/cube" + "github.com/df-mc/dragonfly/server/item" + "github.com/df-mc/dragonfly/server/world" +) + +// Cobweb is a block that can slow down entity movement and negate fall damage. +type Cobweb struct { + empty +} + +// BreakInfo ... +func (c Cobweb) BreakInfo() BreakInfo { + return newBreakInfo( + 4, + alwaysHarvestable, + func(t item.Tool) bool { + return swordEffective(t) || shearsEffective(t) + }, + func(t item.Tool, e []item.Enchantment) []item.Stack { + if t.ToolType() == item.TypeShears || (t.ToolType() == item.TypeSword && hasSilkTouch(e)) { + return oneOf(c)(t, e) + } + return oneOf(String{})(t, e) + }, + ) +} + +// EntityInside ... +func (Cobweb) EntityInside(_ cube.Pos, _ *world.World, e world.Entity) { + if fallEntity, ok := e.(fallDistanceEntity); ok { + fallEntity.ResetFallDistance() + } +} + +// EncodeItem ... +func (c Cobweb) EncodeItem() (name string, meta int16) { + return "minecraft:web", 0 +} + +// EncodeBlock ... +func (c Cobweb) EncodeBlock() (string, map[string]interface{}) { + return "minecraft:web", nil +} diff --git a/server/block/hash.go b/server/block/hash.go index 4137a1d20..4525b8928 100644 --- a/server/block/hash.go +++ b/server/block/hash.go @@ -33,6 +33,7 @@ const ( hashCoal hashCoalOre hashCobblestone + hashCobweb hashCocoaBean hashConcrete hashConcretePowder @@ -156,6 +157,7 @@ const ( hashTallGrass hashTerracotta hashTorch + hashTripwire hashTuff hashWall hashWater @@ -288,6 +290,10 @@ func (c Cobblestone) Hash() uint64 { return hashCobblestone | uint64(boolByte(c.Mossy))<<8 } +func (Cobweb) Hash() uint64 { + return hashCobweb +} + func (c CocoaBean) Hash() uint64 { return hashCocoaBean | uint64(c.Facing)<<8 | uint64(c.Age)<<10 } @@ -780,6 +786,10 @@ func (t Torch) Hash() uint64 { return hashTorch | uint64(t.Facing)<<8 | uint64(t.Type.Uint8())<<11 } +func (String) Hash() uint64 { + return hashTripwire +} + func (Tuff) Hash() uint64 { return hashTuff } diff --git a/server/block/register.go b/server/block/register.go index 39de7f8ff..ad70ab664 100644 --- a/server/block/register.go +++ b/server/block/register.go @@ -26,6 +26,7 @@ func init() { world.RegisterBlock(Coal{}) world.RegisterBlock(Cobblestone{Mossy: true}) world.RegisterBlock(Cobblestone{}) + world.RegisterBlock(Cobweb{}) world.RegisterBlock(CraftingTable{}) world.RegisterBlock(DeadBush{}) world.RegisterBlock(DeepslateBricks{Cracked: true}) @@ -98,6 +99,7 @@ func init() { world.RegisterBlock(SporeBlossom{}) world.RegisterBlock(Stone{Smooth: true}) world.RegisterBlock(Stone{}) + world.RegisterBlock(String{}) world.RegisterBlock(TNT{}) world.RegisterBlock(Terracotta{}) world.RegisterBlock(Tuff{}) @@ -218,6 +220,7 @@ func init() { world.RegisterItem(Coal{}) world.RegisterItem(Cobblestone{Mossy: true}) world.RegisterItem(Cobblestone{}) + world.RegisterItem(Cobweb{}) world.RegisterItem(CocoaBean{}) world.RegisterItem(CraftingTable{}) world.RegisterItem(DeadBush{}) @@ -315,6 +318,7 @@ func init() { world.RegisterItem(Stonecutter{}) world.RegisterItem(Stone{Smooth: true}) world.RegisterItem(Stone{}) + world.RegisterItem(String{}) world.RegisterItem(SugarCane{}) world.RegisterItem(TNT{}) world.RegisterItem(Terracotta{}) diff --git a/server/block/string.go b/server/block/string.go new file mode 100644 index 000000000..b4e43ec1e --- /dev/null +++ b/server/block/string.go @@ -0,0 +1,40 @@ +package block + +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/go-gl/mathgl/mgl64" +) + +// String is an item dropped by spiders and cobwebs. When placed, it turns into a tripwire. +type String struct { + empty + transparent +} + +// UseOnBlock ... +func (s String) 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, s) + if !used { + return + } + + place(w, pos, s, user, ctx) + return placed(ctx) +} + +// BreakInfo ... +func (s String) BreakInfo() BreakInfo { + return newBreakInfo(0, alwaysHarvestable, nothingEffective, oneOf(String{})) +} + +// EncodeItem ... +func (String) EncodeItem() (name string, meta int16) { + return "minecraft:string", 0 +} + +// EncodeBlock ... +func (s String) EncodeBlock() (string, map[string]interface{}) { + return "minecraft:tripWire", map[string]interface{}{"attached_bit": uint8(0), "disarmed_bit": uint8(0), "powered_bit": uint8(0), "suspended_bit": uint8(1)} +}