Adds level calculation on login option.

https://github.com/BentoBoxWorld/Level/issues/36
This commit is contained in:
tastybento 2019-08-08 16:30:10 -07:00
parent ef81d6c985
commit d49859bc78
7 changed files with 37 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.bukkit.World; import org.bukkit.World;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
@ -54,7 +55,7 @@ public class Level extends Addon {
* @param user - the user who is asking, or null if none * @param user - the user who is asking, or null if none
* @param playerUUID - the target island member's UUID * @param playerUUID - the target island member's UUID
*/ */
public void calculateIslandLevel(World world, User user, UUID playerUUID) { public void calculateIslandLevel(World world, @Nullable User user, UUID playerUUID) {
levelCalc.calculateIslandLevel(world, user, playerUUID); levelCalc.calculateIslandLevel(world, user, playerUUID);
} }

View File

@ -41,16 +41,18 @@ class LevelPresenter {
targetPlayer = plugin.getIslands().getOwner(world, targetPlayer); targetPlayer = plugin.getIslands().getOwner(world, targetPlayer);
inTeam = true; inTeam = true;
} else { } else {
sender.sendMessage("general.errors.player-has-no-island"); if (sender != null) sender.sendMessage("general.errors.player-has-no-island");
return; return;
} }
} }
// Player asking for their own island calc // Player asking for their own island calc
if (inTeam || !sender.isPlayer() || sender.getUniqueId().equals(targetPlayer) || sender.isOp() || sender.hasPermission(permPrefix + "mod.info")) { if (sender == null || inTeam || !sender.isPlayer() || sender.getUniqueId().equals(targetPlayer) || sender.isOp() || sender.hasPermission(permPrefix + "mod.info")) {
// Newer better system - uses chunks // Newer better system - uses chunks
if (!onLevelWaitTime(sender) || levelWait <= 0 || sender.isOp() || sender.hasPermission(permPrefix + "mod.info")) { if (sender == null || !onLevelWaitTime(sender) || levelWait <= 0 || sender.isOp() || sender.hasPermission(permPrefix + "mod.info")) {
sender.sendMessage("island.level.calculating"); if (sender != null) {
setLevelWaitTime(sender); sender.sendMessage("island.level.calculating");
setLevelWaitTime(sender);
}
new PlayerLevel(addon, plugin.getIslands().getIsland(world, targetPlayer), targetPlayer, sender); new PlayerLevel(addon, plugin.getIslands().getIsland(world, targetPlayer), targetPlayer, sender);
} else { } else {
// Cooldown // Cooldown

View File

@ -5,6 +5,7 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.bukkit.World; import org.bukkit.World;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.events.addon.AddonEvent; import world.bentobox.bentobox.api.events.addon.AddonEvent;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
@ -34,7 +35,7 @@ public class PlayerLevel {
private CalcIslandLevel calc; private CalcIslandLevel calc;
public PlayerLevel(final Level addon, final Island island, final UUID targetPlayer, final User asker) { public PlayerLevel(final Level addon, final Island island, final UUID targetPlayer, @Nullable final User asker) {
this.addon = addon; this.addon = addon;
this.island = island; this.island = island;
this.world = island.getCenter().getWorld(); this.world = island.getCenter().getWorld();
@ -70,7 +71,7 @@ public class PlayerLevel {
// Save the results // Save the results
island.getMemberSet().forEach(m -> addon.setIslandLevel(world, m, results.getLevel())); island.getMemberSet().forEach(m -> addon.setIslandLevel(world, m, results.getLevel()));
// Display result if event is not cancelled // Display result if event is not cancelled
if (!ilce.isCancelled()) { if (!ilce.isCancelled() && asker != null) {
informPlayers(results); informPlayers(results);
} }
} }

View File

@ -13,11 +13,11 @@ import world.bentobox.level.Level;
public class AdminLevelCommand extends CompositeCommand { public class AdminLevelCommand extends CompositeCommand {
private final Level levelPlugin; private final Level addon;
public AdminLevelCommand(Level levelPlugin, CompositeCommand parent) { public AdminLevelCommand(Level addon, CompositeCommand parent) {
super(parent, "level"); super(parent, "level");
this.levelPlugin = levelPlugin; this.addon = addon;
} }
@Override @Override
@ -38,7 +38,7 @@ public class AdminLevelCommand extends CompositeCommand {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0)); user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return true; return true;
} else { } else {
levelPlugin.calculateIslandLevel(getWorld(), user, playerUUID); addon.calculateIslandLevel(getWorld(), user, playerUUID);
} }
return true; return true;
} else { } else {

View File

@ -205,4 +205,11 @@ public class Settings {
return level.getConfig().getBoolean("end"); return level.getConfig().getBoolean("end");
} }
/**
* @return true if level should be calculated on login
*/
public boolean isLogin() {
return level.getConfig().getBoolean("login");
}
} }

View File

@ -28,6 +28,14 @@ public class JoinLeaveListener implements Listener {
public void onPlayerJoin(PlayerJoinEvent e) { public void onPlayerJoin(PlayerJoinEvent e) {
// Load player into cache // Load player into cache
addon.getLevelsData(e.getPlayer().getUniqueId()); addon.getLevelsData(e.getPlayer().getUniqueId());
// If level calc on login is enabled, run through all the worlds and calculate the level
if (addon.getSettings().isLogin()) {
addon.getPlugin().getAddonsManager().getGameModeAddons().stream()
.filter(gm -> addon.getSettings().getGameModes().contains(gm.getDescription().getName()))
.forEach(gm -> {
addon.calculateIslandLevel(gm.getOverWorld(), null, e.getPlayer().getUniqueId());
});
}
} }
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)

View File

@ -14,6 +14,11 @@ game-modes:
# Players with the permission askyblock.island.multiplier.# will have their blocks # Players with the permission askyblock.island.multiplier.# will have their blocks
# multiplied in value by that amount. # multiplied in value by that amount.
# Calculate island level on login
# This silently calculates the player's island level when they login
# This applies to all islands the player has on the server, e.g., BSkyBlock, AcidIsland
login: false
# Include nether island in level calculations. # Include nether island in level calculations.
# Warning: Enabling this mid-game will give players with an island a jump in # Warning: Enabling this mid-game will give players with an island a jump in
# island level. New islands will be correctly zeroed. # island level. New islands will be correctly zeroed.