diff --git a/src/com/github/ustc_zzzz/virtualchest/inventory/VirtualChestInventory.java b/src/com/github/ustc_zzzz/virtualchest/inventory/VirtualChestInventory.java index d379def..c220a65 100644 --- a/src/com/github/ustc_zzzz/virtualchest/inventory/VirtualChestInventory.java +++ b/src/com/github/ustc_zzzz/virtualchest/inventory/VirtualChestInventory.java @@ -141,13 +141,16 @@ private List> createListFromMultiMap(Multimap list, in private void updateInventory(Player player, Inventory inventory, String name) { - int index = 0; - for (Slot slot : inventory.slots()) + try (Timing ignored1 = VirtualChestTimings.updateAndRefreshMappings(name).startTiming()) { - Timing timing = VirtualChestTimings.updateAndRefreshMapping(name, index); - timing.startTimingIfSync(); - this.setItemInInventory(player, slot, index++, name); - timing.stopTimingIfSync(); + int index = 0; + for (Slot slot : inventory.slots()) + { + try (Timing ignored2 = VirtualChestTimings.updateAndRefreshMapping(name, index).startTiming()) + { + this.setItemInInventory(player, slot, index++, name); + } + } } } @@ -276,18 +279,23 @@ private void fireOpenEvent(InteractInventoryEvent.Open e) { Player player = optional.get(); UUID actionUUID = UUID.randomUUID(); + Container targetContainer = e.getTargetInventory(); Inventory targetInventory = targetContainer.first(); - Timing timing = VirtualChestTimings.updateAndRefreshMappings(name); + + VirtualChestActions actions = plugin.getVirtualChestActions(); Map context = ImmutableMap.of(VirtualChestActions.ACTION_UUID_KEY, actionUUID); - boolean record = recordManager.filter(name, VirtualChestInventory.this); - if (record) + if (recordManager.filter(name, VirtualChestInventory.this)) { recordManager.recordOpen(actionUUID, name, player); logger.debug("Player {} opens the chest GUI", player.getName()); + actions.submitCommands(player, parsedOpenAction.stream(), context, true); + } + else + { + actions.submitCommands(player, parsedOpenAction.stream(), context, false); } - plugin.getVirtualChestActions().submitCommands(player, parsedOpenAction.stream(), context, record); if (updateIntervalTick > 0) { @@ -295,9 +303,7 @@ boolean record = recordManager.filter(name, VirtualChestInventory.this); { if (dispatcher.isInventoryOpening(player, targetContainer)) { - timing.startTimingIfSync(); updateInventory(player, targetInventory, name); - timing.stopTimingIfSync(); } else { @@ -309,9 +315,7 @@ boolean record = recordManager.filter(name, VirtualChestInventory.this); plugin.getScriptManager().onOpeningInventory(player); - timing.startTimingIfSync(); updateInventory(player, targetInventory, name); - timing.stopTimingIfSync(); } } @@ -372,57 +376,57 @@ private void closeWhile(boolean shouldKeepInventoryOpen, Player player) private CompletableFuture runCommand(ClickInventoryEvent e, Player player, int slotIndex) { - Timing timing = VirtualChestTimings.updateAndRefreshMapping(name, slotIndex); - timing.startTimingIfSync(); - - String playerName = player.getName(); - ClickStatus status = new ClickStatus(e); - String keyString = slotIndexToKey(slotIndex); CompletableFuture future = new CompletableFuture<>(); - VirtualChestPermissionManager permissionManager = plugin.getPermissionManager(); - List items = VirtualChestInventory.this.items.get(slotIndex); - logger.debug("Player {} tries to click the chest GUI at {} with {}", playerName, keyString, status); int size = items.size(); List> setFutures = new ArrayList<>(size); List>> clearFutures = new ArrayList<>(size); - for (VirtualChestItem item : items) + + try (Timing ignored = VirtualChestTimings.updateAndRefreshMapping(name, slotIndex).startTiming()) { - UUID actionUUID = UUID.randomUUID(); - List list = item.getIgnoredPermissions(); - setFutures.add(permissionManager.setIgnored(player, actionUUID, list)); - clearFutures.add(() -> + String playerName = player.getName(); + ClickStatus status = new ClickStatus(e); + String keyString = slotIndexToKey(slotIndex); + VirtualChestPermissionManager permissionManager = plugin.getPermissionManager(); + + logger.debug("Player {} tries to click the chest GUI at {} with {}", playerName, keyString, status); + + for (VirtualChestItem item : items) { - if (future.isDone() || !item.matchRequirements(player, slotIndex, playerName)) + UUID actionUUID = UUID.randomUUID(); + List list = item.getIgnoredPermissions(); + setFutures.add(permissionManager.setIgnored(player, actionUUID, list)); + clearFutures.add(() -> { - return permissionManager.clearIgnored(player, actionUUID); - } + if (future.isDone() || !item.matchRequirements(player, slotIndex, playerName)) + { + return permissionManager.clearIgnored(player, actionUUID); + } - boolean record = recordManager.filter(name, VirtualChestInventory.this); - Optional optional = item.getAction(status); - if (record) - { - recordManager.recordSlotClick(actionUUID, name, slotIndex, status, player); - logger.debug("Player {} now submits an action: {}", playerName, actionUUID); - } + boolean record = recordManager.filter(name, VirtualChestInventory.this); + Optional optional = item.getAction(status); + if (record) + { + recordManager.recordSlotClick(actionUUID, name, slotIndex, status, player); + logger.debug("Player {} now submits an action: {}", playerName, actionUUID); + } - if (!optional.isPresent()) - { - future.complete(true); - return permissionManager.clearIgnored(player, actionUUID); - } + if (!optional.isPresent()) + { + future.complete(true); + return permissionManager.clearIgnored(player, actionUUID); + } - Tuple> tuple; - tuple = optional.get().runCommand(plugin, actionUUID, player, record); + Tuple> tuple; + tuple = optional.get().runCommand(plugin, actionUUID, player, record); - future.complete(tuple.getFirst()); - return tuple.getSecond().thenCompose(r -> permissionManager.clearIgnored(player, actionUUID)); - }); + future.complete(tuple.getFirst()); + return tuple.getSecond().thenCompose(r -> permissionManager.clearIgnored(player, actionUUID)); + }); + } } - timing.stopTimingIfSync(); - CompletableFuture.allOf(setFutures.toArray(new CompletableFuture[0])) .thenComposeAsync(v -> CompletableFuture.allOf(clearFutures.stream().map(Supplier::get) .toArray(CompletableFuture[]::new)), executorService).thenRun(() -> future.complete(true)); diff --git a/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItem.java b/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItem.java index b39dd43..aa48109 100644 --- a/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItem.java +++ b/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItem.java @@ -128,9 +128,7 @@ public static List getViewListOrSingletonList(DataQuery key, DataView public void fillInventory(Player player, Inventory inventory, int index, String name) { - Timing timing = VirtualChestTimings.setItemInInventory(name, index); - timing.startTimingIfSync(); - try + try (Timing ignored = VirtualChestTimings.setItemInInventory(name, index).startTiming()) { inventory.set(this.serializer.apply(player, this.serializedStack)); } @@ -139,19 +137,14 @@ public void fillInventory(Player player, Inventory inventory, int index, String String posString = VirtualChestInventory.slotIndexToKey(index); throw new InvalidDataException("Find error when generating item at " + posString, e); } - finally - { - timing.stopTimingIfSync(); - } } public boolean matchRequirements(Player player, int index, String name) { - Timing timing = VirtualChestTimings.checkRequirements(name, index); - timing.startTimingIfSync(); - boolean matchRequirements = this.plugin.getScriptManager().execute(player, this.requirements); - timing.stopTimingIfSync(); - return matchRequirements; + try (Timing ignored = VirtualChestTimings.checkRequirements(name, index).startTiming()) + { + return this.plugin.getScriptManager().execute(player, this.requirements); + } } public Optional getAction(VirtualChestInventory.ClickStatus status) diff --git a/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItemStackSerializer.java b/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItemStackSerializer.java index f603c7a..563865c 100644 --- a/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItemStackSerializer.java +++ b/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItemStackSerializer.java @@ -1,5 +1,6 @@ package com.github.ustc_zzzz.virtualchest.inventory.item; +import co.aikar.timings.Timing; import com.github.ustc_zzzz.virtualchest.VirtualChestPlugin; import com.github.ustc_zzzz.virtualchest.timings.VirtualChestTimings; import com.github.ustc_zzzz.virtualchest.unsafe.SpongeUnimplemented; @@ -153,8 +154,7 @@ public ItemStack apply(Player player, DataView view) throws InvalidDataException private ConfigurationNode applyPlaceholders(Player player, DataView view) { - VirtualChestTimings.applyPlaceholders().startTimingIfSync(); - try + try (Timing ignored = VirtualChestTimings.applyPlaceholders().startTiming()) { return this.applyPlaceholders(player, this.convertToConfigurationNode(view)); } @@ -166,16 +166,11 @@ private ConfigurationNode applyPlaceholders(Player player, DataView view) { throw new InvalidDataException(e); } - finally - { - VirtualChestTimings.applyPlaceholders().stopTimingIfSync(); - } } private ItemStack deserializeItemFrom(ConfigurationNode node) { - VirtualChestTimings.deserializeItem().startTimingIfSync(); - try + try (Timing ignored = VirtualChestTimings.deserializeItem().startTiming()) { ItemStack stack = Objects.requireNonNull(node.getValue(TypeToken.of(ItemStack.class))); for (Map.Entry entry : node.getChildrenMap().entrySet()) @@ -207,10 +202,6 @@ private ItemStack deserializeItemFrom(ConfigurationNode node) { throw new InvalidDataException(e); } - finally - { - VirtualChestTimings.deserializeItem().stopTimingIfSync(); - } } private static final class TextSerializer implements TypeSerializer diff --git a/src/com/github/ustc_zzzz/virtualchest/script/VirtualChestJavaScriptManager.java b/src/com/github/ustc_zzzz/virtualchest/script/VirtualChestJavaScriptManager.java index 138478f..3cf4cdf 100644 --- a/src/com/github/ustc_zzzz/virtualchest/script/VirtualChestJavaScriptManager.java +++ b/src/com/github/ustc_zzzz/virtualchest/script/VirtualChestJavaScriptManager.java @@ -1,5 +1,6 @@ package com.github.ustc_zzzz.virtualchest.script; +import co.aikar.timings.Timing; import com.github.ustc_zzzz.virtualchest.VirtualChestPlugin; import com.github.ustc_zzzz.virtualchest.placeholder.VirtualChestPlaceholderManager; import com.github.ustc_zzzz.virtualchest.timings.VirtualChestTimings; @@ -71,9 +72,8 @@ public boolean execute(Player player, Tuple tuple) { String scriptLiteral = tuple.getFirst(); ScriptContext context = this.getContext(player); - try + try (Timing ignored = VirtualChestTimings.executeRequirementScript().startTiming()) { - VirtualChestTimings.executeRequirementScript().startTimingIfSync(); return Boolean.valueOf(String.valueOf(tuple.getSecond().eval(context))); } catch (ScriptException e) @@ -86,7 +86,6 @@ public boolean execute(Player player, Tuple tuple) finally { this.removeContextAttributes(context); - VirtualChestTimings.executeRequirementScript().stopTimingIfSync(); } } @@ -99,18 +98,18 @@ private void removeContextAttributes(ScriptContext context) private ScriptContext getContext(Player player) { - VirtualChestTimings.prepareRequirementBindings().startTimingIfSync(); - - SimpleScriptContext context = this.tempContext; - VirtualChestPlaceholderManager placeholderManager = this.plugin.getPlaceholderManager(); - Function papi = s -> Text.of(placeholderManager.replacePlaceholder(player, s)).toPlain(); + try (Timing ignored = VirtualChestTimings.prepareRequirementBindings().startTiming()) + { + SimpleScriptContext context = this.tempContext; + VirtualChestPlaceholderManager placeholderManager = this.plugin.getPlaceholderManager(); + Function papi = s -> Text.of(placeholderManager.replacePlaceholder(player, s)).toPlain(); - context.setAttribute("papi", papi, ScriptContext.ENGINE_SCOPE); - context.setAttribute("player", player, ScriptContext.ENGINE_SCOPE); - context.setAttribute("tick", this.getTickFromOpeningInventory(player), ScriptContext.ENGINE_SCOPE); + context.setAttribute("papi", papi, ScriptContext.ENGINE_SCOPE); + context.setAttribute("player", player, ScriptContext.ENGINE_SCOPE); + context.setAttribute("tick", this.getTickFromOpeningInventory(player), ScriptContext.ENGINE_SCOPE); - VirtualChestTimings.prepareRequirementBindings().stopTimingIfSync(); - return context; + return context; + } } private Long getTickFromOpeningInventory(Player player)