2019-12-16 16:47:51 +01:00
|
|
|
package world.bentobox.limits;
|
2018-10-05 06:48:44 +02:00
|
|
|
|
2021-08-01 01:30:41 +02:00
|
|
|
import java.util.Arrays;
|
2019-02-07 08:20:45 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2021-07-29 10:38:45 +02:00
|
|
|
import org.bukkit.Material;
|
2019-02-07 08:20:45 +01:00
|
|
|
import org.bukkit.World;
|
|
|
|
|
2021-07-30 11:16:28 +02:00
|
|
|
import org.eclipse.jdt.annotation.Nullable;
|
2018-10-05 06:48:44 +02:00
|
|
|
import world.bentobox.bentobox.api.addons.Addon;
|
2019-02-07 08:20:45 +01:00
|
|
|
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
2021-07-30 11:16:28 +02:00
|
|
|
import world.bentobox.bentobox.api.user.User;
|
|
|
|
import world.bentobox.bentobox.database.objects.Island;
|
2019-12-16 16:47:51 +01:00
|
|
|
import world.bentobox.limits.commands.AdminCommand;
|
|
|
|
import world.bentobox.limits.commands.PlayerCommand;
|
|
|
|
import world.bentobox.limits.listeners.BlockLimitsListener;
|
|
|
|
import world.bentobox.limits.listeners.EntityLimitListener;
|
|
|
|
import world.bentobox.limits.listeners.JoinListener;
|
2021-12-22 23:37:10 +01:00
|
|
|
import world.bentobox.limits.objects.IslandBlockCount;
|
2018-10-05 06:48:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-02-07 08:20:45 +01:00
|
|
|
* Addon to BentoBox that monitors and enforces limits
|
2018-10-05 06:48:44 +02:00
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class Limits extends Addon {
|
|
|
|
|
2021-12-25 18:10:53 +01:00
|
|
|
private static final String LIMIT_NOT_SET = "Limit not set";
|
2019-02-07 08:20:45 +01:00
|
|
|
private Settings settings;
|
2019-02-09 07:35:29 +01:00
|
|
|
private List<GameModeAddon> gameModes;
|
2019-02-07 08:20:45 +01:00
|
|
|
private BlockLimitsListener blockLimitListener;
|
2021-03-07 18:20:28 +01:00
|
|
|
private JoinListener joinListener;
|
2018-10-05 06:48:44 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisable(){
|
2019-02-07 08:20:45 +01:00
|
|
|
if (blockLimitListener != null) {
|
|
|
|
blockLimitListener.save();
|
|
|
|
}
|
2018-10-05 06:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
|
|
|
// Load the plugin's config
|
|
|
|
saveDefaultConfig();
|
|
|
|
// Load settings
|
|
|
|
settings = new Settings(this);
|
2019-02-07 08:20:45 +01:00
|
|
|
// Register worlds from GameModes
|
2019-02-09 07:35:29 +01:00
|
|
|
gameModes = getPlugin().getAddonsManager().getGameModeAddons().stream()
|
2019-02-07 08:20:45 +01:00
|
|
|
.filter(gm -> settings.getGameModes().contains(gm.getDescription().getName()))
|
|
|
|
.collect(Collectors.toList());
|
2019-02-09 19:07:16 +01:00
|
|
|
gameModes.forEach(gm ->
|
|
|
|
{
|
|
|
|
// Register commands
|
|
|
|
gm.getAdminCommand().ifPresent(a -> new AdminCommand(this, a));
|
|
|
|
gm.getPlayerCommand().ifPresent(a -> new PlayerCommand(this, a));
|
2021-07-29 10:38:45 +02:00
|
|
|
registerPlaceholders(gm);
|
2019-02-09 19:07:16 +01:00
|
|
|
log("Limits will apply to " + gm.getDescription().getName());
|
|
|
|
}
|
|
|
|
);
|
2019-02-07 08:20:45 +01:00
|
|
|
// Register listener
|
|
|
|
blockLimitListener = new BlockLimitsListener(this);
|
|
|
|
registerListener(blockLimitListener);
|
2021-03-07 18:20:28 +01:00
|
|
|
joinListener = new JoinListener(this);
|
|
|
|
registerListener(joinListener);
|
2019-04-23 00:50:15 +02:00
|
|
|
registerListener(new EntityLimitListener(this));
|
2018-10-05 06:48:44 +02:00
|
|
|
// Done
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the settings
|
|
|
|
*/
|
|
|
|
public Settings getSettings() {
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
2019-02-09 07:35:29 +01:00
|
|
|
/**
|
|
|
|
* @return the gameModes
|
|
|
|
*/
|
|
|
|
public List<GameModeAddon> getGameModes() {
|
|
|
|
return gameModes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the blockLimitListener
|
|
|
|
*/
|
|
|
|
public BlockLimitsListener getBlockLimitListener() {
|
|
|
|
return blockLimitListener;
|
|
|
|
}
|
2018-10-05 06:48:44 +02:00
|
|
|
|
2019-02-09 07:35:29 +01:00
|
|
|
/**
|
|
|
|
* Checks if this world is covered by the activated game modes
|
|
|
|
* @param world - world
|
|
|
|
* @return true or false
|
|
|
|
*/
|
|
|
|
public boolean inGameModeWorld(World world) {
|
|
|
|
return gameModes.stream().anyMatch(gm -> gm.inWorld(world));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the game mode for this world
|
|
|
|
* @param world - world
|
|
|
|
* @return game mode name or empty string if none
|
|
|
|
*/
|
2019-02-09 20:13:20 +01:00
|
|
|
public String getGameModeName(World world) {
|
2019-02-09 07:35:29 +01:00
|
|
|
return gameModes.stream().filter(gm -> gm.inWorld(world)).findFirst().map(gm -> gm.getDescription().getName()).orElse("");
|
|
|
|
}
|
|
|
|
|
2019-02-09 20:13:20 +01:00
|
|
|
/**
|
|
|
|
* Get the name of the game mode for this world
|
|
|
|
* @param world - world
|
|
|
|
* @return game mode name or empty string if none
|
|
|
|
*/
|
|
|
|
public String getGameModePermPrefix(World world) {
|
2019-11-08 21:35:47 +01:00
|
|
|
return gameModes.stream().filter(gm -> gm.inWorld(world)).findFirst().map(GameModeAddon::getPermissionPrefix).orElse("");
|
2019-02-09 20:13:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-09 07:35:29 +01:00
|
|
|
/**
|
|
|
|
* Check if any of the game modes covered have this name
|
|
|
|
* @param gameMode - name of game mode
|
|
|
|
* @return true or false
|
|
|
|
*/
|
|
|
|
public boolean isCoveredGameMode(String gameMode) {
|
|
|
|
return gameModes.stream().anyMatch(gm -> gm.getDescription().getName().equals(gameMode));
|
|
|
|
}
|
2019-02-09 19:07:16 +01:00
|
|
|
|
2021-03-07 18:20:28 +01:00
|
|
|
/**
|
|
|
|
* @return the joinListener
|
|
|
|
*/
|
|
|
|
public JoinListener getJoinListener() {
|
|
|
|
return joinListener;
|
|
|
|
}
|
|
|
|
|
2021-07-29 10:38:45 +02:00
|
|
|
private void registerPlaceholders(GameModeAddon gm) {
|
|
|
|
if (getPlugin().getPlaceholdersManager() == null) return;
|
2021-08-01 01:30:41 +02:00
|
|
|
Arrays.stream(Material.values())
|
2021-12-22 23:37:10 +01:00
|
|
|
.filter(Material::isBlock)
|
2021-12-22 19:11:59 +01:00
|
|
|
.forEach(m -> registerCountAndLimitPlaceholders(m, gm));
|
2021-07-30 11:16:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers placeholders for the count and limit of the material
|
|
|
|
* in the format of %Limits_(gamemode prefix)_island_(lowercase material name)_count%
|
|
|
|
* and %Limits_(gamemode prefix)_island_(lowercase material name)_limit%
|
|
|
|
*
|
|
|
|
* Example: registerCountAndLimitPlaceholders("HOPPER", gm);
|
|
|
|
* Placeholders:
|
|
|
|
* "Limits_bskyblock_island_hopper_count"
|
|
|
|
* "Limits_bskyblock_island_hopper_limit"
|
|
|
|
*
|
2021-12-22 23:37:10 +01:00
|
|
|
* @param m material
|
|
|
|
* @param gm game mode
|
2021-07-30 11:16:28 +02:00
|
|
|
*/
|
2021-08-01 01:30:41 +02:00
|
|
|
private void registerCountAndLimitPlaceholders(Material m, GameModeAddon gm) {
|
2021-07-29 10:38:45 +02:00
|
|
|
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
2021-08-01 01:30:41 +02:00
|
|
|
gm.getDescription().getName().toLowerCase() + "_island_" + m.toString().toLowerCase() + "_count",
|
|
|
|
user -> String.valueOf(getCount(user, m, gm)));
|
2021-07-29 10:38:45 +02:00
|
|
|
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
2021-08-01 01:30:41 +02:00
|
|
|
gm.getDescription().getName().toLowerCase() + "_island_" + m.toString().toLowerCase() + "_limit",
|
|
|
|
user -> getLimit(user, m, gm));
|
2021-07-30 11:16:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param user - Used to identify the island the user belongs to
|
2021-08-01 01:30:41 +02:00
|
|
|
* @param m - The material we are trying to count on the island
|
2021-12-22 23:37:10 +01:00
|
|
|
* @param gm Game Mode Addon
|
2021-07-30 11:16:28 +02:00
|
|
|
* @return Number of blocks of the specified material on the given user's island
|
|
|
|
*/
|
2021-08-01 01:30:41 +02:00
|
|
|
private int getCount(@Nullable User user, Material m, GameModeAddon gm) {
|
2021-07-30 11:16:28 +02:00
|
|
|
Island is = gm.getIslands().getIsland(gm.getOverWorld(), user);
|
|
|
|
if (is == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-12-22 23:37:10 +01:00
|
|
|
@Nullable IslandBlockCount ibc = getBlockLimitListener().getIsland(is.getUniqueId());
|
|
|
|
if (ibc == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ibc.getBlockCount(m);
|
2021-07-30 11:16:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param user - Used to identify the island the user belongs to
|
2021-08-01 01:30:41 +02:00
|
|
|
* @param m - The material whose limit we are querying
|
2021-12-22 23:37:10 +01:00
|
|
|
* @param gm Game Mode Addon
|
2021-07-30 11:16:28 +02:00
|
|
|
* @return The limit of the specified material on the given user's island
|
|
|
|
*/
|
2021-08-01 01:30:41 +02:00
|
|
|
private String getLimit(@Nullable User user, Material m, GameModeAddon gm) {
|
2021-07-30 11:16:28 +02:00
|
|
|
Island is = gm.getIslands().getIsland(gm.getOverWorld(), user);
|
|
|
|
if (is == null) {
|
2021-12-25 18:10:53 +01:00
|
|
|
return LIMIT_NOT_SET;
|
2021-07-30 11:16:28 +02:00
|
|
|
}
|
2022-03-27 13:43:31 +02:00
|
|
|
|
|
|
|
int limit = this.getBlockLimitListener().
|
|
|
|
getMaterialLimits(is.getWorld(), is.getUniqueId()).
|
|
|
|
getOrDefault(m, -1);
|
|
|
|
|
2021-12-25 18:10:53 +01:00
|
|
|
return limit == -1 ? LIMIT_NOT_SET : String.valueOf(limit);
|
2021-07-29 10:38:45 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 06:48:44 +02:00
|
|
|
}
|