Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
paulikauro committed Dec 7, 2021
2 parents 282d20a + e4296fb commit b450bd3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 47 deletions.
26 changes: 13 additions & 13 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ group = ""
version = "1.2"

plugins {
val kotlinVersion = "1.4.21"
val kotlinVersion = "1.6.0"
kotlin("jvm") version kotlinVersion
kotlin("kapt") version kotlinVersion
id("com.github.johnrengelman.shadow") version "2.0.4"
id("com.github.johnrengelman.shadow") version "7.1.0"
id("net.minecrell.plugin-yml.bukkit") version "0.3.0"
}

Expand All @@ -23,25 +23,25 @@ repositories {
}

dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("stdlib"))
implementation(kotlin("reflect"))
implementation(group = "co.aikar", name = "acf-paper", version = "0.5.0-SNAPSHOT")
implementation(group = "com.google.re2j", name = "re2j", version = "1.5")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8")
implementation("co.aikar:acf-paper:0.5.0-SNAPSHOT")
implementation("com.google.re2j:re2j:1.6")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-RC")

compileOnly(group = "de.tr7zw", name = "item-nbt-api-plugin", version = "2.5.0")
compileOnly(group = "org.spigotmc", name = "spigot-api", version = "1.15-R0.1-SNAPSHOT")
compileOnly(group = "com.sk89q.worldedit", name = "worldedit-bukkit", version = "7.2.0-SNAPSHOT")
compileOnly(group = "com.comphenix.protocol", name = "ProtocolLib", version = "4.5.0")
compileOnly("de.tr7zw:item-nbt-api-plugin:2.8.0")
compileOnly("org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT")
compileOnly("com.sk89q.worldedit:worldedit-bukkit:7.2.0-SNAPSHOT")
compileOnly("com.comphenix.protocol:ProtocolLib:4.7.0")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.3.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.3.1")
}

bukkit {
main = "redstonetools.RedstoneTools"
apiVersion = "1.15"
depend = listOf("WorldEdit")
apiVersion = "1.17"
depend = listOf("WorldEdit", "ProtocolLib", "NBTAPI")
}

