Bukkit Change EntityType

Discussion in 'Plugin Development' started by AngryCupcake274, Dec 18, 2014.

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

    RingOfStorms

    I just lost brain cells reading this post...

    @AngryCupcake274 you reallllllly need to learn java before you attempt making plugins. You obviously don't understand anything about the different kinds of Objects and how they work or how they are compared. Similarly, you also aren't using the instanceof check at all the way it is supposed to. That isn't even used for comparing two objects.

    I would recommend looking at the official Oracle tutorials to learn java.
     
    MisterErwin and AdamQpzm like this.
  2. Offline

    AngryCupcake274

    @TheMrGong the comh is the name for the CommandHandler class, where the "ent" variable is defined. When it compares it to "comh.ent" it's comparing it to the "ent" in the CommandHandler class.

    @RingOfStorms I did learn Java before coding Bukkit plugins. The problem is that the instanceof was testing to see if the current entity the server was checking was equal to what entity I defined. If trying to get around writing 40 different methods for all types of entities, so I need a way to create an entity variable that I can change with each command.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  3. Offline

    TheMrGong

    What. So you set ent.. but ent is also in command handler class... so.. there are two ents? I'm extremely confused.
     
  4. Offline

    AngryCupcake274

    @TheMrGong "ent" is the same as "comh.ent". When it is "comh.ent", I am referencing it from another class. When it's just "ent", I am already in the CommandHandler class.
     
  5. Offline

    WesJD

    @AngryCupcake274 It's better to name them differently. It just makes it that much less confusing.
     
  6. Offline

    TheMrGong

    Why do you reffer to the class inside the class? Thats like doing

    Code:
    public class Main extends JavaPlugin {
      Main plugin;
      @Override
      public void onEnable() {
        plugin = this;
      }
      plugin.onEnable();
    }
     
  7. Offline

    RingOfStorms

    As I said, instanceof isn't even used to compare two objects, so you've obviously messed up there. instanceof is used to check if an object is an instance of a certain type.

    This post on oracle talks about equality/comparisons, although the subject may be a bit advanced for your level and you may want to consider doing tutorials at a beginner level and work your way up.

    You can't simply instanceof on an enum, because an enum is still an instance of an object and not a type.
     
  8. Offline

    WesJD

    @TheMrGong How do you know it's the same class?
     
  9. Offline

    AngryCupcake274

    @WesJD @TheMrGong "comh.ent" and "ent" are the exact same variable, but when I use "comh.ent" I'm grabbing the variable from my RemoveEntites class.
     
  10. Offline

    TheMrGong

    Now your saying cohm.ent is from your RemoveEntities class. Before you were saying it was the CommandHandler class.
     
  11. Offline

    AngryCupcake274

    @TheMrGong let me start over. "ent" is used when I'm coding inside the class it originates from (CommandHandler). "comh.ent" is used when I'm grabbing it from the CommandHandler class. I use "comh.ent" when I need to use the original "ent" variable in another class.
     
  12. Offline

    TheMrGong

    I give up.
     
  13. Offline

    MisterErwin

    @AngryCupcake274 I will just quote RingOfStorms here
     
  14. Offline

    AngryCupcake274

    @MisterErwin @TheMrGong this post is not to talk about how I misinterpreted a piece of code! It's about trying to make an Entity variable that I can use to see if said Entity variable is the same as the Entity the server is going through in a loop! I need help making that Entity variable that I can set (i.e. Arrow)!
     
  15. @AngryCupcake274 And you're going about it all wrong. getType() returns an EntityType, check with that.
     
  16. Offline

    AngryCupcake274

    @AdamQpzm how can I get an Entity variable (i.e. Arrow) amd use it in my instanceof?
     
  17. @AngryCupcake274 You don't, that doesn't make any sense from a Java point of view, as many people have told you. instanceof does not take two variables. You don't know how to use Java, and since you've heard of "instanceof", you're trying to use it for a place it's not at all appropriate. Please learn Java from either the Oracle tutorial, or a Java book before trying to make plugins, otherwise you will always run into this sort of problem and will have no idea how to solve it,
     
  18. Offline

    AngryCupcake274

    @AdamQpzm I do know Java, but I've never used instanceof in depth. Thank you for clearing that up for me. I will just write many methods.
     
    Last edited: Dec 20, 2014
  19. Offline

    Europia79

    Similar to what @TheMrGong suggested:
    Code:java
    1. public void clearEntities(EntityType type) {
    2. for (World worlds : Bukkit.getWorlds()) {
    3. List<Entity> entities = worlds.getEntities();
    4. for (Entity entity : entities) {
    5. if (entity.getType() == type) {
    6. entity.remove();
    7. }
    8. }
    9. }
    10. plugin.cleaner.sendMessage(ChatColor.BLUE + "All " + type.getName() + "s have been removed.");
    11. }


    Pass the type of entity that you want to remove as a parameter to the method.

    Don't grab information from a specific object, like CommandHandler... The point of a method is to be reusable... And pulling information from a specific object like that means that only that object can use the method.

    Whereas, if your method simply ASKS for the information that it needs to do its job (ask for it by declaring a parameter(s)), then other objects are now able to freely utilize this method... as long as they supply the required the information.

    For more information on method parameterization, this is a good video:
    Stuart Marks - parameterizing behavior

    And a similar thing applies to constructors too: Avoid pulling from the global space... instead, just ask for what you need. If you can handle 4.5 hours of presentations:
    Misko Hevery - Clean Code Talks

    If you can't handle 4.5 hours, here's the summary:
    http://misko.hevery.com/code-reviewers-guide/

     
  20. Offline

    AngryCupcake274

    @Europia79 is there a way to have one entity variable, and change it to whatever entity I want? For example, above in the "entity.getType()", could I set "entity" to an Arrow or Boat? Or is there no way to do that?
     
  21. Offline

    xTrollxDudex

    Set entity to an arrow...
     
  22. Offline

    Europia79

    @AngryCupcake274

    http://docs.oracle.com/javase/tutorial/
    http://wiki.bukkit.org/Plugin_Tutorial
    http://wiki.bukkit.org/Configuration_API_Reference

    Code:java
    1. String path = "Entities.RemoveAll";
    2. String defaultValue = "arrow";
    3. String name= getConfig().getString(path, defaultValue).toUpperCase();
    4. EntityType type = EntityType.valueOf(name);
    5. // ...
    6. // elsewhere in your code, you'd have:
    7. clearEntities(type);


    Then in your config.yml, you'd have
    Code:
    Entities:
        RemoveAll: boat
    Does this answer your question ?
     
  23. Offline

    AngryCupcake274

    @Europia79 I've got it! I've been so blind to what you have been saying. If I do this:
    Code:
        // method for removing entities via command
        @SuppressWarnings("deprecation")
        public void clearEntities(EntityType ent) {
           
            for (World worlds : Bukkit.getWorlds()) {
                List<Entity> entList = worlds.getEntities();
                for (Entity current : entList) {
                    if (current.getType() == ent) {
                        current.remove();
                    }
                }
            }
            plugin.cleaner.sendMessage(ChatColor.BLUE + "All " + ent.getName() + " Entities Removed!");
        }
    It works! I never thought that I could use instanceof in temp.getType() and an EntityType, I was trying to compare temp and Entity. Thank you all so much for all of your help! You have helped me finally squash this bug!
     
Thread Status:
Not open for further replies.

Share This Page