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

Switch to island homes from player homes. #1689

Merged
merged 7 commits into from
Mar 1, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package world.bentobox.bentobox.api.commands.admin.conversations;

import org.bukkit.Bukkit;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;
import org.eclipse.jdt.annotation.NonNull;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;

/**
* Renames a home
* @author tastybento
*
*/
public class NamePrompt extends StringPrompt {

private @NonNull final Island island;
private @NonNull final User user;
private final String oldName;
private final BentoBox plugin;

public NamePrompt(BentoBox plugin, @NonNull Island island, User user, String oldName) {
this.plugin = plugin;
this.island = island;
this.user = user;
this.oldName = oldName;
}

@Override
public String getPromptText(ConversationContext context) {
return user.getTranslation("commands.island.renamehome.enter-new-name");
}

@Override
public Prompt acceptInput(ConversationContext context, String input) {
if (island.renameHome(oldName, input)) {
plugin.getIslands().save(island);
Bukkit.getScheduler().runTask(plugin, () -> user.sendMessage("general.success"));
} else {
Bukkit.getScheduler().runTask(plugin, () -> user.sendMessage("commands.island.renamehome.already-exists"));
}
return Prompt.END_OF_CONVERSATION;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public void setup() {

// Settings related commands
new IslandSettingsCommand(this);
new IslandSethomeCommand(this);
new IslandSetnameCommand(this);
new IslandResetnameCommand(this);
new IslandLanguageCommand(this);
Expand All @@ -74,6 +73,11 @@ public void setup() {

// Team commands
new IslandTeamCommand(this);

// Home commands
new IslandSethomeCommand(this);
new IslandDeletehomeCommand(this);
new IslandRenamehomeCommand(this);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package world.bentobox.bentobox.api.commands.island;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.eclipse.jdt.annotation.Nullable;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;

/**
* Deletes a home
* @author tastybento
*
*/
public class IslandDeletehomeCommand extends ConfirmableCommand {

private @Nullable Island island;

public IslandDeletehomeCommand(CompositeCommand islandCommand) {
super(islandCommand, "deletehome");
}

@Override
public void setup() {
setPermission("island.deletehome");
setOnlyPlayer(true);
setParametersHelp("commands.island.deletehome.parameters");
setDescription("commands.island.deletehome.description");
}

@Override
public boolean canExecute(User user, String label, List<String> args) {
if (args.isEmpty()) {
this.showHelp(this, user);
return false;
}
island = getIslands().getIsland(getWorld(), user);
// Check island
if (island == null) {
user.sendMessage("general.errors.no-island");
return false;
}
// Check if the name is known
if (!getIslands().isHomeLocation(island, String.join(" ", args))) {
user.sendMessage("commands.island.go.unknown-home");
user.sendMessage("commands.island.sethome.homes-are");
island.getHomes().keySet().stream().filter(s -> !s.isEmpty()).forEach(s -> user.sendMessage("home-list-syntax", TextVariables.NAME, s));
return false;
}
return true;
}

@Override
public boolean execute(User user, String label, List<String> args) {
this.askConfirmation(user, () -> delete(island, user, String.join(" ", args)));
return true;
}


private void delete(Island island, User user, String name) {
island.removeHome(name);
user.sendMessage("general.success");
}

@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
Island island = getIslands().getIsland(getWorld(), user.getUniqueId());
if (island != null) {
return Optional.of(Util.tabLimit(new ArrayList<>(island.getHomes().keySet()), lastArg));
} else {
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package world.bentobox.bentobox.api.commands.island;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import java.util.Optional;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.DelayedTeleportCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util;

/**
* @author tastybento
Expand All @@ -24,8 +26,8 @@ public IslandGoCommand(CompositeCommand islandCommand) {
public void setup() {
setPermission("island.home");
setOnlyPlayer(true);
setParametersHelp("commands.island.go.parameters");
setDescription("commands.island.go.description");
new CustomIslandMultiHomeHelp(this);
}

@Override
Expand All @@ -47,21 +49,32 @@ public boolean canExecute(User user, String label, List<String> args) {
user.sendMessage(Flags.PREVENT_TELEPORT_WHEN_FALLING.getHintReference());
return false;
}
if (!args.isEmpty()) {
if (!getIslands().isHomeLocation(island, String.join(" ", args))) {
user.sendMessage("commands.island.go.unknown-home");
user.sendMessage("commands.island.sethome.homes-are");
island.getHomes().keySet().stream().filter(s -> !s.isEmpty()).forEach(s -> user.sendMessage("commands.island.sethome.home-list-syntax", TextVariables.NAME, s));
return false;
}
}
return true;
}

@Override
public boolean execute(User user, String label, List<String> args) {
if (!args.isEmpty() && NumberUtils.isDigits(args.get(0))) {
int homeValue = Integer.parseInt(args.get(0));
int maxHomes = user.getPermissionValue(getPermissionPrefix() + "island.maxhomes", getIWM().getMaxHomes(getWorld()));
if (homeValue > 1 && homeValue <= maxHomes) {
this.delayCommand(user, () -> getIslands().homeTeleportAsync(getWorld(), user.getPlayer(), homeValue));
return true;
}
}
this.delayCommand(user, () -> getIslands().homeTeleportAsync(getWorld(), user.getPlayer()));
this.delayCommand(user, () -> getIslands().homeTeleportAsync(getWorld(), user.getPlayer(), String.join(" ", args)));
return true;
}

@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
Island island = getIslands().getIsland(getWorld(), user.getUniqueId());
if (island != null) {
return Optional.of(Util.tabLimit(new ArrayList<>(island.getHomes().keySet()), lastArg));
} else {
return Optional.empty();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package world.bentobox.bentobox.api.commands.island;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.bukkit.conversations.ConversationFactory;
import org.eclipse.jdt.annotation.Nullable;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.commands.admin.conversations.NamePrompt;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;

/**
* Renames a home
* @author tastybento
*
*/
public class IslandRenamehomeCommand extends ConfirmableCommand {

private @Nullable Island island;

public IslandRenamehomeCommand(CompositeCommand islandCommand) {
super(islandCommand, "renamehome");
}

@Override
public void setup() {
setPermission("island.renamehome");
setOnlyPlayer(true);
setParametersHelp("commands.island.renamehome.parameters");
setDescription("commands.island.renamehome.description");
}

@Override
public boolean canExecute(User user, String label, List<String> args) {
if (args.isEmpty()) {
this.showHelp(this, user);
return false;
}
island = getIslands().getIsland(getWorld(), user);
// Check island
if (island == null) {
user.sendMessage("general.errors.no-island");
return false;
}
// Check if the name is known
if (!getIslands().isHomeLocation(island, String.join(" ", args))) {
user.sendMessage("commands.island.go.unknown-home");
user.sendMessage("commands.island.sethome.homes-are");
island.getHomes().keySet().stream().filter(s -> !s.isEmpty()).forEach(s -> user.sendMessage("commands.island.sethome.home-list-syntax", TextVariables.NAME, s));
this.showHelp(this, user);
return false;
}
return true;
}

@Override
public boolean execute(User user, String label, List<String> args) {
new ConversationFactory(BentoBox.getInstance())
.withModality(true)
.withLocalEcho(false)
.withTimeout(90)
.withFirstPrompt(new NamePrompt(getPlugin(), island, user, String.join(" ", args)))
.buildConversation(user.getPlayer()).begin();
return true;
}


@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
Island island = getIslands().getIsland(getWorld(), user.getUniqueId());
if (island != null) {
return Optional.of(Util.tabLimit(new ArrayList<>(island.getHomes().keySet()), lastArg));
} else {
return Optional.empty();
}
}
}
Loading