π
pasted-code-1788150087264.txt
txt
214 lines
βΆ
# Paper 1.21.11 Custom Weapon β Speed Sword
Create a custom weapon for a Minecraft SMP running **Paper 1.21.11**.
Use the **modern Paper 1.21.11 API** and avoid deprecated or outdated Bukkit/Spigot approaches whenever possible.
## Weapon: Speed Sword
The weapon is called **Speed Sword**.
### Base Item
* Base material: `NETHERITE_SWORD`
* Enchantment: `SHARPNESS II`
* Display name: **Speed Sword**
* The sword must have a unique internal identifier so it cannot be confused with a normal Netherite Sword.
Use the item's **PersistentDataContainer (PDC)** to identify the weapon rather than checking its display name.
## Special Ability β Speed I
The Speed Sword gives its wielder **Speed I** while the sword is actively held in their main hand.
### Behavior
When a player switches to the Speed Sword:
* Give the player `SPEED` level 1.
* The effect should remain active while the Speed Sword is held in the main hand.
When the player switches away from the Speed Sword:
* Remove the Speed effect that was provided by the Speed Sword.
The implementation must prevent the Speed effect from stacking or constantly refreshing unnecessarily.
### Important Potion Effect Behavior
Do **not** accidentally remove a Speed effect that came from another source.
For example, if a player already has Speed from a potion, beacon, command, or another custom item, switching away from the Speed Sword should not blindly remove that unrelated Speed effect.
The plugin should distinguish between Speed provided by the Speed Sword and Speed provided by other sources as much as reasonably possible with the Paper API.
The Speed Sword should also work correctly when:
* The player joins the server.
* The player respawns.
* The player dies.
* The player changes hotbar slots.
* The player drops the sword.
* The player picks the sword back up.
* The sword is moved between inventory slots.
* The player disconnects and reconnects.
## Crafting Recipe
Create a custom shaped crafting recipe for the Speed Sword.
### Required Materials
* 1Γ Diamond Sword
* 2Γ Netherite Scrap
* 4Γ Diamond Block
* 2Γ Sugar Cane
### Exact 3Γ3 Recipe
```text
[Diamond Block] [Sugar Cane] [Diamond Block]
[Netherite Scrap] [Diamond Sword] [Netherite Scrap]
[Diamond Block] [Sugar Cane] [Diamond Block]
```
The crafting table should produce exactly **1 Speed Sword**.
The recipe must not accept incorrect materials or incorrect item positions.
## Custom Item Identification
Use a `NamespacedKey` and the item's `PersistentDataContainer`.
For example, create a custom PDC value similar to:
`speed_sword`
The exact implementation is up to you, but the plugin must reliably determine whether an ItemStack is the custom Speed Sword.
Do NOT rely solely on:
* Display name
* Lore
* Enchantments
* Material type
A normal Netherite Sword with Sharpness II should NOT be treated as a Speed Sword.
## Item Persistence
The Speed Sword must retain its custom identifier when:
* Dropped on the ground
* Picked up
* Stored in a chest
* Stored in a shulker box
* Moved through inventories
* Logged out with it
* Logged back in with it
## Item Appearance
Give the sword a clearly visible custom name:
**Speed Sword**
Optionally add lore describing its ability, such as:
**Ability:**
`While held: Speed I`
Do not make the lore responsible for identifying the sword.
## Architecture
Organize the plugin cleanly.
Recommended structure:
```text
src/main/java/
βββ your/package/name/
βββ Main.java
βββ SpeedSword.java
βββ SpeedSwordListener.java
βββ SpeedSwordRecipe.java
```
You may change the structure if there is a cleaner implementation.
### Main Class
The main plugin class should:
* Extend `JavaPlugin`
* Register the Speed Sword recipe
* Register the required event listeners
* Initialize the Speed Sword system
### SpeedSword Class
Create reusable methods for:
* Creating the Speed Sword
* Checking whether an ItemStack is the Speed Sword
* Applying the sword's ability
* Removing the sword's ability
* Getting the custom PDC identifier
### Listener
Create the necessary listeners for detecting when the player starts or stops holding the sword.
Use appropriate Paper/Bukkit events rather than constantly running expensive checks on every player.
If a repeating task is necessary, make it efficient and explain why it is being used.
## Edge Cases
Make sure the plugin handles these situations correctly:
1. Player switches from Speed Sword β normal item.
2. Player switches from normal item β Speed Sword.
3. Player dies while holding the sword.
4. Player respawns while the sword is in their inventory.
5. Player disconnects while holding the sword.
6. Player reconnects with the sword.
7. Player drops the sword.
8. Player picks the sword back up.
9. Player moves the sword between inventory slots.
10. Player already has Speed from another source.
11. Player has multiple Speed Swords in their inventory.
12. A normal Netherite Sword with Sharpness II is not treated as the custom weapon.
## Code Requirements
Write complete, production-ready Java code for **Paper 1.21.11**.
Do not use obsolete APIs simply because they work on older Minecraft versions.
Include:
* All Java classes
* All imports
* `plugin.yml`
* `pom.xml` or Gradle configuration appropriate for Paper 1.21.11
* Correct Paper API dependency
* Correct Java version required by Paper 1.21.11
* Instructions for compiling and installing the plugin
Make sure the code is internally consistent and compiles without missing classes, imports, methods, or dependencies.
## Final Goal
The finished plugin should add exactly one custom SMP weapon:
**β Speed Sword**
**Material:** Netherite Sword
**Enchantment:** Sharpness II
**Ability:** Speed I while held in the main hand
**Crafting:** 4 Diamond Blocks + 2 Sugar Cane + 2 Netherite Scrap + 1 Diamond Sword
The weapon should feel like a polished custom SMP item rather than simply being a renamed Netherite Sword.
Create the Gradle/Paper 1.21.11 project configuration and plugin metadata.
Implement the PDC-identified Speed Sword item and ability lifecycle.
Add exact shaped crafting and event listeners for inventory, lifecycle, and effect edge cases.
Implemented the Paper 1.21.11 Speed Sword plugin.
Included:
Gradle build configured for Java 21 and Paper 1.21.11
plugin.ymlPDC-based
speed_swordidentificationNetherite Sword with Sharpness II
Custom Adventure display name and lore
Speed I while held in the main hand
Protection against removing unrelated Speed effects
Handling for joins, respawns, deaths, quits, hotbar changes, drops, pickups, inventory moves, item breaks, world changes, and off-hand swaps
Exact shaped crafting recipe:
Plain TextD C D N W N D C DWhere:
D= Diamond BlockC= Sugar CaneN= Netherite ScrapW= Diamond Sword
Build and install:
./gradlew build
Copy the resulting JAR from:
build/libs/speed-sword-1.0.0.jar
to the serverβs plugins/ directory, then restart the Paper server.
package com.kodari.speedsword;
import org.bukkit.plugin.java.JavaPlugin;
public final class SpeedSwordPlugin extends JavaPlugin {
private SpeedSword speedSword;
@Override
public void onEnable() {
this.speedSword = new SpeedSword(this);
new SpeedSwordRecipe(this, this.speedSword).register();
getServer().getPluginManager().registerEvents(new SpeedSwordListener(this, this.speedSword), this);
}
@Override
public void onDisable() {
if (this.speedSword != null) {
this.speedSword.removeAllAbilities();
}
}
}