Skip to content

Commit

Permalink
Add an ability to change visitor spawn location.
Browse files Browse the repository at this point in the history
  • Loading branch information
BONNe committed Feb 20, 2021
1 parent 7d44bdf commit 12cb7da
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void setup()
this.setDescription(Constants.PLAYER_COMMANDS + "main.description");

new VisitConfigureCommand(this.getAddon(), this);
new VisitSetLocationCommand(this.getAddon(), this);

this.setOnlyPlayer(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//
// Created by BONNe
// Copyright - 2021
//

package world.bentobox.visit.commands.player;


import org.bukkit.World;
import java.util.List;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.visit.VisitAddon;
import world.bentobox.visit.panels.player.ConfigurePanel;
import world.bentobox.visit.utils.Constants;
import world.bentobox.visit.utils.Utils;


/**
* This class manages {@code /{player_command} visit setlocation} command call.
*/
public class VisitSetLocationCommand extends CompositeCommand
{
/**
* This is simple constructor for initializing /{player_command} visit configure command.
*
* @param addon Our Visit addon.
* @param parentCommand Parent Command where we hook our command into.
*/
public VisitSetLocationCommand(VisitAddon addon, CompositeCommand parentCommand)
{
super(addon,
parentCommand,
addon.getSettings().getPlayerSetLocationCommand().split(" ")[0],
addon.getSettings().getPlayerSetLocationCommand().split(" "));
}


/**
* Setups anything that is needed for this command. <br/><br/> It is recommended you do the following in this
* method:
* <ul>
* <li>Register any of the sub-commands of this command;</li>
* <li>Define the permission required to use this command using {@link
* CompositeCommand#setPermission(String)};</li>
* <li>Define whether this command can only be run by players or not using {@link
* CompositeCommand#setOnlyPlayer(boolean)};</li>
* </ul>
*/
@Override
public void setup()
{
this.setPermission("setlocation");
this.setParametersHelp(Constants.PLAYER_COMMANDS + "set-location.parameters");
this.setDescription(Constants.PLAYER_COMMANDS + "set-location.description");

this.setOnlyPlayer(true);
}


/**
* 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,
* List)}.
*
* @param user the {@link User} who is executing this command.
* @param label the label which has been used to execute this command. It can be {@link CompositeCommand#getLabel()}
* or an alias.
* @param args the command arguments.
* @return {@code true} if this command can be executed, {@code false} otherwise.
* @since 1.3.0
*/
@Override
public boolean canExecute(User user, String label, List<String> args)
{
Island island = this.getIslands().getIsland(this.getWorld(), user);

if (island == null)
{
// No island
user.sendMessage("general.errors.no-island");
return false;
}
else if (island.isAllowed(user, VisitAddon.VISIT_CONFIG_PERMISSION))
{
// No permission to edit.
return false;
}
else if (user.getLocation() == null || World.Environment.NORMAL.equals(user.getWorld().getEnvironment()))
{
// User must be in overworld.
Utils.sendMessage(user, user.getTranslation(Constants.ERRORS, "not-in-overworld"));
return false;
}
else if (!island.getProtectionBoundingBox().contains(user.getLocation().toVector()))
{
// User must be in protected area.
Utils.sendMessage(user, user.getTranslation(Constants.ERRORS, "not-in-protected-area"));
}
else if (!this.getAddon().getIslands().isSafeLocation(user.getLocation()))
{
// Location must be safe.
Utils.sendMessage(user, user.getTranslation(Constants.ERRORS, "not-safe-location"));
return false;
}

return true;
}


/**
* Defines what will be executed when this command is run.
*
* @param user the {@link User} who is executing this command.
* @param label the label which has been used to execute this command. It can be {@link CompositeCommand#getLabel()}
* or an alias.
* @param args the command arguments.
* @return {@code true} if the command executed successfully, {@code false} otherwise.
*/
@Override
public boolean execute(User user, String label, List<String> args)
{
Island island = this.getIslands().getIsland(this.getWorld(), user);

if (island != null)
{
// Utilize island spawn point location.
island.setSpawnPoint(World.Environment.NORMAL, user.getLocation());
}

return true;
}
}
30 changes: 30 additions & 0 deletions src/main/java/world/bentobox/visit/configs/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,28 @@ public void setDefaultConfigPermission(int defaultConfigPermission)
}


/**
* Gets player set location command.
*
* @return the player set location command
*/
public String getPlayerSetLocationCommand()
{
return playerSetLocationCommand;
}


/**
* Sets player set location command.
*
* @param playerSetLocationCommand the player set location command
*/
public void setPlayerSetLocationCommand(String playerSetLocationCommand)
{
this.playerSetLocationCommand = playerSetLocationCommand;
}


// ---------------------------------------------------------------------
// Section: Enums
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -559,6 +581,14 @@ public enum Filter
@ConfigEntry(path = "commands.player.configure", needsRestart = true)
private String playerConfigureCommand = "configure";

/**
* The Player setLocation command.
*/
@ConfigComment("Player setLocation sub-command that allows to change spawn location for visitors..")
@ConfigComment("This command label will be required to write after gamemode player command label, f.e. /[label] visit setLocation")
@ConfigEntry(path = "commands.player.set-location", needsRestart = true)
private String playerSetLocationCommand = "setLocation";

/**
* The Admin main command.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/addon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ permissions:
'[gamemode].visit.configure':
description: Access to '/[player_command] visit configure' command
default: true
'[gamemode].visit.setlocation':
description: Access to '/[player_command] visit setlocation' command
default: true
'visit.icon.STONE':
description: Allows to set a different icon for owner island.
default: false
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ commands:
# This command label will be required to write after gamemode player command label, f.e. /[label] visit configure
# /!\ In order to apply the changes made to this option, you must restart your server. Reloading BentoBox or the server won't work.
configure: configure

set-location: setLocation
admin:
# Admin main sub-command to access the addon.
# This command label will be required to write after gamemode admin command label, f.e. /[label] visit
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ visit:
configure:
description: 'opens GUI that allows to manage visiting settings.'
parameters: ''
set-location:
description: 'allows to change visitors spawn location.'
parameters: ''
# This section contains translations for items inside GUI interfaces.
gui:
# This section contains titles for all GUIs.
Expand Down Expand Up @@ -214,6 +217,9 @@ visit:
not-enough-credits: '&r You do not have enough credits to visit this island. Necessary [number] credits.'
cannot-withdraw-credits: '&r Cannot withdraw [number] from your account! Operation is cancelled.'
cannot-deposit-credits: '&r Cannot deposit [number] into island owner account! Operation is cancelled.'
not-in-overworld: '&r You must be in overworld to change visitor spawn location! Operation is cancelled.'
not-in-protected-area: '&r You must be in your island protected area! Operation is cancelled.'
not-safe-location: '&r Cannot set this location, it is not safe for teleportation! Operation is cancelled.'
conversations:
# List of strings that are valid for confirming input. (separated with ,)
confirm-string: "true, on, yes, confirm, y, valid, correct"
Expand Down

0 comments on commit 12cb7da

Please sign in to comment.