JorelAli / CommandAPI

A Bukkit/Spigot API for the command UI introduced in Minecraft 1.13
https://commandapi.jorel.dev
MIT License
529 stars 66 forks source link

Using withPermission with a custom permission only works after rejoining #498

Closed TheStegosaurus1 closed 11 months ago

TheStegosaurus1 commented 11 months ago

CommandAPI version

9.2.0

Minecraft version

1.20

Are you shading the CommandAPI?

Yes

What I did

    @Override
    public void onLoad() {
        CommandAPI.onLoad(new CommandAPIBukkitConfig(this).verboseOutput(true)); // Load with verbose output

        new CommandAPICommand("ping")
                .withPermission(CommandPermission.fromString("test.permission"))
                .executes((sender, args) -> {
                    sender.sendMessage("pong!");
                })
                .register();
    }

What actually happened

What should have happened

It should have shown the command on the first join instead of saying its unknown

Server logs and CommandAPI config

I start the server with this bat file:

@echo off
java -Xms2G -Xmx2G -jar paper-1.20.2-234.jar --nogui

Server Log:

Starting org.bukkit.craftbukkit.Main
System Info: Java 21 (OpenJDK 64-Bit Server VM 21+35-LTS) Host: Windows 10 10.0 (amd64)
Loading libraries, please wait...
[17:07:23 INFO]: Environment: Environment[accountsHost=https://api.mojang.com, sessionHost=https://sessionserver.mojang.com, servicesHost=https://api.minecraftservices.com, name=PROD]
[17:07:24 INFO]: Loaded 7 recipes
[17:07:24 INFO]: Starting minecraft server version 1.20.2
[17:07:24 INFO]: Loading properties
[17:07:24 INFO]: This server is running Paper version git-Paper-234 (MC: 1.20.2) (Implementing API version 1.20.2-R0.1-SNAPSHOT) (Git: f613437)
[17:07:25 INFO]: Server Ping Player Sample Count: 12
[17:07:25 INFO]: Using 4 threads for Netty based IO
[17:07:25 WARN]: [!] The timings profiler has been enabled but has been scheduled for removal from Paper in the future.
    We recommend installing the spark profiler as a replacement: https://spark.lucko.me/
    For more information please visit: https://github.com/PaperMC/Paper/issues/8948
[17:07:25 INFO]: [ChunkTaskScheduler] Chunk system is using 1 I/O threads, 5 worker threads, and gen parallelism of 5 threads
[17:07:25 INFO]: Default game type: SURVIVAL
[17:07:25 INFO]: Generating keypair
[17:07:25 INFO]: Starting Minecraft server on *:25565
[17:07:25 INFO]: Using default channel type
[17:07:25 INFO]: Paper: Using Java compression from Velocity.
[17:07:25 INFO]: Paper: Using Java cipher from Velocity.
[17:07:25 INFO]: [Test] Loading server plugin Test v1.0
[17:07:25 INFO]: [CommandAPI] Loaded platform NMS_1_20_R2 > NMS_Common > CommandAPIBukkit
[17:07:25 WARN]: [CommandAPI] Could not hook into the NBT API for NBT support. Download it from https://www.spigotmc.org/resources/nbt-api.7939/
[17:07:25 INFO]: [CommandAPI] Hooked into Spigot successfully for Chat/ChatComponents
[17:07:25 INFO]: [CommandAPI] Hooked into Adventure for AdventureChat/AdventureChatComponents
[17:07:25 INFO]: [CommandAPI] Hooked into Paper for paper-specific API implementations
[17:07:25 INFO]: [CommandAPI] Registering command /ping
[17:07:25 INFO]: Server permissions file permissions.yml is empty, ignoring it
[17:07:25 INFO]: Preparing level "world"
[17:07:25 INFO]: Preparing start region for dimension minecraft:overworld
[17:07:25 INFO]: Time elapsed: 153 ms
[17:07:25 INFO]: Preparing start region for dimension minecraft:the_nether
[17:07:26 INFO]: Time elapsed: 20 ms
[17:07:26 INFO]: Preparing start region for dimension minecraft:the_end
[17:07:26 INFO]: Time elapsed: 13 ms
[17:07:26 WARN]: Plugin Test v1.0 tried to register permission 'test.permission' but it's already registered
[17:07:26 INFO]: [Test] Enabling Test v1.0
[17:07:26 INFO]: [CommandAPI] Hooked into Paper ServerResourcesReloadedEvent
[17:07:26 INFO]: Running delayed init tasks
[17:07:26 INFO]: [CommandAPI] Linking permissions to commands:
[17:07:26 INFO]: [CommandAPI]   test.permission -> /ping
[17:07:26 INFO]: [CommandAPI] Linked 1 Bukkit permissions to commands
[17:07:26 INFO]: [CommandAPI] Reloading datapacks...
[17:07:26 INFO]: Loaded 7 recipes
[17:07:26 INFO]: [CommandAPI] Finished reloading datapacks
[17:07:26 INFO]: Done (2.265s)! For help, type "help"
[17:07:26 INFO]: Timings Reset
[17:07:35 INFO]: UUID of player TheStegosaurus_ is f1bb7d85-a529-481e-b931-12cd33887cca
[17:07:36 INFO]: TheStegosaurus_ joined the game
[17:07:36 INFO]: TheStegosaurus_[/127.0.0.1:58666] logged in with entity id 218 at ([world]-51.781821034020716, 64.0, -69.66196735495392)
[17:07:39 INFO]: TheStegosaurus_ issued server command: /ping
[17:07:40 INFO]: TheStegosaurus_ lost connection: Disconnected
[17:07:40 INFO]: TheStegosaurus_ left the game
[17:07:44 INFO]: UUID of player TheStegosaurus_ is f1bb7d85-a529-481e-b931-12cd33887cca
[17:07:44 INFO]: TheStegosaurus_ joined the game
[17:07:44 INFO]: TheStegosaurus_[/127.0.0.1:58675] logged in with entity id 226 at ([world]-51.781821034020716, 64.0, -69.66196735495392)
[17:07:46 INFO]: TheStegosaurus_ issued server command: /ping

Other

My entire Test.java file:

package me.thestegosaurus.test;

import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIBukkitConfig;
import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.CommandPermission;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.permissions.PermissionAttachment;

public class Test extends JavaPlugin implements Listener {

    @Override
    public void onLoad() {
        CommandAPI.onLoad(new CommandAPIBukkitConfig(this).verboseOutput(true)); // Load with verbose output

        new CommandAPICommand("ping")
                .withPermission(CommandPermission.fromString("test.permission"))
                .executes((sender, args) -> {
                    sender.sendMessage("pong!");
                })
                .register();
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        event.getPlayer().sendMessage(Component.text("Setting Permission..."));
        PermissionAttachment attachment = event.getPlayer().addAttachment(this);
        attachment.setPermission("test.permission", true);

        if(event.getPlayer().hasPermission("test.permission")) {
            event.getPlayer().sendMessage(Component.text("Set Permission!"));
        }
    }

    @Override
    public void onEnable() {
        Bukkit.getPluginManager().registerEvents(this, this);
        CommandAPI.onEnable();
    }

    @Override
    public void onDisable() {
        CommandAPI.onDisable();
    }

}

My entire build.gradle file:

plugins {
    id 'java'
    id 'com.github.johnrengelman.shadow' version '7.1.2'
}

group = 'me.thestegosaurus.test'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral()
    maven { url = "https://repo.papermc.io/repository/maven-public/" }
    maven { url = "https://repo.codemc.org/repository/maven-public/" }

}

dependencies {
    compileOnly "io.papermc.paper:paper-api:1.20.2-R0.1-SNAPSHOT"
    implementation "dev.jorel:commandapi-bukkit-shade:9.2.0"
}

shadowJar {
    dependencies {
        include dependency("dev.jorel:commandapi-bukkit-shade:9.2.0")
    }

    relocate("dev.jorel.commandapi", "me.thestegosaurus.test.commandapi")
}

java {
    toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}

My entire plugin.yml file:

name: Test
version: '1.0'
main: me.thestegosaurus.test.Test
api-version: '1.20'
permissions:
  test.permission:
    description: Test Permission
DerEchtePilz commented 11 months ago

After you set the permission, you need to call CommandAPI#updateRequirements That resends the command packet and the command should appear correctly

TheStegosaurus1 commented 11 months ago

Thanks that fixed it!