2018-10-05 06:48:44 +02:00
|
|
|
package bentobox.addon.limits;
|
|
|
|
|
2019-02-07 08:20:45 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2019-05-29 08:27:35 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2019-02-07 08:20:45 +01:00
|
|
|
import org.bukkit.World;
|
|
|
|
|
2019-02-09 19:07:16 +01:00
|
|
|
import bentobox.addon.limits.commands.AdminCommand;
|
|
|
|
import bentobox.addon.limits.commands.PlayerCommand;
|
2019-02-07 08:20:45 +01:00
|
|
|
import bentobox.addon.limits.listeners.BlockLimitsListener;
|
2019-04-23 00:50:15 +02:00
|
|
|
import bentobox.addon.limits.listeners.EntityLimitListener;
|
2019-05-29 08:27:35 +02:00
|
|
|
import bentobox.addon.limits.listeners.EpicSpawnersListener;
|
2019-02-09 07:35:29 +01:00
|
|
|
import bentobox.addon.limits.listeners.JoinListener;
|
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;
|
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));
|
2019-05-29 08:27:35 +02:00
|
|
|
// Register epic spawners one tick after load
|
|
|
|
Bukkit.getScheduler().runTask(getPlugin(), () -> {
|
|
|
|
if (Bukkit.getServer().getPluginManager().getPlugin("EpicSpawners") != null) {
|
|
|
|
registerListener(new EpicSpawnersListener(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) {
|
|
|
|
return gameModes.stream().filter(gm -> gm.inWorld(world)).findFirst().map(gm -> gm.getPermissionPrefix()).orElse("");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|