1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 05:55:27 +01:00

feat(tops): add possibility to be hidder from tops

To do that, the part of the command 'top' and 'gtop' has been moved into
an asynchronous thread because of 'unsafe Vault lookups from LuckPerms'.
If it enable, LP prevent a plugin to check a perm of an online player on
the main thread.

Two new nodes perms have been added :
- `jobs.hidetop.<job>`: to be hidden from a specific job top (replace
  <job> by * to be hidden from all).
- `jobs.hidegtop`: to be hidden from the jobs global top.
This commit is contained in:
MachiganMC 2023-10-07 02:29:47 +02:00
parent 96cfcf78e9
commit 72f745aeb3
3 changed files with 80 additions and 37 deletions

View File

@ -1,21 +1,21 @@
package com.gamingmesh.jobs.commands.list; package com.gamingmesh.jobs.commands.list;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.commands.Cmd;
import com.gamingmesh.jobs.container.TopList; import com.gamingmesh.jobs.container.TopList;
import com.gamingmesh.jobs.i18n.Language; import com.gamingmesh.jobs.i18n.Language;
import net.Zrips.CMILib.Container.PageInfo; import net.Zrips.CMILib.Container.PageInfo;
import net.Zrips.CMILib.Locale.LC; import net.Zrips.CMILib.Locale.LC;
import net.Zrips.CMILib.Messages.CMIMessages; import net.Zrips.CMILib.Messages.CMIMessages;
import net.Zrips.CMILib.Scoreboards.CMIScoreboard; import net.Zrips.CMILib.Scoreboards.CMIScoreboard;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class gtop implements Cmd { public class gtop implements Cmd {
@ -52,10 +52,17 @@ public class gtop implements Cmd {
int amount = Jobs.getGCManager().JobsTopAmount; int amount = Jobs.getGCManager().JobsTopAmount;
PageInfo pi = new PageInfo(amount, Jobs.getPlayerManager().getPlayersCache().size(), page); PageInfo pi = new PageInfo(amount, Jobs.getPlayerManager().getPlayersCache().size(), page);
List<TopList> FullList = Jobs.getJobsDAO().getGlobalTopList(pi.getStart()); Bukkit.getScheduler().runTaskAsynchronously(Jobs.getInstance(), () -> showGlobalTop(sender, pi, amount));
return true;
}
private static void showGlobalTop(CommandSender sender, PageInfo pi, int amount) {
Player player = (Player) sender;
List<TopList> FullList = Jobs.getJobsDAO().getGlobalTopList(pi.getStart())
.stream().filter(gtop::hasToBeSeenInGlobalTop).collect(Collectors.toList());
if (FullList.isEmpty()) { if (FullList.isEmpty()) {
Language.sendMessage(sender, "command.gtop.error.nojob"); Language.sendMessage(sender, "command.gtop.error.nojob");
return true; return;
} }
if (!Jobs.getGCManager().ShowToplistInScoreboard) { if (!Jobs.getGCManager().ShowToplistInScoreboard) {
@ -67,11 +74,11 @@ public class gtop implements Cmd {
break; break;
Language.sendMessage(sender, "command.gtop.output.list", Language.sendMessage(sender, "command.gtop.output.list",
"%number%", pi.getPositionForOutput(i), "%number%", pi.getPositionForOutput(i),
"%playername%", One.getPlayerInfo().getName(), "%playername%", One.getPlayerInfo().getName(),
"%playerdisplayname%", One.getPlayerInfo().getDisplayName(), "%playerdisplayname%", One.getPlayerInfo().getDisplayName(),
"%level%", One.getLevel(), "%level%", One.getLevel(),
"%exp%", One.getExp()); "%exp%", One.getExp());
++i; ++i;
} }
} else { } else {
@ -82,10 +89,10 @@ public class gtop implements Cmd {
break; break;
ls.add(Jobs.getLanguage().getMessage("scoreboard.line", ls.add(Jobs.getLanguage().getMessage("scoreboard.line",
"%number%", pi.getPositionForOutput(i), "%number%", pi.getPositionForOutput(i),
"%playername%", one.getPlayerInfo().getName(), "%playername%", one.getPlayerInfo().getName(),
"%playerdisplayname%", one.getPlayerInfo().getDisplayName(), "%playerdisplayname%", one.getPlayerInfo().getDisplayName(),
"%level%", one.getLevel())); "%level%", one.getLevel()));
++i; ++i;
} }
@ -93,6 +100,16 @@ public class gtop implements Cmd {
} }
pi.autoPagination(sender, "jobs gtop"); pi.autoPagination(sender, "jobs gtop");
return true; }
private static boolean hasToBeSeenInGlobalTop(TopList topList) {
Player player = topList.getPlayerInfo().getJobsPlayer().getPlayer();
if (player != null)
return !player.hasPermission("jobs.hidegtop");
return !Jobs.getVaultPermission().playerHas(
null,
Bukkit.getOfflinePlayer(topList.getPlayerInfo().getUuid()),
"jobs.hidegtop"
);
} }
} }

View File

