gpt4 book ai didi

java - 检查距中心一定距离内有多少人

转载 作者:行者123 更新时间:2023-12-02 04:28:08 27 4
gpt4 key购买 nike

我正在制作一个 Spleef 插件。我需要统计大厅里的人数。

我想我可以计算出距离大厅中心一定距离内有多少人。我认为这可能比在有人键入命令时进行记录效果更好。

Main.java:

package me.olsyboy.spleef;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.file.YamlConfiguration;

import java.util.Arrays;
import java.util.List;

public class Main extends JavaPlugin {
public void onEnable(int amountOfPlayers) {
amountOfPlayers = 0;
loadConfiguration();
reloadConfig();
}

public void onDisable() {
saveDefaultConfig();
}

public void loadConfiguration() {
//See "Creating you're defaults"
getConfig().options().copyDefaults(true); // NOTE: You do not have to use "plugin." if the class extends the java plugin
//Save the config whenever you manipulate it
saveDefaultConfig();
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player player = (Player) sender;
if (cmd.getName().equalsIgnoreCase("spleef")) {
if (args[0].equalsIgnoreCase("setgame")) {
if (args.length == 2) {
String gameName = args[1]; //initialize the gameName variable here
getConfig().set("Game Locations." + gameName + ".Location", Arrays.asList(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), player.getLocation().getPitch(), player.getLocation().getYaw(), player.getLocation().getWorld().getName()));
getConfig().options().copyDefaults(true);
saveConfig();
player.sendMessage(ChatColor.AQUA + "[Spleef] " + ChatColor.YELLOW + "Spleef Game Location Set");
}
}
if (args[0].equalsIgnoreCase("join")) {
String gameName = args[1]; //initialize the gameName variable here
List<String> joinGameLocation = this.getConfig().getStringList("Game Locations." + gameName + ".Location");
String xPos = joinGameLocation.get(0);
double xPos2 = Double.parseDouble(xPos);

String yPos = joinGameLocation.get(1);
double yPos2 = Double.parseDouble(yPos);

String zPos = joinGameLocation.get(2);
double zPos2 = Double.parseDouble(zPos);

String pitch = joinGameLocation.get(3);
float pitch2 = Float.parseFloat(pitch);

String Yaw = joinGameLocation.get(4);
float Yaw2 = Float.parseFloat(Yaw);

World actualWorld = Bukkit.getWorld(joinGameLocation.get(5));
Location spleefGameLocation = new Location(actualWorld, xPos2, yPos2, zPos2);
spleefGameLocation.setPitch(pitch2);
spleefGameLocation.setYaw(Yaw2);
player.teleport(spleefGameLocation);
}
else if (!(args[0].equalsIgnoreCase("setgame"))) {
if (!args[0].equalsIgnoreCase("join")) {
player.sendMessage("/spleef join {GameName}");
}
}
}
return true;
}
}

playerJoinedGame.java:

package me.olsyboy.spleef;

public class playerJoinedGame extends Main {
public void onPlayerJoin(int amountOfPlayers)
{
amountOfPlayers = amountOfPlayers + 1;
}
}

我还没有从主类调用 onPlayerJoin 方法。

我对任何有更好的方法来计算大厅人数的人持开放态度。

最佳答案

确保您有一个 Location 对象,您希望以该对象为中心获取附近的玩家。

Location center = new Location(world, x, y, z);

然后,使用具有所需距离的 double 值。

double distance = 10D;

首先,您应该对服务器上的所有玩家进行循环:

for (Player player : Bukkit.getOnlinePlayers()) {

}

然后,获取玩家的位置:

for (Player player : Bukkit.getOnlinePlayers()) {
Location location = player.getLocation();
}

现在我们可以检查两个位置(centerlocation)之间的距离:

for (Player player : Bukkit.getOnlinePlayers()) {
Location location = player.getLocation();
if (location.distanceSquared(center) <= distance * distance) {
// Do something
}
}

注意:您应该使用distanceSquared(Location),它相当于distance(Location)的结果平方,因为 distance(Location) 使用 Java 的平方根方法,该方法非常消耗资源。

最终结果:

double distance = 10D;
Location center = new Location(Bukkit.getWorld("world"), x, y, z);

for (Player player : Bukkit.getOnlinePlayers()) {
Location location = player.getLocation();
if (location.distanceSquared(center) <= distance * distance) {
// Do something
}
}

关于java - 检查距中心一定距离内有多少人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31879939/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com