Skip to content

Commit 903f0b0

Browse files
committed
Refactored templates and improved defensive programming
Added: - Nil checks in Item.GetLookData() to handle nil items - Nil checks in Mob.GetLookData() to handle nil mobs - Division-by-zero protection for mob health calculations Changed: - Consolidated equipment display logic in mob templates from 3 blocks to 1 - Used template variables to control equipment slot visibility - Reduced code duplication in both standard and screenreader templates Removed: - TextFormats to UserInterface.Formats migration logic - Redundant equipment display blocks in mob templates Breaking Changes: - Users with custom config files must manually update TextFormats to UserInterface.Formats structure
1 parent 7d7abe1 commit 903f0b0

File tree

5 files changed

+115
-162
lines changed

5 files changed

+115
-162
lines changed

_datafiles/world/default/templates/descriptions/look-mob.screenreader.template

Lines changed: 42 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{/* Screenreader-friendly template for looking at mobs */}}
2-
{{ .Character.Name }}{{ if .IsCharmed }} (charmed by {{ .CharmedBy }}){{ end }}{{ if .IsShop }} (merchant){{ end }}
2+
Name: {{ .Character.Name }}{{ if .IsCharmed }} (charmed by {{ .CharmedBy }}){{ end }}{{ if .IsShop }} (merchant){{ end }}
33

44
Description:
55
{{ .Character.GetDescription }}
@@ -17,81 +17,52 @@ Health Status: {{ .HealthStatus }}
1717
{{- if and (not .Equipment.Ring.IsDisabled) (gt .Equipment.Ring.ItemId 0) }}{{ $hasEquipment = true }}{{ end }}
1818
{{- if and (not .Equipment.Legs.IsDisabled) (gt .Equipment.Legs.ItemId 0) }}{{ $hasEquipment = true }}{{ end }}
1919
{{- if and (not .Equipment.Feet.IsDisabled) (gt .Equipment.Feet.ItemId 0) }}{{ $hasEquipment = true }}{{ end }}
20-
{{- if not $hasEquipment }}
21-
{{- if not $showEmpty }}
20+
{{- if and (not $hasEquipment) (not $showEmpty) }}
2221

2322
{{ .Character.Name }} is not wearing anything.
24-
{{- else }}
23+
{{- else if or $hasEquipment $showEmpty }}
2524

