2018-10-31 14:09:56 +01:00
|
|
|
package world.bentobox.level;
|
2017-10-21 20:32:32 +02:00
|
|
|
|
2020-04-19 19:46:41 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2017-10-21 20:32:32 +02:00
|
|
|
|
2020-02-02 04:56:28 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2018-05-26 04:59:44 +02:00
|
|
|
import org.bukkit.World;
|
2020-04-19 19:46:41 +02:00
|
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
2018-05-26 04:59:44 +02:00
|
|
|
|
2019-01-03 19:27:20 +01:00
|
|
|
import world.bentobox.bentobox.api.addons.Addon;
|
2020-04-19 23:02:45 +02:00
|
|
|
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
2020-04-19 19:46:41 +02:00
|
|
|
import world.bentobox.bentobox.api.configuration.Config;
|
2019-01-03 19:27:20 +01:00
|
|
|
import world.bentobox.bentobox.api.user.User;
|
2020-06-22 02:54:51 +02:00
|
|
|
import world.bentobox.level.calculators.IslandLevelCalculator;
|
|
|
|
import world.bentobox.level.calculators.Pipeliner;
|
|
|
|
import world.bentobox.level.commands.AdminLevelCommand;
|
|
|
|
import world.bentobox.level.commands.AdminLevelStatusCommand;
|
|
|
|
import world.bentobox.level.commands.AdminTopCommand;
|
|
|
|
import world.bentobox.level.commands.IslandLevelCommand;
|
|
|
|
import world.bentobox.level.commands.IslandTopCommand;
|
|
|
|
import world.bentobox.level.commands.IslandValueCommand;
|
2020-04-19 19:46:41 +02:00
|
|
|
import world.bentobox.level.config.BlockConfig;
|
|
|
|
import world.bentobox.level.config.ConfigSettings;
|
2020-06-22 02:54:51 +02:00
|
|
|
import world.bentobox.level.listeners.IslandActivitiesListeners;
|
2019-02-03 06:43:27 +01:00
|
|
|
import world.bentobox.level.listeners.JoinLeaveListener;
|
2018-12-23 21:20:15 +01:00
|
|
|
import world.bentobox.level.requests.LevelRequestHandler;
|
2019-02-27 23:14:20 +01:00
|
|
|
import world.bentobox.level.requests.TopTenRequestHandler;
|
|
|
|
|
2017-10-25 01:24:43 +02:00
|
|
|
/**
|
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
2017-12-28 17:43:07 +01:00
|
|
|
public class Level extends Addon {
|
2018-04-16 02:32:09 +02:00
|
|
|
|
2018-02-14 05:48:27 +01:00
|
|
|
// Settings
|
2020-04-19 19:46:41 +02:00
|
|
|
private ConfigSettings settings;
|
|
|
|
private Config<ConfigSettings> configObject = new Config<>(this, ConfigSettings.class);
|
|
|
|
private BlockConfig blockConfig;
|
2020-06-22 02:54:51 +02:00
|
|
|
private Pipeliner pipeliner;
|
|
|
|
private LevelsManager manager;
|
2018-02-19 22:06:58 +01:00
|
|
|
|
2020-04-19 19:46:41 +02:00
|
|
|
@Override
|
|
|
|
public void onLoad() {
|
|
|
|
// Save the default config from config.yml
|
|
|
|
saveDefaultConfig();
|
2020-04-20 00:17:34 +02:00
|
|
|
if (loadSettings()) {
|
2020-06-22 02:54:51 +02:00
|
|
|
// Disable
|
|
|
|
logError("Level settings could not load! Addon disabled.");
|
|
|
|
setState(State.DISABLED);
|
|
|
|
} else {
|
2020-04-20 00:17:34 +02:00
|
|
|
configObject.saveConfigObject(settings);
|
|
|
|
}
|
2020-04-19 19:46:41 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 00:17:34 +02:00
|
|
|
private boolean loadSettings() {
|
2020-04-19 19:46:41 +02:00
|
|
|
// Load settings again to get worlds
|
|
|
|
settings = configObject.loadConfigObject();
|
2020-06-22 02:54:51 +02:00
|
|
|
return settings == null;
|
2020-04-19 19:46:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-21 20:32:32 +02:00
|
|
|
@Override
|
2017-10-22 04:52:11 +02:00
|
|
|
public void onEnable() {
|
2020-04-19 19:46:41 +02:00
|
|
|
loadBlockSettings();
|
2020-06-22 02:54:51 +02:00
|
|
|
// Start pipeline
|
|
|
|
pipeliner = new Pipeliner(this);
|
|
|
|
// Start Manager
|
|
|
|
manager = new LevelsManager(this);
|
|
|
|
manager.loadTopTens();
|
|
|
|
// Register listeners
|
|
|
|
this.registerListener(new IslandActivitiesListeners(this));
|
|
|
|
this.registerListener(new JoinLeaveListener(this));
|
2020-04-19 23:02:45 +02:00
|
|
|
// Register commands for GameModes
|
2019-01-16 04:35:08 +01:00
|
|
|
getPlugin().getAddonsManager().getGameModeAddons().stream()
|
2020-06-22 02:54:51 +02:00
|
|
|
.filter(gm -> !settings.getGameModes().contains(gm.getDescription().getName()))
|
2019-01-16 04:35:08 +01:00
|
|
|
.forEach(gm -> {
|
2019-02-04 07:30:27 +01:00
|
|
|
log("Level hooking into " + gm.getDescription().getName());
|
2020-04-19 23:02:45 +02:00
|
|
|
registerCommands(gm);
|
2020-06-22 02:54:51 +02:00
|
|
|
registerPlaceholders(gm);
|
2018-05-27 03:20:33 +02:00
|
|
|
});
|
2018-12-23 21:20:15 +01:00
|
|
|
// Register request handlers
|
|
|
|
registerRequestHandler(new LevelRequestHandler(this));
|
2019-02-27 23:14:20 +01:00
|
|
|
registerRequestHandler(new TopTenRequestHandler(this));
|
2018-12-23 21:20:15 +01:00
|
|
|
|
2020-01-28 17:32:52 +01:00
|
|
|
// Check if WildStackers is enabled on the server
|
2020-04-19 23:02:45 +02:00
|
|
|
// I only added support for counting blocks into the island level
|
|
|
|
// Someone else can PR if they want spawners added to the Leveling system :)
|
2020-06-22 02:54:51 +02:00
|
|
|
IslandLevelCalculator.stackersEnabled = Bukkit.getPluginManager().getPlugin("WildStacker") != null;
|
2020-01-28 17:32:52 +01:00
|
|
|
|
2017-10-21 20:32:32 +02:00
|
|
|
}
|
2017-10-22 08:09:21 +02:00
|
|
|
|
2020-04-19 23:02:45 +02:00
|
|
|
private void registerPlaceholders(GameModeAddon gm) {
|
2020-06-22 02:54:51 +02:00
|
|
|
if (getPlugin().getPlaceholdersManager() == null) return;
|
2020-04-19 23:02:45 +02:00
|
|
|
// Island Level
|
|
|
|
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
|
|
|
gm.getDescription().getName().toLowerCase() + "_island_level",
|
2020-06-22 02:54:51 +02:00
|
|
|
user -> getManager().getIslandLevelString(gm.getOverWorld(), user.getUniqueId()));
|
|
|
|
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
|
|
|
gm.getDescription().getName().toLowerCase() + "_points_to_next_level",
|
|
|
|
user -> getManager().getPointsToNextString(gm.getOverWorld(), user.getUniqueId()));
|
2020-04-19 23:02:45 +02:00
|
|
|
|
|
|
|
// Visited Island Level
|
|
|
|
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
|
|
|
gm.getDescription().getName().toLowerCase() + "_visited_island_level", user -> getVisitedIslandLevel(gm, user));
|
|
|
|
|
2020-06-22 02:54:51 +02:00
|
|
|
// Register Top Ten Placeholders
|
|
|
|
for (int i = 1; i < 11; i++) {
|
2020-04-19 23:02:45 +02:00
|
|
|
final int rank = i;
|
|
|
|
// Name
|
|
|
|
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
2020-06-22 02:54:51 +02:00
|
|
|
gm.getDescription().getName().toLowerCase() + "_top_name_" + i, u -> getRankName(gm.getOverWorld(), rank));
|
|
|
|
// Level
|
|
|
|
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
|
|
|
gm.getDescription().getName().toLowerCase() + "_top_value_" + i, u -> getRankLevel(gm.getOverWorld(), rank));
|
2020-04-19 23:02:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-22 02:54:51 +02:00
|
|
|
private String getRankName(World world, int rank) {
|
|
|
|
if (rank < 1) rank = 1;
|
|
|
|
if (rank > 10) rank = 10;
|
|
|
|
return getPlayers().getName(getManager().getTopTen(world, 10).keySet().stream().skip(rank - 1).limit(1).findFirst().orElse(null));
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getRankLevel(World world, int rank) {
|
|
|
|
if (rank < 1) rank = 1;
|
|
|
|
if (rank > 10) rank = 10;
|
|
|
|
return getManager().formatLevel(getManager().getTopTen(world, 10).values().stream().skip(rank - 1).limit(1).findFirst().orElse(null));
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:02:45 +02:00
|
|
|
private String getVisitedIslandLevel(GameModeAddon gm, User user) {
|
2020-04-20 00:49:16 +02:00
|
|
|
if (!gm.inWorld(user.getLocation())) return "";
|
2020-04-19 23:02:45 +02:00
|
|
|
return getIslands().getIslandAt(user.getLocation())
|
2020-06-22 02:54:51 +02:00
|
|
|
.map(island -> getManager().getIslandLevelString(gm.getOverWorld(), island.getOwner()))
|
2020-04-19 23:02:45 +02:00
|
|
|
.orElse("0");
|
|
|
|
}
|
|
|
|
|
2020-06-22 02:54:51 +02:00
|
|
|
|
2020-04-19 23:02:45 +02:00
|
|
|
private void registerCommands(GameModeAddon gm) {
|
|
|
|
gm.getAdminCommand().ifPresent(adminCommand -> {
|
|
|
|
new AdminLevelCommand(this, adminCommand);
|
|
|
|
new AdminTopCommand(this, adminCommand);
|
2020-06-22 02:54:51 +02:00
|
|
|
new AdminLevelStatusCommand(this, adminCommand);
|
2020-04-19 23:02:45 +02:00
|
|
|
});
|
|
|
|
gm.getPlayerCommand().ifPresent(playerCmd -> {
|
|
|
|
new IslandLevelCommand(this, playerCmd);
|
|
|
|
new IslandTopCommand(this, playerCmd);
|
|
|
|
new IslandValueCommand(this, playerCmd);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-22 02:54:51 +02:00
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
|
|
|
if (manager != null) {
|
|
|
|
manager.save();
|
|
|
|
}
|
|
|
|
|
2017-10-21 20:32:32 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 02:54:51 +02:00
|
|
|
private void loadBlockSettings() {
|
|
|
|
// Save the default blockconfig.yml
|
|
|
|
this.saveResource("blockconfig.yml", false);
|
|
|
|
|
|
|
|
YamlConfiguration blockValues = new YamlConfiguration();
|
|
|
|
try {
|
|
|
|
File file = new File(this.getDataFolder(), "blockconfig.yml");
|
|
|
|
blockValues.load(file);
|
|
|
|
// Load the block config class
|
|
|
|
blockConfig = new BlockConfig(this, blockValues, file);
|
|
|
|
} catch (IOException | InvalidConfigurationException e) {
|
|
|
|
// Disable
|
|
|
|
logError("Level blockconfig.yml settings could not load! Addon disabled.");
|
|
|
|
setState(State.DISABLED);
|
2019-01-07 16:29:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-06-22 02:54:51 +02:00
|
|
|
|
2018-07-28 22:38:04 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 02:54:51 +02:00
|
|
|
|
2018-07-28 22:38:04 +02:00
|
|
|
/**
|
2020-06-22 02:54:51 +02:00
|
|
|
* @return the blockConfig
|
2018-07-28 22:38:04 +02:00
|
|
|
*/
|
2020-06-22 02:54:51 +02:00
|
|
|
public BlockConfig getBlockConfig() {
|
|
|
|
return blockConfig;
|
2017-10-22 04:52:11 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 04:56:44 +01:00
|
|
|
/**
|
2020-06-22 02:54:51 +02:00
|
|
|
* @return the settings
|
2019-02-22 04:56:44 +01:00
|
|
|
*/
|
2020-06-22 02:54:51 +02:00
|
|
|
public ConfigSettings getSettings() {
|
|
|
|
return settings;
|
2019-02-22 04:56:44 +01:00
|
|
|
}
|
2019-08-09 01:30:10 +02:00
|
|
|
|
2019-12-01 03:30:08 +01:00
|
|
|
/**
|
2020-06-22 02:54:51 +02:00
|
|
|
* @return the pipeliner
|
2019-12-01 03:30:08 +01:00
|
|
|
*/
|
2020-06-22 02:54:51 +02:00
|
|
|
public Pipeliner getPipeliner() {
|
|
|
|
return pipeliner;
|
2018-04-16 02:32:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 02:54:51 +02:00
|
|
|
/**
|
|
|
|
* @return the manager
|
|
|
|
*/
|
|
|
|
public LevelsManager getManager() {
|
|
|
|
return manager;
|
2020-04-19 19:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-22 02:54:51 +02:00
|
|
|
* Set the config settings - used for tests only
|
|
|
|
* @param configSettings - config settings
|
2020-04-19 19:46:41 +02:00
|
|
|
*/
|
2020-06-22 02:54:51 +02:00
|
|
|
void setSettings(ConfigSettings configSettings) {
|
|
|
|
this.settings = configSettings;
|
|
|
|
|
2020-04-19 19:46:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-21 20:32:32 +02:00
|
|
|
}
|