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

View file

@ -13,7 +13,8 @@ public class Floor {
private String world; private String world;
private int y; private int y;
private ArrayList<LiftBlock> doorBlocks = new ArrayList<>(); 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) { public Floor(int y, String world) {
this.y = y; this.y = y;
@ -47,7 +48,8 @@ public class Floor {
"world='" + world + '\'' + "world='" + world + '\'' +
", y=" + y + ", y=" + y +
", doorBlocks=" + doorBlocks + ", 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.LiftBlock;
import nl.SBDeveloper.V10Lift.API.Objects.LiftSign; import nl.SBDeveloper.V10Lift.API.Objects.LiftSign;
import nl.SBDeveloper.V10Lift.Managers.DataManager; import nl.SBDeveloper.V10Lift.Managers.DataManager;
import nl.SBDeveloper.V10Lift.Managers.VaultManager;
import nl.SBDeveloper.V10Lift.Utils.ConfigUtil; import nl.SBDeveloper.V10Lift.Utils.ConfigUtil;
import nl.SBDeveloper.V10Lift.Utils.XMaterial; import nl.SBDeveloper.V10Lift.Utils.XMaterial;
import nl.SBDeveloper.V10Lift.V10LiftPlugin; import nl.SBDeveloper.V10Lift.V10LiftPlugin;
@ -535,8 +536,24 @@ public class V10LiftCommand implements CommandExecutor {
} }
Lift lift = DataManager.getLift(DataManager.getEditPlayer(p.getUniqueId())); Lift lift = DataManager.getLift(DataManager.getEditPlayer(p.getUniqueId()));
OfflinePlayer wp = Bukkit.getOfflinePlayer(args[2]); boolean isGroup = false;
UUID wpu = wp.getUniqueId(); 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; String floor = null;
if (args.length < 4) { if (args.length < 4) {
@ -563,18 +580,36 @@ public class V10LiftCommand implements CommandExecutor {
Floor f = lift.getFloors().get(floor); Floor f = lift.getFloors().get(floor);
if (args[1].equalsIgnoreCase("add")) { if (args[1].equalsIgnoreCase("add")) {
if (f.getWhitelist().contains(wpu)) { if (isGroup) {
sender.sendMessage(ChatColor.RED + "Whitelist already contains this user!"); 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 { } else {
f.getWhitelist().add(wpu); if (f.getUserWhitelist().contains(wpu)) {
sender.sendMessage(ChatColor.GREEN + "User added to whitelist!"); 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")) { } else if (args[1].equalsIgnoreCase("del")) {
if (!f.getWhitelist().contains(wpu)) { if (isGroup) {
sender.sendMessage(ChatColor.RED + "Whitelist doesn't contain this user!"); 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 { } else {
f.getWhitelist().remove(wpu); if (!f.getUserWhitelist().contains(wpu)) {
sender.sendMessage(ChatColor.GREEN + "User removed from whitelist!"); sender.sendMessage(ChatColor.RED + "Whitelist doesn't contain this user!");
} else {
f.getUserWhitelist().remove(wpu);
sender.sendMessage(ChatColor.GREEN + "User removed from whitelist!");
}
} }
} else { } else {
return helpCommand(sender); 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.Lift;
import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock; import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock;
import nl.SBDeveloper.V10Lift.Managers.DataManager; import nl.SBDeveloper.V10Lift.Managers.DataManager;
import nl.SBDeveloper.V10Lift.Managers.VaultManager;
import nl.SBDeveloper.V10Lift.Utils.ConfigUtil; import nl.SBDeveloper.V10Lift.Utils.ConfigUtil;
import nl.SBDeveloper.V10Lift.Utils.XMaterial; import nl.SBDeveloper.V10Lift.Utils.XMaterial;
import nl.SBDeveloper.V10Lift.V10LiftPlugin; import nl.SBDeveloper.V10Lift.V10LiftPlugin;
@ -316,7 +317,9 @@ public class PlayerInteractListener implements Listener {
Floor floor = lift.getFloors().get(f2); Floor floor = lift.getFloors().get(f2);
if (lift.getY() == floor.getY()) { if (lift.getY() == floor.getY()) {
sign.setLine(3, ChatColor.GREEN + f2); 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); sign.setLine(3, ChatColor.RED + f2);
} else { } else {
sign.setLine(3, ChatColor.YELLOW + f2); sign.setLine(3, ChatColor.YELLOW + f2);
@ -329,7 +332,13 @@ public class PlayerInteractListener implements Listener {
} }
Floor floor = lift.getFloors().get(f); 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!"); p.sendMessage(ChatColor.RED + "You can't go to that floor!");
e.setCancelled(true); e.setCancelled(true);
return; 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.Listeners.SignChangeListener;
import nl.SBDeveloper.V10Lift.Managers.DBManager; import nl.SBDeveloper.V10Lift.Managers.DBManager;
import nl.SBDeveloper.V10Lift.Managers.DataManager; import nl.SBDeveloper.V10Lift.Managers.DataManager;
import nl.SBDeveloper.V10Lift.Managers.VaultManager;
import nl.SBDevelopment.SBUtilities.Data.YamlFile; import nl.SBDevelopment.SBUtilities.Data.YamlFile;
import nl.SBDevelopment.SBUtilities.PrivateManagers.UpdateManager; import nl.SBDevelopment.SBUtilities.PrivateManagers.UpdateManager;
import nl.SBDevelopment.SBUtilities.SBUtilities; import nl.SBDevelopment.SBUtilities.SBUtilities;
@ -27,6 +28,7 @@ public class V10LiftPlugin extends JavaPlugin {
private static YamlFile config; private static YamlFile config;
private static DBManager dbManager; private static DBManager dbManager;
private static V10LiftAPI api; private static V10LiftAPI api;
private static boolean vault = false;
@Override @Override
public void onEnable() { public void onEnable() {
@ -50,6 +52,12 @@ public class V10LiftPlugin extends JavaPlugin {
//Load the API //Load the API
api = new V10LiftAPI(); api = new V10LiftAPI();
//Load vault if found
if (Bukkit.getServer().getPluginManager().getPlugin("Vault") != null) {
vault = true;
VaultManager.setupPermissions();
}
//Load the command //Load the command
Objects.requireNonNull(getCommand("v10lift"), "Internal error! Command not found.").setExecutor(new V10LiftCommand()); Objects.requireNonNull(getCommand("v10lift"), "Internal error! Command not found.").setExecutor(new V10LiftCommand());
Objects.requireNonNull(getCommand("v10lift"), "Internal error! Command not found.").setTabCompleter(new V10LiftTabCompleter()); 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() { public static V10LiftAPI getAPI() {
return api; return api;
} }
public static boolean isVaultEnabled() {
return vault;
}
} }

View file

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