Skip to content

Commit 3c4cebb

Browse files
authored
Merge pull request #36 from 1x0x11810/hit-distance
Hit distance
2 parents c1813c4 + 08bacc5 commit 3c4cebb

1 file changed

Lines changed: 186 additions & 0 deletions

File tree

module/HitDistance.lua

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
name = "Hit Distance"
2+
description = "Shows the distance you were last hit from."
3+
author = "ZayTheGamer1000"
4+
debug = false
5+
6+
--[[ DEFAULT VALUES
7+
As of Flarial 3.0.3.6, the default values for settings do not seem to work.
8+
If you are here to change the default values, they are provided below.
9+
]]
10+
11+
-- These are the default X & Y values for the GUI's text.
12+
guiX = 0
13+
guiY = 30
14+
15+
-- This is the default font size
16+
fontSize = 200
17+
18+
-- This is the default duration in seconds
19+
duration = 3
20+
21+
-- This is the default search range in blocks
22+
searchRange = 10
23+
24+
25+
lastHealth = player.health()
26+
lastHitDistance = 0.0
27+
28+
showDistance = false
29+
ticks = 0
30+
showDistSecs = 0
31+
32+
guiXSlider = settings.addSlider("GUI X", "The X value that the distance GUI will appear at.", 0,
33+
100, -- Maximum value
34+
0, -- Minimum value
35+
false) -- Allow 0 inclusive (for some reason false means true)
36+
37+
guiYSlider = settings.addSlider("GUI Y", "The Y value that the distance GUI will appear at.", 30,
38+
100,
39+
0,
40+
false)
41+
42+
--Initial constraints (static unless the location of GUIX/GUIY is changed in settings.)
43+
guiXConstraint = Constraints.PercentageConstraint(guiX / 100, "left", true)
44+
guiXAdjusted = false
45+
guiYConstraint = Constraints.PercentageConstraint(guiY / 100, "top", true)
46+
guiYAdjusted = false
47+
48+
fontSizeSlider = settings.addTextBox("Font Size", "The size of the gui's text. (Default 200)", "200", 5)
49+
fontSizeAdjusted = false
50+
51+
showDuration = settings.addTextBox("Duration", "The duration in seconds that the distance will be shown for. (default 3)", "3", 12)
52+
durationAdjusted = false
53+
54+
searchRangeInput = settings.addTextBox("Search range", "The range in which the attacker is searched for. (default 10)", "10", 5)
55+
searchRangeAdjusted = false
56+
57+
includeEntities = settings.addToggle("Include entities", "Whether to include all player entities in detection, rather than just players.", false)
58+
--No need for a default boolean, as the (real) default and what we want are the same: false.
59+
60+
function onEnable()
61+
client.notify("Hit Distance Enabled")
62+
end
63+
64+
function onDisable()
65+
client.notify("Hit Distance Disabled")
66+
end
67+
68+
69+
onEvent("TickEvent", function()
70+
local health = player.health()
71+
72+
if health < lastHealth then
73+
lastHealth = health
74+
playerHit()
75+
else
76+
lastHealth = health
77+
end
78+
79+
if showDistance then
80+
if ticks == 20 then
81+
showDistSecs = showDistSecs + 1
82+
end
83+
84+
85+
if showDistSecs == duration then
86+
showDistance = false
87+
showDistSecs = 0
88+
ticks = 0
89+
end
90+
91+
if ticks == 20 then -- 20 ticks = 1 second (Handles ticks)
92+
ticks = 0
93+
end
94+
ticks = ticks + 1
95+
end
96+
97+
if guiXAdjusted or guiXSlider.value ~= 0.0 then -- X slider has been adjusted for the first time
98+
guiXAdjusted = true
99+
guiX = guiXSlider.value
100+
guiXConstraint = Constraints.PercentageConstraint(guiX / 100, "left", true)
101+
end
102+
if guiYAdjusted or guiYSlider.value ~= 0.0 then -- Y slider has been adjusted for the first time
103+
guiYAdjusted = true
104+
guiY = guiYSlider.value
105+
guiYConstraint = Constraints.PercentageConstraint(guiY / 100, "top", true)
106+
end
107+
108+
if durationAdjusted or showDuration.value ~= "" then
109+
durationAdjusted = true
110+
111+
if tonumber(showDuration.value) == nil then
112+
duration = 0
113+
else
114+
duration = math.floor(tonumber(showDuration.value))
115+
end
116+
end
117+
118+
if fontSizeAdjusted or fontSizeSlider.value ~= "" then
119+
fontSizeAdjusted = true
120+
121+
if tonumber(fontSizeSlider.value) == nil then
122+
fontSize = 0
123+
else
124+
fontSize = math.floor(tonumber(fontSizeSlider.value))
125+
end
126+
end
127+
128+
if searchRangeAdjusted or searchRangeInput.value ~= "" then
129+
searchRangeAdjusted = true
130+
131+
if tonumber(searchRangeInput.value) == nil then
132+
searchRange = 0
133+
else
134+
searchRange = math.floor(tonumber(searchRangeInput.value))
135+
end
136+
end
137+
138+
end)
139+
140+
141+
function playerHit()
142+
local entities = world.getEntities(searchRange)
143+
local entity = nil
144+
local closestDistance = nil
145+
146+
for index, value in ipairs(entities) do
147+
148+
if includeEntities.value then -- If all entities included in search
149+
if value:isValid() then
150+
if closestDistance == nil or value:distanceToPlayer() < closestDistance then -- If first or closest entity iterated
151+
closestDistance = value:distanceToPlayer()
152+
entity = value
153+
end
154+
end
155+
else
156+
if value:isValid() and value:getTypeId() == "minecraft:player" then
157+
if closestDistance == nil or value:distanceToPlayer() < closestDistance then -- If first or closest player iterated
158+
closestDistance = value:distanceToPlayer()
159+
entity = value
160+
end
161+
end
162+
end
163+
end
164+
165+
if entity == nil then -- Failed to find a valid player or entity
166+
return
167+
end
168+
169+
170+
lastHitDistance = entity:distanceToPlayer()
171+
showDistance = true
172+
ticks = 0
173+
showDistSecs = 0
174+
175+
end
176+
177+
onEvent("RenderEvent", function()
178+
179+
if showDistance then
180+
gui.text({guiXConstraint, guiYConstraint}, -- X and Y slider / 100 (gets decimal location value
181+
string.format("%.2f", lastHitDistance),
182+
100,
183+
60,
184+
fontSize)
185+
end
186+
end)

0 commit comments

Comments
 (0)