Add /rtp with normal distribution, economy hook, home limits, new tests, locales

This commit is contained in:
William committed 2022-08-19 19:44:07 +01:00
1 parent 0bb33c6f1e
commit 08e6569027
39 files changed
+1128 -188

No files matched your search

@@ -1,21 +1,31 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
public class BackCommand extends CommandBase {
public BackCommand(@NotNull HuskHomes implementor) {
protected BackCommand(@NotNull HuskHomes implementor) {
super("back", Permission.COMMAND_BACK, implementor);
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
plugin.getDatabase().getLastPosition(onlineUser).thenAccept(lastPosition ->
lastPosition.ifPresentOrElse(position -> plugin.getTeleportManager().timedTeleport(onlineUser, position)
.thenAccept(result -> plugin.getTeleportManager().finishTeleport(onlineUser, result)),
lastPosition.ifPresentOrElse(position -> {
// Validate the /back command economy action and that the user has sufficient funds if needed
if (!plugin.validateEconomyCheck(onlineUser, Settings.EconomyAction.BACK_COMMAND)) {
return;
}
plugin.performEconomyTransaction(onlineUser, Settings.EconomyAction.BACK_COMMAND);
// Teleport the player
plugin.getTeleportManager().timedTeleport(onlineUser, position)
.thenAccept(result -> plugin.getTeleportManager().finishTeleport(onlineUser, result));
},
() -> plugin.getLocales().getLocale("error_no_last_position").ifPresent(onlineUser::sendMessage)));
}
@@ -13,7 +13,7 @@ import java.util.stream.Collectors;
public class DelHomeCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
public DelHomeCommand(@NotNull HuskHomes implementor) {
protected DelHomeCommand(@NotNull HuskHomes implementor) {
super("delhome", Permission.COMMAND_DELETE_HOME, implementor);
}
@@ -11,7 +11,7 @@ import java.util.stream.Collectors;
public class DelWarpCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
public DelWarpCommand(@NotNull HuskHomes implementor) {
protected DelWarpCommand(@NotNull HuskHomes implementor) {
super("delwarp", Permission.COMMAND_DELETE_WARP, implementor);
}
@@ -2,6 +2,7 @@ package net.william278.huskhomes.command;
import de.themoep.minedown.MineDown;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.PositionMeta;
@@ -204,26 +205,46 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons
return;
}
//todo economy check here
plugin.getSavedPositionManager().updateHomePrivacy(home, newIsPublic).thenRun(() -> {
if (home.owner.uuid.equals(editor.uuid)) {
editor.sendMessage(plugin.getLocales().getLocale(
"edit_home_privacy_" + privacyKeyedString + "_success",
home.meta.name).orElse(new MineDown("")));
} else {
editor.sendMessage(plugin.getLocales().getLocale(
"edit_home_privacy_" + privacyKeyedString + "_success_other",
home.owner.username, home.meta.name).orElse(new MineDown("")));
// Perform checks if making the home public
if (newIsPublic && !otherOwner) {
// Check against maximum public homes
final List<Home> existingPublicHomes = plugin.getDatabase().getHomes(editor).join().stream()
.filter(existingHome -> existingHome.isPublic).toList();
final int maxPublicHomes = editor.getMaxPublicHomes(plugin.getSettings().maxPublicHomes,
plugin.getSettings().stackPermissionLimits);
if (existingPublicHomes.size() >= maxPublicHomes) {
plugin.getLocales().getLocale("error_edit_home_maximum_public_homes")
.ifPresent(editor::sendMessage);
return;
}
// Show the menu if the menu flag is set
if (showMenuFlag.get()) {
getHomeEditorWindow(home, false, otherOwner,
!otherOwner || editor.hasPermission(Permission.COMMAND_HOME_OTHER.node))
.forEach(editor::sendMessage);
// Check against economy
if (!plugin.validateEconomyCheck(editor, Settings.EconomyAction.MAKE_HOME_PUBLIC)) {
return;
}
});
}
// Execute the update
plugin.getSavedPositionManager().updateHomePrivacy(home, newIsPublic).join();
if (home.owner.uuid.equals(editor.uuid)) {
editor.sendMessage(plugin.getLocales().getLocale(
"edit_home_privacy_" + privacyKeyedString + "_success",
home.meta.name).orElse(new MineDown("")));
} else {
editor.sendMessage(plugin.getLocales().getLocale(
"edit_home_privacy_" + privacyKeyedString + "_success_other",
home.owner.username, home.meta.name).orElse(new MineDown("")));
}
// Perform necessary economy transaction
plugin.performEconomyTransaction(editor, Settings.EconomyAction.MAKE_HOME_PUBLIC);
// Show the menu if the menu flag is set
if (showMenuFlag.get()) {
getHomeEditorWindow(home, false, otherOwner,
!otherOwner || editor.hasPermission(Permission.COMMAND_HOME_OTHER.node))
.forEach(editor::sendMessage);
}
}
default -> plugin.getLocales().getLocale("error_invalid_syntax",
"/edithome <name> [" + String.join("|", EDIT_HOME_COMPLETIONS) + "] [args]")
@@ -20,7 +20,7 @@ public class EditWarpCommand extends CommandBase implements TabCompletable, Cons
private final String[] EDIT_WARP_COMPLETIONS = {"rename", "description", "relocate"};
public EditWarpCommand(@NotNull HuskHomes implementor) {
protected EditWarpCommand(@NotNull HuskHomes implementor) {
super("editwarp", Permission.COMMAND_EDIT_WARP, implementor);
}
@@ -12,7 +12,7 @@ import java.util.stream.Collectors;
public class HomeCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
public HomeCommand(@NotNull HuskHomes implementor) {
protected HomeCommand(@NotNull HuskHomes implementor) {
super("home", Permission.COMMAND_HOME, implementor);
}
@@ -7,7 +7,7 @@ import org.jetbrains.annotations.NotNull;
public class HomeListCommand extends CommandBase implements ConsoleExecutable {
public HomeListCommand(@NotNull HuskHomes implementor) {
protected HomeListCommand(@NotNull HuskHomes implementor) {
super("homelist", Permission.COMMAND_HOME, implementor, "homes");
}
@@ -15,7 +15,7 @@ import java.util.stream.Collectors;
public class PublicHomeCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
public PublicHomeCommand(@NotNull HuskHomes implementor) {
protected PublicHomeCommand(@NotNull HuskHomes implementor) {
super("publichome", Permission.COMMAND_PUBLIC_HOME, implementor, "phome");
}
@@ -7,7 +7,7 @@ import org.jetbrains.annotations.NotNull;
public class PublicHomeListCommand extends CommandBase implements ConsoleExecutable {
public PublicHomeListCommand(@NotNull HuskHomes implementor) {
protected PublicHomeListCommand(@NotNull HuskHomes implementor) {
super("publichomelist", Permission.COMMAND_HOME, implementor, "phomelist", "phomes");
}
@@ -0,0 +1,54 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
public class RtpCommand extends CommandBase implements ConsoleExecutable {
protected RtpCommand(@NotNull HuskHomes implementor) {
super("rtp", Permission.COMMAND_RTP, implementor);
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
if (args.length != 0) {
plugin.getLocales().getLocale("error_invalid_syntax", "/rtp")
.ifPresent(onlineUser::sendMessage);
return;
}
final Position userPosition = onlineUser.getPosition().join();
if (plugin.getSettings().rtpRestrictedWorlds.stream()
.anyMatch(worldName -> worldName.equals(userPosition.world.name))) {
plugin.getLocales().getLocale("error_rtp_restricted_world")
.ifPresent(onlineUser::sendMessage);
return;
}
if (!plugin.validateEconomyCheck(onlineUser, Settings.EconomyAction.RANDOM_TELEPORT)) {
return;
}
plugin.getRtpEngine().getRandomPosition(userPosition)
.thenAccept(position -> {
if (position.isEmpty()) {
plugin.getLocales().getLocale("error_rtp_randomization_timeout")
.ifPresent(onlineUser::sendMessage);
return;
}
plugin.getTeleportManager().timedTeleport(onlineUser, position.get())
.thenAccept(result -> {
plugin.getTeleportManager().finishTeleport(onlineUser, result);
plugin.performEconomyTransaction(onlineUser, Settings.EconomyAction.RANDOM_TELEPORT);
});
});
}
@Override
public void onConsoleExecute(@NotNull String[] args) {
//todo
}
}
@@ -2,51 +2,109 @@ package net.william278.huskhomes.command;
import de.themoep.minedown.MineDown;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.User;
import net.william278.huskhomes.player.UserData;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.PositionMeta;
import net.william278.huskhomes.position.SavedPositionManager;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
public class SetHomeCommand extends CommandBase {
public SetHomeCommand(@NotNull HuskHomes implementor) {
protected SetHomeCommand(@NotNull HuskHomes implementor) {
super("sethome", Permission.COMMAND_SET_HOME, implementor);
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
switch (args.length) {
case 0 -> plugin.getDatabase().getHomes(onlineUser).thenAccept(homes -> {
if (homes.isEmpty()) {
setHome(onlineUser, onlineUser, "home");
} else {
plugin.getLocales().getLocale("error_invalid_syntax", "/sethome <name>")
.ifPresent(onlineUser::sendMessage);
plugin.getDatabase().getHomes(onlineUser).thenAccept(homes -> {
switch (args.length) {
case 0 -> {
if (homes.isEmpty()) {
setHome(onlineUser, "home", homes);
} else {
plugin.getLocales().getLocale("error_invalid_syntax", "/sethome <name>")
.ifPresent(onlineUser::sendMessage);
}
}
});
case 1 -> setHome(onlineUser, onlineUser, args[0]);
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/sethome <name>")
.ifPresent(onlineUser::sendMessage);
}
case 1 -> setHome(onlineUser, args[0], homes);
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/sethome <name>")
.ifPresent(onlineUser::sendMessage);
}
});
}
private void setHome(@NotNull OnlineUser onlineUser, @NotNull User user, @NotNull String homeName) {
onlineUser.getPosition().thenAccept(position -> plugin.getSavedPositionManager().setHome(new PositionMeta(homeName,
plugin.getLocales().getRawLocale("home_default_description", user.username).orElse("")),
user, position).thenAccept(setResult ->
(switch (setResult.resultType()) {
case SUCCESS -> {
assert setResult.savedPosition().isPresent();
yield plugin.getLocales().getLocale("set_home_success", setResult.savedPosition().get().meta.name);
}
case FAILED_DUPLICATE -> plugin.getLocales().getLocale("error_home_name_taken");
case FAILED_NAME_LENGTH -> plugin.getLocales().getLocale("error_home_name_length");
case FAILED_NAME_CHARACTERS -> plugin.getLocales().getLocale("error_home_name_characters");
default -> Optional.of(new MineDown(""));
}).ifPresent(onlineUser::sendMessage)));
/**
* Attempts to set a home by given name for the {@link OnlineUser}.
* <p>
* A number of validation checks will take place before the home is set. If these checks fail, the home won't be set.
* <ul>
* <li>The user's currentHomes must not exceed the permissive maximum home limit</li>
* <li>If economy features are on and the user does not have enough home slots, they must have sufficient funds to buy another</li>
* <li>The home name must not already exist</li>
* <li>The home name must meet the length and character criteria</li>
* </ul>
*
* @param onlineUser The {@link OnlineUser} to set the home for
* @param homeName The name of the home to set
* @param currentHomes The current homes of the {@link OnlineUser}
*/
private void setHome(@NotNull OnlineUser onlineUser, @NotNull String homeName, @NotNull List<Home> currentHomes) {
// Check against maximum homes
final int maxHomes = onlineUser.getMaxHomes(plugin.getSettings().maxHomes, plugin.getSettings().stackPermissionLimits);
if (currentHomes.size() >= maxHomes) {
plugin.getLocales().getLocale("error_set_home_maximum_homes", Integer.toString(maxHomes))
.ifPresent(onlineUser::sendMessage);
return;
}
// Check against economy if needed
boolean newSlotNeeded = false;
UserData userDataToUpdate = null;
if (plugin.getSettings().economy) {
final int freeHomes = onlineUser.getFreeHomes(plugin.getSettings().freeHomeSlots, plugin.getSettings().stackPermissionLimits);
final Optional<UserData> fetchedData = plugin.getDatabase().getUserData(onlineUser.uuid).join();
if (fetchedData.isPresent()) {
newSlotNeeded = (currentHomes.size() + 1) > (freeHomes + fetchedData.get().homeSlots());
}
// If a new slot is needed, validate the user has enough funds to purchase one
if (newSlotNeeded) {
if (!plugin.validateEconomyCheck(onlineUser, Settings.EconomyAction.ADDITIONAL_HOME_SLOT)) {
return;
}
userDataToUpdate = new UserData(onlineUser, (currentHomes.size() + 1) - freeHomes,
fetchedData.get().ignoringTeleports(), fetchedData.get().rtpCooldown());
}
}
// Set the home in the saved position manager
final SavedPositionManager.SaveResult setResult = plugin.getSavedPositionManager()
.setHome(new PositionMeta(homeName, plugin.getLocales().getRawLocale("home_default_description",
onlineUser.username).orElse("")), onlineUser, onlineUser.getPosition().join()).join();
// Display feedback of the result of the set operation
(switch (setResult.resultType()) {
case SUCCESS -> {
assert setResult.savedPosition().isPresent();
// If the user needed to buy a new slot, perform the transaction and update their data
if (newSlotNeeded) {
plugin.performEconomyTransaction(onlineUser, Settings.EconomyAction.ADDITIONAL_HOME_SLOT);
plugin.getDatabase().updateUserData(userDataToUpdate).join();
}
yield plugin.getLocales().getLocale("set_home_success", setResult.savedPosition().get().meta.name);
}
case FAILED_DUPLICATE -> plugin.getLocales().getLocale("error_home_name_taken");
case FAILED_NAME_LENGTH -> plugin.getLocales().getLocale("error_home_name_length");
case FAILED_NAME_CHARACTERS -> plugin.getLocales().getLocale("error_home_name_characters");
default -> Optional.of(new MineDown(""));
}).ifPresent(onlineUser::sendMessage);
}
}
@@ -11,7 +11,7 @@ import java.util.Optional;
public class SetWarpCommand extends CommandBase {
public SetWarpCommand(@NotNull HuskHomes implementor) {
protected SetWarpCommand(@NotNull HuskHomes implementor) {
super("setwarp", Permission.COMMAND_SET_WARP, implementor);
}
@@ -32,7 +32,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
return;
}
// If the first argument is a player
// If the first argument is a player target
if (args.length == 1) {
targetPosition = new TeleportCommandTarget(args[0]);
} else {
@@ -10,7 +10,7 @@ import java.util.stream.Collectors;
public class WarpCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
public WarpCommand(@NotNull HuskHomes implementor) {
protected WarpCommand(@NotNull HuskHomes implementor) {
super("warp", Permission.COMMAND_WARP, implementor);
}
@@ -7,7 +7,7 @@ import org.jetbrains.annotations.NotNull;
public class WarpListCommand extends CommandBase implements ConsoleExecutable {
public WarpListCommand(@NotNull HuskHomes implementor) {
protected WarpListCommand(@NotNull HuskHomes implementor) {
super("warplist", Permission.COMMAND_WARP, implementor, "warps");
}