I want to know why my commands are limited to 4 when using this code.
CONFIG:
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:
Last edited:
