Skip to content

Commit

Permalink
Prevent binds
Browse files Browse the repository at this point in the history
  • Loading branch information
Tems-py committed Nov 7, 2023
1 parent 81900d9 commit db91ae3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ repositories {
name = "sonatype"
url = "https://oss.sonatype.org/content/groups/public/"
}

}

dependencies {
Expand Down
51 changes: 40 additions & 11 deletions src/main/java/org/tems/ttotem/Listeners/InventoryClickEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,79 @@ import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.Material
import org.bukkit.entity.EntityType
import org.bukkit.entity.HumanEntity
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.entity.EntityResurrectEvent
import org.bukkit.event.inventory.ClickType
import org.bukkit.event.inventory.InventoryClickEvent
import org.tems.ttotem.Tetotem
import java.util.*
import kotlin.collections.HashMap

class InventoryClickEvent(private val plugin: Tetotem) : Listener {
private var flags: HashMap<UUID, Int> = HashMap<UUID, Int>()
private var lastClick: HashMap<UUID, Long> = HashMap<UUID, Long>()

@EventHandler
fun inventoryClickEvent(event: InventoryClickEvent) {
if (autoTotemChecks(event)) return
if (plugin.config.getBoolean("detectFastTotemEquipInInventory")){
bindTotemChecks(event)
}
val player = event.whoClicked
lastClick[player.uniqueId] = plugin.i
}

private fun bindTotemChecks(event: InventoryClickEvent){
val player = event.whoClicked
if (!plugin.config.getBoolean("detectFastTotemEquipInInventory")) return
if (event.click != ClickType.UNKNOWN) return
if (event.slot != 40) return

logFlag(player, "detect type - UNKNOWN")
punishAndRegisterFlag(player)
}

private fun autoTotemChecks(event: InventoryClickEvent) : Boolean {
val item = event.cursor
val player = event.whoClicked
val ticks = plugin.config.getInt("maximumAllowedTicksToChangeTotem")
if (item.type != Material.TOTEM_OF_UNDYING || event.slot != 40) return
if (plugin.lastTotem[player.uniqueId] == null) return
val ticks = plugin.config.getInt("maximumAllowedTicksToChangeTotemAfterPop")
if (item.type != Material.TOTEM_OF_UNDYING || event.slot != 40) return false
if (plugin.lastTotem[player.uniqueId] == null) return false

val lastUse = plugin.lastTotem[player.uniqueId] ?: return
if (plugin.i.minus(lastUse) > ticks) return
val lastUse = plugin.lastTotem[player.uniqueId] ?: return false
if (plugin.i.minus(lastUse) > ticks) return false

val mm = MiniMessage.miniMessage();
logFlag(player, ticks.toString())
punishAndRegisterFlag(player)
return true
}

private fun logFlag(player: HumanEntity, ticks: String){
if (plugin.config.getString("adminMessage") == null) {
println("\"adminMessage\" is missing in config")
return
}

val mm = MiniMessage.miniMessage();
val message = plugin.config.getString("adminMessage").toString().replace("%player%", player.name)
.replace("%ticks%", ticks.toString())
.replace("%ticks%", ticks)
val parsed: Component = mm.deserialize(message)
val consoleMessage = plugin.config.getString("consoleMessage").toString().replace("%player%", player.name)
.replace("%ticks%", ticks.toString())
.replace("%ticks%", ticks)
plugin.logger.info(consoleMessage)

for (lp in plugin.server.onlinePlayers) {
if (lp.hasPermission("ttotem.admin")) {
lp.sendMessage(parsed)
}
}
}

private fun punishAndRegisterFlag(player: HumanEntity) {
val tps = plugin.config.getDouble("minimumTpsForFlagsCount")
if (plugin.server.tps[0] < tps) return


if (flags[player.uniqueId] == null) {
flags[player.uniqueId] = 1
} else {
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# ----------
# Auto Totem
# ----------
# If player moves totem from inventory faster than this number he will be flagged as cheater
maximumAllowedTicksToChangeTotem: 10
maximumAllowedTicksToChangeTotemAfterPop: 10
# If server's TPS are lower than this player won't get punished but will show logs to console and admins
minimumTpsForFlagsCount: 17.5

# Prevent people using "binds" for auto equipping totems
detectUnknownTypeActionsWhenSwitchingToOffhand: true

punishAfterFlags: 3
# Commands that will be executed after reaching desired number of flags. Use %player% for player's name
punishments:
Expand Down

0 comments on commit db91ae3

Please sign in to comment.