Solved Random in runnable with HashMap

Discussion in 'Spigot Plugin Development' started by Futurekiller45, Feb 21, 2021.

  1. Hi,
    I want give level to Entites like: "1,2,3,4,50,100"
    but the Randoms aren't working pretty well, because when they span they should get a HashMap,
    but they don't. I tried fixing it but im still mindless of doing that.

  2. I think your switch is wrong.
    A switch continues its code until there is no 'break' keyword.
    Like this:

    Code (Java):

    int i = 0;
    int x = 3;
    switch (x) {
     case 1:
       i = 1
     case 5:
     case 2:
      i +=1;
     case 3:
      i = 3;
      case 4:
      i = 0;
    That's why the order of a switch is so important.
    In the example i am allowed to change the position of 3 and 4. But 1,5 and 2 must have this order to end up in the same result. Only changing 5 and 2 to 2 and 5 is allowed.

    The only exception is the 'default' keyword it is only used if no 'case' can be applied.
    In Java you should also try to prevent static things.

    A switch can also work for strings and enums. Like this:

    String example:
    Code (Java):

    String result;
    switch (word) {
       case "hi":
       result += "hi";
      case "hello":
       result h= "hello"
      result = "no input";
    Enum example:
    Code (Java):

    Material material;
    String result;
    switch (material) {
      case DIRT:
      case STONE:
      result = "dirt and stone are not allowed";
      case WATER_BUCKET:
      result = " u use a water bucket";
       result = "you don't use a special item";
  3. And just for informations :

    Code (Java):
    will return a value between 0 and 6 (not 1 and 7)
  4. I am not totally sure why you want to heal all the entities to their max health every second, but I will not ask you about this

    Code (Java):
    if(Spawnevent.lvl.get(e.getUniqueId()) == "lvl 20") {
    You are comparing Strings with == and not with String#equals
    Code (Java):
    if(e instanceof LivingEntity) continue;
    All the entities from World#getLivingEntities are LivingEntity, so you will never go past the continue. I am surprise it even compiles with unreachable code
    Code (Java):
    new BukkitRunnable() {
    Do not use BukkitRunnable, they are deprecated for a reason. Use Bukkit.getScheduler()#scheduleAsyncRepeatingTask(Plugin, Runnable, long, long)
    What a huge mess. Wouldn't be easier to do something like this?
    Code (Java):
        private static final Map<Integer, Integer> levels = new HashMap<Integer, Integer>() {{
            put(1, 2);
            put(2, 5);
            // ... All the levels with the corresponding health...
            put(100, 160);
        private static final Integer[] array = levels.keySet().toArray(new Integer[levels.size()]);
        private static final Map<UUID, Integer> entities = new HashMap<>();
        private static final String NAME_FIRST_PIECE = ChatColor.translateAlternateColorCodes('&', "&7lvl &85 &c");
        private static final String NAME_SECOND_PIECE = ChatColor.translateAlternateColorCodes('&', "§8/§c");
        private static int getRandomLevel() {
            return array[(int) (array.length * Math.random())];
        public static void assignLevel(LivingEntity entity) {
            int level = getRandomLevel();
            entities.put(entity.getUniqueId(), level);
            int health = levels.get(level);
            entity.setCustomName(NAME_FIRST_PIECE + health + NAME_SECOND_PIECE + health);