Skip to content

Commit

Permalink
Make all the timings based on try-with-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Feb 16, 2019
1 parent dd272a7 commit 3d04b7a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,16 @@ private <T> List<List<T>> createListFromMultiMap(Multimap<SlotIndex, T> list, in

private void updateInventory(Player player, Inventory inventory, String name)
{
int index = 0;
for (Slot slot : inventory.<Slot>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.<Slot>slots())
{
try (Timing ignored2 = VirtualChestTimings.updateAndRefreshMapping(name, index).startTiming())
{
this.setItemInInventory(player, slot, index++, name);
}
}
}
}

Expand Down Expand Up @@ -276,28 +279,31 @@ 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<String, Object> 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)
{
Task.Builder builder = Sponge.getScheduler().createTaskBuilder().execute(task ->
{
if (dispatcher.isInventoryOpening(player, targetContainer))
{
timing.startTimingIfSync();
updateInventory(player, targetInventory, name);
timing.stopTimingIfSync();
}
else
{
Expand All @@ -309,9 +315,7 @@ boolean record = recordManager.filter(name, VirtualChestInventory.this);

plugin.getScriptManager().onOpeningInventory(player);

timing.startTimingIfSync();
updateInventory(player, targetInventory, name);
timing.stopTimingIfSync();
}
}

Expand Down Expand Up @@ -372,57 +376,57 @@ private void closeWhile(boolean shouldKeepInventoryOpen, Player player)

private CompletableFuture<Boolean> 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<Boolean> future = new CompletableFuture<>();
VirtualChestPermissionManager permissionManager = plugin.getPermissionManager();

List<VirtualChestItem> 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<CompletableFuture<?>> setFutures = new ArrayList<>(size);
List<Supplier<CompletableFuture<?>>> clearFutures = new ArrayList<>(size);
for (VirtualChestItem item : items)

try (Timing ignored = VirtualChestTimings.updateAndRefreshMapping(name, slotIndex).startTiming())
{
UUID actionUUID = UUID.randomUUID();
List<String> 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<String> 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<VirtualChestActionDispatcher> 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<VirtualChestActionDispatcher> 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<Boolean, CompletableFuture<CommandResult>> tuple;
tuple = optional.get().runCommand(plugin, actionUUID, player, record);
Tuple<Boolean, CompletableFuture<CommandResult>> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ public static List<DataView> 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));
}
Expand All @@ -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<VirtualChestActionDispatcher> getAction(VirtualChestInventory.ClickStatus status)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
Expand All @@ -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<Object, ? extends ConfigurationNode> entry : node.getChildrenMap().entrySet())
Expand Down Expand Up @@ -207,10 +202,6 @@ private ItemStack deserializeItemFrom(ConfigurationNode node)
{
throw new InvalidDataException(e);
}
finally
{
VirtualChestTimings.deserializeItem().stopTimingIfSync();
}
}

private static final class TextSerializer implements TypeSerializer<Text>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -71,9 +72,8 @@ public boolean execute(Player player, Tuple<String, CompiledScript> 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)
Expand All @@ -86,7 +86,6 @@ public boolean execute(Player player, Tuple<String, CompiledScript> tuple)
finally
{
this.removeContextAttributes(context);
VirtualChestTimings.executeRequirementScript().stopTimingIfSync();
}
}

Expand All @@ -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<String, Object> 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<String, Object> 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)
Expand Down

0 comments on commit 3d04b7a

Please sign in to comment.