diff --git a/Auto-Tune/src/unprotesting/com/github/Main.java b/Auto-Tune/src/unprotesting/com/github/Main.java index aafb04f..266a634 100644 --- a/Auto-Tune/src/unprotesting/com/github/Main.java +++ b/Auto-Tune/src/unprotesting/com/github/Main.java @@ -10,7 +10,9 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; +import java.util.Collections; import java.util.Date; import java.util.LinkedList; import java.util.List; @@ -165,10 +167,10 @@ public final class Main extends JavaPlugin implements Listener { public static Integer materialListSize; @Getter - public static TopMover[] topSellers; + public static ArrayList topSellers; @Getter - public static TopMover[] topBuyers; + public static ArrayList topBuyers; @Getter public static ConcurrentHashMap itemPrices = new ConcurrentHashMap(); @@ -465,8 +467,8 @@ public static void setupDataFiles() { if (tempdatadata.get("GDP")==null){ tempdatadata.put("GDP", 0.0); } - topSellers = new TopMover[Config.getTopMoversAmount()]; - topBuyers = new TopMover[Config.getTopMoversAmount()]; + topSellers = new ArrayList(); + topBuyers = new ArrayList(); } public static void closeDataFiles(){ @@ -482,9 +484,13 @@ public static void closeDataFiles(){ } public static void loadTopMovers(){ + Main.topBuyers.clear(); + Main.topSellers.clear(); for (String item : Main.map.keySet()){ TopMover itemMover = new TopMover(item); } + Collections.sort(Main.topBuyers); + Collections.sort(Main.topSellers); if (Config.isDebugEnabled()){ Main.debugLog("Top Buyers: "); for (TopMover mover : topBuyers){ diff --git a/Auto-Tune/src/unprotesting/com/github/util/JoinEventHandler.java b/Auto-Tune/src/unprotesting/com/github/util/JoinEventHandler.java index 51d0c25..9075448 100644 --- a/Auto-Tune/src/unprotesting/com/github/util/JoinEventHandler.java +++ b/Auto-Tune/src/unprotesting/com/github/util/JoinEventHandler.java @@ -1,5 +1,7 @@ package unprotesting.com.github.util; +import java.util.Arrays; +import java.util.Collections; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; diff --git a/Auto-Tune/src/unprotesting/com/github/util/TopMover.java b/Auto-Tune/src/unprotesting/com/github/util/TopMover.java index 04c8687..f98c792 100644 --- a/Auto-Tune/src/unprotesting/com/github/util/TopMover.java +++ b/Auto-Tune/src/unprotesting/com/github/util/TopMover.java @@ -1,11 +1,12 @@ package unprotesting.com.github.util; +import java.util.Collections; import java.util.List; import unprotesting.com.github.Main; import unprotesting.com.github.Commands.AutoTuneGUIShopUserCommand; -public class TopMover { +public class TopMover implements Comparable< TopMover >{ public double price; public double percentage_change = 0.0; @@ -15,58 +16,28 @@ public TopMover(String name){ this.name = name; this.price = AutoTuneGUIShopUserCommand.getItemPrice(name, false); this.percentage_change = loadPercentageChange(this); - if (percentage_change != 0.0){ - if (percentage_change > 0){ - int nullValue = 100000; - for (int k = 0; k < Config.getTopMoversAmount(); k++){ - if (Main.topBuyers[k] == null){ - nullValue = k; - break; - } - } - if (nullValue < 99999){ - Main.topBuyers[nullValue] = this; + if (this.percentage_change != 0.0){ + if (this.percentage_change > 0){ + if (Main.getTopBuyers().size() < Config.getTopMoversAmount()){ + Main.topBuyers.add(this); } - else { - double lowest_percentage = 100.00; - int pos = 10000; - int i = 0; - for (TopMover topMover : Main.topBuyers){ - if (topMover.percentage_change < lowest_percentage){ - lowest_percentage = topMover.percentage_change; - pos = i; - } - i++; - } - if (this.percentage_change > lowest_percentage && pos != 10000){ - Main.topBuyers[pos] = this; + else{ + Collections.sort(Main.topBuyers); + if (Main.getTopBuyers().get(Config.getTopMoversAmount()-1).percentage_change < this.percentage_change){ + Main.topBuyers.remove(Config.getTopMoversAmount()-1); + Main.topBuyers.add(Config.getTopMoversAmount()-1, this); } } } - else if (percentage_change < 0){ - int nullValue = 100000; - for (int k = 0; k < Config.getTopMoversAmount(); k++){ - if (Main.topSellers[k] == null){ - nullValue = k; - break; - } - } - if (nullValue < 99999){ - Main.topSellers[nullValue] = this; + if (this.percentage_change < 0){ + if (Main.getTopSellers().size() < Config.getTopMoversAmount()){ + Main.topSellers.add(this); } - else { - double highest_percentage = 100.00; - int pos = 10000; - int i = 0; - for (TopMover topMover : Main.topSellers){ - if (topMover.percentage_change < highest_percentage){ - highest_percentage = topMover.percentage_change; - pos = i; - } - i++; - } - if (this.percentage_change < highest_percentage && pos != 10000){ - Main.topSellers[pos] = this; + else{ + Collections.sort(Main.topSellers); + if (Main.getTopSellers().get(Config.getTopMoversAmount()-1).percentage_change > this.percentage_change){ + Main.topSellers.remove(Config.getTopMoversAmount()-1); + Main.topSellers.add(Config.getTopMoversAmount()-1, this); } } } @@ -98,5 +69,15 @@ public double loadPercentageChange(TopMover mover) { public String toString(){ return ("Name: " + this.name + " | Price: " + this.price + " | Percentage Change: %" + this.percentage_change); } + + @Override + public int compareTo(TopMover o) { + if (o.percentage_change < 0 && this.percentage_change < 0){ + return (int) (this.percentage_change*1000 - o.percentage_change*1000); + } + else{ + return (int) (o.percentage_change*1000 - this.percentage_change*1000); + } + } }