Forge-MDK我的世界模组-物品Plus(1.19)-day2

回顾启新

在day1中我们创建了自己的物品GodStickItem,但是在游戏中只是看到了我们的物品,没有任何实际操作的用途,今天我们将为他赋予一些功能。

在注册时赋予基础属性

在Forge中,有一些属性可以再注册的时候进行赋予

注册物品时属性代码解析

在我们继承Item之后在day1中我们使用idea快捷生成的构造方法,在物品注册时,new TestItem(new Item.Properties())需要传入一个new Item.Properties()这个就是我们可以操作的物品属性

    public TestItem(Properties p_41383_) {
        super(p_41383_);
    }

我们可以按住Ctrl键点击new Item.Properties()查看源码

public static class Properties {
      int maxStackSize = 64;
      int maxDamage;
      @Nullable
      Item craftingRemainingItem;
      @Nullable
      CreativeModeTab category;
      Rarity rarity = Rarity.COMMON;
      @Nullable
      FoodProperties foodProperties;
      boolean isFireResistant;
      private boolean canRepair = true;

      public Item.Properties food(FoodProperties p_41490_) {
         this.foodProperties = p_41490_;
         return this;
      }

      public Item.Properties stacksTo(int p_41488_) {
         if (this.maxDamage > 0) {
            throw new RuntimeException("Unable to have damage AND stack.");
         } else {
            this.maxStackSize = p_41488_;
            return this;
         }
      }

      public Item.Properties defaultDurability(int p_41500_) {
         return this.maxDamage == 0 ? this.durability(p_41500_) : this;
      }

      public Item.Properties durability(int p_41504_) {
         this.maxDamage = p_41504_;
         this.maxStackSize = 1;
         return this;
      }

      public Item.Properties craftRemainder(Item p_41496_) {
         this.craftingRemainingItem = p_41496_;
         return this;
      }

      public Item.Properties tab(CreativeModeTab p_41492_) {
         this.category = p_41492_;
         return this;
      }

      public Item.Properties rarity(Rarity p_41498_) {
         this.rarity = p_41498_;
         return this;
      }

      public Item.Properties fireResistant() {
         this.isFireResistant = true;
         return this;
      }

      public Item.Properties setNoRepair() {
         canRepair = false;
         return this;
      }
   }

这些就是我们可以为物品修改的方法
在这里插入图片描述
.craftRemainder():入参为Item,设置我们的物品在制作之后剩下什么物品,入参Item就是剩余物品

.tab():入参为CreativeModeTab,可以Ctrl点击查看可用使用那些栏目,表示物品会在那个栏目中展示

.stacksTo():入参为正整数,设置物品的最大堆叠数量,默认是64,阅读源码可以发现,在物品拥有损坏值(maxDamage)时,设置堆叠数(maxStackSize)会报错

.defaultDurability():入参为正整数,设置物品默认耐久,阅读源码可以发现,当物品的损坏值(maxDamage)=0的时候调用durability()设置耐久,设置的是损坏值(maxDamage),同时会把堆叠数(maxStackSize)设置为1

ps:损耗值应该就是耐久值

.fireResistant():没有入参,设置物品是否防火

.durability():我们可以直接调用这个方法设置耐久(maxDamage),默认把堆叠数(maxStackSize)设置为1

.rarity():设置物品稀有等级

.setNoRepair():设置物品不可修复

.food():需要new FoodProperties.Builder()设置食物属性,结尾一定是.build()
在这里插入图片描述

食物属性代码解析

按住Ctrl键点击new FoodProperties.Builder()查看源码

public static class Builder {
      private int nutrition;
      private float saturationModifier;
      private boolean isMeat;
      private boolean canAlwaysEat;
      private boolean fastFood;
      private final List<Pair<java.util.function.Supplier<MobEffectInstance>, Float>> effects = Lists.newArrayList();

      public FoodProperties.Builder nutrition(int p_38761_) {
         this.nutrition = p_38761_;
         return this;
      }

      public FoodProperties.Builder saturationMod(float p_38759_) {
         this.saturationModifier = p_38759_;
         return this;
      }

      public FoodProperties.Builder meat() {
         this.isMeat = true;
         return this;
      }

      public FoodProperties.Builder alwaysEat() {
         this.canAlwaysEat = true;
         return this;
      }

      public FoodProperties.Builder fast() {
         this.fastFood = true;
         return this;
      }

      public FoodProperties.Builder effect(java.util.function.Supplier<MobEffectInstance> effectIn, float probability) {
          this.effects.add(Pair.of(effectIn, probability));
          return this;
       }

      // Forge: Use supplier method instead
      @Deprecated
      public FoodProperties.Builder effect(MobEffectInstance p_38763_, float p_38764_) {
         this.effects.add(Pair.of(() -> p_38763_, p_38764_));
         return this;
      }

      public FoodProperties build() {
         return new FoodProperties(this);
      }
   }

这些都是食物可以设置的属性

.saturationMod():入参为浮点数,设置食物增加的饱食度

.meat():设置食物是肉

.nutrition():设置食物营养等级

.alwaysEat():设置食物可以一直吃

.fast():设置食物是速食

.effect():入参是new MobEffectInstance()和一个浮点数,设置食用物品后获得的效果

.build():构建,必须在最后加

运用

public static final RegistryObject<Item> GodStickItem = ITEMS.register("god_stick_item", () -> new GodStickItem(new Item.Properties().defaultDurability(20).tab(CreativeModeTab.TAB_TOOLS).fireResistant().rarity(Rarity.COMMON)));

创建一个默认耐久为20,在工具栏,稀有等级为普通,并且防火的物品(day1中的物品注册)

在物品类中实现功能

所有的物品都是继承Item的,所以通过重写Item中的方法可以实现自己想要的效果

编辑物品展示文本

生成复写方法

在我们物品类中输入appendHoverText可以快速生成复写方法
在这里插入图片描述
生成的代码如下

    @Override
    public void appendHoverText(ItemStack p_41421_, @Nullable Level p_41422_, List<Component> p_41423_, TooltipFlag p_41424_) {
        super.appendHoverText(p_41421_, p_41422_, p_41423_, p_41424_);
    }

生成的名字等可以按照个人习惯决定要不要修改,修改后如下

    @Override
    public void appendHoverText(@NotNull ItemStack itemStack, @Nullable Level level, @NotNull List<Component> componentList, @NotNull TooltipFlag tooltipFlag) {
        super.appendHoverText(itemStack, level, componentList, tooltipFlag);
    }

其中componentList是我们主要操作的对象,它可以编辑鼠标悬浮在物品上的时候展示的文本

componentList新增描述

由于componentList是一个列表,我们可以通过.add()新增描述,如下图:

    @Override
    public void appendHoverText(@NotNull ItemStack itemStack, @Nullable Level level, @NotNull List<Component> componentList, @NotNull TooltipFlag tooltipFlag) {
        componentList.add(Component.literal("第一个物品"));
        super.appendHoverText(itemStack, level, componentList, tooltipFlag);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值