Jump to content

[SOLVED]|How to add a Sword in 1.16.5 (IItemTier/SwordItem)


Recommended Posts

Posted (edited)

HI ! So I tried to add a sword using a custom class and and ItemTier class. Here's the relevant code :

/*package com.pandaw.magical_macrocosm.items;

import com.pandaw.magical_macrocosm.MagicalMacrocosm;
import com.pandaw.magical_macrocosm.init.ModItemTier;
import net.minecraft.item.Food;
import net.minecraft.item.IItemTier;
import net.minecraft.item.Item;
import net.minecraft.item.SwordItem;


*/

/* //How I tried to do at first
public class AdamasSword extends SwordItem {
    public AdamasSword() {
        super(new SwordItem(ModItemTier.ADAMAS,9,1.6f, (new Item.Properties())
                .group(MagicalMacrocosm.TAB))
        );
    }
}
*/

/* //What I found in the SwordItem class (tried to adjust it after, but here is the original version from SwordItem class)
public SwordItem(IItemTier tier, int attackDamageIn, float attackSpeedIn, Item.Properties builderIn) {
        super(tier, builderIn);
        this.attackDamage = (float)attackDamageIn + tier.getAttackDamage();
        Builder<Attribute, AttributeModifier> builder = ImmutableMultimap.builder();
        builder.put(Attributes.ATTACK_DAMAGE, new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Weapon modifier", (double)this.attackDamage, AttributeModifier.Operation.ADDITION));
        builder.put(Attributes.ATTACK_SPEED, new AttributeModifier(ATTACK_SPEED_MODIFIER, "Weapon modifier", (double)attackSpeedIn, AttributeModifier.Operation.ADDITION));
        this.attributeModifiers = builder.build();
        }
*/
package com.pandaw.magical_macrocosm.init;

import java.util.function.Supplier;
import net.minecraft.item.IItemTier;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.LazyValue;

public enum ModItemTier implements IItemTier {
    ADAMAS(4, 0, 6.0f, 9.0f, 16, () -> {
        return Ingredient.fromItems(ModItems.ADAMAS_DROP.get());
    });

    private final int harvestLevel;
    private final int maxUses;
    private final float efficiency;
    private final float attackDamage;
    private final int enchantability;
    private final LazyValue<Ingredient> repairMaterial;

    private ModItemTier(int harvestLevelIn, int maxUsesIn, float efficiencyIn, float attackDamageIn, int enchantabilityIn, Supplier<Ingredient> repairMaterialIn) {
        this.harvestLevel = harvestLevelIn;
        this.maxUses = maxUsesIn;
        this.efficiency = efficiencyIn;
        this.attackDamage = attackDamageIn;
        this.enchantability = enchantabilityIn;
        this.repairMaterial = new LazyValue<>(repairMaterialIn);
    }

    public int getMaxUses() {
        return this.maxUses;
    }

    public float getEfficiency() {
        return this.efficiency;
    }

    public float getAttackDamage() {
        return this.attackDamage;
    }

    public int getHarvestLevel() {
        return this.harvestLevel;
    }

    public int getEnchantability() {
        return this.enchantability;
    }

    public Ingredient getRepairMaterial() {
        return this.repairMaterial.getValue();
    }
}

Then I tried directly via registering

package com.pandaw.magical_macrocosm.init;

import com.pandaw.magical_macrocosm.MagicalMacrocosm;
import com.pandaw.magical_macrocosm.blocks.BlockItemBase;
import com.pandaw.magical_macrocosm.items.ItemBase;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemTier;
import net.minecraft.item.SwordItem;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

public class ModItems {

    public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MagicalMacrocosm.MOD_ID);

    //Items
    /*public static final RegistryObject<Item> ADAMAS_SWORD = ITEMS.register("adamas_sword", AdamasSword::new);*/ //When I tried the "custom class way"
    public static final RegistryObject<Item> ADAMAS_SWORD = ITEMS.register("adamas_sword", new SwordItem(ModItemTier.ADAMAS, 9, 1.6f, (new Item.Properties()).group(MagicalMacrocosm.TAB))); //The "registering way"

With first method, I have " 'SwordItem(net.minecraft.item.IItemTier, int, float, net.minecraft.item.Item.Properties)' in 'net.minecraft.item.SwordItem' cannot be applied to '(net.minecraft.item.SwordItem)' "

 

With the "registering" method; I have " Inferred type 'I' for type parameter 'I' is not within its bound; should extend 'net.minecraft.item.Item' "

 

Now, I know it may be simple, but I've just been stuck on that, and if someone is capable of explaining it to me, I'm open to know, thanks 😭

Edited by Pandaw
Posted

the register method requires a String and and Supplier
 

Spoiler
Quote

Supplier:

The Java Supplier interface is a functional interface that represents an function that supplies a value of some sorts. The Supplier interface can also be thought of as a factory interface. Here is an example implementation of the Java Supplier interface:



Supplier<Integer> supplier = () -> new Integer((int) (Math.random() * 1000D));

This Java Supplier implementation returns a new Integer instance with a random value between 0 and 1000.

quote from: http://tutorials.jenkov.com/java-functional-programming/functional-interfaces.html

 

  • Pandaw changed the title to [SOLVED]|How to add a Sword in 1.16.5 (IItemTier/SwordItem)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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