gpt4 book ai didi

java - 具有特别命名项目的形状配方?

转载 作者:行者123 更新时间:2023-11-30 08:22:52 40 4
gpt4 key购买 nike

我正在尝试使用 bukkit 创建一个制作配方,我希望该配方只接受名称为“Better Helmet”的皮革 Helm 。现在我有这个:

public static ItemStack lvl2Head = new ItemStack(Material.LEATHER_HELMET, 1);
{
//removed unnessecary information
lvl2HeadMeta.setDisplayName("Better Helmet 2");
}
public void lvl1ArmorHead() {
ShapedRecipe recipe = new ShapedRecipe(lvl2Head);
recipe.shape("AAA", "ABA", "AAA");
recipe.setIngredient('A', Material.DIAMOND);
//want it to check it under here in place of "LEATHER_HELMET"
recipe.setIngredient('B', Material.LEATHER_HELMET);
this.getServer().addRecipe(recipe);
}

有什么办法可以做到这一点吗?我尝试用新 ItemStack 的名称代替 Material.LEATHER_HELMET 但它需要 MaterialData 而不是 ItemStack

更新

我仍然能够使用使用 jojodmo 创建的普通皮革 Helm 将元素从工作台中取出。

主类:

public static ShapedRecipe lvl1ArmorHeadRecipe() {
ShapedRecipe recipe = new ShapedRecipe(lvl1Head);
recipe.shape("AAA", "ABA", "AAA");
recipe.setIngredient('A', Material.DIAMOND);
recipe.setIngredient('B', Material.LEATHER_HELMET);
return recipe;
}
public void lvl1ArmorHead(){
this.getServer().addRecipe(lvl1ArmorHeadRecipe());
}

事件处理器类:

@EventHandler
public void craft(CraftItemEvent e){
if(e.getInventory() instanceof CraftingInventory){
CraftingInventory inv = (CraftingInventory) e.getInventory();
if(inv.getSize() != 4 && e.getRecipe().equals(Main.lvl1ArmorHeadRecipe())){
org.bukkit.inventory.ItemStack helm = inv.getMatrix()[5];
if(helm.hasItemMeta()){
if(helm.getItemMeta().getDisplayName().equals("Better Helmet")){
//done.
} else{
e.setCancelled(true);
}
} else {
e.setCancelled(true);
}
}
}
}

注意:这是针对 Bukkit 1.7.2

最佳答案

我以前做过这个,我花了很长时间才弄明白怎么做!唯一的问题是,如果您使用普通的皮革 Helm ,结果仍然会显示,但它不会让您将结果从工作台中取出。

这是我的做法:

public ShapedRecipie lvl1ArmorHeadRecipie(){
ShapedRecipe recipe = new ShapedRecipe(lvl2Head);
recipe.shape("AAA", "ABA", "AAA");
recipe.setIngredient('A', Material.DIAMOND);
recipe.setIngredient('B', Material.LEATHER_HELMET);
return recipie;
}

public void lvl1ArmorHead(){
this.getServer().addRecipe(lvl1ArmorHeadRecipie());
//do everything in here normally
}

接下来,使用这个。确保使该类位于 implement Listener

@EventHandler
public void craft(CraftItemEvent e){
if(e.getInventory() instanceof CraftingInventory){
CraftingInventory inv = (CraftingInventory) e.getInventory();
if(inv.getSize() != 4 && e.getRecipe().equals(lvl1ArmorHelmetRecipe())){
ItemStack helm = inv.getMatrix()[4];//get the middle item in the bench, which is the helmet
if(helm.hasItemMeta()){//make sure the helmet has item meta
if(helm.getItemMeta().getDisplayName().equals("Better Helmet")){//make sure the helmet's display name is 'Better Helmet'
//you're done! It works! Do something like tell the player "you have crafted better armor" or something here.
return;
}
}
//the return; above would have been called if the crafting had succeeded. When it got called, the remainder of this method would not run (this part will not be run if the crafting has succeeded)
//send the player a message to make it more realistic here. For my wizardry server I use: 'One of thee items used was incorrect, and unbalanced the energy. The death block hath been destroyed'
e.setCanceled(true);
e.setResult(new ItemStack(Material.AIR));
}
}
}

关于java - 具有特别命名项目的形状配方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24103281/

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