tasks.test {
Expand All @@ -54,7 +54,7 @@ tasks.shadowJar {

tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = "11"
javaParameters = true
freeCompilerArgs = listOf(
"-Xopt-in=kotlin.ExperimentalStdlibApi",
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
28 changes: 17 additions & 11 deletions src/main/kotlin/Autowire.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.comphenix.protocol.PacketType
import com.comphenix.protocol.ProtocolManager
import com.comphenix.protocol.wrappers.EnumWrappers
import com.comphenix.protocol.wrappers.WrappedChatComponent
import net.md_5.bungee.api.ChatMessageType
import net.md_5.bungee.api.chat.TextComponent
import org.bukkit.GameMode
import org.bukkit.Material
import org.bukkit.block.data.type.RedstoneWire
Expand All @@ -25,7 +27,6 @@ import java.util.*
@Description("Get that there redstone automagically!")
@CommandPermission("redstonetools.autowire")
class Autowire(
private val protocolManager: ProtocolManager,
private val pluginManager: PluginManager,
) : BaseCommand(), Listener {
private val autos = mutableSetOf<UUID>()
Expand All @@ -42,23 +43,24 @@ class Autowire(
}

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)
spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent(message))
}

@EventHandler
fun onLeaveEvent(event: PlayerQuitEvent) {
autos.remove(event.player.uniqueId)
}

@EventHandler
fun onAutoWireEvent(event: BlockPlaceEvent) {
if (event.player.uniqueId !in autos) return
if (event.player.gameMode != GameMode.CREATIVE) return
if (!event.block.blockData.material.isSolid) return
if (event.blockPlaced.type.hasGravity()) return
with(event) {
arrayOf(
player.uniqueId !in autos,
player.gameMode != GameMode.CREATIVE,
!block.blockData.material.isSolid,
blockPlaced.type.hasGravity(),
).any { it }.ifTrue { return }
}
val wirePosition = event.blockPlaced.location.add(0.0, 1.0, 0.0)
if (wirePosition.block.type != Material.AIR) return
val airState = wirePosition.block.state
Expand All @@ -74,10 +76,14 @@ class Autowire(
pluginManager.callEvent(blockPlaceEvent)
if (blockPlaceEvent.isCancelled) return
wirePosition.block.type = Material.REDSTONE_WIRE
val wireData: RedstoneWire = Material.REDSTONE_WIRE.createBlockData() as RedstoneWire
val wireData = Material.REDSTONE_WIRE.createBlockData() as RedstoneWire
wireData.allowedFaces.forEach {
wireData.setFace(it, RedstoneWire.Connection.SIDE)
}
wirePosition.block.blockData = wireData
}
}

private inline fun Boolean.ifTrue(block: () -> Unit) {
if (this) block()
}
21 changes: 16 additions & 5 deletions src/main/kotlin/RStack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ class RStack(private val worldEdit: WorldEdit) : BaseCommand() {
args: Array<String>
) {
var expand = false
var withAir = false
var direction: String? = null
val numbers = mutableListOf<Int>()
for (arg in args) {
when {
// don't care about duplicate flags
arg == "-e" -> expand = true
arg == "-w" -> withAir = true
arg.all { it.isDigit() || it in "+-" } -> numbers.add(parseInt(arg))
else -> {
// probably a direction string
Expand All @@ -61,15 +63,22 @@ class RStack(private val worldEdit: WorldEdit) : BaseCommand() {
count *= -1
spacing *= -1
}
val affected = try {
doStack(BukkitAdapter.adapt(player), count, spacing, expand, direction ?: "me")
try {
doStack(BukkitAdapter.adapt(player), count, spacing, expand, withAir, direction ?: "me")
} catch (e: UnknownDirectionException) {
throw ConditionFailedException("Unknown direction")
}
}

// throws UnknownDirectionException
private fun doStack(player: WEPlayer, count: Int, spacing: Int, expand: Boolean, direction: String): Int {
private fun doStack(
player: WEPlayer,
count: Int,
spacing: Int,
expand: Boolean,
withAir: Boolean,
direction: String
): Int {
val session = worldEdit.sessionManager.get(player)
val selection = try {
// TODO: null check session.selectionWorld
Expand All @@ -88,7 +97,9 @@ class RStack(private val worldEdit: WorldEdit) : BaseCommand() {
isCopyingBiomes = false
isCopyingEntities = false
isRemovingEntities = false
sourceMask = ExistingBlockMask(editSession)
if (!withAir) {
sourceMask = ExistingBlockMask(editSession)
}
}
Operations.complete(copy)
session.remember(editSession)
Expand Down Expand Up @@ -141,7 +152,7 @@ class RStack(private val worldEdit: WorldEdit) : BaseCommand() {

private fun isDiagDirStr(direction: String, upOrDown: Char) =
// check length, because 'd' and 'u' alone are not diagonal directions (they're just up or down)
direction.length > 1 && direction.last().toLowerCase() == upOrDown
direction.length > 1 && direction.last().lowercaseChar() == upOrDown
}

private fun <E> List<E>.getOrDefault(index: Int, default: E): E = getOrNull(index) ?: default
Expand Down
24 changes: 14 additions & 10 deletions src/main/kotlin/RedstoneTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.sk89q.worldedit.util.formatting.text.format.TextColor
import org.bukkit.ChatColor
import org.bukkit.Material
import org.bukkit.plugin.java.JavaPlugin
import java.util.*
import java.util.logging.Level

const val MAKE_SELECTION_FIRST = "Make a region selection first."
Expand Down Expand Up @@ -50,11 +49,11 @@ class RedstoneTools : JavaPlugin() {
}
val worldEdit = wePlugin.worldEdit
val protocolManager = ProtocolLibrary.getProtocolManager()
val autowire = Autowire(protocolManager, server.pluginManager)
val autowire = Autowire(server.pluginManager)
arrayOf(
WorldEditHelper(this, worldEdit),
autowire,
SlabListener(),
autowire,
).forEach { server.pluginManager.registerEvents(it, this) }
PaperCommandManager(this).apply {
arrayOf(
Expand All @@ -63,8 +62,10 @@ class RedstoneTools : JavaPlugin() {
"find_page" to FindPageCompletionHandler(),
"search_page" to SearchPageCompletionHandler(),
).forEach { (id, handler) -> commandCompletions.registerCompletion(id, handler) }
registerThing(SignalStrength)
registerThing(SignalContainer)
arrayOf(
SignalStrength,
SignalContainer,
).forEach { registerThing(it) }
setDefaultExceptionHandler(::handleCommandException, false)
arrayOf(
RStack(worldEdit),
Expand All @@ -83,18 +84,19 @@ class RedstoneToolsException(message: String) : Exception(message)
private interface Thing<T> {
val readableName: String
fun of(arg: String): T?
val values: List<String>
val values: Collection<String>
val valueClass: Class<T>
}

private inline fun <reified T> PaperCommandManager.registerThing(thing: Thing<T>) {
val name = thing.readableName.replace(" ", "_").toLowerCase()
private fun <T> PaperCommandManager.registerThing(thing: Thing<T>) {
val name = thing.readableName.replace(" ", "_").lowercase()
val errorMessage = "${thing.readableName} must be one of ${thing.values}"
commandContexts.registerContext(T::class.java) { context ->
commandContexts.registerContext(thing.valueClass) { context ->
thing.of(context.popFirstArg()) ?: throw InvalidCommandArgument(errorMessage)
}
commandCompletions.apply {
registerStaticCompletion(name, thing.values)
setDefaultCompletion(name, T::class.java)
setDefaultCompletion(name, thing.valueClass)
}
}

Expand All @@ -110,6 +112,7 @@ class SignalStrength(val value: Int) {
private val hexValues = ('a'..'f').map(Char::toString)
override val values = intValues + hexValues
override val readableName = "Signal strength"
override val valueClass = SignalStrength::class.java
}
}

Expand All @@ -130,6 +133,7 @@ class SignalContainer(val material: Material) {
?.let { (_, material) -> SignalContainer(material) }

override val readableName = "Container"
override val valueClass = SignalContainer::class.java
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/main/kotlin/Slab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Slab : BaseCommand() {
}

private fun getSlab(type: String): ItemStack? {
val material = Material.getMaterial(type.toUpperCase()) ?: return null
val material = Material.getMaterial(type.uppercase()) ?: return null
if (!material.isBlock) return null
val blockData = material.createBlockData() as? Slab ?: return null
val itemStack = ItemStack(material, 1)
Expand Down Expand Up @@ -73,10 +73,8 @@ class SlabListener : Listener {

class SlabCompletionHandler :
CommandCompletions.CommandCompletionHandler<BukkitCommandCompletionContext> {
override fun getCompletions(context: BukkitCommandCompletionContext?): MutableCollection<String> =
Material.values().filter {
it.isBlock && (it.createBlockData() is Slab)
}.map {
it.toString().toLowerCase()
}.toMutableList()
override fun getCompletions(context: BukkitCommandCompletionContext): Collection<String> = Material
.values()
.filter { it.isBlock && it.createBlockData() is Slab }
.map { it.toString().lowercase() }
}

0 comments on commit b450bd3

Please sign in to comment.