diff --git a/build.gradle b/build.gradle index d4f61ca..398519a 100644 --- a/build.gradle +++ b/build.gradle @@ -20,6 +20,7 @@ repositories { dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compileOnly group: 'com.destroystokyo.paper', name: 'paper-api', version: '1.15.2-R0.1-SNAPSHOT' + compileOnly group: 'net.luckperms', name: 'api', version: '5.0' compile group: 'io.papermc', name: 'paperlib', version: '1.0.2' } @@ -39,7 +40,7 @@ buildscript { } shadowJar { - relocate 'io.papermc.lib', 'org.popcraft.paperlib' + relocate 'io.papermc.lib', 'org.popcraft.stress.paperlib' } build { diff --git a/gradle.properties b/gradle.properties index 323d967..f2bce18 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ pluginGroup=org.popcraft -pluginVersion=1.0.1 +pluginVersion=1.0.2 diff --git a/src/main/java/org/popcraft/stress/LuckPermsContexts.java b/src/main/java/org/popcraft/stress/LuckPermsContexts.java new file mode 100644 index 0000000..8fa0589 --- /dev/null +++ b/src/main/java/org/popcraft/stress/LuckPermsContexts.java @@ -0,0 +1,75 @@ +package org.popcraft.stress; + +import net.luckperms.api.LuckPerms; +import net.luckperms.api.context.ContextCalculator; +import net.luckperms.api.context.ContextConsumer; +import net.luckperms.api.context.ContextSet; +import net.luckperms.api.context.ImmutableContextSet; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.RegisteredServiceProvider; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class LuckPermsContexts { + private static LuckPerms luckPerms; + private static Set> contextCalculators; + private static final String tpsAboveKey = "stress:tps-above"; + private static final String tpsBelowKey = "stress:tps-below"; + + static { + RegisteredServiceProvider provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class); + if (provider != null) { + luckPerms = provider.getProvider(); + contextCalculators = new HashSet<>(); + } + } + + public static void register() { + if (luckPerms == null) { + return; + } + ContextCalculator contextTPSAbove = new ContextCalculator() { + @Override + public void calculate(Player target, ContextConsumer consumer) { + int currentTps = (int) Math.floor(Stress.getPlugin().getTickProfiler().getInterval("shortest").getResult().tps); + List tpsValues = IntStream.range(currentTps, 22).mapToObj(Integer::toString).collect(Collectors.toList()); + tpsValues.forEach(value -> consumer.accept(tpsAboveKey, value)); + } + + @Override + public ContextSet estimatePotentialContexts() { + return ImmutableContextSet.empty(); + } + }; + luckPerms.getContextManager().registerCalculator(contextTPSAbove); + contextCalculators.add(contextTPSAbove); + ContextCalculator contextTPSBelow = new ContextCalculator() { + @Override + public void calculate(Player target, ContextConsumer consumer) { + int currentTps = (int) Math.floor(Stress.getPlugin().getTickProfiler().getInterval("shortest").getResult().tps); + List tpsValues = IntStream.range(0, currentTps + 1).mapToObj(Integer::toString).collect(Collectors.toList()); + tpsValues.forEach(value -> consumer.accept(tpsBelowKey, value)); + } + + @Override + public ContextSet estimatePotentialContexts() { + return ImmutableContextSet.empty(); + } + }; + luckPerms.getContextManager().registerCalculator(contextTPSBelow); + contextCalculators.add(contextTPSBelow); + } + + public static void unregister() { + if (luckPerms == null) { + return; + } + contextCalculators.forEach(contextCalculator -> luckPerms.getContextManager().unregisterCalculator(contextCalculator)); + contextCalculators.clear(); + } +} diff --git a/src/main/java/org/popcraft/stress/Stress.java b/src/main/java/org/popcraft/stress/Stress.java index 61de021..f1c08ee 100644 --- a/src/main/java/org/popcraft/stress/Stress.java +++ b/src/main/java/org/popcraft/stress/Stress.java @@ -75,11 +75,15 @@ public void onEnable() { // Enable bStats metrics int pluginId = 7063; Metrics metrics = new Metrics(this, pluginId); + // Register LuckPerms contexts + LuckPermsContexts.register(); } @Override public void onDisable() { tickProfiler.stop(); + // Unregister LuckPerms contexts + LuckPermsContexts.unregister(); } @Override diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e6e9528..f2b5525 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,7 @@ name: Stress version: @version@ main: org.popcraft.stress.Stress +softdepend: [LuckPerms] api-version: "1.13" authors: [pop4959] description: A stress test plugin for Bukkit servers.