use of dev.frankheijden.insights.api.concurrent.storage.Storage in project Insights by InsightsPlugin.
the class InsightsListener method handleChunkAddition.
private Optional<Storage> handleChunkAddition(Player player, Chunk chunk, Consumer<Storage> storageConsumer) {
UUID worldUid = chunk.getWorld().getUID();
long chunkKey = ChunkUtils.getKey(chunk);
WorldStorage worldStorage = plugin.getWorldStorage();
ChunkStorage chunkStorage = worldStorage.getWorld(worldUid);
Optional<Storage> storageOptional = chunkStorage.get(chunkKey);
// If the chunk is not known
if (storageOptional.isEmpty()) {
// Notify the user scan started
if (plugin.getSettings().canReceiveAreaScanNotifications(player)) {
plugin.getMessages().getMessage(Messages.Key.AREA_SCAN_STARTED).addTemplates(Messages.tagOf("area", "chunk")).sendTo(player);
}
// Submit the chunk for scanning
plugin.getChunkContainerExecutor().submit(chunk).thenAccept(storageConsumer).exceptionally(th -> {
plugin.getLogger().log(Level.SEVERE, th, th::getMessage);
return null;
});
}
return storageOptional;
}
use of dev.frankheijden.insights.api.concurrent.storage.Storage 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.concurrent.storage.Storage 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 "";
}
use of dev.frankheijden.insights.api.concurrent.storage.Storage in project Insights by InsightsPlugin.
the class InsightsListener method handleModification.
protected void handleModification(Location location, Consumer<Storage> storageConsumer) {
UUID worldUid = location.getWorld().getUID();
long chunkKey = ChunkUtils.getKey(location);
plugin.getWorldStorage().getWorld(worldUid).get(chunkKey).ifPresent(storageConsumer);
plugin.getAddonManager().getRegion(location).flatMap(region -> plugin.getAddonStorage().get(region.getKey())).ifPresent(storageConsumer);
}
Aggregations