基本上我試圖創建一個允許某人(有燙髮)做/泡泡的插件,並且任何與該名稱相對應的名稱都會「冒泡」。這個泡沫基本上是一個拒絕領域(球體),所以除了args [0](用戶名到泡泡)之外的其他人都會非常快速和突然地被拋出。我不知道你是否曾見過這種情況,但如果你需要更多的概念證明,mineplex.com是一款將這個概念貫徹到寶箱中的Minecraft服務器。基本上每當有人點擊它時,他們就會被困在1x2的區域,每個試圖帶上5個區塊的人都會被槍殺,有點像彈跳。這是我想出的代碼。我不知道爲什麼這不起作用,沒有任何錯誤,但它並沒有把它們扔出去。泡泡插件不起作用
注:我使用的是調用本(主類被稱爲「主」主類此外,此類稱爲「泡沫」
編輯:我剛剛更新使用代碼包含HashMap,他們目前不能實現,但我想在插件中使用它們。
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
}
}
}
}
}
您也可以使用'ArrayList'而不是'HashMap '。不要將HashMap值設置爲false,而應從List中刪除,而不是將HashMap值設置爲true,將其添加到List中。 –
Jojodmo