Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export functions which are important when implementing custom blocks #830

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/block/amethyst.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Amethyst struct {

// BreakInfo ...
func (a Amethyst) BreakInfo() BreakInfo {
return newBreakInfo(1.5, pickaxeHarvestable, pickaxeHarvestable, oneOf(a))
return NewBreakInfo(1.5, PickaxeHarvestable, PickaxeHarvestable, OneOf(a))
}

// EncodeItem ...
Expand Down
4 changes: 2 additions & 2 deletions server/block/ancient_debris.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type AncientDebris struct {

// BreakInfo ...
func (a AncientDebris) BreakInfo() BreakInfo {
return newBreakInfo(30, func(t item.Tool) bool {
return NewBreakInfo(30, func(t item.Tool) bool {
return t.ToolType() == item.TypePickaxe && t.HarvestLevel() >= item.ToolTierDiamond.HarvestLevel
}, pickaxeEffective, oneOf(a)).withBlastResistance(3600)
}, PickaxeEffective, OneOf(a)).withBlastResistance(3600)
}

// SmeltInfo ...
Expand Down
8 changes: 4 additions & 4 deletions server/block/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (a Anvil) Model() world.BlockModel {

// BreakInfo ...
func (a Anvil) BreakInfo() BreakInfo {
return newBreakInfo(5, pickaxeHarvestable, pickaxeEffective, oneOf(a)).withBlastResistance(6000)
return NewBreakInfo(5, PickaxeHarvestable, PickaxeEffective, OneOf(a)).withBlastResistance(6000)
}

// Activate ...
Expand All @@ -41,13 +41,13 @@ 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)
pos, _, used = FirstReplaceable(w, pos, face, a)
if !used {
return
}
a.Facing = user.Rotation().Direction().RotateRight()
place(w, pos, a, user, ctx)
return placed(ctx)
Place(w, pos, a, user, ctx)
return Placed(ctx)
}

// NeighbourUpdateTick ...
Expand Down
10 changes: 5 additions & 5 deletions server/block/banner.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (Banner) MaxCount() int {

// BreakInfo ...
func (b Banner) BreakInfo() BreakInfo {
return newBreakInfo(1, alwaysHarvestable, axeEffective, oneOf(b))
return NewBreakInfo(1, AlwaysHarvestable, AxeEffective, OneOf(b))
}

// FuelInfo ...
Expand All @@ -42,19 +42,19 @@ 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)
pos, face, used = FirstReplaceable(w, 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(w, pos, b, user, ctx)
return
}
b.Attach = WallAttachment(face.Direction())
place(w, pos, b, user, ctx)
return placed(ctx)
Place(w, pos, b, user, ctx)
return Placed(ctx)
}

// NeighbourUpdateTick ...
Expand Down
8 changes: 4 additions & 4 deletions server/block/barrel.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,21 @@ 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)
pos, _, used = FirstReplaceable(w, pos, face, b)
if !used {
return
}
//noinspection GoAssignmentToReceiver
b = NewBarrel()
b.Facing = calculateFace(user, pos)

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

// BreakInfo ...
func (b Barrel) BreakInfo() BreakInfo {
return newBreakInfo(2.5, alwaysHarvestable, axeEffective, oneOf(b))
return NewBreakInfo(2.5, AlwaysHarvestable, AxeEffective, OneOf(b))
}

// FlammabilityInfo ...
Expand Down
8 changes: 4 additions & 4 deletions server/block/basalt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ 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)
pos, face, used = FirstReplaceable(w, pos, face, b)
if !used {
return
}
b.Axis = face.Axis()

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

// BreakInfo ...
func (b Basalt) BreakInfo() BreakInfo {
return newBreakInfo(1.25, pickaxeHarvestable, pickaxeEffective, oneOf(b)).withBlastResistance(21)
return NewBreakInfo(1.25, PickaxeHarvestable, PickaxeEffective, OneOf(b)).withBlastResistance(21)
}

