Skip to content

Commit

Permalink
Fix bugs in map keys and groups. Fix code smells.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Dec 25, 2021
1 parent 5140875 commit 4cff598
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
7 changes: 4 additions & 3 deletions src/main/java/world/bentobox/limits/Limits.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
public class Limits extends Addon {

private static final String LIMIT_NOT_SET = "Limit not set";
private Settings settings;
private List<GameModeAddon> gameModes;
private BlockLimitsListener blockLimitListener;
Expand Down Expand Up @@ -188,14 +189,14 @@ private int getCount(@Nullable User user, Material m, GameModeAddon gm) {
private String getLimit(@Nullable User user, Material m, GameModeAddon gm) {
Island is = gm.getIslands().getIsland(gm.getOverWorld(), user);
if (is == null) {
return "Limit not set";
return LIMIT_NOT_SET;
}
@Nullable IslandBlockCount ibc = getBlockLimitListener().getIsland(is.getUniqueId());
if (ibc == null) {
return "Limit not set";
return LIMIT_NOT_SET;
}
int limit = ibc.getBlockLimit(m);
return limit == -1 ? "Limit not set" : String.valueOf(limit);
return limit == -1 ? LIMIT_NOT_SET : String.valueOf(limit);
}

}
2 changes: 1 addition & 1 deletion src/main/java/world/bentobox/limits/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Map<EntityType, List<EntityGroup>> getGroupLimits() {
* @return the group limit definitions
*/
public List<EntityGroup> getGroupLimitDefinitions() {
return groupLimits.values().stream().flatMap(Collection::stream).distinct().collect(Collectors.toList());
return groupLimits.values().stream().flatMap(Collection::stream).distinct().toList();
}

/**
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/world/bentobox/limits/commands/LimitTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,20 @@ public LimitTab(Limits addon, IslandBlockCount ibc, Map<Material, Integer> matLi

private void addEntityGroupLimits(IslandBlockCount ibc, Island island) {
// Entity group limits
Map<EntityGroup, Integer> groupmap = addon.getSettings().getGroupLimitDefinitions().stream().collect(Collectors.toMap(e -> e, EntityGroup::getLimit));
Map<String, EntityGroup> groupbyname = groupmap.keySet().stream().collect(Collectors.toMap(EntityGroup::getName, e -> e));
Map<EntityGroup, Integer> groupMap = addon.getSettings().getGroupLimitDefinitions().stream().collect(Collectors.toMap(e -> e, EntityGroup::getLimit));
// Group by same loop up map
Map<String, EntityGroup> groupByName = groupMap.keySet().stream().collect(Collectors.toMap(EntityGroup::getName, e -> e));
// Merge in any permission-based limits
if (ibc == null) {
return;
}
ibc.getEntityGroupLimits().entrySet().stream()
.filter(e -> groupbyname.containsKey(e.getKey()))
.forEach(e -> groupmap.put(groupbyname.get(e.getKey()), e.getValue()));

ibc.getEntityGroupLimitsOffset().forEach((key, value) -> groupmap.put(groupbyname.get(key), (groupmap.getOrDefault(key, 0) + value)));
groupmap.forEach((v, limit) -> {
.filter(e -> groupByName.containsKey(e.getKey()))
.forEach(e -> groupMap.put(groupByName.get(e.getKey()), e.getValue()));
// Update the group map for each group limit offset. If the value already exists add it
ibc.getEntityGroupLimitsOffset().forEach((key, value) ->
groupMap.put(groupByName.get(key), (groupMap.getOrDefault(groupByName.get(key), 0) + value)));
groupMap.forEach((v, limit) -> {
PanelItemBuilder pib = new PanelItemBuilder();
EntityType k = v.getTypes().iterator().next();
pib.name(v.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ private void preSpawn(Entity entity, SpawnReason reason, Location l) {
justSpawned.add(entity.getUniqueId());
// Check for entities that need cleanup
switch (reason) {
case BUILD_IRONGOLEM -> detectIronGolem(l);
case BUILD_SNOWMAN -> detectSnowman(l);
case BUILD_WITHER -> {
detectWither(l);
// Create explosion
l.getWorld().createExplosion(l, 7F, true, true, entity);
}
default -> {
}
case BUILD_IRONGOLEM -> detectIronGolem(l);
case BUILD_SNOWMAN -> detectSnowman(l);
case BUILD_WITHER -> {
detectWither(l);
// Create explosion
l.getWorld().createExplosion(l, 7F, true, true, entity);
}
default -> {
// Do nothing
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void setEntityGroupLimit(String name, int limit) {
* @return limit or -1 for unlimited
*/
public int getEntityGroupLimit(String name) {
return entityGroupLimits.getOrDefault(name, -1) + getEntityLimitsOffset().getOrDefault(name, 0);
return entityGroupLimits.getOrDefault(name, -1) + getEntityGroupLimitsOffset().getOrDefault(name, 0);
}

/**
Expand Down

0 comments on commit 4cff598

Please sign in to comment.