(龙头 1.8) "stained_hardened_clay:14" != " stained_hardened_clay:14"

(Spigot 1.8) "stained_hardened_clay:14" != " stained_hardened_clay:14"

我正在制作一个小游戏插件,我需要替换所有不是类型的方块,所以我做了这个:

    public static void replaceBlock(String x, String y, String z, String x2, String y2, String z2, String block, String secondBlockType, String mapDict) {
    String[] BlocksToReplace = mapDict.split("\*");
    System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " " + x + " " + y + " " + z + " " + x2 + " " + y2 + " " + z2);
    ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
    String pos1 = "/pos1 " + x + "," + y + "," + z;
    String pos2 = "/pos2 " + x2 + "," + y2 + "," + z2;
    Bukkit.dispatchCommand(console, pos1);
    Bukkit.dispatchCommand(console, pos2);
    tasak =  Bukkit.getServer().getScheduler().runTaskTimer(Main.plugin, new Runnable() {
        private int count = 2;
        @Override
        public void run() {
            if(count == 0) {
                for(String b : BlocksToReplace) {
                    if(b == block) {
                        System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " fdbshjfbdjshq ");
                    }else {
                        String fillcmd = "/replace " + b + " " + secondBlockType;
                        System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " " + fillcmd + "         " + block);
                        Bukkit.dispatchCommand(console, fillcmd);
                    }
                }
                tasak.cancel();
            } else {
                count--;
            }
        }
    }, 20, 20);
}

地图字典是 => gold_block*wool:15*stained_hardened_clay:4*wool:4*stained_hardened_clay:3*stained_hardened_clay:11*wool:9*wool:11*stained_hardened_clay:9*wool:10*wool:2*stained_hardened_clay:10*stained_hardened_clay:2*stained_hardened_clay:6*diamond_block*prismarine:2*prismarine:1*melon_block*wool:5*slime*emerald_block*quartz_block*stained_hardened_clay*sandstone:2*nether_brick*wool:14*wool:13

块变量是:stained_hardened_clay:14

当我调用该函数时,它会替换所有块,但不包括我想要的块

有什么办法可以解决吗?

不要对字符串使用“==”。使用 b.equals(块)。因为“==”比较的是String对象的哈希值。而 equals 方法比较字符串本身。

更多信息:https://www.java67.com/2012/11/difference-between-operator-and-equals-method-in.html