Skip to content

Commit

Permalink
Implement command confirmation requirement if visiting has set a paym…
Browse files Browse the repository at this point in the history
…ent cost.

Implement settings option to enable/disable confirmation.
Implement message about payment before visiting island for command.
  • Loading branch information
BONNe committed Jul 6, 2021
1 parent 604ebab commit 3719287
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
package world.bentobox.visit.commands.player;


import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask;
import java.util.*;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.DelayedTeleportCommand;
Expand Down Expand Up @@ -66,6 +66,52 @@ public void setup()
}


/**
* Ask confirmation for teleportation.
* @param user User who need to confirm.
* @param message Message that will be set to user.
* @param confirmed Confirm task.
*/
public void askConfirmation(User user, String message, Runnable confirmed)
{
// Check for pending confirmations
if (toBeConfirmed.containsKey(user))
{
if (toBeConfirmed.get(user).getTopLabel().equals(getTopLabel()) &&
toBeConfirmed.get(user).getLabel().equalsIgnoreCase(getLabel()))
{
toBeConfirmed.get(user).getTask().cancel();
Bukkit.getScheduler().runTask(getPlugin(), toBeConfirmed.get(user).getRunnable());
toBeConfirmed.remove(user);
return;
}
else
{
// Player has another outstanding confirmation request that will now be cancelled
user.sendMessage("commands.confirmation.previous-request-cancelled");
}
}
// Send user the context message if it is not empty
if (!message.trim().isEmpty())
{
user.sendRawMessage(message);
}
// Tell user that they need to confirm
user.sendMessage("commands.confirmation.confirm",
"[seconds]",
String.valueOf(getSettings().getConfirmationTime()));
// Set up a cancellation task
BukkitTask task = Bukkit.getScheduler().runTaskLater(getPlugin(), () ->
{
user.sendMessage("commands.confirmation.request-cancelled");
toBeConfirmed.remove(user);
}, getPlugin().getSettings().getConfirmationTime() * 20L);

// Add to the global confirmation map
toBeConfirmed.put(user, new Confirmer(getTopLabel(), getLabel(), confirmed, task));
}


/**
* Returns whether the command can be executed by this user or not. It is recommended to send messages to let this
* user know why they could not execute the command. Note that this is run previous to {@link #execute(User, String,
Expand Down Expand Up @@ -137,9 +183,45 @@ public boolean execute(User user, String label, List<String> args)
}
else if (args.size() == 1)
{
// Process teleportation
this.delayCommand(user, () ->
this.<VisitAddon>getAddon().getAddonManager().processTeleportation(user, this.island));
double tax;
double earnings;

if (this.<VisitAddon>getAddon().getSettings().isDisableEconomy())
{
tax = 0;
earnings = 0;
}
else
{
// check tax and island earnings that if economy is enabled.
tax = this.<VisitAddon>getAddon().getSettings().getTaxAmount();
earnings = this.<VisitAddon>getAddon().getAddonManager().getIslandEarnings(this.island);
}

String prefix = user.getTranslation(Constants.CONVERSATIONS + "prefix");
String message = prefix +
user.getTranslation(Constants.CONVERSATIONS + "visit-payment",
Constants.PARAMETER_PAYMENT, String.valueOf(tax + earnings),
Constants.PARAMETER_TAX, String.valueOf(tax),
Constants.PARAMETER_ISLAND, String.valueOf(this.island.getName()),
Constants.PARAMETER_OWNER, this.getPlayers().getName(this.island.getOwner()),
Constants.PARAMETER_RECEIVER, String.valueOf(earnings));

if (this.<VisitAddon>getAddon().getSettings().isPaymentConfirmation() && (tax + earnings) > 0)
{
// If there is associated cost, then ask confirmation from user.
this.askConfirmation(user,
message,
() -> this.delayCommand(user, () ->
this.<VisitAddon>getAddon().getAddonManager().processTeleportation(user, this.island)));
}
else
{
// Execute teleportation without confirmation.
this.delayCommand(user,
(tax + earnings > 0) ? message : "",
() -> this.<VisitAddon>getAddon().getAddonManager().processTeleportation(user, this.island));
}
}
else
{
Expand Down Expand Up @@ -167,8 +249,70 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
}


// ---------------------------------------------------------------------
// Section: Classes
// ---------------------------------------------------------------------


/**
* This is clone from BentoBox ConfirmableCommand class.
*/
private static class Confirmer {
private final String topLabel;
private final String label;
private final Runnable runnable;
private final BukkitTask task;

/**
* @param label - command label
* @param runnable - runnable to run when confirmed
* @param task - task ID to cancel when confirmed
*/
Confirmer(String topLabel, String label, Runnable runnable, BukkitTask task) {
this.topLabel = topLabel;
this.label = label;
this.runnable = runnable;
this.task = task;
}
/**
* @return the topLabel
*/
public String getTopLabel() {
return topLabel;
}
/**
* @return the label
*/
public String getLabel() {
return label;
}
/**
* @return the runnable
*/
public Runnable getRunnable() {
return runnable;
}
/**
* @return the task
*/
public BukkitTask getTask() {
return task;
}
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------


/**
* Island instance to which player will be teleported.
*/
private Island island;

/**
* Map that contains which users are in confirmation process.
*/
private static final Map<User, Confirmer> toBeConfirmed = new HashMap<>();
}
29 changes: 29 additions & 0 deletions src/main/java/world/bentobox/visit/configs/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,28 @@ public void setMaxAmount(double maxAmount)
}


