fabric-docs icon indicating copy to clipboard operation
fabric-docs copied to clipboard

SwordItem class not found?

Open youkononame opened this issue 10 months ago • 1 comments

I was reading the guide for creating custom tools (https://docs.fabricmc.net/develop/items/custom-tools) and I noticed the use of the "SwordItem" class. IntelliJ could not automatically find a package to import it from and it didn't seem to appear in anywhere. I know this is written for 1.21.4 and I was on 1.21.5 but I can't imagine any drastic changes being made between those two versions.

Any way of fixing this? The ToolMaterial for Stone being passed to .applySwordSettings with 1f attackDamage and attackSpeed gave me the attack speed and damage of a pickaxe, and I couldn't find any alternatives. Help would be appreciated!

youkononame avatar Apr 25 '25 21:04 youkononame

but I can't imagine any drastic changes being made between those two versions.

Lol. You underestimate how many changes Mojang makes behind the scenes. There is no SwordItem class anymore, you can just use Item while setting the sword settings appropriately on the item settings. You can ask on the Fabric discord if you need any further help.

Earthcomputer avatar Apr 25 '25 21:04 Earthcomputer

It seems that it has been removed in 1.21.5. You can use new Item.Settings().sword(...) To create swords. But I crashed when I was using it:

---- Minecraft Crash Report ----
// Hi. I'm Minecraft, and I'm a crashaholic.

Time: 2025-06-10 13:04:04
Description: Initializing game

java.lang.RuntimeException: Could not execute entrypoint stage 'main' due to errors, provided by 'ua' at 'cn.yizhimcqiu.usefulamethyst.UsefulAmethystMod'!
	at net.fabricmc.loader.impl.FabricLoaderImpl.lambda$invokeEntrypoints$2(FabricLoaderImpl.java:403)
	at net.fabricmc.loader.impl.util.ExceptionUtil.gatherExceptions(ExceptionUtil.java:33)
	at net.fabricmc.loader.impl.FabricLoaderImpl.invokeEntrypoints(FabricLoaderImpl.java:401)
	at net.fabricmc.loader.impl.game.minecraft.Hooks.startClient(Hooks.java:52)
	at knot//net.minecraft.client.MinecraftClient.<init>(MinecraftClient.java:539)
	at knot//net.minecraft.client.main.Main.main(Main.java:224)
	at net.fabricmc.loader.impl.game.minecraft.MinecraftGameProvider.launch(MinecraftGameProvider.java:480)
	at net.fabricmc.loader.impl.launch.knot.Knot.launch(Knot.java:74)
	at net.fabricmc.loader.impl.launch.knot.KnotClient.main(KnotClient.java:23)
	at net.fabricmc.devlaunchinjector.Main.main(Main.java:86)
Caused by: java.lang.ExceptionInInitializerError
	at knot//cn.yizhimcqiu.usefulamethyst.UsefulAmethystMod.onInitialize(UsefulAmethystMod.java:12)
	at net.fabricmc.loader.impl.FabricLoaderImpl.invokeEntrypoints(FabricLoaderImpl.java:399)
	... 7 more
Caused by: java.lang.IllegalStateException: Registry is already frozen
	at knot//net.minecraft.registry.SimpleRegistry.assertNotFrozen(SimpleRegistry.java:88)
	at knot//net.minecraft.registry.SimpleRegistry.createMutableRegistryLookup(SimpleRegistry.java:379)
	at knot//net.minecraft.registry.Registries.createEntryLookup(Registries.java:345)
	at knot//net.minecraft.item.ToolMaterial.applyToolSettings(ToolMaterial.java:38)
	at knot//net.minecraft.item.Item$Settings.tool(Item.java:751)
	at knot//net.minecraft.item.Item$Settings.pickaxe(Item.java:755)
	at knot//cn.yizhimcqiu.usefulamethyst.registries.ModItems.<clinit>(ModItems.java:17)
	... 9 more


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

-- Head --
Thread: Render thread
Stacktrace:
	at net.fabricmc.loader.impl.FabricLoaderImpl.lambda$invokeEntrypoints$2(FabricLoaderImpl.java:403)
	at net.fabricmc.loader.impl.util.ExceptionUtil.gatherExceptions(ExceptionUtil.java:33)
	at net.fabricmc.loader.impl.FabricLoaderImpl.invokeEntrypoints(FabricLoaderImpl.java:401)
	at net.fabricmc.loader.impl.game.minecraft.Hooks.startClient(Hooks.java:52)
	at knot//net.minecraft.client.MinecraftClient.<init>(MinecraftClient.java:539)

-- Initialization --
Details:
	Modules: 
Stacktrace:
	at knot//net.minecraft.client.main.Main.main(Main.java:224)
	at net.fabricmc.loader.impl.game.minecraft.MinecraftGameProvider.launch(MinecraftGameProvider.java:480)
	at net.fabricmc.loader.impl.launch.knot.Knot.launch(Knot.java:74)
	at net.fabricmc.loader.impl.launch.knot.KnotClient.main(KnotClient.java:23)
	at net.fabricmc.devlaunchinjector.Main.main(Main.java:86)

-- System Details --
Details:
	Minecraft Version: 1.21.5
	Minecraft Version ID: 1.21.5
	Operating System: Mac OS X (aarch64) version 15.4
	Java Version: 21.0.6, Oracle Corporation
	Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode, sharing), Oracle Corporation
	Memory: 324957720 bytes (309 MiB) / 1069547520 bytes (1020 MiB) up to 4294967296 bytes (4096 MiB)
	CPUs: 8
	Processor Vendor: Apple Inc.
	Processor Name: Apple M3
	Identifier: Apple Inc. Family 0x8765edea Model 0 Stepping 0
	Microarchitecture: ARM64 SoC: Everest + Sawtooth
	Frequency (GHz): 4.06
	Number of physical packages: 1
	Number of physical CPUs: 8
	Number of logical CPUs: 8
	Graphics card #0 name: Apple M3
	Graphics card #0 vendor: Apple (0x106b)
	Graphics card #0 VRAM (MiB): 0.00
	Graphics card #0 deviceId: unknown
	Graphics card #0 versionInfo: unknown
	Memory slot #0 capacity (MiB): 0.00
	Memory slot #0 clockSpeed (GHz): 0.00
	Memory slot #0 type: unknown
	Virtual memory max (MiB): 16384.00
	Virtual memory used (MiB): 10744.39
	Swap memory total (MiB): 0.00
	Swap memory used (MiB): 0.00
	Space in storage for jna.tmpdir (MiB): <path not set>
	Space in storage for org.lwjgl.system.SharedLibraryExtractPath (MiB): <path not set>
	Space in storage for io.netty.native.workdir (MiB): <path not set>
	Space in storage for java.io.tmpdir (MiB): available: 32498.64, total: 233752.44
	Space in storage for workdir (MiB): available: 32498.64, total: 233752.44
	JVM Flags: 0 total; 
	Launched Version: Fabric
	Backend library: LWJGL version 3.3.3-snapshot
	Backend API: Unknown
	Window size: <not initialized>
	GFLW Platform: <error>
	Render Extensions: ERR
	GL debug messages: <no renderer available>
	Is Modded: Definitely; Client brand changed to 'fabric'
	Universe: 404
	Type: Client (map_client.txt)
	Locale: zh_CN_#Hans
	System encoding: UTF-8
	File encoding: UTF-8
	CPU: 8x Apple M3

