Solved Check which world the player is in for event

Discussion in 'Plugin Development' started by BlueNova, Mar 27, 2020.

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

    BlueNova

    Hello! i have made a plugin that damages the player every time they left click a block. I wanted to check if the player is in a specific world though..but my code does not work for some reason..

    Code:
    public class BlockInteractEvent implements Listener {
        @EventHandler
        public void onInteract(PlayerInteractEvent event) {
            Action blockInteract = event.getAction();
            Player plr = event.getPlayer();
            String world= plr.getWorld().getName();
            if (world.equalsIgnoreCase("testworld")) {
                if (blockInteract.equals(Action.LEFT_CLICK_BLOCK)) {
                    Block block = event.getClickedBlock();
                    if (block.getType().equals(Material.GLOWSTONE)||(block.getType().equals(Material.EMERALD_BLOCK))) {
                        if (!(plr.getHealth()==20)&&(plr.getHealth()< 20.5)) {
                            plr.setHealth(plr.getHealth()+1);
                            plr.sendMessage(ChatColor.GREEN + "YOU HAVE BEEN HEALED!");
                        }else {
                            plr.sendMessage(ChatColor.RED + "You are full health now.");
                        }
                    }else {
                        plr.sendMessage(ChatColor.DARK_RED + "DIE!!");
                        plr.damage(2);
                    }
               
                }
    
           
            }
        }
    }
    
    the checking for world happens in the first 7 lines of code. somehow it doesnt work.. :/
     
  2. Online

    timtower Administrator Administrator Moderator

    @BlueNova Is the check not working or is the entire event not running?
     
  3. Offline

    Dai_Kunai

    If you want to try debugging yourself, you can add either print statements in certain spots to see if the code ever reaches those spots. Add print statements to print the server console or Bukkit.broadcastMessage() messages to see them in the server chat. The messages can be anything as long as you can trace them back to the location in the code...
     
  4. Offline

    BlueNova

    the entire event IS running because i did debug it adding print statements before.. @timtower @Eccentric_Gamer

    i printed out the name of the world and it IS actually "TestWorld"

    i also used if world == "TestWorld" but that didnt get me any luck cuz theoretically both if statements should be the same..


    PS: i got it to work by copying the name of the world object itself, which is very inconvenient but works until i figure out why this one doesnt work..
     
  5. Online

    timtower Administrator Administrator Moderator

    @BlueNova You have lowercase in the code, and uppercase in your comment.
    Which one is it? Or you need to use equalsIgnoreCase
     
  6. Offline

    BlueNova

    I dont need to use it but i tried to change it since i used world=="TestWorld" before. i know either way works and shouldnt change anything. the world is named "TestWorld"

    so basically i change the code once with :

    if (world == "TestWorld")
    and
    if(world.equalsIgnoreCase("testworld")

    i debugged the name of the world out and it actually says "TestWorld" so im really unsure whats wrong

    EDIT: nvm i got it lol i didnt use if(world.equals("TestWorld") {}

    turns out strings cant work with ==
     
    Last edited: Mar 28, 2020
  7. Offline

    Strahan

    Yes, you cannot compare String values with ==. Also you may want to consider negative check and return, it would clean that up a little. Also what's with:
    Code:
    if (!(plr.getHealth()==20)&&(plr.getHealth()< 20.5)) {
    You don't need to do that bizarre nested check. Instead of !(var == value) just do var != value. Also unless you are dealing with a >10 heart situation, health normally maxes out at 20. Also it seems a little silly to create variables for things you reference only once. Lastly I'd recommend against making classes with the names of other classes.

    If I were to implement this, I'd do it as such:
    Code:
    public class BlockInteract implements Listener {
      @EventHandler
      public void onInteract(PlayerInteractEvent event) {
        Player plr = event.getPlayer();
        if (!plr.getWorld().getName().equalsIgnoreCase("testworld")) return;
        if (event.getAction() != Action.LEFT_CLICK_BLOCK) return;
        if (event.getClickedBlock() == null) return;
        if (plr.getHealth() >= 20) {
          plr.sendMessage(ChatColor.RED + "You are full health now.");
          return;
        }
    
        switch (event.getClickedBlock().getType()) {
        case GLOWSTONE:
        case EMERALD_BLOCK:
          plr.setHealth(plr.getHealth()+1);
          plr.sendMessage(ChatColor.GREEN + "YOU HAVE BEEN HEALED!");
          break;
        default:
          plr.sendMessage(ChatColor.DARK_RED + "DIE!!");
          plr.damage(2);
        }
      }
    }
    Well, that's not entirely accurate. If I were actually implementing this, nothing would be hard coded. I'd read the world restriction, block types and messages from config.
     
  8. Offline

    BlueNova

    @Strahan you seem to be very experienced in coding plugins! thanks alot for the tips you have showed me!

    as for the configs, i have not yet fully understood or learned what i can do with config files, as i am very new to java AND coding plugins (only 4 days in)

    i experiment almost everyday with a new plugin i can create.

    again thanks for the tips and i will look into making my codes alot neater.

    this thread is marked as sloved!
     
Thread Status:
Not open for further replies.

Share This Page