/**
* Is payment confirmation boolean.
*
* @return the boolean
*/
public boolean isPaymentConfirmation()
{
return paymentConfirmation;
}


/**
* Sets payment confirmation.
*
* @param paymentConfirmation the payment confirmation
*/
public void setPaymentConfirmation(boolean paymentConfirmation)
{
this.paymentConfirmation = paymentConfirmation;
}


// ---------------------------------------------------------------------
// Section: Enums
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -626,6 +648,13 @@ public enum Filter
@ConfigEntry(path = "gui.border-block-name")
private String borderBlockName = " ";

/**
* The Player main command.
*/
@ConfigComment("Option that allows to enable asking for confirmation before teleporting via command if there are associated cost for it.")
@ConfigEntry(path = "commands.player.ask-payment-confirmation")
private boolean paymentConfirmation = true;

/**
* The Player main command.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ private PanelItem createButton(Button button)

break;
}
case ENABLE_CONFIRMATION:
// TODO: need Implementing
return PanelItem.empty();
default:
return PanelItem.empty();
}
Expand Down Expand Up @@ -572,6 +575,10 @@ private enum Button
* Allows to disable economy part of the addon.
*/
ENABLE_ECONOMY,
/**
* Allows to enable confirmation requirement before teleportation if cost is set.
*/
ENABLE_CONFIRMATION,
}


Expand Down
15 changes: 15 additions & 0 deletions src/main/java/world/bentobox/visit/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ public class Constants
*/
public static final String PARAMETER_NO_VISIT = "[no-visit]";

/**
* Reference string to tax parameter in translations.
*/
public static final String PARAMETER_TAX = "[tax]";

/**
* Reference string to earn parameter in translations.
*/
public static final String PARAMETER_RECEIVER = "[earn]";

/**
* Reference string to island parameter in translations.
*/
public static final String PARAMETER_ISLAND = "[island]";

// ---------------------------------------------------------------------
// Section: Metadata
// ---------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ gui:
border-block-name: ' '
commands:
player:
# Option that allows to enable asking for confirmation before teleporting via command if there are associated cost for it.
ask-payment-confirmation: true
# Player main sub-command to access the addon.
# This command label will be required to write after gamemode player command label, f.e. /[label] visit
# /!\ In order to apply the changes made to this option, you must restart your server. Reloading BentoBox or the server won't work.
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ visit:
user-data-removed: "&a Success, all user data for [gamemode] was removed!"
# Message that appears when user updates spawn point.
spawn-point-updated: "&a Success, spawn point is changed!"
# Message that appears when user process teleportation to the island with the associated cost.
# [tax], [earn], [payment], [island] and [owner] are available variables.
visit-payment: "&e Visiting [owner] island will cost &l&6 $[payment]."
# Protection flags that are used in current addon.
protection:
flags:
Expand Down

0 comments on commit 3719287

Please sign in to comment.