我正在制作一个迷你游戏插件,我需要替换所有不是类型的 block ,所以我做了这个:
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);
}
map 字典是=> gold_block*wool:15*tained_hardened_clay:4*wool:4*tained_hardened_clay:3*ished_hardened_clay:11*wool:9*wool:11*tained_hardened_clay:9*wool:10*wool :2*染色硬化粘土:10*染色硬化粘土:2*染色硬化粘土:6*钻石 block *海晶石:2*海晶石:1*瓜 block *羊毛:5*史莱姆* Jade block * quartz block *染色硬化粘土*砂岩:2*下界砖*羊毛:14*羊毛:13
block 变量是: tained_hardened_clay:14
当我调用该函数时,它会替换每个 block ,而不是除了我想要的 block 之外的所有 block
有办法解决吗?
最佳答案
不要对字符串使用“==”。使用 b.equals(block)。因为“==”比较的是String对象的哈希值。 equals 方法比较字符串本身。
更多信息:https://www.java67.com/2012/11/difference-between-operator-and-equals-method-in.html
关于java - (龙头1.8) "stained_hardened_clay:14"!= "stained_hardened_clay:14",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71125805/