Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void openCrate(Player whoOpened, Crate crateOpened) {
- FileConfiguration crateConfig = getCrateConfig(crateOpened.getName());
- if (crateConfig == null) return;
- List<String> rewards = crateConfig.getStringList("Rewards");
- Map<Integer, String> listOfPercent = new HashMap<>();
- int genNumber;
- int totalPercent = 0;
- if (rewards.size() == 0) return;
- for (String line : rewards) {
- String[] words = line.split(" ");
- String percent = words[0];
- int numPercent;
- if (!percent.contains("%")) continue;
- while (percent.endsWith("%")) {
- // removing the percentage sign at the end
- percent = percent.substring(0, percent.length() - 1);
- }
- try {
- numPercent = Integer.valueOf(percent);
- } catch (NumberFormatException e) {
- getLogger().log(Level.WARNING, "Was unable to parse percent at line '" + line + "' in " + crateOpened.getName() + ".yml");
- continue; // skip the line
- }
- StringBuilder command = new StringBuilder();
- for (int i = 1; i < words.length; i++)
- command.append(words[i]).append(" ");
- totalPercent += numPercent; // shorter for totalPercent = totalPercent + numPercent
- listOfPercent.put(numPercent, command.substring(0, command.length() - 1));
- }
- if (totalPercent < 100)
- totalPercent += (100 - totalPercent);
- genNumber = new Random().nextInt(totalPercent - 1);
- int n = 0;
- int timesLooped = 0;
- int lastPercent = 0;
- while (n <= genNumber) {
- if (timesLooped < listOfPercent.size()) {
- lastPercent = toList(listOfPercent.keySet()).get(timesLooped);
- n = n + lastPercent;
- } else
- lastPercent = -1;
- timesLooped++;
- }
- if (lastPercent != -1) {
- String command = listOfPercent.get(lastPercent);
- getServer().dispatchCommand(getServer().getConsoleSender(), command.replaceAll("%player%", whoOpened.getName()));
- }
- crateOpened.setOpened(crateOpened.getOpened() + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement