Could anyone help me compile?

Discussion in 'Plugin Development' started by ldigsst, Mar 15, 2023.

Thread Status:
Not open for further replies.
  1. Offline

    ldigsst

    Hey! I just wrote a plugin and I can't for the life of me find anywhere that tells me how to compile it (in simple terms) Can anyone link a place, or compile it for me? thanks!


    Code:
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJumpEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.HashSet;
    import java.util.Random;
    import java.util.Set;
    
    public class JumpRandomItem extends JavaPlugin implements Listener {
    
        private final Set<Material> excludedItems = new HashSet<>();
        private double stackChance = 0.1;
    
        @Override
        public void onEnable() {
            // Register the plugin's listener
            getServer().getPluginManager().registerEvents(this, this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("excludeitem") && sender instanceof Player && sender.isOp()) {
                if (args.length != 1) {
                    sender.sendMessage("Usage: /excludeitem <material>");
                    return true;
                }
    
                Material material = Material.getMaterial(args[0].toUpperCase());
                if (material == null) {
                    sender.sendMessage("Invalid material: " + args[0]);
                    return true;
                }
    
                excludedItems.add(material);
                sender.sendMessage("Excluded item: " + material.toString());
                return true;
            }
    
            if (command.getName().equalsIgnoreCase("changestackchance") && sender instanceof Player && sender.isOp()) {
                if (args.length != 1) {
                    sender.sendMessage("Usage: /changestackchance <chance>");
                    return true;
                }
    
                try {
                    double chance = Double.parseDouble(args[0]);
                    if (chance < 0 || chance > 1) {
                        sender.sendMessage("Chance must be between 0 and 1");
                        return true;
                    }
    
                    stackChance = chance;
                    sender.sendMessage("Stack chance set to: " + chance);
                } catch (NumberFormatException e) {
                    sender.sendMessage("Invalid chance: " + args[0]);
                }
    
                return true;
            }
    
            return false;
        }
    
        @EventHandler
        public void onPlayerJump(PlayerJumpEvent event) {
            // Get the player who jumped
            Player player = event.getPlayer();
    
            // Create a new random instance
            Random random = new Random();
    
            // Get a random material for the item
            Material material;
            do {
                material = Material.values()[random.nextInt(Material.values().length)];
            } while (excludedItems.contains(material));
    
            // Determine the quantity of the item to give
            int quantity = random.nextInt(64) + 1;
    
            // Roll for a second stack with the configured chance
            if (random.nextDouble() < stackChance) {
                quantity += 64;
            }
    
            // Create a new ItemStack with the random material and quantity
            ItemStack item = new ItemStack(material, quantity);
    
            // Give the item to the player
            player.getInventory().addItem(item);
        }
    }
    
     
    mehboss likes this.
  2. Online

    timtower Administrator Administrator Moderator

    @ldigsst What did you use to make this?
    Did you make anything before?
     
  3. Offline

    ldigsst

    no I haven't, this is my first attempt at making anything like this
     
  4. Online

    timtower Administrator Administrator Moderator

    mehboss likes this.
  5. Offline

    ldigsst

  6. Offline

    Strahan

    I wish ChatGPT would learn that ThreadLocalRandom exists. It keeps telling people to make new Random instances, when you don't need to.
     
  7. Offline

    ldigsst

    yeah
     
  8. Online

    timtower Administrator Administrator Moderator

    Then post those errors instead of getting a bot to write your code.
     
Thread Status:
Not open for further replies.

Share This Page