Skip to content

Commit

Permalink
#7 Adding new type of upgrade: command upgrade
Browse files Browse the repository at this point in the history
Can now set upgrade which on completion run command
  • Loading branch information
Guillaume-Lebegue committed Jul 6, 2020
1 parent 855eb88 commit e02bd0d
Show file tree
Hide file tree
Showing 6 changed files with 403 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/main/java/world/bentobox/upgrades/UpgradesAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import world.bentobox.upgrades.listeners.IslandChangeListener;
import world.bentobox.upgrades.listeners.JoinPermCheckListener;
import world.bentobox.upgrades.upgrades.BlockLimitsUpgrade;
import world.bentobox.upgrades.upgrades.CommandUpgrade;
import world.bentobox.upgrades.upgrades.EntityLimitsUpgrade;
import world.bentobox.upgrades.upgrades.RangeUpgrade;
import world.bentobox.level.Level;
Expand Down Expand Up @@ -93,6 +94,8 @@ public void onEnable() {
this.getSettings().getMaterialsLimitsUpgrade().forEach(mat -> this.registerUpgrade(new BlockLimitsUpgrade(this, mat)));
}

this.getSettings().getCommandUpgrade().forEach(cmd -> this.registerUpgrade(new CommandUpgrade(this, cmd, this.getSettings().getCommandIcon(cmd))));

this.registerUpgrade(new RangeUpgrade(this));

this.registerListener(new IslandChangeListener(this));
Expand Down
109 changes: 109 additions & 0 deletions src/main/java/world/bentobox/upgrades/UpgradesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,42 @@ public Map<EntityType, List<Settings.UpgradeTier>> getAllEntityLimitsUpgradeTier
return tierList;
}

public Map<String, List<Settings.CommandUpgradeTier>> getAllCommandUpgradeTiers(World world) {
String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null);
if (name == null) {
return Collections.emptyMap();
}

Map<String, Map<String, Settings.CommandUpgradeTier>> defaultTiers = this.addon.getSettings().getDefaultCommandUpgradeTierMap();
Map<String, Map<String, Settings.CommandUpgradeTier>> customAddonTiers = this.addon.getSettings().getAddonCommandUpgradeTierMap(name);

Map<String, List<Settings.CommandUpgradeTier>> tierList = new HashMap<>();

if (customAddonTiers.isEmpty()) {
defaultTiers.forEach((cmd, tiers) -> tierList.put(cmd, new ArrayList<>(tiers.values())));
} else {
customAddonTiers.forEach((cmd, tiers) -> {
Set<String> uniqueIDSet = new HashSet<>(tiers.keySet());
if (defaultTiers.containsKey(cmd))
uniqueIDSet.addAll(defaultTiers.get(cmd).keySet());
List<Settings.CommandUpgradeTier> cmdTier = new ArrayList<>(uniqueIDSet.size());

uniqueIDSet.forEach(id -> cmdTier.add(tiers.getOrDefault(id, defaultTiers.get(cmd).get(id))));
tierList.put(cmd, cmdTier);
});

defaultTiers.forEach((cmd, tiers) -> tierList.putIfAbsent(cmd, new ArrayList<>(tiers.values())));
}

if (tierList.isEmpty()) {
return Collections.emptyMap();
}

tierList.forEach((cmd, tiers) -> tiers.sort(Comparator.comparingInt(Settings.UpgradeTier::getMaxLevel)));

return tierList;
}

