Skip to content

Commit

Permalink
Reformat & Refactor commands, Shorten tab-completion code (#390)
Browse files Browse the repository at this point in the history
* Reformat & Refactor commands

* Remove empty line

* Shorten command tab-completion code

* Optimize imports

* Remove unnecessary streams

* Remove useless code
  • Loading branch information
dlsf authored Sep 6, 2021
1 parent cf224b8 commit 124614e
Show file tree
Hide file tree
Showing 63 changed files with 480 additions and 404 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import com.iridium.iridiumcore.utils.StringUtils;
import com.iridium.iridiumskyblock.IridiumSkyblock;
import java.time.Duration;
import org.bukkit.command.CommandSender;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.bukkit.command.CommandSender;

/**
* Command which display plugin information to the user.
Expand Down Expand Up @@ -57,7 +56,7 @@ public boolean execute(CommandSender sender, String[] args) {
@Override
public List<String> onTabComplete(CommandSender commandSender, org.bukkit.command.Command command, String label, String[] args) {
// We currently don't want to tab-completion here
// Return a new List so it isn't a list of online players
// Return a new List, so it isn't a list of online players
return Collections.emptyList();
}

Expand Down
17 changes: 7 additions & 10 deletions src/main/java/com/iridium/iridiumskyblock/commands/BanCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
import com.iridium.iridiumskyblock.database.User;
import com.iridium.iridiumskyblock.gui.BansGUI;
import com.iridium.iridiumskyblock.utils.PlayerUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
* Command which bans a visitor from your Island.
Expand Down Expand Up @@ -44,10 +42,10 @@ public boolean execute(CommandSender sender, String[] args) {
sender.sendMessage(StringUtils.color(syntax.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
}

Player player = (Player) sender;
User user = IridiumSkyblock.getInstance().getUserManager().getUser(player);
Optional<Island> island = user.getIsland();

if (!island.isPresent()) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noIsland.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
Expand Down Expand Up @@ -84,11 +82,13 @@ public boolean execute(CommandSender sender, String[] args) {
sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().alreadyBanned.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
}

IridiumSkyblock.getInstance().getDatabaseManager().getIslandBanTableManager().addEntry(new IslandBan(island.get(), user, targetUser));
if (island.get().isInIsland(targetPlayer.getLocation())) {
PlayerUtils.teleportSpawn(targetPlayer);
targetPlayer.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().youHaveBeenBanned.replace("%player%", user.getName()).replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
}

sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().playerBanned.replace("%player%", targetUser.getName()).replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return true;
}
Expand All @@ -104,10 +104,7 @@ public boolean execute(CommandSender sender, String[] args) {
*/
@Override
public List<String> onTabComplete(CommandSender commandSender, org.bukkit.command.Command command, String label, String[] args) {
return Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.filter(s -> s.toLowerCase().contains(args[1].toLowerCase()))
.collect(Collectors.toList());
return PlayerUtils.getOnlinePlayerNames();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import com.iridium.iridiumskyblock.database.Island;
import com.iridium.iridiumskyblock.database.User;
import com.iridium.iridiumskyblock.gui.BankGUI;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
* Command which opens the Island bank GUI.
Expand Down Expand Up @@ -50,11 +49,11 @@ public boolean execute(CommandSender sender, String[] args) {
Player player = (Player) sender;
User user = IridiumSkyblock.getInstance().getUserManager().getUser(player);
Optional<Island> island = user.getIsland();

if (!island.isPresent()) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noIsland.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
}

player.openInventory(new BankGUI(island.get()).getInventory());
return true;
}
Expand All @@ -71,7 +70,7 @@ public boolean execute(CommandSender sender, String[] args) {
@Override
public List<String> onTabComplete(CommandSender commandSender, org.bukkit.command.Command cmd, String label, String[] args) {
// We currently don't want to tab-completion here
// Return a new List so it isn't a list of online players
// Return a new List, so it isn't a list of online players
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
import com.iridium.iridiumskyblock.database.User;
import com.iridium.iridiumskyblock.gui.BiomeGUI;
import java.time.Duration;
import org.apache.commons.lang.WordUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.commons.lang.WordUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class BiomeCommand extends Command {

Expand All @@ -30,15 +29,14 @@ public boolean execute(CommandSender sender, String[] args) {
final Player player = (Player) sender;

final User user = this.plugin.getUserManager().getUser(player);
final Optional<Island> optionalIsland = user.getIsland();

if (!optionalIsland.isPresent()) {
final Optional<Island> islandOptional = user.getIsland();
if (!islandOptional.isPresent()) {
player.sendMessage(StringUtils.color(this.plugin.getMessages().noIsland.replace("%prefix%", plugin.getConfiguration().prefix)));
return false;
}

if (args.length != 2) {
player.openInventory(new BiomeGUI(1, optionalIsland.get(), player.getWorld().getEnvironment(), getCooldownProvider()).getInventory());
player.openInventory(new BiomeGUI(1, islandOptional.get(), player.getWorld().getEnvironment(), getCooldownProvider()).getInventory());
// The BiomeGUI handles the cooldown
return false;
}
Expand All @@ -50,8 +48,7 @@ public boolean execute(CommandSender sender, String[] args) {
return false;
}

IridiumSkyblock.getInstance().getIslandManager().setIslandBiome(optionalIsland.get(), biomeOptional.get());

IridiumSkyblock.getInstance().getIslandManager().setIslandBiome(islandOptional.get(), biomeOptional.get());
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().changedBiome
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)
.replace("%biome%", WordUtils.capitalizeFully(biomeOptional.get().name().toLowerCase().replace("_", " ")))));
Expand All @@ -60,17 +57,16 @@ public boolean execute(CommandSender sender, String[] args) {

@Override
public List<String> onTabComplete(CommandSender commandSender, org.bukkit.command.Command command, String label, String[] args) {
if (commandSender instanceof Player) {
Player player = (Player) commandSender;
return Arrays.stream(XBiome.VALUES)
.filter(biome -> biome.getEnvironment() == player.getWorld().getEnvironment())
.filter(biome -> biome.getBiome() != null)
.map(Enum::toString)
.filter(s -> s.toUpperCase().contains(args[1].toUpperCase()))
.collect(Collectors.toList());
} else {
if (!(commandSender instanceof Player)) {
return Collections.emptyList();
}

Player player = (Player) commandSender;
return Arrays.stream(XBiome.VALUES)
.filter(biome -> biome.getEnvironment() == player.getWorld().getEnvironment())
.filter(biome -> biome.getBiome() != null)
.map(XBiome::name)
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.iridium.iridiumskyblock.IridiumSkyblock;
import com.iridium.iridiumskyblock.gui.BlockValueGUI;
import com.iridium.iridiumskyblock.gui.BlockValueGUI.BlockValueType;
import com.iridium.iridiumskyblock.gui.InventoryConfigGUI;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
* Command which shows all the valuable blocks and spawners.
Expand All @@ -24,7 +24,6 @@ public class BlockValueCommand extends Command {
*/
public BlockValueCommand() {
super(Collections.singletonList("blockvalues"), "Show the values of blocks", "", true, Duration.ZERO);

}

/**
Expand All @@ -42,11 +41,13 @@ public boolean execute(CommandSender sender, String[] arguments) {
player.openInventory(new InventoryConfigGUI(IridiumSkyblock.getInstance().getInventories().blockValueSelectGUI).getInventory());
return true;
}

BlockValueGUI.BlockValueType blockValueType = BlockValueGUI.BlockValueType.getType(arguments[1]);
if (blockValueType == null) {
player.openInventory(new InventoryConfigGUI(IridiumSkyblock.getInstance().getInventories().blockValueSelectGUI).getInventory());
return true;
}

player.openInventory(new BlockValueGUI(blockValueType).getInventory());
return true;
}
Expand All @@ -63,8 +64,10 @@ public boolean execute(CommandSender sender, String[] arguments) {
@Override
public List<String> onTabComplete(CommandSender commandSender, org.bukkit.command.Command command, String label, String[] args) {
// We currently don't want to tab-completion here
// Return a new List so it isn't a list of online players
return Arrays.stream(BlockValueGUI.BlockValueType.values()).map(Enum::toString).filter(s -> s.toLowerCase().contains(args[1].toLowerCase())).collect(Collectors.toList());
// Return a new List, so it isn't a list of online players
return Arrays.stream(BlockValueGUI.BlockValueType.values())
.map(BlockValueType::name)
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@
import com.iridium.iridiumskyblock.database.User;
import com.iridium.iridiumskyblock.gui.BoostersGUI;
import com.iridium.iridiumskyblock.utils.PlayerUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class BoostersCommand extends Command {
public class BoosterCommand extends Command {

/**
* The default constructor.
*/
public BoostersCommand() {
public BoosterCommand() {
super(Arrays.asList("booster", "boosters"), "Open the Island Boosters Menu", "", true, Duration.ZERO);
}

Expand All @@ -42,15 +41,14 @@ public boolean execute(CommandSender sender, String[] args) {
Player player = (Player) sender;
User user = IridiumSkyblock.getInstance().getUserManager().getUser(player);
Optional<Island> island = user.getIsland();

if (!island.isPresent()) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noIsland.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
}

if (args.length != 2) {
player.openInventory(new BoostersGUI(island.get()).getInventory());
return true;

}

String boosterName = args[1];
Expand All @@ -59,14 +57,17 @@ public boolean execute(CommandSender sender, String[] args) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().unknownBooster.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
}

IslandBooster islandBooster = IridiumSkyblock.getInstance().getIslandManager().getIslandBooster(island.get(), boosterName);
if (islandBooster.isActive()) {
return false;
}

if (!PlayerUtils.pay(player, island.get(), booster.crystalsCost, booster.vaultCost)) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().cannotAfford.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
}

islandBooster.setTime(LocalDateTime.now().plusSeconds(booster.time));
IslandLog islandLog = new IslandLog(island.get(), LogAction.BOOSTER_PURCHASE, user, null, 0, boosterName);
IridiumSkyblock.getInstance().getDatabaseManager().getIslandLogTableManager().addEntry(islandLog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import com.iridium.iridiumskyblock.database.Island;
import com.iridium.iridiumskyblock.database.User;
import com.iridium.iridiumskyblock.gui.InventoryConfigGUI;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;

/**
* Command which changes the Island's Border Color.
*/
Expand All @@ -40,11 +43,11 @@ public boolean execute(CommandSender sender, String[] args) {
Player player = (Player) sender;
User user = IridiumSkyblock.getInstance().getUserManager().getUser(player);
Optional<Island> island = user.getIsland();

if (!island.isPresent()) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noIsland.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
}

if (!IridiumSkyblock.getInstance().getIslandManager().getIslandPermission(island.get(), IridiumSkyblock.getInstance().getUserManager().getUser(player), PermissionType.BORDER)) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().cannotManageBorder.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
Expand All @@ -54,18 +57,23 @@ public boolean execute(CommandSender sender, String[] args) {
player.openInventory(new InventoryConfigGUI(IridiumSkyblock.getInstance().getInventories().islandBorder).getInventory());
return true;
}

Color color = Color.getColor(args[1]);
if (color == null) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().notAColor.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
}

if (!IridiumSkyblock.getInstance().getBorder().enabled.getOrDefault(color, true)) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().borderColorDisabled.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
}

island.get().setColor(color);
island.get().getMembers().stream().map(islandUser -> Bukkit.getPlayer(islandUser.getUuid())).filter(Objects::nonNull).forEach(islandPlayer ->
islandPlayer.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().islandBorderChanged.replace("%player%", player.getName()).replace("%color%", color.toString()).replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)))
island.get().getMembers().stream()
.map(islandUser -> Bukkit.getPlayer(islandUser.getUuid()))
.filter(Objects::nonNull)
.forEach(islandPlayer -> islandPlayer.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().islandBorderChanged.replace("%player%", player.getName()).replace("%color%", color.toString()).replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)))
);
return true;
}
Expand All @@ -81,7 +89,9 @@ public boolean execute(CommandSender sender, String[] args) {
*/
@Override
public List<String> onTabComplete(CommandSender commandSender, org.bukkit.command.Command command, String label, String[] args) {
return Arrays.stream(Color.values()).map(Enum::name).filter(s -> s.toLowerCase().contains(args[1].toLowerCase())).collect(Collectors.toList());
return Arrays.stream(Color.values())
.map(Color::name)
.collect(Collectors.toList());
}

}
Loading

0 comments on commit 124614e

Please sign in to comment.