Fixed Level rewards.

This commit is contained in:
Wertík 2020-03-29 09:10:30 -04:00 committed by Brianna
parent 2e2843422a
commit 9d1dfeff04
6 changed files with 138 additions and 113 deletions

View File

@ -1,20 +1,19 @@
package com.songoda.skyblock.island;
import com.google.common.base.Strings;
import com.songoda.skyblock.SkyBlock;
import com.songoda.skyblock.config.FileManager.Config;
import com.songoda.skyblock.island.reward.LevelReward;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
public class IslandLevel {
@ -126,29 +125,40 @@ public class IslandLevel {
if (level <= highestLevel)
return;
Config config = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml"));
FileConfiguration configLoad = config.getFileConfiguration();
final FileConfiguration language = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")).getFileConfiguration();
final FileConfiguration config = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration();
String msg = configLoad.getString("Command.Island.Level.LevelUp.Message")
.replace("%level%", "" + level);
OfflinePlayer owner = Bukkit.getOfflinePlayer(ownerUUID);
Island island = skyblock.getIslandManager().getIsland(Bukkit.getOfflinePlayer(ownerUUID));
if (owner.isOnline()) {
for (Player player : skyblock.getIslandManager().getPlayersAtIsland(island)) {
Player player = owner.getPlayer();
skyblock.getMessageManager().sendMessage(player, msg);
if (config.getBoolean("Island.LevelRewards.Rewards", false)) {
// Reward the player for each level reached, message only for the highest, so we don't spam the chat
for (int i = (int) highestLevel + 1; i <= level; i++) {
LevelReward levelReward = skyblock.getRewardManager().getReward(i);
// Reward the player for each level reached, message only for the highest, so we don't spam the chat
for (int i = (int) highestLevel; i <= level; i++) {
LevelReward levelReward = skyblock.getRewardManager().getReward(i);
if (levelReward != null)
levelReward.give(player, skyblock, i);
if (levelReward != null)
levelReward.give(player, skyblock, i);
List<LevelReward> repeatRewards = skyblock.getRewardManager().getRepeatRewards(i);
LevelReward repeatReward = skyblock.getRewardManager().getRepeatReward(i);
if (!repeatRewards.isEmpty()) {
for (LevelReward reward : repeatRewards) {
reward.give(player, skyblock, i);
}
}
}
}
if (repeatReward != null)
repeatReward.give(player, skyblock, i);
if (config.getBoolean("Island.LevelRewards.Messages", false)) {
String msg = language.getString("Command.Island.Level.LevelUp.Message");
if (!Strings.isNullOrEmpty(msg)) {
msg = msg.replace("%level%", String.valueOf(level));
skyblock.getMessageManager().sendMessage(player, msg);
}
}
}
@ -226,10 +236,6 @@ public class IslandLevel {
}
}
public long getHighestLevel() {
return highestLevel;
}
public void setHighestLevel(long highestLevel) {
Config config = skyblock.getFileManager().getConfig(new File(new File(skyblock.getDataFolder().toString() + "/level-data"), ownerUUID.toString() + ".yml"));
FileConfiguration configLoad = config.getFileConfiguration();

View File

@ -2,47 +2,53 @@ package com.songoda.skyblock.island.reward;
import com.songoda.core.hooks.EconomyManager;
import com.songoda.skyblock.SkyBlock;
import com.songoda.skyblock.island.Island;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
public class LevelReward {
private List<String> commands;
private List<String> commands = new ArrayList<>();
private double money;
private double money = 0;
public LevelReward(List<String> commands, double money) {
this.commands = commands;
this.money = money;
private double islandBalance = 0;
public LevelReward() {
}
public void give(Player player, SkyBlock skyblock, long level) {
if (islandBalance > 0) {
Island island = skyblock.getIslandManager().getIsland(player);
island.addToBank(islandBalance);
}
if (money > 0)
EconomyManager.deposit(player, money);
if (!commands.isEmpty()) {
for (String cmd : commands) {
cmd = cmd.replace("%level%", "" + level);
cmd = cmd.replace("%player%", player.getName());
cmd = cmd.replace("%level%", String.valueOf(level))
.replace("%player%", player.getName())
.trim();
skyblock.getServer().dispatchCommand(skyblock.getConsole(), cmd);
}
}
}
public List<String> getCommands() {
return commands;
}
public void setCommands(List<String> commands) {
this.commands = commands;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
public void setIslandBalance(double islandBalance) {
this.islandBalance = islandBalance;
}
}

View File

@ -28,57 +28,69 @@ public class RewardManager {
this.repeatRewards.clear();
ConfigurationSection onceSection = configLoad.getConfigurationSection("Once");
for (String key : onceSection.getKeys(false)) {
long level;
try {
level = Long.parseLong(key);
} catch (NumberFormatException e) {
continue;
if (onceSection != null) {
for (String key : onceSection.getKeys(false)) {
long level;
try {
level = Long.parseLong(key);
} catch (NumberFormatException e) {
continue;
}
this.registeredRewards.put(level, loadReward("Once." + key));
}
ConfigurationSection section = onceSection.getConfigurationSection(key);
double money = section.getDouble("money", 0);
List<String> commands = section.contains("commands") ? section.getStringList("commands") : new ArrayList<>();
LevelReward levelReward = new LevelReward(commands, money);
this.registeredRewards.put(level, levelReward);
}
ConfigurationSection repeatSection = configLoad.getConfigurationSection("Repeat");
for (String key : repeatSection.getKeys(false)) {
long level;
try {
level = Long.parseLong(key);
} catch (NumberFormatException e) {
continue;
if (repeatSection != null) {
for (String key : repeatSection.getKeys(false)) {
long level;
try {
level = Long.parseLong(key);
} catch (NumberFormatException e) {
continue;
}
this.repeatRewards.put(level, loadReward("Repeat." + key));
}
ConfigurationSection section = repeatSection.getConfigurationSection(key);
double money = section.getDouble("money", 0);
List<String> commands = section.contains("commands") ? section.getStringList("commands") : new ArrayList<>();
LevelReward levelReward = new LevelReward(commands, money);
this.repeatRewards.put(level, levelReward);
}
}
private LevelReward loadReward(String path) {
final FileConfiguration config = skyBlock.getFileManager().getConfig(new File(skyBlock.getDataFolder(), "rewards.yml")).getFileConfiguration();
ConfigurationSection section = config.getConfigurationSection(path);
LevelReward levelReward = new LevelReward();
if (section == null) return levelReward;
double money = section.getDouble("money", 0);
levelReward.setMoney(money);
double islandVault = section.getDouble("island-balance", 0);
levelReward.setIslandBalance(islandVault);
List<String> commands = section.contains("commands") ? section.getStringList("commands") : new ArrayList<>();
levelReward.setCommands(commands);
return levelReward;
}
public LevelReward getReward(long level) {
return this.registeredRewards.getOrDefault(level, null);
}
public LevelReward getRepeatReward(long level) {
public List<LevelReward> getRepeatRewards(long level) {
List<LevelReward> levelRewards = new ArrayList<>();
for (long loopLevel : this.repeatRewards.keySet()) {
if (level % loopLevel == 0) {
return this.repeatRewards.get(loopLevel);
}
if (level % loopLevel == 0) levelRewards.add(this.repeatRewards.get(loopLevel));
}
return null;
return levelRewards;
}
public Map<Long, LevelReward> getRegisteredRewards() {

View File

@ -1,28 +1,6 @@
package com.songoda.skyblock.levelling.rework;
import java.io.File;
import java.text.NumberFormat;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
import com.songoda.core.compatibility.CompatibleMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import com.songoda.skyblock.SkyBlock;
import com.songoda.skyblock.api.event.island.IslandLevelChangeEvent;
import com.songoda.skyblock.blockscanner.BlockInfo;
@ -34,15 +12,30 @@ import com.songoda.skyblock.levelling.ChunkUtil;
import com.songoda.skyblock.levelling.rework.amount.AmountMaterialPair;
import com.songoda.skyblock.levelling.rework.amount.BlockAmount;
import com.songoda.skyblock.message.MessageManager;
import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.File;
import java.text.NumberFormat;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
public final class IslandScan extends BukkitRunnable {
private static final NumberFormat FORMATTER = NumberFormat.getInstance();;
private static final NumberFormat FORMATTER = NumberFormat.getInstance();
;
private final Set<Location> doubleBlocks;
private final Island island;
private final Map<CompatibleMaterial, BlockAmount> amounts;
private final Configuration config;
private final Configuration language;
private final int runEveryX;
private int totalScanned;
@ -53,8 +46,8 @@ public final class IslandScan extends BukkitRunnable {
if (island == null) throw new IllegalArgumentException("island cannot be null");
this.island = island;
this.amounts = new EnumMap<>(CompatibleMaterial.class);
this.config = SkyBlock.getInstance().getFileManager().getConfig(new File(SkyBlock.getInstance().getDataFolder(), "language.yml")).getFileConfiguration();
this.runEveryX = config.getInt("Command.Island.Level.Scanning.Progress.Display-Every-X-Scan");
this.language = SkyBlock.getInstance().getFileManager().getConfig(new File(SkyBlock.getInstance().getDataFolder(), "language.yml")).getFileConfiguration();
this.runEveryX = language.getInt("Command.Island.Level.Scanning.Progress.Display-Every-X-Scan");
this.doubleBlocks = new HashSet<>();
}
@ -107,7 +100,7 @@ public final class IslandScan extends BukkitRunnable {
int scanned = 0;
for (Iterator<BlockInfo> it = blocks.iterator(); it.hasNext();) {
for (Iterator<BlockInfo> it = blocks.iterator(); it.hasNext(); ) {
final BlockInfo info = it.next();
@ -140,23 +133,24 @@ public final class IslandScan extends BukkitRunnable {
SkyBlock.getInstance().getLevellingManager().stopScan(island);
}
if (config.getBoolean("Command.Island.Level.Scanning.Progress.Should-Display-Message") && executions == 1 || totalScanned == blocksSize || executions % runEveryX == 0) {
if (language.getBoolean("Command.Island.Level.Scanning.Progress.Should-Display-Message") && executions == 1 || totalScanned == blocksSize || executions % runEveryX == 0) {
final double percent = ((double) totalScanned / (double) blocksSize) * 100;
String message = config.getString("Command.Island.Level.Scanning.Progress.Message");
String message = language.getString("Command.Island.Level.Scanning.Progress.Message");
message = message.replace("%current_scanned_blocks%", String.valueOf(totalScanned));
message = message.replace("%max_blocks%", String.valueOf(blocksSize));
message = message.replace("%percent_whole%", String.valueOf((int) percent));
message = message.replace("%percent%", FORMATTER.format(percent));
final boolean displayComplete = totalScanned == blocksSize && config.getBoolean("Command.Island.Level.Scanning.Finished.Should-Display-Message");
final boolean displayComplete = totalScanned == blocksSize && language.getBoolean("Command.Island.Level.Scanning.Finished.Should-Display-Message");
final MessageManager messageManager = SkyBlock.getInstance().getMessageManager();
for (Player player : SkyBlock.getInstance().getIslandManager().getPlayersAtIsland(island)) {
messageManager.sendMessage(player, message);
if (displayComplete) messageManager.sendMessage(player, config.getString("Command.Island.Level.Scanning.Finished.Message"));
if (displayComplete)
messageManager.sendMessage(player, language.getString("Command.Island.Level.Scanning.Finished.Message"));
// Check for level ups
island.getLevel().checkLevelUp();

View File

@ -295,6 +295,9 @@ Island:
RequirePermission: false
Limit:
Enable: false
LevelRewards:
Rewards: false
Messages: false
Challenge:
# Enable or disable challenges. Configuration for challenges are in challenges.yml file
Enable: true

View File

@ -1,15 +1,19 @@
# FabledSkyblock Island level rewards
# FabledSkyblock Island Level Rewards
# Rewards that should run only once.
Once:
# Level
1:
# Vault balance
# Vault balance added directly to the owners account
money: 100
# Commands ran by console
commands:
- 'say %player% reached level 1!'
- 'say %player% reached level 1 on his island!'
2:
money: 100
commands:
- 'say %player% reached level 1!'
# Balance added to island bank
island-balance: 1000
# Rewards that repeat every x levels
Repeat:
5:
money: 1000