气泡剔除
Bubble rejection
我正在尝试编写一个带有两个参数的命令:第一个是要泡泡的玩家,第二个是泡泡的半径。
它看起来像 /bubble <player> <radius>
.
我基本上希望任何在该半径范围内行走的人都被射出很远,但我真的不知道该怎么做。
我坚持的主要部分是知道是否有人在距受害者 5 个街区以内(使用元数据)以及如何对他们进行排序。
package me.Glowhoo.EpicUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
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.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.plugin.Plugin;
/*
* Author =
* Glowhoo
*
*/
public class Bubble implements CommandExecutor, Listener {
private Main plugin;
public Bubble(Main plugin)
{
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
Bukkit.getServer().getPluginManager().registerEvents(this, this.plugin);
if (cmd.getName().equalsIgnoreCase("bubble"))
{
if (sender instanceof Player)
{
if (args.length > 0 && args.length <= 2)
{
if (Bukkit.getPlayer(args[0]) != null)
{
Player victim = (Bukkit.getPlayer(args[0]));
FixedMetadataValue metadataValue = new FixedMetadataValue(this.plugin, true);
if (args[1].equalsIgnoreCase("off")){
Bukkit.broadcastMessage(ChatColor.GREEN + victim.getName() + ChatColor.DARK_GRAY + " Is no longer in a bubble!");
victim.removeMetadata("isInbubble", this.plugin);
return true;
}else if (args[1].equalsIgnoreCase("on")){
Bukkit.broadcastMessage(ChatColor.GREEN + victim.getName() + ChatColor.DARK_GRAY + " Is now in a bubble!");
victim.setMetadata("isInBubble", metadataValue);
if (victim.hasMetadata("isInBubble"))
{
victim.sendMessage("Metadata assigned");
}
return true;
}
}
else
{
sender.sendMessage(ChatColor.RED + "Player is not online!");
return false;
}
}
else
{
sender.sendMessage(ChatColor.RED + "Invalid arguments!");
return false;
}
}
else
{
sender.sendMessage(ChatColor.AQUA + "The console cannot bubble someone!");
return false;
}
}
return false;
}
@EventHandler
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
mover.sendMessage("mover");
victim.sendMessage("victim");
//Cancel so cant move
//we have nothing left no need to get in for statement again
}
}
}
}
}
向人们插入气泡元数据,以便我们知道谁在气泡中。
FixedMetadataValue metadataValue = new FixedMetadataValue(plugin, true);
victim.setMetadata("isInBubble", metadataValue);
现在你需要处理 PlayerMoveEvent
并让玩家围绕移动者并检查这些 "isInBubble" 中是否有这样的(不需要检查值):
mover.hasMetadata("isInBubble");
要删除它,您需要使用与创建它时相同的插件 class。
mover.removeMetadata("isInBubble", plugin);
我为你写了这段代码:
@EventHandler
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
}
}
}
}
}
您可以使用元数据来设置和获取半径。
关注@EventHandler
.
我正在尝试编写一个带有两个参数的命令:第一个是要泡泡的玩家,第二个是泡泡的半径。
它看起来像 /bubble <player> <radius>
.
我基本上希望任何在该半径范围内行走的人都被射出很远,但我真的不知道该怎么做。 我坚持的主要部分是知道是否有人在距受害者 5 个街区以内(使用元数据)以及如何对他们进行排序。
package me.Glowhoo.EpicUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
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.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.plugin.Plugin;
/*
* Author =
* Glowhoo
*
*/
public class Bubble implements CommandExecutor, Listener {
private Main plugin;
public Bubble(Main plugin)
{
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
Bukkit.getServer().getPluginManager().registerEvents(this, this.plugin);
if (cmd.getName().equalsIgnoreCase("bubble"))
{
if (sender instanceof Player)
{
if (args.length > 0 && args.length <= 2)
{
if (Bukkit.getPlayer(args[0]) != null)
{
Player victim = (Bukkit.getPlayer(args[0]));
FixedMetadataValue metadataValue = new FixedMetadataValue(this.plugin, true);
if (args[1].equalsIgnoreCase("off")){
Bukkit.broadcastMessage(ChatColor.GREEN + victim.getName() + ChatColor.DARK_GRAY + " Is no longer in a bubble!");
victim.removeMetadata("isInbubble", this.plugin);
return true;
}else if (args[1].equalsIgnoreCase("on")){
Bukkit.broadcastMessage(ChatColor.GREEN + victim.getName() + ChatColor.DARK_GRAY + " Is now in a bubble!");
victim.setMetadata("isInBubble", metadataValue);
if (victim.hasMetadata("isInBubble"))
{
victim.sendMessage("Metadata assigned");
}
return true;
}
}
else
{
sender.sendMessage(ChatColor.RED + "Player is not online!");
return false;
}
}
else
{
sender.sendMessage(ChatColor.RED + "Invalid arguments!");
return false;
}
}
else
{
sender.sendMessage(ChatColor.AQUA + "The console cannot bubble someone!");
return false;
}
}
return false;
}
@EventHandler
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
mover.sendMessage("mover");
victim.sendMessage("victim");
//Cancel so cant move
//we have nothing left no need to get in for statement again
}
}
}
}
}
向人们插入气泡元数据,以便我们知道谁在气泡中。
FixedMetadataValue metadataValue = new FixedMetadataValue(plugin, true);
victim.setMetadata("isInBubble", metadataValue);
现在你需要处理 PlayerMoveEvent
并让玩家围绕移动者并检查这些 "isInBubble" 中是否有这样的(不需要检查值):
mover.hasMetadata("isInBubble");
要删除它,您需要使用与创建它时相同的插件 class。
mover.removeMetadata("isInBubble", plugin);
我为你写了这段代码:
@EventHandler
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
}
}
}
}
}
您可以使用元数据来设置和获取半径。
关注@EventHandler
.