Fix bugs in map keys and groups. Fix code smells.
This commit is contained in:
parent
51408751a6
commit
4cff598aee
|
@ -27,6 +27,7 @@ import world.bentobox.limits.objects.IslandBlockCount;
|
|||
*/
|
||||
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;
|
||||
|
@ -188,14 +189,14 @@ public class Limits extends Addon {
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -119,7 +119,7 @@ public class Settings {
|
|||
* @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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -92,18 +92,20 @@ public class LimitTab implements Tab {
|
|||
|
||||
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());
|
||||
|
|
|
@ -196,15 +196,16 @@ public class EntityLimitListener implements Listener {
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -270,7 +270,7 @@ public class IslandBlockCount implements DataObject {
|
|||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue