Skip to content

Commit 464dd93

Browse files
Implement SCP-268 effect lingering
1 parent 22900a7 commit 464dd93

File tree

1 file changed

+30
-2
lines changed
  • src/main/java/dev/enderman/minecraft/plugins/scp/items

1 file changed

+30
-2
lines changed

src/main/java/dev/enderman/minecraft/plugins/scp/items/SCP268Item.kt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import org.bukkit.Bukkit
99
import org.bukkit.Material
1010
import org.bukkit.NamespacedKey
1111
import org.bukkit.entity.EntityType
12+
import org.bukkit.entity.LivingEntity
1213
import org.bukkit.entity.Mob
1314
import org.bukkit.event.EventHandler
1415
import org.bukkit.event.entity.EntityTargetLivingEntityEvent
1516
import org.bukkit.event.player.PlayerJoinEvent
1617
import org.bukkit.persistence.PersistentDataType
1718
import org.bukkit.potion.PotionEffect
1819
import org.bukkit.potion.PotionEffectType
20+
import kotlin.math.pow
1921

2022
private val unaffectedEntities = listOfNotNull(
2123
EntityType.WARDEN,
@@ -56,10 +58,22 @@ private val confusedEntities = listOfNotNull(
5658
EntityType.SPIDER
5759
)
5860

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+
5972
class SCP268Item(plugin: SCPPlugin) : TexturedItem(plugin, "scp_268", Material.LEATHER_HELMET) {
6073

6174
private val timeHatPutOnKey = NamespacedKey(plugin, "time_scp_268_put_on")
6275
private val timeHatWornKey = NamespacedKey(plugin, "time_worn_scp_268")
76+
private val lingerTimeExpirationKey = NamespacedKey(plugin, "scp_268_linger_expiration")
6377

6478
@EventHandler
6579
private fun onEquip(event: PlayerArmorChangeEvent) {
@@ -120,9 +134,12 @@ class SCP268Item(plugin: SCPPlugin) : TexturedItem(plugin, "scp_268", Material.L
120134
val difference = currentTime - timePutOn
121135

122136
var timeWorn = player.persistentDataContainer[timeHatWornKey, PersistentDataType.INTEGER] ?: 0
137+
123138
timeWorn += difference
124139

125140
player.persistentDataContainer[timeHatPutOnKey, PersistentDataType.INTEGER] = timeWorn
141+
142+
if (timeWorn >= TWENTY_MINECRAFT_HOURS_IN_TICKS) makeEffectLinger(player, timeWorn)
126143
}
127144

128145
@EventHandler
@@ -139,9 +156,12 @@ class SCP268Item(plugin: SCPPlugin) : TexturedItem(plugin, "scp_268", Material.L
139156

140157
val helmet = equipment.helmet
141158

142-
if (!isItem(helmet)) return
159+
val lingerExpiry = target.persistentDataContainer[lingerTimeExpirationKey, PersistentDataType.DOUBLE] ?: 0.0
160+
val currentTick = Bukkit.getServer().currentTick.toDouble()
143161

144-
event.isCancelled = true
162+
if (isItem(helmet) || lingerExpiry > currentTick || lingerExpiry == INFINITE_LINGER_DURATION) {
163+
event.isCancelled = true
164+
}
145165
}
146166

147167
@EventHandler
@@ -155,4 +175,12 @@ class SCP268Item(plugin: SCPPlugin) : TexturedItem(plugin, "scp_268", Material.L
155175

156176
dataContainer[timeHatPutOnKey, PersistentDataType.INTEGER] = Bukkit.getServer().currentTick
157177
}
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+
}
158186
}

0 commit comments

Comments
 (0)