removed some code repetition

This commit is contained in:
youhavetrouble
2020-08-04 01:12:42 +02:00
parent ca18d17c13
commit 506c13b5bc
5 changed files with 43 additions and 31 deletions
@@ -0,0 +1,35 @@
package eu.endermite.commandwhitelist.api;
import eu.endermite.commandwhitelist.bungee.CommandWhitelistBungee;
import eu.endermite.commandwhitelist.spigot.CommandWhitelist;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class CommandsList {
public static List<String> getCommands(Player player) {
List<String> commandList = new ArrayList<>();
for (Map.Entry<String, List<String>> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) {
if (player.hasPermission("commandwhitelist.commands." + s.getKey())) {
commandList.addAll(s.getValue());
}
}
return commandList;
}
public static List<String> getCommands(ProxiedPlayer player) {
List<String> commandList = new ArrayList<>();
for (Map.Entry<String, List<String>> s : CommandWhitelistBungee.getConfigCache().getPermList().entrySet()) {
if (player.hasPermission("commandwhitelist.commands." + s.getKey())) {
commandList.addAll(s.getValue());
}
}
return commandList;
}
}
@@ -1,5 +1,6 @@
package eu.endermite.commandwhitelist.bungee.listeners;
import eu.endermite.commandwhitelist.api.CommandsList;
import eu.endermite.commandwhitelist.bungee.CommandWhitelistBungee;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
@@ -22,12 +23,8 @@ public class BungeeTabCompleteListener implements Listener {
return;
}
List<String> commandList = new ArrayList<>();
for (Map.Entry<String, List<String>> entry : CommandWhitelistBungee.getConfigCache().getPermList().entrySet()) {
if (player.hasPermission("commandwhitelist.commands."+entry.getKey())) {
commandList.addAll(entry.getValue());
}
}
List<String> commandList = CommandsList.getCommands(player);
HashMap<String, Command> commandHashMap = new HashMap<>();
for (String s : commandList) {
for (Map.Entry<String, Command> command : CommandWhitelistBungee.getPlugin().getProxy().getPluginManager().getCommands()) {
@@ -5,7 +5,6 @@ import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import java.util.ArrayList;
import java.util.List;
@@ -13,7 +12,6 @@ public class MainCommand implements TabExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length > 0) {
if (args[0].equalsIgnoreCase("reload")) {
if (sender.hasPermission("commandwhitelist.reload")) {
@@ -35,15 +33,12 @@ public class MainCommand implements TabExecutor {
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
List<String> list = new ArrayList<>();
if(args.length == 1) {
if ("restart".startsWith(args[0]) && sender.hasPermission("commandwhitelist.reload")) {
list.add("reload");
}
}
return list;
}
}
@@ -7,6 +7,7 @@ import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import eu.endermite.commandwhitelist.api.CommandsList;
import eu.endermite.commandwhitelist.spigot.CommandWhitelist;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@@ -36,16 +37,10 @@ public class LegacyPlayerTabChatCompleteListener {
PacketContainer packet = event.getPacket();
String[] message = packet.getSpecificModifier(String[].class).read(0);
List<String> commandList = new ArrayList<>();
for (Map.Entry<String, List<String>> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) {
if (player.hasPermission("commandwhitelist.commands." + s.getKey())) {
commandList.addAll(s.getValue());
}
}
List<String> commandList = CommandsList.getCommands(player);
List<String> finalList = new ArrayList<>();
int components = 0;
for (String cmd : message) {
for (String cmdFromList : commandList) {
if (cmd.equalsIgnoreCase("/" + cmdFromList) || !cmd.startsWith("/")) {
@@ -63,9 +58,7 @@ public class LegacyPlayerTabChatCompleteListener {
packet.getSpecificModifier(String[].class).write(0, toWrite);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception ignored) {}
}
});
@@ -97,8 +90,6 @@ public class LegacyPlayerTabChatCompleteListener {
}
}
event.setCancelled(true);
} catch (Exception e) {
e.printStackTrace();
}
@@ -1,6 +1,6 @@
package eu.endermite.commandwhitelist.spigot.listeners;
import eu.endermite.commandwhitelist.spigot.CommandWhitelist;
import eu.endermite.commandwhitelist.api.CommandsList;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@@ -17,13 +17,7 @@ public class PlayerCommandSendListener implements Listener {
return;
}
List<String> commandList = new ArrayList<>();
for (Map.Entry<String, List<String>> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) {
if (player.hasPermission("commandwhitelist.commands."+s.getKey())) {
commandList.addAll(s.getValue());
}
}
List<String> commandList = CommandsList.getCommands(player);
event.getCommands().removeIf((cmd) -> !commandList.contains(cmd));