Bukkit插件编码修复?

时间:2014-02-03 05:58:18

标签: java minecraft bukkit

我正在尝试制作一个插件。这个插件会在玩家的第一次加入时为其提供一个特殊的cookie,这样他们就可以点击它并查看他们在知识中的点击次数。我知道,这听起来没用,但我会加入它。它当前不起作用,并且控制台中没有错误。当我尝试右键单击使用它时,它什么都不做。请帮忙。我是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();


                }
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:3)

这是因为您没有在@EventHandler方法之上使用onPlayerUse。它应该是这样的:

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

请记住,所有活动都需要@EventHandler面前

答案 1 :(得分:0)

如果上述答案无效,是否忘记添加 plugin.yml 文件?

如果您没有添加此plugin.yml文件,服务器将无法识别您的插件。

要做到这一点,您只需右键点击“src&#39;并选择新的&#39;然后&#39;文件&#39;。作为名称,添加&#39; plugin.yml&#39; 。打开&#39; plugin.yml&#39;在Eclipse中使用此文件作为&#39; plugin.yml&#39;格式:

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

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

既然你声称自己是新的&#39;对Java来说,我认为这可能是问题所在。