#Ask GPT to write a plugin to place a block under a player on a command execution

1 messages · Page 1 of 1 (latest)

fickle grail
#

I asked a chatGPT bot to write a plugin with a simple task of placing a block of glowstone when executing a command “/example block”

How’d it do?


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.plugin.java.JavaPlugin;

public class ExamplePlugin extends JavaPlugin implements CommandExecutor {

    @Override
    public void onEnable() {
        // Register the "placeblock" command
        getCommand("placeblock").setExecutor(this);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player) sender;
            // Get the block below the player's location
            Material blockType = player.getLocation().subtract(0, 1, 0).getBlock().getType();
            // Check if the block below is air
            if (blockType == Material.AIR) {
                // Place a block of stone below the player's location
                player.getLocation().subtract(0, 1, 0).getBlock().setType(Material.STONE);
                player.sendMessage("Block placed!");
            } else {
                player.sendMessage("There's already a block below you.");
            }
            return true;
        }
        return false;
    }
}

slate matrix
#

good for java8, bad for java 17

#

also it likes nested ifs

fickle grail
#

Told it to update it to Java 17… and updated command to choose block type

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class PlaceBlockPlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        getLogger().info("PlaceBlockPlugin has been enabled!");
    }

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

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (label.equalsIgnoreCase("placeblock")) {
            if (!(sender instanceof Player player)) {
                sender.sendMessage("Only players can use this command!");
                return true;
            }
            Location location = player.getLocation().clone().add(0, -1, 0);
            Material material = Material.STONE;
            if (args.length > 0) {
                try {
                    material = Material.valueOf(args[0].toUpperCase());
                } catch (IllegalArgumentException ex) {
                    player.sendMessage("Invalid material!");
                    return true;
                }
            }
            location.getBlock().setType(material);
            player.sendMessage("Block placed!");
            return true;
        }
        return false;
    }
}
slate matrix
#

now it's more sussy but at least has pattern matching

#

and there is no reason to use "label.equalsIgnoreCase"

#

literally useless

fickle grail
#

😂😂 so it’s not perfect haha

slate matrix
#

there's also Material#matchMaterial which afair just returns null if not found, and no the IllegalArgumentException