Skip to content

Commit

Permalink
Permission added to all active players
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsimmo committed Sep 29, 2021
1 parent 227668e commit c58cb16
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

group = 'io.github.mdsimmo'
version = '0.6.1'
version = '0.6.2'

repositories {
mavenCentral()
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/github/mdsimmo/bomberman/Bomberman.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/io/github/mdsimmo/bomberman/commands/game/DevInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.github.mdsimmo.bomberman.commands.Cmd
import io.github.mdsimmo.bomberman.commands.Permission
import io.github.mdsimmo.bomberman.commands.Permissions
import io.github.mdsimmo.bomberman.messaging.Message
import io.github.mdsimmo.bomberman.messaging.SimpleContext
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.command.CommandSender
Expand All @@ -18,7 +19,7 @@ class DevInfo(parent: Cmd) : Cmd(parent) {

override fun options(sender: CommandSender, args: List<String>): List<String> {
return listOf("handlerlist", "handlercount", "handlerwatch", "nocancelled",
"tasklist", "taskcount", "taskwatch", "watch")
"tasklist", "taskcount", "taskwatch", "watch", "permissions")
}

override fun run(sender: CommandSender, args: List<String>, flags: Map<String, String>): Boolean {
Expand Down Expand Up @@ -123,14 +124,24 @@ class DevInfo(parent: Cmd) : Cmd(parent) {
sender.sendMessage("Watching for new tasks")
true
}
"permissions" -> {
if (args.size == 1) {
sender.effectivePermissions.forEach {
sender.sendMessage(" - ${it.permission}")
}
} else {
sender.sendMessage(args[1] + " : " + sender.hasPermission(args[1]))
}
true
}
else -> {
false
}
}
}

override fun permission(): Permission {
return Permissions.CREATE
return Permissions.BASE
}

override fun example(): Message {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/github/mdsimmo/bomberman/game/GamePlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.mdsimmo.bomberman.game
import io.github.mdsimmo.bomberman.Bomberman
import io.github.mdsimmo.bomberman.events.*
import io.github.mdsimmo.bomberman.messaging.Text
import io.github.mdsimmo.bomberman.utils.BukkitUtils
import org.bukkit.Bukkit
import org.bukkit.GameMode
import org.bukkit.Location
Expand All @@ -17,9 +18,13 @@ import org.bukkit.event.entity.EntityDamageEvent
import org.bukkit.event.entity.EntityRegainHealthEvent
import org.bukkit.event.player.*
import org.bukkit.inventory.ItemStack
import org.bukkit.permissions.PermissibleBase
import org.bukkit.permissions.PermissionAttachment
import org.bukkit.permissions.PermissionAttachmentInfo
import org.bukkit.potion.PotionEffect
import org.bukkit.potion.PotionEffectType
import java.io.File
import java.util.regex.Pattern

class GamePlayer private constructor(private val player: Player, private val game: Game) : Listener {

Expand Down Expand Up @@ -72,6 +77,10 @@ class GamePlayer private constructor(private val player: Player, private val gam

// Add tag for customisation
player.addScoreboardTag("bm_player")

// Add a permission group for WorldGuard compatibility
val playerPermissions = BukkitUtils.getPermissibleBase(player)
playerPermissions?.addAttachment(plugin, "group.bomberman", true)
}

fun bombStrength(game: Game, player: Player): Int {
Expand Down Expand Up @@ -133,6 +142,10 @@ class GamePlayer private constructor(private val player: Player, private val gam
player.teleport(location)

player.removeScoreboardTag("bm_player")

val playerPermissions = BukkitUtils.getPermissibleBase(player)
playerPermissions?.addAttachment(plugin, "group.bomberman", false)

file.delete()

removePotionEffects(player)
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/io/github/mdsimmo/bomberman/utils/BukkitUtils.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package io.github.mdsimmo.bomberman.utils

import io.github.mdsimmo.bomberman.Bomberman
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.World
import org.bukkit.entity.HumanEntity
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.PotionMeta
import org.bukkit.permissions.PermissibleBase
import org.bukkit.potion.PotionData
import org.bukkit.potion.PotionType
import org.bukkit.util.BoundingBox
import java.lang.reflect.Field
import java.util.regex.Pattern
import javax.annotation.CheckReturnValue

object BukkitUtils {
Expand Down Expand Up @@ -40,4 +46,50 @@ object BukkitUtils {
return Location(loc.world,
loc.blockX.toDouble(), loc.blockY.toDouble(), loc.blockZ.toDouble())
}

/**
* Attempts to get the PermissibleBase object of the player. Null if cannot be gained.
* @param player the player to get the PermissibleBase of
* @return the base
*/
fun getPermissibleBase(player: HumanEntity) : PermissibleBase? {
return try {
HUMAN_ENTITY_PERMISSIBLE_FIELD.value?.get(player) as? PermissibleBase
} catch (e: Exception) {
throw e
}
}

// The reflex field containing the PermissibleBase object
// Adapted from LuckPerms code:
// me.lucko.luckperms.bukkit.util.CraftBukkitImplementation
private val HUMAN_ENTITY_PERMISSIBLE_FIELD: Lazy<Field?> = lazy {
val humanEntityPermissibleField = try {
// CraftBukkit
val serverPacketVersion = run {
val server = Bukkit.getServer().javaClass
val matcher = Pattern.compile("^org\\.bukkit\\.craftbukkit\\.(\\w+)\\.CraftServer$")
.matcher(server.name)
if (matcher.matches()) {
'.' + matcher.group(1) + '.'
} else {
"."
}
}
val craftBukkitClassName = "org.bukkit.craftbukkit${serverPacketVersion}entity.CraftHumanEntity"
Class.forName(craftBukkitClassName).getDeclaredField("perm")
} catch (e: Exception) {
try {
// glowstone
Class.forName("net.glowstone.entity.GlowHumanEntity").getDeclaredField("permissions")
} catch (e: Exception) {
// Cannot find anything
Bomberman.instance.logger.warning("Unsupported server - Cannot modify players permissions.")
null
}
}
humanEntityPermissibleField?.isAccessible = true
humanEntityPermissibleField
}

}
24 changes: 14 additions & 10 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,59 @@ permissions:
bomberman.dictator: true
bomberman.operator: true
bomberman.player: true
default: op
bomberman.player:
description: Allows join/leave/info
children:
bomberman.join: true
bomberman.leave: true
bomberman.info: true
bomberman.list: true
default: true
bomberman.operator:
description: Allows control of games (start/stop)
children:
bomberman.start: true
bomberman.stop: true
bomberman.reload: true
default: op
bomberman.dictator:
description: Allows building/configuring games
children:
bomberman.create: true
bomberman.delete: true
bomberman.configure: true
default: op
bomberman.bm:
description: Root bomberman command (/bm)
default: true
bomberman.join:
description: /bm join
default: true
default: false
bomberman.leave:
description: /bm join
default: true
default: false
bomberman.info:
description: /bm info
default: true
default: false
bomberman.list:
description: /bm list
default: true
default: false
bomberman.start:
description: /bm start
default: op
default: false
bomberman.stop:
description: /bm stop
default: op
default: false
bomberman.reload:
description: /bm reload
default: op
default: false
bomberman.create:
description: /bm create
default: op
default: false
bomberman.delete:
description: /bm delete
default: op
default: false
bomberman.configure:
description: /bm configure
default: op
default: false

0 comments on commit c58cb16

Please sign in to comment.