Now multi-game support.

This commit is contained in:
tastybento 2018-05-26 18:20:33 -07:00
parent f49d6e9e42
commit a09d974d95
11 changed files with 78 additions and 95 deletions

View File

@ -9,6 +9,7 @@ admin:
description: "calculate the island level for player"
top:
description: "show the top ten list"
unknown-world: "&cUnknown world!"
island:
level:

View File

@ -12,7 +12,6 @@ import bskyblock.addon.level.commands.IslandLevel;
import bskyblock.addon.level.commands.IslandTop;
import bskyblock.addon.level.config.Settings;
import bskyblock.addon.level.database.object.LevelsData;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.addons.Addon;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
@ -47,9 +46,10 @@ public class Level extends Addon {
* @param user
* @param playerUUID - the player's UUID
* @param b
* @param permPrefix
*/
public void calculateIslandLevel(World world, User user, UUID playerUUID, boolean b) {
levelCalc.calculateIslandLevel(world, user, playerUUID, b);
public void calculateIslandLevel(World world, User user, UUID playerUUID, boolean b, String permPrefix) {
levelCalc.calculateIslandLevel(world, user, playerUUID, b, permPrefix);
}
/**
@ -61,7 +61,7 @@ public class Level extends Addon {
LevelsData ld = getLevelsData(targetPlayer);
return ld == null ? 0L : ld.getLevel(world);
}
private LevelsData getLevelsData(UUID targetPlayer) {
// Load player
return levelsCache.getOrDefault(targetPlayer, handler.loadObject(targetPlayer.toString()));
@ -84,6 +84,9 @@ public class Level extends Addon {
if (levelsCache != null) {
save(false);
}
if (topTen != null) {
topTen.saveTopTen();
}
}
@Override
@ -103,15 +106,28 @@ public class Level extends Addon {
// Initialize the cache
levelsCache = new HashMap<>();
// Load the calculator
levelCalc = new LevelPresenter(this);
levelCalc = new LevelPresenter(this, getBSkyBlock());
// Start the top ten and register it for clicks
topTen = new TopTen(this);
registerListener(topTen);
// Register commands
CompositeCommand bsbIslandCmd = getBSkyBlock().getCommandsManager().getCommand(Constants.ISLANDCOMMAND);
// Register commands - run one tick later to allow all addons to load
// AcidIsland hook in
getServer().getScheduler().runTask(getBSkyBlock(), () -> {
this.getBSkyBlock().getAddonsManager().getAddonByName("AcidIsland").ifPresent(a -> {
getLogger().info("DEBUG: " + getBSkyBlock().getCommandsManager().listCommands());
CompositeCommand acidIslandCmd = getBSkyBlock().getCommandsManager().getCommand("ai");
new IslandLevel(this, acidIslandCmd);
new IslandTop(this, acidIslandCmd);
CompositeCommand acidCmd = getBSkyBlock().getCommandsManager().getCommand("acid");
new AdminLevel(this, acidCmd);
new AdminTop(this, acidCmd);
});
});
// BSkyBlock hook in
CompositeCommand bsbIslandCmd = getBSkyBlock().getCommandsManager().getCommand("island");
new IslandLevel(this, bsbIslandCmd);
new IslandTop(this, bsbIslandCmd);
CompositeCommand bsbAdminCmd = getBSkyBlock().getCommandsManager().getCommand(Constants.ADMINCOMMAND);
CompositeCommand bsbAdminCmd = getBSkyBlock().getCommandsManager().getCommand("bsbadmin");
new AdminLevel(this, bsbAdminCmd);
new AdminTop(this, bsbAdminCmd);
// Done
@ -135,8 +151,9 @@ public class Level extends Addon {
* @param world
* @param targetPlayer
* @param level
* @param permPrefix
*/
protected void setIslandLevel(World world, UUID targetPlayer, long level) {
protected void setIslandLevel(World world, UUID targetPlayer, long level, String permPrefix) {
LevelsData ld = getLevelsData(targetPlayer);
if (ld == null) {
ld = new LevelsData(targetPlayer, level, world);
@ -145,7 +162,7 @@ public class Level extends Addon {
}
// Add to cache
levelsCache.put(targetPlayer, ld);
topTen.addEntry(world, targetPlayer, level);
topTen.addEntry(world, targetPlayer, level, permPrefix);
}
public BSBDatabase<LevelsData> getHandler() {

View File

@ -25,7 +25,6 @@ import com.google.common.collect.Multisets;
import bskyblock.addon.level.event.IslandPostLevelEvent;
import bskyblock.addon.level.event.IslandPreLevelEvent;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.util.Pair;
@ -51,9 +50,10 @@ public class LevelCalcByChunk {
HashMap<MaterialData, Integer> limitCount;
private boolean report;
private long oldLevel;
private String permPrefix;
public LevelCalcByChunk(final Level addon, final Island island, final UUID targetPlayer, final User asker, final boolean report) {
public LevelCalcByChunk(final Level addon, final Island island, final UUID targetPlayer, final User asker, final boolean report, String permPrefix) {
this.addon = addon;
this.island = island;
this.world = island != null ? island.getCenter().getWorld() : null;
@ -62,6 +62,7 @@ public class LevelCalcByChunk {
this.limitCount = new HashMap<>(addon.getSettings().getBlockLimits());
this.report = report;
this.oldLevel = addon.getIslandLevel(world, targetPlayer);
this.permPrefix = permPrefix;
// Results go here
result = new Results();
@ -231,8 +232,8 @@ public class LevelCalcByChunk {
if (player != null) {
// Get permission multiplier
for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) {
if (perms.getPermission().startsWith(Constants.PERMPREFIX + "island.multiplier.")) {
String spl[] = perms.getPermission().split(Constants.PERMPREFIX + "island.multiplier.");
if (perms.getPermission().startsWith(permPrefix + "island.multiplier.")) {
String spl[] = perms.getPermission().split(permPrefix + "island.multiplier.");
if (spl.length > 1) {
if (!NumberUtils.isDigits(spl[1])) {
addon.getLogger().severe("Player " + player.getName() + " has permission: " + perms.getPermission() + " <-- the last part MUST be a number! Ignoring...");
@ -301,22 +302,22 @@ public class LevelCalcByChunk {
addon.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
// Save the value
addon.setIslandLevel(world, island.getOwner(), event.getLevel());
addon.setIslandLevel(world, island.getOwner(), event.getLevel(), permPrefix);
if (addon.getIslands().inTeam(island.getWorld(), targetPlayer)) {
//plugin.getLogger().info("DEBUG: player is in team");
for (UUID member : addon.getIslands().getMembers(island.getWorld(), targetPlayer)) {
//plugin.getLogger().info("DEBUG: updating team member level too");
if (addon.getIslandLevel(world, member) != event.getLevel()) {
addon.setIslandLevel(world, member, event.getLevel());
addon.setIslandLevel(world, member, event.getLevel(), permPrefix);
}
}
if (addon.getIslands().inTeam(island.getWorld(), targetPlayer)) {
UUID leader = addon.getIslands().getTeamLeader(island.getWorld(), targetPlayer);
if (leader != null) {
addon.getTopTen().addEntry(world, leader, event.getLevel());
addon.getTopTen().addEntry(world, leader, event.getLevel(), permPrefix);
}
} else {
addon.getTopTen().addEntry(world, targetPlayer, event.getLevel());
addon.getTopTen().addEntry(world, targetPlayer, event.getLevel(), permPrefix);
}
}
}

View File

@ -1,25 +0,0 @@
package bskyblock.addon.level;
import java.util.logging.Logger;
import us.tastybento.bskyblock.BSkyBlock;
/**
* Makes code look nicer
* @author ben
*
*/
public abstract class LevelPlugin {
protected final Level plugin;
protected final BSkyBlock bSkyBlock;
public LevelPlugin(Level plugin) {
this.plugin = plugin;
this.bSkyBlock = BSkyBlock.getInstance();
}
public final Logger getLogger() {
return plugin.getLogger();
}
}

View File

@ -6,31 +6,21 @@ import java.util.UUID;
import org.bukkit.World;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.user.User;
public class LevelPresenter extends LevelPlugin {
public class LevelPresenter {
private int levelWait;
private final Level plugin;
private final BSkyBlock bSkyBlock;
// Level calc cool down
private HashMap<UUID, Long> levelWaitTime = new HashMap<UUID, Long>();
public LevelPresenter(Level plugin) {
super(plugin);
}
/**
* Calculates the island level
*
* @param world - world to check
* @param sender
* - Player object of player who is asking
* @param targetPlayer
* - UUID of the player's island that is being requested
* @return - true if successful.
*/
public boolean calculateIslandLevel(World world, final User sender, final UUID targetPlayer) {
return calculateIslandLevel(world, sender, targetPlayer, false);
public LevelPresenter(Level plugin, BSkyBlock bSkyBlock) {
this.plugin = plugin;
this.bSkyBlock = bSkyBlock;
}
/**
@ -39,9 +29,10 @@ public class LevelPresenter extends LevelPlugin {
* @param sender - asker of the level info
* @param targetPlayer
* @param report - if true, a detailed report will be provided
* @param permPrefix - per prefix for this player
* @return - false if this is cannot be done
*/
public boolean calculateIslandLevel(World world, final User sender, UUID targetPlayer, boolean report) {
public boolean calculateIslandLevel(World world, final User sender, UUID targetPlayer, boolean report, String permPrefix) {
// Check if sender has island
boolean inTeam = false;
if (!bSkyBlock.getIslands().hasIsland(world, targetPlayer)) {
@ -55,12 +46,12 @@ public class LevelPresenter extends LevelPlugin {
}
}
// Player asking for their own island calc
if (inTeam || !sender.isPlayer() || sender.getUniqueId().equals(targetPlayer) || sender.isOp() || sender.hasPermission(Constants.PERMPREFIX + "mod.info")) {
if (inTeam || !sender.isPlayer() || sender.getUniqueId().equals(targetPlayer) || sender.isOp() || sender.hasPermission(permPrefix + "mod.info")) {
// Newer better system - uses chunks
if (!onLevelWaitTime(sender) || levelWait <= 0 || sender.isOp() || sender.hasPermission(Constants.PERMPREFIX + "mod.info")) {
if (!onLevelWaitTime(sender) || levelWait <= 0 || sender.isOp() || sender.hasPermission(permPrefix + "mod.info")) {
sender.sendMessage("island.level.calculating");
setLevelWaitTime(sender);
new LevelCalcByChunk(plugin, bSkyBlock.getIslands().getIsland(world, targetPlayer), targetPlayer, sender, report);
new LevelCalcByChunk(plugin, bSkyBlock.getIslands().getIsland(world, targetPlayer), targetPlayer, sender, report, permPrefix);
} else {
// Cooldown
sender.sendMessage("island.level.cooldown", "[time]", String.valueOf(getLevelWaitTime(sender)));

View File

@ -18,7 +18,6 @@ import org.bukkit.event.inventory.ClickType;
import bskyblock.addon.level.database.object.LevelsData;
import bskyblock.addon.level.database.object.TopTenData;
import bskyblock.addon.warps.Warp;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.panels.PanelItem;
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder;
@ -36,7 +35,7 @@ public class TopTen implements Listener {
// Top ten list of players
private Map<World,TopTenData> topTenList;
private final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25};
private final boolean DEBUG = true;
private final boolean DEBUG = false;
private BSBDatabase<TopTenData> handler;
public TopTen(Level addon) {
@ -53,19 +52,20 @@ public class TopTen implements Listener {
* @param ownerUUID
* @param l
*/
public void addEntry(World world, UUID ownerUUID, long l) {
public void addEntry(World world, UUID ownerUUID, long l, String permPrefix) {
// Check if player is an island owner or not
if (!addon.getIslands().isOwner(world, ownerUUID)) {
return;
}
// Set up world data
topTenList.putIfAbsent(world, new TopTenData());
topTenList.get(world).setUniqueId(world.getName());
// Try and see if the player is online
Player player = addon.getServer().getPlayer(ownerUUID);
if (player != null) {
// Online
if (!player.hasPermission(Constants.PERMPREFIX + "intopten")) {
if (!player.hasPermission(permPrefix + "intopten")) {
topTenList.get(world).remove(ownerUUID);
return;
}
@ -78,7 +78,7 @@ public class TopTen implements Listener {
* takes the level from the player's file.
* Runs asynchronously from the main thread.
*/
public void create() {
public void create(String permPrefix) {
// Obtain all the levels for each known player
BSBDatabase<LevelsData> levelHandler = addon.getHandler();
long index = 0;
@ -89,22 +89,23 @@ public class TopTen implements Listener {
// Convert to UUID
UUID playerUUID = UUID.fromString(lv.getUniqueId());
// Get the world
lv.getLevels().forEach((k,v) -> addEntry(Bukkit.getWorld(k), playerUUID, v));
lv.getLevels().forEach((k,v) -> addEntry(Bukkit.getWorld(k), playerUUID, v, permPrefix));
}
saveTopTen();
}
/**
* Displays the Top Ten list
* @param world
*
* @param user
* - the requesting player
* @return - true if successful, false if no Top Ten list exists
*/
public boolean getGUI(final User user) {
public boolean getGUI(World world, final User user, String permPrefix) {
// Check world
topTenList.putIfAbsent(user.getWorld(), new TopTenData());
topTenList.putIfAbsent(world, new TopTenData());
topTenList.get(world).setUniqueId(world.getName());
if (DEBUG)
addon.getLogger().info("DEBUG: GUI display");
@ -113,7 +114,7 @@ public class TopTen implements Listener {
.user(user);
int i = 1;
Iterator<Entry<UUID, Long>> it = topTenList.get(user.getWorld()).getTopTen().entrySet().iterator();
Iterator<Entry<UUID, Long>> it = topTenList.get(world).getTopTen().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<UUID, Long> m = it.next();
UUID topTenUUID = m.getKey();
@ -125,7 +126,7 @@ public class TopTen implements Listener {
if (entry != null) {
if (DEBUG)
addon.getLogger().info("DEBUG: removing from topten");
if (!entry.hasPermission(Constants.PERMPREFIX + "intopten")) {
if (!entry.hasPermission(permPrefix + "intopten")) {
it.remove();
show = false;
}
@ -135,7 +136,7 @@ public class TopTen implements Listener {
}
if (show) {
panel.item(SLOTS[i-1], getHead(i, m.getValue(), topTenUUID, user));
panel.item(SLOTS[i-1], getHead(i, m.getValue(), topTenUUID, user, world));
if (i++ == 10) break;
}
}
@ -151,15 +152,15 @@ public class TopTen implements Listener {
* @param asker - the asker of the top ten
* @return PanelItem
*/
private PanelItem getHead(int rank, Long level, UUID playerUUID, User asker) {
private PanelItem getHead(int rank, Long level, UUID playerUUID, User asker, World world) {
final String name = addon.getPlayers().getName(playerUUID);
List<String> description = new ArrayList<>();
if (name != null) {
description.add(asker.getTranslation("island.top.gui-heading", "[name]", name, "[rank]", String.valueOf(rank)));
description.add(asker.getTranslation("island.top.island-level","[level]", String.valueOf(level)));
if (addon.getIslands().inTeam(asker.getWorld(), playerUUID)) {
if (addon.getIslands().inTeam(world, playerUUID)) {
List<String> memberList = new ArrayList<>();
for (UUID members : addon.getIslands().getMembers(asker.getWorld(), playerUUID)) {
for (UUID members : addon.getIslands().getMembers(world, playerUUID)) {
memberList.add(ChatColor.AQUA + addon.getPlayers().getName(members));
}
description.addAll(memberList);
@ -205,6 +206,7 @@ public class TopTen implements Listener {
*/
public void removeEntry(World world, UUID ownerUUID) {
topTenList.putIfAbsent(world, new TopTenData());
topTenList.get(world).setUniqueId(world.getName());
topTenList.get(world).remove(ownerUUID);
}

View File

@ -6,7 +6,6 @@ import java.util.UUID;
import org.bukkit.World;
import bskyblock.addon.level.Level;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
@ -39,9 +38,9 @@ public class AdminLevel extends CompositeCommand {
return true;
} else {
if (user.isPlayer()) {
levelPlugin.calculateIslandLevel(world, user, playerUUID, false);
levelPlugin.calculateIslandLevel(world, user, playerUUID, false, getPermissionPrefix());
} else {
levelPlugin.calculateIslandLevel(world, user, playerUUID, true);
levelPlugin.calculateIslandLevel(world, user, playerUUID, true, getPermissionPrefix());
}
}
return true;
@ -53,7 +52,7 @@ public class AdminLevel extends CompositeCommand {
@Override
public void setup() {
this.setPermission(Constants.PERMPREFIX + "admin.level");
this.setPermission(getPermissionPrefix() + "admin.level");
this.setOnlyPlayer(false);
this.setParameters("admin.level.parameters");
this.setDescription("admin.level.description");

View File

@ -7,7 +7,6 @@ import java.util.UUID;
import org.bukkit.World;
import bskyblock.addon.level.Level;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
@ -57,7 +56,7 @@ public class AdminTop extends CompositeCommand {
@Override
public void setup() {
this.setPermission(Constants.PERMPREFIX + "admin.top");
this.setPermission(getPermissionPrefix() + "admin.top");
this.setOnlyPlayer(false);
this.setDescription("admin.top.description");
}

View File

@ -4,7 +4,6 @@ import java.util.List;
import java.util.UUID;
import bskyblock.addon.level.Level;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
@ -29,21 +28,21 @@ public class IslandLevel extends CompositeCommand {
return true;
} else if (user.getUniqueId().equals(playerUUID) ) {
// Self level request
levelPlugin.calculateIslandLevel(user.getWorld(), user, user.getUniqueId(), false);
levelPlugin.calculateIslandLevel(getWorld(), user, user.getUniqueId(), false, this.getPermissionPrefix());
} else {
user.sendMessage("island.level.island-level-is", "[level]", String.valueOf(levelPlugin.getIslandLevel(user.getWorld(), playerUUID)));
user.sendMessage("island.level.island-level-is", "[level]", String.valueOf(levelPlugin.getIslandLevel(getWorld(), playerUUID)));
return true;
}
} else {
// Self level request
levelPlugin.calculateIslandLevel(user.getWorld(), user, user.getUniqueId(), false);
levelPlugin.calculateIslandLevel(getWorld(), user, user.getUniqueId(), false, this.getPermissionPrefix());
}
return false;
}
@Override
public void setup() {
this.setPermission(Constants.PERMPREFIX + "island.level");
this.setPermission(getPermissionPrefix() + "island.level");
this.setParameters("island.level.parameters");
this.setDescription("island.level.description");
this.setOnlyPlayer(true);

View File

@ -3,7 +3,6 @@ package bskyblock.addon.level.commands;
import java.util.List;
import bskyblock.addon.level.Level;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
@ -18,13 +17,13 @@ public class IslandTop extends CompositeCommand {
@Override
public boolean execute(User user, List<String> list) {
plugin.getTopTen().getGUI(user);
plugin.getTopTen().getGUI(getWorld(), user, getPermissionPrefix());
return true;
}
@Override
public void setup() {
this.setPermission(Constants.PERMPREFIX + "island.top");
this.setPermission(getPermissionPrefix() + "island.top");
this.setDescription("island.top.description");

View File

@ -29,7 +29,6 @@ public class Settings {
private boolean islandResetDeathReset;
private boolean teamJoinDeathReset;
@SuppressWarnings("deprecation")
public Settings(Level level) {
level.saveDefaultConfig();
@ -104,6 +103,7 @@ public class Settings {
// All done
}
@SuppressWarnings("deprecation")
private MaterialData getMaterialData(String material) {
String[] split = material.split(":");
byte data = 0;