public Settings.UpgradeTier getRangeUpgradeTier(int rangeLevel, World world) {
List<Settings.UpgradeTier> tierList = this.getAllRangeUpgradeTiers(world);

Expand Down Expand Up @@ -216,6 +252,27 @@ public Settings.UpgradeTier getEntityLimitsUpgradeTier(EntityType ent, int limit
return null;
}

public Settings.CommandUpgradeTier getCommandUpgradeTier(String cmd, int cmdLevel, World world) {
Map<String, List<Settings.CommandUpgradeTier>> cmdTierList = this.getAllCommandUpgradeTiers(world);

if (cmdTierList.isEmpty()) {
return null;
}

if (!cmdTierList.containsKey(cmd)) {
return null;
}

List<Settings.CommandUpgradeTier> tierList = cmdTierList.get(cmd);

for (int i = 0; i < tierList.size(); i++) {
if (cmdLevel <= tierList.get(i).getMaxLevel())
return tierList.get(i);
}

return null;
}

public Map<String, Integer> getRangeUpgradeInfos(int rangeLevel, int islandLevel, int numberPeople, World world) {
Settings.UpgradeTier rangeUpgradeTier = this.getRangeUpgradeTier(rangeLevel, world);

Expand Down Expand Up @@ -324,6 +381,58 @@ public int getEntityLimitsUpgradeMax(EntityType ent, World world) {
return this.addon.getSettings().getMaxEntityLimitsUpgrade(ent, name);
}

public Map<String, Integer> getCommandUpgradeInfos(String cmd, int cmdLevel, int islandLevel, int numberPeople, World world) {
Settings.CommandUpgradeTier cmdUpgradeTier = this.getCommandUpgradeTier(cmd, cmdLevel, world);
if (cmdUpgradeTier == null) {
return null;
}

Map<String, Integer> info = new HashMap<>();

info.put("islandMinLevel", (int) cmdUpgradeTier.calculateIslandMinLevel(cmdLevel, islandLevel, numberPeople));
info.put("vaultCost", (int) cmdUpgradeTier.calculateVaultCost(cmdLevel, islandLevel, numberPeople));
info.put("upgrade", (int) cmdUpgradeTier.calculateUpgrade(cmdLevel, islandLevel, numberPeople));

return info;
}

public int getCommandPermissionLevel(String cmd, int cmdLevel, World world) {
Settings.CommandUpgradeTier cmdUpgradeTier = this.getCommandUpgradeTier(cmd, cmdLevel, world);

if (cmdUpgradeTier == null)
return 0;
return cmdUpgradeTier.getPermissionLevel();
}

public String getCommandUpgradeTierName(String cmd, int cmdLevel, World world) {
Settings.CommandUpgradeTier cmdUpgradeTier = this.getCommandUpgradeTier(cmd, cmdLevel, world);

if (cmdUpgradeTier == null)
return null;
return cmdUpgradeTier.getTierName();
}

public int getCommandUpgradeMax(String cmd, World world) {
String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null);
return this.addon.getSettings().getMaxCommandUpgrade(cmd, name);
}

public List<String> getCommandList(String cmd, int cmdLevel, World world, String playerName) {
Settings.CommandUpgradeTier cmdUpgradeTier = this.getCommandUpgradeTier(cmd, cmdLevel, world);

if (cmdUpgradeTier == null)
return Collections.emptyList();
return cmdUpgradeTier.getCommandList(playerName, cmdLevel);
}

public Boolean isCommantConsole(String cmd, int cmdLevel, World world) {
Settings.CommandUpgradeTier cmdUpgradeTier = this.getCommandUpgradeTier(cmd, cmdLevel, world);

if (cmdUpgradeTier == null)
return false;
return cmdUpgradeTier.getConsole();
}

public Map<EntityType, Integer> getEntityLimits(Island island) {
if (!this.addon.isLimitsProvided())
return Collections.emptyMap();
Expand Down
183 changes: 183 additions & 0 deletions src/main/java/world/bentobox/upgrades/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ else if (mat == null)
this.entityLimitsUpgradeTierMap = this.loadEntityLimits(section, null);
}

if (this.addon.getConfig().isSet("command-icon")) {
ConfigurationSection section = this.addon.getConfig().getConfigurationSection("command-icon");
for (String commandId: Objects.requireNonNull(section).getKeys(false)) {
String material = section.getString(commandId);
Material mat = Material.getMaterial(material);
if (mat == null)
this.addon.logError("Config: Material " + material + " is not a valid material");
else
this.commandIcon.put(commandId, mat);
}
}

if (this.addon.getConfig().isSet("command-upgrade")) {
ConfigurationSection section = this.addon.getConfig().getConfigurationSection("command-upgrade");
this.commandUpgradeTierMap = this.loadCommand(section, null);
}

if (this.addon.getConfig().isSet("gamemodes")) {
ConfigurationSection section = this.addon.getConfig().getConfigurationSection("gamemodes");

Expand Down Expand Up @@ -89,6 +106,11 @@ else if (mat == null)
ConfigurationSection lowSection = gameModeSection.getConfigurationSection("entity-limits-upgrade");
this.customEntityLimitsUpgradeTierMap.computeIfAbsent(gameMode, k -> loadEntityLimits(lowSection, gameMode));
}

if (gameModeSection.isSet("command-upgrade")) {
ConfigurationSection lowSection = gameModeSection.getConfigurationSection("command-upgrade");
this.customCommandUpgradeTierMap.computeIfAbsent(gameMode, k -> loadCommand(lowSection, gameMode));
}
}
}

