use of dev.frankheijden.insights.api.config.LimitEnvironment in project Insights by InsightsPlugin.
the class InsightsListener method handleAddition.
protected boolean handleAddition(Player player, Location location, ScanObject<?> item, int delta, boolean included) {
Optional<Region> regionOptional = plugin.getAddonManager().getRegion(location);
var chunk = location.getChunk();
var world = location.getWorld();
UUID worldUid = world.getUID();
long chunkKey = ChunkUtils.getKey(chunk);
boolean queued;
String area;
LimitEnvironment env;
if (regionOptional.isPresent()) {
var region = regionOptional.get();
queued = plugin.getAddonScanTracker().isQueued(region.getKey());
area = plugin.getAddonManager().getAddon(region.getAddon()).getAreaName();
env = new LimitEnvironment(player, world.getName(), region.getAddon());
} else {
queued = plugin.getWorldChunkScanTracker().isQueued(worldUid, chunkKey);
area = "chunk";
env = new LimitEnvironment(player, world.getName());
}
if (queued) {
if (plugin.getSettings().canReceiveAreaScanNotifications(player)) {
plugin.getMessages().getMessage(Messages.Key.AREA_SCAN_QUEUED).addTemplates(Messages.tagOf("area", area)).sendTo(player);
}
return true;
}
// Get the first (smallest) limit for the specific user (bypass permissions taken into account)
Optional<Limit> limitOptional = plugin.getLimits().getFirstLimit(item, env);
if (limitOptional.isEmpty())
return false;
var limit = limitOptional.get();
var limitInfo = limit.getLimit(item);
if (regionOptional.isEmpty() && limit.getSettings().isDisallowedPlacementOutsideRegion()) {
plugin.getMessages().getMessage(Messages.Key.LIMIT_DISALLOWED_PLACEMENT).addTemplates(TagResolver.resolver(Messages.tagOf("name", limitInfo.getName()), Messages.tagOf("area", area))).sendTo(player);
return true;
}
Consumer<Storage> storageConsumer = storage -> {
// Only iff the block was included in the chunk AND its not a cuboid/area scan.
if (included && regionOptional.isEmpty()) {
storage.modify(item, -delta);
}
// Notify the user scan completed
if (plugin.getSettings().canReceiveAreaScanNotifications(player)) {
plugin.getMessages().getMessage(Messages.Key.AREA_SCAN_COMPLETED).sendTo(player);
}
};
Optional<Storage> storageOptional;
if (regionOptional.isPresent()) {
storageOptional = handleAddonAddition(player, regionOptional.get(), storageConsumer);
} else {
storageOptional = handleChunkAddition(player, chunk, storageConsumer);
}
// If the storage is not present, cancel.
if (storageOptional.isEmpty())
return true;
var storage = storageOptional.get();
long count = storage.count(limit, item);
// If count is beyond limit, act
if (count + delta > limitInfo.getLimit()) {
plugin.getMessages().getMessage(Messages.Key.LIMIT_REACHED).addTemplates(Messages.tagOf("limit", StringUtils.pretty(limitInfo.getLimit())), Messages.tagOf("name", limitInfo.getName()), Messages.tagOf("area", area)).sendTo(player);
return true;
}
return false;
}
use of dev.frankheijden.insights.api.config.LimitEnvironment in project Insights by InsightsPlugin.
the class InsightsListener method handleRemoval.
protected void handleRemoval(Player player, Location location, ScanObject<?> item, int delta, boolean included) {
Optional<Region> regionOptional = plugin.getAddonManager().getRegion(location);
Chunk chunk = location.getChunk();
World world = location.getWorld();
UUID worldUid = world.getUID();
long chunkKey = ChunkUtils.getKey(chunk);
UUID uuid = player.getUniqueId();
boolean queued;
LimitEnvironment env;
Optional<Storage> storageOptional;
if (regionOptional.isPresent()) {
Region region = regionOptional.get();
queued = plugin.getAddonScanTracker().isQueued(region.getKey());
env = new LimitEnvironment(player, world.getName(), region.getAddon());
storageOptional = plugin.getAddonStorage().get(region.getKey());
} else {
queued = plugin.getWorldChunkScanTracker().isQueued(worldUid, chunkKey);
env = new LimitEnvironment(player, world.getName());
storageOptional = plugin.getWorldStorage().getWorld(worldUid).get(chunkKey);
}
// Modify the area to account for the broken block.
storageOptional.ifPresent(storage -> storage.modify(item, -delta));
// Notify the user (if they have permission)
if (player.hasPermission("insights.notifications")) {
// If the area is queued, stop check here (notification will be displayed when it completes).
if (queued)
return;
// Get the first (smallest) limit for the specific user (bypass permissions taken into account)
Optional<Limit> limitOptional = plugin.getLimits().getFirstLimit(item, env);
if (limitOptional.isEmpty())
return;
Limit limit = limitOptional.get();
LimitInfo limitInfo = limit.getLimit(item);
// Create a runnable for the notification.
Consumer<Storage> notification = storage -> {
long count = storage.count(limit, item);
float progress = (float) count / limitInfo.getLimit();
plugin.getNotifications().getCachedProgress(uuid, Messages.Key.LIMIT_NOTIFICATION).progress(progress).add(player).create().addTemplates(Messages.tagOf("name", limitInfo.getName()), Messages.tagOf("count", StringUtils.pretty(count)), Messages.tagOf("limit", StringUtils.pretty(limitInfo.getLimit()))).send();
};
// If the data is already stored, send the notification immediately.
if (storageOptional.isPresent()) {
notification.accept(storageOptional.get());
return;
}
// Else, we need to scan the area first.
Consumer<Storage> storageConsumer = storage -> {
// Only if we're not scanning a cuboid (iff cuboid, the block is already removed from the chunk)
if (included && regionOptional.isEmpty())
storage.modify(item, -delta);
// Notify the user
notification.accept(storage);
};
if (regionOptional.isPresent()) {
scanRegion(player, regionOptional.get(), storageConsumer);
} else {
plugin.getChunkContainerExecutor().submit(chunk).thenAccept(storageConsumer).exceptionally(th -> {
plugin.getLogger().log(Level.SEVERE, th, th::getMessage);
return null;
});
}
}
}
use of dev.frankheijden.insights.api.config.LimitEnvironment in project Insights by InsightsPlugin.
the class InsightsListener method evaluateAddition.
protected void evaluateAddition(Player player, Location location, ScanObject<?> item, int delta) {
Optional<Region> regionOptional = plugin.getAddonManager().getRegion(location);
World world = location.getWorld();
long chunkKey = ChunkUtils.getKey(location);
UUID uuid = player.getUniqueId();
LimitEnvironment env;
Optional<Storage> storageOptional;
if (regionOptional.isPresent()) {
Region region = regionOptional.get();
env = new LimitEnvironment(player, world.getName(), region.getAddon());
storageOptional = plugin.getAddonStorage().get(region.getKey());
} else {
env = new LimitEnvironment(player, world.getName());
storageOptional = plugin.getWorldStorage().getWorld(world.getUID()).get(chunkKey);
}
if (storageOptional.isEmpty())
return;
Storage storage = storageOptional.get();
// If limit is not present, stop here
Optional<Limit> limitOptional = plugin.getLimits().getFirstLimit(item, env);
if (limitOptional.isEmpty())
return;
Limit limit = limitOptional.get();
LimitInfo limitInfo = limit.getLimit(item);
long count = storage.count(limit, item);
// Notify the user (if they have permission)
if (player.hasPermission("insights.notifications")) {
float progress = (float) (count + delta) / limitInfo.getLimit();
plugin.getNotifications().getCachedProgress(uuid, Messages.Key.LIMIT_NOTIFICATION).progress(progress).add(player).create().addTemplates(Messages.tagOf("name", limitInfo.getName()), Messages.tagOf("count", StringUtils.pretty(count + delta)), Messages.tagOf("limit", StringUtils.pretty(limitInfo.getLimit()))).send();
}
}
use of dev.frankheijden.insights.api.config.LimitEnvironment 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