Browse Source

change int to double for payout chances

MasterCake 1 year ago
parent
commit
268fe88602

+ 11 - 6
Plugin/src/main/java/de/Linus122/TimeIsMoney/Main.java

@@ -10,6 +10,7 @@ import java.io.PrintWriter;
 import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -239,7 +240,7 @@ public class Main extends JavaPlugin {
 				}
 				
 				if (finalconfig.isSet("payouts." + key + ".chance")) {
-					payout.chance = finalconfig.getInt("payouts." + key + ".chance");
+					payout.chance = finalconfig.getDouble("payouts." + key + ".chance");
 				}
 				payouts.add(payout);
 			}
@@ -276,14 +277,18 @@ public class Main extends JavaPlugin {
 		}else {
 			// Get a random payout
 			Random rnd = new Random();
-			List<Payout> list = new ArrayList<>();
+			
+			double d = rnd.nextDouble() * 100;
+			
 			for (Payout payout : payouts) {
-				for (int i = 0; i < payout.chance; i++) list.add(payout);
+				if(payout.chance == 0.0) continue;
+				if ((d -= payout.chance) < 0){
+					return Collections.singletonList(payout);
+				}
 			}
-			List<Payout> payoutList = new ArrayList<>();
-			payoutList.add(list.get(rnd.nextInt(list.size() - 1)));
-			return payoutList;
+			
 		}
+		return Collections.emptyList(); 
 	}
 	
 	/**

+ 1 - 1
Plugin/src/main/java/de/Linus122/TimeIsMoney/Payout.java

@@ -25,7 +25,7 @@ class Payout {
 	/**
 	 * The chance of getting the payout.
 	 */
-	int chance = 0;
+	double chance = 0;
 	/**
 	 * The list of commands to execute if this payout is earned.
 	 */