Added group whitelist system

This commit is contained in:
stijnb1234 2020-02-25 10:34:19 +01:00
parent 6b21dd687d
commit 92840b7864
7 changed files with 117 additions and 14 deletions

12
pom.xml
View file

@ -146,6 +146,10 @@
<id>CodeMC</id>
<url>https://repo.codemc.org/repository/maven-public</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
@ -197,6 +201,14 @@
<version>1.7</version>
<scope>compile</scope>
</dependency>
<!-- Vault, used for permission checks -->
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -13,7 +13,8 @@ public class Floor {
private String world;
private int y;
private ArrayList<LiftBlock> doorBlocks = new ArrayList<>();
private HashSet<UUID> whitelist = new HashSet<>();
private HashSet<UUID> userWhitelist = new HashSet<>();
private HashSet<String> groupWhitelist = new HashSet<>();
public Floor(int y, String world) {
this.y = y;
@ -47,7 +48,8 @@ public class Floor {
"world='" + world + '\'' +
", y=" + y +
", doorBlocks=" + doorBlocks +
", whitelist=" + whitelist +
", userWhitelist=" + userWhitelist +
", groupWhitelist=" + groupWhitelist +
'}';
}
}

View file

@ -6,6 +6,7 @@ import nl.SBDeveloper.V10Lift.API.Objects.Lift;
import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock;
import nl.SBDeveloper.V10Lift.API.Objects.LiftSign;
import nl.SBDeveloper.V10Lift.Managers.DataManager;
import nl.SBDeveloper.V10Lift.Managers.VaultManager;
import nl.SBDeveloper.V10Lift.Utils.ConfigUtil;
import nl.SBDeveloper.V10Lift.Utils.XMaterial;
import nl.SBDeveloper.V10Lift.V10LiftPlugin;
@ -535,8 +536,24 @@ public class V10LiftCommand implements CommandExecutor {
}
Lift lift = DataManager.getLift(DataManager.getEditPlayer(p.getUniqueId()));
OfflinePlayer wp = Bukkit.getOfflinePlayer(args[2]);
UUID wpu = wp.getUniqueId();
boolean isGroup = false;
String wgn = null;
UUID wpu = null;
if (args[2].startsWith("g:")) {
if (!V10LiftPlugin.isVaultEnabled()) {
sender.sendMessage(ChatColor.RED + "You can't add a group when Vault is not found.");
return true;
}
isGroup = true;
wgn = args[2].replace("g:", "");
if (!VaultManager.isGroup(wgn)) {
sender.sendMessage(ChatColor.RED + "Group " + wgn + " not found!");
return true;
}
} else {
OfflinePlayer wp = Bukkit.getOfflinePlayer(args[2]);
wpu = wp.getUniqueId();
}
String floor = null;
if (args.length < 4) {
@ -563,18 +580,36 @@ public class V10LiftCommand implements CommandExecutor {
Floor f = lift.getFloors().get(floor);
if (args[1].equalsIgnoreCase("add")) {
if (f.getWhitelist().contains(wpu)) {
sender.sendMessage(ChatColor.RED + "Whitelist already contains this user!");
if (isGroup) {
if (f.getGroupWhitelist().contains(wgn)) {
sender.sendMessage(ChatColor.RED + "Whitelist already contains this group!");
} else {
f.getGroupWhitelist().add(wgn);
sender.sendMessage(ChatColor.GREEN + "Group added to whitelist!");
}
} else {
f.getWhitelist().add(wpu);
sender.sendMessage(ChatColor.GREEN + "User added to whitelist!");
if (f.getUserWhitelist().contains(wpu)) {
sender.sendMessage(ChatColor.RED + "Whitelist already contains this user!");
} else {
f.getUserWhitelist().add(wpu);
sender.sendMessage(ChatColor.GREEN + "User added to whitelist!");
}
}
} else if (args[1].equalsIgnoreCase("del")) {
if (!f.getWhitelist().contains(wpu)) {
sender.sendMessage(ChatColor.RED + "Whitelist doesn't contain this user!");
if (isGroup) {
if (!f.getGroupWhitelist().contains(wgn)) {
sender.sendMessage(ChatColor.RED + "Whitelist doesn't contain this group!");
} else {
f.getGroupWhitelist().remove(wgn);
sender.sendMessage(ChatColor.GREEN + "Group removed from whitelist!");
}
} else {
f.getWhitelist().remove(wpu);
sender.sendMessage(ChatColor.GREEN + "User removed from whitelist!");
if (!f.getUserWhitelist().contains(wpu)) {
sender.sendMessage(ChatColor.RED + "Whitelist doesn't contain this user!");
} else {
f.getUserWhitelist().remove(wpu);
sender.sendMessage(ChatColor.GREEN + "User removed from whitelist!");
}
}
} else {
return helpCommand(sender);

View file

@ -4,6 +4,7 @@ import nl.SBDeveloper.V10Lift.API.Objects.Floor;
import nl.SBDeveloper.V10Lift.API.Objects.Lift;
import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock;
import nl.SBDeveloper.V10Lift.Managers.DataManager;
import nl.SBDeveloper.V10Lift.Managers.VaultManager;
import nl.SBDeveloper.V10Lift.Utils.ConfigUtil;
import nl.SBDeveloper.V10Lift.Utils.XMaterial;
import nl.SBDeveloper.V10Lift.V10LiftPlugin;
@ -316,7 +317,9 @@ public class PlayerInteractListener implements Listener {
Floor floor = lift.getFloors().get(f2);
if (lift.getY() == floor.getY()) {
sign.setLine(3, ChatColor.GREEN + f2);
} else if (!floor.getWhitelist().isEmpty() && !floor.getWhitelist().contains(p.getUniqueId()) && !p.hasPermission("v10lift.admin")) {
} else if (!floor.getUserWhitelist().isEmpty() && !floor.getUserWhitelist().contains(p.getUniqueId()) && !p.hasPermission("v10lift.admin")) {
sign.setLine(3, ChatColor.RED + f2);
} else if (!floor.getGroupWhitelist().isEmpty() && !VaultManager.userHasAnyGroup(p, floor.getGroupWhitelist()) && !p.hasPermission("v10lift.admin")) {
sign.setLine(3, ChatColor.RED + f2);
} else {
sign.setLine(3, ChatColor.YELLOW + f2);
@ -329,7 +332,13 @@ public class PlayerInteractListener implements Listener {
}
Floor floor = lift.getFloors().get(f);
if (!floor.getWhitelist().isEmpty() && !floor.getWhitelist().contains(p.getUniqueId()) && !p.hasPermission("v10lift.admin")) {
if (!floor.getUserWhitelist().isEmpty() && !floor.getUserWhitelist().contains(p.getUniqueId()) && !p.hasPermission("v10lift.admin")) {
p.sendMessage(ChatColor.RED + "You can't go to that floor!");
e.setCancelled(true);
return;
}
if (!floor.getGroupWhitelist().isEmpty() && !VaultManager.userHasAnyGroup(p, floor.getGroupWhitelist()) && !p.hasPermission("v10lift.admin")) {
p.sendMessage(ChatColor.RED + "You can't go to that floor!");
e.setCancelled(true);
return;

View file

@ -0,0 +1,32 @@
package nl.SBDeveloper.V10Lift.Managers;
import net.milkbowl.vault.permission.Permission;
import nl.SBDeveloper.V10Lift.V10LiftPlugin;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.HashSet;
public class VaultManager {
private static Permission perms = null;
public static boolean setupPermissions() {
RegisteredServiceProvider<Permission> rsp = V10LiftPlugin.getInstance().getServer().getServicesManager().getRegistration(Permission.class);
perms = rsp.getProvider();
return perms != null;
}
public static boolean isGroup(String groupName) {
return Arrays.asList(perms.getGroups()).contains(groupName);
}
public static boolean userHasAnyGroup(Player player, @Nonnull HashSet<String> whitelistSet) {
boolean found = false;
for (String group : whitelistSet) {
found = Arrays.asList(perms.getPlayerGroups(player)).contains(group);
}
return found;
}
}

View file

@ -10,6 +10,7 @@ import nl.SBDeveloper.V10Lift.Listeners.PlayerInteractListener;
import nl.SBDeveloper.V10Lift.Listeners.SignChangeListener;
import nl.SBDeveloper.V10Lift.Managers.DBManager;
import nl.SBDeveloper.V10Lift.Managers.DataManager;
import nl.SBDeveloper.V10Lift.Managers.VaultManager;
import nl.SBDevelopment.SBUtilities.Data.YamlFile;
import nl.SBDevelopment.SBUtilities.PrivateManagers.UpdateManager;
import nl.SBDevelopment.SBUtilities.SBUtilities;
@ -27,6 +28,7 @@ public class V10LiftPlugin extends JavaPlugin {
private static YamlFile config;
private static DBManager dbManager;
private static V10LiftAPI api;
private static boolean vault = false;
@Override
public void onEnable() {
@ -50,6 +52,12 @@ public class V10LiftPlugin extends JavaPlugin {
//Load the API
api = new V10LiftAPI();
//Load vault if found
if (Bukkit.getServer().getPluginManager().getPlugin("Vault") != null) {
vault = true;
VaultManager.setupPermissions();
}
//Load the command
Objects.requireNonNull(getCommand("v10lift"), "Internal error! Command not found.").setExecutor(new V10LiftCommand());
Objects.requireNonNull(getCommand("v10lift"), "Internal error! Command not found.").setTabCompleter(new V10LiftTabCompleter());
@ -109,4 +117,8 @@ public class V10LiftPlugin extends JavaPlugin {
public static V10LiftAPI getAPI() {
return api;
}
public static boolean isVaultEnabled() {
return vault;
}
}

View file

@ -3,6 +3,7 @@ main: nl.SBDeveloper.V10Lift.V10LiftPlugin
version: ${project.version}
api-version: "1.13"
author: SBDeveloper
softdepend: ["Vault"]
commands:
v10lift:
description: The V10Lift Command