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

Add Confirmation GUI for Island Regen #878

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
Expand Up @@ -10,7 +10,9 @@
import com.iridium.iridiumteams.IridiumTeams;
import com.iridium.iridiumteams.bank.BankItem;
import com.iridium.iridiumteams.commands.Command;
import com.iridium.iridiumteams.commands.ConfirmableCommand;
import com.iridium.iridiumteams.database.TeamBank;
import com.iridium.iridiumteams.gui.ConfirmationGUI;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand All @@ -20,14 +22,14 @@
import java.util.*;
import java.util.stream.Collectors;

public class RegenCommand extends Command<Island, User> {
public class RegenCommand extends ConfirmableCommand<Island, User> {

public RegenCommand() {
super(Collections.singletonList("regen"), "Regenerate your Island", "%prefix% &7/is regen <schematic>", "", 300);
super(Collections.singletonList("regen"), "Regenerate your Island", "%prefix% &7/is regen <schematic>", "", 300, true);
}

@Override
public boolean execute(User user, Island island, String[] args, IridiumTeams<Island, User> iridiumTeams) {
public boolean isCommandValid(User user, Island island, String[] args, IridiumTeams<Island, User> iridiumTeams) {
Player player = user.getPlayer();
if (args.length == 0 && IridiumSkyblock.getInstance().getSchematics().schematics.entrySet().size() > 1) {
if (!IridiumSkyblock.getInstance().getIslandManager().getTeamPermission(island, IridiumSkyblock.getInstance().getUserManager().getUser(player), "regen")) {
Expand All @@ -53,24 +55,46 @@ public boolean execute(User user, Island island, String[] args, IridiumTeams<Isl

Schematics.SchematicConfig schematicConfig = IridiumSkyblock.getInstance().getSchematics().schematics.get(schematic.get());

if(island.getLevel() < schematicConfig.minLevel) {
if (island.getLevel() < schematicConfig.minLevel) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().notHighEnoughLevel
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)
.replace("%level%", String.valueOf(schematicConfig.minLevel))));
return false;
}

if (schematicConfig.regenCost.money != 0 || !schematicConfig.regenCost.bankItems.isEmpty()) {
if (!IridiumSkyblock.getInstance().getSchematicManager().buy(player, schematicConfig)) {
return false;
}
return IridiumSkyblock.getInstance().getSchematicManager().buy(player, schematicConfig);
}

return true;
}

@Override
public boolean executeAfterConfirmation(User user, Island island, String[] args, IridiumTeams<Island, User> iridiumTeams) {
Player player = user.getPlayer();
Schematics.SchematicConfig schematicConfig = IridiumSkyblock.getInstance().getSchematics().schematics.get(args[0]);

regenerateIsland(player, island, schematicConfig, iridiumTeams);
return true;
}

private void regenerateIsland(Player player, Island island, Schematics.SchematicConfig schematicConfig, IridiumTeams<Island, User> iridiumTeams) {
List<Placeholder> bankPlaceholders = IridiumSkyblock.getInstance().getBankItemList().stream()
.map(BankItem::getName)
.map(name -> new Placeholder(name + "_cost", formatPrice(getBankBalance(player, name))))
.collect(Collectors.toList());

sendConfirmationMessage(player, island, schematicConfig, bankPlaceholders);

player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().regeneratingIsland
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)
));

IridiumSkyblock.getInstance().getIslandManager().clearTeamInventory(island);
regenerateAndTeleport(player, island, schematicConfig);
}

private void sendConfirmationMessage(Player player, Island island, Schematics.SchematicConfig schematicConfig, List<Placeholder> bankPlaceholders) {
IridiumSkyblock.getInstance().getIslandManager().getTeamMembers(island).stream()
.map(User::getPlayer)
.filter(Objects::nonNull)
Expand All @@ -81,23 +105,18 @@ public boolean execute(User user, Island island, String[] args, IridiumTeams<Isl
.replace("%vault_cost%", formatPrice(schematicConfig.regenCost.money)),
bankPlaceholders)
)));
}

player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().regeneratingIsland
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)
));

IridiumSkyblock.getInstance().getIslandManager().clearTeamInventory(island);

IridiumSkyblock.getInstance().getIslandManager().generateIsland(island, schematicConfig).thenRun(() -> Bukkit.getScheduler().runTask(IridiumSkyblock.getInstance(), () -> {

if (IridiumSkyblock.getInstance().getTeamManager().teleport(player, island.getHome(), island)) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().teleportingHome
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)
));
}
}));

return true;
private void regenerateAndTeleport(Player player, Island island, Schematics.SchematicConfig schematicConfig) {
IridiumSkyblock.getInstance().getIslandManager().generateIsland(island, schematicConfig).thenRun(() ->
Bukkit.getScheduler().runTask(IridiumSkyblock.getInstance(), () -> {
if (IridiumSkyblock.getInstance().getTeamManager().teleport(player, island.getHome(), island)) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().teleportingHome
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)
));
}
})
);
}

private double getBankBalance(Player player, String bankItem) {
Expand Down