Jump to content

[1.7.10]Sword NBT Help?


SureShotM

Recommended Posts

So I am Making a weapon(not really a sword), and have some NBT that goes with the weapon. I was thinking that I wanted souls that will go into the NBT and change the weapons stats. Now where I need help is with the best way to implement this, as in should I have a item GUI or should I do it when the soul is right clicked. I was thinking the soul is right clicked, but wasn't sure if there was a way to check if the weapon was in players inventory. 

 

Edit: I guess I am asking two things

 

1: how should I go about doing my task.

2: If I go the one rout how can I do it .

~SureShotM/Leo~ gaming coding all around fun!

Link to comment
Share on other sites

Right-clicking, assuming you mean while the sword is in hand, would be the simplest implementation - you already know that the player is holding your sword by the fact that your sword's Item#onItemRightClick method was called, and you won't have to deal with all the complications of a GUI (IGuiHandler, opening the GUI, sending packets to server, etc.).

 

onItemRightClick is called on both client and server, so you can do all the server-side things (such as changing the ItemStack NBT) directly from there.

 

If you want the 'soul' is an entity, though, it might be better to handle this from your Entity class' interact method (or whatever it's callled - don't have my IDE open right now), since it can be tricky to get the clicked entity from the Item method. Then you just have to check the held item of the entity that is interacting with the soul using #getHeldItem().

Link to comment
Share on other sites

Hmmmm? the thought of having the soul as an entity never crossed my mind, I was thinking the soul as an item that when you right click it will change the swords NBT. Now I have some more thinking to do, brb if anymore questions pop up. 

~SureShotM/Leo~ gaming coding all around fun!

Link to comment
Share on other sites

Would it be more approachable if you were to have multiple "material" types that you could convert to? I imagine you could right-click the SWORD and consume a soul item, then swap materials, or even swap entire swords, removing the one clicked from your inventory and placing the new one in its place. If you had multiple "types" of swords, then you could use a series of "if" statements to determine which souls would be prioritized for consumption. I don't know enough about your ideal game-mechanics to flesh this idea out, but this is how I would do it.

Link to comment
Share on other sites

@Exo594 that is a good idea as well but I think I have a better idea.

 

My question for this idea would be, is it possible to have a Item talk to another Item.

 

ex: In sword class. if baubles item is equiped and if it has NBT tag of soul do what ever to change the sword(increase damage, or enchant sword ect.

~SureShotM/Leo~ gaming coding all around fun!

Link to comment
Share on other sites

If you just want the damage to change based on number of souls, you would simply override Item#getAttributeModifiers(ItemStack) to return a multimap with the appropriately leveled AttributeModifier for attack damage according to the ItemStack NBT.

 

If you need access to another item in the player's inventory (vanilla or otherwise), it becomes slightly more complicated. There is no way to know about any other items or other player-related info in the method mentioned above, so you'd probably have to use the LivingHurtEvent to modify damage and the PlayerTickEvent to check for the equipped item every tick and apply or remove whatever enchantment, but you'd also need to make sure the item tooltip displays the correct damage amount, prevent the re-equip animation from playing each time you update the NBT, etc.

 

Lots more things to think about in the second situation, as you can see, which is why I would recommend you start with the simpler version and go from there. It's far easier to make something simple that works more complicated, rather than trying to make something complicated immediately and trying to figure out why it's not working. One step at a time.

Link to comment
Share on other sites

Okay so I finally got the soul function to work, now I forgot about the one other function that was asked for and that was no matter how much damage the souls increase it will always do .5 more damage to players.

 

Would I need to use,

 

@Override

public void onHitEntity(not sure what goes in here){

}

 

To achieve this, if so can some one link  me to an example of how it is used.

~SureShotM/Leo~ gaming coding all around fun!

Link to comment
Share on other sites

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Okay one last question and I should be good.

 

Is there a way to remove an enchantment.

 

What I have so far is mt weapon adds enchantments on right click.

 

What I want to do now is on update if the weapons nbt Boolean is false it will remove the enchantments, how would I achieve this since I do not see a removeEnchantment as a function under ItemStack.

~SureShotM/Leo~ gaming coding all around fun!

Link to comment
Share on other sites

There is no easy way to remove an enchantment, but it can be done by iterating through the enchantment list stored in the ItemStack's NBT and removing a specific enchantment that way.

 

E.g.

NBTTagList enchList = stack.getTagCompound().getTagList("ench", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < enchList.tagCount(); ++i) {
NBTTagCompound compound = enchList.getCompoundTagAt(i);
if (compound.getShort("id") == Enchantment.whatever.effectId) {
	enchList.remove(i); // example only - you should use Iterators when removing elements from a Collection
}
}

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.