Create a Minecraft Lifesteal Duplication plugin using the Paper API and Kotlin. This…
- User
Create a Minecraft Lifesteal Duplication plugin using the Paper API and Kotlin. This plugin should allow players to duplicate certain items while enabling the easy blacklisting of specific items to prevent duplication. Additionally, ensure that items like bundles or shulker boxes cannot be used as containers for duped items by validating item IDs against a blacklist.
Steps
-
Plugin Setup:
- Set up your development environment for creating a Minecraft plugin using Paper and Kotlin.
- Create the basic structure of a Minecraft plugin, including necessary configuration files.
-
Duplication Logic:
- Implement a command
/dupethat allows players to duplicate the item they are currently holding. - Add logic to handle the duplication process, ensuring that any duplicated item respects server rules.
- Implement a command
-
Blacklist Functionality:
- Implement a customizable blacklist for items that cannot be duplicated. This should be simple for server administrators to modify.
- Ensure the
/dupecommand checks the item's ID against this blacklist before allowing duplication.
-
Container Checks:
- Add checks to prevent the use of containers (e.g., bundles, shulker boxes) to bypass duplication restrictions.
- Validate if the duped items are within said containers, and cancel the duplication if any blacklisted IDs are detected.
-
Optimization and Testing:
- Test the plugin extensively to ensure no exploits are present and the blacklist system works as intended.
- Optimize code for efficient performance under various server loads.
-
Documentation:
- Provide clear instructions on how to install and configure the plugin.
- Document how administrators can modify the blacklist and any other customizable features.
Output Format
The final product should be a fully functional Minecraft plugin packaged in a JAR file, ready to be installed on a Paper server. Include documentation in a README file, explaining installation and configuration details.
Examples
- Example Blacklist Entry:
(In practice, the blacklist should include all items that should be blocked from duplication, using their respective Minecraft IDs.)val blacklist = listOf("minecraft:tripwire_hook", "minecraft:shulker_box")
Notes
- Consider edge cases where players might attempt to exploit the plugin, such as renaming items or using other hidden IDs.
- These need to save in like a config.yml what cnat be duped with id
- Since this involves duplication mechanics, ensure compatibility with server rules and consider limiting the duplication ability to certain player ranks or permissions.
-
- Assistant
Creating a Lifesteal Duplication Plugin for Minecraft using the Paper API and Kotlin involves several steps, from setting up your development environment to implementing the desired functionalities and ensuring compatibility and security. Below, you'll find a comprehensive guide to help you develop this plugin, complete with code snippets, configuration examples, and documentation.
Table of Contents
- Plugin Setup
- Duplication Logic
- Blacklist Functionality
- Container Checks
- Optimization and Testing
- Documentation
- Final Project Structure
1. Plugin Setup
1.1. Development Environment
To develop a Minecraft plugin using Paper and Kotlin, follow these steps:
-
Install Java Development Kit (JDK):
- Ensure you have JDK 17 or higher installed. You can download it from Adoptium.
-
Set Up an IDE:
- Use an IDE that supports Kotlin and Gradle, such as IntelliJ IDEA.
-
Install Gradle:
- Although IntelliJ IDEA can handle Gradle projects natively, you can also install Gradle separately if preferred.
-
Create a New Gradle Project:
- Open IntelliJ IDEA.
- Select New Project > Gradle > Kotlin/JVM.
- Name your project (e.g.,
LifestealDuplicationPlugin).
1.2. Project Configuration
1.2.1.
build.gradle.ktsConfigure your
build.gradle.ktsfile to include necessary dependencies and plugins.plugins { kotlin("jvm") version "1.8.10" id("com.github.johnrengelman.shadow") version "7.1.2" id("java") } group = "com.yourname.lifesteal" version = "1.0.0" repositories { mavenCentral() maven("https://papermc.io/repo/repository/maven-public/") } dependencies { implementation(kotlin("stdlib")) compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT") // Use the latest Paper API version } java { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { kotlinOptions.jvmTarget = "17" } tasks { shadowJar { archiveClassifier.set("") relocate("com.google", "com.yourname.lifesteal.shadow.com.google") } }Notes:
- Shadow Plugin: Used to create a fat JAR if you include any dependencies that need to be shaded.
- Kotlin Version: Ensure compatibility with the Paper API version you're targeting.
1.2.2.
settings.gradle.ktsrootProject.name = "LifestealDuplicationPlugin"1.3. Directory Structure
Ensure your project follows the standard Gradle directory structure:
LifestealDuplicationPlugin/ ├── build.gradle.kts ├── settings.gradle.kts ├── src/ │ ├── main/ │ │ ├── kotlin/ │ │ │ └── com/ │ │ │ └── yourname/ │ │ │ └── lifesteal/ │ │ │ └── LifestealDuplicationPlugin.kt │ │ └── resources/ │ │ ├── plugin.yml │ │ └── config.yml └── README.md1.4.
plugin.ymlCreate a
plugin.ymlfile insrc/main/resources/to define your plugin's metadata.name: LifestealDuplication version: "1.0.0" main: com.yourname.lifesteal.LifestealDuplicationPlugin api-version: 1.20 description: A plugin that allows players to duplicate certain items with blacklist functionality. authors: - YourName commands: dupe: description: Duplicate the item you are holding. usage: /dupe permission: lifesteal.dupe permission-message: You do not have permission to use this command. permissions: lifesteal.dupe: description: Allows players to use the /dupe command. default: op
2. Duplication Logic
2.1. Main Plugin Class
Create the main plugin class
LifestealDuplicationPlugin.ktinsrc/main/kotlin/com/yourname/lifesteal/.package com.yourname.lifesteal import org.bukkit.Material import org.bukkit.command.Command import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.bukkit.inventory.ItemStack import org.bukkit.plugin.java.JavaPlugin class LifestealDuplicationPlugin : JavaPlugin() { private lateinit var blacklist: Set<String> override fun onEnable() { // Save default config if not exists saveDefaultConfig() // Load blacklist from config loadBlacklist() // Register the /dupe command this.getCommand("dupe")?.setExecutor(DupeCommandExecutor()) logger.info("LifestealDuplication Plugin Enabled!") } override fun onDisable() { logger.info("LifestealDuplication Plugin Disabled!") } private fun loadBlacklist() { blacklist = config.getStringList("blacklist").map { it.lowercase() }.toSet() logger.info("Loaded blacklist with ${blacklist.size} items.") } inner class DupeCommandExecutor : CommandExecutor { override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean { if (sender !is Player) { sender.sendMessage("This command can only be used by players.") return true } val player = sender as Player if (!player.hasPermission("lifesteal.dupe")) { player.sendMessage(config.getString("messages.no-permission") ?: "You do not have permission to use this command.") return true } val itemInHand: ItemStack? = player.inventory.itemInMainHand if (itemInHand == null || itemInHand.type == Material.AIR) { player.sendMessage(config.getString("messages.no-item") ?: "You are not holding any item to duplicate.") return true } val itemId = itemInHand.type.key.toString().lowercase() if (isBlacklisted(itemId)) { player.sendMessage(config.getString("messages.blacklisted-item") ?: "This item cannot be duplicated.") return true } // Check if the item is a container if (isContainer(itemInHand.type)) { if (containsBlacklistedItems(itemInHand)) { player.sendMessage(config.getString("messages.blacklisted-container") ?: "This container holds blacklisted items and cannot be duplicated.") return true } } // Duplicate the item val duplicatedItem = itemInHand.clone() val addResult = player.inventory.addItem(duplicatedItem) if (addResult.isEmpty()) { player.sendMessage(config.getString("messages.duplicate-success") ?: "Item duplicated successfully.") } else { player.sendMessage(config.getString("messages.duplicate-fail") ?: "Your inventory is full. Could not duplicate the item.") } return true } private fun isBlacklisted(itemId: String): Boolean { return blacklist.contains(itemId) } private fun isContainer(material: Material): Boolean { return when (material) { Material.SHULKER_BOX, Material.BUNDLE, Material.CHEST, Material.TRAPPED_CHEST, Material.ENDER_CHEST -> true else -> false } } private fun containsBlacklistedItems(container: ItemStack): Boolean { val meta = container.itemMeta ?: return false return when (container.type) { Material.SHULKER_BOX, Material.CHEST, Material.TRAPPED_CHEST, Material.ENDER_CHEST -> { val inventory = (meta as? org.bukkit.inventory.BlockStateMeta)?.blockState?.inventory ?: return false inventory.contents.any { it != null && isBlacklisted(it.type.key.toString().lowercase()) } } Material.BUNDLE -> { val items = (meta as? org.bukkit.inventory.BundleMeta)?.items ?: return false items.any { it != null && isBlacklisted(it.type.key.toString().lowercase()) } } else -> false } } } }2.2. Explanation
- onEnable(): Loads the configuration and registers the
/dupecommand. - loadBlacklist(): Loads the blacklist from
config.ymland stores it in aSetfor efficient lookup. - DupeCommandExecutor:
- Permission Check: Ensures the player has the necessary permission to use the command.
- Item Check: Verifies that the player is holding an item.
- Blacklist Check: Prevents duplication of blacklisted items.
- Container Check: Ensures that containers like shulker boxes and bundles do not contain blacklisted items.
- Duplication: Clones the item and attempts to add it to the player's inventory, handling success and failure messages.
3. Blacklist Functionality
3.1.
config.ymlCreate a
config.ymlfile insrc/main/resources/to allow server administrators to customize the blacklist and messages.blacklist: - minecraft:tripwire_hook - minecraft:shulker_box - minecraft:bundle - minecraft:command_block - minecraft:ender_chest - minecraft:structure_block - minecraft:jigsaw - minecraft:barrier # Add more items as needed messages: no-permission: "§cYou do not have permission to use this command." no-item: "§cYou are not holding any item to duplicate." blacklisted-item: "§cThis item cannot be duplicated." blacklisted-container: "§cThis container holds blacklisted items and cannot be duplicated." duplicate-success: "§aItem duplicated successfully." duplicate-fail: "§cYour inventory is full. Could not duplicate the item."3.2. Explanation
- blacklist: A list of Minecraft item IDs that cannot be duplicated. Server admins can easily add or remove items here.
- messages: Customizable messages to send to players based on different scenarios. This allows for easy localization and customization.
4. Container Checks
4.1. Handling Containers
The plugin already includes container checks within the
DupeCommandExecutor. Here's a detailed breakdown:private fun isContainer(material: Material): Boolean { return when (material) { Material.SHULKER_BOX, Material.BUNDLE, Material.CHEST, Material.TRAPPED_CHEST, Material.ENDER_CHEST -> true else -> false } } private fun containsBlacklistedItems(container: ItemStack): Boolean { val meta = container.itemMeta ?: return false return when (container.type) { Material.SHULKER_BOX, Material.CHEST, Material.TRAPPED_CHEST, Material.ENDER_CHEST -> { val inventory = (meta as? org.bukkit.inventory.BlockStateMeta)?.blockState?.inventory ?: return false inventory.contents.any { it != null && isBlacklisted(it.type.key.toString().lowercase()) } } Material.BUNDLE -> { val items = (meta as? org.bukkit.inventory.BundleMeta)?.items ?: return false items.any { it != null && isBlacklisted(it.type.key.toString().lowercase()) } } else -> false } }4.2. Explanation
- isContainer(): Determines if the item is a container type that can hold other items.
- containsBlacklistedItems():
- Shulker Boxes, Chests, etc.: Retrieves the inventory contents and checks for any blacklisted items.
- Bundles: Retrieves the bundled items and checks for any blacklisted items.
- Duplication Prevention: If any blacklisted items are found within the container, duplication is canceled, and a corresponding message is sent to the player.
5. Optimization and Testing
5.1. Code Optimization
- Using Sets for Blacklist: Utilizing a
Setfor the blacklist ensures constant-time lookup, enhancing performance. - Early Returns: The command executor uses early returns to minimize unnecessary processing.
- Cloning Items: Cloning the item ensures that the original item remains unaffected during duplication.
5.2. Testing
-
Local Testing:
- Set up a local Paper server.
- Compile the plugin and place the JAR file in the
pluginsdirectory. - Start the server and test the
/dupecommand with various items.
-
Edge Cases:
- Attempt to duplicate blacklisted items.
- Try duplicating containers with and without blacklisted items inside.
- Test with renamed items to ensure only the item ID is checked.
-
Performance Testing:
- Evaluate the plugin's performance under scenarios with multiple players using the
/dupecommand simultaneously. - Monitor for any lag or server performance issues.
- Evaluate the plugin's performance under scenarios with multiple players using the
-
Security Testing:
- Ensure that players cannot bypass the blacklist through item renaming, enchantments, or other metadata manipulations.
5.3. Potential Enhancements
- Permissions Based on Ranks: Extend the permission system to allow specific groups or ranks to use the duplication command.
- Logging: Implement logging for duplication attempts, especially for blocked attempts, to monitor potential abuse.
- Cooldowns: Add cooldowns to prevent spamming the
/dupecommand.
6. Documentation
Provide a clear
README.mdto guide users on installation, configuration, and usage.6.1.
README.md# Lifesteal Duplication Plugin A Minecraft plugin designed for Paper servers that allows players to duplicate certain items while providing a blacklist system to prevent duplication of specific items. Ensures that containers like bundles and shulker boxes cannot be used to bypass duplication restrictions. ## Features - **/dupe Command:** Allows players to duplicate the item they are currently holding. - **Blacklist System:** Easily configurable list of items that cannot be duplicated. - **Container Validation:** Prevents duplication if containers hold any blacklisted items. - **Permission-Based Access:** Control who can use the duplication command. ## Installation 1. **Download the Plugin:** - Compile the plugin using Gradle: ```bash ./gradlew shadowJar ``` - The compiled JAR file will be located in `build/libs/`. 2. **Place the JAR File:** - Move the `LifestealDuplicationPlugin.jar` to your Paper server's `plugins` directory. 3. **Start the Server:** - Launch your Paper server. The plugin will generate a default `config.yml` upon first run. ## Configuration ### `config.yml` Located in `plugins/LifestealDuplication/config.yml`, this file allows you to customize the blacklist and messages. #### Blacklist Define items that cannot be duplicated using their Minecraft IDs. ```yaml blacklist: - minecraft:tripwire_hook - minecraft:shulker_box - minecraft:bundle - minecraft:command_block - minecraft:ender_chest - minecraft:structure_block - minecraft:jigsaw - minecraft:barrier # Add more items as neededAdding Items:
To add an item to the blacklist, include its Minecraft ID in the
blacklistlist. Ensure the ID is in lowercase and follows thenamespace:item_idformat.Messages
Customize the messages sent to players based on different scenarios.
messages: no-permission: "§cYou do not have permission to use this command." no-item: "§cYou are not holding any item to duplicate." blacklisted-item: "§cThis item cannot be duplicated." blacklisted-container: "§cThis container holds blacklisted items and cannot be duplicated." duplicate-success: "§aItem duplicated successfully." duplicate-fail: "§cYour inventory is full. Could not duplicate the item."Customization:
- Use Minecraft color codes (e.g.,
§cfor red) to color your messages. - Modify the text to suit your server's theme or language.
Permissions
- Command Permission:
lifesteal.dupe- Description: Allows players to use the
/dupecommand. - Default: Only server operators (
op) have access by default.
- Description: Allows players to use the
Assigning Permissions:
Use a permissions plugin (e.g., LuckPerms) to assign the
lifesteal.dupepermission to specific groups or players./lp group <group-name> permission set lifesteal.dupe trueUsage
-
Command:
/dupe -
Requirements:
- Player must have the
lifesteal.dupepermission. - Player must be holding an item in their main hand.
- The item must not be blacklisted.
- If the item is a container, it must not contain any blacklisted items.
- Player must have the
-
Responses:
- Success: "Item duplicated successfully."
- Failure: Appropriate message based on the reason (e.g., no permission, item is blacklisted).
Examples
Blacklist Entry
blacklist: - minecraft:tripwire_hook - minecraft:shulker_boxCommand Usage
-
Player: Holds a diamond sword and executes
/dupe. -
Result: Receives another diamond sword in their inventory.
-
Player: Holds a shulker box and executes
/dupe. -
Result: Command is canceled with the message "This item cannot be duplicated."
Contributing
Contributions are welcome! Please open an issue or submit a pull request with your enhancements or bug fixes.
License
This project is licensed under the MIT License.
Acknowledgements
- Built using the Paper API.
- Developed in Kotlin for better safety and conciseness.
### 6.2. Explanation - **Features:** Brief overview of what the plugin offers. - **Installation:** Step-by-step guide to installing the plugin on a Paper server. - **Configuration:** Detailed explanation of `config.yml`, including how to customize the blacklist and messages. - **Permissions:** Information on how to manage command permissions. - **Usage:** Instructions on how to use the `/dupe` command and expected outcomes. - **Examples:** Practical scenarios demonstrating the plugin's behavior. - **Contributing & License:** Standard sections for open-source projects. --- ## 7. Final Project Structure Here's how your project should be organized:LifestealDuplicationPlugin/ ├── build.gradle.kts ├── settings.gradle.kts ├── src/ │ ├── main/ │ │ ├── kotlin/ │ │ │ └── com/ │ │ │ └── yourname/ │ │ │ └── lifesteal/ │ │ │ └── LifestealDuplicationPlugin.kt │ │ └── resources/ │ │ ├── plugin.yml │ │ └── config.yml └── README.md
--- ## Conclusion By following the steps outlined above, you will have a fully functional **Lifesteal Duplication Plugin** for your Minecraft Paper server. This plugin allows players to duplicate items while maintaining control over which items can or cannot be duplicated through a customizable blacklist system. Additionally, container checks ensure that players cannot exploit the duplication mechanics using items like shulker boxes or bundles. Feel free to enhance and expand upon this foundation to add more features, improve security, or tailor it to your server's specific needs. Happy coding!