Solved Giving Item On Join

Discussion in 'Plugin Development' started by top2001, Dec 26, 2014.

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

    top2001

    Hello Bukkit.

    I was wondering why the following code I made isn't giving a compass to the player when they join the server. From what I can see, everything seems correct.

    Here's the code:

    Code:
    package me.top2001.OreCloudServerSelector;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
    
        public void onEnable() {
            getLogger().info("Plugin Enabled!");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
        public void onDisable() {
            getLogger().info("Plugin Disabled!");
        }
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            ItemStack ServerSelector = new ItemStack(Material.COMPASS);
            ItemMeta ServerSelectorMeta = ServerSelector.getItemMeta();
           
            ServerSelectorMeta.setDisplayName(ChatColor.GRAY + "»" + ChatColor.DARK_GREEN + "»" + ChatColor.GRAY + "»" + ChatColor.GREEN + " Server Selector " + ChatColor.GRAY + "«" + ChatColor.DARK_GREEN + "«" + ChatColor.GRAY + "«");
            ArrayList<String> LoreList = new ArrayList<String>();
            LoreList.add(ChatColor.GRAY + "» Select The Server You");
            LoreList.add(ChatColor.GRAY + "  Wish To Play On");
           
            ServerSelectorMeta.setLore(LoreList);
            ServerSelector.setItemMeta(ServerSelectorMeta);
            e.getPlayer().getInventory().setItem(2, ServerSelector);
        }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) {
                final ItemStack currentItem = e.getPlayer().getItemInHand();
                if(currentItem != null && currentItem.getType() != Material.AIR) {
                    if(currentItem.getItemMeta().getDisplayName() == ChatColor.GRAY + "»" + ChatColor.DARK_GREEN + "»" + ChatColor.GRAY + "»" + ChatColor.GREEN + " Server Selector " + ChatColor.GRAY + "«" + ChatColor.DARK_GREEN + "«" + ChatColor.GRAY + "«") {
                        final String prefix = ChatColor.GREEN + "Ore" + ChatColor.GRAY + "Cloud" + ChatColor.DARK_GREEN + " > ";
                        e.getPlayer().sendMessage(prefix + ChatColor.BLUE + "Sorry, this selector is currently in development.");
                    }
                }
            }
        }
    }
    
     
  2. Offline

    JordyPwner

    try to add a delay
    Why do you have final ItemStack?
     
  3. Offline

    mine-care

    Ontop of what was said by @JordyPwner correctly, it's a good dean not create itemstacks each time event is called. This way u recreate the same over and over, keep it loaded on class initialization this way oh reduce lag.. Also u don't need to print enable or disable messages because bulk it does it for you
     
  4. Offline

    DemKazoo

    Code:
    public void onJoin(PlayerJoinEvent e){
    Player p = e.getPlayer();
    
    ItemStack is = new ItemStack(Material.COMPASS);
    ItemMeta im = is.getItemMeta();
    //More itemstack blabla
    
    if(!(p.getInventory().contains(is);
    p.getInventory().addItem(is);
    //^ This checks if the player has a compass already, if not add it
    
    if(p.getInventory().contains(is);
    return;
    
    
    Did this without IDE btw
     
  5. Offline

    ngk578

    Did you do the plugin.yml correctly?
     
  6. @DemKazoo Double check that code ;) I will give you a hint if != ;.

    @top2001 Stacktrace?
     
  7. Offline

    DemKazoo

    @bwfcwalshy Ahh I see, :p

    @ngk578
    I don't think this event issue would've anything to do with the plugin.yml lol

    <Edited by bwfcwalshy: Merged posts, please do not double post instead use the edit button.>
     
  8. @DemKazoo He may have miss-spelt something or done the main class path wrong, ect. Yes events have no place in the plugin.yml doesn't mean there can't be mistakes.
     
  9. Offline

    ZodiacTheories

    bwfcwalshy likes this.
  10. Offline

    top2001

    Here's my Plugin.yml:

    Code:
    name: OreCloudServerSelector
    author: top2001
    version: 1.0
    description: Server Selector.
    main: me.top2001.OreCloudServerSelector.Main
    commands:
    It looks like I had a plugin removing items on join so I tested this plugin on my test server and it gives the player the item. Now what isn't working is when the player right clicks the compass, it doesn't say the message.
     
  11. Offline

    JordyPwner

    Add an delay when the player joins, this will SOLVE it :p
     
  12. Offline

    top2001

    Did you even read my reply?
     
  13. Offline

    JordyPwner

    Did you even read my reply?
    plugin.yml is fine. Just add a little delay and you got it fixed, trust me!
     
  14. Offline

    Experminator

    Code:
      @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
              Bukkit.getServer().getScheduler().scheduleAsyncDelayTask(this, new Runnable(){
                      public void run(){
                            e.getPlayer().getInventory().setItem(2, getSelector());
                      }
             }, 20);
        }
        
        public ItemStack getSelector(){
           ItemStack ServerSelector = new ItemStack(Material.COMPASS);
            ItemMeta ServerSelectorMeta = ServerSelector.getItemMeta();
         
            ServerSelectorMeta.setDisplayName(ChatColor.GRAY + "»" + ChatColor.DARK_GREEN + "»" + ChatColor.GRAY + "»" + ChatColor.GREEN + " Server Selector " + ChatColor.GRAY + "«" + ChatColor.DARK_GREEN + "«" + ChatColor.GRAY + "«");
            ArrayList<String> LoreList = new ArrayList<String>();
            LoreList.add(ChatColor.GRAY + "» Select The Server You");
            LoreList.add(ChatColor.GRAY + "  Wish To Play On");
         
            ServerSelectorMeta.setLore(LoreList);
            ServerSelector.setItemMeta(ServerSelectorMeta);
          
            return ServerSelector;
        }
    
     
  15. Offline

    Mysticate

    @JordyPwner er... kindly re-read the bottom part of the post you commented... @Experminator you too...

    Try this code out.

    Code:
    package me.top2001.OreCloudServerSelector;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
    
    //Since this is a prefix and you'll most likely need it again, we can just have it stored as a variable for simplicity.
    
    private final String prefix = ChatColor.GREEN + "Ore" + ChatColor.GRAY + "Cloud" + ChatColor.DARK_GREEN + " > ";
    
        public void onEnable() {
        //register events.
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
        //Set the item in the third slot on join
            e.getPlayer().getInventory().setItem(2, getSelector());
        }
    
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) {
                ItemStack currentItem = e.getPlayer().getItemInHand();
                //We can just return this if it is air or null
                if (currentItem == null || currentItem.getType() == Material.AIR) return;
                //We want to make sure that the item HAS item meta and a display name before we try to access it.
                if (!currentItem.hasItemMeta() || (currentItem.hasItemMeta() && !currentItem.getItemMeta().hasDisplayName())) return;
                //Now we can actually check the display name. Just for simiplicity let's run it by the getSelector method.
                if (currentItem.getItemMeta().getDisplayName().equals(getSelector().getItemMeta().getDisplayName())) {
                //Let's cancel the event, just because.
                e.setCancelled(true);
                //Let's also update the player's inventory. This isn't really necessary for a compass but it's a good practice to use for more complex things.
                e.getPlayer().updateInventory();
                //And finally, send the message.
                e.getPlayer().sendMessage(prefix + ChatColor.BLUE + "Sorry, this selector is currently in development.");
                return;
            }
        }
    
    
    
        private ItemStack getSelector(){
        ItemStack serverSelector = new ItemStack(Material.COMPASS);
            ItemMeta serverSelectorMeta = serverSelector.getItemMeta();
            serverSelectorMeta.setDisplayName(ChatColor.GRAY + "»" + ChatColor.DARK_GREEN + "»" + ChatColor.GRAY + "»" + ChatColor.GREEN + " Server Selector " + ChatColor.GRAY + "«" + ChatColor.DARK_GREEN + "«" + ChatColor.GRAY + "«");
            ArrayList<String> loreList = new ArrayList<String>();
            loreList.add(ChatColor.GRAY + "» Select The Server You");
            loreList.add(ChatColor.GRAY + "  Wish To Play On");
            serverSelectorMeta.setLore(loreList);
            serverSelector.setItemMeta(serverSelectorMeta);
            return serverSelector;
        }
    }
    
    EDIT: The spacing screwed up between Eclipse and the browser for some reason. I also recommend looking into basic Java conventions; just little things such as spacing and variable naming.
     
    Last edited: Dec 28, 2014
  16. Offline

    top2001

    Nobody is listening to me! It's not the item giving which is the problem. It's the right clicking the compass and it says a message which is the problem. Giving the player the item on join is working. Giving the player the item was never issue I came here for.
     
  17. Offline

    Mysticate

    Please just try my code :/ It should fix the interact issue

    EDIT: I descrewed the spacing; now it's actually readable
     
  18. Offline

    DemKazoo

    I don't see what the issue is, my code worked fine as off the beginning as I posted?
     
  19. Offline

    Experminator

    @top2001 If you are not here for 'Giving item on join', then is the title not useful...
     
  20. Offline

    Mysticate

    Now he wants help with his interact event.
     
  21. @top2001 Can we see the updated code so we can help you further?
     
  22. Offline

    top2001

    Thanks Mysticate. Just the code I wanted :)
     
Thread Status:
Not open for further replies.

Share This Page