Solved Spawn Undead Horse?

Discussion in 'Plugin Help/Development/Requests' started by Meatiex, Jan 7, 2015.

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

    Meatiex

    Heres my code, it doesn't spawn anything...
    Code:
                Horse horse = (Horse) loc.getWorld().spawnCreature(loc, EntityType.HORSE);
                horse.setVariant(Variant.UNDEAD_HORSE);
    Any help is awesome :)
     
  2. Offline

    sirrus86

    World.spawnCreature() has been deprecated for awhile (and may no longer work), try World.spawnEntity() and see if that helps.
     
  3. Offline

    Meatiex

    no, same thing happens... (nothing)
    The code below works, so i'm pretty sure World.spawnCreature() still works
    Code:
        public void spawn(Location loc, String name) {
                loc.getWorld().spawnCreature(loc, EntityType.valueOf(unformat(name)));
        }
     
    Last edited: Jan 7, 2015
  4. Code:
    @SuppressWarnings("deprecation")
    LivingEntity horse = p.getWorld().spawnCreature(p.getLocation(), EntityType.HORSE);
    Variant v = Variant.UNDEAD_HORSE;
    ((Horse) horse).setVariant(v);
    
    this works for me haha try
    hope this helps

    but if it doesn't work send the error or the complete class
     
    Meatiex likes this.
  5. Offline

    Meatiex

    hmm... not working (I can spawn all the other mobs in my code below)
    Code:
        @EventHandler
        public void inv(InventoryClickEvent event) {
            if (Mob.spawnmob.contains(event.getWhoClicked().getName()))
                if (event.getCurrentItem().getType() == Material.MONSTER_EGG) {
                Player player = (Player)event.getWhoClicked();
                spawn(player.getLocation(), unformat(event.getCurrentItem().getItemMeta().getDisplayName()).toUpperCase());
                player.closeInventory();
                event.setCancelled(true);
                }
        }
        public String unformat(String msg) {
            return msg.replace(" ", "_").substring(2);
        }
    
        @SuppressWarnings("deprecation")
        public void spawn(Location loc, String name) {
        
    if (name.equals("UNDEAD_HORSE")) {
                LivingEntity horse = loc.getWorld().spawnCreature(loc, EntityType.HORSE);
                Variant v = Variant.UNDEAD_HORSE;
                ((Horse)horse).setVariant(v);
                return;
            }
                if (EntityType.valueOf(unformat(name)) != null) {
                }
                loc.getWorld().spawnCreature(loc, EntityType.valueOf(unformat(name)));
        }
    Code:
    public static ArrayList<String> spawnmob = new ArrayList<String>();
            public static void cmd(Player player, String[] args, PlayerCommandPreprocessEvent event) {
            event.setCancelled(true);
            spawnmob.add(player.getName());
            Inventory inv = Bukkit.createInventory(null, 36, "&9&lSpawn Mob");
            //Passive
            inv.setItem(0, item("&dPig", 90));
            inv.setItem(1, item("&7Sheep", 91));
            inv.setItem(2, item("&8Cow", 92));
            inv.setItem(3, item("&7Chicken", 93));
            inv.setItem(4, item("&9Squid", 94));
            inv.setItem(5, item("&7Wolf", 95));
            inv.setItem(6, item("&cMushroom Cow", 96));
            inv.setItem(7, item("&eOcelot", 98));
            inv.setItem(8, item("&6Horse", 100));
            inv.setItem(9, item("&8Rabbit", 101));
            inv.setItem(10, item("&8Villager", 120));
            inv.setItem(11, item("&8Bat", 65));
            //Hostile
                inv.setItem(12, item("&2Creeper", 50));
                inv.setItem(13, item("&7Skeleton", 51));
                inv.setItem(14, item("&8Spider", 52));
                inv.setItem(15, item("&3Zombie", 54));
                inv.setItem(16, item("&aSlime", 55));
                inv.setItem(17, item("&7Ghast", 56));
                inv.setItem(18, item("&dPig Zombie", 57));
                inv.setItem(19, item("&8Enderman", 58));
                inv.setItem(20, item("&8Cave Spider", 59));
                inv.setItem(21, item("&7Silverfish", 60));
                inv.setItem(22, item("&6Blaze", 61));
                inv.setItem(23, item("&4Maga Cube", 62));
                inv.setItem(24, item("&5Witch", 66));
                inv.setItem(25, item("&8Endermite", 67));
                inv.setItem(26, item("&7Guardian", 68));
            
                inv.setItem(27, item("&3Giant", 54));
                inv.setItem(28, item("&7Iron Golem", 91));
                inv.setItem(29, item("&7Snowman", 56));
    //TODO Over Here
                inv.setItem(31, item("&7Undead Horse", 55));
                player.openInventory(inv);
        }
    
        public static ItemStack item(String name, int id) {
            ItemStack stack = new ItemStack(Material.MONSTER_EGG, 1, (short)id);
            ItemMeta meta = stack.getItemMeta();
            meta.setDisplayName(Fix.format("&l" + name));
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(Fix.format("&e&lClick To Spawn " + name));
            meta.setLore(lore);
            stack.setItemMeta(meta);
            return stack;
        }    
     
    Last edited: Jan 7, 2015
  6. @Meatiex have you registered the command in the plugin.yml ?
    if the void is on an extra class have you registered the listener ?
    you could also build in every if statement an else statement to check where the error occures and you could also look at the console in the error where it says "Caused by:"

    EDIT: also you have to replace the colorcode i think cause if you set it to uppercase and replace the " " with an underline the "&7" stays i think
     
    Meatiex likes this.
  7. Offline

    Meatiex

    Its not in plugin.yml, but that dosn't matter, Im using PlayerCommandPreprocessEvent all the rest of mobs can be spawned.
    Code:
    [13:14:51] [Server thread/ERROR]: Could not pass event InventoryClickEvent to project4 v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:1586) [Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at net.minecraft.server.v1_8_R1.PacketPlayInWindowClick.a(SourceFile:31) [Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at net.minecraft.server.v1_8_R1.PacketPlayInWindowClick.a(SourceFile:9) [Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_25]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_25]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:683) [Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:623) [Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:526) [Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]
    Caused by: java.lang.IllegalArgumentException: No enum constant org.bukkit.entity.EntityType.UNDEAD_HORSE
        at java.lang.Enum.valueOf(Unknown Source) ~[?:1.8.0_25]
        at org.bukkit.entity.EntityType.valueOf(EntityType.java:17) ~[Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        at me.Meatie.Commands.Listiners.SawnMob_.spawn(SawnMob_.java:44) ~[?:?]
        at me.Meatie.Commands.Listiners.SawnMob_.inv(SawnMob_.java:24) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[Bukkit.jar:git-Bukkit-47b1dff-f233e7d]
        ... 14 more
     
  8. send the class me.Meatie.Commands.Listiners.SawnMob_ cause the error occures at line 44 and 24
    @Meatiex
     
    Meatiex likes this.
  9. Offline

    Meatiex

    Code:
    package me.Meatie.Commands.Listiners;
    
    import me.Meatie.Commands.Mob;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Horse;
    import org.bukkit.entity.Horse.Variant;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    
    public class SawnMob_ implements Listener {
       
        @EventHandler
        public void inv(InventoryClickEvent event) {
            if (Mob.spawnmob.contains(event.getWhoClicked().getName()))
                if (event.getCurrentItem().getType() == Material.MONSTER_EGG) {
                Player player = (Player)event.getWhoClicked();
                spawn(player.getLocation(), unformat(event.getCurrentItem().getItemMeta().getDisplayName()).toUpperCase());
                player.closeInventory();
                event.setCancelled(true);
                }
        }
       
        public String unformat(String msg) {
            return msg.replace(" ", "_").substring(2);
        }
       
        @SuppressWarnings("deprecation")
        public void spawn(Location loc, String name) {
           
            if (name.equals("SKELETON_HORSE")) {
                Horse horse = (Horse) loc.getWorld().spawnEntity(loc, EntityType.HORSE);
                horse.setVariant(Variant.SKELETON_HORSE);
                return;
            } if (name.equals("UNDEAD_HORSE")) {
                LivingEntity horse = loc.getWorld().spawnCreature(loc, EntityType.HORSE);
                Variant v = Variant.UNDEAD_HORSE;
                ((Horse)horse).setVariant(v);
                return;
            } if (name.equals("MULE")) {
                Horse horse = (Horse) loc.getWorld().spawnEntity(loc, EntityType.HORSE);
                horse.setVariant(Variant.MULE);
                return;
            } if (name.equals("DONKEY")) {
                Horse horse = (Horse) loc.getWorld().spawnEntity(loc, EntityType.HORSE);
                horse.setVariant(Variant.DONKEY);
                return;
            }
           
            try {
                if (EntityType.valueOf(unformat(name)) != null) {
                }
                loc.getWorld().spawnCreature(loc, EntityType.valueOf(unformat(name)));
                } catch (Exception e) {}
        }
       
        @EventHandler
        public void exit(InventoryCloseEvent event) {
            if (Mob.spawnmob.contains(event.getPlayer().getName()))
                Mob.spawnmob.remove(event.getPlayer().getName());
        }
       
    }
    
     
  10. @Meatiex
    sorry for the long waiting i got the problem

    this is the problem line

    Code:
    spawn(player.getLocation(), unformat(event.getCurrentItem().getItemMeta().getDisplayName()).toUpperCase().replace("&", "").replace("7", "").replace("§", "").replace("L", ""));
    
    if you only "unformat()" and "toUpperCase()" the name it will reply the name "§7§LUNDEAD_HORSE" else of "UNDEAD_HORSE"

    maybe this is not the best way but it will work too
    Code:
    String s = unformat(event.getCurrentItem().getItemMeta().getDisplayName()).toUpperCase();
    for(int i = 9; i >= 0; i--){
         s = s.replace(i + "", "");
    }
    s = s.replace("§", "");
    s = s.replace("L", "");
    spawn(player.getLocation(), s);
    
    let me know if this solved your problem :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
    Meatiex likes this.
  11. Offline

    1Rogue

    Instead of substringing to remove color codes, use ChatColor's method for it:

    Code:java
    1. String clean = ChatColor.stripColor("Some string");


    I wrote a general cleaner implementation here:

    Code:java
    1. /**
    2. * Converts a minecraft string into an enum-constant standard.
    3. *
    4. * @param raw The raw string
    5. * @return The formatted string
    6. */
    7. private String clean(String raw) {
    8. return ChatColor.stripColor(raw).replace(' ', '_').toUpperCase();
    9. }
    10.  
    11. /**
    12. * Spawns an entity at a specific location according to a specified
    13. * string name or horse variant.
    14. *
    15. * @param loc The location to spawn at
    16. * @param name The name of the entity (or horse variant) to spawn
    17. * @throws IllegalArgumentException if passed name parameter is neither
    18. * an entity type or horse variant
    19. * @return The newly spawned entity
    20. */
    21. private LivingEntity spawn(Location loc, String name) {
    22. name = this.clean(name); //Clean the string passed in
    23. //spawn logic
    24. EntityType type = EntityType.valueOf(name); //Try to get an entity from the name
    25. if (type == null) { //If no entity is found....
    26. if (Variant.valueOf(name) == null) { //Make sure it's a horse variant
    27. //If not, we are unable to spawn based on the information
    28. throw new IllegalArgumentException(String.format("'%s' is not a valid entity or horse variant!", name));
    29. }
    30. type = EntityType.HORSE; //If no error is thrown, then it is a horse variant
    31. }
    32. LivingEntity e = loc.getWorld().spawnEntity(loc, type); //Spawn the entity
    33. if (type == EntityType.HORSE) { //Check the type again here to avoid casting non-horses to horses.
    34. Variant v = Variant.valueOf(name); //Get the horse variant
    35. if (v != null) { //Check if input is either bad or "HORSE"
    36. ((Horse) e).setVariant(v); //Set the variant
    37. }
    38. }
    39. return e; //Return the value
    40. }


    This will take one of your string arguments (without any of your own cleaning) and spawn the animal:

    Code:java
    1. //Prereq: 'loc' is of type org.bukkit.Location
    2. this.spawn(loc, "magma cube");
    3. this.spawn(loc, "undead horse");
    4. this.spawn(loc, "§cEnder§edragon"); //Note color characters are §, not &


    The real thing to learn from this is to design your methods to be future-proofed and still work farther down the line. This will work if new horse variants or entities are added (however will not work for variants of other new entities that could be added).

    This method would replace every occurance of a number and letter L in the string. Would not work in the slightest.
     
    Last edited: Jan 7, 2015
    Meatiex likes this.
  12. Offline

    Meatiex

    @sn1cko @1Rogue
    Thank you, it works perfectly now :D
    Code:
        private String unformat(String raw) {
           return ChatColor.stripColor(raw).replace(' ', '_').toUpperCase();
        }
    
        public void spawn(Location loc, String name) {
            if (name.equals("SKELETON_HORSE") || name.equals("UNDEAD_HORSE") || name.equals("MULE") || name.equals("DONKEY")) {
                spawnHorse(loc, name);
                return;
            }
        loc.getWorld().spawnCreature(loc, EntityType.valueOf(unformat(name)));
        }
    
        public void spawnHorse(Location loc, String name) {
            if (Variant.valueOf(name) == null) return;
            Entity e = loc.getWorld().spawnEntity(loc, EntityType.HORSE);
            ((Horse)e).setVariant(Variant.valueOf(name));
        }
    FYI I was using & instead of § because I was using this method I use for all chat on my server:
    Thanks Tho :p
    Code:
        Fix.format("&l" + name)
    
    public static String format(String input) {
            return input.replace("(heart)", "❤").replace("(check)", "✔").replace("(music)", "♪")
                    .replace("(plane)", "✈").replace("(x)", "✘").replace("(pencil)", "✎")
                    .replace("(mail)", "✉").replace("(right)", "➡").replace("(music2)", "♫").replace("(snowflake)", "❄")
                    .replace("(tempf)", "℉").replace("(tempc)", "℃").replace("&", "§");
        }
    Fun Fact: If you Hold ALT and Press '21' on the keypad (square one on the right) it will type in § :D

    Any way to set killer rabbits or Elder Guardian?
     
    Last edited: Jan 7, 2015
  13. Offline

    1Rogue

    You should not be placing section symbols (§) in your code directly, you should use the ChatColor enum. You also shouldn't use .replace("&", "§"), what if I want an ampersand in my text?
     
    Meatiex likes this.
  14. Offline

    Meatiex

    Like this?
    Code:
    ChatColor.translateAlternateColorCodes('&', msg);
     
    1Rogue likes this.
  15. jep :) i think thats what he ment
     
    Meatiex likes this.
Thread Status:
Not open for further replies.

Share This Page