// EncodeItem ...
Expand Down
2 changes: 1 addition & 1 deletion server/block/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type BeaconSource interface {

// BreakInfo ...
func (b Beacon) BreakInfo() BreakInfo {
return newBreakInfo(3, alwaysHarvestable, nothingEffective, oneOf(b))
return NewBreakInfo(3, AlwaysHarvestable, NothingEffective, OneOf(b))
}

// Activate manages the opening of a beacon by activating it.
Expand Down
8 changes: 4 additions & 4 deletions server/block/beetroot_seeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (b BeetrootSeeds) BoneMeal(pos cube.Pos, w *world.World) bool {

// 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)
pos, _, used := FirstReplaceable(w, pos, face, b)
if !used {
return false
}
Expand All @@ -44,13 +44,13 @@ func (b BeetrootSeeds) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w
return false
}

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

// BreakInfo ...
func (b BeetrootSeeds) BreakInfo() BreakInfo {
return newBreakInfo(0, alwaysHarvestable, nothingEffective, func(item.Tool, []item.Enchantment) []item.Stack {
return NewBreakInfo(0, AlwaysHarvestable, NothingEffective, func(item.Tool, []item.Enchantment) []item.Stack {
if b.Growth < 7 {
return []item.Stack{item.NewStack(b, 1)}
}
Expand Down
4 changes: 2 additions & 2 deletions server/block/blackstone.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Blackstone struct {

// BreakInfo ...
func (b Blackstone) BreakInfo() BreakInfo {
drops := oneOf(b)
drops := OneOf(b)
if b.Type == GildedBlackstone() {
drops = func(item.Tool, []item.Enchantment) []item.Stack {
if rand.Float64() < 0.1 {
Expand All @@ -27,7 +27,7 @@ func (b Blackstone) BreakInfo() BreakInfo {
return []item.Stack{item.NewStack(b, 1)}
}
}
return newBreakInfo(1.5, pickaxeHarvestable, pickaxeEffective, drops).withBlastResistance(30)
return NewBreakInfo(1.5, PickaxeHarvestable, PickaxeEffective, drops).withBlastResistance(30)
}

// EncodeItem ...
Expand Down
8 changes: 4 additions & 4 deletions server/block/blast_furnace.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ func (b BlastFurnace) EncodeBlock() (name string, properties map[string]interfac

// UseOnBlock ...
func (b BlastFurnace) 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)
pos, _, used := FirstReplaceable(w, pos, face, b)
if !used {
return false
}

place(w, pos, NewBlastFurnace(user.Rotation().Direction().Face().Opposite()), user, ctx)
return placed(ctx)
Place(w, pos, NewBlastFurnace(user.Rotation().Direction().Face().Opposite()), user, ctx)
return Placed(ctx)
}

// BreakInfo ...
func (b BlastFurnace) BreakInfo() BreakInfo {
xp := b.Experience()
return newBreakInfo(3.5, alwaysHarvestable, pickaxeEffective, oneOf(b)).withXPDropRange(xp, xp)
return NewBreakInfo(3.5, AlwaysHarvestable, PickaxeEffective, OneOf(b)).withXPDropRange(xp, xp)
}

// Activate ...
Expand Down
24 changes: 12 additions & 12 deletions server/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type LightDiffuser interface {
// Replaceable represents a block that may be replaced by another block automatically. An example is grass,
// which may be replaced by clicking it with another block.
type Replaceable interface {
// ReplaceableBy returns a bool which indicates if the block is replaceableWith by another block.
// ReplaceableBy returns a bool which indicates if the block is ReplaceableWith by another block.
ReplaceableBy(b world.Block) bool
}

Expand Down Expand Up @@ -117,8 +117,8 @@ func abs(x int) int {
return -x
}

// replaceableWith checks if the block at the position passed is replaceable with the block passed.
func replaceableWith(w *world.World, pos cube.Pos, with world.Block) bool {
// ReplaceableWith checks if the block at the position passed is replaceable with the block passed.
func ReplaceableWith(w *world.World, pos cube.Pos, with world.Block) bool {
if pos.OutOfBounds(w.Range()) {
return false
}
Expand All @@ -129,25 +129,25 @@ func replaceableWith(w *world.World, pos cube.Pos, with world.Block) bool {
return false
}

// firstReplaceable finds the first replaceable block position eligible to have a block placed on it after
// FirstReplaceable finds the first replaceable block position eligible to have a block placed on it after
// clicking on the position and face passed.
// If none can be found, the bool returned is false.
func firstReplaceable(w *world.World, pos cube.Pos, face cube.Face, with world.Block) (cube.Pos, cube.Face, bool) {
if replaceableWith(w, pos, with) {
// A replaceableWith block was clicked, so we can replace it. This will then be assumed to be placed on
func FirstReplaceable(w *world.World, pos cube.Pos, face cube.Face, with world.Block) (cube.Pos, cube.Face, bool) {
if ReplaceableWith(w, pos, with) {
// A ReplaceableWith block was clicked, so we can replace it. This will then be assumed to be placed on
// the top face. (Torches, for example, will get attached to the floor when clicking tall grass.)
return pos, cube.FaceUp, true
}
side := pos.Side(face)
if replaceableWith(w, side, with) {
if ReplaceableWith(w, side, with) {
return side, face, true
}
return pos, face, false
}

// place places the block passed at the position passed. If the user implements the block.Placer interface, it
// Place places the block passed at the position passed. If the user implements the block.Placer interface, it
// will use its PlaceBlock method. If not, the block is placed without interaction from the user.
func place(w *world.World, pos cube.Pos, b world.Block, user item.User, ctx *item.UseContext) {
func Place(w *world.World, pos cube.Pos, b world.Block, user item.User, ctx *item.UseContext) {
if placer, ok := user.(Placer); ok {
placer.PlaceBlock(pos, b, ctx)
return
Expand All @@ -172,8 +172,8 @@ func horizontalDirection(d cube.Direction) cube.Direction {
panic("invalid direction")
}

// placed checks if an item was placed with the use context passed.
func placed(ctx *item.UseContext) bool {
// Placed checks if an item was placed with the use context passed.
func Placed(ctx *item.UseContext) bool {
return ctx.CountSub > 0
}

Expand Down
2 changes: 1 addition & 1 deletion server/block/blue_ice.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type BlueIce struct {

// BreakInfo ...
func (b BlueIce) BreakInfo() BreakInfo {
return newBreakInfo(2.8, alwaysHarvestable, pickaxeEffective, silkTouchOnlyDrop(b))
return NewBreakInfo(2.8, AlwaysHarvestable, PickaxeEffective, SilkTouchOnlyDrop(b))
}

// Friction ...
Expand Down
8 changes: 4 additions & 4 deletions server/block/bone.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ func (b Bone) Instrument() sound.Instrument {

// UseOnBlock handles the rotational placing of bone blocks.
func (b Bone) 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)
pos, face, used = FirstReplaceable(w, pos, face, b)
if !used {
return
}
b.Axis = face.Axis()

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

// BreakInfo ...
func (b Bone) BreakInfo() BreakInfo {
return newBreakInfo(2, pickaxeHarvestable, pickaxeEffective, oneOf(b))
return NewBreakInfo(2, PickaxeHarvestable, PickaxeEffective, OneOf(b))
}

// EncodeItem ...
Expand Down
2 changes: 1 addition & 1 deletion server/block/bookshelf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Bookshelf struct {

// BreakInfo ...
func (b Bookshelf) BreakInfo() BreakInfo {
return newBreakInfo(1.5, alwaysHarvestable, axeEffective, silkTouchDrop(item.NewStack(item.Book{}, 3), item.NewStack(b, 1)))
return NewBreakInfo(1.5, AlwaysHarvestable, AxeEffective, SilkTouchDrop(item.NewStack(item.Book{}, 3), item.NewStack(b, 1)))
}

// FlammabilityInfo ...
Expand Down
Loading
Loading