Skip to content

Commit

Permalink
Merge pull request #99 from PowerNetworkHub/1.10.10
Browse files Browse the repository at this point in the history
1.10.10
  • Loading branch information
svenar-nl authored Aug 22, 2024
2 parents c23bb4f + d5c4dd0 commit 4217c24
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 171 deletions.
6 changes: 3 additions & 3 deletions Bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@

<dependency>
<groupId>com.github.seeseemelk</groupId>
<artifactId>MockBukkit-v1.20</artifactId>
<version>3.39.0</version>
<artifactId>MockBukkit-v1.21</artifactId>
<version>3.109.0</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -210,7 +210,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.2-R0.1-SNAPSHOT</version>
<version>1.21-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static void load(String dataDirectory) {
List<String> addonStorageManager = prAddon.getValue().getStorageManagerNames();
if (Objects.nonNull(addonStorageManager)) {
for (String storageName : addonStorageManager) {
if (storageType.equals(storageName.toUpperCase())) {
if (storageType.equalsIgnoreCase(storageName)) {
usedStorageManagerAddon = prAddon.getValue();
usedStorageManagerAddon.setupStorageManager(storageName);
storageManager = usedStorageManagerAddon.getStorageManager(storageName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public LanguageManager() {
* @return List of found keys
*/
public List<String> getKeys(String path) {
path = "lang." + this.language + "." + path;
String targetPath = "lang." + this.language + "." + path;

List<String> keys = new ArrayList<String>();

for (Entry<?, ?> entry : this.languageManager.getMap(path, new HashMap<String, String>()).entrySet()) {
for (Entry<?, ?> entry : this.languageManager.getMap(targetPath, new HashMap<String, String>()).entrySet()) {
keys.add(String.valueOf(entry.getKey()));
}

Expand Down Expand Up @@ -78,9 +78,9 @@ public String getMessage(String path, boolean addPrefix) {
* @return Unformatted message
*/
public String getUnformattedMessage(String path) {
path = "lang." + this.language + "." + path;
String output = this.languageManager.getString(path);
output = output == null ? path : output;
String targetPath = "lang." + this.language + "." + path;
String output = this.languageManager.getString(targetPath);
output = output == null ? targetPath : output;
return output;
}

Expand Down Expand Up @@ -108,11 +108,10 @@ public String getFormattedMessage(String path, boolean addPrefix) {
}

public String getUsageMessage(String commandLabel, String commandName, String path, boolean isPlayer) {
path = "lang." + this.language + "." + path;
String output = this.languageManager.getString(path);
output = output == null ? path : output;
output = this.languageManager.getString("lang." + this.language + ".general.prefix") + " "
+ (isPlayer ? "/" : "") + commandLabel + " " + commandName + " " + output;
String targetPath = "lang." + this.language + "." + path;
String output = this.languageManager.getString(targetPath);
output = output == null ? targetPath : output;
output = (isPlayer ? "/" : "") + commandLabel + " " + commandName + " " + output;
return output;
}

Expand Down Expand Up @@ -144,7 +143,6 @@ public void setLanguage(String language) {
public void save() {
this.languageManager.save();
}

/**
* Reload the language data
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import nl.svenar.powerranks.bukkit.commands.core.cmd_stats;
import nl.svenar.powerranks.bukkit.commands.core.cmd_tablist;
import nl.svenar.powerranks.bukkit.commands.core.cmd_verbose;
import nl.svenar.powerranks.bukkit.commands.core.cmd_permissionlookups;
import nl.svenar.powerranks.bukkit.commands.player.cmd_addownrank;
import nl.svenar.powerranks.bukkit.commands.player.cmd_addplayerperm;
import nl.svenar.powerranks.bukkit.commands.player.cmd_addrank;
Expand Down Expand Up @@ -66,7 +67,6 @@
import nl.svenar.powerranks.bukkit.commands.rank.cmd_setprefix;
import nl.svenar.powerranks.bukkit.commands.rank.cmd_setsuffix;
import nl.svenar.powerranks.bukkit.commands.rank.cmd_setweight;
import nl.svenar.powerranks.bukkit.commands.test.cmd_test;
import nl.svenar.powerranks.bukkit.commands.usertags.cmd_addusertag;
import nl.svenar.powerranks.bukkit.commands.usertags.cmd_clearusertag;
import nl.svenar.powerranks.bukkit.commands.usertags.cmd_createusertag;
Expand Down Expand Up @@ -98,6 +98,7 @@ public PowerCommandHandler(PowerRanks plugin) {
new cmd_addonmanager(plugin, "addonmanager", COMMAND_EXECUTOR.ALL);
new cmd_playerinfo(plugin, "playerinfo", COMMAND_EXECUTOR.ALL);
new cmd_haspermission(plugin, "haspermission", COMMAND_EXECUTOR.ALL);
new cmd_permissionlookups(plugin, "permissionlookups", COMMAND_EXECUTOR.ALL);
new cmd_dump(plugin, "dump", COMMAND_EXECUTOR.ALL);

new cmd_setrank(plugin, "setrank", COMMAND_EXECUTOR.ALL);
Expand Down Expand Up @@ -151,8 +152,6 @@ public PowerCommandHandler(PowerRanks plugin) {

new cmd_webeditor(plugin, "webeditor", COMMAND_EXECUTOR.ALL);
new cmd_webeditor(plugin, "we", COMMAND_EXECUTOR.ALL);

new cmd_test(plugin, "test", COMMAND_EXECUTOR.ALL);
}

private static boolean canExecuteCommand(CommandSender sender, PowerCommand command_handler) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package nl.svenar.powerranks.bukkit.commands.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

import com.google.common.collect.ImmutableMap;

import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import nl.svenar.powerranks.bukkit.PowerRanks;
import nl.svenar.powerranks.bukkit.commands.PowerCommand;
import nl.svenar.powerranks.bukkit.data.PowerPermissibleBase;
import nl.svenar.powerranks.bukkit.textcomponents.DefaultFontInfo;
import nl.svenar.powerranks.bukkit.textcomponents.PageNavigationManager;
import nl.svenar.powerranks.common.utils.PowerColor;

public class cmd_permissionlookups extends PowerCommand {

private final int maxResultsConsole = 20;
private final int maxResultsPlayer = 10;

public cmd_permissionlookups(PowerRanks plugin, String command_name, COMMAND_EXECUTOR ce) {
super(plugin, command_name, ce);
this.setCommandPermission("powerranks.cmd." + command_name.toLowerCase());
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String commandName,
String[] args) {

int page = 1;
if (args.length > 0) {
try {
page = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Invalid page number.");
return false;
}
}

Map<String, Integer> permissionCallCount = PowerPermissibleBase.permissionCallCount;
// sort from high to low value
permissionCallCount = sortByValue(permissionCallCount, false);

List<PRCallCount> callCounts = new ArrayList<>();
for (Entry<String, Integer> entry : permissionCallCount.entrySet()) {
callCounts.add(new PRCallCount(entry.getKey(), entry.getValue()));
}

PageNavigationManager pageNavigationManager = new PageNavigationManager();
pageNavigationManager.setItemsPerPage(sender instanceof Player ? maxResultsPlayer : maxResultsConsole);
pageNavigationManager.setMonospace(sender instanceof ConsoleCommandSender);
pageNavigationManager.setFancyPageControls(sender instanceof Player);
pageNavigationManager.setBaseCommand("pr permissionlookups");
pageNavigationManager.setItems(formatPermissionCountList(callCounts, sender instanceof ConsoleCommandSender));

for (Object line : pageNavigationManager.getPage(page).generate()) {
if (line instanceof String) {
sender.sendMessage((String) line);
} else if (line instanceof TextComponent) {
sender.spigot().sendMessage((TextComponent) line);
} else {
sender.spigot().sendMessage((BaseComponent[]) line);
}
}

sender.sendMessage(PowerRanks.chatColor("&7callcount &apermission&2.&anode", true));
sender.sendMessage(PowerRanks.chatColor("&2Total called permissions: &a" + permissionCallCount.size(), true));

return false;
}

private Map<String, Integer> sortByValue(Map<String, Integer> permissionCallCount, boolean asc) {
// sort from high to low value
List<Entry<String, Integer>> list = new LinkedList<>(permissionCallCount.entrySet());
java.util.Collections.sort(list, (Entry<String, Integer> o1, Entry<String, Integer> o2) -> {
if (asc) {
return (o1.getValue()).compareTo(o2.getValue());
} else {
return (o2.getValue()).compareTo(o1.getValue());
}
});
// return
Map<String, Integer> result = new LinkedHashMap<>();
for (Entry<String, Integer> entry : list) {
result.put(entry.getKey(), entry.getValue());
}

return result;
}

@Override
public ArrayList<String> tabCompleteEvent(CommandSender sender, String[] args) {
ArrayList<String> tabcomplete = new ArrayList<>();

return tabcomplete;
}

private List<String> formatPermissionCountList(List<PRCallCount> prCallCounts, boolean hasMonospaceFont) {
List<String> list = new ArrayList<>();

Map<String, String> permissionNameBuffer = new HashMap<>();
Map<String, String> countBuffer = new HashMap<>();

for (PRCallCount prcc : prCallCounts) {
permissionNameBuffer.put(prcc.getName(), prcc.getName());
countBuffer.put(prcc.getName(), String.valueOf(prcc.getCount()));
}

if (!hasMonospaceFont) {
int longestName = permissionNameBuffer.values().stream().mapToInt(
name -> name.chars().map(c -> DefaultFontInfo.getDefaultFontInfo((char) c).getLength()).sum()).max()
.orElse(0);
int longestCount = countBuffer.values().stream().mapToInt(
name -> name.chars().map(c -> DefaultFontInfo.getDefaultFontInfo((char) c).getLength()).sum()).max()
.orElse(0);

for (Entry<String, String> entry : permissionNameBuffer.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
int currentLength = value.chars().map(c -> DefaultFontInfo.getDefaultFontInfo((char) c).getLength())
.sum();
while (currentLength < longestName) {
value += " ";
currentLength = value.chars().map(c -> DefaultFontInfo.getDefaultFontInfo((char) c).getLength())
.sum();
}
permissionNameBuffer.put(key, value);
}

for (Entry<String, String> entry : countBuffer.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
int currentLength = value.chars().map(c -> DefaultFontInfo.getDefaultFontInfo((char) c).getLength())
.sum();
while (currentLength < longestCount) {
value = " " + value;
currentLength = value.chars().map(c -> DefaultFontInfo.getDefaultFontInfo((char) c).getLength())
.sum();
}
countBuffer.put(key, value);
}

} else {
int longestName = permissionNameBuffer.values().stream().mapToInt(name -> name.length()).max().orElse(0);
int longestCount = countBuffer.values().stream().mapToInt(name -> name.length()).max().orElse(0);

for (Entry<String, String> entry : permissionNameBuffer.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
int currentLength = value.length();
while (currentLength < longestName) {
value += " ";
currentLength = value.length();
}
permissionNameBuffer.put(key, value);
}

for (Entry<String, String> entry : countBuffer.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
int currentLength = value.length();
while (currentLength < longestCount) {
value = " " + value;
currentLength = value.length();
}
countBuffer.put(key, value);
}
}

for (Iterator<PRCallCount> it = prCallCounts.iterator(); it.hasNext();) {
PRCallCount prcc = it.next();
String line = prepareMessage("list.list-permissioncallcount-item", ImmutableMap.of( //
"permission", "&a" + permissionNameBuffer.get(prcc.getName()).replaceAll("\\.", "&2.&a"), //
"callcount", countBuffer.get(prcc.getName()) //
), false);
while (line.endsWith(" ") || line.toLowerCase().endsWith(PowerColor.COLOR_CHAR + "")
|| line.toLowerCase().endsWith(PowerColor.COLOR_CHAR + "r")) {
line = line.substring(0, line.length() - 1);
}
list.add(line);
}
return list;
}

private class PRCallCount {
private final String name;
private final int count;

public PRCallCount(String name, int count) {
this.name = name;
this.count = count;
}

public String getName() {
return name;
}

public int getCount() {
return count;
}
}
}
Loading

0 comments on commit 4217c24

Please sign in to comment.