Skip to content

Commit

Permalink
Implemented trusted command to list trusted players
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksilassila committed Sep 30, 2020
1 parent 18786fb commit 8d8e854
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 139 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ To further customize the plugin, check `plugins/islands/config.yml`.
| `islands.home` | Use /home command |
| `islands.home.list` | Use /homes command |
| `islands.turst` | Trust person |
| `islands.turst.list` | Not implemented |
| `islands.turst.list` | List island's trusted players |
| `islands.untrust` | Untrust person |
| `islands.visit` | Visit island |

Expand All @@ -125,6 +125,7 @@ Bypasses
| `islands.bypass.name` | Name anyone's island |
| `islands.bypass.unname` | Unname anyone's island |
| `islands.bypass.trust` | Add trusted person to anyone's island |
| `islands.bypass.trust.list` | List any island's trusted players |
| `islands.bypass.untrust` | Remove trusted person from anyone's island |
| `islands.bypass.protection` | Interact with anyone's island |
| `islands.bypass.home` | Use /home from anywhere |
9 changes: 7 additions & 2 deletions src/me/aleksilassila/islands/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.aleksilassila.islands.commands.IslandCommands;
import me.aleksilassila.islands.commands.IslandManagmentCommands;
import me.aleksilassila.islands.commands.TrustCommands;
import me.aleksilassila.islands.generation.EmptyWorldGenerator;
import me.aleksilassila.islands.listeners.IslandsListener;
import net.milkbowl.vault.permission.Permission;
Expand Down Expand Up @@ -56,8 +57,12 @@ public void onEnable() {

islandCommands.new HomeCommand();
islandCommands.new VisitCommand();
islandCommands.new TrustCommand();
islandCommands.new UntrustCommand();

TrustCommands trustCommands = new TrustCommands(this);

trustCommands.new UntrustCommand();
trustCommands.new TrustCommand();
trustCommands.new ListTrustedCommand();

new IslandsListener(this);

Expand Down
3 changes: 0 additions & 3 deletions src/me/aleksilassila/islands/Permissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ public static boolean checkPermission(Player player, String permission) {

String[] parts = permission.split("\\.");

Bukkit.getLogger().info("Parts: " + String.join(" ", parts));

for (int index = 0; index < parts.length; index++) {
String joined = String.join(".", Arrays.copyOfRange(parts, 0, index + 1));
Bukkit.getLogger().info("Starred: " + joined + ".*");
if (player.hasPermission(joined + ".*")) return true;
}

Expand Down
122 changes: 3 additions & 119 deletions src/me/aleksilassila/islands/commands/IslandCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,115 +28,6 @@ public IslandCommands(Main plugin) {
this.grid = plugin.islands.grid;
}

public class UntrustCommand implements CommandExecutor {
public UntrustCommand() {
plugin.getCommand("untrust").setExecutor(this);
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) return false;

Player player = (Player) sender;

plugin.islands.confirmations.remove(player.getUniqueId().toString());

if (!Permissions.checkPermission(player, Permissions.island.untrust)) {
player.sendMessage(Messages.error.NO_PERMISSION);
return true;
}


if (!player.getWorld().equals(plugin.islandsWorld)) {
player.sendMessage(Messages.error.WRONG_WORLD);
return true;
}


if (args.length != 1) {
player.sendMessage(Messages.help.UNTRUST);
return true;
}

String ownerUUID = plugin.islands.grid.getBlockOwnerUUID(player.getLocation().getBlockX(), player.getLocation().getBlockZ());
String islandId = plugin.islands.grid.getIslandId(player.getLocation().getBlockX(), player.getLocation().getBlockZ());

if (ownerUUID == null || islandId == null) {
player.sendMessage(Messages.error.NOT_ON_ISLAND);
return true;
}

if (!ownerUUID.equals(player.getUniqueId().toString()) && !Permissions.checkPermission(player, Permissions.bypass.untrust)) {
player.sendMessage(Messages.error.NOT_OWNED);
return true;
}

Player targetPlayer = Bukkit.getPlayer(args[0]);

if (targetPlayer == null) {
player.sendMessage(Messages.error.PLAYER_NOT_FOUND);
return true;
}

plugin.islands.grid.removeTrusted(islandId, targetPlayer.getUniqueId().toString());

player.sendMessage(Messages.success.UNTRUSTED);

return true;
}
}

public class TrustCommand implements CommandExecutor {
public TrustCommand() {
plugin.getCommand("trust").setExecutor(this);
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) return false;

Player player = (Player) sender;

plugin.islands.confirmations.remove(player.getUniqueId().toString());

if (!Permissions.checkPermission(player, Permissions.island.trust)) {
player.sendMessage(Messages.error.NO_PERMISSION);
return true;
}

if (args.length != 1) {
player.sendMessage(Messages.help.TRUST);
return true;
}

String ownerUUID = plugin.islands.grid.getBlockOwnerUUID(player.getLocation().getBlockX(), player.getLocation().getBlockZ());
String islandId = plugin.islands.grid.getIslandId(player.getLocation().getBlockX(), player.getLocation().getBlockZ());

if (ownerUUID == null || islandId == null) {
player.sendMessage(Messages.error.NOT_ON_ISLAND);
return true;
}

if (!ownerUUID.equals(player.getUniqueId().toString()) && !Permissions.checkPermission(player, Permissions.bypass.trust)) {
player.sendMessage(Messages.error.NOT_OWNED);
return true;
}

Player targetPlayer = Bukkit.getPlayer(args[0]);

if (targetPlayer == null) {
player.sendMessage(Messages.error.PLAYER_NOT_FOUND);
return true;
}

plugin.islands.grid.addTrusted(islandId, targetPlayer.getUniqueId().toString());

player.sendMessage(Messages.success.TRUSTED);

return true;
}
}