(Is it compliance to upload the crash report here?)

AnemoFlower avatar Jun 10 '25 05:06 AnemoFlower

I think we should now have to put the tool properties and make it into a ToolItem.

public static final Item RIP_SWORD_ITEM = register(
    "rip_sword_item",
    settings -> new ToolItem(
        TOOL_MATERIAL,      // ToolMaterial
        new ToolAttributes.Builder()
            .damage(3.0f)
            .speed(-2.4f)
            .type(ToolAttributes.Type.SWORD)
            .build(),
        settings
    ),
    new Item.Settings()
);

oniroo avatar Aug 03 '25 04:08 oniroo

You can check the Items.class code. This should work:

public static final Item YOUR_SWORD_ITEM = register(
            "your_sword_item",
            settings -> new Item(
                    settings.sword(ToolMaterial.WOOD,
                            3.0f,
                            -2.4f)
            ),
            new Item.Settings()
    );

detectivelll avatar Aug 03 '25 08:08 detectivelll

By the way, if you want your sword to have sweeping attacks and enchantment capabilities, create a swords.json file here: src/main/resources/data/minecraft/tags/item/ "swords.json" can be like this:

{
  "replace": false,
  "values": [
    "tutorialmod:your_sword_item"
  ]
}

detectivelll avatar Aug 03 '25 08:08 detectivelll

I've seen the up coming PR and I found that we can use this

public static final Item YOUR_SWORD_ITEM = register(
            "your_sword_item",
            Item::new,
            new Item.Settings().sword(...)     // Like this
    );

oniroo avatar Aug 05 '25 04:08 oniroo