gpt4 book ai didi

java - 获取一个新字符串,除非某个值等于 false java

转载 作者:行者123 更新时间:2023-12-02 02:49:07 25 4
gpt4 key购买 nike

我目前正在制作一个 spigot 插件,该插件会在加入时随机生成用户。我主要有两种方法。 getLocationListgetRandomStringFromList

public String getRandomStringFromList(List<String> list) {
ThreadLocalRandom random = ThreadLocalRandom.current();
int randomNumber = random.nextInt(list.size());
String randomString = list.get(randomNumber);

return randomString;
}
private List<String> locationList;

public List<String> getLocationList() {
if (this.locationList == null)
this.locationList = new ArrayList<>();
this.locationList = locationList.stream().distinct().collect(Collectors.toList());
return this.locationList;
}

然后,在我的事件类中,我有这个。

RandomSpawn plugin;

public RSEvent(RandomSpawn instance) {
plugin = instance;
}
@EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent e) {

Player p = e.getPlayer();
String randomString = plugin.getRandomStringFromList(plugin.getLocationList());

p.teleport(new Location(Bukkit.getWorld(String.valueOf(plugin.getConfig().getString("locations." + randomString + ".world"))),
plugin.getConfig().getDouble("locations." + randomString + ".x"),
plugin.getConfig().getDouble("locations." + randomString + ".y"),
plugin.getConfig().getDouble("locations." + randomString + ".z"),
plugin.getConfig().getInt("locations." + randomString + ".yaw"),
plugin.getConfig().getInt("locations." + randomString + ".pitch")));

这工作得很好,但是,我添加了一个禁用命令。因此,在配置中,每个位置都会有一个新的默认值,称为“禁用”。可以将其设置为 true 或 false。我希望插件检查 randomString.disabled 是否设置为 true,如果是,则重新运行整个 getRandomStringFromList 事物以获取新的 randomString 直到禁用设置为 false。

最佳答案

我将您的问题解释为您想要从列表中选择一个随机位置(由字符串表示)。但是,任何被禁用的位置(存储在其他地方)都不应被选择。

您的方法是选择一个随机位置,检查它是否已禁用,如果是,请重试。重复此操作,直到弹出未禁用的位置。这不是一个好主意。
如果所有位置都被禁用怎么办? → 无限循环
如果仅启用多个位置之一怎么办? → 许多位置都进行了不必要的测试

更好的方法是获取位置列表,对其进行过滤,以便仅保留已启用的位置,然后随机选择一个。当过滤后的列表为空时,您就知道您可能遇到了问题(也许您可以选择一个默认位置,不知何故?)。

可以使用 streams 以优雅的方式过滤列表。 。之后creating一个stream从我们最初的位置列表(字符串)中,我们可以 filter流然后 collect那些地点 in a new List 。过滤功能可以查找某个位置是否被禁用。

这是一个过滤的小示例,它可以自行编译并演示该想法。

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

class Filtering {

public static void main(String[] args) {
// a list of strings
List<String> strings = new ArrayList<>();
strings.add("foo");
strings.add("bar");
strings.add("baz");

// a quick and dirty way of representing if a string is enabled
HashSet<String> disabled = new HashSet<>();
disabled.add("bar");

// filter strings to only enabled ones
strings.stream()
.filter(s -> !disabled.contains(s))
.forEach(System.out::println);
}

}

在您的代码中,所做的更改可能应如下所示。

@EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent e) {
Player p = e.getPlayer();
FileConfiguration cfg = plugin.getConfig();

// get all enabled locations
List<String> locations = plugin.getLocationList().stream().
filter(s -> !cfg.getBoolean("locations." + s + ".disabled")).
collect(Collectors.toList());
if (locations.size() === 0) {
// TODO: handle this special case
}

// teleport to random location
String randomString = plugin.getRandomStringFromList(locations);
String cfgPrefix = "locations." + randomString + ".";
p.teleport(new Location(
Bukkit.getWorld(cfg.getString(cfgPrefix + "world")),
cfg.getDouble(cfgPrefix + "x"),
cfg.getDouble(cfgPrefix + "y"),
cfg.getDouble(cfgPrefix + "z"),
cfg.getInt(cfgPrefix + "yaw"),
cfg.getInt(cfgPrefix + "pitch")));
}

我引入了一些临时变量来提高可读性,删除了不必要的 String.valueOf 调用,并对格式进行了微小的更改。然而,主要的变化是//获取所有启用的位置下面的 block 。那里发生的事情应该很直观,但是如果您需要我澄清任何事情,请告诉我。

关于java - 获取一个新字符串,除非某个值等于 false java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44095125/

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