Headgrabber help

Discussion in 'Plugin Development' started by generilisk, Dec 1, 2012.

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

    generilisk

    Hey, everyone. I'm writing my first plugin, Headgrabber. I've got most of it working, the commands and so on, but I'm trying to add a drop when a player dies. I don't get any errors, using the code I've shown below, but the head drop doesn't happen. I had trouble following the event listener tutorial, so I'm fairly sure that's where I'm doing it wrong.

    Anyways, here's the code:

    Code:
    package com.globaldominationhq.headgrabber;
     
    import java.io.IOException;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.inventory.CraftItemStack;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.entity.PlayerDeathEvent;
     
     
    public final class HeadGrabber extends JavaPlugin {
        HeadSet HeadSetObject = new HeadSet();
       
        public void onEnable(){
            getLogger().info("HeadGrabber is enabled, enjoy!");
            this.saveDefaultConfig();//creates the default config file as outlined in config.yml
            try {
                Metrics metrics = new Metrics(this);
                metrics.start();
            } catch (IOException e) {
                //failed to submit the stats :(
            }
        }
       
        public void onDisable(){
            getLogger().info("HeadGrabber is disabled, have a nice day!");
        }
        @EventHandler
        public void playerDeath(PlayerDeathEvent event){
            String headName = event.getEntity().getName();
            CraftItemStack head;
                head = HeadSetObject.headSet(headName);
                event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(), head);
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        //sender is the person who typed the command, cmd is the command used, label is the alias used, and String[] args is an array of the arguments, starting with args[0]
           
            if(cmd.getName().equalsIgnoreCase("headgrabber")){ //watch for the command "headgrabber"
                if (!(sender instanceof Player)) {//check to make sure this isn't the console.
                    sender.sendMessage("This command cannot be run from the console.");//if it is, let him down gently
                    } else {//otherwise
                        Player player = (Player) sender; //set the player variable to the person who executed the command
                        if (args.length == 0){//no player argument given
                            if (player.hasPermission("headgrabber.self")){//permission check
                                //give the player their head
                                PlayerInventory inventory = player.getInventory();//call the players inventory
                                String headName = sender.getName(); //set the name of the player who used the command to the String variable playername
                                CraftItemStack head;
                                    head = HeadSetObject.headSet(headName);
                                inventory.addItem(head);//adds the head to their inventory
                                player.sendMessage(HeadGrabber.this.getConfig().getString("headgrabber.can.own"));//message to let the player know it worked
                            }else{
                                sender.sendMessage(HeadGrabber.this.getConfig().getString("headgrabber.cannot.own"));//you're not good enough!
                                return false;
                            }
                        }
                        if (args.length == 1){
                            if (player.hasPermission("headgrabber.others")){//permission check
                                //give the player the specified head
                                String headName = args[0];
                                PlayerInventory inventory = player.getInventory();//call the players inventory
                                CraftItemStack head;
                                    head = HeadSetObject.headSet(headName);
                               
                                inventory.addItem(head);//adds the head to their inventory
                                player.sendMessage(HeadGrabber.this.getConfig().getString("headgrabber.can.other"));//message to let the player know it worked
                            }else{
                                sender.sendMessage(HeadGrabber.this.getConfig().getString("headgrabber.cannot.other"));//you're not good enough!
                                return false;
                            }   
                        }
                        if (args.length > 1){
                            sender.sendMessage("Invalid arguments, specify one head at most.");
                            return false;
                        }
                    }
                return true;//If the plugin runs successfully, function will return true.
            }         
            return false;// If this hasn't happened the a value of false will be returned.
        }
     
    }
    I'm also having an issue where if you have a player head in your inventory already, it stacks the new head into the same inventory slot, and the new head's tag is lost, so you have two of the same. For example:
    Code:
    /headgrabber Joe
    /headgrabber Jim
    will make you end up with two Joe heads.
    Any help would be appreciated, thank you.
     
  2. Offline

    generilisk

    Anybody? I've tried a few things, but none of them have worked, so far.
     
  3. Offline

    gomeow

    Could you explain more what the plugin does? I am confused
     
  4. Offline

    generilisk

    Sure! It adds the /headgrabber command, which gives the player the head specified. For example, /headgrabber Jim will give Jim's head. I'm trying to add heads dropping when a player dies.
    The HeadSet Method is in another class file; I don't have any issues with it so I didn't include it, but in case it helps:
    Code:
            private CraftItemStack headSet(String whosHead){
                    CraftItemStack tempHead;//create a CraftItemStack
                            tempHead = new CraftItemStack(Material.SKULL_ITEM,1,(short)3);//set the CraftItemStack "head" to the Skull item
                    NBTTagCompound tempHeadNBT = new NBTTagCompound();//create an NBT tag
                    tempHeadNBT.setString("SkullOwner", whosHead);//Set the NBT tag "tempHeadNBT"'s "SkullOwner" string to whosHead
                    tempHead.getHandle().tag = tempHeadNBT;//Apply the tempHeadNBT to the tempHead CraftItemStack
                    return tempHead;
            }
    
     
  5. Offline

    gomeow

    Define head. Is it the skin? Or what?
     
  6. Offline

    chasechocolate

    gomeow the skull/head of the player. http://www.minecraftwiki.net/wiki/Mob_head

    generilisk Maybe try adding
    Code:
    return true;
    to your code. Should be like this:
    Code:
    if (player.hasPermission("headgrabber.others")){//permission check
                                //give the player the specified head
                                String headName = args[0];
                                PlayerInventory inventory = player.getInventory();//call the players inventory
                                CraftItemStack head;
                                    head = HeadSetObject.headSet(headName);
                             
                                inventory.addItem(head);//adds the head to their inventory
                                player.sendMessage(HeadGrabber.this.getConfig().getString("headgrabber.can.other"));//message to let the player know it worked
                                return true;
                          }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  7. Offline

    generilisk

    Unfortunately, that didn't work for either issue. No errors, just...no change.

    Ok, let me ask a different way:
    Question 1: I'd like to drop a block when a player dies. How do I make that happen? I thought I'd followed the Listener tutorial, but apparently not well enough.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page