From 1d5176e3eb506e35cd6805f15f914641ddde9aae Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Wed, 8 Jul 2020 20:38:14 +0200 Subject: [PATCH 1/4] Implement itemblocking & server online times --- src/main/kotlin/org/wlosp/varo/VaroPlugin.kt | 14 ++++- .../org/wlosp/varo/configuration/Config.kt | 9 +++ .../org/wlosp/varo/dependencies/Dependency.kt | 7 ++- .../varo/dependencies/DependencyLoader.kt | 27 ++++---- .../wlosp/varo/listeners/DamageListener.kt | 13 ++++ .../org/wlosp/varo/listeners/IteamBlocker.kt | 62 +++++++++++++++++++ .../wlosp/varo/listeners/ShutdownHandler.kt | 52 ++++++++++++++++ .../kotlin/org/wlosp/varo/util/TimeUtils.kt | 8 +++ src/main/resources/config.yml | 1 + 9 files changed, 176 insertions(+), 17 deletions(-) create mode 100644 src/main/kotlin/org/wlosp/varo/listeners/DamageListener.kt create mode 100644 src/main/kotlin/org/wlosp/varo/listeners/IteamBlocker.kt create mode 100644 src/main/kotlin/org/wlosp/varo/listeners/ShutdownHandler.kt create mode 100644 src/main/kotlin/org/wlosp/varo/util/TimeUtils.kt diff --git a/src/main/kotlin/org/wlosp/varo/VaroPlugin.kt b/src/main/kotlin/org/wlosp/varo/VaroPlugin.kt index 319178e..1751053 100644 --- a/src/main/kotlin/org/wlosp/varo/VaroPlugin.kt +++ b/src/main/kotlin/org/wlosp/varo/VaroPlugin.kt @@ -5,6 +5,9 @@ import org.bukkit.plugin.java.JavaPlugin import org.wlosp.varo.configuration.Config import org.wlosp.varo.dependencies.Dependency import org.wlosp.varo.dependencies.loadDependencies +import org.wlosp.varo.listeners.registerDamageListener +import org.wlosp.varo.listeners.registerItemBlockListeners +import org.wlosp.varo.listeners.registerShutdownHandler import kotlin.properties.Delegates /** @@ -13,7 +16,7 @@ import kotlin.properties.Delegates @Suppress("unused") class VaroPlugin : JavaPlugin() { private var dependencyManager by Delegates.notNull() - private var config by Delegates.notNull() + var varoConfig by Delegates.notNull() override fun onEnable() { dependencyManager = BukkitLibraryManager(this) @@ -21,11 +24,16 @@ class VaroPlugin : JavaPlugin() { saveDefaultConfig() loadConfig() + + // Events + registerDamageListener() + registerShutdownHandler() + registerItemBlockListeners() } private fun loadConfig() { - config = Config.fromFileConfiguration(getConfig()) - println(config) + varoConfig = Config.fromFileConfiguration(getConfig()) + println(varoConfig) } } diff --git a/src/main/kotlin/org/wlosp/varo/configuration/Config.kt b/src/main/kotlin/org/wlosp/varo/configuration/Config.kt index 7d02e14..d3d5e24 100644 --- a/src/main/kotlin/org/wlosp/varo/configuration/Config.kt +++ b/src/main/kotlin/org/wlosp/varo/configuration/Config.kt @@ -12,6 +12,7 @@ data class Config( val enableWhitelist: Boolean, val friendlyFire: Boolean, val fishingRod: Boolean, + val itemBlockStrategy: ItemBlockStrategy, val blockedItems: List, val time: TimeConfig, val tablist: TablistConfig, @@ -35,6 +36,11 @@ data class Config( NETHER_PORTAL } + enum class ItemBlockStrategy { + DELETE, + IGNORE + } + companion object { fun fromFileConfiguration(configuration: FileConfiguration): Config { @@ -43,6 +49,8 @@ data class Config( val enableWhitelist = configuration.getBoolean("enableWhitelist") val friendlyFire = configuration.getBoolean("friendlyFire") val fishingRod = configuration.getBoolean("fishingRod") + val itemBlockStrategy = configuration.getString("itemBlockStrategy")?.let { ItemBlockStrategy.valueOf(it) } + ?: error("Missing itemBlockStrategy property in config") val blockedItems = configuration.getStringList("blockedItems").map(Material::valueOf) val timeNode = configuration.getConfigurationSection("time") ?: error("Missing time property in config") @@ -88,6 +96,7 @@ data class Config( enableWhitelist, friendlyFire, fishingRod, + itemBlockStrategy, blockedItems, time, tablist, diff --git a/src/main/kotlin/org/wlosp/varo/dependencies/Dependency.kt b/src/main/kotlin/org/wlosp/varo/dependencies/Dependency.kt index 63794f2..6ef6b88 100644 --- a/src/main/kotlin/org/wlosp/varo/dependencies/Dependency.kt +++ b/src/main/kotlin/org/wlosp/varo/dependencies/Dependency.kt @@ -4,10 +4,11 @@ enum class Dependency( val groupId: String, val artifactId: String, val version: String, - val repository: String? = null + val repository: String? = null, + val dependencies: List = emptyList() ) { - SPIGLIN("com.github.johnnyjayjay", "spiglin", "2.0.3", "https://dl.bintray.com/johnnyjayjay/spiglin"), KORD("com.gitlab.kordlib.kord", "kord-core", "0.5.6"), - COROUTINES("org.jetbrains.kotlinx", "kotlinx-coroutines-jdk8", "1.3.4") + COROUTINES_CORE("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.3.4"), + COROUTINES("org.jetbrains.kotlinx", "kotlinx-coroutines-jdk8", "1.3.4", dependencies = listOf(COROUTINES_CORE)) } \ No newline at end of file diff --git a/src/main/kotlin/org/wlosp/varo/dependencies/DependencyLoader.kt b/src/main/kotlin/org/wlosp/varo/dependencies/DependencyLoader.kt index c067651..a9434bd 100644 --- a/src/main/kotlin/org/wlosp/varo/dependencies/DependencyLoader.kt +++ b/src/main/kotlin/org/wlosp/varo/dependencies/DependencyLoader.kt @@ -2,16 +2,21 @@ package org.wlosp.varo.dependencies import net.byteflux.libby.BukkitLibraryManager import net.byteflux.libby.Library +import net.byteflux.libby.LibraryManager fun BukkitLibraryManager.loadDependencies(vararg dependencies: Dependency) { - dependencies.forEach { dependency -> - dependency.repository?.let { addRepository(it) } - addJCenter() - val library = Library.builder() - .groupId(dependency.groupId) - .artifactId(dependency.artifactId) - .version(dependency.version) - .build() - loadLibrary(library) - } -} \ No newline at end of file + addJCenter() + addMavenCentral() + dependencies.forEach(::loadDependency) +} + +fun LibraryManager.loadDependency(dependency: Dependency) { + dependency.dependencies.forEach(::loadDependency) + dependency.repository?.let { addRepository(it) } + val library = Library.builder() + .groupId(dependency.groupId) + .artifactId(dependency.artifactId) + .version(dependency.version) + .build() + loadLibrary(library) +} diff --git a/src/main/kotlin/org/wlosp/varo/listeners/DamageListener.kt b/src/main/kotlin/org/wlosp/varo/listeners/DamageListener.kt new file mode 100644 index 0000000..c54eae6 --- /dev/null +++ b/src/main/kotlin/org/wlosp/varo/listeners/DamageListener.kt @@ -0,0 +1,13 @@ +package org.wlosp.varo.listeners + +import com.github.johnnyjayjay.spiglin.event.hear +import org.bukkit.entity.FishHook +import org.bukkit.event.entity.EntityDamageByEntityEvent +import org.bukkit.event.entity.EntityDamageEvent +import org.wlosp.varo.VaroPlugin + +fun VaroPlugin.registerDamageListener() = hear { + if (it is EntityDamageByEntityEvent) { + it.isCancelled = it.damager is FishHook && varoConfig.fishingRod + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/wlosp/varo/listeners/IteamBlocker.kt b/src/main/kotlin/org/wlosp/varo/listeners/IteamBlocker.kt new file mode 100644 index 0000000..84eddc4 --- /dev/null +++ b/src/main/kotlin/org/wlosp/varo/listeners/IteamBlocker.kt @@ -0,0 +1,62 @@ +package org.wlosp.varo.listeners + +import com.github.johnnyjayjay.spiglin.event.hear +import com.github.johnnyjayjay.spiglin.inventory.set +import org.bukkit.Material +import org.bukkit.entity.Player +import org.bukkit.event.block.BlockBreakEvent +import org.bukkit.event.block.BlockPlaceEvent +import org.bukkit.event.entity.EntityPickupItemEvent +import org.bukkit.event.player.PlayerDropItemEvent +import org.bukkit.event.player.PlayerJoinEvent +import org.bukkit.inventory.ItemStack +import org.wlosp.varo.VaroPlugin +import org.wlosp.varo.configuration.Config + +internal fun VaroPlugin.registerItemBlockListeners() { + hear { + if (!it.player.isAllowedToUseItem(it.itemDrop.itemStack.type, varoConfig.blockedItems)) { + it.itemDrop.remove() + } + } + + hear { + if ((it.entity as? Player)?.isAllowedToUseItem(it.item.itemStack.type, varoConfig.blockedItems) == false) { + it.isCancelled = true + it.item.remove() + } + } + + hear { + if (!it.player.isAllowedToUseItem(it.blockPlaced.type, varoConfig.blockedItems)) { + it.isCancelled = true + it.player.sanitizeInventory(varoConfig.blockedItems) + } + } + + hear { + if (!it.player.isAllowedToUseItem(it.block.type, varoConfig.blockedItems)) { + it.isCancelled = true + if (varoConfig.itemBlockStrategy == Config.ItemBlockStrategy.DELETE) { + it.block.setType(Material.AIR, false) + } + } + } + + hear { + val player = it.player + player.sanitizeInventory(varoConfig.blockedItems) + player.updateInventory() + } +} + +private fun Player.isAllowedToUseItem(type: Material?, blockedItems: List) = + hasPermission("varo.bypassItemBlock") || type !in blockedItems + +private fun Player.sanitizeInventory(blockedItems: List) { + inventory.forEachIndexed { index, itemStack: ItemStack? -> + if (isAllowedToUseItem(itemStack?.type, blockedItems)) { + inventory[index] = null + } + } +} diff --git a/src/main/kotlin/org/wlosp/varo/listeners/ShutdownHandler.kt b/src/main/kotlin/org/wlosp/varo/listeners/ShutdownHandler.kt new file mode 100644 index 0000000..26508cf --- /dev/null +++ b/src/main/kotlin/org/wlosp/varo/listeners/ShutdownHandler.kt @@ -0,0 +1,52 @@ +package org.wlosp.varo.listeners + +import com.github.johnnyjayjay.spiglin.event.hear +import com.github.johnnyjayjay.spiglin.onlinePlayers +import com.github.johnnyjayjay.spiglin.scheduler.run +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import kotlinx.coroutines.time.delay +import org.bukkit.event.player.PlayerLoginEvent +import org.wlosp.varo.VaroPlugin +import org.wlosp.varo.util.toHoursPart +import org.wlosp.varo.util.toMinutesPart +import java.time.* + +internal fun VaroPlugin.registerShutdownHandler() { + + val date = LocalDate.now(varoConfig.time.timezone) + val joinDate = with(varoConfig.time.startTime) { + OffsetDateTime.of( + date, + LocalTime.of(toHoursPart(), toMinutesPart()), + varoConfig.time.timezone.rules.getOffset(LocalDateTime.now()) + ) + } + + val kickDate = + with(varoConfig.time.shutdownTime) { + OffsetDateTime.of( + date, + LocalTime.of(toHoursPart(), toMinutesPart()), + varoConfig.time.timezone.rules.getOffset(LocalDateTime.now()) + ) + } + + GlobalScope.launch { + delay(Duration.between(OffsetDateTime.now(), kickDate)) + onlinePlayers.forEach { + this@registerShutdownHandler.run { + it.kickPlayer("Der server schließt!") + } + } + } + + hear { event -> + val now = OffsetDateTime.ofInstant(Instant.now(), varoConfig.time.timezone) + if (now.isBefore(joinDate) or now.isAfter(kickDate)) { + if (!event.player.hasPermission("varo.joinbypass")) { + event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Varo server is closed!") + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/wlosp/varo/util/TimeUtils.kt b/src/main/kotlin/org/wlosp/varo/util/TimeUtils.kt new file mode 100644 index 0000000..3fe553d --- /dev/null +++ b/src/main/kotlin/org/wlosp/varo/util/TimeUtils.kt @@ -0,0 +1,8 @@ +package org.wlosp.varo.util + +import java.time.Duration + + +fun Duration.toHoursPart(): Int = (toHours() % 24).toInt() + +fun Duration.toMinutesPart(): Int = (toMinutes() % 60).toInt() \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index d22ca73..1912d63 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -5,6 +5,7 @@ borderSize: 1 shrinkBorderOnDeath: 1 friendlyFire: false fishingRodPvp: true +itemBlockStrategy: DELETE # or IGNORE blockedItems: - STONE From 2dff829f7af779d10177c9dceb3f4d28c5e7ad7d Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Fri, 17 Jul 2020 20:54:08 +0200 Subject: [PATCH 2/4] Store teams in SQL - add color to setup conversation - add delete command - add reset command --- .idea/inspectionProfiles/Project_Default.xml | 1 - .idea/jarRepositories.xml | 10 ++ build.gradle.kts | 14 ++- src/main/kotlin/org/wlosp/varo/VaroPlugin.kt | 22 +++- .../org/wlosp/varo/commands/VaroCommand.kt | 12 ++ .../wlosp/varo/commands/VaroResetCommand.kt | 27 ++++ .../varo/commands/VaroTeamCreateCommand.kt | 76 ++++++++++++ .../varo/commands/VaroTeamDeleteCommand.kt | 34 +++++ .../org/wlosp/varo/configuration/Config.kt | 49 +++++++- .../org/wlosp/varo/converstaion/EnumPrompt.kt | 15 +++ .../converstaion/TeamSetupConversation.kt | 117 ++++++++++++++++++ .../org/wlosp/varo/database/DatabaseType.kt | 10 ++ .../org/wlosp/varo/dependencies/Dependency.kt | 26 +++- .../org/wlosp/varo/entities/VaroPlayer.kt | 52 ++++++++ .../org/wlosp/varo/entities/VaroTeam.kt | 23 ++++ .../wlosp/varo/listeners/ShutdownHandler.kt | 6 +- .../org/wlosp/varo/listeners/TeamHandler.kt | 31 +++++ .../kotlin/org/wlosp/varo/util/AccountUtil.kt | 37 ++++++ .../kotlin/org/wlosp/varo/util/Extensions.kt | 7 ++ .../org/wlosp/varo/util/LocationUtils.kt | 15 +++ .../wlosp/varo/util/ScoreboardTeamUtils.kt | 7 ++ .../kotlin/org/wlosp/varo/util/TimeUtils.kt | 1 - src/main/resources/config.yml | 11 ++ src/main/resources/plugin.yml | 6 +- 24 files changed, 593 insertions(+), 16 deletions(-) create mode 100644 src/main/kotlin/org/wlosp/varo/commands/VaroCommand.kt create mode 100644 src/main/kotlin/org/wlosp/varo/commands/VaroResetCommand.kt create mode 100644 src/main/kotlin/org/wlosp/varo/commands/VaroTeamCreateCommand.kt create mode 100644 src/main/kotlin/org/wlosp/varo/commands/VaroTeamDeleteCommand.kt create mode 100644 src/main/kotlin/org/wlosp/varo/converstaion/EnumPrompt.kt create mode 100644 src/main/kotlin/org/wlosp/varo/converstaion/TeamSetupConversation.kt create mode 100644 src/main/kotlin/org/wlosp/varo/database/DatabaseType.kt create mode 100644 src/main/kotlin/org/wlosp/varo/entities/VaroPlayer.kt create mode 100644 src/main/kotlin/org/wlosp/varo/entities/VaroTeam.kt create mode 100644 src/main/kotlin/org/wlosp/varo/listeners/TeamHandler.kt create mode 100644 src/main/kotlin/org/wlosp/varo/util/AccountUtil.kt create mode 100644 src/main/kotlin/org/wlosp/varo/util/Extensions.kt create mode 100644 src/main/kotlin/org/wlosp/varo/util/LocationUtils.kt create mode 100644 src/main/kotlin/org/wlosp/varo/util/ScoreboardTeamUtils.kt diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 417efce..3fe62a6 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -14,7 +14,6 @@