Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgH93 committed Apr 6, 2020
1 parent 778594c commit 13ba721
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 GeorgH93
* Copyright (C) 2020 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -31,7 +31,7 @@ public interface MarriageMasterPlugin extends at.pcgamingfreaks.MarriageMaster.A
* @param player2 The second player to be checked.
* @param range The range in which the two players should be.
* @return True if the players are within the given range, false if not.
* Also True if one of the players has the "marry.bypassrangelimit" permission, the range is 0 and both players are in the same world or the range is below 0.
* Also True if one of the players has the "marry.bypass.rangelimit" permission, the range is 0 and both players are in the same world or the range is below 0.
*/
boolean isInRange(@NotNull Player player1, @NotNull Player player2, double range);

Expand All @@ -42,7 +42,7 @@ public interface MarriageMasterPlugin extends at.pcgamingfreaks.MarriageMaster.A
* @param player2 The second player to be checked.
* @param rangeSquared The squared distance in which the two players should be.
* @return True if the players are within the given range, false if not.
* Also True if one of the players has the "marry.bypassrangelimit" permission, the range is 0 and both players are in the same world or the range is below 0.
* Also True if one of the players has the "marry.bypass.rangelimit" permission, the range is 0 and both players are in the same world or the range is below 0.
*/
boolean isInRangeSquared(@NotNull Player player1, @NotNull Player player2, double rangeSquared);

