ksgfk
Forge版本:forge-1.12.2-14.23.4.2705
自己做了一把剑,想让它在被使用一次后加上已有的NBT标签,比如Unbreakable这种
  1. item.setTagCompound(new NBTTagCompound());
复制代码

这样做似乎只是给它加个栏位
找不到NBT的教程...

Himmelt
先构造一个nbtcompound,然后往里放东西,放好之后,再setNBTCompound

Himmelt
不过应该先获取nbt,然后修改获取到的nbt

1850343382
如何添加nbt至你的物品?这个应该可以帮得到你
https://emxtutorials.wordpress.com/adding-nbt-data-to-items/

ksgfk
Himmelt 发表于 2018-7-27 08:20
不过应该先获取nbt,然后修改获取到的nbt

还有个问题,部分mod会在物品上写上一些介绍,就像核电工艺的聚变堆核心有一大串街上,这是怎么做到的,还是nbt吗

Himmelt
ksgfk 发表于 2018-7-31 08:31
还有个问题,部分mod会在物品上写上一些介绍,就像核电工艺的聚变堆核心有一大串街上,这是怎么做到的, ...

重写Item的方法
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean advanced);


ksgfk
Himmelt 发表于 2018-7-31 14:19
重写Item的方法
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer pla ...

我在给addInformation方法的List添加元素的时候,一直提示我错误....
我想在这里面添上我自己设的nbt
  1.     @SideOnly(Side.CLIENT)
  2.     public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
  3.         String a = Integer.toString(stack.getTagCompound().getInteger("levelup"));
  4.         tooltip.add(a);
  5.     }
复制代码

然后控制台报错
  1. [main/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:629]: ---- Minecraft Crash Report ----
  2. // Why did you do that?

  3. Time: 8/2/18 10:05 AM
  4. Description: Initializing game

  5. java.lang.NullPointerException: Initializing game
  6.         at com.github.ksgfk.oceanheart.objects.tools.ToolSwordTrailblazer.addInformation(ToolSwordTrailblazer.java:95)
  7.         at net.minecraft.item.ItemStack.getTooltip(ItemStack.java:811)
  8.         at net.minecraft.client.Minecraft.lambda$populateSearchTreeManager$1(Minecraft.java:625)
  9.         at net.minecraft.client.util.SearchTree.index(SearchTree.java:93)
  10.         at net.minecraft.client.util.SearchTree.add(SearchTree.java:78)
  11.         at java.lang.Iterable.forEach(Iterable.java:75)
  12.         at net.minecraft.client.Minecraft.populateSearchTreeManager(Minecraft.java:639)
  13.         at net.minecraft.client.Minecraft.init(Minecraft.java:575)
  14.         at net.minecraft.client.Minecraft.run(Minecraft.java:421)
  15.         at net.minecraft.client.main.Main.main(Main.java:118)
  16.         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  17.         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  18.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  19.         at java.lang.reflect.Method.invoke(Method.java:498)
  20.         at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
  21.         at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
  22.         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  23.         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  24.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  25.         at java.lang.reflect.Method.invoke(Method.java:498)
  26.         at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
  27.         at GradleStart.main(GradleStart.java:25)


  28. A detailed walkthrough of the error, its code path and all known details is as follows:
  29. ---------------------------------------------------------------------------------------

  30. -- Head --
  31. Thread: Client thread
  32. Stacktrace:
  33.         at com.github.ksgfk.oceanheart.objects.tools.ToolSwordTrailblazer.addInformation(ToolSwordTrailblazer.java:95)
  34.         at net.minecraft.item.ItemStack.getTooltip(ItemStack.java:811)
  35.         at net.minecraft.client.Minecraft.lambda$populateSearchTreeManager$1(Minecraft.java:625)
  36.         at net.minecraft.client.util.SearchTree.index(SearchTree.java:93)
  37.         at net.minecraft.client.util.SearchTree.add(SearchTree.java:78)
  38.         at java.lang.Iterable.forEach(Iterable.java:75)
  39.         at net.minecraft.client.Minecraft.populateSearchTreeManager(Minecraft.java:639)
  40.         at net.minecraft.client.Minecraft.init(Minecraft.java:575)

  41. -- Initialization --
  42. Details:
  43. Stacktrace:
  44.         at net.minecraft.client.Minecraft.run(Minecraft.java:421)
  45.         at net.minecraft.client.main.Main.main(Main.java:118)
  46.         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  47.         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  48.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  49.         at java.lang.reflect.Method.invoke(Method.java:498)
  50.         at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
  51.         at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
  52.         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  53.         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  54.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  55.         at java.lang.reflect.Method.invoke(Method.java:498)
  56.         at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
  57.         at GradleStart.main(GradleStart.java:25)
复制代码

大佬这是怎么回事...

的风格好
本帖最后由 的风格好 于 2018-8-2 11:40 编辑

getInteger("levelup") 产生了NullPointerException
try catch 或者 看看tag里有没有"levelup"




ksgfk
本帖最后由 ksgfk 于 2018-8-2 13:23 编辑
的风格好 发表于 2018-8-2 11:33
getInteger("levelup") 产生了NullPointerException
看看tag里有没有"levelup"

tag里的"levelup"是在攻击以后才会被添加上去....所以初始化的时候才会报错嘛....
那如何让物品直接附带一个tag
-----------------------------------------------------------------

解决了,
  1.     @SideOnly(Side.CLIENT)
  2.     public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
  3.         super.addInformation(stack, worldIn, tooltip, flagIn);
  4.         if (!(stack.getTagCompound() == null)) {
  5.             int a = stack.getTagCompound().getInteger("levelup");
  6.             String b = "能量:" + a + "/100000";
  7.             tooltip.add(b);
  8.         }
  9.     }
复制代码
感谢dalao提醒

c1230
Mod好像不知道,我知道插件的

的风格好
本帖最后由 的风格好 于 2018-8-3 21:05 编辑
ksgfk 发表于 2018-8-2 11:42
tag里的"levelup"是在攻击以后才会被添加上去....所以初始化的时候才会报错嘛....
那如何让物品直接附带一 ...

建议使用try catch 语句包围,以防玩家用铁砧命名物品或其他方式使物品有了nbt标签。
  1. @SideOnly(Side.CLIENT)
  2. public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
  3. {
  4.     super.addInformation(stack, worldIn, tooltip, flagIn);
  5.     try
  6.     {
  7.         int a = stack.getTagCompound().getInteger("levelup");
  8.         String b = "能量:" + a + "/100000";
  9.         tooltip.add(b);
  10.     }
  11.     catch(NullPointerException e)
  12.     {
  13.         System.out.println("No nbt key called "levelup"");
  14.     }
  15. }
复制代码

思念丶···
的风格好 发表于 2018-8-3 20:59
建议使用try catch 语句包围,以防玩家用铁砧命名物品或其他方式使物品有了nbt标签。
...

Q:2820538825 定制类似的MOD 请加我