Skip to content

Commit

Permalink
added support for entity groups of the limits addon (#27)
Browse files Browse the repository at this point in the history
* added support for entity groups of the limits addon

* added entity groups documentation

* added new description local from #30
  • Loading branch information
weaondara committed Nov 12, 2020
1 parent 6be38bd commit 23b3fa6
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ The config.yml has the following sections:
* **Range Upgrade Tiers** - specify default Range upgrade tiers. Upgrading this will augment island protection size
* **Block Limits Upgrade Tier** - specify default Block Limits tiers. Upgrading this will augment the block limits of the limits addon
* **Entity Limits Upgrade Tier** - specify default Entity Limits tiers. Upgrading this will augment the entity limits of the limits addon
* **Entity Group Limits Upgrade Tier** - specify default Entity Group Limits tiers. Upgrading this will augment the entity group limits of the limits addon
* **Command Upgrade Tier** - specify default Command Tiers. You can link command to this upgrade.
* **GameMode** - ability to specify upgrade tiers for specific game mode.
* **Entity Icon** - This list the icons for Entity Upgrades
* **Entity Group Icon** - This list the icons for Entity Group Upgrades
* **Command Icon** - This list the icons for Command Upgrades

### Other Add-ons
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<spigot.version>1.16.3-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.14.0-SNAPSHOT</bentobox.version>
<level.version>2.0.0</level.version>
<limits.version>1.13.0-SNAPSHOT</limits.version>
<limits.version>1.15.0-SNAPSHOT</limits.version>
<vault.version>1.7</vault.version>

<revision>${build.version}-SNAPSHOT</revision>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/world/bentobox/upgrades/UpgradesAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import world.bentobox.upgrades.listeners.JoinPermCheckListener;
import world.bentobox.upgrades.upgrades.BlockLimitsUpgrade;
import world.bentobox.upgrades.upgrades.CommandUpgrade;
import world.bentobox.upgrades.upgrades.EntityGroupLimitsUpgrade;
import world.bentobox.upgrades.upgrades.EntityLimitsUpgrade;
import world.bentobox.upgrades.upgrades.RangeUpgrade;
import world.bentobox.level.Level;
Expand Down Expand Up @@ -97,6 +98,7 @@ public void onEnable() {

if (this.isLimitsProvided()) {
this.getSettings().getEntityLimitsUpgrade().forEach(ent -> this.registerUpgrade(new EntityLimitsUpgrade(this, ent)));
this.getSettings().getEntityGroupLimitsUpgrade().forEach(group -> this.registerUpgrade(new EntityGroupLimitsUpgrade(this, group)));
this.getSettings().getMaterialsLimitsUpgrade().forEach(mat -> this.registerUpgrade(new BlockLimitsUpgrade(this, mat)));
}

Expand Down
106 changes: 106 additions & 0 deletions src/main/java/world/bentobox/upgrades/UpgradesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.bukkit.Material;
import org.bukkit.World;
Expand Down Expand Up @@ -154,6 +155,42 @@ public Map<EntityType, List<Settings.UpgradeTier>> getAllEntityLimitsUpgradeTier

return tierList;
}

public Map<String, List<Settings.UpgradeTier>> getAllEntityGroupLimitsUpgradeTiers(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.UpgradeTier>> defaultTiers = this.addon.getSettings().getDefaultEntityGroupLimitsUpgradeTierMap();
Map<String, Map<String, Settings.UpgradeTier>> customAddonTiers = this.addon.getSettings().getAddonEntityGroupLimitsUpgradeTierMap(name);

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

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

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

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

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

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

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);
Expand Down Expand Up @@ -251,6 +288,27 @@ public Settings.UpgradeTier getEntityLimitsUpgradeTier(EntityType ent, int limit

return null;
}

public Settings.UpgradeTier getEntityGroupLimitsUpgradeTier(String group, int limitsLevel, World world) {
Map<String, List<Settings.UpgradeTier>> entTierList = this.getAllEntityGroupLimitsUpgradeTiers(world);

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

if (!entTierList.containsKey(group)) {
return null;
}

List<Settings.UpgradeTier> tierList = entTierList.get(group);

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

return null;
}

public Settings.CommandUpgradeTier getCommandUpgradeTier(String cmd, int cmdLevel, World world) {
Map<String, List<Settings.CommandUpgradeTier>> cmdTierList = this.getAllCommandUpgradeTiers(world);
Expand Down Expand Up @@ -359,6 +417,21 @@ public Map<String, Integer> getEntityLimitsUpgradeInfos(EntityType ent, int limi

return info;
}

public Map<String, Integer> getEntityGroupLimitsUpgradeInfos(String group, int limitsLevel, int islandLevel, int numberPeople, World world) {
Settings.UpgradeTier limitsUpgradeTier = this.getEntityGroupLimitsUpgradeTier(group, limitsLevel, world);
if (limitsUpgradeTier == null) {
return null;
}

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

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

return info;
}

public int getEntityLimitsPermissionLevel(EntityType ent, int limitsLevel, World world) {
Settings.UpgradeTier limitsUpgradeTier = this.getEntityLimitsUpgradeTier(ent, limitsLevel, world);
Expand All @@ -367,6 +440,14 @@ public int getEntityLimitsPermissionLevel(EntityType ent, int limitsLevel, World
return 0;
return limitsUpgradeTier.getPermissionLevel();
}

public int getEntityGroupLimitsPermissionLevel(String group, int limitsLevel, World world) {
Settings.UpgradeTier limitsUpgradeTier = this.getEntityGroupLimitsUpgradeTier(group, limitsLevel, world);

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

public String getEntityLimitsUpgradeTierName(EntityType ent, int limitsLevel, World world) {
Settings.UpgradeTier limitsUpgradeTier = this.getEntityLimitsUpgradeTier(ent, limitsLevel, world);
Expand All @@ -375,11 +456,24 @@ public String getEntityLimitsUpgradeTierName(EntityType ent, int limitsLevel, Wo
return null;
return limitsUpgradeTier.getTierName();
}

public String getEntityGroupLimitsUpgradeTierName(String group, int limitsLevel, World world) {
Settings.UpgradeTier limitsUpgradeTier = this.getEntityGroupLimitsUpgradeTier(group, limitsLevel, world);

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

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

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

public Map<String, Integer> getCommandUpgradeInfos(String cmd, int cmdLevel, int islandLevel, int numberPeople, World world) {
Settings.CommandUpgradeTier cmdUpgradeTier = this.getCommandUpgradeTier(cmd, cmdLevel, world);
Expand Down Expand Up @@ -442,6 +536,18 @@ public Map<EntityType, Integer> getEntityLimits(Island island) {
if (ibc != null) ibc.getEntityLimits().forEach(entityLimits::put);
return entityLimits;
}

public Map<String, Integer> getEntityGroupLimits(Island island) {
if (!this.addon.isLimitsProvided())
return Collections.emptyMap();

Map<String, Integer> entityGroupLimits = new HashMap<>(this.addon.getLimitsAddon().getSettings().getGroupLimits().values()
.stream().flatMap(e -> e.stream()).distinct()
.collect(Collectors.toMap(e -> e.getName(), e -> e.getLimit())));
IslandBlockCount ibc = this.addon.getLimitsAddon().getBlockLimitListener().getIsland(island.getUniqueId());
if (ibc != null) ibc.getEntityGroupLimits().forEach(entityGroupLimits::put);
return entityGroupLimits;
}

private UpgradesAddon addon;

Expand Down
88 changes: 88 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,11 +62,28 @@ else if (mat == null)
}
}

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

if (this.addon.getConfig().isSet("entity-limits-upgrade")) {
ConfigurationSection section = this.addon.getConfig().getConfigurationSection("entity-limits-upgrade");
this.entityLimitsUpgradeTierMap = this.loadEntityLimits(section, null);
}

if (this.addon.getConfig().isSet("entity-group-limits-upgrade")) {
ConfigurationSection section = this.addon.getConfig().getConfigurationSection("entity-group-limits-upgrade");
this.entityGroupLimitsUpgradeTierMap = this.loadEntityGroupLimits(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)) {
Expand Down Expand Up @@ -114,6 +131,11 @@ else if (mat == null)
this.customEntityLimitsUpgradeTierMap.computeIfAbsent(gameMode, k -> loadEntityLimits(lowSection, gameMode));
}

if (gameModeSection.isSet("entity-group-limits-upgrade")) {
ConfigurationSection lowSection = gameModeSection.getConfigurationSection("entity-group-limits-upgrade");
this.customEntityGroupLimitsUpgradeTierMap.computeIfAbsent(gameMode, k -> loadEntityGroupLimits(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 @@ -194,6 +216,35 @@ private Map<EntityType, Map<String, UpgradeTier>> loadEntityLimits(Configuration
return ents;
}

private Map<String, Map<String, UpgradeTier>> loadEntityGroupLimits(ConfigurationSection section, String gameMode) {
Map<String, Map<String, UpgradeTier>> ents = new HashMap<>();
for (String entitygroup : Objects.requireNonNull(section).getKeys(false)) {
Map<String, UpgradeTier> tier = new HashMap<>();
ConfigurationSection entSection = section.getConfigurationSection(entitygroup);
for (String key : Objects.requireNonNull(entSection).getKeys(false)) {
UpgradeTier newUpgrade = addUpgradeSection(entSection, key);

if (gameMode == null) {
if (this.maxEntityGroupLimitsUpgrade.get(entitygroup) == null || this.maxEntityGroupLimitsUpgrade.get(entitygroup) < newUpgrade.getMaxLevel())
this.maxEntityGroupLimitsUpgrade.put(entitygroup, newUpgrade.getMaxLevel());
} else {
if (this.customMaxEntityGroupLimitsUpgrade.get(gameMode) == null) {
Map<String, Integer> newMap = new HashMap<>();
newMap.put(entitygroup, newUpgrade.getMaxLevel());
this.customMaxEntityGroupLimitsUpgrade.put(gameMode, newMap);
} else {
if (this.customMaxEntityGroupLimitsUpgrade.get(gameMode).get(entitygroup) == null || this.customMaxEntityGroupLimitsUpgrade.get(gameMode).get(entitygroup) < newUpgrade.getMaxLevel())
this.customMaxEntityGroupLimitsUpgrade.get(gameMode).put(entitygroup, newUpgrade.getMaxLevel());
}
}

tier.put(key, newUpgrade);
}
ents.put(entitygroup, tier);
}
return ents;
}

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

Expand Down Expand Up @@ -354,21 +405,37 @@ public Material getEntityIcon(EntityType entity) {
return this.entityIcon.getOrDefault(entity, null);
}

public Material getEntityGroupIcon(String group) {
return this.entityGroupIcon.getOrDefault(group, null);
}

public int getMaxEntityLimitsUpgrade(EntityType entity, String addon) {
return this.customMaxEntityLimitsUpgrade.getOrDefault(addon, this.maxEntityLimitsUpgrade).getOrDefault(entity, 0);
}

public int getMaxEntityGroupLimitsUpgrade(String group, String addon) {
return this.customMaxEntityGroupLimitsUpgrade.getOrDefault(addon, this.maxEntityGroupLimitsUpgrade).getOrDefault(group, 0);
}

public Map<EntityType, Map<String, UpgradeTier>> getDefaultEntityLimitsUpgradeTierMap() {
return this.entityLimitsUpgradeTierMap;
}

public Map<String, Map<String, UpgradeTier>> getDefaultEntityGroupLimitsUpgradeTierMap() {
return this.entityGroupLimitsUpgradeTierMap;
}

/**
* @return the rangeUpgradeTierMap
*/
public Map<EntityType, Map<String, UpgradeTier>> getAddonEntityLimitsUpgradeTierMap(String addon) {
return this.customEntityLimitsUpgradeTierMap.getOrDefault(addon, Collections.emptyMap());
}

public Map<String, Map<String, UpgradeTier>> getAddonEntityGroupLimitsUpgradeTierMap(String addon) {
return this.customEntityGroupLimitsUpgradeTierMap.getOrDefault(addon, Collections.emptyMap());
}

public Set<EntityType> getEntityLimitsUpgrade() {
Set<EntityType> entity = new HashSet<>();

Expand All @@ -380,6 +447,17 @@ public Set<EntityType> getEntityLimitsUpgrade() {
return entity;
}

public Set<String> getEntityGroupLimitsUpgrade() {
Set<String> groups = new HashSet<>();

this.customEntityGroupLimitsUpgradeTierMap.forEach((addon, addonUpgrade) -> {
groups.addAll(addonUpgrade.keySet());
});
groups.addAll(this.entityGroupLimitsUpgradeTierMap.keySet());

return groups;
}

private EntityType getEntityType(String key) {
return Arrays.stream(EntityType.values()).filter(v -> v.name().equalsIgnoreCase(key)).findFirst().orElse(null);
}
Expand Down Expand Up @@ -442,14 +520,24 @@ public String getCommandName(String command) {

private Map<EntityType, Material> entityIcon = new EnumMap<>(EntityType.class);

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

private Map<EntityType, Integer> maxEntityLimitsUpgrade = new EnumMap<>(EntityType.class);

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

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

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

private Map<EntityType, Map<String, UpgradeTier>> entityLimitsUpgradeTierMap = new EnumMap<>(EntityType.class);

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

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

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

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

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

0 comments on commit 23b3fa6

Please sign in to comment.