(Java,Bukkit)使用扩展类方法不起作用(Noob oop)

时间:2014-07-16 15:25:53

标签: java oop bukkit extending-classes

我有一个名为' Regions'的类,它创建区域并检查坐标是否在其中。然后我有其他课程#ArrowTower'延伸'地区'。 Class' 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());
            }
        }
    }
}

My 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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

然后你手里拿着一根骨头,然后再次点击 - 但是这次你的事件处理程序会用它的第一行创建一个全新的ArrowTower(实际上你甚至没有引用第一个,因为它只在函数的范围内声明:

ArrowTower tower = new ArrowTower();

然后你打电话:

tower.insideRegion(e.getClickedBlock());

但是这个tower刚刚创建 - 实际上它的xhyh等值尚未初始化。