All Downloads are FREE. Search and download functionalities are using the official Maven repository.

walkmc.API.kt Maven / Gradle / Ivy

The newest version!
package walkmc

import be.maximvdw.featherboard.api.*
import be.maximvdw.placeholderapi.*
import be.maximvdw.placeholderapi.PlaceholderAPI
import com.comphenix.protocol.*
import com.comphenix.protocol.events.*
import com.gmail.filoghost.holographicdisplays.api.*
import com.gmail.filoghost.holographicdisplays.api.handler.*
import com.gmail.filoghost.holographicdisplays.api.line.*
import com.intellectualcrafters.plot.*
import com.intellectualcrafters.plot.`object`.*
import com.sk89q.worldedit.*
import com.sk89q.worldedit.bukkit.*
import com.sk89q.worldedit.extent.clipboard.*
import com.sk89q.worldedit.regions.*
import com.sk89q.worldguard.protection.*
import com.sk89q.worldguard.protection.managers.*
import com.sk89q.worldguard.protection.regions.*
import net.luckperms.api.cacheddata.*
import net.luckperms.api.query.*
import org.bukkit.*
import org.bukkit.Location
import org.bukkit.entity.*
import org.bukkit.inventory.*
import org.bukkit.plugin.Plugin
import walkmc.extensions.*
import walkmc.extensions.strings.*

/**
 * Gets the skin name of this player.
 */
var OfflinePlayer.skin: String
	get() = SkinRestorer.getSkinName(name)
	set(value) = SkinRestorer.setSkin(name, value)

/**
 * Returns if this player has any skin from skin restorer.
 */
val OfflinePlayer.hasSkin: Boolean
	get() = SkinRestorer.hasSkin(name)

/**
 * Removes any present custom skin from skin restorer of this player.
 */
fun OfflinePlayer.removeSkin() = SkinRestorer.removeSkin(name)

/**
 * Gets the luck perm user instance of this player.
 */
val OfflinePlayer.luckpermUser
	get() = LuckPermProvider.userManager.getUser(name)!!

/**
 * Gets the cached metadata from luck perms.
 */
val OfflinePlayer.luckpermMetadata: CachedMetaData
	get() = luckpermUser.cachedData.getMetaData(QueryOptions.defaultContextualOptions())

/**
 * Gets the prefix with name of this player.
 */
val OfflinePlayer.prefixNamed: String
	get() = "$prefix$name"

/**
 * Gets the prefix of this player.
 */
val OfflinePlayer.prefix: String
	get() = luckpermMetadata.prefix?.reverseColorize() ?: ""

/**
 * Gets the prefix color of this player.
 */
val OfflinePlayer.prefixColor: String
	get() = prefix.substring(0..2)

/**
 * Gets the suffix of this player.
 */
val OfflinePlayer.suffix: String
	get() = luckpermMetadata.suffix?.reverseColorize() ?: ""

/**
 * Gets the suffix color of this player.
 */
val OfflinePlayer.suffixColor: String
	get() = suffix.substring(0..2)

/**
 * Gets the world edit local session of this player.
 */
fun OfflinePlayer.worldEditSession(): LocalSession = SessionManager.findByName(name)!!

/**
 * Gets the world edit clipboard of this player.
 */
fun OfflinePlayer.clipboard(): Clipboard = worldEditSession().clipboard.clipboard

/**
 * Gets the current world edit selection of this player.
 */
fun Player.selection(): Region = worldEditSession().getSelection(world.toWorldEditWorld())

/**
 * Gets the current world edit selection of this player or null.
 */
fun Player.selectionOrNull(): Region? {
	return try {
		selection()
	} catch (ex: Exception) {
		null
	}
}

/**
 * Gets the current balance of this player
 */
var OfflinePlayer.balance: Double
	get() = EconomyProvider.getBalance(this)
	set(value) {
		val provider = EconomyProvider
		provider.withdrawPlayer(this, provider.getBalance(this))
		provider.depositPlayer(this, value)
	}

/**
 * Returns all plots of this player.
 */
val OfflinePlayer.plots: MutableSet
	get() = toPlotPlayer().plots

/**
 * Returns if this player has any plots.
 */
val OfflinePlayer.hasPlots
	get() = PS.get().hasPlot(uuid)

/**
 * Converts this player to a plot player.
 */
fun OfflinePlayer.toPlotPlayer(): PlotPlayer = PlotPlayer.wrap(this)

/**
 * Returns if this entity is a hologram.
 */