Expand All @@ -53,7 +53,7 @@ public interface MarriageMasterPlugin extends at.pcgamingfreaks.MarriageMaster.A
* @param player2 The second player to be checked.
* @param range The range in which the two players should be.
* @return True if the players are within the given range, false if not.
* Also True if one of the players has the "marry.bypassrangelimit" permission, the range is 0 and both players are in the same world or the range is below 0.
* Also True if one of the players has the "marry.bypass.rangelimit" permission, the range is 0 and both players are in the same world or the range is below 0.
*/
default boolean isInRange(@NotNull MarriagePlayer player1, @NotNull MarriagePlayer player2, double range)
{
Expand All @@ -68,7 +68,7 @@ default boolean isInRange(@NotNull MarriagePlayer player1, @NotNull MarriagePlay
* @param player2 The second player to be checked.
* @param rangeSquared The squared distance in which the two players should be.
* @return True if the players are within the given range, false if not.
* Also True if one of the players has the "marry.bypassrangelimit" permission, the range is 0 and both players are in the same world or the range is below 0.
* Also True if one of the players has the "marry.bypass.rangelimit" permission, the range is 0 and both players are in the same world or the range is below 0.
*/
default boolean isInRangeSquared(@NotNull MarriagePlayer player1, @NotNull MarriagePlayer player2, double rangeSquared)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ else if(getMarriagePlugin().getCommandManager().isToggleSwitch(args[0]))
private void openBackpack(MarriagePlayer opener, MarriagePlayer partner)
{
Player sender = opener.getPlayerOnline();
if(sender.getGameMode() == GameMode.SURVIVAL || sender.hasPermission("marry.bypass.backpackgamemode")) //TODO use minepacks settings
if(sender.getGameMode() == GameMode.SURVIVAL || sender.hasPermission(Permissions.BYPASS_BACKPACK_GAME_MODE)) //TODO use minepacks settings
{
Player partnerPlayer = partner.getPlayerOnline();
if(partnerPlayer == null || !partnerPlayer.isOnline())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ public void onLeave(PlayerQuitEvent event)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onJoin(PlayerJoinEvent event)
{
//noinspection SpellCheckingInspection
if(allowChatSurveillance && event.getPlayer().hasPermission("marry.listenchat.autojoin"))
if(allowChatSurveillance && event.getPlayer().hasPermission(Permissions.LISTEN_CHAT_AUTO_JOIN))
{
listeners.add(event.getPlayer());
}
Expand Down Expand Up @@ -412,7 +411,7 @@ public static class ChatListenCommand extends MarryCommand

public ChatListenCommand(MarriageMaster plugin, ChatCommand chatCommand)
{
super(plugin, "listenchat", plugin.getLanguage().getTranslated("Commands.Description.ChatListen"), "marry.listenchat", true, plugin.getLanguage().getCommandAliases("ChatListen"));
super(plugin, "listenchat", plugin.getLanguage().getTranslated("Commands.Description.ChatListen"), Permissions.LISTEN_CHAT, true, plugin.getLanguage().getCommandAliases("ChatListen"));

this.chatCommand = chatCommand;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void execute(@NotNull CommandSender sender, @NotNull String mainCommandAl
{
MarriagePlayer player = getMarriagePlugin().getPlayerData((Player) sender);
Player bPlayer = (Player) sender;
if(bPlayer.getGameMode().equals(GameMode.SURVIVAL) || allowedInCreative || bPlayer.hasPermission("marry.bypass.giftgamemode"))
if(bPlayer.getGameMode().equals(GameMode.SURVIVAL) || allowedInCreative || bPlayer.hasPermission(Permissions.BYPASS_GIFT_GAME_MODE))
{
MarriagePlayer partner;
if(getMarriagePlugin().areMultiplePartnersAllowed() && args.length == 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void kiss(MarriagePlayer player, MarriagePlayer partner)
Bukkit.getPluginManager().callEvent(event);
if(!event.isCancelled())
{
if(!player.hasPermission("marry.bypass.delay")) wait.put(player, System.currentTimeMillis());
if(!player.hasPermission(Permissions.BYPASS_DELAY)) wait.put(player, System.currentTimeMillis());
player.send(messageKissed);
partner.send(messageGotKissed);
if(particleSpawner != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String m
if(args.length == 0 || !canUse(sender)) return null;
List<String> names = new LinkedList<>();
String arg = args[args.length - 1].toLowerCase(Locale.ENGLISH);
final boolean excludeVanished = !sender.hasPermission("marry.bypass.vanish");
final boolean excludeVanished = !sender.hasPermission(Permissions.BYPASS_VANISH);
for(Player player : Bukkit.getOnlinePlayers())
{
if(excludeVanished && sender instanceof Player && !((Player) sender).canSee(player)) continue; // Hide vanished players
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ public void execute(@NotNull CommandSender sender, @NotNull String mainCommandAl
}
else if(partner.isOnline())
{
//noinspection SpellCheckingInspection
if(requireConfirmation && !(player.hasPermission("marry.bypass.tpconfirmation") || partner.hasPermission("marry.autoaccept.tprequest")))
if(requireConfirmation && !(player.hasPermission(Permissions.BYPASS_TP_CONFIRMATION) || partner.hasPermission(Permissions.AUTO_ACCEPT_TP_REQUEST)))
{
messageWaitForConfirmation.send(sender);
getMarriagePlugin().getCommandManager().registerAcceptPendingRequest(new TpRequest(player, partner));
Expand Down Expand Up @@ -168,7 +167,7 @@ public void doTheTP(Player player, Player partner)
if(player.canSee(partner))
{
//noinspection SpellCheckingInspection
if(!blacklistedWorlds.contains(partner.getLocation().getWorld().getName().toLowerCase()) || player.hasPermission("marry.bypass.worldblacklist"))
if(!blacklistedWorlds.contains(partner.getLocation().getWorld().getName().toLowerCase()) || player.hasPermission(Permissions.BYPASS_WORLD_BLACKLIST))
{
Location loc = partner.getLocation();
if(safetyCheck && (loc = getSaveLoc(loc)) == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ else if(otherPlayer.getOpenRequest() != null || otherPlayer.getRequestsToCancel(
Player bPriest = priest.getPlayerOnline();
if(marryPriestTestCanMarry(player1, player2, bPriest))
{
if(priest.hasPermission("marry.bypass.rangelimit") || (plugin.isInRangeSquared(player1, player2, rangeMarrySquared) && plugin.isInRangeSquared(priest, player1, rangeMarrySquared) &&
if(priest.hasPermission(Permissions.BYPASS_RANGELIMIT) || (plugin.isInRangeSquared(player1, player2, rangeMarrySquared) && plugin.isInRangeSquared(priest, player1, rangeMarrySquared) &&
plugin.isInRangeSquared(priest, player2, rangeMarrySquared)))
{
if(!confirm)
Expand Down Expand Up @@ -597,7 +597,7 @@ public void divorce(@NotNull Marriage marriage, @NotNull MarriagePlayer divorceB
if(!otherPlayer.isOnline())
{
// Self divorce with offline partner
if(divorceBy.hasPermission("marry.offlinedivorce"))
if(divorceBy.hasPermission(Permissions.OFFLINEDIVORCE))
{
selfFinishDivorce(marriage, divorceBy);
}
Expand Down Expand Up @@ -666,7 +666,7 @@ public void divorce(@NotNull Marriage marriage, @NotNull MarriagePlayer divorceB
{
if(marriage.isBothOnline())
{
if(divorceBy.hasPermission("marry.bypass.rangelimit") || (plugin.isInRange(divorceBy, marriage.getPartner1(), rangeDivorce) && plugin.isInRange(divorceBy, marriage.getPartner2(), rangeDivorce)))
if(divorceBy.hasPermission(Permissions.BYPASS_RANGELIMIT) || (plugin.isInRange(divorceBy, marriage.getPartner1(), rangeDivorce) && plugin.isInRange(divorceBy, marriage.getPartner2(), rangeDivorce)))
{
if(confirm)
{
Expand Down Expand Up @@ -696,7 +696,7 @@ public void divorce(@NotNull Marriage marriage, @NotNull MarriagePlayer divorceB
divorceBy.send(messageDivorceNotInRange, rangeDivorce);
}
}
else if(divorceBy.hasPermission("marry.offlinedivorce"))
else if(divorceBy.hasPermission(Permissions.OFFLINEDIVORCE))
{
if(!marriage.getPartner1().isOnline() && !marriage.getPartner2().isOnline()) // Both are offline
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import at.pcgamingfreaks.MarriageMaster.Bukkit.SpecialInfoWorker.NoDatabaseWorker;
import at.pcgamingfreaks.MarriageMaster.IUpdater;
import at.pcgamingfreaks.MarriageMaster.MagicValues;
import at.pcgamingfreaks.MarriageMaster.Permissions;
import at.pcgamingfreaks.StringUtils;
import at.pcgamingfreaks.Updater.UpdateProviders.UpdateProvider;
import at.pcgamingfreaks.Updater.Updater;
Expand All @@ -58,7 +59,6 @@

public class MarriageMaster extends JavaPlugin implements MarriageMasterPlugin, IUpdater
{
private static final String RANGE_LIMIT_PERM = "marry.bypass.rangelimit";
@Setter(AccessLevel.PRIVATE) private static Version version = null;
@Getter @Setter(AccessLevel.PRIVATE) private static MarriageMaster instance;

Expand Down Expand Up @@ -351,21 +351,21 @@ public boolean isSurnamesForced()
@Override
public boolean isInRange(@NotNull Player player1, @NotNull Player player2, double range)
{
return Utils.inRange(player1, player2, range, RANGE_LIMIT_PERM);
return Utils.inRange(player1, player2, range, Permissions.BYPASS_RANGELIMIT);
}

@Override
public boolean isInRangeSquared(@NotNull Player player1, @NotNull Player player2, double rangeSquared)
{
return Utils.inRangeSquared(player1, player2, rangeSquared, RANGE_LIMIT_PERM);
return Utils.inRangeSquared(player1, player2, rangeSquared, Permissions.BYPASS_RANGELIMIT);
}

@Override
public void doDelayableTeleportAction(@NotNull final DelayableTeleportAction action)
{
//noinspection ConstantConditions
if(action == null) return;
if(action.getDelay() == 0 || action.getPlayer().hasPermission("marry.bypass.delay"))
if(action.getDelay() == 0 || action.getPlayer().hasPermission(Permissions.BYPASS_DELAY))
{
action.run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

public class ChatCommand extends MarryCommand implements Listener
{
Expand Down Expand Up @@ -164,10 +165,10 @@ public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String s
}
else
{
String arg = args[args.length - 1].toLowerCase();
String arg = args[args.length - 1].toLowerCase(Locale.ENGLISH);
for(ProxiedPlayer player : plugin.getProxy().getPlayers())
{
if(player.getName().toLowerCase().startsWith(arg))
if(player.getName().toLowerCase(Locale.ENGLISH).startsWith(arg))
{
data.add(player.getName());
}
Expand Down Expand Up @@ -390,7 +391,7 @@ public static class ChatListenCommand extends MarryCommand

public ChatListenCommand(MarriageMaster plugin, ChatCommand chatCommand)
{
super(plugin, "listenchat", plugin.getLanguage().getTranslated("Commands.Description.ChatListen"), "marry.listenchat", true, plugin.getLanguage().getCommandAliases("ChatListen"));
super(plugin, "listenchat", plugin.getLanguage().getTranslated("Commands.Description.ChatListen"), Permissions.LISTEN_CHAT, true, plugin.getLanguage().getCommandAliases("ChatListen"));

this.chatCommand = chatCommand;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class HomeCommand extends MarryCommand

public HomeCommand(MarriageMaster plugin)
{
super(plugin, "home", plugin.getLanguage().getTranslated("Commands.Description.Home"), "marry.home", false, true, plugin.getLanguage().getCommandAliases("Home"));
super(plugin, "home", plugin.getLanguage().getTranslated("Commands.Description.Home"), Permissions.HOME, false, true, plugin.getLanguage().getCommandAliases("Home"));

messageNoHome = plugin.getLanguage().getMessage("Ingame.Home.NoHome");
messagePlayerNoHome = plugin.getLanguage().getMessage("Ingame.Home.PlayerNoHome");
Expand Down Expand Up @@ -91,7 +91,7 @@ public void execute(@NotNull CommandSender sender, @NotNull String mainCommandAl
//noinspection ConstantConditions
if(!blockedTo.contains(marriage.getHome().getHomeServer().toLowerCase(Locale.ENGLISH)))
{
if(delayed && !player.hasPermission("marry.bypass.delay"))
if(delayed && !player.hasPermission(Permissions.BYPASS_DELAY))
{
//noinspection ConstantConditions
communicator.sendMessage(((ProxiedPlayer) sender).getServer().getInfo(), "delayHome", player.getUUID().toString(), marriage.getPartner(player).getUUID().toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 GeorgH93
* Copyright (C) 2020 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,6 +22,7 @@
import at.pcgamingfreaks.MarriageMaster.Bungee.API.MarryCommand;
import at.pcgamingfreaks.MarriageMaster.Bungee.Listener.PluginChannelCommunicator;
import at.pcgamingfreaks.MarriageMaster.Bungee.MarriageMaster;
import at.pcgamingfreaks.MarriageMaster.Permissions;

import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.config.ServerInfo;
Expand All @@ -44,7 +45,7 @@ public class TpCommand extends MarryCommand

public TpCommand(MarriageMaster plugin)
{
super(plugin, "tp", plugin.getLanguage().getTranslated("Commands.Description.Tp"), "marry.tp", true, true, plugin.getLanguage().getCommandAliases("Tp"));
super(plugin, "tp", plugin.getLanguage().getTranslated("Commands.Description.Tp"), Permissions.TP, true, true, plugin.getLanguage().getCommandAliases("Tp"));

blockedFrom = plugin.getConfig().getTPBlackListedServersFrom();
blockedTo = plugin.getConfig().getTPBlackListedServersTo();
Expand Down Expand Up @@ -79,7 +80,7 @@ else if(partner.isOnline())
{
if(!blockedTo.contains(partner.getPlayer().getServer().getInfo().getName().toLowerCase()))
{
if(delayed && !sender.hasPermission("marry.bypass.delay"))
if(delayed && !sender.hasPermission(Permissions.BYPASS_DELAY))
{
communicator.sendMessage(player.getPlayer().getServer().getInfo(), "delayTP", player.getUUID().toString(), player.getUUID().toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

public class Permissions
{
public static final String BASE = "marry.";
private static final String BASE = "marry.";
private static final String BYPASS = BASE + "bypass.";
public static final String BACKPACK = BASE + "backpack";
public static final String CHAT = BASE + "chat";
public static final String CHAT_COLOR = CHAT + ".color";
Expand All @@ -43,6 +44,17 @@ public class Permissions
public static final String SET_PRIEST = BASE + "setpriest";
public static final String CHANGE_SURNAME = BASE + "changesurname";
public static final String TP = BASE + "tp";
public static final String BYPASS_BACKPACK_GAME_MODE = BYPASS + "backpackgamemode";
public static final String BYPASS_DELAY = BYPASS + "delay";
public static final String BYPASS_RANGELIMIT = BYPASS + "rangelimit";
public static final String BYPASS_GIFT_GAME_MODE = BYPASS + "giftgamemode";
public static final String BYPASS_WORLD_BLACKLIST = BYPASS + "worldblacklist";
public static final String BYPASS_TP_CONFIRMATION = BYPASS + "tpconfirmation";
public static final String BYPASS_VANISH = BYPASS + "vanish";
public static final String LISTEN_CHAT = BASE + "listenchat";
public static final String LISTEN_CHAT_AUTO_JOIN = LISTEN_CHAT + ".autojoin";
public static final String AUTO_ACCEPT_TP_REQUEST = BASE + "autoaccept.tprequest";
public static final String OFFLINEDIVORCE = BASE + "offlinedivorce";
public static final String UPDATE = BASE + "update";
public static final String RELOAD = BASE + "reload";
public static final String VERSION = BASE + "version";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2020 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package at.pcgamingfreaks.MarriageMaster;

import at.pcgamingfreaks.yaml.YAML;
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
import at.pcgamingfreaks.yaml.YamlKeyNotFoundException;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashSet;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class PermissionsTest
{
private static Collection<String> permissions;

@BeforeAll
public static void setup() throws IllegalAccessException
{ // Collect all permissions defined in the Permissions class
permissions = new HashSet<>();
for(Field declaredField : Permissions.class.getDeclaredFields())
{
if(declaredField.getName().equals("BASE") || declaredField.getName().equals("BYPASS")) continue;
permissions.add((String) declaredField.get(null));
}
}

private int countKeysStartingWith(Collection<String> keys, String startsWith)
{
int count = 0;
for(String key : keys)
{
if(key.startsWith(startsWith)) count++;
}
return count;
}

@Test
public void testPermissionsInPluginYaml() throws IOException, YamlInvalidContentException, YamlKeyNotFoundException
{
YAML pluginYaml = new YAML(new File("resources/plugin.yml"));
YAML permissionsYaml = pluginYaml.getSection("permissions");
// Check if all permissions are defined in the plugin.yml
for(String permission : permissions)
{
assertTrue(permissionsYaml.isSet(permission + ".description"), "The plugin.yml should contain the permission " + permission);
}
// Check if all the permissions defined in the plugin.yml are also defined in the Permissions class
Collection<String> keys = permissionsYaml.getKeys(true);
for(String key : keys)
{
if(!key.endsWith("description")) continue;
String perm = key.substring(0, key.length() - 12);
if(perm.contains(".size.")) continue; // Ignore size permissions
if(countKeysStartingWith(keys, perm + ".children") > 1) continue; // Skip all the permissions that are just for permission grouping
assertTrue(permissions.contains(perm), "The plugin.yml should not contain the permission " + perm);
}
}
}

0 comments on commit 13ba721

Please sign in to comment.