任何能够回答此问题的人。我正在配置中存储信标位置。当信标被破坏时,我得到了正在破坏的块的位置,并检查它是否等于我的配置中的那个。我不知道为什么它不起作用,我甚至做了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!");
}
^^我为每个灯塔都这样做。
答案 0 :(得分:2)
可能不会导致您遇到的问题,但在比较Location
类实例时,请使用equals
方法而不是==
,因为它们绝对不是当您在同一方法中创建其中一个比较位置时的相同实例。
与您的问题没有密切关系,但稍微重构您的代码。如果你必须复制粘贴一些东西,它标记为你可以提取的方法。因此,使用签名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);
}