Guide Plugins Making a Simple Economy Plugin - Like DonutSMP

FateKid

DEVIL DID IT

Admin

Status

offline

Posts

14,980

Likes

131,702

Resources

3,400

Bits Credits

511

LEVEL

11

5,910 XP

Want to make your own /pay command plugin like DonutSMP? Look no further!​


Handling a simple pay plugin such as this is easy! Follow the instructions below and you'll have the basics in creating a custom economy plugin for your server.
This plugin also comes equipped with K and M parsing, allowing you to send 50k, 100k, 1m and other values instead of the integer/numeric full values in the command.

Step 1: Set up the development environment​

To get started, you need to set up your development environment.
Make sure you have Java Development Kit (JDK) installed on your computer and an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA.

Also, you need the Spigot API and Vault library. You can find the Spigot API and Vault plugin JARs online or use a build tool like Maven or Gradle to manage dependencies.

Step 2: Create a new Java project​

Create a new Java project in your IDE and set up the required Spigot API and Vault plugin JARs as dependencies.

Step 3: Implement the main plugin class​

Create a new class that extends the JavaPlugin class from the Spigot API. This class will act as your main plugin class. In this class, you'll handle command registration, economy integration, and the logic for the /pay command.

Main Java Class Example:​

[CODE lang="java" title="Main.class"]package com.NullForumsEco; // Replace with your plugin's package name

import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

public class NullForumsEco extends JavaPlugin {

private Economy economy;

@Override
public void onEnable() {
if (!setupEconomy()) {
// require vault, iConomy, Essentials, etc...
getLogger().severe("Vault economy not found! Disabling plugin.");
getServer().getPluginManager().disablePlugin(this);
return;
}

getLogger().info("NullForumsEco has been enabled!");
}

@Override
public void onDisable() {
getLogger().info("NullForumsEco has been disabled!");
}

private boolean setupEconomy() {
if (getServer().getPluginManager().getPlugin("Vault") == null) {
return false;
}

RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
return false;
}

economy = rsp.getProvider();
return true;
}

// Handle parsing of K & M multipliers
private double parseAmount(String input) {
double multiplier = 1.0;

if (input.endsWith("k") || input.endsWith("K")) {
multiplier = 1000.0;
input = input.substring(0, input.length() - 1);
} else if (input.endsWith("m") || input.endsWith("M")) {
multiplier = 1000000.0;
input = input.substring(0, input.length() - 1);
}

try {
return Double.parseDouble(input) * multiplier;
} catch (NumberFormatException e) {
return -1.0;
}
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can use this command.");
return true;
}

Player player = (Player) sender;

if (args.length < 2) {
player.sendMessage("Usage: /pay <username> <amount>");
return true;
}

String targetUsername = args[0];
double amount = parseAmount(args[1]);

if (amount < 0) {
player.sendMessage("Invalid amount format. Please enter a valid number.");
return true;
}

Player targetPlayer = Bukkit.getPlayer(targetUsername);

if (targetPlayer == null) {
player.sendMessage("Player not found or not online.");
return true;
}

if (!economy.has(player, amount)) {
player.sendMessage("Insufficient funds.");
return true;
}

economy.withdrawPlayer(player, amount);
economy.depositPlayer(targetPlayer, amount);

player.sendMessage("Successfully paid " + targetPlayer.getName() + " $" + amount);
targetPlayer.sendMessage("You received $" + amount + " from " + player.getName());

return true;
}
}[/CODE]

To manage your Spigot plugin's dependencies and build using Maven, you'll need to create a pom.xml file in your project's root directory.
Remember to replace com.NullForumsEco, your-plugin-artifact-id, and 1.0-SNAPSHOT with appropriate values based on your plugin's package name, artifact ID, and version, respectively.

To build your plugin using Maven, open a terminal or command prompt, navigate to your project's root directory (where the pom.xml file is located), and run the following command:
mvn clean package

Maven XML Example:​

[CODE lang="xml" title="Pom.xml"]<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.NullForumsEco</groupId> <!-- Replace with your plugin's package name -->
<artifactId>your-plugin-artifact-id</artifactId> <!-- Replace with your plugin's artifact ID -->
<version>1.0-SNAPSHOT</version> <!-- Replace with your plugin's version -->

<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
</repositories>

<dependencies>
<!-- Spigot API -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.17.1-R0.1-SNAPSHOT</version> <!-- Replace with the appropriate version for your server -->
<scope>provided</scope>
</dependency>

<!-- Vault API for economy integration -->
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version> <!-- Replace with the appropriate version -->
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Maven Compiler Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <!-- Replace with the appropriate version -->
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<!-- Shade Plugin to create an uber-JAR with dependencies -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version> <!-- Replace with the appropriate version -->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>org.spigotmc</pattern>
<!-- You can change the package name to avoid conflicts -->
<shadedPattern>your.plugin.package.spigotmc</shadedPattern>
</relocation>
<relocation>
<pattern>net.milkbowl</pattern>
<!-- You can change the package name to avoid conflicts -->
<shadedPattern>your.plugin.package.milkbowl</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>[/CODE]

Step 4: Export the plugin​

After you have completed the plugin's code, you can export it as a JAR file. In your IDE, build the project to generate the JAR file.
If you chose to use a manager such as Maven, it will then compile your code, resolve dependencies, and package your plugin into an uber-JAR with all the required dependencies included.

After the build is successful, you can find the JAR file with dependencies in the target directory of your project. Use this JAR file to install your plugin in your Spigot server as usual.

Step 5: Install the plugin​

Copy the generated JAR file and paste it into the plugins folder of your Spigot Minecraft server. Restart the server, and the plugin should now be active and ready to use.

Please note that this is a basic implementation of the /pay command using Vault for economy management. You may want to add additional error handling, permission checks, and custom messages to improve the user experience. Additionally, consider checking if the amount is greater than zero before processing the payment, and handle any other specific requirements you have for your server.
 

Status

offline

Posts

9

Likes

33

Resources

2

Bits Credits

0

LEVEL

1

220 XP

How can we put this into java? Can you open a guide for this?
 

FateKid

DEVIL DID IT

Admin

Status

offline

Posts

14,980

Likes

131,702

Resources

3,400

Bits Credits

511

LEVEL

11

5,910 XP

How can we put this into java? Can you open a guide for this?
There are a few video tutorials I want to make, eventually I will make a video for this one as well.
I'm sure Envoia can assist more with this than I can ;)
 
Liked by:

Latest threads

Forum statistics

Threads
59,491
Messages
90,775
Members
56,494
Latest member
darkhorizon
Top Bottom