Skip to content

Commit

Permalink
show limits in limit command
Browse files Browse the repository at this point in the history
  • Loading branch information
weaondara committed Apr 12, 2020
1 parent e0c208e commit 625bbe3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/world/bentobox/limits/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -84,6 +85,8 @@ public Settings(Limits addon) {
}
return null;
}).filter(e -> e != null).collect(Collectors.toSet());
if (entities.isEmpty())
continue;
EntityGroup group = new EntityGroup(name, entities, limit);
entities.forEach(e -> {
List<EntityGroup> groups = groupLimits.getOrDefault(e, new ArrayList());
Expand Down Expand Up @@ -111,6 +114,10 @@ public Map<EntityType, Integer> getLimits() {
public Map<EntityType, List<EntityGroup>> getGroupLimits() {
return groupLimits;
}

public List<EntityGroup> getGroupLimitDefinitions() {
return groupLimits.values().stream().flatMap(e -> e.stream()).distinct().collect(Collectors.toList());
}

/**
* @return the gameModes
Expand Down Expand Up @@ -145,5 +152,28 @@ public Set<EntityType> getTypes() {
public int getLimit() {
return limit;
}

@Override
public int hashCode()
{
int hash = 7;
hash = 83 * hash + Objects.hashCode(this.name);
return hash;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final EntityGroup other = (EntityGroup) obj;
if (!Objects.equals(this.name, other.name))
return false;
return true;
}
}
}
54 changes: 54 additions & 0 deletions src/main/java/world/bentobox/limits/commands/LimitPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.bukkit.entity.EntityType;

import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.stream.Collectors;

import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
Expand All @@ -19,6 +21,8 @@
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
import world.bentobox.limits.Limits;
import world.bentobox.limits.Settings;
import world.bentobox.limits.Settings.EntityGroup;
import world.bentobox.limits.objects.IslandBlockCount;

/**
Expand Down Expand Up @@ -133,6 +137,37 @@ public void showLimits(World world, User user, UUID target) {
"[limit]", String.valueOf(v)));
pb.item(pib.build());
});

// Entity group limits
List<Settings.EntityGroup> groupmap = addon.getSettings().getGroupLimitDefinitions();
// Merge in any permission-based limits
// if (ibc != null) ibc.getEntityLimits().forEach(map::put);
groupmap.forEach(v -> {
PanelItemBuilder pib = new PanelItemBuilder();
EntityType k = v.getTypes().iterator().next();
pib.name(v.getName() + " (" + v.getTypes().stream().map(e -> Util.prettifyText(e.toString())).collect(Collectors.joining(", ")) + ")");
Material m;
try {
if (E2M.containsKey(k)) {
m = E2M.get(k);
} else if (k.isAlive()) {
m = Material.valueOf(k.toString() + "_SPAWN_EGG");
} else {
// Regular material
m = Material.valueOf(k.toString());
}
} catch (Exception e) {
m = Material.BARRIER;
}
pib.icon(m);
long count = getCount(island, v);
String color = count >= v.getLimit() ? user.getTranslation("island.limits.max-color") : user.getTranslation("island.limits.regular-color");
pib.description(color
+ user.getTranslation("island.limits.block-limit-syntax",
TextVariables.NUMBER, String.valueOf(count),
"[limit]", String.valueOf(v.getLimit())));
pb.item(pib.build());
});
pb.build();
}

Expand All @@ -154,4 +189,23 @@ long getCount(Island island, EntityType ent) {
}
return count;
}

long getCount(Island island, EntityGroup group) {
long count = island.getWorld().getEntities().stream()
.filter(e -> group.contains(e.getType()))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
// Nether
if (addon.getPlugin().getIWM().isNetherIslands(island.getWorld()) && addon.getPlugin().getIWM().getNetherWorld(island.getWorld()) != null) {
count += addon.getPlugin().getIWM().getNetherWorld(island.getWorld()).getEntities().stream()
.filter(e -> group.contains(e.getType()))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
}
// End
if (addon.getPlugin().getIWM().isEndIslands(island.getWorld()) && addon.getPlugin().getIWM().getEndWorld(island.getWorld()) != null) {
count += addon.getPlugin().getIWM().getEndWorld(island.getWorld()).getEntities().stream()
.filter(e -> group.contains(e.getType()))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
}
return count;
}
}

0 comments on commit 625bbe3

Please sign in to comment.