forked from Slimefun-Addon-Community/Galactifun2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
258 additions
and
15 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
.../src/main/kotlin/io/github/addoncommunity/galactifun/api/betteritem/BetterSlimefunItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package io.github.addoncommunity.galactifun.api.betteritem | ||
|
||
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup | ||
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem | ||
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack | ||
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType | ||
import io.github.thebusybiscuit.slimefun4.core.handlers.* | ||
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler | ||
import org.bukkit.block.Block | ||
import org.bukkit.event.block.BlockBreakEvent | ||
import org.bukkit.event.block.BlockPlaceEvent | ||
import org.bukkit.inventory.ItemStack | ||
import java.lang.invoke.MethodHandle | ||
import java.lang.invoke.MethodHandles | ||
import java.lang.reflect.Method | ||
import kotlin.reflect.KClass | ||
import io.github.thebusybiscuit.slimefun4.api.items.ItemHandler as BaseItemHandler | ||
|
||
open class BetterSlimefunItem : SlimefunItem { | ||
|
||
constructor( | ||
itemGroup: ItemGroup, | ||
item: SlimefunItemStack, | ||
recipeType: RecipeType, | ||
recipe: Array<out ItemStack?> | ||
) : super(itemGroup, item, recipeType, recipe) | ||
|
||
constructor( | ||
itemGroup: ItemGroup, | ||
item: SlimefunItemStack, | ||
recipeType: RecipeType, | ||
recipe: Array<out ItemStack?>, | ||
recipeOutput: ItemStack | ||
) : super(itemGroup, item, recipeType, recipe, recipeOutput) | ||
|
||
constructor( | ||
itemGroup: ItemGroup, | ||
item: ItemStack, | ||
id: String, | ||
recipeType: RecipeType, | ||
recipe: Array<out ItemStack?> | ||
) : super(itemGroup, item, id, recipeType, recipe) | ||
|
||
companion object { | ||
|
||
private val handlerMap = | ||
mutableMapOf<KClass<out BaseItemHandler>, (MethodHandle) -> BaseItemHandler>() | ||
|
||
fun <T : BaseItemHandler> registerHandler( | ||
clazz: KClass<T>, | ||
handler: (MethodHandle) -> BaseItemHandler | ||
) { | ||
handlerMap[clazz] = handler | ||
} | ||
|
||
init { | ||
registerHandler(BlockBreakHandler::class) { handle -> | ||
object : BlockBreakHandler(false, false) { | ||
override fun onPlayerBreak(e: BlockBreakEvent, item: ItemStack, drops: MutableList<ItemStack>) { | ||
handle.invoke(e, item, drops) | ||
} | ||
} | ||
} | ||
registerHandler(BlockDispenseHandler::class) { BlockDispenseHandler(it::invoke) } | ||
registerHandler(BlockPlaceHandler::class) { handle -> | ||
object : BlockPlaceHandler(false) { | ||
override fun onPlayerPlace(e: BlockPlaceEvent) { | ||
handle.invoke(e) | ||
} | ||
} | ||
} | ||
registerHandler(BlockUseHandler::class) { BlockUseHandler(it::invoke) } | ||
registerHandler(BowShootHandler::class) { BowShootHandler(it::invoke) } | ||
registerHandler(EntityInteractHandler::class) { EntityInteractHandler(it::invoke) } | ||
registerHandler(EntityKillHandler::class) { EntityKillHandler(it::invoke) } | ||
registerHandler(ItemConsumptionHandler::class) { ItemConsumptionHandler(it::invoke) } | ||
registerHandler(ItemDropHandler::class) { | ||
ItemDropHandler { e, p, i -> it.invoke(e, p, i) as Boolean } | ||
} | ||
registerHandler(ItemUseHandler::class) { ItemUseHandler(it::invoke) } | ||
registerHandler(MultiBlockInteractionHandler::class) { | ||
MultiBlockInteractionHandler { p, mb, b -> it.invoke(p, mb, b) as Boolean } | ||
} | ||
registerHandler(SimpleBlockBreakHandler::class) { | ||
object : SimpleBlockBreakHandler() { | ||
override fun onBlockBreak(b: Block) { | ||
it.invoke(b) | ||
} | ||
} | ||
} | ||
registerHandler(ToolUseHandler::class) { ToolUseHandler(it::invoke) } | ||
registerHandler(WeaponUseHandler::class) { WeaponUseHandler(it::invoke) } | ||
} | ||
} | ||
|
||
override fun preRegister() { | ||
for (method in javaClass.getAllMethods()) { | ||
if (method.isAnnotationPresent(ItemHandler::class.java)) { | ||
method.isAccessible = true | ||
val handle = MethodHandles.lookup().unreflect(method) | ||
val handler = method.getAnnotation(ItemHandler::class.java).handler | ||
val handlerInstance = handlerMap[handler]?.invoke(handle.bindTo(this)) | ||
?: throw IllegalStateException("Handler $handler is not registered for BetterSlimefunItem") | ||
addItemHandler(handlerInstance) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun Class<*>.getAllMethods(): List<Method> { | ||
val methods = mutableListOf<Method>() | ||
var currentClass: Class<*>? = this | ||
while (currentClass != null) { | ||
methods.addAll(currentClass.declaredMethods) | ||
currentClass = currentClass.superclass | ||
} | ||
return methods | ||
} |
8 changes: 8 additions & 0 deletions
8
plugin/src/main/kotlin/io/github/addoncommunity/galactifun/api/betteritem/ItemHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.github.addoncommunity.galactifun.api.betteritem | ||
|
||
import io.github.thebusybiscuit.slimefun4.api.items.ItemHandler | ||
import kotlin.reflect.KClass | ||
|
||
@Target(AnnotationTarget.FUNCTION) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class ItemHandler(val handler: KClass<out ItemHandler>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/items/CaptainsChair.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.github.addoncommunity.galactifun.impl.items | ||
|
||
import io.github.addoncommunity.galactifun.impl.items.abstract.Seat | ||
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup | ||
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack | ||
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType | ||
import org.bukkit.block.Block | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
|
||
class CaptainsChair( | ||
itemGroup: ItemGroup, | ||
item: SlimefunItemStack, | ||
recipeType: RecipeType, | ||
recipe: Array<out ItemStack?> | ||
) : Seat(itemGroup, item, recipeType, recipe) { | ||
|
||
override fun onSit(p: Player, b: Block) { | ||
p.sendMessage("You are now sitting in the Captain's Chair") | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/items/abstract/Seat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.github.addoncommunity.galactifun.impl.items.abstract | ||
|
||
import io.github.addoncommunity.galactifun.api.betteritem.BetterSlimefunItem | ||
import io.github.addoncommunity.galactifun.api.betteritem.ItemHandler | ||
import io.github.addoncommunity.galactifun.units.Angle.Companion.radians | ||
import io.github.addoncommunity.galactifun.util.key | ||
import io.github.addoncommunity.galactifun.util.nearbyEntitiesByType | ||
import io.github.addoncommunity.galactifun.util.summon | ||
import io.github.seggan.sf4k.serial.pdc.set | ||
import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent | ||
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup | ||
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack | ||
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType | ||
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler | ||
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler | ||
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler | ||
import org.bukkit.Location | ||
import org.bukkit.block.Block | ||
import org.bukkit.block.data.Directional | ||
import org.bukkit.entity.ArmorStand | ||
import org.bukkit.entity.Player | ||
import org.bukkit.event.block.BlockPlaceEvent | ||
import org.bukkit.inventory.ItemStack | ||
import kotlin.math.atan2 | ||
|
||
open class Seat( | ||
itemGroup: ItemGroup, | ||
item: SlimefunItemStack, | ||
recipeType: RecipeType, | ||
recipe: Array<out ItemStack?> | ||
) : BetterSlimefunItem(itemGroup, item, recipeType, recipe) { | ||
|
||
private val armorStandKey = "seat".key() | ||
|
||
@ItemHandler(BlockPlaceHandler::class) | ||
private fun onPlace(e: BlockPlaceEvent) { | ||
val b = e.block | ||
val armorStand = b.world.summon<ArmorStand>(b.location.toStandLocation()) | ||
armorStand.isInvisible = true | ||
armorStand.isInvulnerable = true | ||
armorStand.isSmall = true | ||
armorStand.setGravity(false) | ||
armorStand.setAI(false) | ||
armorStand.isMarker = true | ||
armorStand.persistentDataContainer.set(armorStandKey, true) | ||
val data = b.blockData | ||
if (data is Directional) { | ||
val facing = data.facing | ||
val location = armorStand.location | ||
location.yaw = atan2(facing.modZ.toDouble(), facing.modX.toDouble()).radians | ||
.degrees.toFloat() + 90 | ||
armorStand.teleportAsync(location) | ||
} | ||
} | ||
|
||
@ItemHandler(SimpleBlockBreakHandler::class) | ||
private fun onBreak(b: Block) { | ||
val armorStand = b.world.nearbyEntitiesByType<ArmorStand>(b.location.toStandLocation(), 0.5) { | ||
it.persistentDataContainer.has(armorStandKey) | ||
}.firstOrNull() ?: return | ||
armorStand.remove() | ||
} | ||
|
||
@ItemHandler(BlockUseHandler::class) | ||
private fun onUse(e: PlayerRightClickEvent) { | ||
val block = e.clickedBlock.get() | ||
val armorStand = block.world.nearbyEntitiesByType<ArmorStand>(block.location.toStandLocation(), 0.5) { | ||
it.persistentDataContainer.has(armorStandKey) | ||
}.firstOrNull() ?: return | ||
armorStand.addPassenger(e.player) | ||
onSit(e.player, block) | ||
} | ||
|
||
protected open fun onSit(p: Player, b: Block) {} | ||
} | ||
|
||
private fun Location.toStandLocation() = add(0.5, 0.4, 0.5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
plugin/src/main/kotlin/io/github/addoncommunity/galactifun/util/DummyMetadataValue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.github.addoncommunity.galactifun.util | ||
|
||
import io.github.addoncommunity.galactifun.pluginInstance | ||
import org.bukkit.metadata.MetadataValueAdapter | ||
|
||
object DummyMetadataValue : MetadataValueAdapter(pluginInstance) { | ||
override fun value() = Unit | ||
override fun invalidate() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters