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

Feature/2393 multiple home per world #5601

Open
wants to merge 6 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions Essentials/src/main/java/com/earth2me/essentials/ISettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,22 @@ public interface ISettings extends IConf {

Set getMultipleHomes();

Set<String> getHomesPerWorld();

Set<String> getHomesPerWorldGroup();

int getHomeLimit(String set);

int getWorldHomeLimit(String set);

int getWorldGroupHomeLimit(String set);

Set<String> getWorldGroupHomeList(String set);

boolean isHomeLimitPerWorldEnabled();

boolean isHomeLimitPerWorldGroupEnabled();

int getHomeLimit(User user);

int getSpawnMobLimit();
Expand Down
87 changes: 84 additions & 3 deletions Essentials/src/main/java/com/earth2me/essentials/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Objects;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;

import static com.earth2me.essentials.I18n.tl;

Expand Down Expand Up @@ -169,13 +171,28 @@ public Set<String> getMultipleHomes() {
return section == null ? null : ConfigurateUtil.getKeys(section);
}

@Override
public Set<String> getHomesPerWorld() {
final CommentedConfigurationNode section = config.getSection("homes-per-world");
return section == null ? null : ConfigurateUtil.getKeys(section);
}

@Override
public Set<String> getHomesPerWorldGroup() {
final CommentedConfigurationNode section = config.getSection("homes-per-world-group");
return section == null ? null : ConfigurateUtil.getKeys(section);
}

@Override
public int getHomeLimit(final User user) {
int limit = 1;
if (user.isAuthorized("essentials.sethome.multiple")) {
limit = getHomeLimit("default");
final boolean hasMultipleHomeAuthorization = user.isAuthorized("essentials.sethome.multiple");
if (!hasMultipleHomeAuthorization) {
return limit;
}

limit = getHomeLimit("default");

final Set<String> homeList = getMultipleHomes();
if (homeList != null) {
for (final String set : homeList) {
Expand All @@ -184,14 +201,78 @@ public int getHomeLimit(final User user) {
}
}
}

final boolean homesPerWorldEnabled = isHomeLimitPerWorldEnabled();
final boolean homesPerWorldGroupEnabled = isHomeLimitPerWorldGroupEnabled();

if(!homesPerWorldEnabled)
return limit;

if(homesPerWorldGroupEnabled) {
final Set<String> homesPerWorldGroup = getHomesPerWorldGroup();
if (homesPerWorldGroup != null) {
for (final String set : homesPerWorldGroup) {
if (limit < getWorldGroupHomeLimit(set) && isUserInWorldGroup(user, set)) {
limit = getWorldGroupHomeLimit(set);
}
}
}
}else{
final Set<String> homesPerWorld = getHomesPerWorld();
if (homesPerWorld != null) {
for (final String set : homesPerWorld) {
if (limit < getWorldHomeLimit(set) && isUserInWorld(user, set)) {
limit = getWorldHomeLimit(set);
}
}
}
}

return limit;
}

public boolean isUserInWorld(User user, String worldName) {
return Objects.requireNonNull(user.getWorld()).getName().equalsIgnoreCase(worldName);
}

public boolean isUserInWorldGroup(User user, String worldGroup) {
return getWorldGroupHomeList(worldGroup).stream().anyMatch(worldName -> isUserInWorld(user, worldName));
}

@Override
public int getHomeLimit(final String set) {
return config.getInt("sethome-multiple." + set, config.getInt("sethome-multiple.default", 3));
}

@Override
public int getWorldHomeLimit(String set) {
return config.getInt("homes-per-world." + set, config.getInt("sethome-multiple.default", 3));
}

@Override
public int getWorldGroupHomeLimit(String set) {
return config.getInt("homes-per-world-group." + set + ".home-limit", config.getInt("sethome-multiple.default", 3));
}

@Override
public Set<String> getWorldGroupHomeList(String set) {
final String worlds = config.getString("homes-per-world-group." + set + ".worlds", null);
if(worlds == null) {
return new HashSet<>();
}
return Arrays.stream(worlds.split(",")).collect(Collectors.toSet());
}

@Override
public boolean isHomeLimitPerWorldEnabled() {
return config.getBoolean("home-limit-per-world", false);
}

@Override
public boolean isHomeLimitPerWorldGroupEnabled() {
return config.getBoolean("home-limit-per-world-group", false);
}

private int _getChatRadius() {
return config.getInt("chat.radius", config.getInt("chat-radius", 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.UUID;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.earth2me.essentials.I18n.tl;

Expand Down Expand Up @@ -178,6 +179,12 @@ public List<String> getHomes() {
return new ArrayList<>(holder.homes().keySet());
}

public List<String> getHomesPerWorld(String worldName) {
return holder.homes().entrySet().stream()
.filter(homeLocation -> homeLocation.getValue().worldName().equalsIgnoreCase(worldName))
.map(Map.Entry::getKey).collect(Collectors.toList());
}

public void setHome(String name, final Location loc) {
//Invalid names will corrupt the yaml
name = StringUtil.safeString(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import org.bukkit.Server;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;

import static com.earth2me.essentials.I18n.tl;
Expand Down Expand Up @@ -120,8 +122,8 @@ private String getHomeLimit(final User player) {
return Integer.toString(ess.getSettings().getHomeLimit(player));
}

private void goHome(final User user, final User player, final String home, final Trade charge, final CompletableFuture<Boolean> future) throws Exception {
if (home.length() < 1) {
public void goHome(final User user, final User player, final String home, final Trade charge, final CompletableFuture<Boolean> future) throws Exception {
if (home.isEmpty()) {
throw new NotEnoughArgumentsException();
}
final Location loc = player.getHome(home);
Expand All @@ -131,6 +133,9 @@ private void goHome(final User user, final User player, final String home, final
if (user.getWorld() != loc.getWorld() && ess.getSettings().isWorldHomePermissions() && !user.isAuthorized("essentials.worlds." + loc.getWorld().getName())) {
throw new Exception(tl("noPerm", "essentials.worlds." + loc.getWorld().getName()));
}
if(!isUserHomeInWorldOrWorldGroupWorld(user.getWorld().getName(), Objects.requireNonNull(loc.getWorld()).getName())) {
throw new Exception(tl("teleportNotPossible"));
}
final UserTeleportHomeEvent event = new UserTeleportHomeEvent(user, home, loc, UserTeleportHomeEvent.HomeType.HOME);
user.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
Expand All @@ -143,6 +148,27 @@ private void goHome(final User user, final User player, final String home, final
}
}

public boolean isUserHomeInWorldOrWorldGroupWorld(String worldFrom, String worldTo) {
final boolean isHomeLimitPerWorldEnabled = ess.getSettings().isHomeLimitPerWorldEnabled();
final boolean isHomeLimitPerWorldGroupEnabled = ess.getSettings().isHomeLimitPerWorldGroupEnabled();
if(!isHomeLimitPerWorldEnabled) {
return true;
}
if(isHomeLimitPerWorldGroupEnabled) {
final Set<String> worldGroups = ess.getSettings().getHomesPerWorldGroup();

for (String wGroup : worldGroups) {
final Set<String> worldsPerWG = ess.getSettings().getWorldGroupHomeList(wGroup);

if (worldsPerWG.contains(worldFrom) && worldsPerWG.contains(worldTo))
return true;
}
}else{
return worldFrom.equalsIgnoreCase(worldTo);
}
return false;
}

@Override
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
final boolean canVisitOthers = user.isAuthorized("essentials.home.others");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static com.earth2me.essentials.I18n.tl;
Expand Down Expand Up @@ -90,7 +91,10 @@ public void run(final Server server, final User user, final String commandLabel,
private boolean checkHomeLimit(final User user, final User usersHome, final String name) throws Exception {
if (!user.isAuthorized("essentials.sethome.multiple.unlimited")) {
final int limit = ess.getSettings().getHomeLimit(user);
if (usersHome.getHomes().size() >= limit) {
final List<String> homes = usersHome.isReachable() ?
usersHome.getHomesPerWorld(Objects.requireNonNull(usersHome.getLocation().getWorld()).getName())
: usersHome.getHomes();
if (homes.size() >= limit) {
if (usersHome.getHomes().contains(name)) {
return false;
}
Expand Down
32 changes: 32 additions & 0 deletions Essentials/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,38 @@ spawn-if-no-home: true
# Should players be asked to provide confirmation for homes which they attempt to overwrite?
confirm-home-overwrite: false

# This setting enables to have for each world limit of homes
# e.g. when enabled, user can have 3 homes in world, 3 in world_nether and 3 in world_the_end.
home-limit-per-world: false

# When home-limit-per-world is enabled, with configuration sethome-multiple whichever setting has a number higher,
# it then will be used
# e.g. if sethome-multiple.vip is 5 and user has permission for this, and user is in world named world_the_end which
# limit for home is 2, then user will have limit of higher number, therefore 5 homes in that world.
# To not follow this logic, please remove user permission for that 'home rank'.
# If world is not present in this configuration, configuration sethome-multiple.default
# will be used as a home limit for that world.
homes-per-world:
world: 5
world_the_end: 2

# For this configuration, previous configuration home-limit-per-world needs to be enabled.
# This allows you to create 'world groups' which can consist of how many worlds you want
# and set limit for these worlds, then they will act as one world with their limit.
# e.g. 'world group' named player-worlds which has worlds 'world' and 'world_nether'
# which home limit is set to 4. User will have maximum of 4 homes in these worlds total.
# If world is not present in this configuration, configuration sethome-multiple.default
# will be used as a home limit for that world.
home-limit-per-world-group: false

# Name of the world group can be any english letter with spaces that are -
# Worlds are split by a colon without spaces, as shown in the example: world,world_nether .
# Worlds in a 'world group' act as one world.
homes-per-world-group:
player-worlds:
worlds: world,world_nether
home-limit: 4

############################################################
# +------------------------------------------------------+ #
# | Economy | #
Expand Down
1 change: 1 addition & 0 deletions Essentials/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,7 @@ teleportDisabled=\u00a7c{0} \u00a74has teleportation disabled.
teleportHereRequest=\u00a7c{0}\u00a76 has requested that you teleport to them.
teleportHome=\u00a76Teleporting to \u00a7c{0}\u00a76.
teleporting=\u00a76Teleporting...
teleportNotPossible=\u00a7cTeleportation to that location is not possible from this world
teleportInvalidLocation=Value of coordinates cannot be over 30000000
teleportNewPlayerError=\u00a74Failed to teleport new player\!
teleportNoAcceptPermission=\u00a7c{0} \u00a74does not have permission to accept teleport requests.
Expand Down
Loading