val Entity.isHologram
	get() = HologramsAPI.isHologramEntity(this)

/**
 * Converts this location to a plot location.
 */
fun Location.toPlotLocation(): PlotLocation = walkmc.PlotLocation(world.name, blockX, blockY, blockZ)

/**
 * Gets the plot of this location or null.
 */
fun Location.toPlotOrNull(): Plot? = toPlotLocation().plot

/**
 * Gets the plot of this location.
 */
fun Location.toPlot(): Plot = toPlotLocation().plot

/**
 * Returns if this location is in a plot area.
 */
val Location.isPlotArea: Boolean
	get() = toPlotLocation().isPlotArea

/**
 * Returns if this location is in a plot road.
 */
val Location.isPlotRoad: Boolean
	get() = toPlotLocation().isPlotRoad

/**
 * Returns if this location is in a plot area that's not has any owner.
 */
val Location.isUnownedPlotArea: Boolean
	get() = toPlotLocation().isUnownedPlotArea

/**
 * Converts this plot location to a bukkit location.
 */
fun PlotLocation.toLocation(): Location = Location(world(world), x, y, z)

/**
 * Gets all holograms registered by this plugin.
 */
val Plugin.holograms: MutableCollection
	get() = HologramsAPI.getHolograms(this)

/**
 * Creates a hologram with this plugin as owner and the specified location.
 */
fun Plugin.createHologram(location: Location): Hologram =
	HologramsAPI.createHologram(this, location)

/**
 * Creates a hologram with this plugin as owner and the specified location, with a additional [builder].
 */
inline fun Plugin.createHologram(location: Location, builder: Hologram.() -> Unit): Hologram =
	HologramsAPI.createHologram(this, location).apply(builder)

/**
 * Gets the region manager of this world.
 */
fun World.regionManager(): RegionManager = RegionContainer[this]!!

/**
 * Gets the region manager of this world.
 */
fun World.regionManagerOrNull() = RegionContainer[this]

/**
 * Gets all regions from this world as map.
 */
fun World.regionsMap(): MutableMap =
	regionManager().regions

/**
 * Gets all regions from this world.
 */
fun World.regions() = regionsMap().values

/**
 * Converts this world to a world edit world.
 */
fun World.toWorldEditWorld() = BukkitWorld(this)

/**
 * Gets the global region of this world.
 */
fun World.globalRegion() = regionManager().getRegion("__global__")!!

/**
 * Gets the region manager of the world of this location.
 */
fun Location.regionManager() = world.regionManager()

/**
 * Gets the region manager of the world of this location.
 */
fun Location.regionManagerOrNull() = world.regionManagerOrNull()

/**
 * Gets all applicables regions presents in this location.
 */
fun Location.regions(): ApplicableRegionSet =
	world.regionManager().getApplicableRegions(this)

/**
 * Gets all applicables regions presents in this location or null.
 */
fun Location.regionsOrNull(): ApplicableRegionSet? =
	regionManagerOrNull()?.getApplicableRegions(this)

/**
 * Gets the primary regions presents in this location,
 * this is, the region that's has the largest priority.
 */
fun Location.primaryRegion(): ProtectedRegion =
	regions().maxByOrNull { it.priority }!!

/**
 * Gets the primary regions presents in this location, this is, the region that's has
 * the largest priority or null if not has any region in this location.
 */
fun Location.primaryRegionOrNull(): ProtectedRegion? =
	regionsOrNull()?.maxByOrNull { it.priority }

/**
 * Returns if in this location has any regions.
 */
fun Location.anyRegions(): Boolean {
	val regions = regionsOrNull() ?: return false
	return regions.size() >= 1
}

/**
 * Specify the given action when a player clicks in this hologram text line.
 */
fun TextLine.onTouch(action: TouchHandler) {
	touchHandler = action
}

/**
 * Specify the given action when a player clicks in this hologram item line.
 */
fun ItemLine.onTouch(action: TouchHandler) {
	touchHandler = action
}

/**
 * Specify the given action when a player pickups the item in this hologram item line.
 */
fun ItemLine.onPickup(action: PickupHandler) {
	pickupHandler = action
}

/**
 * Appends the given text to this hologram with a additional [builder].
 */
inline fun Hologram.appendTextLine(text: String, builder: TextLine.() -> Unit): TextLine =
	appendTextLine(text).apply(builder)

/**
 * Appends the given text to this hologram with a additional [builder].
 */
inline fun Hologram.appendItemLine(item: ItemStack, builder: ItemLine.() -> Unit): ItemLine =
	appendItemLine(item).apply(builder)

