2019-12-16 16:47:51 +01:00
|
|
|
package world.bentobox.limits;
|
2018-10-05 06:48:44 +02:00
|
|
|
|
2019-02-07 08:20:45 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import org.bukkit.World;
|
|
|
|
|
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;
|
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;
|
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 {
|
|
|
|
|
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;
|
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));
|
|
|
|
log("Limits will apply to " + gm.getDescription().getName());
|
|
|
|
}
|
|
|
|
);
|
2019-02-07 08:20:45 +01:00
|
|
|
// Register listener
|
|
|
|
blockLimitListener = new BlockLimitsListener(this);
|
|
|
|
registerListener(blockLimitListener);
|
2019-02-09 07:35:29 +01:00
|
|
|
registerListener(new JoinListener(this));
|
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
|
|
|
|
2018-10-05 06:48:44 +02:00
|
|
|
}
|