@ -1,22 +1,23 @@
package com.gamingmesh.jobs.commands.list; package com.gamingmesh.jobs.commands.list;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.commands.Cmd;
import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.TopList; import com.gamingmesh.jobs.container.TopList;
import com.gamingmesh.jobs.i18n.Language; import com.gamingmesh.jobs.i18n.Language;
import net.Zrips.CMILib.Container.PageInfo; import net.Zrips.CMILib.Container.PageInfo;
import net.Zrips.CMILib.Locale.LC; import net.Zrips.CMILib.Locale.LC;
import net.Zrips.CMILib.Messages.CMIMessages; import net.Zrips.CMILib.Messages.CMIMessages;
import net.Zrips.CMILib.Scoreboards.CMIScoreboard; import net.Zrips.CMILib.Scoreboards.CMIScoreboard;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class top implements Cmd { public class top implements Cmd {
@ -56,11 +57,19 @@ public class top implements Cmd {
int workingIn = Jobs.getUsedSlots(job); int workingIn = Jobs.getUsedSlots(job);
PageInfo pi = new PageInfo(Jobs.getGCManager().JobsTopAmount, workingIn, page); PageInfo pi = new PageInfo(Jobs.getGCManager().JobsTopAmount, workingIn, page);
final int finalPage = page;
Bukkit.getScheduler().runTaskAsynchronously(Jobs.getInstance(), () ->showTop(sender, job, pi, finalPage) );
return true;
}
private static void showTop(CommandSender sender, Job job, PageInfo pi, int page) {
Player player = (Player) sender;
List<TopList> fullList = Jobs.getJobsDAO().toplist(job.getName(), pi.getStart())
.stream().filter(topList -> hasToBeSeenInTop(topList, job)).collect(Collectors.toList());
List<TopList> fullList = Jobs.getJobsDAO().toplist(job.getName(), pi.getStart());
if (fullList.isEmpty()) { if (fullList.isEmpty()) {
CMIMessages.sendMessage(sender, LC.info_NoInformation); CMIMessages.sendMessage(sender, LC.info_NoInformation);
return true; return;
} }
int place = 1; int place = 1;
@ -69,15 +78,16 @@ public class top implements Cmd {
Language.sendMessage(sender, "command.top.output.topline", "%jobname%", job.getName(), "%amount%", Jobs.getGCManager().JobsTopAmount); Language.sendMessage(sender, "command.top.output.topline", "%jobname%", job.getName(), "%amount%", Jobs.getGCManager().JobsTopAmount);
for (TopList one : fullList) { for (TopList one : fullList) {
System.out.println(one.getPlayerInfo().getName());
if (place > Jobs.getGCManager().JobsTopAmount) if (place > Jobs.getGCManager().JobsTopAmount)
break; break;
Language.sendMessage(sender, "command.top.output.list", Language.sendMessage(sender, "command.top.output.list",
"%number%", ((page - 1) * Jobs.getGCManager().JobsTopAmount) + place, "%number%", ((page - 1) * Jobs.getGCManager().JobsTopAmount) + place,
"%playername%", one.getPlayerInfo().getName(), "%playername%", one.getPlayerInfo().getName(),
"%playerdisplayname%", one.getPlayerInfo().getDisplayName(), "%playerdisplayname%", one.getPlayerInfo().getDisplayName(),
"%level%", one.getLevel(), "%level%", one.getLevel(),
"%exp%", one.getExp()); "%exp%", one.getExp());
place++; place++;
} }
pi.autoPagination(sender, "jobs top " + job.getName()); pi.autoPagination(sender, "jobs top " + job.getName());
@ -88,7 +98,7 @@ public class top implements Cmd {
if (place > Jobs.getGCManager().JobsTopAmount) if (place > Jobs.getGCManager().JobsTopAmount)
break; break;
ls.add(Jobs.getLanguage().getMessage("scoreboard.line", "%number%", ((page - 1) * Jobs.getGCManager().JobsTopAmount) + place, ls.add(Jobs.getLanguage().getMessage("scoreboard.line", "%number%", ((page - 1) * Jobs.getGCManager().JobsTopAmount) + place,
"%playername%", one.getPlayerInfo().getName(), "%playerdisplayname%", one.getPlayerInfo().getDisplayName(), "%level%", one.getLevel())); "%playername%", one.getPlayerInfo().getName(), "%playerdisplayname%", one.getPlayerInfo().getDisplayName(), "%level%", one.getLevel()));
place++; place++;
} }
@ -96,6 +106,16 @@ public class top implements Cmd {
pi.autoPagination(sender, "jobs top " + job.getName()); pi.autoPagination(sender, "jobs top " + job.getName());
} }
return true;
} }
private static boolean hasToBeSeenInTop(TopList topList, Job job) {
Player player = topList.getPlayerInfo().getJobsPlayer().getPlayer();
if (player != null)
return !player.hasPermission("jobs.hidetop.*") || !player.hasPermission("jobs.hidetop." + job.getName().toLowerCase());
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(topList.getPlayerInfo().getUuid());
return !
(Jobs.getVaultPermission().playerHas(null, offlinePlayer, "jobs.hidetop.*")
|| Jobs.getVaultPermission().playerHas(null, offlinePlayer, "jobs.hidetop." + job.getName().toLowerCase()));
}
} }

View File

@ -232,3 +232,9 @@ permissions:
jobs.area.remove: jobs.area.remove:
description: Grants access to the area remove command description: Grants access to the area remove command
default: op default: op
jobs.hidetop.*:
description: Prevent player to be shown in all job top
default: false
jobs.hidegtop:
description: Prevent player to be shown in the global jobs top
default: false