Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None
  1.     private void openCrate(Player whoOpened, Crate crateOpened) {
  2.         FileConfiguration crateConfig = getCrateConfig(crateOpened.getName());
  3.  
  4.         if (crateConfig == null) return;
  5.  
  6.         List<String> rewards = crateConfig.getStringList("Rewards");
  7.         Map<Integer, String> listOfPercent = new HashMap<>();
  8.         int genNumber;
  9.         int totalPercent = 0;
  10.  
  11.  
  12.         if (rewards.size() == 0) return;
  13.  
  14.         for (String line : rewards) {
  15.  
  16.             String[] words = line.split(" ");
  17.             String percent = words[0];
  18.             int numPercent;
  19.  
  20.             if (!percent.contains("%")) continue;
  21.  
  22.             while (percent.endsWith("%")) {
  23.                 // removing the percentage sign at the end
  24.                 percent = percent.substring(0, percent.length() - 1);
  25.             }
  26.  
  27.             try {
  28.                 numPercent = Integer.valueOf(percent);
  29.             } catch (NumberFormatException e) {
  30.                 getLogger().log(Level.WARNING, "Was unable to parse percent at line '" + line + "' in " + crateOpened.getName() + ".yml");
  31.  
  32.                 continue; // skip the line
  33.             }
  34.  
  35.  
  36.  
  37.             StringBuilder command = new StringBuilder();
  38.  
  39.             for (int i = 1; i < words.length; i++)
  40.                 command.append(words[i]).append(" ");
  41.  
  42.  
  43.             totalPercent += numPercent; // shorter for totalPercent = totalPercent + numPercent
  44.             listOfPercent.put(numPercent, command.substring(0, command.length() - 1));
  45.  
  46.         }
  47.         if (totalPercent < 100)
  48.             totalPercent += (100 - totalPercent);
  49.  
  50.         genNumber = new Random().nextInt(totalPercent - 1);
  51.         int n = 0;
  52.         int timesLooped = 0;
  53.  
  54.         int lastPercent = 0;
  55.  
  56.         while (n <= genNumber) {
  57.             if (timesLooped < listOfPercent.size()) {
  58.                 lastPercent = toList(listOfPercent.keySet()).get(timesLooped);
  59.                 n = n + lastPercent;
  60.             } else
  61.                 lastPercent = -1;
  62.  
  63.             timesLooped++;
  64.         }
  65.  
  66.         if (lastPercent != -1) {
  67.  
  68.             String command = listOfPercent.get(lastPercent);
  69.             getServer().dispatchCommand(getServer().getConsoleSender(), command.replaceAll("%player%", whoOpened.getName()));
  70.         }
  71.  
  72.         crateOpened.setOpened(crateOpened.getOpened() + 1);
  73.     }
Advertisement
RAW Paste Data Copied
Advertisement