Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed AddOwner Offline :D #430

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 17 additions & 61 deletions src/main/java/dev/espi/protectionstones/commands/ArgAddRemove.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/

package dev.espi.protectionstones.commands;

import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
Expand Down Expand Up @@ -56,69 +41,55 @@ public HashMap<String, Boolean> getRegisteredFlags() {
@Override
public boolean executeArgument(CommandSender s, String[] args, HashMap<String, String> flags) {
Player p = (Player) s;
String operationType = args[0].toLowerCase(); // add, remove, addowner, removeowner
String operationType = args[0].toLowerCase();

// check permission
if ((operationType.equals("add") || operationType.equals("remove")) && !p.hasPermission("protectionstones.members")) {
return PSL.msg(p, PSL.NO_PERMISSION_MEMBERS.msg());
} else if ((operationType.equals("addowner") || operationType.equals("removeowner")) && !p.hasPermission("protectionstones.owners")) {
return PSL.msg(p, PSL.NO_PERMISSION_OWNERS.msg());
}

// determine player to be added or removed
if (args.length < 2) {
return PSL.msg(p, PSL.COMMAND_REQUIRES_PLAYER_NAME.msg());
}
if (!UUIDCache.containsName(args[1])) {
return PSL.msg(p, PSL.PLAYER_NOT_FOUND.msg());
}

// user being added
UUID addPlayerUuid = UUIDCache.getUUIDFromName(args[1]);
String addPlayerName = UUIDCache.getNameFromUUID(addPlayerUuid);

// getting player regions is slow, so run it async
Bukkit.getServer().getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> {
List<PSRegion> regions;

// obtain region list that player is being added to or removed from
if (flags.containsKey("-a")) { // add or remove to all regions a player owns

// don't let players remove themself from all of their regions
if (flags.containsKey("-a")) {
if (operationType.equals("removeowner") && addPlayerUuid.equals(p.getUniqueId())) {
PSL.msg(p, PSL.CANNOT_REMOVE_YOURSELF_FROM_ALL_REGIONS.msg());
return;
}

regions = PSPlayer.fromPlayer(p).getPSRegions(p.getWorld(), false);
} else { // add or remove to one region (the region currently in)
} else {
PSRegion r = PSRegion.fromLocationGroup(p.getLocation());

if (r == null) {
PSL.msg(p, PSL.NOT_IN_REGION.msg());
return;
} else if (WGUtils.hasNoAccess(r.getWGRegion(), p, WorldGuardPlugin.inst().wrapPlayer(p), false)) {
PSL.msg(p, PSL.NO_ACCESS.msg());
return;
} else if (operationType.equals("removeowner") && addPlayerUuid.equals(p.getUniqueId()) && r.getOwners().size() == 1) {
// don't let users remove themself if they are the last owner of the region
PSL.msg(p, PSL.CANNOT_REMOVE_YOURSELF_LAST_OWNER.msg());
return;
}

regions = Collections.singletonList(r);
}

// check that the player is not over their limit if they are being set owner
if (operationType.equals("addowner")) {
if (determinePlayerSurpassedLimit(p, regions, PSPlayer.fromUUID(addPlayerUuid))) {
return;
}
}

// apply operation to regions
for (PSRegion r : regions) {

if (operationType.equals("add") || operationType.equals("addowner")) {
if (flags.containsKey("-a")) {
PSL.msg(p, PSL.ADDED_TO_REGION_SPECIFIC.msg()
Expand All @@ -127,13 +98,9 @@ public boolean executeArgument(CommandSender s, String[] args, HashMap<String, S
} else {
PSL.msg(p, PSL.ADDED_TO_REGION.msg().replace("%player%", addPlayerName));
}

// add to WorldGuard profile cache
Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> UUIDCache.storeWGProfile(addPlayerUuid, addPlayerName));

} else if ((operationType.equals("remove") && r.isMember(addPlayerUuid))
|| (operationType.equals("removeowner") && r.isOwner(addPlayerUuid))) {

if (flags.containsKey("-a")) {
PSL.msg(p, PSL.REMOVED_FROM_REGION_SPECIFIC.msg()
.replace("%player%", addPlayerName)
Expand Down Expand Up @@ -167,13 +134,12 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg

try {
if (args.length == 2 || (args.length == 3 && args[1].equals("-a"))) {

switch (args[0].toLowerCase()) {
case "add":
case "addowner":
List<String> names = new ArrayList<>();
for (Player pAdd : Bukkit.getOnlinePlayers()) {
if (p.canSee(pAdd)) { // check if the player is not hidden
if (p.canSee(pAdd)) {
names.add(pAdd.getName());
}
}
Expand Down Expand Up @@ -205,29 +171,19 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
}

public boolean determinePlayerSurpassedLimit(Player commandSender, List<PSRegion> regionsToBeAddedTo, PSPlayer addedPlayer) {

if (addedPlayer.getPlayer() == null && !ProtectionStones.getInstance().isLuckPermsSupportEnabled()) { // offline player
if (ProtectionStones.getInstance().getConfigOptions().allowAddownerForOfflinePlayersWithoutLp) {
// bypass config option
return false;
} else {
// we need luckperms to determine region limits for offline players, so if luckperms isn't detected, prevent the action
PSL.msg(commandSender, PSL.ADDREMOVE_PLAYER_NEEDS_TO_BE_ONLINE.msg());
return true;
}
}

// find total region amounts after player is added to the regions, and their existing total
String err = LimitUtil.checkAddOwner(addedPlayer, regionsToBeAddedTo.stream()
.flatMap(r -> {
if (r instanceof PSGroupRegion) {
return ((PSGroupRegion) r).getMergedRegions().stream();
}
return Stream.of(r);
})
.map(PSRegion::getTypeOptions)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
String err = LimitUtil.checkAddOwner(
addedPlayer,
regionsToBeAddedTo.stream()
.flatMap(r -> {
if (r instanceof PSGroupRegion) {
return ((PSGroupRegion) r).getMergedRegions().stream();
}
return Stream.of(r);
})
.map(PSRegion::getTypeOptions)
.filter(Objects::nonNull)
.collect(Collectors.toList())
);
if (err.equals("")) {
return false;
} else {
Expand Down