Skip to content

feat: lock subsequent interactions while interacting #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void onEnable() {
ViewFrame viewFrame = ViewFrame.create(this)
.install(AnvilInputFeature.AnvilInput)
.with(new AnvilInputSample(), new Failing(), new SimplePagination(), new AutoUpdate())
.enableDebug()
.register();

getCommand("ifexample").setExecutor(new IFExampleCommandExecutor(viewFrame));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ public interface Viewer {
@ApiStatus.Internal
void setSwitching(boolean switching);

/**
* <b><i> This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided. </i></b>
*/
@ApiStatus.Internal
boolean isInteractionsLocked();

/**
* <b><i> This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided. </i></b>
*/
@ApiStatus.Internal
void setInteractionsLocked(boolean interactionsLocked);

/**
* <b><i> This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided. </i></b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import me.devnatan.inventoryframework.IFDebug;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;

Expand Down Expand Up @@ -115,6 +116,7 @@ public void execute(S subject) {
public void execute(PipelinePhase phase, S subject) {
final PipelineContext<S> context =
new PipelineContext<>(phase, interceptors.getOrDefault(phase, Collections.emptyList()));
IFDebug.debug("Pipeline call: %s", phase.getName());
context.execute(subject);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.devnatan.inventoryframework.pipeline;

import java.util.List;
import me.devnatan.inventoryframework.IFDebug;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;

Expand Down Expand Up @@ -40,6 +41,9 @@ private void loop() {
final PipelineInterceptor<S> nextInterceptor = safeInterceptors.get(pointer);
index = pointer + 1;

IFDebug.debug(
"Interception loop #%d: %s",
index, nextInterceptor.getClass().getName());
nextInterceptor.intercept(this, subject);
} while (true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public void removeComponent(@NotNull Component component) {

private IFSlotRenderContext createSlotRenderContext(@NotNull Component component, boolean force) {
if (!(this instanceof IFRenderContext))
throw new InventoryFrameworkException("Slot render context cannot be created from non-render parent");
throw new InventoryFrameworkException("Slot render context cannot be created from non-render parent: "
+ getClass().getName());

final IFRenderContext renderContext = (IFRenderContext) this;
final IFSlotRenderContext slotRender = getRoot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public final class BukkitViewer implements Viewer {
private IFRenderContext activeContext;
private Deque<IFRenderContext> previousContexts = new LinkedList<>();
private long lastInteractionInMillis;
private boolean switching;
private boolean switching, interactionsLocked;

public BukkitViewer(@NotNull Player player, IFRenderContext activeContext) {
this.player = player;
Expand Down Expand Up @@ -97,6 +97,16 @@ public void setSwitching(boolean switching) {
this.switching = switching;
}

@Override
public boolean isInteractionsLocked() {
return interactionsLocked || isBlockedByInteractionDelay();
}

@Override
public void setInteractionsLocked(boolean interactionsLocked) {
this.interactionsLocked = interactionsLocked;
}

@Override
public IFRenderContext getPreviousContext() {
return previousContexts.peekLast();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public void onInventoryClick(final InventoryClickEvent event) {
final Viewer viewer = viewFrame.getViewer(player);
if (viewer == null) return;

if (viewer.isInteractionsLocked()) {
IFDebug.debug("Interaction locked at listener level %s", player.getName());
event.setCancelled(true);
return;
}

viewer.setInteractionsLocked(true);

final IFRenderContext context = viewer.getActiveContext();
final Component clickedComponent = context.getComponentsAt(event.getRawSlot()).stream()
.filter(Component::isVisible)
Expand All @@ -55,6 +63,8 @@ public void onInventoryClick(final InventoryClickEvent event) {
.createSlotClickContext(event.getRawSlot(), viewer, clickedContainer, clickedComponent, event, false);

root.getPipeline().execute(StandardPipelinePhases.CLICK, clickContext);

viewer.setInteractionsLocked(false);
}

@SuppressWarnings("unused")
Expand All @@ -68,11 +78,15 @@ public void onInventoryClose(final InventoryCloseEvent event) {
final Viewer viewer = viewFrame.getViewer(player);
if (viewer == null) return;

viewer.setInteractionsLocked(true);

final IFRenderContext context = viewer.getCurrentContext();
final RootView root = context.getRoot();
final IFCloseContext closeContext = root.getElementFactory().createCloseContext(viewer, context);

root.getPipeline().execute(StandardPipelinePhases.CLOSE, closeContext);

viewer.setInteractionsLocked(false);
}

@SuppressWarnings("deprecation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static me.devnatan.inventoryframework.ViewConfig.CANCEL_ON_CLICK;

import me.devnatan.inventoryframework.IFDebug;
import me.devnatan.inventoryframework.VirtualView;
import me.devnatan.inventoryframework.context.SlotClickContext;
import org.bukkit.event.inventory.InventoryClickEvent;
Expand All @@ -15,7 +16,10 @@ public final class GlobalClickInterceptor implements PipelineInterceptor<Virtual

@Override
public void intercept(@NotNull PipelineContext<VirtualView> pipeline, @NotNull VirtualView subject) {
if (!(subject instanceof SlotClickContext)) return;
if (!(subject instanceof SlotClickContext)) {
IFDebug.debug("Cannot handle global click: not a SlotClickContext");
return;
}

final SlotClickContext context = (SlotClickContext) subject;
final InventoryClickEvent event = context.getClickOrigin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class MinestomViewer(
private val previousContexts: Deque<IFRenderContext> = LinkedList()
private var lastInteractionInMillis: Long = 0
private var switching = false
private var interactionsLocked = false

override fun getCurrentContext(): IFRenderContext =
if (isSwitching()) {
Expand Down Expand Up @@ -71,6 +72,12 @@ class MinestomViewer(
this.switching = switching
}

override fun isInteractionsLocked(): Boolean = interactionsLocked || isBlockedByInteractionDelay

override fun setInteractionsLocked(interactionsLocked: Boolean) {
this.interactionsLocked = interactionsLocked
}

override fun getPreviousContext(): IFRenderContext? = previousContexts.peekLast()

override fun setPreviousContext(previousContext: IFRenderContext) {
Expand Down
Loading