2625
Equipment:
27-
{{ if or (and (not .Equipment.Weapon.IsDisabled) (gt .Equipment.Weapon.ItemId 0)) (and $showEmpty (not .Equipment.Weapon.IsDisabled)) }}Weapon: {{ if gt .Equipment.Weapon.ItemId 0 }}{{ .Equipment.Weapon.NameSimple }}{{ else }}nothing{{ end }}
28-
{{ end -}}
29-
{{- if or (and (not .Equipment.Offhand.IsDisabled) (gt .Equipment.Offhand.ItemId 0)) (and $showEmpty (not .Equipment.Offhand.IsDisabled)) }}Offhand: {{ if gt .Equipment.Offhand.ItemId 0 }}{{ .Equipment.Offhand.NameSimple }}{{ else }}nothing{{ end }}
30-
{{ end -}}
31-
{{- if or (and (not .Equipment.Head.IsDisabled) (gt .Equipment.Head.ItemId 0)) (and $showEmpty (not .Equipment.Head.IsDisabled)) }}Head: {{ if gt .Equipment.Head.ItemId 0 }}{{ .Equipment.Head.NameSimple }}{{ else }}nothing{{ end }}
32-
{{ end -}}
33-
{{- if or (and (not .Equipment.Neck.IsDisabled) (gt .Equipment.Neck.ItemId 0)) (and $showEmpty (not .Equipment.Neck.IsDisabled)) }}Neck: {{ if gt .Equipment.Neck.ItemId 0 }}{{ .Equipment.Neck.NameSimple }}{{ else }}nothing{{ end }}
34-
{{ end -}}
35-
{{- if or (and (not .Equipment.Body.IsDisabled) (gt .Equipment.Body.ItemId 0)) (and $showEmpty (not .Equipment.Body.IsDisabled)) }}Body: {{ if gt .Equipment.Body.ItemId 0 }}{{ .Equipment.Body.NameSimple }}{{ else }}nothing{{ end }}
36-
{{ end -}}
37-
{{- if or (and (not .Equipment.Belt.IsDisabled) (gt .Equipment.Belt.ItemId 0)) (and $showEmpty (not .Equipment.Belt.IsDisabled)) }}Belt: {{ if gt .Equipment.Belt.ItemId 0 }}{{ .Equipment.Belt.NameSimple }}{{ else }}nothing{{ end }}
38-
{{ end -}}
39-
{{- if or (and (not .Equipment.Gloves.IsDisabled) (gt .Equipment.Gloves.ItemId 0)) (and $showEmpty (not .Equipment.Gloves.IsDisabled)) }}Gloves: {{ if gt .Equipment.Gloves.ItemId 0 }}{{ .Equipment.Gloves.NameSimple }}{{ else }}nothing{{ end }}
40-
{{ end -}}
41-
{{- if or (and (not .Equipment.Ring.IsDisabled) (gt .Equipment.Ring.ItemId 0)) (and $showEmpty (not .Equipment.Ring.IsDisabled)) }}Ring: {{ if gt .Equipment.Ring.ItemId 0 }}{{ .Equipment.Ring.NameSimple }}{{ else }}nothing{{ end }}
42-
{{ end -}}
43-
{{- if or (and (not .Equipment.Legs.IsDisabled) (gt .Equipment.Legs.ItemId 0)) (and $showEmpty (not .Equipment.Legs.IsDisabled)) }}Legs: {{ if gt .Equipment.Legs.ItemId 0 }}{{ .Equipment.Legs.NameSimple }}{{ else }}nothing{{ end }}
44-
{{ end -}}
45-
{{- if or (and (not .Equipment.Feet.IsDisabled) (gt .Equipment.Feet.ItemId 0)) (and $showEmpty (not .Equipment.Feet.IsDisabled)) }}Feet: {{ if gt .Equipment.Feet.ItemId 0 }}{{ .Equipment.Feet.NameSimple }}{{ else }}nothing{{ end }}
46-
{{ end -}}
26+
{{- $showItem := false }}
27+
{{- $showItem = or (and (not .Equipment.Weapon.IsDisabled) (gt .Equipment.Weapon.ItemId 0)) (and $showEmpty (not .Equipment.Weapon.IsDisabled)) }}
28+
{{- if $showItem }}
29+
Weapon: {{ if gt .Equipment.Weapon.ItemId 0 }}{{ .Equipment.Weapon.NameSimple }}{{ else }}nothing{{ end }}
4730
{{- end }}
48-
{{- else }}
49-
{{- if $showEmpty }}
50-
51-
Equipment:
52-
{{ if or (and (not .Equipment.Weapon.IsDisabled) (gt .Equipment.Weapon.ItemId 0)) (and $showEmpty (not .Equipment.Weapon.IsDisabled)) }}Weapon: {{ if gt .Equipment.Weapon.ItemId 0 }}{{ .Equipment.Weapon.NameSimple }}{{ else }}nothing{{ end }}
53-
{{ end -}}
54-
{{- if or (and (not .Equipment.Offhand.IsDisabled) (gt .Equipment.Offhand.ItemId 0)) (and $showEmpty (not .Equipment.Offhand.IsDisabled)) }}Offhand: {{ if gt .Equipment.Offhand.ItemId 0 }}{{ .Equipment.Offhand.NameSimple }}{{ else }}nothing{{ end }}
55-
{{ end -}}
56-
{{- if or (and (not .Equipment.Head.IsDisabled) (gt .Equipment.Head.ItemId 0)) (and $showEmpty (not .Equipment.Head.IsDisabled)) }}Head: {{ if gt .Equipment.Head.ItemId 0 }}{{ .Equipment.Head.NameSimple }}{{ else }}nothing{{ end }}
57-
{{ end -}}
58-
{{- if or (and (not .Equipment.Neck.IsDisabled) (gt .Equipment.Neck.ItemId 0)) (and $showEmpty (not .Equipment.Neck.IsDisabled)) }}Neck: {{ if gt .Equipment.Neck.ItemId 0 }}{{ .Equipment.Neck.NameSimple }}{{ else }}nothing{{ end }}
59-
{{ end -}}
60-
{{- if or (and (not .Equipment.Body.IsDisabled) (gt .Equipment.Body.ItemId 0)) (and $showEmpty (not .Equipment.Body.IsDisabled)) }}Body: {{ if gt .Equipment.Body.ItemId 0 }}{{ .Equipment.Body.NameSimple }}{{ else }}nothing{{ end }}
61-
{{ end -}}
62-
{{- if or (and (not .Equipment.Belt.IsDisabled) (gt .Equipment.Belt.ItemId 0)) (and $showEmpty (not .Equipment.Belt.IsDisabled)) }}Belt: {{ if gt .Equipment.Belt.ItemId 0 }}{{ .Equipment.Belt.NameSimple }}{{ else }}nothing{{ end }}
63-
{{ end -}}
64-
{{- if or (and (not .Equipment.Gloves.IsDisabled) (gt .Equipment.Gloves.ItemId 0)) (and $showEmpty (not .Equipment.Gloves.IsDisabled)) }}Gloves: {{ if gt .Equipment.Gloves.ItemId 0 }}{{ .Equipment.Gloves.NameSimple }}{{ else }}nothing{{ end }}
65-
{{ end -}}
66-
{{- if or (and (not .Equipment.Ring.IsDisabled) (gt .Equipment.Ring.ItemId 0)) (and $showEmpty (not .Equipment.Ring.IsDisabled)) }}Ring: {{ if gt .Equipment.Ring.ItemId 0 }}{{ .Equipment.Ring.NameSimple }}{{ else }}nothing{{ end }}
67-
{{ end -}}
68-
{{- if or (and (not .Equipment.Legs.IsDisabled) (gt .Equipment.Legs.ItemId 0)) (and $showEmpty (not .Equipment.Legs.IsDisabled)) }}Legs: {{ if gt .Equipment.Legs.ItemId 0 }}{{ .Equipment.Legs.NameSimple }}{{ else }}nothing{{ end }}
69-
{{ end -}}
70-
{{- if or (and (not .Equipment.Feet.IsDisabled) (gt .Equipment.Feet.ItemId 0)) (and $showEmpty (not .Equipment.Feet.IsDisabled)) }}Feet: {{ if gt .Equipment.Feet.ItemId 0 }}{{ .Equipment.Feet.NameSimple }}{{ else }}nothing{{ end }}
71-
{{ end -}}
72-
{{- else }}
73-
74-
Equipment:
75-
{{ if and (not .Equipment.Weapon.IsDisabled) (gt .Equipment.Weapon.ItemId 0) }}Weapon: {{ .Equipment.Weapon.NameSimple }}
76-
{{ end -}}
77-
{{- if and (not .Equipment.Offhand.IsDisabled) (gt .Equipment.Offhand.ItemId 0) }}Offhand: {{ .Equipment.Offhand.NameSimple }}
78-
{{ end -}}
79-
{{- if and (not .Equipment.Head.IsDisabled) (gt .Equipment.Head.ItemId 0) }}Head: {{ .Equipment.Head.NameSimple }}
80-
{{ end -}}
81-
{{- if and (not .Equipment.Neck.IsDisabled) (gt .Equipment.Neck.ItemId 0) }}Neck: {{ .Equipment.Neck.NameSimple }}
82-
{{ end -}}
83-
{{- if and (not .Equipment.Body.IsDisabled) (gt .Equipment.Body.ItemId 0) }}Body: {{ .Equipment.Body.NameSimple }}
84-
{{ end -}}
85-
{{- if and (not .Equipment.Belt.IsDisabled) (gt .Equipment.Belt.ItemId 0) }}Belt: {{ .Equipment.Belt.NameSimple }}
86-
{{ end -}}
87-
{{- if and (not .Equipment.Gloves.IsDisabled) (gt .Equipment.Gloves.ItemId 0) }}Gloves: {{ .Equipment.Gloves.NameSimple }}
88-
{{ end -}}
89-
{{- if and (not .Equipment.Ring.IsDisabled) (gt .Equipment.Ring.ItemId 0) }}Ring: {{ .Equipment.Ring.NameSimple }}
90-
{{ end -}}
91-
{{- if and (not .Equipment.Legs.IsDisabled) (gt .Equipment.Legs.ItemId 0) }}Legs: {{ .Equipment.Legs.NameSimple }}
92-
{{ end -}}
93-
{{- if and (not .Equipment.Feet.IsDisabled) (gt .Equipment.Feet.ItemId 0) }}Feet: {{ .Equipment.Feet.NameSimple }}
94-
{{ end -}}
31+
{{- $showItem = or (and (not .Equipment.Offhand.IsDisabled) (gt .Equipment.Offhand.ItemId 0)) (and $showEmpty (not .Equipment.Offhand.IsDisabled)) }}
32+
{{- if $showItem }}
33+
Offhand: {{ if gt .Equipment.Offhand.ItemId 0 }}{{ .Equipment.Offhand.NameSimple }}{{ else }}nothing{{ end }}
34+
{{- end }}
35+
{{- $showItem = or (and (not .Equipment.Head.IsDisabled) (gt .Equipment.Head.ItemId 0)) (and $showEmpty (not .Equipment.Head.IsDisabled)) }}
36+
{{- if $showItem }}
37+
Head: {{ if gt .Equipment.Head.ItemId 0 }}{{ .Equipment.Head.NameSimple }}{{ else }}nothing{{ end }}
38+
{{- end }}
39+
{{- $showItem = or (and (not .Equipment.Neck.IsDisabled) (gt .Equipment.Neck.ItemId 0)) (and $showEmpty (not .Equipment.Neck.IsDisabled)) }}
40+
{{- if $showItem }}
41+
Neck: {{ if gt .Equipment.Neck.ItemId 0 }}{{ .Equipment.Neck.NameSimple }}{{ else }}nothing{{ end }}
42+
{{- end }}
43+
{{- $showItem = or (and (not .Equipment.Body.IsDisabled) (gt .Equipment.Body.ItemId 0)) (and $showEmpty (not .Equipment.Body.IsDisabled)) }}
44+
{{- if $showItem }}
45+
Body: {{ if gt .Equipment.Body.ItemId 0 }}{{ .Equipment.Body.NameSimple }}{{ else }}nothing{{ end }}
46+
{{- end }}
47+
{{- $showItem = or (and (not .Equipment.Belt.IsDisabled) (gt .Equipment.Belt.ItemId 0)) (and $showEmpty (not .Equipment.Belt.IsDisabled)) }}
48+
{{- if $showItem }}
49+
Belt: {{ if gt .Equipment.Belt.ItemId 0 }}{{ .Equipment.Belt.NameSimple }}{{ else }}nothing{{ end }}
50+
{{- end }}
51+
{{- $showItem = or (and (not .Equipment.Gloves.IsDisabled) (gt .Equipment.Gloves.ItemId 0)) (and $showEmpty (not .Equipment.Gloves.IsDisabled)) }}
52+
{{- if $showItem }}
53+
Gloves: {{ if gt .Equipment.Gloves.ItemId 0 }}{{ .Equipment.Gloves.NameSimple }}{{ else }}nothing{{ end }}
54+
{{- end }}
55+
{{- $showItem = or (and (not .Equipment.Ring.IsDisabled) (gt .Equipment.Ring.ItemId 0)) (and $showEmpty (not .Equipment.Ring.IsDisabled)) }}
56+
{{- if $showItem }}
57+
Ring: {{ if gt .Equipment.Ring.ItemId 0 }}{{ .Equipment.Ring.NameSimple }}{{ else }}nothing{{ end }}
58+
{{- end }}
59+
{{- $showItem = or (and (not .Equipment.Legs.IsDisabled) (gt .Equipment.Legs.ItemId 0)) (and $showEmpty (not .Equipment.Legs.IsDisabled)) }}
60+
{{- if $showItem }}
61+
Legs: {{ if gt .Equipment.Legs.ItemId 0 }}{{ .Equipment.Legs.NameSimple }}{{ else }}nothing{{ end }}
62+
{{- end }}
63+
{{- $showItem = or (and (not .Equipment.Feet.IsDisabled) (gt .Equipment.Feet.ItemId 0)) (and $showEmpty (not .Equipment.Feet.IsDisabled)) }}
64+
{{- if $showItem }}
65+
Feet: {{ if gt .Equipment.Feet.ItemId 0 }}{{ .Equipment.Feet.NameSimple }}{{ else }}nothing{{ end }}
9566
{{- end }}
9667
{{- end }}
9768

0 commit comments

Comments
 (0)