generated from MiniPlaceholders/Example-Expansion
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
144 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
dependencies { | ||
compileOnly(libs.miniplaceholders) | ||
compileOnly(libs.adventure.api) | ||
compileOnly(libs.adventure.minimessage) | ||
compileOnly(libs.luckperms) | ||
} |
105 changes: 105 additions & 0 deletions
105
.../src/main/java/io/github/miniplaceholders/expansion/luckperms/common/CommonExpansion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package io.github.miniplaceholders.expansion.luckperms.common; | ||
|
||
import io.github.miniplaceholders.api.Expansion; | ||
import io.github.miniplaceholders.api.utils.LegacyUtils; | ||
import net.kyori.adventure.audience.Audience; | ||
import net.kyori.adventure.identity.Identity; | ||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.format.NamedTextColor; | ||
import net.kyori.adventure.text.minimessage.tag.Tag; | ||
import net.luckperms.api.LuckPerms; | ||
import net.luckperms.api.model.group.Group; | ||
import net.luckperms.api.model.user.User; | ||
import net.luckperms.api.util.Tristate; | ||
|
||
import java.util.UUID; | ||
|
||
import static io.github.miniplaceholders.api.utils.Components.*; | ||
|
||
public record CommonExpansion(LuckPerms luckPerms) { | ||
private static final Component UNDEFINED_COMPONENT = Component.text("undefined", NamedTextColor.GRAY); | ||
|
||
public Expansion.Builder commonBuilder() { | ||
return Expansion.builder("luckperms") | ||
.audiencePlaceholder("prefix", (aud, queue, ctx) -> { | ||
final User user = user(aud); | ||
if (user == null) { | ||
return null; | ||
} | ||
return Tag.inserting(LegacyUtils.parsePossibleLegacy(user.getCachedData().getMetaData().getPrefix())); | ||
}) | ||
.audiencePlaceholder("suffix", (aud, queue, ctx) -> { | ||
final User user = user(aud); | ||
if (user == null) { | ||
return null; | ||
} | ||
return Tag.inserting(LegacyUtils.parsePossibleLegacy(user.getCachedData().getMetaData().getSuffix())); | ||
}) | ||
.audiencePlaceholder("has_permission", (aud, queue, ctx) -> { | ||
final User user = user(aud); | ||
if (user == null) { | ||
return null; | ||
} | ||
String permission = queue.popOr(() -> "you need to introduce an permission").value(); | ||
Tristate result = user.getCachedData().getPermissionData().checkPermission(permission); | ||
return Tag.selfClosingInserting(result.asBoolean() | ||
? TRUE_COMPONENT | ||
: FALSE_COMPONENT | ||
); | ||
}) | ||
.audiencePlaceholder("check_permission", (aud, queue, ctx) -> { | ||
final User user = user(aud); | ||
if (user == null) { | ||
return null; | ||
} | ||
final String permission = queue.popOr(() -> "you need to introduce an permission").value(); | ||
final Tristate result = user.getCachedData().getPermissionData().checkPermission(permission); | ||
|
||
return Tag.selfClosingInserting(switch(result) { | ||
case TRUE -> TRUE_COMPONENT; | ||
case FALSE -> FALSE_COMPONENT; | ||
case UNDEFINED -> UNDEFINED_COMPONENT; | ||
}); | ||
}) | ||
.audiencePlaceholder("inherited_groups", (aud, queue, ctx) -> { | ||
final User user = user(aud); | ||
if (user == null) { | ||
return null; | ||
} | ||
final Component groups = user.getInheritedGroups(user.getQueryOptions()).stream() | ||
.map(group -> LegacyUtils.parsePossibleLegacy(group.getDisplayName())) | ||
.collect(Component.toComponent(Component.text(", "))); | ||
return Tag.selfClosingInserting(groups); | ||
}) | ||
.audiencePlaceholder("primary_group_name", (aud, queue, ctx) -> { | ||
final User user = user(aud); | ||
if (user == null) { | ||
return null; | ||
} | ||
final String primaryGroup = user.getCachedData().getMetaData().getPrimaryGroup(); | ||
if (primaryGroup == null) { | ||
return null; | ||
} | ||
return Tag.preProcessParsed(primaryGroup); | ||
}) | ||
.audiencePlaceholder("inherits_group", (aud, queue, ctx) -> { | ||
final User user = user(aud); | ||
if (user == null) { | ||
return null; | ||
} | ||
Group group = luckPerms.getGroupManager().getGroup(queue.popOr("you need to provide a group").value()); | ||
return Tag.selfClosingInserting(group != null && user.getInheritedGroups(user.getQueryOptions()).contains(group) | ||
? TRUE_COMPONENT | ||
: FALSE_COMPONENT | ||
); | ||
}); | ||
} | ||
|
||
private User user(final Audience audience) { | ||
final UUID uuid = audience.get(Identity.UUID).orElse(null); | ||
if (uuid == null) { | ||
return null; | ||
} | ||
return luckPerms.getUserManager().getUser(uuid); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 7 additions & 66 deletions
73
paper/src/main/java/io/github/miniplaceholders/expansion/luckperms/paper/PaperPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,30 @@ | ||
package io.github.miniplaceholders.expansion.luckperms.paper; | ||
|
||
import io.github.miniplaceholders.api.Expansion; | ||
import io.github.miniplaceholders.api.utils.LegacyUtils; | ||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.format.NamedTextColor; | ||
import net.kyori.adventure.text.minimessage.tag.Tag; | ||
import io.github.miniplaceholders.expansion.luckperms.common.CommonExpansion; | ||
import net.luckperms.api.LuckPerms; | ||
import net.luckperms.api.model.group.Group; | ||
import net.luckperms.api.model.user.User; | ||
import net.luckperms.api.platform.PlayerAdapter; | ||
import net.luckperms.api.util.Tristate; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.plugin.RegisteredServiceProvider; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
@SuppressWarnings("unused") | ||
public final class PaperPlugin extends JavaPlugin { | ||
private static final Component FALSE_COMPONENT = Component.text(false, NamedTextColor.RED); | ||
private static final Component TRUE_COMPONENT = Component.text(true, NamedTextColor.GREEN); | ||
private static final Component UNDEFINED_COMPONENT = Component.text("undefined", NamedTextColor.GRAY); | ||
@Override | ||
public void onEnable(){ | ||
this.getSLF4JLogger().info("Starting LuckPerms Expansion for Paper"); | ||
|
||
RegisteredServiceProvider<LuckPerms> provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class); | ||
final RegisteredServiceProvider<LuckPerms> provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class); | ||
if (provider == null) { | ||
this.getSLF4JLogger().error("Cannot found LuckPerms plugin, disabling expansion"); | ||
Bukkit.getPluginManager().disablePlugin(this); | ||
return; | ||
} | ||
|
||
LuckPerms luckPerms = provider.getProvider(); | ||
final LuckPerms luckPerms = provider.getProvider(); | ||
|
||
PlayerAdapter<Player> adapter = luckPerms.getPlayerAdapter(Player.class); | ||
|
||
Expansion.builder("luckperms") | ||
.filter(Player.class) | ||
.audiencePlaceholder("prefix", (aud, queue, ctx) -> | ||
Tag.inserting(LegacyUtils.parsePossibleLegacy(adapter.getMetaData((Player)aud).getPrefix()))) | ||
.audiencePlaceholder("suffix", (aud, queue, ctx) -> | ||
Tag.inserting(LegacyUtils.parsePossibleLegacy(adapter.getMetaData((Player)aud).getSuffix()))) | ||
.audiencePlaceholder("has_permission", (aud, queue, ctx) -> { | ||
User user = adapter.getUser((Player)aud); | ||
String permission = queue.popOr(() -> "you need to introduce an permission").value(); | ||
Tristate result = user.getCachedData().getPermissionData().checkPermission(permission); | ||
return Tag.selfClosingInserting(result.asBoolean() | ||
? TRUE_COMPONENT | ||
: FALSE_COMPONENT | ||
); | ||
}) | ||
.audiencePlaceholder("check_permission", (aud, queue, ctx) -> { | ||
User user = adapter.getUser((Player)aud); | ||
String permission = queue.popOr(() -> "you need to introduce an permission").value(); | ||
Tristate result = user.getCachedData().getPermissionData().checkPermission(permission); | ||
|
||
return Tag.selfClosingInserting(switch(result) { | ||
case TRUE -> TRUE_COMPONENT; | ||
case FALSE -> FALSE_COMPONENT; | ||
case UNDEFINED -> UNDEFINED_COMPONENT; | ||
}); | ||
}) | ||
.audiencePlaceholder("inherited_groups", (aud, queue, ctx) -> { | ||
User user = adapter.getUser((Player)aud); | ||
Component groups = user.getInheritedGroups(user.getQueryOptions()).stream() | ||
.map(group -> LegacyUtils.parsePossibleLegacy(group.getDisplayName())) | ||
.collect(Component.toComponent(Component.space())); | ||
return Tag.selfClosingInserting(groups); | ||
}) | ||
.audiencePlaceholder("primary_group_name", (aud, queue, ctx) -> { | ||
final String primaryGroup = adapter.getUser((Player)aud).getCachedData().getMetaData().getPrimaryGroup(); | ||
if (primaryGroup == null) { | ||
return null; | ||
} | ||
return Tag.preProcessParsed(primaryGroup); | ||
}) | ||
.audiencePlaceholder("inherits_group", (aud, queue, ctx) -> { | ||
User user = adapter.getUser((Player)aud); | ||
Group group = luckPerms.getGroupManager().getGroup(queue.popOr(() -> "you need to provide a group").value()); | ||
return Tag.selfClosingInserting(group != null && user.getInheritedGroups(user.getQueryOptions()).contains(group) | ||
? TRUE_COMPONENT | ||
: FALSE_COMPONENT | ||
); | ||
}) | ||
.build() | ||
.register(); | ||
new CommonExpansion(luckPerms).commonBuilder() | ||
.filter(Player.class) | ||
.build() | ||
.register(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") | ||
rootProject.name = "Luckperms-Expansion" | ||
|
||
arrayOf("paper", "velocity").forEach { | ||
arrayOf("paper", "velocity", "common").forEach { | ||
include("luckperms-expansion-$it") | ||
project(":luckperms-expansion-$it").projectDir = file(it) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters