Forge版本:forge-1.12.2-14.23.4.2705
自己做了一把剑,想让它在被使用一次后加上已有的NBT标签,比如Unbreakable这种
复制代码
这样做似乎只是给它加个栏位
找不到NBT的教程...
自己做了一把剑,想让它在被使用一次后加上已有的NBT标签,比如Unbreakable这种
- item.setTagCompound(new NBTTagCompound());
这样做似乎只是给它加个栏位
找不到NBT的教程...
先构造一个nbtcompound,然后往里放东西,放好之后,再setNBTCompound
不过应该先获取nbt,然后修改获取到的nbt
如何添加nbt至你的物品?这个应该可以帮得到你
https://emxtutorials.wordpress.com/adding-nbt-data-to-items/
https://emxtutorials.wordpress.com/adding-nbt-data-to-items/
Himmelt 发表于 2018-7-27 08:20
不过应该先获取nbt,然后修改获取到的nbt
还有个问题,部分mod会在物品上写上一些介绍,就像核电工艺的聚变堆核心有一大串街上,这是怎么做到的,还是nbt吗
ksgfk 发表于 2018-7-31 08:31
还有个问题,部分mod会在物品上写上一些介绍,就像核电工艺的聚变堆核心有一大串街上,这是怎么做到的, ...
重写Item的方法
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean advanced);
Himmelt 发表于 2018-7-31 14:19
重写Item的方法
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer pla ...
我在给addInformation方法的List添加元素的时候,一直提示我错误....
我想在这里面添上我自己设的nbt
- @SideOnly(Side.CLIENT)
- public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
- String a = Integer.toString(stack.getTagCompound().getInteger("levelup"));
- tooltip.add(a);
- }
然后控制台报错
- [main/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:629]: ---- Minecraft Crash Report ----
- // Why did you do that?
- Time: 8/2/18 10:05 AM
- Description: Initializing game
- java.lang.NullPointerException: Initializing game
- at com.github.ksgfk.oceanheart.objects.tools.ToolSwordTrailblazer.addInformation(ToolSwordTrailblazer.java:95)
- at net.minecraft.item.ItemStack.getTooltip(ItemStack.java:811)
- at net.minecraft.client.Minecraft.lambda$populateSearchTreeManager$1(Minecraft.java:625)
- at net.minecraft.client.util.SearchTree.index(SearchTree.java:93)
- at net.minecraft.client.util.SearchTree.add(SearchTree.java:78)
- at java.lang.Iterable.forEach(Iterable.java:75)
- at net.minecraft.client.Minecraft.populateSearchTreeManager(Minecraft.java:639)
- at net.minecraft.client.Minecraft.init(Minecraft.java:575)
- at net.minecraft.client.Minecraft.run(Minecraft.java:421)
- at net.minecraft.client.main.Main.main(Main.java:118)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:498)
- at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
- at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:498)
- at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
- at GradleStart.main(GradleStart.java:25)
- A detailed walkthrough of the error, its code path and all known details is as follows:
- ---------------------------------------------------------------------------------------
- -- Head --
- Thread: Client thread
- Stacktrace:
- at com.github.ksgfk.oceanheart.objects.tools.ToolSwordTrailblazer.addInformation(ToolSwordTrailblazer.java:95)
- at net.minecraft.item.ItemStack.getTooltip(ItemStack.java:811)
- at net.minecraft.client.Minecraft.lambda$populateSearchTreeManager$1(Minecraft.java:625)
- at net.minecraft.client.util.SearchTree.index(SearchTree.java:93)
- at net.minecraft.client.util.SearchTree.add(SearchTree.java:78)
- at java.lang.Iterable.forEach(Iterable.java:75)
- at net.minecraft.client.Minecraft.populateSearchTreeManager(Minecraft.java:639)
- at net.minecraft.client.Minecraft.init(Minecraft.java:575)
- -- Initialization --
- Details:
- Stacktrace:
- at net.minecraft.client.Minecraft.run(Minecraft.java:421)
- at net.minecraft.client.main.Main.main(Main.java:118)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:498)
- at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
- at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:498)
- at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
- at GradleStart.main(GradleStart.java:25)
大佬这是怎么回事...
本帖最后由 的风格好 于 2018-8-2 11:40 编辑
getInteger("levelup") 产生了NullPointerException
try catch 或者 看看tag里有没有"levelup"
getInteger("levelup") 产生了NullPointerException
try catch 或者 看看tag里有没有"levelup"
本帖最后由 ksgfk 于 2018-8-2 13:23 编辑
tag里的"levelup"是在攻击以后才会被添加上去....所以初始化的时候才会报错嘛....
那如何让物品直接附带一个tag
-----------------------------------------------------------------
解决了,
复制代码感谢dalao提醒
的风格好 发表于 2018-8-2 11:33
getInteger("levelup") 产生了NullPointerException
看看tag里有没有"levelup"
tag里的"levelup"是在攻击以后才会被添加上去....所以初始化的时候才会报错嘛....
那如何让物品直接附带一个tag
-----------------------------------------------------------------
解决了,
- @SideOnly(Side.CLIENT)
- public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
- super.addInformation(stack, worldIn, tooltip, flagIn);
- if (!(stack.getTagCompound() == null)) {
- int a = stack.getTagCompound().getInteger("levelup");
- String b = "能量:" + a + "/100000";
- tooltip.add(b);
- }
- }
Mod好像不知道,我知道插件的
本帖最后由 的风格好 于 2018-8-3 21:05 编辑
建议使用try catch 语句包围,以防玩家用铁砧命名物品或其他方式使物品有了nbt标签。
复制代码
ksgfk 发表于 2018-8-2 11:42
tag里的"levelup"是在攻击以后才会被添加上去....所以初始化的时候才会报错嘛....
那如何让物品直接附带一 ...
建议使用try catch 语句包围,以防玩家用铁砧命名物品或其他方式使物品有了nbt标签。
- @SideOnly(Side.CLIENT)
- public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
- {
- super.addInformation(stack, worldIn, tooltip, flagIn);
- try
- {
- int a = stack.getTagCompound().getInteger("levelup");
- String b = "能量:" + a + "/100000";
- tooltip.add(b);
- }
- catch(NullPointerException e)
- {
- System.out.println("No nbt key called "levelup"");
- }
- }