Minecraft Forge: Tips On Modding Weapons

Introduction


Weapons are items. Like other items, when they are wielded in the player's hand they can be used to take action (i.e. attack).

If you want to mod weapons, make sure you are familiar with items including using the Item, ItemStack and EntityItem classes. I'll also assume you know how to register your item, create texture resources, etc.

Extending The Player's Weapon Reach ("Guns")


Check out Jabelar's Extended Weapon Reach Tutorial.

How To Detect If Weapon Is Being Held By Player


If it is your own custom item, you probably are writing code for the onUpdate() method. In that method, the last parameter is a boolean that represents whether the item is in the player's hands.

If you're writing code to detect whether a vanilla item is held, then you can use Player#getCurrentEquippedItem().

Adding NBT Data To Weapons


NBT data allows you to make an ItemStack unique. For example, maybe you want to give a sword a name, or have a weapon that has ammo or charges. NBT is perfect for that. 

See my item modding tips on how to add NBT data to items, including weapons.

Setting Up Weapon Models And Textures


This topic is already well covered by other tutorials. Check out TheGreyGhost's Item Rendering Tutorial.

Making A Custom Weapon Effect


Thanks to coolAlias for this tip.

For a continuous effect there is a built-in method that can be used: onUsingItemTick() (or just onUsingTick(), in 1.7.2), which is called every tick so long as you are using an item (i.e. you are continuing to hold right click and in onItemRightClick() you setItemInUse() for the player and you've been holding right click for a length of time less than the Item's getMaxUseDuration()).

Custom Weapon Actions (EnumAction)


Thanks to Choonster for this info.

EnumAction are used to control animations for items that are used over time. Like the swinging of a sword.

If you want similar behaviour to swords (hold right click to play an EnumAction@Override Item#getitemUseAction to return the appropriate action and call EntityPlayer#setItemInUse in Item#onItemRightClick like ItemSword does.

Choonster has made a working example.

No comments:

Post a Comment