dezzy_dis19234

A fishing Rod called 'Switch a roo' that when punching the entity then pressing right button switches you with the entity you punched and when you punch it do /playsound @p chain_breaks

Kodari AI
KodariJul 30 at 8:44 pm 1
SwitcharooPlugin
A Minecraft plugin that adds the “Switch a roo” fishing rod. When a player punches an entity with the marked rod, the entity becomes the switch target and the server plays the custom chain_breaks sound for that player. Right-clicking with the rod swaps the player and stored entity positions, including safe validation for removed, dead, offline, or cross-world targets. The /switcharoo command gives players the custom rod.

ાડી

Created SwitcharooPlugin, a Paper plugin featuring:

  • A named “Switch a roo” fishing rod

  • Punching an entity stores it as the switch target

  • Punching plays the chain_breaks sound

  • Right-clicking swaps the player and entity

  • /switcharoo gives the custom rod

  • Safety checks for invalid, dead, removed, or cross-world entities

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
4 199
src/main/java/com/kodari/switcharoo/SwitcharooPlugin.java
package com.kodari.switcharoo;

import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.block.Action;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;

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

public final class SwitcharooPlugin extends org.bukkit.plugin.java.JavaPlugin implements Listener, CommandExecutor {
    private final Map<UUID, UUID> switchTargets = new HashMap<>();
    private NamespacedKey rodKey;

    @Override
    public void onEnable() {
        rodKey = new NamespacedKey(this, "switcharoo_rod");
        getServer().getPluginManager().registerEvents(this, this);

        if (getCommand("switcharoo") != null) {
            getCommand("switcharoo").setExecutor(this);
        }
    }

    @Override
    public void onDisable() {
        switchTargets.clear();
    }

    @EventHandler
    public void onEntityPunch(EntityDamageByEntityEvent event) {
        if (!(event.getDamager() instanceof Player)) {
            return;
        }

        Player player = (Player) event.getDamager();
        if (!isSwitcharooRod(player.getInventory().getItemInMainHand())) {
            return;
        }

        Entity target = event.getEntity();
        if (target.getUniqueId().equals(player.getUniqueId())) {
            return;
        }

        switchTargets.put(player.getUniqueId(), target.getUniqueId());
        event.setCancelled(true);

        Bukkit.dispatchCommand(
                Bukkit.getConsoleSender(),
                "playsound chain_breaks master " + player.getName()
        );
    }

    @EventHandler
    public void onRightClick(PlayerInteractEvent event) {
        Action action = event.getAction();
        if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) {
            return;
        }

        Player player = event.getPlayer();
        if (!isSwitcharooRod(player.getInventory().getItemInMainHand())) {
            return;
        }

        UUID targetId = switchTargets.remove(player.getUniqueId());
        if (targetId == null) {
            return;
        }

        Entity target = Bukkit.getEntity(targetId);
        if (target == null || !target.isValid() || target.isDead()
                || !target.getWorld().equals(player.getWorld())) {
            event.setCancelled(true);
            return;
        }

        Location playerLocation = player.getLocation().clone();
        Location targetLocation = target.getLocation().clone();

        event.setCancelled(true);

        if (!player.teleport(targetLocation)) {
            return;
        }

        if (!target.teleport(playerLocation)) {
            player.teleport(playerLocation);
        }
    }

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

        Player player = (Player) sender;
        ItemStack rod = createSwitcharooRod();
        if (rod == null) {
            player.sendMessage(ChatColor.RED + "The Switch a roo rod could not be created.");
            return true;
        }

        Map<Integer, ItemStack> leftovers = player.getInventory().addItem(rod);
        for (ItemStack leftover : leftovers.values()) {
            player.getWorld().dropItemNaturally(player.getLocation(), leftover);
        }

        player.sendMessage(ChatColor.GREEN + "You received the " + ChatColor.AQUA + "Switch a roo"
                + ChatColor.GREEN + " rod.");
        return true;
    }

    private ItemStack createSwitcharooRod() {
        ItemStack rod = XMaterial.matchXMaterial("FISHING_ROD")
                .map(XMaterial::parseItem)
                .orElse(null);
        if (rod == null) {
            return null;
        }

        ItemMeta meta = rod.getItemMeta();
        if (meta == null) {
            return null;
        }

        meta.setDisplayName(ChatColor.AQUA + "Switch a roo");
        meta.getPersistentDataContainer().set(rodKey, PersistentDataType.BYTE, (byte) 1);
        rod.setItemMeta(meta);
        return rod;
    }

    private boolean isSwitcharooRod(ItemStack item) {
        if (item == null) {
            return false;
        }

        ItemMeta meta = item.getItemMeta();
        return meta != null
                && meta.getPersistentDataContainer().has(rodKey, PersistentDataType.BYTE);
    }
}