0

我有一个名为“区域”的类,它创建区域并检查坐标是否在其中。然后我有其他类'ArrowTower'扩展'Region'。'ArrowTower' 类创建塔。我整天都在试验,没有结果。我想要做的是将第一个和第二个位置分配给“区域”类,然后创建区域并检查坐标是否属于它。我也有事件 - 'BlockTouch' 类创建 'ArrowTower' 对象。当我尝试它时,tower.insideRegion(e.getClickedBlock());它给了我我的位置然后归零,因为它没有在 中设置值public void buildArrowTower,但值就在这里。所以我只是不明白为什么这不起作用:/

我的活动课程:

import eu.anavicius.TomTom1997.TomTowerDefence.Towers.ArrowTower;

public class BlockTouch implements Listener {   

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        ArrowTower tower = new ArrowTower();
        if (e.getAction() == Action.RIGHT_CLICK_BLOCK ) {
            if (e.getItem().getType() == Material.ARROW) {
                tower.buildArrowTower(e.getClickedBlock(),e.getPlayer());
            } else if (e.getItem().getType() == Material.BONE) {
                Bukkit.broadcastMessage("Bone");
                tower.insideRegion(e.getClickedBlock());
            }
        }
    }
}

我的 Region.class:

public class Regions {
    private int xl,yl,zl,xh,yh,zh;

    public void setLRegion (int x, int y, int z) {
        xl = x;
        yl = y;
        zl = z;
        //Bukkit.broadcastMessage("SetLMethod" + " \t " +    "|" + xl + "|"+ xh + "||" + "|" + yl + "|" + yh + "||"  + "|" +zl + "|" + zh + "||");
    }

    public void setHRegion (int x, int y, int z) {
        xh = x;
        yh = y;
        zh = z;
        //Bukkit.broadcastMessage("SetHMethod" + " \t " +    "|" + xl + "|"+ xh + "||" + "|" + yl + "|" + yh + "||"  + "|" +zl + "|" + zh + "||");
    }


    public void insideRegion (Block l) {
        int x,y,z;
        x = l.getX();
        y = l.getY();
        z =l.getZ();
        Bukkit.broadcastMessage("InsideMethod" + " \\t " + x  + "|" + xl + "|"+ xh + "||" +y + "|" + yl + "|" + yh + "||" + z + "|" +zl + "|" + zh + "||");

        if (x >= xl && x <= xh ) {
            Bukkit.broadcastMessage("Region check 1");
            if (z >= zl && z <= zh) {
                Bukkit.broadcastMessage("Region check 2");
                if (y >= yl && y >= yh) {
                    Bukkit.broadcastMessage("Regione");
                }
            }
        }

    }
}

我的 ArrowTower.class:

public class ArrowTower extends Regions {

    public ArrowTower () {

    }

    public void buildArrowTower (Block b,Player p) {
        if (b.getType().equals(Material.EMERALD_BLOCK)) {
            Location loc = b.getLocation();
            for (int y = 0; y < 4;y++) {
                int layloc = 0;
                int by = loc.getBlock().getY()+1 + y;
                for (int x = 0;x <3;x++) {
                    int bx = loc.getBlock().getX()-1 + x;
                    for (int z = 0;z < 3;z++) { // Pagalvot dar del delay, nes su kintamaisiais pirma blogai buvau
                        int bz = loc.getBlock().getZ()-1 + z; // sugalvojas, atsispausdina tuscios vietos
                        Location block = new Location(b.getWorld(),bx,by,bz); // pass loop values to method
                        if (y == 0 && layloc == 0) {
                            Bukkit.broadcastMessage("SetR L");
                            setLRegion(bx,by,bz);
                        } else if (y == 3 && layloc == 8) {
                            Bukkit.broadcastMessage("SetR H");
                            setHRegion(bx,by,bz);
                        }
                        block.getBlock().setType(Material.matchMaterial(towerl1(y,layloc)));
                        layloc++;
                    }
                }
            }
        }
    }

    public String towerl1(int h, int layloc) {
        String[] layer1 = { // - l
                "LOG","AIR","LOG",
                "AIR","AIR","AIR",
                "LOG","AIR","LOG"}; 
        String[] layer2 = { // - i
                "COBBLE_WALL","COBBLESTONE","COBBLE_WALL",
                "COBBLESTONE","AIR","COBBLESTONE",
                "COBBLE_WALL","COBBLESTONE","COBBLE_WALL"};
        String[] layer3 = { // - t
                "COBBLE_WALL","AIR","COBBLE_WALL",
                "COBBLE_WALL","MOSSY_COBBLESTONE","COBBLE_WALL",
                "COBBLE_WALL","AIR","COBBLE_WALL"};
        String[] layer4 = {
                "AIR","AIR","AIR",
                "AIR","JACK_O_LANTERN","AIR",
                "AIR","AIR","AIR"};
        if (h == 0) {
            return layer1[layloc];
        } else if (h == 1) {
            return layer2[layloc];
        } else if (h == 2) {
            return layer3[layloc];
        } else if (h == 3) {
            return layer4[layloc];
        } else {
            return null;
        }
    }
}
4

1 回答 1

0

问题是您ArrowTower在每个交互事件上创建一个新对象。我假设您首先右键单击一个块,同时手持箭头 - 这tower.buildArrowTower(e.getClickedBlock(),e.getPlayer());就是被调用的时间。

然后你拿起一根骨头,再次点击——但这一次你的事件处理程序将 ArrowTower用它的第一行创建一个全新的(实际上你甚至没有对第一行的引用,因为它只是在函数的范围):

ArrowTower tower = new ArrowTower();

然后你打电话:

tower.insideRegion(e.getClickedBlock());

但这tower是刚刚创建的 - 实际上它的xh,yh等值从未初始化过。

于 2014-07-16T18:52:48.600 回答