Kasia Ibek

Create plugins paper 1.21.1 in Minecraft Which one using command /portalgun I give you a green candle which you can click right block doesn't build it teleport you a random place max 15 block cooldown 30 second

Kodari AI
KodariSep 26 at 9:14 am 1
PortalCandle
A Minecraft Paper 1.21.1 plugin adding a green candle item that serves as a teleportation device. When a player right-clicks a block while holding the candle, it does not place the candle but instead teleports them to a random location within a 15-block radius of the clicked position. The teleportation has a 30-second cooldown per player, preventing spam. The plugin registers a custom recipe to craft the green candle, tracks cooldowns via a hashmap, and provides clear action bar messages showing remaining cooldown time. Includes configurable cooldown duration and teleport radius values.
  • Create a Java 21 Gradle project targeting Paper 1.21.1.

  • Add /portalgun to give a marked green candle.

  • Make right-clicking a block teleport the player to a safe random spot within 15 blocks, with a 30-second cooldown.

Created the Paper 1.21.1 plugin. /portalgun gives a marked green candle; right-clicking a block teleports you to a safe random spot within 15 blocks and starts a 30-second cooldown.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
4 174
src/main/java/com/example/portalgun/PortalGunPlugin.java
package com.example.portalgun;

import com.cryptomorin.xseries.XMaterial;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

public final class PortalGunPlugin extends JavaPlugin implements Listener, CommandExecutor {
    private static final long COOLDOWN_MILLIS = 30_000L;
    private static final double MAX_DISTANCE = 15.0;

    private final Map<UUID, Long> cooldowns = new HashMap<>();
    private final Random random = new Random();
    private NamespacedKey portalGunKey;

    @Override
    public void onEnable() {
        portalGunKey = new NamespacedKey(this, "portal_gun");
        getServer().getPluginManager().registerEvents(this, this);
        getCommand("portalgun").setExecutor(this);
    }

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

        ItemStack candle = XMaterial.matchXMaterial("GREEN_CANDLE")
                .map(XMaterial::parseItem)
                .orElse(null);
        if (candle == null) {
            player.sendMessage(ChatColor.RED + "Could not create the Portal Gun.");
            return true;
        }

        ItemMeta meta = candle.getItemMeta();
        meta.setDisplayName(ChatColor.GREEN + "Portal Gun");
        meta.setLore(java.util.List.of(ChatColor.GRAY + "Right-click a block to teleport."));
        meta.getPersistentDataContainer().set(portalGunKey, PersistentDataType.BYTE, (byte) 1);
        candle.setItemMeta(meta);
        player.getInventory().addItem(candle).values().forEach(item ->
                player.getWorld().dropItemNaturally(player.getLocation(), item));
        player.sendMessage(ChatColor.GREEN + "You received a Portal Gun!");
        return true;
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (event.getHand() != EquipmentSlot.HAND || event.getAction() != Action.RIGHT_CLICK_BLOCK) {
            return;
        }

        ItemStack item = event.getItem();
        if (!isPortalGun(item)) {
            return;
        }

        event.setCancelled(true);
        Player player = event.getPlayer();
        long now = System.currentTimeMillis();
        long readyAt = cooldowns.getOrDefault(player.getUniqueId(), 0L);
        if (now < readyAt) {
            long secondsLeft = (readyAt - now + 999L) / 1000L;
            player.sendMessage(ChatColor.RED + "Portal Gun cooldown: " + secondsLeft + " seconds.");
            return;
        }

        Location destination = findSafeDestination(player, event.getClickedBlock());
        if (destination == null) {
            player.sendMessage(ChatColor.RED + "No safe destination nearby.");
            return;
        }

        if (player.teleport(destination)) {
            cooldowns.put(player.getUniqueId(), now + COOLDOWN_MILLIS);
        }
    }

    private boolean isPortalGun(ItemStack item) {
        if (item == null || !XMaterial.matchXMaterial(item.getType()).name().equals("GREEN_CANDLE")) {
            return false;
        }
        ItemMeta meta = item.getItemMeta();
        return meta != null && meta.getPersistentDataContainer().has(portalGunKey, PersistentDataType.BYTE);
    }

    private Location findSafeDestination(Player player, Block clickedBlock) {
        Location origin = player.getLocation();
        for (int attempt = 0; attempt < 30; attempt++) {
            double angle = random.nextDouble() * Math.PI * 2.0;
            double distance = Math.sqrt(random.nextDouble()) * MAX_DISTANCE;
            int x = (int) Math.floor(origin.getX() + Math.cos(angle) * distance);
            int z = (int) Math.floor(origin.getZ() + Math.sin(angle) * distance);
            int groundY = player.getWorld().getHighestBlockYAt(x, z);

            Block floor = player.getWorld().getBlockAt(x, groundY, z);
            Block feet = player.getWorld().getBlockAt(x, groundY + 1, z);
            Block head = player.getWorld().getBlockAt(x, groundY + 2, z);
            if (!floor.getType().isSolid() || floor.isLiquid()
                    || !feet.isPassable() || feet.isLiquid()
                    || !head.isPassable() || head.isLiquid()) {
                continue;
            }

            if (clickedBlock != null && clickedBlock.getWorld().equals(player.getWorld())
                    && x == clickedBlock.getX() && z == clickedBlock.getZ()) {
                continue;
            }

            return new Location(player.getWorld(), x + 0.5, groundY + 1.0, z + 0.5,
                    origin.getYaw(), origin.getPitch());
        }
        return null;
    }
}