Question

I am trying to make a plugin. This plugin will give a player a special cookie on their first join, which allows them to click it and see their clicks in the lore. I know, it sounds useless but I will add to it. It does not currently work and there are no errors in the console. When I try to right click to use it it simply does nothing. Please help. I am new to Java.

package me.jrneulight.cookieclicker;

import java.util.Arrays;
import java.util.List;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
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.PlayerLoginEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin implements Listener {
    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);

        FileConfiguration config = this.getConfig();
        config.addDefault("players.example", "1");

        config.options().copyDefaults(true);
        saveConfig();
    }
    @Override
    public void onDisable() {

    }
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("cookieclicker")) {

            ItemStack cookie = new ItemStack(Material.COOKIE);
            ItemMeta meta = cookie.getItemMeta();
            String clicked = "0";
            List<String> lore = Arrays.asList("Click me!", "Cookies Clicked: " + clicked);

            meta.setDisplayName(ChatColor.GOLD + "Cookie Clicker");
            meta.setLore(lore);
            cookie.setItemMeta(meta);
            ((HumanEntity) sender).getInventory().addItem(cookie);
            return true;
        }
        return false;
    }
    @EventHandler
    public void onPlayerLoginEvent(PlayerLoginEvent event) {
        Player player = event.getPlayer();
        String pname = player.getDisplayName();
        FileConfiguration config = this.getConfig();
        if(!event.getPlayer().hasPlayedBefore()) {

        ItemStack cookie = new ItemStack(Material.COOKIE);
        ItemMeta meta = cookie.getItemMeta();
        String clicked = "0";
        List<String> lore = Arrays.asList(ChatColor.GRAY + "Click me!", ChatColor.GRAY + "Cookies Clicked: " + clicked);

        meta.setDisplayName(ChatColor.GOLD + "Cookie Clicker");
        meta.setLore(lore);
        cookie.setItemMeta(meta);
        player.getInventory().addItem(cookie);

        config.set("players." + pname, "0");
        saveConfig();
      }
    ItemStack[] contents = player.getInventory().getContents();

    ItemStack cookiestack = null;
    for(ItemStack i: contents) {
        if(i != null) {
            if (i.getType().equals(Material.COOKIE) && i.getItemMeta().getDisplayName().equals(ChatColor.GOLD + "Cookie Clicker")) {
                cookiestack = i;
            }
        }
    }
    String clicked = config.getString("players." + pname);
    ItemMeta cookiestackmeta = cookiestack.getItemMeta();

    List<String> lorenew = Arrays.asList("Click me!", "Cookies Clicked: " + clicked);
    cookiestackmeta.setLore(lorenew);

    }
    public void onPlayerUse(PlayerInteractEvent event){
        Player p = event.getPlayer();
        String pname = p.getDisplayName();
        ItemStack inhand = p.getItemInHand();
        ItemMeta inhandmeta = inhand.getItemMeta();

        if(event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
            if(p.getItemInHand().getType() == Material.COOKIE && inhandmeta.getDisplayName().equals(ChatColor.GOLD + "Cookie Clicker") ){
                FileConfiguration config = this.getConfig();
                if (config.getString("players." + pname) != null ) {
                    String clicked = config.getString("players." + pname);

                    int clickedint = Integer.parseInt(clicked);
                    int clickedintnew = clickedint + 1;

                    config.set("players." + pname, Integer.toString(clickedintnew));
                    saveConfig();


                }
                }
            }
        }
    }
Was it helpful?

Solution

It's because you are not using @EventHandler above your onPlayerUse method. It should look like this:

@EventHandler
public void onPlayerUse(PlayerInteractEvent event){
    //code here
}

Remember, All events need to have @EventHandler before them

OTHER TIPS

If the above answer doesn't work, did forget to add the plugin.yml file?

If you don't add this plugin.yml file, the server will not recognise your plugin.

To do this you simply have to right click 'src' and select 'new' then 'file'. As the name, put 'plugin.yml'. Open the 'plugin.yml' file in Eclipse and use this as your 'plugin.yml' format:

main: me.<yourname>.<pluginname>.<classname>
name: <plugin name> 
version: <version number>
author: <your name> 

commands: 
    <command name>:
        description: <description here>

Since you claim yourself 'new' to Java, I thought that this could be the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top