use of dev.frankheijden.insights.api.config.limits.Limit in project Insights by InsightsPlugin.
the class Limits method addLimit.
/**
* Adds the given limit to the data structure.
*/
public void addLimit(Limit limit) {
this.limits.add(limit);
this.limitsByFileName.put(limit.getFile().getName(), limit);
for (Material m : limit.getMaterials()) {
materialLimits.computeIfAbsent(m, k -> new TreeSet<>(comparingInt(l -> l.getLimit(k).getLimit()))).add(limit);
}
for (EntityType e : limit.getEntities()) {
entityLimits.computeIfAbsent(e, k -> new TreeSet<>(comparingInt(l -> l.getLimit(k).getLimit()))).add(limit);
}
}
use of dev.frankheijden.insights.api.config.limits.Limit in project Insights by InsightsPlugin.
the class PistonListener method handlePistonBlock.
private boolean handlePistonBlock(Block from, Block to) {
Optional<Region> regionOptional = plugin.getAddonManager().getRegion(to.getLocation());
// Always allow piston pushes within the same chunk
if (regionOptional.isEmpty() && BlockUtils.isSameChunk(from, to))
return false;
Material material = from.getType();
Optional<Limit> limitOptional = plugin.getLimits().getFirstLimit(material, limit -> true);
// If no limit is present, allow the block to be moved.
if (limitOptional.isEmpty())
return false;
Chunk chunk = to.getChunk();
UUID worldUid = chunk.getWorld().getUID();
long chunkKey = ChunkUtils.getKey(chunk);
boolean queued;
Optional<Storage> storageOptional;
if (regionOptional.isPresent()) {
String key = regionOptional.get().getKey();
queued = plugin.getAddonScanTracker().isQueued(key);
storageOptional = plugin.getAddonStorage().get(key);
} else {
queued = plugin.getWorldChunkScanTracker().isQueued(worldUid, chunkKey);
storageOptional = plugin.getWorldStorage().getWorld(worldUid).get(chunkKey);
}
// If the area is already queued, cancel the event and wait for the area to complete scanning.
if (queued)
return true;
// If the storage is not present, scan it & cancel the event.
if (storageOptional.isEmpty()) {
if (regionOptional.isPresent()) {
Region region = regionOptional.get();
plugin.getAddonScanTracker().add(region.getAddon());
List<ChunkPart> chunkParts = region.toChunkParts();
ScanTask.scan(plugin, chunkParts, chunkParts.size(), ScanOptions.scanOnly(), info -> {
}, storage -> {
plugin.getAddonScanTracker().remove(region.getAddon());
plugin.getAddonStorage().put(region.getKey(), storage);
});
} else {
plugin.getChunkContainerExecutor().submit(chunk);
}
return true;
}
// Else, the storage is present, and we can apply a limit.
Storage storage = storageOptional.get();
Limit limit = limitOptional.get();
LimitInfo limitInfo = limit.getLimit(material);
// Cache doesn't need to updated here just yet, needs to be done in MONITOR event phase.
return storage.count(limit, ScanObject.of(material)) + 1 > limitInfo.getLimit();
}
use of dev.frankheijden.insights.api.config.limits.Limit in project Insights by InsightsPlugin.
the class InsightsPlaceholderExpansion method onPlaceholderRequest.
@Override
public String onPlaceholderRequest(Player player, String identifier) {
if (identifier == null)
return "";
String[] args = identifier.split("_");
switch(args[0].toLowerCase(Locale.ENGLISH)) {
case "limits":
if (args.length < 3)
break;
String itemString = StringUtils.join(args, "_", 2).toUpperCase(Locale.ENGLISH);
final ScanObject<?> item;
try {
item = ScanObject.parse(itemString);
} catch (IllegalArgumentException ex) {
return "";
}
Location location = player.getLocation();
World world = location.getWorld();
UUID worldUid = world.getUID();
LimitEnvironment env = new LimitEnvironment(player, world.getName());
Optional<Limit> limitOptional = plugin.getLimits().getFirstLimit(item, env);
if (!limitOptional.isPresent())
break;
Limit limit = limitOptional.get();
switch(args[1].toLowerCase(Locale.ENGLISH)) {
case "name":
return limit.getLimit(item).getName();
case "max":
return String.valueOf(limit.getLimit(item).getLimit());
case "count":
Optional<Region> regionOptional = plugin.getAddonManager().getRegion(location);
Optional<Storage> storageOptional;
if (regionOptional.isPresent()) {
Region region = regionOptional.get();
storageOptional = plugin.getAddonStorage().get(region.getKey());
} else {
long chunkKey = ChunkUtils.getKey(location);
storageOptional = plugin.getWorldStorage().getWorld(worldUid).get(chunkKey);
}
return storageOptional.map(storage -> String.valueOf(storage.count(limit, item))).orElse("");
default:
break;
}
break;
default:
break;
}
return "";
}
Aggregations