Skip to content

Commit

Permalink
player/handler.go: HandleHurt: Add immune bool that can be used to …
Browse files Browse the repository at this point in the history
…easily disable 'switching' in pvp.
  • Loading branch information
Sandertv committed Nov 27, 2024
1 parent 5917794 commit d14f89f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
77 changes: 40 additions & 37 deletions server/player/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ type Handler interface {
// HandleHurt handles the player being hurt by any damage source. ctx.Cancel() may be called to cancel the
// damage being dealt to the player.
// The damage dealt to the player may be changed by assigning to *damage.
// *damage is the final damage dealt to the player.
HandleHurt(ctx *Context, damage *float64, attackImmunity *time.Duration, src world.DamageSource)
// *damage is the final damage dealt to the player. Immune is set to true
// if the player was hurt during an immunity frame with higher damage than
// the original cause of the immunity frame. In this case, the damage is
// reduced but the player is still knocked back.
HandleHurt(ctx *Context, damage *float64, immune bool, attackImmunity *time.Duration, src world.DamageSource)
// HandleDeath handles the player dying to a particular damage cause.
HandleDeath(p *Player, src world.DamageSource, keepInv *bool)
// HandleRespawn handles the respawning of the player in the world. The spawn position passed may be
Expand Down Expand Up @@ -156,38 +159,38 @@ type NopHandler struct{}
// Compile time check to make sure NopHandler implements Handler.
var _ Handler = NopHandler{}

func (NopHandler) HandleItemDrop(*Context, item.Stack) {}
func (NopHandler) HandleHeldSlotChange(*Context, int, int) {}
func (NopHandler) HandleMove(*Context, mgl64.Vec3, cube.Rotation) {}
func (NopHandler) HandleJump(*Player) {}
func (NopHandler) HandleTeleport(*Context, mgl64.Vec3) {}
func (NopHandler) HandleChangeWorld(*world.World, *world.World) {}
func (NopHandler) HandleToggleSprint(*Context, bool) {}
func (NopHandler) HandleToggleSneak(*Context, bool) {}
func (NopHandler) HandleCommandExecution(*Context, cmd.Command, []string) {}
func (NopHandler) HandleTransfer(*Context, *net.UDPAddr) {}
func (NopHandler) HandleChat(*Context, *string) {}
func (NopHandler) HandleSkinChange(*Context, *skin.Skin) {}
func (NopHandler) HandleFireExtinguish(*Context, cube.Pos) {}
func (NopHandler) HandleStartBreak(*Context, cube.Pos) {}
func (NopHandler) HandleBlockBreak(*Context, cube.Pos, *[]item.Stack, *int) {}
func (NopHandler) HandleBlockPlace(*Context, cube.Pos, world.Block) {}
func (NopHandler) HandleBlockPick(*Context, cube.Pos, world.Block) {}
func (NopHandler) HandleSignEdit(*Context, cube.Pos, bool, string, string) {}
func (NopHandler) HandleLecternPageTurn(*Context, cube.Pos, int, *int) {}
func (NopHandler) HandleItemPickup(*Context, *item.Stack) {}
func (NopHandler) HandleItemUse(*Context) {}
func (NopHandler) HandleItemUseOnBlock(*Context, cube.Pos, cube.Face, mgl64.Vec3) {}
func (NopHandler) HandleItemUseOnEntity(*Context, world.Entity) {}
func (NopHandler) HandleItemConsume(*Context, item.Stack) {}
func (NopHandler) HandleItemDamage(*Context, item.Stack, int) {}
func (NopHandler) HandleAttackEntity(*Context, world.Entity, *float64, *float64, *bool) {}
func (NopHandler) HandleExperienceGain(*Context, *int) {}
func (NopHandler) HandlePunchAir(*Context) {}
func (NopHandler) HandleHurt(*Context, *float64, *time.Duration, world.DamageSource) {}
func (NopHandler) HandleHeal(*Context, *float64, world.HealingSource) {}
func (NopHandler) HandleFoodLoss(*Context, int, *int) {}
func (NopHandler) HandleDeath(*Player, world.DamageSource, *bool) {}
func (NopHandler) HandleRespawn(*Player, *mgl64.Vec3, **world.World) {}
func (NopHandler) HandleQuit(*Player) {}
func (NopHandler) HandleDiagnostics(*Player, session.Diagnostics) {}
func (NopHandler) HandleItemDrop(*Context, item.Stack) {}
func (NopHandler) HandleHeldSlotChange(*Context, int, int) {}
func (NopHandler) HandleMove(*Context, mgl64.Vec3, cube.Rotation) {}
func (NopHandler) HandleJump(*Player) {}
func (NopHandler) HandleTeleport(*Context, mgl64.Vec3) {}
func (NopHandler) HandleChangeWorld(*world.World, *world.World) {}
func (NopHandler) HandleToggleSprint(*Context, bool) {}
func (NopHandler) HandleToggleSneak(*Context, bool) {}
func (NopHandler) HandleCommandExecution(*Context, cmd.Command, []string) {}
func (NopHandler) HandleTransfer(*Context, *net.UDPAddr) {}
func (NopHandler) HandleChat(*Context, *string) {}
func (NopHandler) HandleSkinChange(*Context, *skin.Skin) {}
func (NopHandler) HandleFireExtinguish(*Context, cube.Pos) {}
func (NopHandler) HandleStartBreak(*Context, cube.Pos) {}
func (NopHandler) HandleBlockBreak(*Context, cube.Pos, *[]item.Stack, *int) {}
func (NopHandler) HandleBlockPlace(*Context, cube.Pos, world.Block) {}
func (NopHandler) HandleBlockPick(*Context, cube.Pos, world.Block) {}
func (NopHandler) HandleSignEdit(*Context, cube.Pos, bool, string, string) {}
func (NopHandler) HandleLecternPageTurn(*Context, cube.Pos, int, *int) {}
func (NopHandler) HandleItemPickup(*Context, *item.Stack) {}
func (NopHandler) HandleItemUse(*Context) {}
func (NopHandler) HandleItemUseOnBlock(*Context, cube.Pos, cube.Face, mgl64.Vec3) {}
func (NopHandler) HandleItemUseOnEntity(*Context, world.Entity) {}
func (NopHandler) HandleItemConsume(*Context, item.Stack) {}
func (NopHandler) HandleItemDamage(*Context, item.Stack, int) {}
func (NopHandler) HandleAttackEntity(*Context, world.Entity, *float64, *float64, *bool) {}
func (NopHandler) HandleExperienceGain(*Context, *int) {}
func (NopHandler) HandlePunchAir(*Context) {}
func (NopHandler) HandleHurt(*Context, *float64, bool, *time.Duration, world.DamageSource) {}
func (NopHandler) HandleHeal(*Context, *float64, world.HealingSource) {}
func (NopHandler) HandleFoodLoss(*Context, int, *int) {}
func (NopHandler) HandleDeath(*Player, world.DamageSource, *bool) {}
func (NopHandler) HandleRespawn(*Player, *mgl64.Vec3, **world.World) {}
func (NopHandler) HandleQuit(*Player) {}
func (NopHandler) HandleDiagnostics(*Player, session.Diagnostics) {}
6 changes: 4 additions & 2 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,17 @@ func (p *Player) Hurt(dmg float64, src world.DamageSource) (float64, bool) {
}
totalDamage := p.FinalDamageFrom(dmg, src)
damageLeft := totalDamage
if time.Now().Before(p.immuneUntil) {

immune := time.Now().Before(p.immuneUntil)
if immune {
if damageLeft = damageLeft - p.lastDamage; damageLeft <= 0 {
return 0, false
}
}

immunity := time.Second / 2
ctx := event.C(p)
if p.Handler().HandleHurt(ctx, &damageLeft, &immunity, src); ctx.Cancelled() {
if p.Handler().HandleHurt(ctx, &damageLeft, immune, &immunity, src); ctx.Cancelled() {
return 0, false
}
p.setAttackImmunity(immunity, totalDamage)
Expand Down

0 comments on commit d14f89f

Please sign in to comment.