@@ -9,13 +9,15 @@ import org.bukkit.Bukkit
9
9
import org.bukkit.Material
10
10
import org.bukkit.NamespacedKey
11
11
import org.bukkit.entity.EntityType
12
+ import org.bukkit.entity.LivingEntity
12
13
import org.bukkit.entity.Mob
13
14
import org.bukkit.event.EventHandler
14
15
import org.bukkit.event.entity.EntityTargetLivingEntityEvent
15
16
import org.bukkit.event.player.PlayerJoinEvent
16
17
import org.bukkit.persistence.PersistentDataType
17
18
import org.bukkit.potion.PotionEffect
18
19
import org.bukkit.potion.PotionEffectType
20
+ import kotlin.math.pow
19
21
20
22
private val unaffectedEntities = listOfNotNull(
21
23
EntityType .WARDEN ,
@@ -56,10 +58,22 @@ private val confusedEntities = listOfNotNull(
56
58
EntityType .SPIDER
57
59
)
58
60
61
+ private const val TWENTY_MINECRAFT_HOURS_IN_TICKS = 20_000
62
+ private const val INFINITE_LINGER_DURATION_IN_TICKS = 60_000
63
+ private const val INFINITE_LINGER_DURATION = - 1.0
64
+
65
+ /* *
66
+ A formula to calculate how long the effect should linger
67
+ based on how long someone has worn the hat:
68
+
69
+ 1000000000000 / (t-60000)^2 where t is in ticks that has been worn, and the output is how long in ticks the effect should linger, eventually becoming permanent.
70
+ */
71
+
59
72
class SCP268Item (plugin : SCPPlugin ) : TexturedItem(plugin, " scp_268" , Material .LEATHER_HELMET ) {
60
73
61
74
private val timeHatPutOnKey = NamespacedKey (plugin, " time_scp_268_put_on" )
62
75
private val timeHatWornKey = NamespacedKey (plugin, " time_worn_scp_268" )
76
+ private val lingerTimeExpirationKey = NamespacedKey (plugin, " scp_268_linger_expiration" )
63
77
64
78
@EventHandler
65
79
private fun onEquip (event : PlayerArmorChangeEvent ) {
@@ -120,9 +134,12 @@ class SCP268Item(plugin: SCPPlugin) : TexturedItem(plugin, "scp_268", Material.L
120
134
val difference = currentTime - timePutOn
121
135
122
136
var timeWorn = player.persistentDataContainer[timeHatWornKey, PersistentDataType .INTEGER ] ? : 0
137
+
123
138
timeWorn + = difference
124
139
125
140
player.persistentDataContainer[timeHatPutOnKey, PersistentDataType .INTEGER ] = timeWorn
141
+
142
+ if (timeWorn >= TWENTY_MINECRAFT_HOURS_IN_TICKS ) makeEffectLinger(player, timeWorn)
126
143
}
127
144
128
145
@EventHandler
@@ -139,9 +156,12 @@ class SCP268Item(plugin: SCPPlugin) : TexturedItem(plugin, "scp_268", Material.L
139
156
140
157
val helmet = equipment.helmet
141
158
142
- if (! isItem(helmet)) return
159
+ val lingerExpiry = target.persistentDataContainer[lingerTimeExpirationKey, PersistentDataType .DOUBLE ] ? : 0.0
160
+ val currentTick = Bukkit .getServer().currentTick.toDouble()
143
161
144
- event.isCancelled = true
162
+ if (isItem(helmet) || lingerExpiry > currentTick || lingerExpiry == INFINITE_LINGER_DURATION ) {
163
+ event.isCancelled = true
164
+ }
145
165
}
146
166
147
167
@EventHandler
@@ -155,4 +175,12 @@ class SCP268Item(plugin: SCPPlugin) : TexturedItem(plugin, "scp_268", Material.L
155
175
156
176
dataContainer[timeHatPutOnKey, PersistentDataType .INTEGER ] = Bukkit .getServer().currentTick
157
177
}
178
+
179
+ private fun makeEffectLinger (entity : LivingEntity , timeWorn : Int ) {
180
+ val lingerTime = if (timeWorn >= INFINITE_LINGER_DURATION_IN_TICKS ) INFINITE_LINGER_DURATION else 1000000000000.0 / (timeWorn.toDouble() - 60000.0 ).pow(2.0 )
181
+
182
+ val currentTick = Bukkit .getServer().currentTick.toDouble()
183
+
184
+ entity.persistentDataContainer[lingerTimeExpirationKey, PersistentDataType .DOUBLE ] = if (lingerTime == INFINITE_LINGER_DURATION ) INFINITE_LINGER_DURATION else currentTick + lingerTime
185
+ }
158
186
}
0 commit comments