Expand Down Expand Up @@ -165,6 +187,48 @@ private Map<EntityType, Map<String, UpgradeTier>> loadEntityLimits(Configuration
return ents;
}

private Map<String, Map<String, CommandUpgradeTier>> loadCommand(ConfigurationSection section, String gamemode) {
Map<String, Map<String, CommandUpgradeTier>> commands = new HashMap<>();

for (String commandId: Objects.requireNonNull(section).getKeys(false)) {
if (this.commandIcon.containsKey(commandId)) {
String name = commandId;
Map<String, CommandUpgradeTier> tier = new HashMap<>();
ConfigurationSection cmdSection = section.getConfigurationSection(commandId);
for (String key: Objects.requireNonNull(cmdSection).getKeys(false)) {
if (key.equals("name")) {
name = cmdSection.getString(key);
} else {
CommandUpgradeTier newUpgrade = addCommandUpgradeSection(cmdSection, key);

if (gamemode == null) {
if (this.maxCommandUpgrade.get(key) == null || this.maxCommandUpgrade.get(key) < newUpgrade.getMaxLevel())
this.maxCommandUpgrade.put(key, newUpgrade.getMaxLevel());
} else {
if (this.customMaxCommandUpgrade.get(gamemode) == null) {
Map<String, Integer> newMap = new HashMap<>();
newMap.put(key, newUpgrade.getMaxLevel());
this.customMaxCommandUpgrade.put(gamemode, newMap);
} else {
if (this.customMaxCommandUpgrade.get(gamemode).get(key) == null || this.customMaxCommandUpgrade.get(gamemode).get(key) < newUpgrade.getMaxLevel())
this.customMaxCommandUpgrade.get(gamemode).put(key, newUpgrade.getMaxLevel());
}
}

tier.put(key, newUpgrade);
}
}
if (!this.commandName.containsKey(commandId) || name != commandId)
this.commandName.put(commandId, name);
commands.put(commandId, tier);
} else {
this.addon.logError("Command " + commandId + " is missing a corresponding icon. Skipping...");
}
}

return commands;
}

@NonNull
private UpgradeTier addUpgradeSection(ConfigurationSection section, String key) {
ConfigurationSection tierSection = section.getConfigurationSection(key);
Expand All @@ -185,6 +249,43 @@ private UpgradeTier addUpgradeSection(ConfigurationSection section, String key)

if (tierSection.isSet("permission-level"))
upgradeTier.setPermissionLevel(tierSection.getInt("permission-level"));
else
upgradeTier.setPermissionLevel(0);

return upgradeTier;

}

