Bukkit Change EntityType

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

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

    AngryCupcake274

    Hello, I'm back with a very quick question. In my new plugin, when a player types a command, I need to change the type of an Entity. I've tried:
    Code:
    entn = Entity.EnderSignal
    and
    Code:
    entn = EnderSignal
    but none of these work.

    P.S. the variable "entn" is an Entity.
     
  2. Offline

    Skionz

    @AngryCupcake274 You can't change the type of an entity, but you can remove the current entity and spawn a new one.
     
    Konato_K likes this.
  3. Offline

    AngryCupcake274

    @Skionz is there any way to convert user input (a String) to an Entity? Otherwise I have to write about 100 methods.
     
  4. Offline

    API_Tutorials

    @AngryCupcake274
    Considering you're doing a command.


    Code:
    EntityType type = null;
    
    try {
    type = EntityType.valueOf(args[0]);
    } catch (Exception e){
    <send message to player saying it's an invalid entity type>
    return false;
    }
     
  5. Offline

    AngryCupcake274

    @API_Tutorials could I also do:
    Code:
    type = EntityType.valueOf("Arrow");
    This way the user couldn't get it wrong.
     
  6. Offline

    API_Tutorials

    @AngryCupcake274
    "Arrow" would have to be uppercase.
    Considering 'Arrow' isn't an enum in EntityType
     
  7. Offline

    AngryCupcake274

    @API_Tutorials when I use "EntityType.valueOf("Arrow"); I get an error. Is there another way to convert String to Entity? Or just set an Entity as a different EntityType?
     
  8. Offline

    xTrollxDudex

    @AngryCupcake274
    That would be very bad. Create a new entity and transfer thr data that you need.
     
  9. Offline

    AngryCupcake274

    @xTrollxDudex for example, how would I create an arrow entity? When I try:
    Code:
    Entity entity = Entity.Arrow;
    It gives an error. How would I create that entity?
     
  10. Offline

    pookeythekid

    @AngryCupcake274 Too lazy not to spoon feed.
    Here's the general idea of what I've just used in one of my plugins, and it works (at least, in the complete version it does).
    Code:
    Map<String, EntityType> entityNames = new HashMap<>();
    // The below Map is more for what feeds into your code, like from a config.
    Map<EntityType, Set<String>> nameSets = new HashMap<>();
    // blah blah, some code is skipped
    nameSets.put(someEntityType, someSet/*Which you filled with acceptable entity names in earlier code*/);
    // more code is skipped, you loop through your nameSets keys like so
    for (EntityType eType : nameSets.keySet()) {
        Set<String> names = nameSets.get(eType);
        for (String str : names) {
            entityNames.put(str.toLowerCase(), eType);
        }
    }
    // Then somewhere in your command:
    if (entityNames.containsKey(args[0].toLowerCase())) {
        // entity spawn code
    }
    This may have a couple things missing here and here. But you should get the idea.
     
  11. Offline

    xTrollxDudex

  12. Offline

    AngryCupcake274

    @pookeythekid @xTrollxDudex I don't want to spawn an entity, I need to use an entity variable to be able to put in one of my methods. The problem is that I can't convert an EntityType to an Entity, which is what I need for that method to work.
     
  13. Offline

    Skionz

    @AngryCupcake274 What are you talking about? EntityType is an enum used to describe the type of the Entity when creating it, or comparing it. You can't turn it into an Entity, but as I have said before, the best you can do is spawn a new Entity using that type.
     
    pookeythekid likes this.
  14. Offline

    AngryCupcake274

    @Skionz that's the problem. I need to be able to create an Entity (i.e. an Arrow entity) to a variable. For example:
    Code:
    Entity test = Arrow;
    
    -or-
    
    Entity test = Entity.Arrow;
     
  15. Offline

    Skionz

  16. Offline

    AngryCupcake274

    @Skionz no. I'm not physically creating an entity, I'm creating an Entity variable (i.e. Arrow Entity) to use. Here is the method I need to use said Entity variable for:
    Code:
            for (World worlds : Bukkit.getWorlds()) {
                List<Entity> entList = worlds.getEntities();
                for (Entity current : entList) {
                    if (current instanceof [entity variable]) {
                        current.remove();
                    }
                }
            }
     
  17. Offline

    Skionz

    @AngryCupcake274 You can't have a variable without having an instance of the Entity class. Well you can, but it would be null. Just use Entity#getType() and compare it using the EntityType enum.
     
  18. Offline

    AngryCupcake274

    @Skionz I tried to compare the EntityType.ARROW to Entity.getType(EntityType.ARROW), but got an error. I happened to stumbe upon this though, would it work?
    Code:
    Class<? extends EntityType> e = EntityType.ARROW.getClass();
    If so, how do I make an Entity variable from it?

    @Skionz a new way to phrase my question: Is there a way to assign an entity (i.e. Arrow) to an Entity variable, to use in the method in a previous post?

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

    TheMrGong

    As for converting string into entitytype you can use this function I made
    [​IMG]
    Code:
        /**
         * Turns a string into an entity type
         *
         * @param arg Entity name. "dropped item" is converted to "DROPPED_ITEM"
         * @return The entity type of the string, or null if entity type not found
         */
        public EntityType getEntityFromString(String arg) {
            EntityType ret;
            String completed = arg.toUpperCase().replace(" ", "_");
            try {
                ret = EntityType.valueOf(completed);
            } catch(Exception ex) {ret = null;}
            return ret;
        }
     
  20. Offline

    AngryCupcake274

    @TheMrGong I need the String to turn into an Entity, in an earlier post I put the method where the entity variable needs to go.
     
  21. Offline

    TheMrGong

    You can't convert a string into a entity. You can only turn it into a entity type, then create a entity using that entity type.
     
  22. Offline

    AngryCupcake274

    @TheMrGong so let's say I do:
    Code:
    EntityType temp = EntityType.ARROW;
    How do I make the Entity variable from temp?
     
  23. Offline

    TheMrGong

    [​IMG]
    Replace yourLoc = null with yourLoc = *Your location*
    Like
    [​IMG]
    And use that in setting your location.
     
  24. Offline

    AngryCupcake274

    @TheMrGong Earlier in this thread I stated that I don't want to spawn an entity, I need an Entity variable to compare other entities to. Here is the method I need to put the variable in:
    Code:
        public void clearEntities() {
            Entity temp = comh.ent;
           
            for (World worlds : Bukkit.getWorlds()) {
                List<Entity> entList = worlds.getEntities();
                for (Entity current : entList) {
                    if (current instanceof temp) {
                        current.remove();
                    }
                }
            }
            plugin.cleaner.sendMessage(ChatColor.BLUE + "All " + temp.getName() + " Entities Removed!");
        }
    Note: the variable "temp" is the Entity variable I need. I need to be able to set what the "temp" variable is (what type of entity it is).
     
  25. Offline

    TheMrGong

    If you mean that you want to compare entitys you can do this:
    [​IMG]
     
  26. Offline

    AngryCupcake274

    @TheMrGong I'm putting the new code in and am about to test it....
    And it threw an error:
    Code:
    [14:53:49] [Server thread/INFO]: AngryCupcake274 issued server command: /entitymanager clearentity arrow
    [14:53:49] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'entitymanager' in plugin EntityManager v3.0.1.2
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-6b061e2]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-6b061e2]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:624) ~[craftbukkit.jar:git-Bukkit-6b061e2]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1058) [craftbukkit.jar:git-Bukkit-6b061e2]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:919) [craftbukkit.jar:git-Bukkit-6b061e2]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-6b061e2]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-6b061e2]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-6b061e2]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_60]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_60]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:643) [craftbukkit.jar:git-Bukkit-6b061e2]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit.jar:git-Bukkit-6b061e2]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:598) [craftbukkit.jar:git-Bukkit-6b061e2]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:506) [craftbukkit.jar:git-Bukkit-6b061e2]
        at java.lang.Thread.run(Unknown Source) [?:1.7.0_60]
    Caused by: java.lang.NullPointerException
        at me.AngryCupcake274.EntityManager.RemoveEntities.clearEntities(RemoveEntities.java:36) ~[?:?]
        at me.AngryCupcake274.EntityManager.CommandHandler.onCommand(CommandHandler.java:212) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-6b061e2]
        ... 14 more
    Line 36 of RemoveEntities is the:
    Code:
    if (current.getType() == comh.ent) {
        current.remove();
    }
     
  27. Offline

    TheMrGong

    I don't know what "comh.ent" is, and it could be null.
     
  28. Offline

    AngryCupcake274

    @TheMrGong here is where comh.ent is defined:
    Code:
    if (args[1].equalsIgnoreCase("arrow")) {
        ent = EntityType.ARROW; // set entity to arrow
        re.clearEntities();
        return true;
    }
    so it's not null.
     
  29. Offline

    TheMrGong

    Whats the "cohm"
     
  30. Offline

    WesJD

    @TheMrGong Are you blind? It would most likely be a class...
     
Thread Status:
Not open for further replies.

Share This Page