如何从另一个类调用itemStack?

时间:2018-11-07 16:24:51

标签: java plugins minecraft bukkit

我正在制作一个Custom Horse插件,这是我在制作的第一个真正的插件,因此请原谅我的代码混乱。

在一类(HorseSaddles)中,我创建了4种不同的鞍座:

package io.github.bxnie.Items;

import java.util.ArrayList;
import org.bukkit.Material;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import net.md_5.bungee.api.ChatColor;

public class HorseSaddles implements Listener {
    public void DonkeyHorse() {
        ItemStack dhorse = new ItemStack(Material.SADDLE);
        ItemMeta dhorsemeta = dhorse.getItemMeta();

        dhorsemeta.setDisplayName(ChatColor.RED + "Donkey");
        ArrayList<String> dhorselore = new ArrayList<String>();
        dhorselore.add(ChatColor.DARK_GRAY + "1/4");
        dhorselore.add(ChatColor.DARK_GRAY + "Right click to spawn your Donkey!");
        dhorsemeta.setLore(dhorselore);
        dhorse.setItemMeta(dhorsemeta);
    }

    public void BrownHorse() {
        ItemStack brhorse = new ItemStack(Material.SADDLE);
        ItemMeta brhorsemeta = brhorse.getItemMeta();

        brhorsemeta.setDisplayName(ChatColor.RED + "Brown Horse");
        ArrayList<String> brhorselore = new ArrayList<String>();
        brhorselore.add(ChatColor.DARK_GRAY + "2/4");
        brhorselore.add(ChatColor.DARK_GRAY + "Right click to spawn your Horse!");
        brhorsemeta.setLore(brhorselore);
        brhorse.setItemMeta(brhorsemeta);
    }

    public void BlackHorse() {
        ItemStack blhorse = new ItemStack(Material.SADDLE);
        ItemMeta blhorsemeta = blhorse.getItemMeta();

        blhorsemeta.setDisplayName(ChatColor.BLACK + "Black" + ChatColor.RED + "Horse");
        ArrayList<String> blhorselore = new ArrayList<String>();
        blhorselore.add(ChatColor.DARK_GRAY + "3/4");
        blhorselore.add(ChatColor.DARK_GRAY + "Right click to spawn your Horse!");
        blhorsemeta.setLore(blhorselore);
        blhorse.setItemMeta(blhorsemeta);
    }

    public void WhiteHorse() {
        ItemStack whorse = new ItemStack(Material.SADDLE);
        ItemMeta whorsemeta = whorse.getItemMeta();

        whorsemeta.setDisplayName(ChatColor.WHITE + "White" + ChatColor.RED + "Horse");
        ArrayList<String> whorselore = new ArrayList<String>();
        whorselore.add(ChatColor.DARK_GRAY + "4/4");
        whorselore.add(ChatColor.DARK_GRAY + "Right click to spawn your Horse!");
        whorsemeta.setLore(whorselore);
        whorse.setItemMeta(whorsemeta);
    }
}

在另一个类(SaddleCommands)中,我试图创建一个命令,如果用户具有正确的权限,该命令将为用户提供该项目

package io.github.bxnie.commands;

import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

import net.minecraft.server.v1_12_R1.CommandExecute;

public class SaddleCommands extends CommandExecute implements Listener, CommandExecutor{

    public String cmd1 = "fhdonkey";

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] arg3) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("Only players may execute this command!");
            return true;
        }

        Player p = (Player) sender;
        if(cmd.getName().equalsIgnoreCase(cmd1)) {
            if (p.hasPermission("fh.donkey")) {
                p.sendMessage("You Have Purchased a Donkey");
                p.getInventory().addItem(...);
                return true;
            } else {
                p.sendMessage("§cInsufficient Permission!");
                return false;
            }
        }
        return false;
    }
}

在上面写着p.getInventory().addItem(...);的地方,我将3点作为占位符放进去,因为我不知道如何从另一个类中检索项目。

1 个答案:

答案 0 :(得分:1)

欢迎来到论坛:)

您创建了一个类别HorseSaddles。在那里,您有3个函数可以创建ItemStack,而无需对对象进行任何处理。只需返回ItemStack对象即可。

我创建了一个简约的解决方案:

@ViewChild('automcomplete') autocomplete;
//...
//...
clear(control: string): void {
    this.form.get(control).setValue('');
    setTimeout(()=> {
      this.autocomplete.nativeElement.blur();
      this.autocomplete.nativeElement.focus();
    })
}

有关更多信息:Forked Demo