spawners

Discussion in 'Plugin Development' started by rafaeltab, Oct 9, 2016.

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

    rafaeltab

    hey guys i am making a new plugin called Spawners, its a plugin that makes it so that if you have a spawner with the name <entitytype> spawner and the lore : common,rare,epic,Legendary it will change the type of the spawner to the type specified in the name and the spawn delay of the specified level. to test if it worked i added a command: /givespawner but now if i run the command it dousnt work this is my code:
    Code:
    package me.rafaeltab.spawners.Main;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.CreatureSpawner;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main1 extends JavaPlugin implements Listener{
    
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Main1 plugin;
    
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled");
        }
    
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled");
            this.getServer().getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
           try{
              Player player = (Player) sender;
        
            if(commandLabel.equalsIgnoreCase("giveSpawner")){
                ItemStack is = new ItemStack(Material.MOB_SPAWNER, 1);
                ItemMeta im = is.getItemMeta();
                im.setDisplayName(ChatColor.YELLOW + args[1] + ChatColor.GRAY + "spawner");
                List<String> list = new ArrayList<String>();
                list.add(args[2]);
                im.setLore(list);
                is.setItemMeta(im);
                player.getInventory().addItem(is);
                player.sendMessage(ChatColor.GREEN + "You have gain a special Mob_Spawner!");
            }
        
            
            }catch (NullPointerException ex){
            
            }
           return false;
        }
    
    
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent event){
            try{
                Player player = event.getPlayer();     
            if(event.getBlock().getType() == Material.MOB_SPAWNER){
                ItemMeta im = player.getItemInHand().getItemMeta();
                List<String> lore = im.getLore();
                if(lore.contains("Common")){           
                    int delay = 1;
                    Block block = event.getBlock();
                    CreatureSpawner spawner = (CreatureSpawner) block.getState();
                    spawner.setDelay(delay);           
                    String Name = im.getDisplayName();
                
                    if(Name.equals(ChatColor.YELLOW + "Spider" + ChatColor.GRAY + "spawner")){               
                        spawner.setSpawnedType(EntityType.SPIDER);
                    }else if(Name.equals(ChatColor.YELLOW + "Skeleton" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.SKELETON);
                    }else if(Name.equals(ChatColor.YELLOW + "IronGolem" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.IRON_GOLEM);
                    }else if(Name.equals(ChatColor.YELLOW + "Creeper" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.CREEPER);    
                    }else if(Name.equals(ChatColor.YELLOW + "Blaze" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.BLAZE);
                    }else if(Name.equals(ChatColor.YELLOW + "Bat" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.BAT);
                    }else if(Name.equals(ChatColor.YELLOW + "CaveSpider" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.CAVE_SPIDER);
                    }else if(Name.equals(ChatColor.YELLOW + "Chicken" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.CHICKEN);
                    }else if(Name.equals(ChatColor.YELLOW + "Cow" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.COW);
                    }else if(Name.equals(ChatColor.YELLOW + "EnderDragon" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.ENDER_DRAGON);
                    }else if(Name.equals(ChatColor.YELLOW + "Enderman" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.ENDERMAN);
                    }else if(Name.equals(ChatColor.YELLOW + "Ghast" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.GHAST);
                    }else if(Name.equals(ChatColor.YELLOW + "Guardian" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.GUARDIAN);
                    }else if(Name.equals(ChatColor.YELLOW + "Horse" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.HORSE);
                    }else if(Name.equals(ChatColor.YELLOW + "MagmaSlime" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.MAGMA_CUBE);
                    }else if(Name.equals(ChatColor.YELLOW + "Mooshroom" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.MUSHROOM_COW);
                    }else if(Name.equals(ChatColor.YELLOW + "Ocelot" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.OCELOT);
                    }else if(Name.equals(ChatColor.YELLOW + "Pig" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.PIG);
                    }else if(Name.equals(ChatColor.YELLOW + "ZombiePigman" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.PIG_ZOMBIE);
                    }else if(Name.equals(ChatColor.YELLOW + "Rabbit" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.RABBIT);
                    }else if(Name.equals(ChatColor.YELLOW + "Sheep" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.SHEEP);
                    }else if(Name.equals(ChatColor.YELLOW + "Silverfish" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.SILVERFISH);
                    }else if(Name.equals(ChatColor.YELLOW + "Slime" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.SLIME);
                    }else if(Name.equals(ChatColor.YELLOW + "SnowMan" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.SNOWMAN);
                    }else if(Name.equals(ChatColor.YELLOW + "Squid" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.SQUID);
                    }else if(Name.equals(ChatColor.YELLOW + "Villager" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.VILLAGER);
                    }else if(Name.equals(ChatColor.YELLOW + "Witch" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.WITCH);
                    }else if(Name.equals(ChatColor.YELLOW + "Wither" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.WITHER);
                    }else if(Name.equals(ChatColor.YELLOW + "Wolf" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.WOLF);
                    }else if(Name.equals(ChatColor.YELLOW + "Zombie" + ChatColor.GRAY + "spawner")){
                        spawner.setSpawnedType(EntityType.ZOMBIE);
                    }                 
                    }
                }   
            }catch (NullPointerException ex){
            }
        }
    
    
    }
    
    and the server log :
    Code:
    11:35:02] [Server thread/INFO]: Wheat Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Mob Spawn Range: 4
    [11:35:02] [Server thread/INFO]: -------- World Settings For [world_nether] --------
    [11:35:02] [Server thread/INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
    [11:35:02] [Server thread/INFO]: Arrow Despawn Rate: 1200
    [11:35:02] [Server thread/INFO]: Chunks to Grow per Tick: 650
    [11:35:02] [Server thread/INFO]: Clear tick list: false
    [11:35:02] [Server thread/INFO]: Experience Merge Radius: 3.0
    [11:35:02] [Server thread/INFO]: View Distance: 6
    [11:35:02] [Server thread/INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64
    [11:35:02] [Server thread/INFO]: Alternative Hopper Ticking: false
    [11:35:02] [Server thread/INFO]: Hopper Transfer: 8 Hopper Check: 8 Hopper Amount: 1
    [11:35:02] [Server thread/INFO]: Random Lighting Updates: false
    [11:35:02] [Server thread/INFO]: Structure Info Saving: true
    [11:35:02] [Server thread/INFO]: Sending up to 10 chunks per packet
    [11:35:02] [Server thread/INFO]: Max TNT Explosions: 100
    [11:35:02] [Server thread/INFO]: Item Despawn Rate: 6000
    [11:35:02] [Server thread/INFO]: Item Merge Radius: 2.5
    [11:35:02] [Server thread/INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16
    [11:35:02] [Server thread/INFO]: Zombie Aggressive Towards Villager: true
    [11:35:02] [Server thread/INFO]: Max Entity Collisions: 8
    [11:35:02] [Server thread/INFO]: Custom Map Seeds:  Village: 10387312 Feature: 14357617
    [11:35:02] [Server thread/INFO]: Nerfing mobs spawned from spawners: false
    [11:35:02] [Server thread/INFO]: Anti X-Ray: true
    [11:35:02] [Server thread/INFO]:     Engine Mode: 1
    [11:35:02] [Server thread/INFO]:     Hidden Blocks: [14, 15, 16, 21, 48, 49, 54, 56, 73, 74, 82, 129, 130]
    [11:35:02] [Server thread/INFO]:     Replace Blocks: [1, 5]
    [11:35:02] [Server thread/INFO]: Cactus Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Cane Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Melon Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Mushroom Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Pumpkin Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Sapling Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Wheat Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Mob Spawn Range: 4
    [11:35:02] [Server thread/INFO]: -------- World Settings For [world_the_end] --------
    [11:35:02] [Server thread/INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
    [11:35:02] [Server thread/INFO]: Arrow Despawn Rate: 1200
    [11:35:02] [Server thread/INFO]: Chunks to Grow per Tick: 650
    [11:35:02] [Server thread/INFO]: Clear tick list: false
    [11:35:02] [Server thread/INFO]: Experience Merge Radius: 3.0
    [11:35:02] [Server thread/INFO]: View Distance: 6
    [11:35:02] [Server thread/INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64
    [11:35:02] [Server thread/INFO]: Alternative Hopper Ticking: false
    [11:35:02] [Server thread/INFO]: Hopper Transfer: 8 Hopper Check: 8 Hopper Amount: 1
    [11:35:02] [Server thread/INFO]: Random Lighting Updates: false
    [11:35:02] [Server thread/INFO]: Structure Info Saving: true
    [11:35:02] [Server thread/INFO]: Sending up to 10 chunks per packet
    [11:35:02] [Server thread/INFO]: Max TNT Explosions: 100
    [11:35:02] [Server thread/INFO]: Item Despawn Rate: 6000
    [11:35:02] [Server thread/INFO]: Item Merge Radius: 2.5
    [11:35:02] [Server thread/INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16
    [11:35:02] [Server thread/INFO]: Zombie Aggressive Towards Villager: true
    [11:35:02] [Server thread/INFO]: Max Entity Collisions: 8
    [11:35:02] [Server thread/INFO]: Custom Map Seeds:  Village: 10387312 Feature: 14357617
    [11:35:02] [Server thread/INFO]: Nerfing mobs spawned from spawners: false
    [11:35:02] [Server thread/INFO]: Anti X-Ray: true
    [11:35:02] [Server thread/INFO]:     Engine Mode: 1
    [11:35:02] [Server thread/INFO]:     Hidden Blocks: [14, 15, 16, 21, 48, 49, 54, 56, 73, 74, 82, 129, 130]
    [11:35:02] [Server thread/INFO]:     Replace Blocks: [1, 5]
    [11:35:02] [Server thread/INFO]: Cactus Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Cane Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Melon Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Mushroom Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Pumpkin Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Sapling Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Wheat Growth Modifier: 100%
    [11:35:02] [Server thread/INFO]: Mob Spawn Range: 4
    [11:35:02] [Server thread/INFO]: Preparing start region for level 0 (Seed: 7217449949937026959)
    [11:35:03] [Server thread/INFO]: Preparing spawn area: 43%
    [11:35:03] [Server thread/INFO]: Preparing start region for level 1 (Seed: 8712693226781228967)
    [11:35:04] [Server thread/INFO]: Preparing spawn area: 69%
    [11:35:05] [Server thread/INFO]: Preparing start region for level 2 (Seed: 8712693226781228967)
    [11:35:05] [Server thread/INFO]: [Spawners] Enabling Spawners v1.0
    [11:35:05] [Server thread/INFO]: Spawners Version 1.0 Has Been Enabled
    [11:35:05] [Server thread/ERROR]: [Spawners] Spawners v1.0 attempted to register an invalid EventHandler method signature "public boolean me.rafaeltab.spawners.Main.Main1.onCommand(org.bukkit.command.CommandSender,org.bukkit.command.Command,java.lang.String,java.lang.String[])" in class me.rafaeltab.spawners.Main.Main1
    [11:35:05] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
    [11:35:05] [Server thread/INFO]: Done (3,514s)! For help, type "help" or "?"
    [11:35:40] [User Authenticator #1/INFO]: UUID of player rafaeltab is 4fa5ea70-dd6f-378d-b635-40ef46893bf3
    [11:35:40] [Server thread/INFO]: rafaeltab[/127.0.0.1:54007] logged in with entity id 190 at ([world] 231.2519725262189, 70.0, 219.33966088375826)
    [11:36:04] [Server thread/INFO]: rafaeltab issued server command: /givespawner Spider Common
    [11:36:04] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'givespawner' in plugin Spawners v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-8dc4297-a7287cb]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot.jar:git-Spigot-8dc4297-a7287cb]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:642) ~[spigot.jar:git-Spigot-8dc4297-a7287cb]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1083) [spigot.jar:git-Spigot-8dc4297-a7287cb]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:918) [spigot.jar:git-Spigot-8dc4297-a7287cb]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-8dc4297-a7287cb]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-8dc4297-a7287cb]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-8dc4297-a7287cb]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_73]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_73]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:683) [spigot.jar:git-Spigot-8dc4297-a7287cb]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-8dc4297-a7287cb]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:623) [spigot.jar:git-Spigot-8dc4297-a7287cb]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:526) [spigot.jar:git-Spigot-8dc4297-a7287cb]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
        at me.rafaeltab.spawners.Main.Main1.onCommand(Main1.java:51) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-8dc4297-a7287cb]
        ... 14 more
    [11:37:04] [Server thread/INFO]: rafaeltab lost connection: Disconnected
    [11:37:04] [Server thread/INFO]: rafaeltab left the game.
    what did i do wrong?
     
  2. Offline

    XxTimexX

    @rafaeltab

    You don't need to use logger, Bukkit does it for you. This seems like a BCBroz code. If you are watching them, I'd recommend you to stop it and find someone else (I did the same mistake).

    Code:
    @EventHandler
    public boolean onCommand()
    
    Remove the @EventHandler

    There is no need to use try/catch blocks because the sender in this case cannot be null.
    Seems like someone didn't learn enough of Java. Array indexes start from 0. So it would be args[0], args[1], args[2], args[3] etc...
    Also, where are those args[1] comming from anyway?
     
  3. @rafaeltab Hello BcBroz been a while.

    1. Do not use Javas logger that's why Bukkit has one.
    2. Don't static abuse
    3. The onDisable is not needed, delete it.
    4. No need to log onEnable/onDisable Bukkit does it for you
    5. Why are you doing a try catch? Handle the null correctly instead
    6. CHECK before casting!! If console runs this command it will error
    7. Use cmd.getName not commandLabel
    8. You aren't checking arg length so this will always error if the args length is not 3

    You obviously do not know Java or have any basic knowledge. So please read this, and thank you again @Tecno_Wizard for creating it.
     
  4. Offline

    rafaeltab

    Ok thx guys and i guess im going to learn some more java. See yo later
     
  5. Offline

    Lordloss

    Caused by: java.lang.ArrayIndexOutOfBoundsException

    This line will mostly always tell you whats the issue, and google will tell you what you did wrong.
     
Thread Status:
Not open for further replies.

Share This Page