public class VisitCommand implements CommandExecutor {

public VisitCommand() {
Expand Down Expand Up @@ -174,7 +65,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (islandId != null) {
player.teleport(plugin.islands.grid.getIslandSpawn(islandId));
} else {
player.sendMessage(Messages.error.ISLAND_NOT_FOUND);
player.sendMessage(Messages.error.HOME_NOT_FOUND);
}

return true;
Expand Down Expand Up @@ -295,19 +186,15 @@ static class Messages extends ChatUtils {
static class error {
public static final String ISLAND_NOT_FOUND = error("404 - Home not found.");
public static final String NO_PERMISSION = error("You don't have permission to use this command.");
public static final String NOT_ON_ISLAND = error("You have to be on an island.");
public static final String NOT_OWNED = error("You don't own this island.");
public static final String PLAYER_NOT_FOUND = error("Player not found.");
public static final String WRONG_WORLD = error("You can't use that command in this world.");
public static final String HOME_NOT_FOUND = error("404 - Home not found :(");

public static String COOLDOWN(int remainingTime) {
return error("You took damage recently. You have to wait for " + remainingTime + "s before teleporting.");
}
}

static class success {
public static final String UNTRUSTED = success("Player untrusted!");
public static final String TRUSTED = success("Player trusted!");


public static String HOMES_FOUND(int amount) {
return success("Found " + amount + " home(s).");
Expand All @@ -320,9 +207,6 @@ static class info {
}

static class help {

public static final String UNTRUST = info("/untrust <player> (You have to be on target island)");
public static final String TRUST = info("/trust <player> (You have to be on target island)");
public static final String VISIT = info("Usage: /visit name");
public static final String HOME = error("Usage: /home <id>");
}
Expand Down
174 changes: 174 additions & 0 deletions src/me/aleksilassila/islands/commands/TrustCommands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package me.aleksilassila.islands.commands;

import me.aleksilassila.islands.Main;
import me.aleksilassila.islands.Permissions;
import me.aleksilassila.islands.utils.Messages;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.UUID;

public class TrustCommands {
private Main plugin;

public TrustCommands(Main plugin) {
this.plugin = plugin;
}

public class UntrustCommand implements CommandExecutor {
public UntrustCommand() {
plugin.getCommand("untrust").setExecutor(this);
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) return false;

Player player = (Player) sender;

plugin.islands.confirmations.remove(player.getUniqueId().toString());

if (!Permissions.checkPermission(player, Permissions.island.untrust)) {
player.sendMessage(Messages.error.NO_PERMISSION);
return true;
}


if (!player.getWorld().equals(plugin.islandsWorld)) {
player.sendMessage(Messages.error.WRONG_WORLD);
return true;
}


if (args.length != 1) {
player.sendMessage(Messages.help.UNTRUST);
return true;
}

String ownerUUID = plugin.islands.grid.getBlockOwnerUUID(player.getLocation().getBlockX(), player.getLocation().getBlockZ());
String islandId = plugin.islands.grid.getIslandId(player.getLocation().getBlockX(), player.getLocation().getBlockZ());

if (ownerUUID == null || islandId == null) {
player.sendMessage(Messages.error.NOT_ON_ISLAND);
return true;
}

if (!ownerUUID.equals(player.getUniqueId().toString()) && !Permissions.checkPermission(player, Permissions.bypass.untrust)) {
player.sendMessage(Messages.error.NOT_OWNED);
return true;
}

Player targetPlayer = Bukkit.getPlayer(args[0]);

if (targetPlayer == null) {
player.sendMessage(Messages.error.PLAYER_NOT_FOUND);
return true;
}

plugin.islands.grid.removeTrusted(islandId, targetPlayer.getUniqueId().toString());

player.sendMessage(Messages.success.UNTRUSTED);

return true;
}
}

public class TrustCommand implements CommandExecutor {
public TrustCommand() {
plugin.getCommand("trust").setExecutor(this);
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) return false;

Player player = (Player) sender;

plugin.islands.confirmations.remove(player.getUniqueId().toString());

if (!Permissions.checkPermission(player, Permissions.island.trust)) {
player.sendMessage(Messages.error.NO_PERMISSION);
return true;
}

if (args.length != 1) {
player.sendMessage(Messages.help.TRUST);
return true;
}

String ownerUUID = plugin.islands.grid.getBlockOwnerUUID(player.getLocation().getBlockX(), player.getLocation().getBlockZ());
String islandId = plugin.islands.grid.getIslandId(player.getLocation().getBlockX(), player.getLocation().getBlockZ());

if (ownerUUID == null || islandId == null) {
player.sendMessage(Messages.error.NOT_ON_ISLAND);
return true;
}

if (!ownerUUID.equals(player.getUniqueId().toString()) && !Permissions.checkPermission(player, Permissions.bypass.trust)) {
player.sendMessage(Messages.error.NOT_OWNED);
return true;
}

Player targetPlayer = Bukkit.getPlayer(args[0]);

if (targetPlayer == null) {
player.sendMessage(Messages.error.PLAYER_NOT_FOUND);
return true;
}

plugin.islands.grid.addTrusted(islandId, targetPlayer.getUniqueId().toString());

player.sendMessage(Messages.success.TRUSTED);

return true;
}
}

public class ListTrustedCommand implements CommandExecutor {
public ListTrustedCommand() {
plugin.getCommand("trusted").setExecutor(this);
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) return false;

Player player = (Player) sender;

plugin.islands.confirmations.remove(player.getUniqueId().toString());

if (!Permissions.checkPermission(player, Permissions.island.listTrusted)) {
player.sendMessage(Messages.error.NO_PERMISSION);
return true;
}

String ownerUUID = plugin.islands.grid.getBlockOwnerUUID(player.getLocation().getBlockX(), player.getLocation().getBlockZ());
String islandId = plugin.islands.grid.getIslandId(player.getLocation().getBlockX(), player.getLocation().getBlockZ());

if (ownerUUID == null || islandId == null) {
player.sendMessage(Messages.error.NOT_ON_ISLAND);
return true;
}

if (!ownerUUID.equals(player.getUniqueId().toString()) && !Permissions.checkPermission(player, Permissions.bypass.listTrusted)) {
player.sendMessage(Messages.error.NOT_OWNED);
return true;
}

List<String> trustedList = plugin.islands.grid.getTrusted(islandId);

player.sendMessage(Messages.info.TRUSTED_INFO(trustedList.size()));
for (String uuid : trustedList) {
Player trustedPlayer = Bukkit.getPlayer(UUID.fromString(uuid));

if (trustedPlayer != null) player.sendMessage(Messages.info.TRUSTED_PLAYER(trustedPlayer.getDisplayName()));
}

return true;
}
}
}
Loading

0 comments on commit 8d8e854

Please sign in to comment.