Custom Plugin (need help)

Status
This thread has been locked.

Niko302

Premium
Feedback score
20
Posts
194
Reactions
21
Resources
6
I want to know why my commands are limited to 4 when using this code.
Code:
        executeCommands(item, e.getPlayer());
        int amount = e.getItem().getAmount();
        if (amount == 1)
        {
          e.getPlayer().setItemInHand(null);
          e.setCancelled(true);
        }
        else
        {
          e.getItem().setAmount(amount - 1);
        }
      }
    }
  }

  public void executeCommands(String item, Player p)
  {
    Map<String, Integer> order = new LinkedHashMap();
    List<String> commands = new ArrayList();
    for (String cmd : getCommands(item))
    {
      String command = cmd.split(";")[0];
      int chance = Integer.parseInt(cmd.split(";")[1]);
  
      order.put(command, Integer.valueOf(chance));
    }
    int add = 0;
    int numb = 0;
    List<Integer> vals = new ArrayList(order.values());
    for (int i = 0; i < order.size(); i++) {
      numb += ((Integer)vals.get(i)).intValue();
    }
    int rn = this.rand.nextInt(numb) + 1;
    int val;
    while (commands.size() < getConfig().getInt("items." + item + ".number_of_commands_to_execute")) {
      for (int i = 0; i < order.size(); i++)
      {
        val = ((Integer)vals.get(i)).intValue() + add;
        if (rn <= val)
        {
          String command = (String)getKeyByValue(order, (Integer)vals.get(i));
          commands.add(command.replaceAll("%player%", p.getName()));
      
          break;
        }
        add += val;
      }
    }
    for (String cmd : commands) {
      Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
    }
  }

  public static <T, E> T getKeyByValue(Map<T, E> map, E value)
  {
    for (Map.Entry<T, E> entry : map.entrySet()) {
      if (Objects.equals(value, entry.getValue())) {
        return (T)entry.getKey();
      }
    }
    return null;
  }

  public List<String> getCommands(String item)
  {
    List<String> cmds = new ArrayList();
    if (getConfig().contains("items")) {
      for (String s : getConfig().getStringList("items." + item + ".commands")) {
        cmds.add(s);
      }


CONFIG:
only4.jpg

gVGtbb
 
Last edited:
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

LordOfTime

Feedback score
15
Posts
602
Reactions
245
Resources
0
1.
  • instead of null do Material.AIR
  • e.getPlayer().setItemInHand(null);
2.
  • Use lambda expressions, try this

Code:
                        List<String> commands = new ArrayList<String>();
                        commands.forEach(s -> {
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), s);
                        });

100% agree with first one, air instead of null is common practice and avoids these issues.

Second one, I'd only advise if you (OP) understand lambdas. Using code you don't understand is asking for a bad time, both today when you try to expand on it and a year from now when you try to read it again after you've forgotten what it all does.

To be clear, I 100% would suggest lambdas for this kind of work, it's much more compact, not to mention perhaps more efficient if I remember correctly? (haven't read up on lambda efficiency recently, could 100% be wrong there). Just that you should learn about them to the point where you understand both their usage and some basic theory before you put them into use in your code. Lambdas are nice to know how to use, just only if you actually know how to use them.

(That is assuming you don't already use lambdas, it's possible you just chose not to for readability whilst working on the code)
 
Status
This thread has been locked.
Top