None of my stuff works, please help!

Discussion in 'Plugin Development' started by william9518, Nov 9, 2012.

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

    william9518

    Help, I am not a beginner in java, but i am as a developer of plugins. none of my COD plugin works. please help!

    GunsPlugin - Main:
    Code:
    package com.plugins.gunplugin;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class GunsPlugin extends JavaPlugin {
        public final GunsPluginEventListener listener = new GunsPluginEventListener();
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("smash")){
                Byte blockData = 0x0;
                Player player = (Player) sender;
                Player target = player.getServer().getPlayer(args[0]);
                target.getWorld().spawnFallingBlock(target.getLocation(), Material.BEDROCK, blockData);
                return true;
            }
            return false;
        }
        //enable or disable plugin
        @Override
        public void onEnable(){
            //saying that if enabled
            getLogger().info("GunsPlugin by william9518 is now enabled");
            PluginManager manager = getServer().getPluginManager();
            manager.registerEvents(listener, this);
        }
       
        @Override
        public void onDisable(){
            //saying that if disabled
            getLogger().info("GunsPlugin by william9518 is now disabled");
        }
    }
    
    GunsPluginEventListener:
    Code:
    package com.plugins.gunplugin;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.Plugin;
     
    public class GunsPluginEventListener implements Listener{
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerShoot(PlayerInteractEvent e){
            Player player = e.getPlayer();
            PlayerInventory inv = player.getInventory();
            if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                if(inv.getItemInHand().getTypeId() == 273){
                    Snowball snowball = player.launchProjectile(Snowball.class);
                    snowball.setShooter(player);
                    snowball.setVelocity(player.getLocation().getDirection().multiply(1.5));
                }
            }
        }
        @EventHandler(priority = EventPriority.HIGH)
        public void gernade(PlayerInteractEvent event){
            Player player = event.getPlayer();
            World world = player.getWorld();
            Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("PLUGINNAME");
            if(player.getItemInHand().getType() == Material.SLIME_BALL){
                if(event.getAction() == Action.LEFT_CLICK_AIR){
                    final Item grenade = world.dropItem(player.getEyeLocation(), new ItemStack(Material.SLIME_BALL));
                    plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable(){
                        @Override
                        public void run(){
                            grenade.getWorld().createExplosion(grenade.getLocation(), 0.0F);
                            grenade.remove();
                        }
                    }, 40L);
                }
            }
        }
       
    }
    
    plugin.yml:
    Code:
    name: GunsPlugin
    main: com.plugins.gunplugin.GunsPlugin
    version: 1.0
    commands:
      smash:
          description: Smash someone to death
          usage: /smash [player]
          permission: gunsplugin.smash
          permission-message: You don't have <permission>
    please halp
    - william9518
     
  2. Offline

    cman1885

    You use args without checking if they exist, you cast sender to a player without knowing if the sender is a player, you spawn bedrock as a falling block (wat?)...
     
  3. Offline

    william9518

    the args exist, read it from bukkit plugin wiki. well the cast i could easily fix, and with 1.4, any block can be spawned that way.
     
  4. Offline

    cman1885

    Type "/smash" with that plugin, tell me what happens.
     
  5. Offline

    wowlover6877

    cman1885 is right. You must check to see if the sender of the command typed the args before initializing them. It should look something like this.
    Code:
      @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("smash")){
                Byte blockData = 0x0;
                Player player = (Player) sender;
            if (args.length != 1) {
                return false;
            }
            if (player.getServer.getPlayer(args[0]).isOnline())    {
     
     
                Player target = player.getServer().getPlayer(args[0]);
                target.getWorld().spawnFallingBlock(target.getLocation(), Material.BEDROCK, blockData);
                return true;
              }
            }
            return false;
        }
    Have in mind that everything from the Bukkit Plugin Wiki is not always right, you should read tutorials from multiple websites because then you will find the best way to do something.

    EDIT: Be more specific when somethings not working. You should put the "onCommand" AFTER "oneEnable" and "onDisable".

    william9518
     
  6. Offline

    william9518

    spawns a "falling bedrock" on top of the player you wrote. whatever, i got them all to work! thanks anyway!

    Thanks, it worked! i just had to put the onCommand method after onEnable and onDisable!

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

    wowlover6877

  8. Offline

    cman1885

    if (player.getServer.getPlayer(args[0]).isOnline()) {
    Change that to if(player.getServer().getPlayer(args[0])!=null)
    getPlayer() returns null if the player is not found.
     
Thread Status:
Not open for further replies.

Share This Page