gpt4 book ai didi

java - 如何从另一个类调用 itemStack?

转载 作者:行者123 更新时间:2023-12-02 10:37:06 26 4
gpt4 key购买 nike

我正在制作一个自定义马插件,这是我制作的第一个真正的插件,所以请原谅我凌乱的代码。

在一个类(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点作为占位符,因为我不知道如何从另一个类中检索项目。

最佳答案

欢迎来到论坛:)

您创建了一个 HorseSaddles 类。在那里你有 3 个函数来创建一个 ItemStack,而不用对对象做任何事情。只需返回 ItemStack 对象即可。

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

public class Horse {
// we create an function which returns an ItemStack
public ItemStack getBrownHorseItemStack() {
// Create the ItemStack Object
ItemStack horseItem = new Itemstack(Material.SADDLE);
// return our ItemStack Object
return horseItem;
}
}

class Saddle {

private Horse horse = new Horse();

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
// an CommandSender object is NOT always an Player Object. Normally we would check if sender is a Player Object
// We cast the CommandSender to an player
Player player = (Player) sender;
// check the command received
if (cmd.getName().equalsIgnoreCase("fhdonkey")) {
// horse.getBrownHorseItemStack() will return an ItemStack Object
player.getInventory().addItem(horse.getBrownHorseItemStack());
}
return false;
}
}

了解更多信息:Returning a Value from a Method

关于java - 如何从另一个类调用 itemStack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53193655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com