Skip to content

Commit

Permalink
Add teleporation processing methods in VisitAddonManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
BONNe committed Jun 17, 2020
1 parent c589d23 commit 816b329
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
85 changes: 81 additions & 4 deletions src/main/java/world/bentobox/visit/managers/VisitAddonManager.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package world.bentobox.visit.managers;


import org.bukkit.World;
import org.eclipse.jdt.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.*;

import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
import world.bentobox.visit.VisitAddon;
import world.bentobox.visit.database.object.IslandVisitSettings;

Expand Down Expand Up @@ -300,4 +299,82 @@ public boolean withdrawCredits(User user, double credits)
return true;
}
}


// ---------------------------------------------------------------------
// Section: Teleportation methods
// ---------------------------------------------------------------------


/**
* This method checks if any island member is online or offline visiting option is
* enabled.
* @param island Island that must be checked.
* @param settings Island Visit Settings object.
* @return {@code true} if offline visiting is enabled or any member is online,
* {@code false} otherwise.
*/
public boolean canVisitOffline(Island island, IslandVisitSettings settings)
{
// Check if settings allow offline visiting or any island member is online.
return settings.isOfflineVisit() ||
island.getMemberSet().stream().anyMatch(uuid ->
User.getInstance(uuid) != null && User.getInstance(uuid).isOnline());
}


/**
* This method process user teleportation to the given island.
* @param user Targeted user who need to be teleported.
* @param island Island where user need to be teleported.
*/
public void processTeleportation(User user,
Island island)
{
this.processTeleportation(user, island, this.getIslandVisitSettings(island));
}


/**
* This method process user teleportation to the given island.
* @param user Targeted user who need to be teleported.
* @param island Island where user need to be teleported.
* @param settings IslandVisitSettings object.
*/
public void processTeleportation(User user,
Island island,
IslandVisitSettings settings)
{
double payment = settings.getPayment() + this.addon.getSettings().getTaxAmount();

if (payment > 0 && !this.hasCredits(user, payment))
{
user.sendMessage("visit.error.not-enough-credits",
"[credits]", String.valueOf(payment));
}
else if (!this.canVisitOffline(island, settings))
{
user.sendMessage("visit.error.noone-is-online");
}
else if (payment > 0 && !this.withdrawCredits(user, payment))
{
// error on withdrawing credits. Cancelling
user.sendMessage("visit.error.cannot-withdraw-credits",
"[credits]", String.valueOf(payment));
}
else if (settings.getPayment() > 0 && !this.depositCredits(User.getInstance(island.getOwner()), settings.getPayment()))
{
// error on depositing credits. Cancelling
this.depositCredits(user, settings.getPayment() + this.addon.getSettings().getTaxAmount());

user.sendMessage("visit.error.cannot-deposit-credits",
"[credits]", String.valueOf(settings.getPayment()));
}
else
{
// Teleport player async to island spawn point.
Util.teleportAsync(user.getPlayer(),
Objects.requireNonNull(island.getSpawnPoint(World.Environment.NORMAL)));
}
}
}
9 changes: 9 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ visit:
name: 'Next Page'
previous:
name: 'Previous Page'
island:
name: '[name]'
noone-online: 'None of island members are online. You cannot visit the island.'
cost: 'It costs [cost] to visit this island.'

messages:
visit:
messages:
load-skipping: 'Skipping to load [value] visit settings object'
load-overwriting: 'Overwriting visit settings object with ID [value]'
load-add: 'Adding visit settings object with ID [value]'
error:
cannot-withdraw-credits: '&r Cannot withdraw [credits] from your account! Operation is cancelled.'
cannot-deposit-credits: '&r Cannot deposit [credits] into island owner account! Operation is cancelled.'
not-enough-credits: '$r You do not have enough credits to visit this island. Necessary [credits] credits.'
noone-is-online: '$r Noone from this island is online.'

protection:
flags:
Expand Down

0 comments on commit 816b329

Please sign in to comment.