Minecraft Java 插件 - 方块位置相同但仍然无效
Minecraft Java Plugin - Block locations are the same but it still doesnt work
谁能回答这个问题。我将信标位置存储在配置中。当信标被破坏时,我正在获取正在破坏的块的位置并检查它是否等于我的配置中的那个。我不知道为什么它不起作用,我什至做了 p.sendmessage 并将两个值作为消息发送,它们完全相同...
https://pastebin.com/Kd4eZyJc第1181行是区块中断事件
区块中断事件:
public HashMap<Integer, Block> beaconStore = new HashMap<>();
// WHEN PLAYER BREAKS BLOCK
@EventHandler
public void onBreak(BlockBreakEvent event){
Player p = event.getPlayer();
Block block = event.getBlock();
if (p.getItemInHand().getType() == Material.WOOD_HOE) {
event.setCancelled(true);
w = block.getLocation().getWorld().getName();
x = block.getLocation().getX() + 0.500;
y = block.getLocation().getY();
z = block.getLocation().getZ() + 0.500;
p.sendMessage("§6Saved coordinates for block. (World:" + w + " x:" + x + "," + " y:" + y + "," + " z:" + z + ")");
}
if (gameRunning == true) {
boolean okBreak = false;
for(Integer integer : BlockReset.keySet()){
Block bl = BlockReset.get(integer);
if (bl.equals(block)) {
okBreak = true;
}
else {
}
}
Location blockLocation = block.getLocation();
if (block.getType() == Material.BEACON) {
okBreak = true;
p.sendMessage(" " + blockLocation);
World world = block.getWorld();
double xyellowBeacon1 = getConfig().getDouble("yellowbeacon1.x");
double yyellowBeacon1 = getConfig().getDouble("yellowbeacon1.y");
double zyellowBeacon1 = getConfig().getDouble("yellowbeacon1.z");
Location yellowBeacon1Location = new Location(world, xyellowBeacon1, yyellowBeacon1, zyellowBeacon1);
double xyellowBeacon2 = getConfig().getDouble("yellowbeacon2.x");
double yyellowBeacon2 = getConfig().getDouble("yellowbeacon2.y");
double zyellowBeacon2 = getConfig().getDouble("yellowbeacon2.z");
Location yellowBeacon2Location = new Location(world, xyellowBeacon2, yyellowBeacon2, zyellowBeacon2);
double xyellowBeacon3 = getConfig().getDouble("yellowbeacon3.x");
double yyellowBeacon3 = getConfig().getDouble("yellowbeacon3.y");
double zyellowBeacon3 = getConfig().getDouble("yellowbeacon3.z");
Location yellowBeacon3Location = new Location(world, xyellowBeacon3, yyellowBeacon3, zyellowBeacon3);
double xyellowBeacon4 = getConfig().getDouble("yellowbeacon4.x");
double yyellowBeacon4 = getConfig().getDouble("yellowbeacon4.y");
double zyellowBeacon4 = getConfig().getDouble("yellowbeacon4.z");
Location yellowBeacon4Location = new Location(world, xyellowBeacon4, yyellowBeacon4, zyellowBeacon4);
if (blockLocation == yellowBeacon1Location) {
yellowbcount--;
}
if (blockLocation == yellowBeacon2Location) {
yellowbcount--;
}
if (blockLocation == yellowBeacon3Location) {
yellowbcount--;
}
if (blockLocation == yellowBeacon4Location) {
yellowbcount--;
}
double xblueBeacon1 = getConfig().getDouble("bluebeacon1.x");
double yblueBeacon1 = getConfig().getDouble("bluebeacon1.y");
double zblueBeacon1 = getConfig().getDouble("bluebeacon1.z");
Location blueBeacon1Location = new Location(world, xblueBeacon1, yblueBeacon1, zblueBeacon1);
double xblueBeacon2 = getConfig().getDouble("bluebeacon2.x");
double yblueBeacon2 = getConfig().getDouble("bluebeacon2.y");
double zblueBeacon2 = getConfig().getDouble("bluebeacon2.z");
Location blueBeacon2Location = new Location(world, xblueBeacon2, yblueBeacon2, zblueBeacon2);
double xblueBeacon3 = getConfig().getDouble("bluebeacon3.x");
double yblueBeacon3 = getConfig().getDouble("bluebeacon3.y");
double zblueBeacon3 = getConfig().getDouble("bluebeacon3.z");
Location blueBeacon3Location = new Location(world, xblueBeacon3, yblueBeacon3, zblueBeacon3);
double xblueBeacon4 = getConfig().getDouble("bluebeacon4.x");
double yblueBeacon4 = getConfig().getDouble("bluebeacon4.y");
double zblueBeacon4 = getConfig().getDouble("bluebeacon4.z");
Location blueBeacon4Location = new Location(world, xblueBeacon4, yblueBeacon4, zblueBeacon4);
if (blockLocation == blueBeacon1Location) {
bluebcount--;
}
if (blockLocation == blueBeacon2Location) {
bluebcount--;
}
if (blockLocation == blueBeacon3Location) {
bluebcount--;
}
if (blockLocation == blueBeacon4Location) {
bluebcount--;
}
double xgreenBeacon1 = getConfig().getDouble("greenbeacon1.x");
double ygreenBeacon1 = getConfig().getDouble("greenbeacon1.y");
double zgreenBeacon1 = getConfig().getDouble("greenbeacon1.z");
Location greenBeacon1Location = new Location(world, xgreenBeacon1, ygreenBeacon1, zgreenBeacon1);
double xgreenBeacon2 = getConfig().getDouble("greenbeacon2.x");
double ygreenBeacon2 = getConfig().getDouble("greenbeacon2.y");
double zgreenBeacon2 = getConfig().getDouble("greenbeacon2.z");
Location greenBeacon2Location = new Location(world, xgreenBeacon2, ygreenBeacon2, zgreenBeacon2);
double xgreenBeacon3 = getConfig().getDouble("greenbeacon3.x");
double ygreenBeacon3 = getConfig().getDouble("greenbeacon3.y");
double zgreenBeacon3 = getConfig().getDouble("greenbeacon3.z");
Location greenBeacon3Location = new Location(world, xgreenBeacon3, ygreenBeacon3, zgreenBeacon3);
double xgreenBeacon4 = getConfig().getDouble("greenbeacon4.x");
double ygreenBeacon4 = getConfig().getDouble("greenbeacon4.y");
double zgreenBeacon4 = getConfig().getDouble("greenbeacon4.z");
Location greenBeacon4Location = new Location(world, xgreenBeacon4, ygreenBeacon4, zgreenBeacon4);
if (blockLocation == greenBeacon1Location) {
greenbcount--;
}
if (blockLocation == greenBeacon2Location) {
greenbcount--;
}
if (blockLocation == greenBeacon3Location) {
greenbcount--;
}
if (blockLocation == greenBeacon4Location) {
greenbcount--;
}
double xredBeacon1 = getConfig().getDouble("redbeacon1.x");
double yredBeacon1 = getConfig().getDouble("redbeacon1.y");
double zredBeacon1 = getConfig().getDouble("redbeacon1.z");
Location redBeacon1Location = new Location(world, xredBeacon1, yredBeacon1, zredBeacon1);
p.sendMessage(" " + redBeacon1Location);
double xredBeacon2 = getConfig().getDouble("redbeacon2.x");
double yredBeacon2 = getConfig().getDouble("redbeacon2.y");
double zredBeacon2 = getConfig().getDouble("redbeacon2.z");
Location redBeacon2Location = new Location(world, xredBeacon2, yredBeacon2, zredBeacon2);
double xredBeacon3 = getConfig().getDouble("redbeacon3.x");
double yredBeacon3 = getConfig().getDouble("redbeacon3.y");
double zredBeacon3 = getConfig().getDouble("redbeacon3.z");
Location redBeacon3Location = new Location(world, xredBeacon3, yredBeacon3, zredBeacon3);
double xredBeacon4 = getConfig().getDouble("redbeacon4.x");
double yredBeacon4 = getConfig().getDouble("redbeacon4.y");
double zredBeacon4 = getConfig().getDouble("redbeacon4.z");
Location redBeacon4Location = new Location(world, xredBeacon4, yredBeacon4, zredBeacon4);
if (blockLocation == redBeacon1Location) {
redbcount--;
p.sendMessage("Okay!");
}
if (blockLocation == redBeacon2Location) {
redbcount--;
}
if (blockLocation == redBeacon3Location) {
redbcount--;
}
if (blockLocation == redBeacon4Location) {
redbcount--;
}
}
if (okBreak == true) {
event.setCancelled(false);
}
else {
event.setCancelled(true);
p.sendMessage("§9§lPillars " + dot + " §7You can only break blocks placed by a player!");
}
}
}
在命令中设置信标位置:
if (args[0].equalsIgnoreCase("yellowbeacon1")) {
getConfig().set("yellowbeacon1.world", w);
getConfig().set("yellowbeacon1.x", x - 0.5);
getConfig().set("yellowbeacon1.y", y);
getConfig().set("yellowbeacon1.z", z - 0.5);
saveConfig();
p.sendMessage("§9§lPillars " + dot + " §7Successfully set §eyellow §5beacon1!");
}
^^ 我为每个信标都这样做。
希望你能理解我的问题。
可能不会导致您遇到的问题,但在比较 Location
class 实例时,请使用 equals
方法而不是 ==
,因为它们肯定是当您使用相同方法创建比较位置之一时,实例不同。
与您的问题关系不大,但请稍微重构一下您的代码。如果您必须 copy-paste 某些东西,它会标记为您可以提取为方法的东西。因此创建带有签名 Location getLocation(World world, String beaconprefix)
的方法,它从配置中获取块位置并创建它,显着减少代码长度。
private Location getLocation(World world, String beaconPrefix) {
double x = getConfig().getDouble(beaconPrefix + ".x");
double y = getConfig().getDouble(beaconPrefix + ".y");
double z = getConfig().getDouble(beaconPrefix + ".z");
return new Location(world, x, y, z);
}
谁能回答这个问题。我将信标位置存储在配置中。当信标被破坏时,我正在获取正在破坏的块的位置并检查它是否等于我的配置中的那个。我不知道为什么它不起作用,我什至做了 p.sendmessage 并将两个值作为消息发送,它们完全相同...
https://pastebin.com/Kd4eZyJc第1181行是区块中断事件
区块中断事件:
public HashMap<Integer, Block> beaconStore = new HashMap<>();
// WHEN PLAYER BREAKS BLOCK
@EventHandler
public void onBreak(BlockBreakEvent event){
Player p = event.getPlayer();
Block block = event.getBlock();
if (p.getItemInHand().getType() == Material.WOOD_HOE) {
event.setCancelled(true);
w = block.getLocation().getWorld().getName();
x = block.getLocation().getX() + 0.500;
y = block.getLocation().getY();
z = block.getLocation().getZ() + 0.500;
p.sendMessage("§6Saved coordinates for block. (World:" + w + " x:" + x + "," + " y:" + y + "," + " z:" + z + ")");
}
if (gameRunning == true) {
boolean okBreak = false;
for(Integer integer : BlockReset.keySet()){
Block bl = BlockReset.get(integer);
if (bl.equals(block)) {
okBreak = true;
}
else {
}
}
Location blockLocation = block.getLocation();
if (block.getType() == Material.BEACON) {
okBreak = true;
p.sendMessage(" " + blockLocation);
World world = block.getWorld();
double xyellowBeacon1 = getConfig().getDouble("yellowbeacon1.x");
double yyellowBeacon1 = getConfig().getDouble("yellowbeacon1.y");
double zyellowBeacon1 = getConfig().getDouble("yellowbeacon1.z");
Location yellowBeacon1Location = new Location(world, xyellowBeacon1, yyellowBeacon1, zyellowBeacon1);
double xyellowBeacon2 = getConfig().getDouble("yellowbeacon2.x");
double yyellowBeacon2 = getConfig().getDouble("yellowbeacon2.y");
double zyellowBeacon2 = getConfig().getDouble("yellowbeacon2.z");
Location yellowBeacon2Location = new Location(world, xyellowBeacon2, yyellowBeacon2, zyellowBeacon2);
double xyellowBeacon3 = getConfig().getDouble("yellowbeacon3.x");
double yyellowBeacon3 = getConfig().getDouble("yellowbeacon3.y");
double zyellowBeacon3 = getConfig().getDouble("yellowbeacon3.z");
Location yellowBeacon3Location = new Location(world, xyellowBeacon3, yyellowBeacon3, zyellowBeacon3);
double xyellowBeacon4 = getConfig().getDouble("yellowbeacon4.x");
double yyellowBeacon4 = getConfig().getDouble("yellowbeacon4.y");
double zyellowBeacon4 = getConfig().getDouble("yellowbeacon4.z");
Location yellowBeacon4Location = new Location(world, xyellowBeacon4, yyellowBeacon4, zyellowBeacon4);
if (blockLocation == yellowBeacon1Location) {
yellowbcount--;
}
if (blockLocation == yellowBeacon2Location) {
yellowbcount--;
}
if (blockLocation == yellowBeacon3Location) {
yellowbcount--;
}
if (blockLocation == yellowBeacon4Location) {
yellowbcount--;
}
double xblueBeacon1 = getConfig().getDouble("bluebeacon1.x");
double yblueBeacon1 = getConfig().getDouble("bluebeacon1.y");
double zblueBeacon1 = getConfig().getDouble("bluebeacon1.z");
Location blueBeacon1Location = new Location(world, xblueBeacon1, yblueBeacon1, zblueBeacon1);
double xblueBeacon2 = getConfig().getDouble("bluebeacon2.x");
double yblueBeacon2 = getConfig().getDouble("bluebeacon2.y");
double zblueBeacon2 = getConfig().getDouble("bluebeacon2.z");
Location blueBeacon2Location = new Location(world, xblueBeacon2, yblueBeacon2, zblueBeacon2);
double xblueBeacon3 = getConfig().getDouble("bluebeacon3.x");
double yblueBeacon3 = getConfig().getDouble("bluebeacon3.y");
double zblueBeacon3 = getConfig().getDouble("bluebeacon3.z");
Location blueBeacon3Location = new Location(world, xblueBeacon3, yblueBeacon3, zblueBeacon3);
double xblueBeacon4 = getConfig().getDouble("bluebeacon4.x");
double yblueBeacon4 = getConfig().getDouble("bluebeacon4.y");
double zblueBeacon4 = getConfig().getDouble("bluebeacon4.z");
Location blueBeacon4Location = new Location(world, xblueBeacon4, yblueBeacon4, zblueBeacon4);
if (blockLocation == blueBeacon1Location) {
bluebcount--;
}
if (blockLocation == blueBeacon2Location) {
bluebcount--;
}
if (blockLocation == blueBeacon3Location) {
bluebcount--;
}
if (blockLocation == blueBeacon4Location) {
bluebcount--;
}
double xgreenBeacon1 = getConfig().getDouble("greenbeacon1.x");
double ygreenBeacon1 = getConfig().getDouble("greenbeacon1.y");
double zgreenBeacon1 = getConfig().getDouble("greenbeacon1.z");
Location greenBeacon1Location = new Location(world, xgreenBeacon1, ygreenBeacon1, zgreenBeacon1);
double xgreenBeacon2 = getConfig().getDouble("greenbeacon2.x");
double ygreenBeacon2 = getConfig().getDouble("greenbeacon2.y");
double zgreenBeacon2 = getConfig().getDouble("greenbeacon2.z");
Location greenBeacon2Location = new Location(world, xgreenBeacon2, ygreenBeacon2, zgreenBeacon2);
double xgreenBeacon3 = getConfig().getDouble("greenbeacon3.x");
double ygreenBeacon3 = getConfig().getDouble("greenbeacon3.y");
double zgreenBeacon3 = getConfig().getDouble("greenbeacon3.z");
Location greenBeacon3Location = new Location(world, xgreenBeacon3, ygreenBeacon3, zgreenBeacon3);
double xgreenBeacon4 = getConfig().getDouble("greenbeacon4.x");
double ygreenBeacon4 = getConfig().getDouble("greenbeacon4.y");
double zgreenBeacon4 = getConfig().getDouble("greenbeacon4.z");
Location greenBeacon4Location = new Location(world, xgreenBeacon4, ygreenBeacon4, zgreenBeacon4);
if (blockLocation == greenBeacon1Location) {
greenbcount--;
}
if (blockLocation == greenBeacon2Location) {
greenbcount--;
}
if (blockLocation == greenBeacon3Location) {
greenbcount--;
}
if (blockLocation == greenBeacon4Location) {
greenbcount--;
}
double xredBeacon1 = getConfig().getDouble("redbeacon1.x");
double yredBeacon1 = getConfig().getDouble("redbeacon1.y");
double zredBeacon1 = getConfig().getDouble("redbeacon1.z");
Location redBeacon1Location = new Location(world, xredBeacon1, yredBeacon1, zredBeacon1);
p.sendMessage(" " + redBeacon1Location);
double xredBeacon2 = getConfig().getDouble("redbeacon2.x");
double yredBeacon2 = getConfig().getDouble("redbeacon2.y");
double zredBeacon2 = getConfig().getDouble("redbeacon2.z");
Location redBeacon2Location = new Location(world, xredBeacon2, yredBeacon2, zredBeacon2);
double xredBeacon3 = getConfig().getDouble("redbeacon3.x");
double yredBeacon3 = getConfig().getDouble("redbeacon3.y");
double zredBeacon3 = getConfig().getDouble("redbeacon3.z");
Location redBeacon3Location = new Location(world, xredBeacon3, yredBeacon3, zredBeacon3);
double xredBeacon4 = getConfig().getDouble("redbeacon4.x");
double yredBeacon4 = getConfig().getDouble("redbeacon4.y");
double zredBeacon4 = getConfig().getDouble("redbeacon4.z");
Location redBeacon4Location = new Location(world, xredBeacon4, yredBeacon4, zredBeacon4);
if (blockLocation == redBeacon1Location) {
redbcount--;
p.sendMessage("Okay!");
}
if (blockLocation == redBeacon2Location) {
redbcount--;
}
if (blockLocation == redBeacon3Location) {
redbcount--;
}
if (blockLocation == redBeacon4Location) {
redbcount--;
}
}
if (okBreak == true) {
event.setCancelled(false);
}
else {
event.setCancelled(true);
p.sendMessage("§9§lPillars " + dot + " §7You can only break blocks placed by a player!");
}
}
}
在命令中设置信标位置:
if (args[0].equalsIgnoreCase("yellowbeacon1")) {
getConfig().set("yellowbeacon1.world", w);
getConfig().set("yellowbeacon1.x", x - 0.5);
getConfig().set("yellowbeacon1.y", y);
getConfig().set("yellowbeacon1.z", z - 0.5);
saveConfig();
p.sendMessage("§9§lPillars " + dot + " §7Successfully set §eyellow §5beacon1!");
}
^^ 我为每个信标都这样做。
希望你能理解我的问题。
可能不会导致您遇到的问题,但在比较 Location
class 实例时,请使用 equals
方法而不是 ==
,因为它们肯定是当您使用相同方法创建比较位置之一时,实例不同。
与您的问题关系不大,但请稍微重构一下您的代码。如果您必须 copy-paste 某些东西,它会标记为您可以提取为方法的东西。因此创建带有签名 Location getLocation(World world, String beaconprefix)
的方法,它从配置中获取块位置并创建它,显着减少代码长度。
private Location getLocation(World world, String beaconPrefix) {
double x = getConfig().getDouble(beaconPrefix + ".x");
double y = getConfig().getDouble(beaconPrefix + ".y");
double z = getConfig().getDouble(beaconPrefix + ".z");
return new Location(world, x, y, z);
}