Entity clear command

Discussion in 'Plugin Development' started by saladhat, Mar 10, 2023.

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

    saladhat

    I want to make a command that will clear a certain entity based on what I provide in the command's arguments. I've got a majority of the code but I can't figure out how to take the arg and pass it into my code. I think I'd pass it where the "Item" is in my for loop.

    upload_2023-3-11_0-38-18.png
     
  2. Offline

    Strahan

    The last item in the constructor are the arguments passed. You just need to first verify an argument was passed, then you use the args element of the appropriate argument to do your matching.

    I have no idea what your matching schema is, but let's say it's entity ID. Here is an example:
    Code:
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
      if (!(sender instanceof Player)) {
        sender.sendMessage("In game use only!");
        return true;
      }
    
      if (args.length == 0) {
        sender.sendMessage("You need to pass the entity ID!");
        return true;
      }
    
      Player p = (Player)sender;
      World world = p.getWorld();
      int entityID = getInt(args[0]);
    
      Entity ent = world.getEntities().stream()
          .filter(x -> x.getEntityId() == entityID)
          .findFirst()
          .orElse(null);
    
      if (ent == null) {
        p.sendMessage("I could not locate that entity!");
        return true;
      }
        
      if (!(ent instanceof Item)) {
        p.sendMessage("That entity is not an item!");
        return true;
      }
    
      ent.remove();
      p.sendMessage("Entity removed!");
    }
    
    public int getInt(String input) {
      return getInt(input, Integer.MIN_VALUE);
    }
    public int getInt(String input, int defaultvalue) {
      try {
        return Integer.valueOf(input);
      } catch (IllegalArgumentException ex) {
        return defaultvalue;
      }
    }
    I refactored a few things in your example. So what I'm doing is first making sure Player is a sender before casting it thus; you really shouldn't blind cast as you are. Then I make sure they passed an argument. Then I make sure the argument is a valid integer. You can do the try/catch in the actual command, but I prefer moving that out to its own method to keep the code cleaner. Then I cast the player and get the World. I have no idea why you are doing it the way you were with the world; why hardcode it? One would assume the player wants to remove the entity in the world in which they are currently located, so I used Player#getWorld() instead.

    Then instead of looping every single Entity in the world to find the one I want, a stream is a more efficient method. I stream the list and filter it to get the entity with the ID I want. Once I get that, if it's null I tell them that isn't valid. If it isn't an item, I yell about that. I actually could do the item check in the stream, but then I wouldn't be able to differentiate between an invalid entity ID and a valid one but for a non item entity. I prefer errors to be explicit, so I do the check outside the stream. Lastly, I remove the entity.
     
Thread Status:
Not open for further replies.

Share This Page