Files
src
build.gradle
settings.gradle
Code
5 123
src/main/java/com/kodari/itemduplicator/ItemDuplicatorPlugin.java
package com.kodari.itemduplicator;
import java.util.Map;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
public final class ItemDuplicatorPlugin extends JavaPlugin implements CommandExecutor {
@Override
public void onEnable() {
saveDefaultConfig();
PluginCommand command = getCommand("x34z87y");
if (command == null) {
getLogger().severe("The x34z87y command is missing from plugin.yml.");
getServer().getPluginManager().disablePlugin(this);
return;
}
command.setExecutor(this);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage(ChatColor.RED + "Only players can use this command.");
return true;
}
if (!player.hasPermission("itemduplicator.clone")) {
player.sendMessage(ChatColor.RED + "You do not have permission to use this command.");
return true;
}
ItemStack heldItem = player.getInventory().getItemInMainHand();
if (heldItem.getType().isAir()) {
player.sendMessage(ChatColor.RED + "Hold an item in your main hand first.");
return true;
}
int configuredCopies = getConfig().getInt("copies-per-use", 100);
int maximumCopies = getConfig().getInt("max-copies", 100);
int copies = Math.min(configuredCopies, maximumCopies);
if (copies < 1) {
player.sendMessage(ChatColor.RED + "Item cloning is disabled in the configuration.");
return true;
}
ItemStack[] clonedItems = new ItemStack[copies];
for (int index = 0; index < copies; index++) {
clonedItems[index] = heldItem.clone();
}
Map<Integer, ItemStack> leftovers = player.getInventory().addItem(clonedItems);
for (ItemStack leftover : leftovers.values()) {
player.getWorld().dropItemNaturally(player.getLocation(), leftover);
}
String itemName = heldItem.getType().name();
player.sendMessage(ChatColor.GREEN + "Cloned " + copies + " copy/copies of " + itemName + ".");
if (!leftovers.isEmpty()) {
player.sendMessage(ChatColor.YELLOW + "Some copies were dropped because your inventory was full.");
}
if (getConfig().getBoolean("log-usage", true)) {
getLogger().info(player.getName() + " cloned " + copies + " copy/copies of " + itemName + ".");
}
return true;
}
}