Skip to content

Commit

Permalink
Merge pull request #95 from PowerNetworkHub/1.10.9R2
Browse files Browse the repository at this point in the history
1.10.9R2
  • Loading branch information
svenar-nl authored Dec 15, 2023
2 parents f52c834 + 3cb99cc commit c23bb4f
Show file tree
Hide file tree
Showing 30 changed files with 1,699 additions and 446 deletions.
30 changes: 0 additions & 30 deletions .github/workflows/maven-publish.yml

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

name: Java CI with Maven

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,18 @@ public static void load(String dataDirectory) {
storageManager = new SQLiteStorageManager(dataDirectory, "ranks.db", "players.db");
} else if (storageType.equals("MYSQL")) {
PowerConfigManager pcm = PowerRanks.getConfigManager();
PowerSQLConfiguration configuration = new PowerSQLConfiguration(pcm.getString("storage.mysql.host", "127.0.0.1"),
pcm.getInt("storage.mysql.port", 3306), pcm.getString("storage.mysql.database", "powerranks"),
pcm.getString("storage.mysql.username", "username"), pcm.getString("storage.mysql.password", "password"),
pcm.getBool("storage.mysql.ssl", false), "ranks", "players", "messages", pcm.getBool("storage.mysql.verbose", false));
PowerSQLConfiguration configuration = new PowerSQLConfiguration();

configuration.setHost(pcm.getString("storage.mysql.host", "127.0.0.1"));
configuration.setPort(pcm.getInt("storage.mysql.port", 3306));
configuration.setDatabase(pcm.getString("storage.mysql.database", "powerranks"));
configuration.setUsername(pcm.getString("storage.mysql.username", "username"));
configuration.setPassword(pcm.getString("storage.mysql.password", "password"));
configuration.setUseSSL(pcm.getBool("storage.mysql.ssl", false));
configuration.setTableRanks("ranks");
configuration.setTablePlayers("players");
configuration.setTableMessages("messages");
configuration.setSilentErrors(pcm.getBool("storage.mysql.verbose", false));
storageManager = new MySQLStorageManager(configuration);
} else { // Default to yaml

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,22 @@ public List<String> getKeys(String path) {
* @return Unformatted message with plugin prefix
*/
public String getMessage(String path) {
return getMessage(path, true);
}

/**
* Get a message from the language cache in a specific language.
*
* @param path
* @param addPrefix
* @return Unformatted message with plugin prefix
*/
public String getMessage(String path, boolean addPrefix) {
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") + " " + output;
output = (addPrefix ? this.languageManager.getString("lang." + this.language + ".general.prefix") + " " : "")
+ output;
return output;
}

Expand Down Expand Up @@ -83,11 +95,24 @@ public String getFormattedMessage(String path) {
return PowerRanks.chatColor(getMessage(path), true);
}

/**
* Get a message from the language cache in a specific language and return the
* chatcolor formatted message.
*
* @param path
* @param addPrefix
* @return Chatcolor formatted message
*/
public String getFormattedMessage(String path, boolean addPrefix) {
return PowerRanks.chatColor(getMessage(path, addPrefix), true);
}

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;
output = this.languageManager.getString("lang." + this.language + ".general.prefix") + " "
+ (isPlayer ? "/" : "") + commandLabel + " " + commandName + " " + output;
return output;
}

Expand Down Expand Up @@ -119,6 +144,7 @@ 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 @@ -4,8 +4,12 @@

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import com.google.common.collect.ImmutableMap;

import nl.svenar.powerranks.bukkit.PowerRanks;
import nl.svenar.powerranks.common.utils.PRUtil;

public abstract class PowerCommand {

Expand Down Expand Up @@ -43,4 +47,18 @@ protected void setCommandPermission(String permission) {
public String getCommandPermission() {
return this.commandPermission;
}

protected String prepareMessage(String langArg, boolean addPluginPrefix) {
return prepareMessage(langArg, ImmutableMap.of(), addPluginPrefix);
}

protected String prepareMessage(String langArg, ImmutableMap<String, @NotNull String> data, boolean addPluginPrefix) {
String langLine = PowerRanks.getLanguageManager().getFormattedMessage(langArg, false);
if (addPluginPrefix) {
String langPrefix = PowerRanks.getLanguageManager().getFormattedMessage("prefix", false);
return PRUtil.powerFormatter(langPrefix + " " + langLine, data, '[', ']');
} else {
return PRUtil.powerFormatter(langLine, data, '[', ']');
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package nl.svenar.powerranks.bukkit.commands.player;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.google.common.collect.ImmutableMap;

import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;

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 nl.svenar.powerranks.common.structure.PRPermission;
import nl.svenar.powerranks.common.structure.PRPlayer;
import nl.svenar.powerranks.common.utils.PRUtil;
import nl.svenar.powerranks.common.utils.PRCache;
import nl.svenar.powerranks.common.utils.PowerColor;
import nl.svenar.powerranks.bukkit.PowerRanks;
import nl.svenar.powerranks.bukkit.cache.CacheManager;
import nl.svenar.powerranks.bukkit.commands.PowerCommand;
import nl.svenar.powerranks.bukkit.util.Util;
import nl.svenar.powerranks.bukkit.textcomponents.PageNavigationManager;

public class cmd_listplayerpermissions extends PowerCommand {

Expand All @@ -28,119 +34,40 @@ public cmd_listplayerpermissions(PowerRanks plugin, String command_name, COMMAND
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String commandName,
String[] args) {
if (args.length == 1) {
PRPlayer targetPlayer = CacheManager.getPlayer(args[0]);
if (targetPlayer != null) {
displayList(sender, targetPlayer, commandLabel, 0);
} else {
sender.sendMessage(PRUtil.powerFormatter(
PowerRanks.getLanguageManager().getFormattedMessage("general.player-not-found"),
ImmutableMap.<String, String>builder()
.put("player", sender.getName())
.put("target", args[0])
.build(),
'[', ']'));
}
} else if (args.length == 2) {
int page = Integer.parseInt(args[1].replaceAll("[a-zA-Z]", ""));
PRPlayer targetPlayer = CacheManager.getPlayer(args[0]);
if (targetPlayer != null) {
displayList(sender, targetPlayer, commandLabel, page);
} else {
sender.sendMessage(PRUtil.powerFormatter(
PowerRanks.getLanguageManager().getFormattedMessage("general.player-not-found"),
ImmutableMap.<String, String>builder()
.put("player", sender.getName())
.put("target", args[0])
.build(),
'[', ']'));
}
} else {
sender.sendMessage(
PowerRanks.getLanguageManager().getFormattedUsageMessage(commandLabel, commandName,
"commands." + commandName.toLowerCase() + ".arguments", sender instanceof Player));
}

return false;
}

private void displayList(CommandSender sender, PRPlayer prPlayer, String commandLabel, int page) {
ArrayList<String> output_messages = new ArrayList<String>();

output_messages.add(ChatColor.BLUE + "===" + ChatColor.DARK_AQUA + "----------" + ChatColor.AQUA
+ plugin.getDescription().getName() + ChatColor.DARK_AQUA + "----------" + ChatColor.BLUE + "===");

Set<PRPermission> playerPermissions = prPlayer.getPermissions();

int lines_per_page = sender instanceof Player ? 5 : 10;
int last_page = playerPermissions.size() / lines_per_page;
String playername = "";
int page = 1;

if (!(sender instanceof Player)) {
page -= 1;
if (args.length > 0) {
playername = args[0];
}

page = page < 0 ? 0 : page;
page = page > last_page ? last_page : page;

if (sender instanceof Player) {
String page_selector_tellraw = "tellraw " + sender.getName()
+ " [\"\",{\"text\":\"Page \",\"color\":\"aqua\"},{\"text\":\"" + "%next_page%"
+ "\",\"color\":\"blue\"},{\"text\":\"/\",\"color\":\"aqua\"}"
+ ",{\"text\":\"%last_page%\",\"color\":\"blue\"},{\"text\":\": \",\"color\":\"aqua\"}"
+ ",{\"text\":\"[\",\"color\":\"aqua\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/"
+ "%commandlabel%" + " listplayerpermissions %playername% " + "%previous_page%"
+ "\"}},{\"text\":\"<\",\"color\":\"blue\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/"
+ "%commandlabel%" + " listplayerpermissions %playername% " + "%previous_page%"
+ "\"}},{\"text\":\"]\",\"color\":\"aqua\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/"
+ "%commandlabel%" + " listplayerpermissions %playername% " + "%previous_page%"
+ "\"}},{\"text\":\" \",\"color\":\"aqua\"},{\"text\":\"[\",\"color\":\"aqua\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/"
+ "%commandlabel%" + " listplayerpermissions %playername% " + "%next_page%"
+ "\"}},{\"text\":\">\",\"color\":\"blue\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/"
+ "%commandlabel%" + " listplayerpermissions %playername% " + "%next_page%"
+ "\"}},{\"text\":\"]\",\"color\":\"aqua\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/"
+ "%commandlabel%" + " listplayerpermissions %playername% " + "%next_page%" + "\"}}]";

page_selector_tellraw = Util.replaceAll(page_selector_tellraw, "%next_page%", String.valueOf(page + 1));
page_selector_tellraw = Util.replaceAll(page_selector_tellraw, "%previous_page%", String.valueOf(page - 1));
page_selector_tellraw = Util.replaceAll(page_selector_tellraw, "%last_page%",
String.valueOf(last_page + 1));
page_selector_tellraw = Util.replaceAll(page_selector_tellraw, "%playername%", prPlayer.getName());
page_selector_tellraw = Util.replaceAll(page_selector_tellraw, "%commandlabel%", commandLabel);

output_messages.add(page_selector_tellraw);

output_messages.add(ChatColor.AQUA + prPlayer.getName() + "'s permissions:");

// sender.sendMessage("[A] " + last_page + " " + lines_per_page);
} else {
output_messages.add(ChatColor.AQUA + "Page " + ChatColor.BLUE + (page + 1) + ChatColor.AQUA + "/"
+ ChatColor.BLUE + (last_page + 1));
output_messages.add(ChatColor.AQUA + "Next page " + ChatColor.BLUE + "/" + commandLabel
+ " listplayerpermissions " + prPlayer.getName() + " " + ChatColor.BLUE
+ (page + 2 > last_page + 1 ? last_page + 1 : page + 2));
}

int line_index = 0;
for (PRPermission permission : playerPermissions) {
if (line_index >= page * lines_per_page && line_index < page * lines_per_page + lines_per_page) {
output_messages.add(ChatColor.DARK_GREEN + "#" + (line_index + 1) + ". "
+ (permission.getValue() ? ChatColor.GREEN : ChatColor.RED) + permission.getName());
if (args.length > 1) {
try {
page = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Invalid page number.");
return false;
}
line_index += 1;
}

output_messages.add(ChatColor.BLUE + "===" + ChatColor.DARK_AQUA + "------------------------------"
+ ChatColor.BLUE + "===");

if (plugin != null) {
for (String msg : output_messages) {
if (msg.startsWith("tellraw")) {
plugin.getServer().dispatchCommand((CommandSender) plugin.getServer().getConsoleSender(), msg);
} else {
sender.sendMessage(msg);
}
PageNavigationManager pageNavigationManager = new PageNavigationManager();
pageNavigationManager.setItemsPerPage(sender instanceof Player ? 5 : 10);
pageNavigationManager.setMonospace(sender instanceof ConsoleCommandSender);
pageNavigationManager.setFancyPageControls(sender instanceof Player);
pageNavigationManager.setBaseCommand("pr listplayerpermissions " + playername);
pageNavigationManager.setItems(
formatList(PRCache.getPlayer(playername).getPermissions(), 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);
}
}

return false;
}

public ArrayList<String> tabCompleteEvent(CommandSender sender, String[] args) {
Expand All @@ -154,4 +81,21 @@ public ArrayList<String> tabCompleteEvent(CommandSender sender, String[] args) {

return tabcomplete;
}

private List<String> formatList(Set<PRPermission> permissions, boolean b) {
List<String> list = new ArrayList<String>();

for (PRPermission prpermission : permissions) {
String line = prepareMessage("list.list-permission-item", ImmutableMap.of( //
"permission", (prpermission.getValue() ? ChatColor.GREEN : ChatColor.RED) + prpermission.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;
}
}
Loading

0 comments on commit c23bb4f

Please sign in to comment.