Skip to content

Commit

Permalink
Encapsulate autowire state
Browse files Browse the repository at this point in the history
... by merging the command with the listener.
  • Loading branch information
paulikauro committed Jan 3, 2021
1 parent d63d3d0 commit ca724c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 50 deletions.
36 changes: 11 additions & 25 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ group = ""
version = execute("git", "describe", "--long", "--dirty")

plugins {
val kotlinVersion = "1.4.10"
val kotlinVersion = "1.4.21"
kotlin("jvm") version kotlinVersion
kotlin("kapt") version kotlinVersion
id("com.github.johnrengelman.shadow") version "2.0.4"
Expand All @@ -14,29 +14,12 @@ plugins {

repositories {
mavenCentral()
maven {
name = "spigotmc-repo"
url = uri("https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
}
maven {
name = "sonatype-oss"
url = uri("https://oss.sonatype.org/content/groups/public/")
}
maven {
name = "enginehub-maven"
url = uri("https://maven.enginehub.org/repo/")
}
maven {
name = "aikar"
url = uri("https://repo.aikar.co/content/groups/aikar/")
}
maven {
name = "codemc-repo"
url = uri("https://repo.codemc.org/repository/maven-public/")
}
maven {
url = uri("https://repo.dmulloy2.net/nexus/repository/public/")
}
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
maven("https://oss.sonatype.org/content/groups/public/")
maven("https://maven.enginehub.org/repo/")
maven("https://repo.aikar.co/content/groups/aikar/")
maven("https://repo.codemc.org/repository/maven-public/")
maven("https://repo.dmulloy2.net/nexus/repository/public/")
}

dependencies {
Expand Down Expand Up @@ -73,7 +56,10 @@ tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
javaParameters = true
freeCompilerArgs = listOf("-Xopt-in=kotlin.ExperimentalStdlibApi")
freeCompilerArgs = listOf(
"-Xopt-in=kotlin.ExperimentalStdlibApi",
"-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi"
)
}
}

Expand Down
40 changes: 18 additions & 22 deletions src/main/kotlin/Autowire.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,37 @@ import org.bukkit.event.Listener
import org.bukkit.event.block.BlockPlaceEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.plugin.PluginManager
import java.util.*

@CommandAlias("autowire|aw")
@Description("Get that there redstone automagically!")
@CommandPermission("redstonetools.autowire")
class Autowire(
private val autos: MutableSet<UUID>,
private val protocolManager: ProtocolManager
) : BaseCommand() {
private val protocolManager: ProtocolManager,
private val pluginManager: PluginManager,
) : BaseCommand(), Listener {
private val autos = mutableSetOf<UUID>()

@Default
fun autowire(player: Player) {
fun toggleAutowire(player: Player) {
if (player.uniqueId in autos) {
autos.remove(player.uniqueId)
protocolManager.sendResponsePacket(player, "Auto Wire Disabled")
"Auto Wire Disabled"
} else {
autos.add(player.uniqueId)
protocolManager.sendResponsePacket(player, "Auto Wire Enabled")
}
"Auto Wire Enabled"
}.let { player.sendActionBarTitle(it) }
}

private fun ProtocolManager.sendResponsePacket(player: Player, message: String) {
val titlePacket = this.createPacket(PacketType.Play.Server.TITLE)
titlePacket.titleActions
.write(0, EnumWrappers.TitleAction.ACTIONBAR)
titlePacket.chatComponents
.write(0, WrappedChatComponent.fromText(message))
this.sendServerPacket(player, titlePacket)
private fun Player.sendActionBarTitle(message: String) {
val titlePacket = protocolManager.createPacket(PacketType.Play.Server.TITLE).apply {
titleActions.write(0, EnumWrappers.TitleAction.ACTIONBAR)
chatComponents.write(0, WrappedChatComponent.fromText(message))
}
protocolManager.sendServerPacket(this, titlePacket)
}
}

class AutoWireListener(
private val plugin: JavaPlugin,
private val autos: MutableSet<UUID>
) : Listener {
@EventHandler
fun onLeaveEvent(event: PlayerQuitEvent) {
autos.remove(event.player.uniqueId)
Expand All @@ -75,7 +71,7 @@ class AutoWireListener(
true,
event.hand
)
plugin.server.pluginManager.callEvent(blockPlaceEvent)
pluginManager.callEvent(blockPlaceEvent)
if (blockPlaceEvent.isCancelled) return
wirePosition.block.type = Material.REDSTONE_WIRE
val wireData: RedstoneWire = Material.REDSTONE_WIRE.createBlockData() as RedstoneWire
Expand All @@ -84,4 +80,4 @@ class AutoWireListener(
}
wirePosition.block.blockData = wireData
}
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/RedstoneTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class RedstoneTools : JavaPlugin() {
return
}
val worldEdit = wePlugin.worldEdit
val autos: MutableSet<UUID> = mutableSetOf()
val protocolManager = ProtocolLibrary.getProtocolManager()
val autowire = Autowire(protocolManager, server.pluginManager)
arrayOf(
WorldEditHelper(this, worldEdit),
AutoWireListener(this, autos),
autowire,
SlabListener(),
).forEach { server.pluginManager.registerEvents(it, this) }
PaperCommandManager(this).apply {
Expand All @@ -72,7 +72,7 @@ class RedstoneTools : JavaPlugin() {
SignSearch(worldEdit),
Container(),
Slab(),
Autowire(autos, protocolManager)
autowire,
).forEach(::registerCommand)
}
}
Expand Down

0 comments on commit ca724c7

Please sign in to comment.