简体   繁体   中英

Bubble plugin doesn't work

Basically I'm trying to create a plugin that allows for the someone (that has perms) to do /bubble , and whoever's name corresponds to that name will get "bubbled". This bubble is basically a rejection field, (sphere) so whenever someone besides the args[0] (username to bubble) will get flung out very fast and abruptly. I don't know if you have ever seen this, but if you need more proof of concept, mineplex.com is a minecraft server that has this concept implemented into treasure chests. Basically whenever someone clicks on that, they become trapped in a 1x2 area, and everyone that tries to come withing 5 blocks of them gets shot out, kind of like them bouncing off. Here is the code I've come up with. I don't know why this doesn't work, there aren't any errors, but it doesn't fling them out.

NOTE: I'm using a main class that calls this one (the main class is called "Main". Also, This class is called "Bubble".

EDIT: I just updated the code to use hashmaps. They aren't currently implemented, but I would like to use them in the plugin.

package me.Glowhoo.EpicUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Arrays;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
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.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Vector;
/*
 * Author =
 * Glowhoo
 * 
 */
public class Bubble implements CommandExecutor, Listener {
private Main plugin;

public Bubble(Main plugin)
{
  this.plugin = plugin;
}
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
    if (cmd.getName().equalsIgnoreCase("bubble"))
    {
        if (sender instanceof Player)
        {
            if (args.length > 0 && args.length <= 2)
            {
            if (Bukkit.getPlayer(args[0]) != null)
            { //Note: I suck with hashmaps.
                HashMap<String, Boolean> bubbles = new HashMap<>(); //Attempted to make a hashmap of the player which has the bubble, and if the bubble is on/off.
                Player victim = (Bukkit.getPlayer(args[0]));
                Bukkit.broadcastMessage(ChatColor.BOLD.GREEN + victim.getName() + ChatColor.BOLD.DARK_GRAY + " Is now in a bubble!");
                FixedMetadataValue metadataValue = new FixedMetadataValue((Plugin)this.plugin, true);
                victim.setMetadata("isInBubble", metadataValue);


                if (args[1].equalsIgnoreCase("on")) //i.e /bubble <username> <on/off>
                {
                bubbles.put(args[0], true);
                }else if (args[1].equalsIgnoreCase("off"))
                {
                    bubbles.put(args[0], false);
                }


            }
            else
            {
                sender.sendMessage(ChatColor.RED + "Player is not online!");

            }

            }
            else
            {
                sender.sendMessage(ChatColor.RED + "Invalid arguments!");
            }


        }
        else
        {
            sender.sendMessage(ChatColor.AQUA + "The console cannot bubble someone!");
        }

    }



    return false;
}
public void onPlayerMove(PlayerMoveEvent e) {
     Player mover = e.getPlayer();
     Location from = e.getFrom();
     Location to = e.getTo();
     Collection<Entity> nearbyEntities = mover.getWorld().
     getNearbyEntities(from,  10, 10, 10);//Get entities in a 10 block square from loc "from"
     List<Player> nearbyPlayers = new ArrayList<Player>();
     for (Entity en : nearbyEntities) {
         if (en instanceof Player)
           nearbyPlayers.add((Player) en);
     }
     for (Player victim : nearbyPlayers) {
         if (victim.hasMetadata("isInBubble") && victim != mover) {
            Location victimLoc = victim.getLocation();
            if (victimLoc.distance(to) <= 5) {//Radius 5
                e.setCancelled(true); //Cancel so cant move
                return; //we have nothing left no need to get in for statement again
            }
            }
         }
     }
 }

You have forgot to add the @EventHandler annotation before onPlayerMove method.

You also need to register the PlayerMoveEvent in the plugin manager, so add the following code to your onEnable() method:

getServer().getPluginManager().registerEvents(this, new Bubble());

If you would like to store the bubble statuses on players in a HashMap , you need to use a HashMap<UUID, Boolean> field, where you store the player UUIDs and the bubble toggled status. You need to store the player when he joins and remove him when he quits the game.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM