문제

Is this possible? I'm doing an bukkit plugin now (in Python, yes :D), and I'm forced to do this within one function, so I can't separate it and call it later... For example, if I have loop that loops through players on server and adds everyone except one player, I want it to finish, and then teleport i.e. "Player1" to random player. At the moment, it teleports "Player1" to random player every time because of for loop... I'll give you just little of code, since It looks messy in preview due to many things that are not involved in problem and could be confusable to you... Here it is:

listica = []
for p1 in org.bukkit.Bukkit.getWorld(nextvalue).getPlayers():
    if p1.getName() not in listica:
       try:
         listica.remove(event.getPlayer().getName())
         randomtarget = choice(listica)
         randomtargetreal = org.bukkit.Bukkit.getPlayer(randomtarget)
         event.getPlayer().teleport(randomtargetreal)
         event.getPlayer().sendMessage("%sYou teleported to: %s%s"%  (bukkit.ChatColor.GREEN, bukkit.ChatColor.DARK_GREEN, randomtarget))                                  
       except ValueError:
             randomtarget = choice(listica)
             randomtargetreal = org.bukkit.Bukkit.getPlayer(randomtarget)
             if event.getPlayer().getLocation() != randomtargetreal.getLocation():
             event.getPlayer().teleport(randomtargetreal)
             event.getPlayer().sendMessage("%sYou teleported to: %s%s"%(bukkit.ChatColor.GREEN, bukkit.ChatColor.DARK_GREEN, randomtarget))

What I want is:

run for loop:
when there is no more players to add a.k.a it finishes
do try loop

P.S. I can't do it in separate function.

Thanks in advance! :)

도움이 되었습니까?

해결책

Do you mean:

def func(args):
      for item in loop:
          do something
      try: # note indentation
          something else
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top