1.16.5 Why aren't my permissions working?

Discussion in 'Spigot Plugin Development' started by TheAspie_, Jun 27, 2021.

  1. So I'm trying to integrate a simple permissions system into my plugin, so that if you die you get the "onetry.death" permission and cannot break, place or damage anything and also be put in creative mode. I don't have a quit EventHandler because if a player logs back in the need to keep the permission. I'm fairly new to plugin developing so I mihgt be missing something obvious lol

    package me.theaspie.onetryplugin;

    import org.bukkit.*;
    import org.bukkit.entity.*;
    import org.bukkit.event.*;
    import org.bukkit.event.block.*;
    import org.bukkit.event.entity.*;
    import org.bukkit.event.inventory.*;
    import org.bukkit.event.player.*;
    import org.bukkit.permissions.*;
    import org.bukkit.plugin.*;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.*;

    public final class OneTryPlugin extends JavaPlugin implements Listener {
    public HashMap<UUID, PermissionAttachment> playerPermissions = new HashMap<>();

    @Override
    public void onEnable() {
    // Plugin startup logic
    Plugin plugin = this;
    this.getConfig().options().copyDefaults(true);
    this.saveConfig();
    this.getServer().getPluginManager().registerEvents(this, this);


    }
    @EventHandler
    public void join(PlayerJoinEvent event) {
    Player player = event.getPlayer();
    setupPermissions(player);
    }

    @EventHandler
    public void death(PlayerDeathEvent event) {
    Player player = (Player) event.getEntity();
    setupPermissions(player);
    }



    @EventHandler
    public void move(PlayerToggleSneakEvent event) {
    Player player = event.getPlayer();
    if (player.hasPermission("onetry.dead")) {
    player.sendMessage("You're dead");
    } else {
    if (player.hasPermission("onetry.alive")) {
    player.sendMessage("You're alive");
    } else {
    player.sendMessage("you're not dead or alive");
    }
    }
    }

    public void setupPermissions(Player player) {
    PermissionAttachment attachment = player.addAttachment(this);
    this.playerPermissions.put(player.getUniqueId(), attachment);

    permissionsSetter(player.getUniqueId());
    }


    private void permissionsSetter(UUID uuid) {
    PermissionAttachment attachment = this.playerPermissions.get(uuid);

    for (String groups : this.getConfig().getConfigurationSection("Groups").getKeys(false)) {
    for (String permissions : this.getConfig().getStringList("Groups." + groups + ".permissions")) {
    attachment.setPermission(permissions, true);
    }
    }
    }

    @EventHandler
    public void onRespawn (PlayerRespawnEvent event) {
    Player player = event.getPlayer();
    if (player.hasPermission("onetry.dead")) {
    player.setGameMode(GameMode.CREATIVE);
    }
    }

    @EventHandler
    public void onBreak (BlockPlaceEvent event){
    Player player = (Player) event.getPlayer();
    if (player.hasPermission("onetry.dead")) {
    event.setCancelled(true);
    } else {return;}
    }

    @EventHandler
    public void onDamageDealt (EntityDamageByEntityEvent event){
    Player player = (Player) event.getDamager();
    if (player.hasPermission("onetry.dead")){
    event.setCancelled(true);
    } else { return; }
    }
    @EventHandler
    public void removePerms (PlayerToggleSprintEvent event) {
    Player player = event.getPlayer();
    playerPermissions.remove(player.getUniqueId());
    }




    @Override
    public void onDisable () {
    // Plugin shutdown logic
    playerPermissions.clear();
    }
    }
     
  2. Where‘s the problem?
     
  3. The permissions aren't given when somebody dies, and a lot of the Event Handlers don't work either, as if I go op, then I can't deal damage but I can break and place blocks
     
    #3 TheAspie_, Jun 28, 2021
    Last edited: Jun 28, 2021