[SPIGOT-2618] Exception when permissions from plugin.yml are already created in onEnable() / Permissions from plugin.yml are loaded after onEnable() Created: 20/Aug/16  Updated: 25/Aug/16  Resolved: 25/Aug/16

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Dragorn421 Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: Craftbukkit, error, onEnable
Environment:

Java 8, Windows 10 64 bits
Java 8, Debian Jessie 64 bits



 Description   

When a permission is set in the plugin.yml, it is loaded after the plugin's onEnable(). The problem is that when the permission is loaded, there is no check to see if the permission is already registered or not, which leads into an error if the permission was added in the plugin's onEnable(). (adding a check would not fix the issue though, keep reading)

To reproduce, declare a permission in the plugin.yml:

permissions:  permission.test.foo:   default: op

Then add it in the onEnable():

String permissionName = "permission.test.foo";
Permission permission = Bukkit.getPluginManager().getPermission(permissionName);
if(permission == null)
{
	permission = new Permission(permissionName);
	Bukkit.getPluginManager().addPermission(permission);
}

And you will get the following exception:
WARN Plugin PluginName v0.1 tried to register permission 'permission.test.foo' but it's already registered
java.lang.IllegalArgumentException: The permission permission.test.foo is already defined!
at org.bukkit.plugin.SimplePluginManager.addPermission(SimplePluginManager.java:598) ~[craftbukkit.jar:git-Bukkit-5e5cf84]
at org.bukkit.craftbukkit.v1_10_R1.CraftServer.loadPlugin(CraftServer.java:353) [craftbukkit.jar:git-Bukkit-5e5cf84]
at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugins(CraftServer.java:319) [craftbukkit.jar:git-Bukkit-5e5cf84]
at net.minecraft.server.v1_10_R1.MinecraftServer.t(MinecraftServer.java:404) [craftbukkit.jar:git-Bukkit-5e5cf84]
at net.minecraft.server.v1_10_R1.MinecraftServer.l(MinecraftServer.java:369) [craftbukkit.jar:git-Bukkit-5e5cf84]
at net.minecraft.server.v1_10_R1.MinecraftServer.a(MinecraftServer.java:324) [craftbukkit.jar:git-Bukkit-5e5cf84]
at net.minecraft.server.v1_10_R1.DedicatedServer.init(DedicatedServer.java:243) [craftbukkit.jar:git-Bukkit-5e5cf84]
at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:514) [craftbukkit.jar:git-Bukkit-5e5cf84]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]

How to fix: (imho)
Load the plugin.yml permissions before onEnable() runs, maybe before onLoad() even.
You can't simply add a check to make sure the permission is not already present because then the permission configuration from plugin.yml would be ignored/overriden by onEnable(), which is not something you would want.
That said, I'd say the issue is about permissions from plugin.yml not being loaded before onEnable().

I did not check with Spigot but I don't see a reason that this behavior would be different.



 Comments   
Comment by Dragorn421 [ 21/Aug/16 ]

My plugin lets you create visits that take you around. When a visit is created a permission object with permission "visit." + visitName is associated with it, and I want this permission to be a child of "visit.*".

There is no problems for visits added after the plugin was enabled (eg using commands), as it gets either the permission object created by some visit or the one created using plugin.yml permissions, but the ones that are loaded from the configuration are loaded from onEnable() and therefore do not find the "visit.*" permission. So I have to create it, and then the error happens.

I think your question was more about "why register permissions in plugin.yml". I prefer to put all permissions I can in the plugin.yml, that way I don't have to keep track of them somewhere else and I can easily change the default values. And I don't see the point of the permissions configuration in plugin.yml if you can't make use of those x)

Comment by md_5 [ 20/Aug/16 ]

I get this, but what I don't get is why your plugin would need to register permissions both in code and in the yaml

Generated at Thu Mar 27 16:22:49 UTC 2025 using Jira 10.3.3#10030003-sha1:d220e3fefc8dfc6d47f522d3b9a20c1455e12b7b.