/**
 * Register a MVDW Placeholder by the specified [placeholder] with this plugin.
 */
fun Plugin.registerPlaceholder(placeholder: String, replacer: PlaceholderReplaceEvent.() -> String) =
	PlaceholderAPI.registerPlaceholder(this, placeholder, replacer)

/**
 * Replaces all placeholders using MVDW Placeholder in this string by the specified [player].
 */
fun String.replacePlaceholders(player: OfflinePlayer, mvdw: Boolean = false): String {
	return if (mvdw)
		PlaceholderAPI.replacePlaceholders(player, this)
	else
		me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, this)
}

/**
 * Toggles the featherboard scoreboard of this player.
 */
fun Player.toggleBoard(notificate: Boolean = false) = FeatherBoardAPI.toggle(this, notificate)

/**
 * Toggles the featherboard scoreboard of this player to show them.
 */
fun Player.toggleBoardToVisible(notificate: Boolean = false) {
	if (!isBoardToggled)
		toggleBoard(notificate)
}

/**
 * Toggles the featherboard scoreboard of this player to show them.
 */
fun Player.toggleBoardToInvisible(notificate: Boolean = false) {
	if (isBoardToggled)
		toggleBoard(notificate)
}

/**
 * Toggles the featherboard scoreboard of this player with an optional boolean to the toggle.
 */
fun Player.toggleBoardTo(visible: Boolean, notificate: Boolean = false) {
	if (visible) toggleBoardToVisible(notificate) else toggleBoardToInvisible(notificate)
}

/**
 * Shows the specified featherboard scoreboard to this player by the specified [scoreboard]
 */
fun Player.showBoard(scoreboard: String) {
	FeatherBoardAPI.removeScoreboardOverride(this, scoreboard)
	FeatherBoardAPI.showScoreboard(this, scoreboard)
}

/**
 * Returns if the featherboard scoreboard of this player is toggled.
 */
val Player.isBoardToggled: Boolean
	get() = FeatherBoardAPI.isToggled(this)

/**
 * Subscribes a new packet receive listener by this plugin with the specified [priority]
 * and all packets listed in [packets]. The execution is performed by [action].
 */
fun Plugin.subscribePacketReceive(
	vararg packets: PacketType,
	priority: ListenerPriority = ListenerPriority.NORMAL,
	async: Boolean = false,
	action: PacketEvent.() -> Unit
) {
	val adapter = object : PacketAdapter(this, priority, *packets) {
		override fun onPacketReceiving(event: PacketEvent) = action(event)
		override fun onPacketSending(event: PacketEvent?) = Unit
	}
	
	if (async) {
		ProtocolManager.asynchronousManager.registerAsyncHandler(adapter).syncStart()
	} else {
		ProtocolManager.addPacketListener(adapter)
	}
}

/**
 * Subscribes a new packet sending listener by this plugin with the specified [priority]
 * and all packets listed in [packets]. The execution is performed by [action].
 */
fun Plugin.subscribePacketSending(
	vararg packets: PacketType,
	priority: ListenerPriority = ListenerPriority.NORMAL,
	async: Boolean = false,
	action: PacketEvent.() -> Unit
) {
	val adapter = object : PacketAdapter(this, priority, *packets) {
		override fun onPacketSending(event: PacketEvent) = action(event)
		override fun onPacketReceiving(event: PacketEvent?) = Unit
	}
	
	if (async) {
		ProtocolManager.asynchronousManager.registerAsyncHandler(adapter).syncStart()
	} else {
		ProtocolManager.addPacketListener(adapter)
	}
}

/**
 * Subscribes a new packet receive listener by this plugin with the specified [priority]
 * and all packets listed in [packets]. The execution is performed by [action].
 */
fun Resource.subscribePacketReceive(
	vararg packets: PacketType,
	priority: ListenerPriority = ListenerPriority.NORMAL,
	async: Boolean = false,
	action: PacketEvent.() -> Unit
) = plugin.subscribePacketReceive(*packets, priority = priority, async = async, action = action)

/**
 * Subscribes a new packet sending listener by this plugin with the specified [priority]
 * and all packets listed in [packets]. The execution is performed by [action].
 */
fun Resource.subscribePacketSending(
	vararg packets: PacketType,
	priority: ListenerPriority = ListenerPriority.NORMAL,
	async: Boolean = false,
	action: PacketEvent.() -> Unit
) = plugin.subscribePacketSending(*packets, priority = priority, async = async, action = action)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy