Solved Infinite Loop

Discussion in 'Plugin Development' started by JavaNoob, Sep 21, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    JavaNoob

    Hi! I am trying to make 10 zombies spawn in a loop, but even thought I checked it, it keeps making an infinite loop that makes my server go crazy. But I don't know for a fact if its an infinite loops, because the console keeps throwing errors really quickly. I can't read what they say, but I don't think an infinite loop would be causing that. Help?

    @Override
    public void onEnable() {
    Bukkit.getPluginManager().registerEvents(this, this);
    }

    public String wave = "Wave 0";
    public String name = "Hello";

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (sender instanceof Player) {
    if (cmd.getName().equals("raidstart")) {
    wave = "Wave 1";
    Player player = (Player) sender;
    name = player.getName();
    }
    }

    return false;
    }

    @SuppressWarnings("deprecation")
    @EventHandler
    public void onSpawn(EntitySpawnEvent event) {
    Player player = (Player) Bukkit.getPlayer(name);
    World world = (World) player.getWorld();
    if (wave == "Wave 1") {
    player.getEquipment().setItemInHand(new ItemStack(Material.STONE_SWORD));
    int wave1 = 0;
    while (wave1 != 10) {
    int y = -4;
    world.spawnEntity(player.getLocation().add(6, 0, y), EntityType.ZOMBIE);
    y++;
    wave1++;
    }
    }
     
    Last edited: Sep 23, 2020
  2. Offline

    Kars

    Compare strings with equals not ==.
    Also, using a string to count something is a bad idea,
     
    Xp10d3 likes this.
  3. Offline

    JavaNoob

    Ok, I just thought it would help me keep track of things better. Also, I don't know why I didn't think of scrolling up the console before, but it says it is throwing a NullPointerException, that is an issue. Do you see anything that could be causing that?

    Edit: I actually figured out the problem. In my code in the EntitySpawnEvent, when I did if (wave == "wave 1"), it is always going to equal it until I change it. So it's going to keep running that code over and over. So it creates an infinite loop and crashes my server. (I think that is it.)

    So onto another question I had. How can you cast an entity inside of a command? For example, inside an event, I usually cast it by doing like Zombie zombie = (Zombie) event.getEntityType.ZOMBIE; or something like that. How would I get the Zombie inside of a command? That is the whole reason I tried to do it inside of the EntitySpawnEvent, which would never work.
     
    Last edited: Sep 21, 2020
  4. Offline

    Xp10d3

    Bruh post your full error pls
     
  5. Offline

    JavaNoob

    If you look at my comment above, I just think I found the problem. But I also found out the error kept running over and over because it was a StackOverflowError.
     
  6. Offline

    Xp10d3

    Sorry, didn't see that. It was edited a minute ago and I didn't see it because I sent that message 3 minutes ago... Just saying, if you just say, "Hey, I get an NPE." We can't really help you. Please post your full stack trace next time.
     
  7. Offline

    JavaNoob

    Im sorry, I will do that. I just thought it was way too long to post. Anyway, do you happen to know how to cast entities inside of a command? Like to get the Zombie entity? Normally inside an event I would do Zombie z = (Zombie) event.getEntityType.ZOMBIE; or something like that, but that's not an option inside of a command.
     
  8. Offline

    Kars

    yes. like i said.
     
  9. Offline

    JavaNoob

    XD sorry
     
  10. Offline

    Xp10d3

    no sry. i dont rlly know what you mean by that....
     
  11. Offline

    JavaNoob

    Inside of a command that you make for the server, like the public boolean onCommand that most people use, I want to call the entity zombie so that I can change it, like make its gravity false or something
     
  12. Offline

    Xp10d3

    Create a global variable in your main class and call it in your commands class

    EDIT: and it would make things a lot easier if you can send your code for both classes
    EDIT2: or you could just call a method from your PlayerListeners class. Ex:
    Code:java
    1.  
    2. Zombie zombie = null;
    3. @EventHandler
    4. public void someEvent(SomeEvent event) {
    5. zombie.doSomething();
    6. }
    7.  

    Then:
    Code:java
    1.  
    2. public boolean onCommand (CommandSender sender, Command cmd, String lable, String[] args) {
    3. // commands
    4. if (cmd.getName().equalsIgnoreCase("somecmd") {
    5. PlayerListeners playerListeners;
    6. Zombie zombie = playerListeners.zombie;
    7. playerListeners.idk(zombie);
    8. }
    9. }
    10.  

    thats pseudo code, and the playerListeners var would obvs not work. the code i sent is prob not gonna work either but im just giving you the idea.
     
    Last edited: Sep 22, 2020
  13. Offline

    JavaNoob

    Thank you. I will try something like that. I am in class rn but when I get out I will send you my code
     
    Xp10d3 likes this.
  14. Offline

    KarimAKL

    Just noting but, everything you sent is valid syntax, it'll compile as long as the references exist. :p
     
    Xp10d3 likes this.
  15. Offline

    JavaNoob

    Thank you all for all the help! I just ended up making my own method instead of trying to call it inside of EntitySpawnEvent. I don't know why i thought that would work. And it works now perfectly! Thank you for all the help and knowledge, it shows me just how much I still have to learn.
     
    Xp10d3 likes this.
Thread Status:
Not open for further replies.

Share This Page