@NonNull
private CommandUpgradeTier addCommandUpgradeSection(ConfigurationSection section, String key) {
ConfigurationSection tierSection = section.getConfigurationSection(key);
CommandUpgradeTier upgradeTier = new CommandUpgradeTier(key);
upgradeTier.setTierName(tierSection.getName());
upgradeTier.setMaxLevel(tierSection.getInt("max-level"));
upgradeTier.setUpgrade(parse("0", upgradeTier.getExpressionVariable()));

if (tierSection.isSet("island-min-level"))
upgradeTier.setIslandMinLevel(parse(tierSection.getString("island-min-level"), upgradeTier.getExpressionVariable()));
else
upgradeTier.setIslandMinLevel(parse("0", upgradeTier.getExpressionVariable()));

if (tierSection.isSet("vault-cost"))
upgradeTier.setVaultCost(parse(tierSection.getString("vault-cost"), upgradeTier.getExpressionVariable()));
else
upgradeTier.setVaultCost(parse("0", upgradeTier.getExpressionVariable()));

if (tierSection.isSet("permission-level"))
upgradeTier.setPermissionLevel(tierSection.getInt("permission-level"));
else
upgradeTier.setPermissionLevel(0);

if (tierSection.isSet("console") && tierSection.isBoolean("console"))
upgradeTier.setConsole(tierSection.getBoolean("console"));
else
upgradeTier.setConsole(false);

if (tierSection.isSet("command"))
upgradeTier.setCommandList(tierSection.getStringList("command"));

return upgradeTier;

Expand Down Expand Up @@ -271,6 +372,40 @@ public Set<EntityType> getEntityLimitsUpgrade() {
private EntityType getEntityType(String key) {
return Arrays.stream(EntityType.values()).filter(v -> v.name().equalsIgnoreCase(key)).findFirst().orElse(null);
}

public int getMaxCommandUpgrade(String commandUpgrade, String addon) {
return this.customMaxCommandUpgrade.getOrDefault(addon, this.maxCommandUpgrade).getOrDefault(commandUpgrade, 0);
}

public Map<String, Map<String, CommandUpgradeTier>> getDefaultCommandUpgradeTierMap() {
return this.commandUpgradeTierMap;
}

/**
* @return the rangeUpgradeTierMap
*/
public Map<String, Map<String, CommandUpgradeTier>> getAddonCommandUpgradeTierMap(String addon) {
return this.customCommandUpgradeTierMap.getOrDefault(addon, Collections.emptyMap());
}

public Set<String> getCommandUpgrade() {
Set<String> command = new HashSet<>();

this.customCommandUpgradeTierMap.forEach((addon, addonUpgrade) -> {
command.addAll(addonUpgrade.keySet());
});
command.addAll(this.commandUpgradeTierMap.keySet());

return command;
}

public Material getCommandIcon(String command) {
return this.commandIcon.getOrDefault(command, null);
}

public String getCommandName(String command) {
return this.commandName.get(command);
}

private UpgradesAddon addon;

Expand Down Expand Up @@ -302,6 +437,18 @@ private EntityType getEntityType(String key) {

private Map<String, Map<EntityType, Map<String, UpgradeTier>>> customEntityLimitsUpgradeTierMap = new HashMap<>();

private Map<String, Integer> maxCommandUpgrade = new HashMap<>();

private Map<String, Map<String, Integer>> customMaxCommandUpgrade = new HashMap<>();

private Map<String, Map<String, CommandUpgradeTier>> commandUpgradeTierMap = new HashMap<>();

private Map<String, Map<String, Map<String, CommandUpgradeTier>>> customCommandUpgradeTierMap = new HashMap<>();

private Map<String, Material> commandIcon = new HashMap<>();

private Map<String, String> commandName = new HashMap<>();

// ------------------------------------------------------------------
// Section: Private object
// ------------------------------------------------------------------
Expand Down Expand Up @@ -468,6 +615,42 @@ public double calculateVaultCost(double level, double islandLevel, double number
private Map<String, Double> expressionVariables;
}

public class CommandUpgradeTier extends UpgradeTier {

public CommandUpgradeTier(String id) {
super(id);
this.commandList = new ArrayList<String>();
}

public void setConsole(Boolean console) {
this.console = console;
}

public Boolean getConsole() {
return this.console;
}

public void setCommandList(List<String> commandsList) {
this.commandList = commandsList;
}

public List<String> getCommandList(String playerName, int level) {
List<String> formatedList = new ArrayList<String>(this.commandList.size());

this.commandList.forEach(cmd -> {
String fcmd = cmd.replace("[player]", playerName)
.replace("[level]", Integer.toString(level));
formatedList.add(fcmd);
});
return formatedList;
}

private List<String> commandList;

private Boolean console;

}


// -------------------------------------------------------------------------
// Section: Arithmetic expressions Parser
Expand Down

0 comments on commit e02bd0d

Please sign in to comment.