Skip to content

Commit

Permalink
Parrot gravestones
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercerenies committed Jun 19, 2022
1 parent 78e88bd commit 76597dc
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions Features.org
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
instead of splash potions
+ unfinished :: If you start mining an ore and then stop, it gets
mad and turns to cobweb
+ parrotdeath :: Parrots get tiny gravestones when they die
* IDEAS (UNIMPLEMENTED)
+ Wolves?
+ Fishing?
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ A Turtle's Troll also depends on the following two libraries.

* Zombies and skeletons sometimes spawn with special hats which drop
when they're killed.
* Parrots get tiny gravestones when they die
* All rabbits are killer rabbits now
* Feeding cookies to parrots causes them to duplicate now
* Pumpkin helmets no longer count as armor for temperature purposes
Expand Down
1 change: 1 addition & 0 deletions src/AllFeatureFactories.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ object AllFeatureFactories {
NamedZombieListener,
OvergrowthListenerFactory(OvergrowthListener::randomWood),
ParrotCookieListener,
ParrotDeathListener,
ParrotManager,
PetPhantomManager,
PillagerGunListener,
Expand Down
2 changes: 1 addition & 1 deletion src/ParrotCookieListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ParrotCookieListener() : AbstractFeature(), Listener {
private fun isCookie(material: Material): Boolean =
material == Material.COOKIE

private fun wasDamagedByCookie(parrot: Parrot): Boolean {
fun wasDamagedByCookie(parrot: Parrot): Boolean {
// So feeding a cookie to a parrot counts as attacking the
// parrot with the cookie, apparently, as though you just poked
// them with a sword or something.
Expand Down
127 changes: 127 additions & 0 deletions src/ParrotDeathListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@

package com.mercerenies.turtletroll

import com.mercerenies.turtletroll.feature.AbstractFeature
import com.mercerenies.turtletroll.feature.container.FeatureContainer
import com.mercerenies.turtletroll.feature.container.ListenerContainer
import com.mercerenies.turtletroll.feature.builder.BuilderState
import com.mercerenies.turtletroll.feature.builder.FeatureContainerFactory
import com.mercerenies.turtletroll.ext.*
import com.mercerenies.turtletroll.gravestone.shape.GravestoneSpawner
import com.mercerenies.turtletroll.gravestone.Inscriptions
import com.mercerenies.turtletroll.gravestone.Rotation

import org.bukkit.Material
import org.bukkit.block.Block
import org.bukkit.block.BlockFace
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.entity.EntityDeathEvent
import org.bukkit.event.entity.EntityDamageByEntityEvent
import org.bukkit.entity.Parrot
import org.bukkit.entity.HumanEntity
import org.bukkit.scheduler.BukkitRunnable
import org.bukkit.block.`data`.`type`.WallSign
import org.bukkit.block.Sign
import org.bukkit.plugin.Plugin

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle

class ParrotDeathListener(val plugin: Plugin) : AbstractFeature(), Listener {

companion object : FeatureContainerFactory<FeatureContainer> {
val DELAY_SECONDS = 2L

override fun create(state: BuilderState): FeatureContainer =
ListenerContainer(ParrotDeathListener(state.plugin))

}

private object ParrotGravestoneSpawner : GravestoneSpawner() {

override fun spawnGravestone(centerBlock: Block, inscriptions: Inscriptions, rotation: Rotation) {

// The stone itself
replaceWithStone(centerBlock)

// Now the sign
val signBlock = centerBlock.location.clone().add(rotation.vector(1, 0, 0)).block
replaceWith(signBlock, Material.BIRCH_WALL_SIGN)

val blockData = signBlock.blockData
if (blockData is WallSign) {
blockData.setFacing(rotation.blockFace(BlockFace.EAST))
}
signBlock.blockData = blockData

val blockState = signBlock.state
if (blockState is Sign) {
for (index in 0..3) {
blockState.setLine(index, inscriptions.getLine(index))
}
}
blockState.update()

}

}

private class ParrotInscriptions(
val parrot: Parrot,
val timestamp: LocalDateTime,
) : Inscriptions {

override fun getLine(index: Int): String =
when (index) {
0 -> {
// Parrot
parrot.getCustomName() ?: "Parrot"
}
1 -> {
parrot.owner?.getName() ?: "Died alone :("
}
2 -> {
timestamp.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT))
}
else -> {
""
}
}

}

override val name = "parrotdeath"

override val description = "Parrots get gravestones too"

private class SpawnParrotGravestone(val block: Block, val cause: Inscriptions) : BukkitRunnable() {
override fun run() {
val rotation = Rotation.NONE
val spawner = ParrotGravestoneSpawner
spawner.spawnGravestone(block, cause, rotation)
}
}

@EventHandler
fun onEntityDeath(event: EntityDeathEvent) {
if (!isEnabled()) {
return
}

val entity = event.entity
if (entity is Parrot) {
// In the name of sanity, don't do this and the
// ParrotCookieListener thing at the same time (let the latter
// take precedent).
if (!ParrotCookieListener.wasDamagedByCookie(entity)) {
val block = entity.location.block
val inscription = ParrotInscriptions(entity, LocalDateTime.now())
SpawnParrotGravestone(block, inscription).runTaskLater(plugin, Constants.TICKS_PER_SECOND * DELAY_SECONDS)
}
}

}

}

0 comments on commit 76597dc

Please sign in to comment.