aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/FlipHandler.java
diff options
context:
space:
mode:
authormatthias-luger <58751503+matthias-luger@users.noreply.github.com>2023-02-17 17:32:26 +0100
committerGitHub <noreply@github.com>2023-02-17 17:32:26 +0100
commit8f3bce7d6fe41e85f2654f7557bbb564305e58b5 (patch)
tree4bae9237e3c0b87bf27e5f590f4c0ca91b145cab /src/main/java/de/torui/coflsky/FlipHandler.java
parente43d5685fdf0d436d5836d06c5d3f15da674e7df (diff)
downloadCOFL-8f3bce7d6fe41e85f2654f7557bbb564305e58b5.tar.gz
COFL-8f3bce7d6fe41e85f2654f7557bbb564305e58b5.tar.bz2
COFL-8f3bce7d6fe41e85f2654f7557bbb564305e58b5.zip
Add TFM Purchase UI to Mod (#78)
* Add TFM Purchase UI to Mod * fix up opening flips by hotkey fix up flip message in tfm ui * readded check if the auction should be drawn mod doesnt crash anymore if a non flip auction is opened * added /cofl set gui command changed gui title * added /fc toggle command dont store toggle locally * rename purchaseOverlay config removed unused code renamed gui command to /cofl set ahbuygui * fix message not available if flip opened by mouse fix old message shown for non flip auctions * added /cofl setGui command added cofl purchase gui * Add sound on flip * Fixed new flip sound Optimized cofl gui Removed flickering of default mc gui Move mouse to middle on screen open * remove unused code * remove debug code * don't open guis if not in skyblock * fix sound being null * fix bed flips fix opening /cofl blocked * remove logging * Rewrote cofl gui to render before the chest gui opens * - fix bed flips - invalidate best flips after open * - fix scrolling * - fix description not updating - case insensitive commands - fix up , at the end of the message * - add gui if item was already bought - fix bug that invalidated too many flips - added missing "break" * fix merge error * add .vscode to gitignore formatting * fix formatting issue * - remove comment - remove emty line
Diffstat (limited to 'src/main/java/de/torui/coflsky/FlipHandler.java')
-rw-r--r--src/main/java/de/torui/coflsky/FlipHandler.java235
1 files changed, 120 insertions, 115 deletions
diff --git a/src/main/java/de/torui/coflsky/FlipHandler.java b/src/main/java/de/torui/coflsky/FlipHandler.java
index 0c89f21..d3b683e 100644
--- a/src/main/java/de/torui/coflsky/FlipHandler.java
+++ b/src/main/java/de/torui/coflsky/FlipHandler.java
@@ -1,122 +1,127 @@
package de.torui.coflsky;
-import java.util.ConcurrentModificationException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Timer;
-import java.util.TimerTask;
+import de.torui.coflsky.commands.models.FlipData;
+import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class FlipHandler {
- public static class Flip {
- public String id;
- public int worth;
-
- public Flip(String id, int worth) {
- super();
- this.id = id;
- this.worth = worth;
- }
-
- public Flip() {
-
- }
-
- }
-
- public static class FlipDataStructure {
-
- private Map<Long, Flip> Flips = new ConcurrentHashMap <>();
- private Map<Flip, Long> ReverseMap = new ConcurrentHashMap <>();
-
- private Flip HighestFlip = null;
-
- private Timer t = new Timer();
- private TimerTask CurrentTask = null;
-
- public synchronized void RunHouseKeeping() {
- synchronized (Flips) {
-
- Long RemoveAllPrior = System.currentTimeMillis() - (Config.KeepFlipsForSeconds*1000);
- Flips.keySet().stream().filter(l -> l <= RemoveAllPrior).forEach(l -> RemoveLong(l));
- if (!Flips.isEmpty()) {
- HighestFlip = Flips.values().stream().max((f1, f2) -> f1.worth - f2.worth).orElse(null);
- } else {
- HighestFlip = null;
- }
- }
-
- if (CurrentTask != null) {
- CurrentTask.cancel();
- CurrentTask = null;
- t.purge();
- }
- if (!Flips.isEmpty()) {
- CurrentTask = new TimerTask() {
- @Override
- public void run() {
- RunHouseKeeping();
- }
- };
- t.schedule(CurrentTask, Config.KeepFlipsForSeconds * 1000 + /* small arbitrary delay */150);
- }
- }
-
- public synchronized void Insert(Flip flip) {
- Long l = System.currentTimeMillis();
-
- synchronized(Flips) {
- Flips.put(l, flip);
- ReverseMap.put(flip, l);
- }
-
- RunHouseKeeping();
- }
-
- private void RemoveLong(Long l) {
- if (l == null)
- return;
- synchronized(Flips) {
- Flip f = Flips.get(l);
- if (f != null) {
- ReverseMap.remove(f);
- Flips.remove(l);
- }
- }
- }
-
- private void RemoveFlip(Flip f) {
- if (f == null)
- return;
-
- synchronized(Flips) {
- Long l = ReverseMap.get(f);
- if (l != null) {
- Flips.remove(l);
- ReverseMap.remove(f);
- }
- }
- }
-
- public Flip GetHighestFlip() {
- return HighestFlip;
- }
-
- public void InvalidateFlip(Flip flip) {
- RemoveFlip(flip);
- RunHouseKeeping();
- }
-
- public int CurrentFlips() {
- return Flips.size();
- }
-
- }
-
- public FlipDataStructure fds;
-
- public FlipHandler() {
- fds = new FlipDataStructure();
- }
+
+ public static class FlipDataStructure {
+
+ private Map<Long, FlipData> Flips = new ConcurrentHashMap<>();
+ private Map<FlipData, Long> ReverseMap = new ConcurrentHashMap<>();
+
+ private FlipData HighestFlip = null;
+ private FlipData LastFlip = null;
+
+ private Timer t = new Timer();
+ private TimerTask CurrentTask = null;
+
+ public synchronized void RunHouseKeeping() {
+ synchronized (Flips) {
+
+ Long RemoveAllPrior = System.currentTimeMillis() - (Config.KeepFlipsForSeconds * 1000);
+ Flips.keySet().stream().filter(l -> l <= RemoveAllPrior).forEach(l -> RemoveLong(l));
+ if (!Flips.isEmpty()) {
+ HighestFlip = Flips.values().stream().max((f1, f2) -> f1.Worth - f2.Worth).orElse(null);
+ } else {
+ HighestFlip = null;
+ }
+ }
+
+ if (CurrentTask != null) {
+ CurrentTask.cancel();
+ CurrentTask = null;
+ t.purge();
+ }
+ if (!Flips.isEmpty()) {
+ CurrentTask = new TimerTask() {
+ @Override
+ public void run() {
+ RunHouseKeeping();
+ }
+ };
+ t.schedule(CurrentTask, Config.KeepFlipsForSeconds * 1000 + /* small arbitrary delay */150);
+ }
+ }
+
+ public synchronized void Insert(FlipData flip) {
+ Long l = System.currentTimeMillis();
+ LastFlip = flip;
+
+ synchronized (Flips) {
+ Flips.put(l, flip);
+ ReverseMap.put(flip, l);
+ }
+
+ RunHouseKeeping();
+ }
+
+ private void RemoveLong(Long l) {
+ if (l == null)
+ return;
+ synchronized (Flips) {
+ FlipData f = Flips.get(l);
+ if (f != null) {
+ ReverseMap.remove(f);
+ Flips.remove(l);
+ }
+ }
+ }
+
+ private void RemoveFlip(FlipData f) {
+ if (f == null)
+ return;
+
+ synchronized (Flips) {
+ Long l = ReverseMap.get(f);
+ if (l != null) {
+ Flips.remove(l);
+ ReverseMap.remove(f);
+ }
+ }
+ }
+
+ public FlipData GetHighestFlip() {
+ return HighestFlip;
+ }
+
+ public FlipData GetLastFlip() {
+ if (LastFlip == null) {
+ return null;
+ }
+ Long l = ReverseMap.get(LastFlip);
+ if (l == null) {
+ LastFlip = null;
+ }
+ return LastFlip;
+ }
+
+ public FlipData getFlipById(String id) {
+ FlipData[] flips = Flips.values().stream().filter(flipData -> flipData.Id.equals(id)).toArray(FlipData[]::new);
+ Flips.forEach((key, value) -> System.out.println(value.Id));
+ if (flips.length == 0) {
+ return null;
+ }
+ return flips[0];
+ }
+
+ public void InvalidateFlip(FlipData flip) {
+ RemoveFlip(flip);
+ RunHouseKeeping();
+ }
+
+ public int CurrentFlips() {
+ return Flips.size();
+ }
+
+ }
+
+ public FlipDataStructure fds;
+ public String lastClickedFlipMessage;
+
+ public FlipHandler() {
+ fds = new FlipDataStructure();
+ }
}