Skip to content
This repository has been archived by the owner on Oct 7, 2022. It is now read-only.

Commit

Permalink
Merge pull request #50 from mtricht/feature/ctrlc
Browse files Browse the repository at this point in the history
Allow ctrl c as keybind. Fix callbacks firing more than once.
  • Loading branch information
mtricht authored Jan 23, 2020
2 parents 988115f + 28d9e75 commit 08abc3f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.tricht.lunaris.listeners;

import dev.tricht.lunaris.item.ItemGrabber;
import dev.tricht.lunaris.util.Platform;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
Expand All @@ -15,8 +16,6 @@ public class ClipboardListenerStack implements GameListener, ClipboardOwner {
private final ItemGrabber itemGrabber;
private final Robot robot;

private FlavorListener currentFlavorListener = null;

private static boolean isOwner = false;
private static ClipboardCallback callback;

Expand All @@ -36,15 +35,20 @@ public void addListener(GameListener listener) {
public void onEvent(GameEvent event) {
for (GameListener listenerEntry : keyListeners) {
if (listenerEntry.supports(event)) {
if (currentFlavorListener != null) {
Toolkit.getDefaultToolkit().getSystemClipboard().removeFlavorListener(currentFlavorListener);
}
if (!isOwner) {
flushClipboard();
}
callback = (String clipboard) -> {
event.setItem(itemGrabber.grab(clipboard));
listenerEntry.onEvent(event);
callback = new ClipboardCallback() {
private boolean fired = false;
@Override
public void onContentChange(String clipboard) {
if (clipboard.isEmpty() || fired) {
return;
}
event.setItem(itemGrabber.grab(clipboard));
listenerEntry.onEvent(event);
fired = true;
}
};
log.debug("Registered clipboard listener for " + listenerEntry.getClass().getName());
pressControlC(event);
Expand All @@ -65,6 +69,9 @@ public boolean supports(GameEvent event) {

private void registerClipboardListener() {
Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(flavorEvent -> {
if (!Platform.isPoeActive()) {
return;
}
String clipboardText = getClipboardText();
log.debug("Got clipboard text:" + clipboardText);
if (clipboardText != null && callback != null) {
Expand Down Expand Up @@ -99,6 +106,7 @@ private String getClipboardText() {
}

private void pressControlC(GameEvent event) {
HotKeyHandler.setPaused(true);
boolean wasControlAlreadyPressed = (event.getOriginalEvent().getModifiers() & 34) != 0;

if (!wasControlAlreadyPressed) {
Expand All @@ -111,6 +119,7 @@ private void pressControlC(GameEvent event) {
robot.keyRelease(KeyEvent.VK_CONTROL);
}
robot.keyRelease(KeyEvent.VK_C);
HotKeyHandler.setPaused(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.tricht.lunaris.util.Platform;
import dev.tricht.lunaris.tooltip.TooltipCreator;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
Expand All @@ -20,6 +21,9 @@
@Slf4j
public class HotKeyHandler implements NativeKeyListener, NativeMouseInputListener, NativeMouseWheelListener {

@Setter
private static boolean isPaused = false;

private Point position;

private ArrayList<GameListener> keyListeners = new ArrayList<>();
Expand All @@ -44,7 +48,10 @@ public void setRespondTo(ArrayList<KeyCombo> combos) {
}

public boolean shouldRespond(NativeKeyEvent event) {
for(KeyCombo combo : listenCombos) {
if (isPaused) {
return false;
}
for (KeyCombo combo : listenCombos) {
if (combo.matches(event)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public boolean supports(GameEvent event) {
}

private void displayItemTooltip(Item item) {
if (item == null || !item.hasPrice()) {
if (item == null || !item.exists()) {
return;
}

Expand All @@ -130,6 +130,10 @@ private Map<Element, int[]> createBaseItemTooltip(Item item) {
}

private void addPoeNinjaPrice(Item item, Map<Element, int[]> elements) {
if (item.getMeanPrice() == null) {
elements.put(new Label("No poe.ninja price available"), new int[]{1, elements.size() - 1});
return;
}
elements.put(new Price(item), new int[]{1, elements.size() - 1});
if (item.getMeanPrice().getReason() != null) {
elements.put(new Label("Reason: " + item.getMeanPrice().getReason()), new int[]{1, elements.size() - 1});
Expand Down

0 comments on commit 08abc3f

Please sign in to comment.